diff --git a/applications/classification/random_fourier_classification.cpp b/applications/classification/random_fourier_classification.cpp index 148747234a7..89401e48fcb 100644 --- a/applications/classification/random_fourier_classification.cpp +++ b/applications/classification/random_fourier_classification.cpp @@ -108,10 +108,10 @@ int main(int argv, char** argc) /** Creating features */ - CBinaryLabels* labels = new CBinaryLabels(label); + BinaryLabels* labels = new BinaryLabels(label); SG_REF(labels); - CSparseFeatures* s_feats = new CSparseFeatures(sparse_data); + SparseFeatures* s_feats = new SparseFeatures(sparse_data); SGVector params(1); params[0] = width; CRandomFourierDotFeatures* r_feats = new CRandomFourierDotFeatures( @@ -119,11 +119,11 @@ int main(int argv, char** argc) /** 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(); @@ -132,7 +132,7 @@ int main(int argv, char** argc) /** Training completed */ /** Evaluating */ - CBinaryLabels* predicted = svm->apply()->as(); + BinaryLabels* predicted = svm->apply()->as(); CPRCEvaluation* prc_evaluator = new CPRCEvaluation(); CROCEvaluation* roc_evaluator = new CROCEvaluation(); CAccuracyMeasure* accuracy_evaluator = new CAccuracyMeasure(); @@ -152,11 +152,11 @@ int main(int argv, char** argc) sparse_data = load_data(testpath, label_vec); label = SGVector(label_vec, sparse_data.num_vectors); - s_feats = new CSparseFeatures(sparse_data); + s_feats = new SparseFeatures(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(); + predicted = svm->apply(r_feats)->as(); auROC = roc_evaluator->evaluate(predicted, test_labels); auPRC = prc_evaluator->evaluate(predicted, test_labels); accuracy = accuracy_evaluator->evaluate(predicted, test_labels); diff --git a/configs/valgrind.supp b/configs/valgrind.supp index 65d66b6fc09..a24df526cfd 100644 --- a/configs/valgrind.supp +++ b/configs/valgrind.supp @@ -127,5 +127,5 @@ Memcheck:Addr8 fun:__GI___strncasecmp_l fun:____strtod_l_internal - fun:*CParser*read_real* + fun:*Parser*read_real* } diff --git a/doc/OpenCV_docs/OpenCV_KNN_vs_Shogun_KNN.md b/doc/OpenCV_docs/OpenCV_KNN_vs_Shogun_KNN.md index 6746444de25..1d3e7ae791c 100644 --- a/doc/OpenCV_docs/OpenCV_KNN_vs_Shogun_KNN.md +++ b/doc/OpenCV_docs/OpenCV_KNN_vs_Shogun_KNN.md @@ -158,33 +158,33 @@ 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 shogun_traindata = CV2SGFactory::get_sgmatrix(traindata); shogun_traindata = linalg::transpose_matrix(shogun_traindata); - CDenseFeatures* shogun_trainfeatures = new CDenseFeatures(shogun_traindata); + auto shogun_trainfeatures = std::make_shared>(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* shogun_dense_response = + DenseFeatures* shogun_dense_response = CV2SGFactory::get_dense_features(shogun_trainresponse); SGVector 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 shogun_testdata = CV2SGFactory::get_sgmatrix(testdata); shogun_testdata = linalg::transpose_matrix(shogun_testdata); - CDenseFeatures* shogun_testfeatures = new CDenseFeatures(shogun_testdata); + DenseFeatures* shogun_testfeatures = new DenseFeatures(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(); @@ -192,7 +192,7 @@ ___ Test it! ```CPP - CMulticlassLabels* output = knn->apply_multiclass(shogun_testfeatures); + MulticlassLabels* output = knn->apply_multiclass(shogun_testfeatures); SGMatrix multiple_k_output = knn->classify_for_multiple_k(); SGVector sgvec = output->get_labels(); diff --git a/doc/OpenCV_docs/OpenCV_NN_vs_Shogun_NN.md b/doc/OpenCV_docs/OpenCV_NN_vs_Shogun_NN.md index 635474017ce..ebeb5b69657 100644 --- a/doc/OpenCV_docs/OpenCV_NN_vs_Shogun_NN.md +++ b/doc/OpenCV_docs/OpenCV_NN_vs_Shogun_NN.md @@ -224,15 +224,15 @@ As usual, we start with creating a ```DenseFeatures``` object with the training ```CPP SGMatrix shogun_traindata = CV2SGFactory::get_sgmatrix(traindata); shogun_traindata = linalg::transpose_matrix(shogun_traindata); - CDenseFeatures* shogun_trainfeatures = new CDenseFeatures(shogun_traindata); + auto shogun_trainfeatures = std::make_shared>(shogun_traindata); ``` The training responses are in an object of type ```MulticlassLabels```. ```CPP - CDenseFeatures* shogun_dense_response = CV2SGFactory::get_dense_features(shogun_trainresponse); + auto shogun_dense_response = CV2SGFactory::get_dense_features(shogun_trainresponse); SGVector shogun_vector_response = shogun_dense_response->get_feature_vector(0); - CMulticlassLabels* labels = new CMulticlassLabels(shogun_vector_response); + auto labels = std::make_shared(shogun_vector_response); ``` @@ -240,13 +240,13 @@ Prepare the testing data. ```CPP SGMatrix shogun_testdata = CV2SGFactory::get_sgmatrix(testdata); shogun_testdata = linalg::transpose_matrix(shogun_testdata); - CDenseFeatures* testfeatures = new CDenseFeatures(shogun_testdata); + auto testfeatures = std::make_shared>(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()```. @@ -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(); ``` @@ -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 shogun_traindata = CV2SGFactory::get_sgmatrix(traindata); shogun_traindata = linalg::transpose_matrix(shogun_traindata); - CDenseFeatures* shogun_trainfeatures = new CDenseFeatures(shogun_traindata); + DenseFeatures* shogun_trainfeatures = new DenseFeatures(shogun_traindata); ``` Now the training responses as the ```MulticlassLabels```. ```CPP - CDenseFeatures* shogun_dense_response = CV2SGFactory::get_dense_features(trainresponse); + DenseFeatures* shogun_dense_response = CV2SGFactory::get_dense_features(trainresponse); SGVector 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); ``` @@ -222,12 +222,12 @@ Prepare the testing data. ```CPP SGMatrix shogun_testdata=CV2SGFactory::get_sgmatrix(testdata); shogun_testdata = linalg::transpose_matrix(shogun_testdata); - CDenseFeatures* testfeatures=new CDenseFeatures(shogun_testdata); + DenseFeatures* testfeatures=new DenseFeatures(shogun_testdata); ``` Testing Procedure. ```CPP - CMulticlassLabels* results=shogunsvm->apply_multiclass(testfeatures); + MulticlassLabels* results=shogunsvm->apply_multiclass(testfeatures); k=0; for(int i=0; i* Face_features=new CDenseFeatures(Stacked_mats); + DenseFeatures* Face_features=new DenseFeatures(Stacked_mats); SG_REF(Face_features); // We initialise the Preprocessor CPCA of Shogun which performs principal @@ -195,19 +195,19 @@ int main() // we need to have the Densefeature pointer of the finalmat. // finalmat_densefeature_ptr is the lhs. - CDenseFeatures* finalmat_densefeature_ptr=new CDenseFeatures(finalmat); + DenseFeatures* finalmat_densefeature_ptr=new DenseFeatures(finalmat); SG_REF(finalmat_densefeature_ptr); // and similarly we just need to convert the testimage_sgvec into the DenseFeature pointer for the rhs. SGMatrixdata_matrix(testimage_projected_vec.vlen, 1); - CDenseFeatures* testimage_dense=new CDenseFeatures(data_matrix); + DenseFeatures* testimage_dense=new DenseFeatures(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; diff --git a/doc/OpenCV_docs/fisherfaces.cpp b/doc/OpenCV_docs/fisherfaces.cpp index f74356053ca..35aab9bd9fc 100644 --- a/doc/OpenCV_docs/fisherfaces.cpp +++ b/doc/OpenCV_docs/fisherfaces.cpp @@ -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* Face_features=new CDenseFeatures(Stacked_mats); + DenseFeatures* Face_features=new DenseFeatures(Stacked_mats); SG_REF(Face_features) // We initialise the Preprocessor CPCA of Shogun which performs principal @@ -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 labels_vector(size); for (int i=0;i* pca_dense_feat= - new CDenseFeatures(pca_projection); + DenseFeatures* pca_dense_feat= + new DenseFeatures(pca_projection); SG_REF(pca_dense_feat); // Applying the classical Fisher LDA algorithm. @@ -216,17 +216,17 @@ int main() // we need to have the Densefeature pointer of the lda_projection. // It is the lhs. - CDenseFeatures* lhs= - new CDenseFeatures(lda_projection); + DenseFeatures* lhs= + new DenseFeatures(lda_projection); // and similarly we just need to convert the testimage_sgvec into the // DenseFeature pointer for the rhs. SGMatrixdata_matrix(testimage_projected_vec.vlen, 1); - CDenseFeatures* rhs= - new CDenseFeatures(data_matrix); + DenseFeatures* rhs= + new DenseFeatures(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; diff --git a/doc/cookbook/source/examples/base_api/dense_dispatching.rst b/doc/cookbook/source/examples/base_api/dense_dispatching.rst index 60ccef85f47..a31735bc0dc 100644 --- a/doc/cookbook/source/examples/base_api/dense_dispatching.rst +++ b/doc/cookbook/source/examples/base_api/dense_dispatching.rst @@ -3,7 +3,7 @@ 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. ------- @@ -11,7 +11,7 @@ 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 diff --git a/doc/cookbook/source/examples/binary/averaged_perceptron.rst b/doc/cookbook/source/examples/binary/averaged_perceptron.rst index d7a2242a57b..e27062f2e8d 100644 --- a/doc/cookbook/source/examples/binary/averaged_perceptron.rst +++ b/doc/cookbook/source/examples/binary/averaged_perceptron.rst @@ -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 @@ -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 diff --git a/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst b/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst index a3de8568d5d..c9707ca5630 100644 --- a/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst +++ b/doc/cookbook/source/examples/binary/kernel_support_vector_machine.rst @@ -16,7 +16,7 @@ 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. @@ -24,16 +24,16 @@ See :cite:`scholkopf2002learning` and Chapter 6 in :cite:`cristianini2000introdu 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 diff --git a/doc/cookbook/source/examples/binary/linear_discriminant_analysis.rst b/doc/cookbook/source/examples/binary/linear_discriminant_analysis.rst index 705173a1c7f..93b83a0a0d6 100644 --- a/doc/cookbook/source/examples/binary/linear_discriminant_analysis.rst +++ b/doc/cookbook/source/examples/binary/linear_discriminant_analysis.rst @@ -22,7 +22,7 @@ See Chapter 16 in :cite:`barber2012bayesian` for a detailed introduction. Example ------- -We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CBinaryLabels` from files with training and test data. +We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`BinaryLabels` from files with training and test data. .. sgexample:: linear_discriminant_analysis.sg:create_features @@ -30,7 +30,7 @@ We create an instance of the :sgclass:`CLDA` classifier and set features and lab .. sgexample:: linear_discriminant_analysis.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CBinaryLabels`. +Then we train and apply it to test data, which here gives :sgclass:`BinaryLabels`. .. sgexample:: linear_discriminant_analysis.sg:train_and_apply diff --git a/doc/cookbook/source/examples/binary/linear_support_vector_machine.rst b/doc/cookbook/source/examples/binary/linear_support_vector_machine.rst index 52ce6bc9b80..9fd3fa99047 100644 --- a/doc/cookbook/source/examples/binary/linear_support_vector_machine.rst +++ b/doc/cookbook/source/examples/binary/linear_support_vector_machine.rst @@ -24,19 +24,19 @@ See :cite:`fan2008liblinear` and Chapter 6 in :cite:`cristianini2000introduction 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:: linear_support_vector_machine.sg:create_features -In order to run :sgclass:`CLibLinear`, we need to initialize some parameters like :math:`C` and epsilon which is the residual convergence parameter of the solver. +In order to run :sgclass:`LibLinear`, we need to initialize some parameters like :math:`C` and epsilon which is the residual convergence parameter of the solver. .. sgexample:: linear_support_vector_machine.sg:set_parameters -We create an instance of the :sgclass:`CLibLinear` classifier by passing it regularization coefficient, features and labels. We here set the solver type to L2 regularized classification. There are many other solver types in :sgclass:`CLibLinear` to choose from. +We create an instance of the :sgclass:`LibLinear` classifier by passing it regularization coefficient, features and labels. We here set the solver type to L2 regularized classification. There are many other solver types in :sgclass:`LibLinear` to choose from. .. sgexample:: linear_support_vector_machine.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CBinaryLabels`. +Then we train and apply it to test data, which here gives :sgclass:`BinaryLabels`. .. sgexample:: linear_support_vector_machine.sg:train_and_apply diff --git a/doc/cookbook/source/examples/binary/multiple_kernel_learning.rst b/doc/cookbook/source/examples/binary/multiple_kernel_learning.rst index 3ee9b700427..f6e8d1800f8 100644 --- a/doc/cookbook/source/examples/binary/multiple_kernel_learning.rst +++ b/doc/cookbook/source/examples/binary/multiple_kernel_learning.rst @@ -18,19 +18,19 @@ See :cite:`sonnenburg2006large` for more details. 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:: multiple_kernel_learning.sg:create_features -Then we create indvidual kernels like :sgclass:`CPolyKernel` and :sgclass:`CGaussianKernel` which will be later combined in one :sgclass:`CCombinedKernel`. +Then we create indvidual kernels like :sgclass:`CPolyKernel` and :sgclass:`GaussianKernel` which will be later combined in one :sgclass:`CombinedKernel`. .. sgexample:: multiple_kernel_learning.sg:create_kernel -We create an instance of :sgclass:`CCombinedKernel` and append the :sgclass:`CKernel` objects. +We create an instance of :sgclass:`CombinedKernel` and append the :sgclass:`Kernel` objects. .. sgexample:: multiple_kernel_learning.sg:create_combined_train -We create an object of :sgclass:`CMKLClassification`, provide the combined kernel and labels before training it. +We create an object of :sgclass:`MKLClassification`, provide the combined kernel and labels before training it. .. sgexample:: multiple_kernel_learning.sg:train_mkl @@ -38,11 +38,11 @@ After training, we can extract :math:`\beta`, SVM coefficients :math:`\alpha` an .. sgexample:: multiple_kernel_learning.sg:extract_weights -We update the :sgclass:`CCombinedKernel` object for testing data. +We update the :sgclass:`CombinedKernel` object for testing data. .. sgexample:: multiple_kernel_learning.sg:create_combined_test -We set the updated kernel and predict :sgclass:`CBinaryLabels` for test data. +We set the updated kernel and predict :sgclass:`BinaryLabels` for test data. .. sgexample:: multiple_kernel_learning.sg:mkl_apply diff --git a/doc/cookbook/source/examples/binary/perceptron.rst b/doc/cookbook/source/examples/binary/perceptron.rst index 8ec844c7ff2..1d45575bf1e 100644 --- a/doc/cookbook/source/examples/binary/perceptron.rst +++ b/doc/cookbook/source/examples/binary/perceptron.rst @@ -19,7 +19,7 @@ See Chapter 17 in :cite:`barber2012bayesian` 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:: perceptron.sg:create_features @@ -27,11 +27,11 @@ We need to initialize parameters for maximum number of iterations (since percept .. sgexample:: perceptron.sg:set_parameters -We create an instance of the :sgclass:`CPerceptron` classifier by passing it training features and labels. +We create an instance of the :sgclass:`Perceptron` classifier by passing it training features and labels. .. sgexample:: perceptron.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CBinaryLabels`. +Then we train and apply it to test data, which here gives :sgclass:`BinaryLabels`. .. sgexample:: perceptron.sg:train_and_apply diff --git a/doc/cookbook/source/examples/clustering/gaussian_mixture_models.rst b/doc/cookbook/source/examples/clustering/gaussian_mixture_models.rst index 253fb59723e..4d77d23dd9d 100644 --- a/doc/cookbook/source/examples/clustering/gaussian_mixture_models.rst +++ b/doc/cookbook/source/examples/clustering/gaussian_mixture_models.rst @@ -18,7 +18,7 @@ See Chapter 20 in :cite:`barber2012bayesian` for a detailed introduction. Example ------- -We start by creating CDenseFeatures (here 64 bit floats aka RealFeatures) as +We start by creating DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: gaussian_mixture_models.sg:create_features diff --git a/doc/cookbook/source/examples/clustering/hierarchical.rst b/doc/cookbook/source/examples/clustering/hierarchical.rst index 14e72bd206f..5361efd0d8a 100644 --- a/doc/cookbook/source/examples/clustering/hierarchical.rst +++ b/doc/cookbook/source/examples/clustering/hierarchical.rst @@ -12,11 +12,11 @@ We start by constructing a pairwise distance matrix. Then, the clusters of the p Example ------- -Imagine we have files with the training data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as: +Imagine we have files with the training data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as: .. sgexample:: hierarchical.sg:create_features -In order to run :sgclass:`CHierarchical`, we need to choose a distance, for example :sgclass:`CEuclideanDistance`, or other sub-classes of :sgclass:`CDistance`. The distance is initialized with the data we want to classify. +In order to run :sgclass:`CHierarchical`, we need to choose a distance, for example :sgclass:`EuclideanDistance`, or other sub-classes of :sgclass:`Distance`. The distance is initialized with the data we want to classify. .. sgexample:: hierarchical.sg:choose_distance diff --git a/doc/cookbook/source/examples/clustering/kmeans.rst b/doc/cookbook/source/examples/clustering/kmeans.rst index 223f254f701..fade92064ee 100644 --- a/doc/cookbook/source/examples/clustering/kmeans.rst +++ b/doc/cookbook/source/examples/clustering/kmeans.rst @@ -16,16 +16,16 @@ See Chapter 20 in :cite:`barber2012bayesian` for a detailed introduction. ------- Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: kmeans.sg:create_features -In order to run :sgclass:`CKMeans`, we need to choose a distance, for example :sgclass:`CEuclideanDistance`, or other sub-classes of :sgclass:`CDistance`. The distance is initialized with the data we want to classify. +In order to run :sgclass:`KMeans`, we need to choose a distance, for example :sgclass:`EuclideanDistance`, or other sub-classes of :sgclass:`Distance`. The distance is initialized with the data we want to classify. .. sgexample:: kmeans.sg:choose_distance -Once we have chosen a distance, we create an instance of the :sgclass:`CKMeans` classifier. -We explicitly set :math:`k`, the number of clusters we are expecting to have as 3 and pass it to :sgclass:`CKMeans`. In this example, we apply Lloyd's method for `k`-means clustering. +Once we have chosen a distance, we create an instance of the :sgclass:`KMeans` classifier. +We explicitly set :math:`k`, the number of clusters we are expecting to have as 3 and pass it to :sgclass:`KMeans`. In this example, we apply Lloyd's method for `k`-means clustering. .. sgexample:: kmeans.sg:create_instance_lloyd @@ -38,8 +38,8 @@ We can extract centers and radius of each cluster: .. sgexample:: kmeans.sg:extract_centers_and_radius -:sgclass:`CKMeans` also supports mini batch :math:`k`-means clustering. -We can create an instance of :sgclass:`CKMeans` classifier with mini batch :math:`k`-means method by providing the batch size and iteration number. +:sgclass:`KMeans` also supports mini batch :math:`k`-means clustering. +We can create an instance of :sgclass:`KMeans` classifier with mini batch :math:`k`-means method by providing the batch size and iteration number. .. sgexample:: kmeans.sg:create_instance_mb diff --git a/doc/cookbook/source/examples/converter/diffusion_maps.rst b/doc/cookbook/source/examples/converter/diffusion_maps.rst index 53bdffded97..39543539f33 100644 --- a/doc/cookbook/source/examples/converter/diffusion_maps.rst +++ b/doc/cookbook/source/examples/converter/diffusion_maps.rst @@ -17,7 +17,7 @@ For more information see :cite:`Coifman-Lafon2006Diffusionmaps`. Example ------- -We create CDenseFeatures (RealFeatures, here 64 bit float values). +We create DenseFeatures (RealFeatures, here 64 bit float values). .. sgexample:: diffusionmaps.sg:create_features diff --git a/doc/cookbook/source/examples/converter/independent_component_analysis_fast.rst b/doc/cookbook/source/examples/converter/independent_component_analysis_fast.rst index 7b284af702c..1e4d0f1c9ac 100644 --- a/doc/cookbook/source/examples/converter/independent_component_analysis_fast.rst +++ b/doc/cookbook/source/examples/converter/independent_component_analysis_fast.rst @@ -6,13 +6,13 @@ Independent component analysis (ICA) separates a multivariate signal into additi It is typically used for separating superimposed signals. The ICA algorithm presented here is fastICA, see :cite:`hyvarinen2000independent` for details. -There are many other ICA implementations, all based on :sgclass:`CICAConverter` +There are many other ICA implementations, all based on :sgclass:`ICAConverter` ------- Example ------- -Given a dataset which we assume consists of linearly mixed signals, we create CDenseFeatures +Given a dataset which we assume consists of linearly mixed signals, we create DenseFeatures (RealFeatures, here 64 bit float values). .. sgexample:: independent_component_analysis_fast.sg:create_features @@ -29,7 +29,7 @@ We can also extract the estimated mixing matrix. .. sgexample:: independent_component_analysis_fast.sg:extract -:sgclass:`CICAConverter` supports inverse transformation. We can mix signals using the mixing matrix learnt before. +:sgclass:`ICAConverter` supports inverse transformation. We can mix signals using the mixing matrix learnt before. .. sgexample:: independent_component_analysis_fast.sg:inverse_transform diff --git a/doc/cookbook/source/examples/distance/braycurtis.rst b/doc/cookbook/source/examples/distance/braycurtis.rst index f850e693bb9..953c5734a6b 100644 --- a/doc/cookbook/source/examples/distance/braycurtis.rst +++ b/doc/cookbook/source/examples/distance/braycurtis.rst @@ -14,11 +14,11 @@ where :math:`\bf x` and :math:`\bf x'` are :math:`n` dimensional feature vectors Example ------- -Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as +Imagine we have files with data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: braycurtis.sg:create_features -We create an instance of :sgclass:`CBrayCurtisDistance` by passing it :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`CBrayCurtisDistance` by passing it :sgclass:`DenseFeatures`. .. sgexample:: braycurtis.sg:create_instance @@ -26,7 +26,7 @@ The distance matrix can be extracted as follows: .. sgexample:: braycurtis.sg:extract_distance -We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows: +We can use the same instance with new :sgclass:`DenseFeatures` to compute asymmetrical distance as follows: .. sgexample:: braycurtis.sg:refresh_distance diff --git a/doc/cookbook/source/examples/distance/chi_square.rst b/doc/cookbook/source/examples/distance/chi_square.rst index 5b3b2391956..15f4bd67b13 100644 --- a/doc/cookbook/source/examples/distance/chi_square.rst +++ b/doc/cookbook/source/examples/distance/chi_square.rst @@ -13,11 +13,11 @@ This distance is calculated by the equation: Example ------- -We first create some sample data. So we instantiate CDenseFeatures containing the sample data. +We first create some sample data. So we instantiate DenseFeatures containing the sample data. .. sgexample:: chi_square.sg:create_features -We create an instance of :sgclass:`CChiSquareDistance` by passing it the sample data :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`CChiSquareDistance` by passing it the sample data :sgclass:`DenseFeatures`. .. sgexample:: cosine.sg:create_instance @@ -25,7 +25,7 @@ The distance matrix can be extracted as follows: .. sgexample:: chi_square.sg:extract_distance -We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows: +We can use the same instance with new :sgclass:`DenseFeatures` to compute asymmetrical distance as follows: .. sgexample:: chi_square.sg:refresh_distance diff --git a/doc/cookbook/source/examples/distance/cosine.rst b/doc/cookbook/source/examples/distance/cosine.rst index d510754d0c1..f10ba33d633 100644 --- a/doc/cookbook/source/examples/distance/cosine.rst +++ b/doc/cookbook/source/examples/distance/cosine.rst @@ -14,11 +14,11 @@ where where :math:`\Vert \cdot\Vert_2` is the Euclidean norm. Example ------- -Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as +Imagine we have files with data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: cosine.sg:create_features -We create an instance of :sgclass:`CCosineDistance` by passing it :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`CCosineDistance` by passing it :sgclass:`DenseFeatures`. .. sgexample:: cosine.sg:create_instance @@ -26,7 +26,7 @@ The distance matrix can be extracted as follows: .. sgexample:: cosine.sg:extract_distance -We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows: +We can use the same instance with new :sgclass:`DenseFeatures` to compute asymmetrical distance as follows: .. sgexample:: cosine.sg:refresh_distance diff --git a/doc/cookbook/source/examples/distance/euclidean.rst b/doc/cookbook/source/examples/distance/euclidean.rst index f83808a02c9..a769007881a 100644 --- a/doc/cookbook/source/examples/distance/euclidean.rst +++ b/doc/cookbook/source/examples/distance/euclidean.rst @@ -14,11 +14,11 @@ where :math:`\bf x` and :math:`\bf x'` are :math:`d` dimensional feature vectors Example ------- -Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as +Imagine we have files with data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: euclidean.sg:create_features -We create an instance of :sgclass:`CEuclideanDistance` by passing it :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`EuclideanDistance` by passing it :sgclass:`DenseFeatures`. .. sgexample:: euclidean.sg:create_instance @@ -26,7 +26,7 @@ Distance matrix can be extracted as follows: .. sgexample:: euclidean.sg:extract_distance -We can use the same instance with new :sgclass:`CDenseFeatures` to compute distance. +We can use the same instance with new :sgclass:`DenseFeatures` to compute distance. .. sgexample:: euclidean.sg:refresh_distance diff --git a/doc/cookbook/source/examples/distance/mahalanobis.rst b/doc/cookbook/source/examples/distance/mahalanobis.rst index 14c3f89f794..948d5546cc5 100644 --- a/doc/cookbook/source/examples/distance/mahalanobis.rst +++ b/doc/cookbook/source/examples/distance/mahalanobis.rst @@ -12,11 +12,11 @@ The Mahalanobis distance for real valued features computes the distance between Example ------- -Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as +Imagine we have files with data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: mahalanobis.sg:create_features -We create an instance of :sgclass:`CMahalanobisDistance` by passing it :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`MahalanobisDistance` by passing it :sgclass:`DenseFeatures`. .. sgexample:: mahalanobis.sg:create_instance @@ -24,7 +24,7 @@ The distance matrix can be extracted as follows: .. sgexample:: mahalanobis.sg:extract_distance -We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows: +We can use the same instance with new :sgclass:`DenseFeatures` to compute asymmetrical distance as follows: .. sgexample:: mahalanobis.sg:refresh_distance diff --git a/doc/cookbook/source/examples/distance/manhattan.rst b/doc/cookbook/source/examples/distance/manhattan.rst index 114d4e4f99e..ac5d58321f0 100644 --- a/doc/cookbook/source/examples/distance/manhattan.rst +++ b/doc/cookbook/source/examples/distance/manhattan.rst @@ -14,11 +14,11 @@ where :math:`\bf x` and :math:`\bf x'` are :math:`d` dimensional feature vectors Example ------- -Imagine we have files with data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) as +Imagine we have files with data. We create DenseFeatures (here 64 bit floats aka RealFeatures) as .. sgexample:: manhattan.sg:create_features -We create an instance of :sgclass:`CManhattanDistance` by passing it :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`CManhattanDistance` by passing it :sgclass:`DenseFeatures`. .. sgexample:: manhattan.sg:create_instance @@ -26,7 +26,7 @@ The distance matrix can be extracted as follows: .. sgexample:: manhattan.sg:extract_distance -We can use the same instance with new :sgclass:`CDenseFeatures` to compute asymmetrical distance as follows: +We can use the same instance with new :sgclass:`DenseFeatures` to compute asymmetrical distance as follows: .. sgexample:: manhattan.sg:refresh_distance diff --git a/doc/cookbook/source/examples/evaluation/cross_validation.rst b/doc/cookbook/source/examples/evaluation/cross_validation.rst index 2569292a0a0..28eb161b24d 100644 --- a/doc/cookbook/source/examples/evaluation/cross_validation.rst +++ b/doc/cookbook/source/examples/evaluation/cross_validation.rst @@ -9,11 +9,11 @@ The process is repeated for several complementary partitions and all validation One type of cross validation is :math:`k-`fold cross validation. Suppose we have the training data :math:`\mathbf{x}_1,\mathbf{x}_2, ..., \mathbf{x}_{N}` for a positive integer :math:`N`. :math:`k-`fold cross validation partitions the data into :math:`P_1, P_2, ..., P_n` each with approximately equal size. -Next, we train the model on :math:`P_1, P_2, ...,P_{n-1}` and test it on :math:`P_n` to get performance measure :math:`a_1`, for example :sgclass:`CAccuracyMeasure`, or :sgclass:`CMeanSquaredError`. +Next, we train the model on :math:`P_1, P_2, ...,P_{n-1}` and test it on :math:`P_n` to get performance measure :math:`a_1`, for example :sgclass:`CAccuracyMeasure`, or :sgclass:`MeanSquaredError`. Finally we train the model again from scratch on :math:`P_1, P_2, ...,P_{n-2}, P_{n}` and test it on :math:`P_{n-1}` to get :math:`a_2`. This process is repeated until we have :math:`a_1, a_2, ..., a_n`, and then we can compute their mean and standard deviation. -We will perform :math:`k-` fold cross validation with both a classification model (:sgclass:`CLibLinear`) and a regression model (:sgclass:` CLinearRidgeRegression`). +We will perform :math:`k-` fold cross validation with both a classification model (:sgclass:`LibLinear`) and a regression model (:sgclass:` CLinearRidgeRegression`). ------------------------------ Example for a binary classifier @@ -25,15 +25,15 @@ Firstly, we import the training data, training labels, test data, and test label .. sgexample:: cross_validation.sg:create_features -Next,we initialize a splitting strategy :sgclass:`CStratifiedCrossValidationSplitting`, which is needed to divide the dataset into :math:`k-`folds for the :math:`k-` fold cross validation. +Next,we initialize a splitting strategy :sgclass:`StratifiedCrossValidationSplitting`, which is needed to divide the dataset into :math:`k-`folds for the :math:`k-` fold cross validation. We also have to decide on an evaluation criterion class (from :sgclass:`CEvaluation`) to evaluate the performance of the trained models. In this case, we use :sgclass:`CAccuracyMeasure`. -We then instantiate :sgclass:`CCrossValidation` and set the number of cross validation's runs. +We then instantiate :sgclass:`CrossValidation` and set the number of cross validation's runs. .. sgexample:: cross_validation.sg:create_cross_validation -Finally, we evaluate the model and get the results (a :sgclass:`CCrossValidationResult` instance). +Finally, we evaluate the model and get the results (a :sgclass:`CrossValidationResult` instance). We can also get the mean of all evaluation results and their standard deviation. .. sgexample:: cross_validation.sg:evaluate_and_get_result @@ -52,8 +52,8 @@ Example for regression We will use the linear ridge regression model. (see :doc:`../regression/linear_ridge_regression` for a more complete example of linear ridge regression usage). Firstly, we import the training data in the same way done above. -Next, we choose :sgclass:`CCrossValidationSplitting` as a splitting strategy (:sgclass:`CStratifiedCrossValidationSplitting` only makes sense for classification problems). -Here, we use :sgclass:`CMeanSquaredError` as an evaluation criterion. +Next, we choose :sgclass:`CrossValidationSplitting` as a splitting strategy (:sgclass:`StratifiedCrossValidationSplitting` only makes sense for classification problems). +Here, we use :sgclass:`MeanSquaredError` as an evaluation criterion. .. sgexample:: cross_validation.sg:create_cross_validation_REGRESSION diff --git a/doc/cookbook/source/examples/evaluation/cross_validation_multiple_kernel_learning_weights_storage.rst b/doc/cookbook/source/examples/evaluation/cross_validation_multiple_kernel_learning_weights_storage.rst index fae1306ecc7..c8f9b5c3cb3 100644 --- a/doc/cookbook/source/examples/evaluation/cross_validation_multiple_kernel_learning_weights_storage.rst +++ b/doc/cookbook/source/examples/evaluation/cross_validation_multiple_kernel_learning_weights_storage.rst @@ -8,25 +8,25 @@ For more information on cross-validation, check :doc:`./cross_validation`. ------- Example ------- -We'll use as example a classification problem solvable by using :sgclass:`CMKLClassification`. +We'll use as example a classification problem solvable by using :sgclass:`MKLClassification`. For the sake of brevity, we'll skip the initialization of features, kernels and so on (see :doc:`../regression/multiple_kernel_learning` for a more complete example of MKL usage). .. sgexample:: cross_validation_multiple_kernel_learning_weights_storage.sg:create_classifier -Firstly, we initialize a splitting strategy :sgclass:`CStratifiedCrossValidationSplitting`, which is needed +Firstly, we initialize a splitting strategy :sgclass:`StratifiedCrossValidationSplitting`, which is needed to divide the dataset into folds, and an evaluation criterium :sgclass:`CAccuracyMeasure`, to evaluate the -performance of the trained models. Secondly, we create the :sgclass:`CCrossValidation` instance. +performance of the trained models. Secondly, we create the :sgclass:`CrossValidation` instance. We set also the number of cross validation's runs. .. sgexample:: cross_validation_multiple_kernel_learning_weights_storage.sg:create_cross_validation -To observe also the partial folds' results, we create a cross validation's observer :sgclass:`CParameterObserverCV` -and then we register it into the :sgclass:`CCrossValidation` instance. +To observe also the partial folds' results, we create a cross validation's observer :sgclass:`ParameterObserverCV` +and then we register it into the :sgclass:`CrossValidation` instance. .. sgexample:: cross_validation_multiple_kernel_learning_weights_storage.sg:create_observer -Finally, we evaluate the model and get the results (aka a :sgclass:`CCrossValidationResult` instance). +Finally, we evaluate the model and get the results (aka a :sgclass:`CrossValidationResult` instance). .. sgexample:: cross_validation_multiple_kernel_learning_weights_storage.sg:evaluate_and_get_result @@ -35,7 +35,7 @@ We get the :math:`mean` of all the evaluation results and its standard deviation .. sgexample:: cross_validation_multiple_kernel_learning_weights_storage.sg:get_results We can get more information about the single cross validation's runs and folds by using the observer we set before, like the kernels' weights. -We get the :sgclass:`CMKLClassification` machine used during the first run and trained on the first fold. +We get the :sgclass:`MKLClassification` machine used during the first run and trained on the first fold. .. sgexample:: cross_validation_multiple_kernel_learning_weights_storage.sg:get_fold_machine diff --git a/doc/cookbook/source/examples/evaluation/cross_validation_pipeline.rst b/doc/cookbook/source/examples/evaluation/cross_validation_pipeline.rst index c5c6ffd45af..0c2ecf1c714 100644 --- a/doc/cookbook/source/examples/evaluation/cross_validation_pipeline.rst +++ b/doc/cookbook/source/examples/evaluation/cross_validation_pipeline.rst @@ -9,9 +9,9 @@ For more information on cross-validation, check :doc:`./cross_validation`. ------- Example ------- -We demonstrate a pipeline consisting of a transformer :sgclass:`CPruneVarSubMean` for normalizing the features, and a machine :sgclass:`CLibLinear` for binary classification. +We demonstrate a pipeline consisting of a transformer :sgclass:`CPruneVarSubMean` for normalizing the features, and a machine :sgclass:`LibLinear` for binary classification. -We create :sgclass:`CFeatures` and :sgclass:`CLabels` via loading from files +We create :sgclass:`Features` and :sgclass:`Labels` via loading from files .. sgexample:: cross_validation_pipeline:create_features @@ -19,15 +19,15 @@ We first chain transformers, and then finalize the pipeline with the classifier. .. sgexample:: cross_validation_pipeline:create_pipeline -Next, we initialize a splitting strategy :sgclass:`CStratifiedCrossValidationSplitting` to divide the dataset into :math:`k-` folds for the :math:`k-` fold cross validation. +Next, we initialize a splitting strategy :sgclass:`StratifiedCrossValidationSplitting` to divide the dataset into :math:`k-` folds for the :math:`k-` fold cross validation. We also have to decide on an evaluation criterion class (see :sgclass:`CEvaluation`) to evaluate the performance of the trained model. In this case, we use :sgclass:`CAccuracyMeasure`. -We then instantiate :sgclass:`CCrossValidation` and set the number of cross validation's runs. +We then instantiate :sgclass:`CrossValidation` and set the number of cross validation's runs. We next create a cross-validation instance and pass the generated pipeline. .. sgexample:: cross_validation_pipeline:create_cross_validation -Finally, we evaluate the model and get the results (aka a :sgclass:`CCrossValidationResult` instance). +Finally, we evaluate the model and get the results (aka a :sgclass:`CrossValidationResult` instance). .. sgexample:: cross_validation_pipeline:evaluate_and_get_result diff --git a/doc/cookbook/source/examples/gaussian_process/classifier.rst b/doc/cookbook/source/examples/gaussian_process/classifier.rst index ad8266f3a7b..de2f5982955 100644 --- a/doc/cookbook/source/examples/gaussian_process/classifier.rst +++ b/doc/cookbook/source/examples/gaussian_process/classifier.rst @@ -11,26 +11,26 @@ and :cite:`Rasmussen2005GPM` for more information on Gaussian processes. Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: classifier.sg:create_features -To fit the input (training) data :math:`\mathbf{X}`, we have to choose appropriate :sgclass:`CMeanFunction` -and :sgclass:`CKernel`. Here we use a basic :sgclass:`CConstMean` and a :sgclass:`CGaussianKernel` with chosen width parameter. +To fit the input (training) data :math:`\mathbf{X}`, we have to choose appropriate :sgclass:`MeanFunction` +and :sgclass:`Kernel`. Here we use a basic :sgclass:`ConstMean` and a :sgclass:`GaussianKernel` with chosen width parameter. .. sgexample:: classifier.sg:create_appropriate_kernel_and_mean_function We need to specify the inference method to find the posterior distribution of the function values :math:`\mathbf{f}`. -Here we choose to perform Laplace approximation inference method with an instance of :sgclass:`CMultiLaplaceInferenceMethod` (See Chapter 18.2 in :cite:`barber2012bayesian` for a detailed introduction) +Here we choose to perform Laplace approximation inference method with an instance of :sgclass:`MultiLaplaceInferenceMethod` (See Chapter 18.2 in :cite:`barber2012bayesian` for a detailed introduction) and pass it the chosen kernel, -the training features, the mean function, the labels and an instance of :sgclass:`CSoftMaxLikelihood`, +the training features, the mean function, the labels and an instance of :sgclass:`SoftMaxLikelihood`, to specify the distribution of the targets/labels as above. -Finally we create an instance of the :sgclass:`CGaussianProcessClassification` classifier. +Finally we create an instance of the :sgclass:`GaussianProcessClassification` classifier. .. sgexample:: classifier.sg:create_instance Then we can train the model and evaluate the predictive distribution. -We get predicted :sgclass:`CMulticlassLabels`. +We get predicted :sgclass:`MulticlassLabels`. .. sgexample:: classifier.sg:train_and_apply @@ -38,7 +38,7 @@ We can extract the probabilities: .. sgexample:: classifier.sg:extract_the_probabilities -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: classifier.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/gaussian_process/regression.rst b/doc/cookbook/source/examples/gaussian_process/regression.rst index 3a6522877d2..d215a0fef67 100644 --- a/doc/cookbook/source/examples/gaussian_process/regression.rst +++ b/doc/cookbook/source/examples/gaussian_process/regression.rst @@ -22,19 +22,19 @@ See :cite:`Rasmussen2005GPM` for a comprehensive treatment of Gaussian Processes Example ------- -Imagine we have files with training and test data. We create `CDenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as: +Imagine we have files with training and test data. We create `DenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as: .. sgexample:: regression.sg:create_features -To fit the input (training) data :math:`\mathbf{X}`, we have to choose an appropriate :sgclass:`CMeanFunction` and :sgclass:`CKernel` and instantiate them. Here we use a basic :sgclass:`CZeroMean` and a :sgclass:`CGaussianKernel` with chosen width parameter. +To fit the input (training) data :math:`\mathbf{X}`, we have to choose an appropriate :sgclass:`MeanFunction` and :sgclass:`Kernel` and instantiate them. Here we use a basic :sgclass:`ZeroMean` and a :sgclass:`GaussianKernel` with chosen width parameter. .. sgexample:: regression.sg:create_appropriate_kernel_and_mean_function -We need to specify the inference method to find the posterior distribution of the function values :math:`\mathbf{f}`. Here we choose to perform exact inference with an instance of :sgclass:`CExactInferenceMethod` and pass it the chosen kernel, the training features, the mean function, the labels and an instance of :sgclass:`CGaussianLikelihood`, to specify the distribution of the targets/labels as above. Finally we generate a CGaussianProcessRegression class to be trained. +We need to specify the inference method to find the posterior distribution of the function values :math:`\mathbf{f}`. Here we choose to perform exact inference with an instance of :sgclass:`ExactInferenceMethod` and pass it the chosen kernel, the training features, the mean function, the labels and an instance of :sgclass:`GaussianLikelihood`, to specify the distribution of the targets/labels as above. Finally we generate a GaussianProcessRegression class to be trained. .. sgexample:: regression.sg:create_instance -Then we can train the model and evaluate the predictive distribution. We get predicted :sgclass:`CRegressionLabels`. +Then we can train the model and evaluate the predictive distribution. We get predicted :sgclass:`RegressionLabels`. .. sgexample:: regression.sg:train_and_apply @@ -43,11 +43,11 @@ We can compute the predictive variances as .. sgexample:: regression.sg:compute_variance The prediction above is based on arbitrarily set hyperparameters :math:`\boldsymbol{\theta}`: kernel width :math:`\tau`, kernel scaling :math:`\gamma` and observation noise :math:`\sigma^2`. We can also learn these parameters by optimizing the marginal likelihood :math:`p(\mathbf{y}|\mathbf{X}, \boldsymbol{\theta})` w.r.t. :math:`\boldsymbol{\theta}`. -To do this, we define a :sgclass:`CGradientModelSelection`, passing to it a :sgclass:`CGradientEvaluation` with its own :sgclass:`CGradientCriterion`, specifying the gradient scheme and direction. Then we can follow the gradient and apply the chosen :math:`\boldsymbol{\theta}` back to the CGaussianProcessRegression instance. +To do this, we define a :sgclass:`CGradientModelSelection`, passing to it a :sgclass:`CGradientEvaluation` with its own :sgclass:`CGradientCriterion`, specifying the gradient scheme and direction. Then we can follow the gradient and apply the chosen :math:`\boldsymbol{\theta}` back to the GaussianProcessRegression instance. .. sgexample:: regression.sg:optimize_marginal_likelihood -Finally, we evaluate the :sgclass:`CMeanSquaredError` and the (negative log) marginal likelihood for the optimized hyperparameters. +Finally, we evaluate the :sgclass:`MeanSquaredError` and the (negative log) marginal likelihood for the optimized hyperparameters. .. sgexample:: regression.sg:evaluate_error_and_marginal_likelihood diff --git a/doc/cookbook/source/examples/gaussian_process/sparse_regression.rst b/doc/cookbook/source/examples/gaussian_process/sparse_regression.rst index 5aabfa0ea26..db1d505312d 100644 --- a/doc/cookbook/source/examples/gaussian_process/sparse_regression.rst +++ b/doc/cookbook/source/examples/gaussian_process/sparse_regression.rst @@ -16,23 +16,23 @@ See :cite:`Quinonero-Candela2005` for detailed overview of Sparse approximate Ga Example ------- -Imagine we have files with training and test data. We create `CDenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as: +Imagine we have files with training and test data. We create `DenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as: .. sgexample:: sparse_regression.sg:create_features -To fit the input (training) data :math:`\mathbf{X}`, we have to choose an appropriate :sgclass:`CMeanFunction` and :sgclass:`CKernel` and instantiate them. Here we use a basic :sgclass:`CZeroMean` and a :sgclass:`CGaussianKernel` with chosen width parameter. +To fit the input (training) data :math:`\mathbf{X}`, we have to choose an appropriate :sgclass:`MeanFunction` and :sgclass:`Kernel` and instantiate them. Here we use a basic :sgclass:`ZeroMean` and a :sgclass:`GaussianKernel` with chosen width parameter. .. sgexample:: sparse_regression.sg:create_kernel_and_mean_function -We need to specify the inference method to find the posterior distribution of the function values :math:`\mathbf{f}`. Here we choose to perform variational inference for fully independent conditional training (FITC) with an instance of :sgclass:`CFITCInferenceMethod`. We use another feature instance for inducing points and add a simple subset for demonstration. The inference method is then created and we pass it the chosen kernel, the training features, the mean function, the labels, an instance of :sgclass:`CGaussianLikelihood`. We use a subset of the training data for inducing features. +We need to specify the inference method to find the posterior distribution of the function values :math:`\mathbf{f}`. Here we choose to perform variational inference for fully independent conditional training (FITC) with an instance of :sgclass:`FITCInferenceMethod`. We use another feature instance for inducing points and add a simple subset for demonstration. The inference method is then created and we pass it the chosen kernel, the training features, the mean function, the labels, an instance of :sgclass:`GaussianLikelihood`. We use a subset of the training data for inducing features. .. sgexample:: sparse_regression.sg:create_inference -Finally we generate a :sgclass:`CGaussianProcessRegression` class to be trained. +Finally we generate a :sgclass:`GaussianProcessRegression` class to be trained. .. sgexample:: sparse_regression.sg:create_instance -Then we can train the model and evaluate the predictive distribution. We get predicted :sgclass:`CRegressionLabels`. +Then we can train the model and evaluate the predictive distribution. We get predicted :sgclass:`RegressionLabels`. .. sgexample:: sparse_regression.sg:train_and_apply @@ -40,7 +40,7 @@ We can compute the predictive variances as .. sgexample:: sparse_regression.sg:compute_variance -Finally, we evaluate the :sgclass:`CMeanSquaredError`. +Finally, we evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: sparse_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/kernel/cauchy.rst b/doc/cookbook/source/examples/kernel/cauchy.rst index b5af118ac52..437b2dcab50 100644 --- a/doc/cookbook/source/examples/kernel/cauchy.rst +++ b/doc/cookbook/source/examples/kernel/cauchy.rst @@ -8,27 +8,27 @@ It is a long-tailed kernel defined as .. math:: k(x,x') = \frac{1}{1+\frac{\| x-x' \|^2}{\sigma}} -, where :math:`|x-x'|` is the distance between two feature vectors given by an instance of :sgclass:`CDistance`. +, where :math:`|x-x'|` is the distance between two feature vectors given by an instance of :sgclass:`Distance`. ------- Example ------- Imagine we have files with data. -We create :sgclass:`CDenseFeatures` (here 64 bit floats aka RealFeatures) as +We create :sgclass:`DenseFeatures` (here 64 bit floats aka RealFeatures) as .. sgexample:: cauchy:create_features -We create an instance of :sgclass:`CEuclideanDistance` and initialize with :sgclass:`CDenseFeatures`. +We create an instance of :sgclass:`EuclideanDistance` and initialize with :sgclass:`DenseFeatures`. Then we create an instance of :sgclass:`CCauchyKernel` with the distance. .. sgexample:: cauchy:create_kernel -We initialize the kernel with :sgclass:`CDenseFeatures`. +We initialize the kernel with :sgclass:`DenseFeatures`. The kernel matrix can be extracted as follows: .. sgexample:: cauchy:kernel_matrix_train -We can use the same instance with new :sgclass:`CDenseFeatures` to compute the kernel matrix between training features and testing features. +We can use the same instance with new :sgclass:`DenseFeatures` to compute the kernel matrix between training features and testing features. .. sgexample:: cauchy:kernel_matrix_test diff --git a/doc/cookbook/source/examples/kernel/custom_kernel.rst b/doc/cookbook/source/examples/kernel/custom_kernel.rst index d8e58cf101a..f70a0d34120 100644 --- a/doc/cookbook/source/examples/kernel/custom_kernel.rst +++ b/doc/cookbook/source/examples/kernel/custom_kernel.rst @@ -7,15 +7,15 @@ The Custom Kernel allows for custom user provided kernel matrices. ------- Example ------- -In this example, we initialize a :sgclass:`CGaussianKernel` and then use the kernel matrix to create a custom kernel. +In this example, we initialize a :sgclass:`GaussianKernel` and then use the kernel matrix to create a custom kernel. Imagine we have files with data. -We create :sgclass:`CDenseFeatures` (here 64 bit floats aka RealFeatures) as +We create :sgclass:`DenseFeatures` (here 64 bit floats aka RealFeatures) as .. sgexample:: custom_kernel_machine:create_features -We create an instance of :sgclass:`CGaussianKernel`. -We initialize with :sgclass:`CDenseFeatures` and then get the kernel matrix. +We create an instance of :sgclass:`GaussianKernel`. +We initialize with :sgclass:`DenseFeatures` and then get the kernel matrix. .. sgexample:: custom_kernel_machine:create_kernel @@ -27,7 +27,7 @@ We create an instance of :sgclass:`CLibSVM` with the custom kernel. .. sgexample:: custom_kernel_machine:create_machine -Then we train the :sgclass:`CLibSVM` and we apply it to the test data, which gives the predicted :sgclass:`CLabels`. +Then we train the :sgclass:`CLibSVM` and we apply it to the test data, which gives the predicted :sgclass:`Labels`. .. sgexample:: custom_kernel_machine:train_and_apply diff --git a/doc/cookbook/source/examples/multiclass/cartree.rst b/doc/cookbook/source/examples/multiclass/cartree.rst index 5fc90b8c8f1..43fbade3bef 100644 --- a/doc/cookbook/source/examples/multiclass/cartree.rst +++ b/doc/cookbook/source/examples/multiclass/cartree.rst @@ -17,7 +17,7 @@ In this example, we showed how to apply CART algorithm to multi-class dataset an Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: cartree.sg:create_features @@ -25,16 +25,16 @@ We set the type of each predictive attribute (true for nominal, false for ordina .. sgexample:: cartree.sg:set_attribute_types -We create an instance of the :sgclass:`CCARTree` classifier by passting it the attribute types and the tree type. +We create an instance of the :sgclass:`CARTree` classifier by passting it the attribute types and the tree type. We can also set the number of subsets used in cross-valiation and whether to use cross-validation pruning. .. sgexample:: cartree.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: cartree.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: cartree.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/chaid_tree.rst b/doc/cookbook/source/examples/multiclass/chaid_tree.rst index 5e0bd8b93e3..0e418bc2328 100644 --- a/doc/cookbook/source/examples/multiclass/chaid_tree.rst +++ b/doc/cookbook/source/examples/multiclass/chaid_tree.rst @@ -11,7 +11,7 @@ they have to be transformed into ordinal predictors by binning before tree growi Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: chaid_tree.sg:create_features @@ -25,11 +25,11 @@ For continuous predictors, the user has to provide the number of bins for contin .. sgexample:: chaid_tree.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: chaid_tree.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: chaid_tree.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/ecoc_random.rst b/doc/cookbook/source/examples/multiclass/ecoc_random.rst index 44cacfece74..9773643fcda 100644 --- a/doc/cookbook/source/examples/multiclass/ecoc_random.rst +++ b/doc/cookbook/source/examples/multiclass/ecoc_random.rst @@ -22,11 +22,11 @@ See :cite:`escalera2010error` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: ecoc_random.sg:create_features -We use :sgclass:`CLibLinear` as base classifier and create an instance of :sgclass:`CLibLinear`. +We use :sgclass:`LibLinear` as base classifier and create an instance of :sgclass:`LibLinear`. (`See the linear SVM cookbook `_) .. sgexample:: ecoc_random.sg:create_classifier @@ -35,15 +35,15 @@ We initialize :sgclass:`CECOCStrategy` by specifying the random dense encoder an .. sgexample:: ecoc_random.sg:choose_strategy -We create an instance of the :sgclass:`CLinearMulticlassMachine` classifier by passing it the ECOC strategies, together with the dataset, binary classifier and the labels. +We create an instance of the :sgclass:`LinearMulticlassMachine` classifier by passing it the ECOC strategies, together with the dataset, binary classifier and the labels. .. sgexample:: ecoc_random.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: ecoc_random.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: ecoc_random.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/gaussian_naive_bayes.rst b/doc/cookbook/source/examples/multiclass/gaussian_naive_bayes.rst index 70a890fe4c9..1beb2c787f6 100644 --- a/doc/cookbook/source/examples/multiclass/gaussian_naive_bayes.rst +++ b/doc/cookbook/source/examples/multiclass/gaussian_naive_bayes.rst @@ -22,15 +22,15 @@ See Chapter 10 in :cite:`barber2012bayesian` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: gaussian_naive_bayes.sg:create_features -We create an instance of the :sgclass:`CGaussianNaiveBayes` classifier, passing it training data and the label. +We create an instance of the :sgclass:`GaussianNaiveBayes` classifier, passing it training data and the label. .. sgexample:: gaussian_naive_bayes.sg:create_instance -Then we run the train Gaussian Naive Bayes algorithm and apply it to the test data, which here gives `CMulticlassLabels` +Then we run the train Gaussian Naive Bayes algorithm and apply it to the test data, which here gives `MulticlassLabels` .. sgexample:: gaussian_naive_bayes.sg:train_and_apply diff --git a/doc/cookbook/source/examples/multiclass/k_nearest_neighbours.rst b/doc/cookbook/source/examples/multiclass/k_nearest_neighbours.rst index 58d6c6f555d..7871244a226 100644 --- a/doc/cookbook/source/examples/multiclass/k_nearest_neighbours.rst +++ b/doc/cookbook/source/examples/multiclass/k_nearest_neighbours.rst @@ -16,23 +16,23 @@ See Chapter 14 in :cite:`barber2012bayesian` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: k_nearest_neighbours.sg:create_features -In order to run :sgclass:`CKNN`, we need to choose a distance, for example :sgclass:`CEuclideanDistance`, or other sub-classes of :sgclass:`CDistance`. The distance is initialized with the data we want to classify. +In order to run :sgclass:`KNN`, we need to choose a distance, for example :sgclass:`EuclideanDistance`, or other sub-classes of :sgclass:`Distance`. The distance is initialized with the data we want to classify. .. sgexample:: k_nearest_neighbours.sg:choose_distance -Once we have chosen a distance, we create an instance of the :sgclass:`CKNN` classifier, passing it :math:`k`. +Once we have chosen a distance, we create an instance of the :sgclass:`KNN` classifier, passing it :math:`k`. .. sgexample:: k_nearest_neighbours.sg:create_instance -Then we run the train KNN algorithm and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we run the train KNN algorithm and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: k_nearest_neighbours.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: k_nearest_neighbours.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/large_margin_nearest_neighbours.rst b/doc/cookbook/source/examples/multiclass/large_margin_nearest_neighbours.rst index 52577680aff..62eb645906e 100644 --- a/doc/cookbook/source/examples/multiclass/large_margin_nearest_neighbours.rst +++ b/doc/cookbook/source/examples/multiclass/large_margin_nearest_neighbours.rst @@ -12,11 +12,11 @@ See :cite:`weinberger2009distance` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: large_margin_nearest_neighbours.sg:create_features -We create an instance of :sgclass:`CLMNN` and provide number of nearest neighbours as parameters. +We create an instance of :sgclass:`LMNN` and provide number of nearest neighbours as parameters. .. sgexample:: large_margin_nearest_neighbours.sg:create_instance @@ -24,11 +24,11 @@ Next we train the LMNN algorithm and get the learned metric. .. sgexample:: large_margin_nearest_neighbours.sg:train_metric -Then we train the :sgclass:`CKNN` algorithm using the learned metric and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train the :sgclass:`KNN` algorithm using the learned metric and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: large_margin_nearest_neighbours.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: large_margin_nearest_neighbours.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/linear.rst b/doc/cookbook/source/examples/multiclass/linear.rst index f88c23d7e08..a05eb920fd2 100644 --- a/doc/cookbook/source/examples/multiclass/linear.rst +++ b/doc/cookbook/source/examples/multiclass/linear.rst @@ -4,35 +4,35 @@ Multi-class Linear Machine We extend the application of linear machines to multi-class datasets by constructing generic multiclass classifiers with ensembles of binary classifiers. -In this example, we show how to apply :sgclass:`CLibLinear` to multi-class cases with :sgclass:`CLinearMulticlassMachine`. +In this example, we show how to apply :sgclass:`LibLinear` to multi-class cases with :sgclass:`LinearMulticlassMachine`. -`See the linear SVM cookbook `_ for the infomration about :sgclass:`CLibLinear` binary classifier. +`See the linear SVM cookbook `_ for the infomration about :sgclass:`LibLinear` binary classifier. ------- Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: linear.sg:create_features -We use :sgclass:`CLibLinear` as base classifier and create an instance of :sgclass:`CLibLinear`. +We use :sgclass:`LibLinear` as base classifier and create an instance of :sgclass:`LibLinear`. .. sgexample:: linear.sg:create_classifier -In order to run :sgclass:`CLinearMulticlassMachine`, we need to specify an multi-class strategy from :sgclass:`CMulticlassOneVsRestStrategy` and :sgclass:`CMulticlassOneVsOneStrategy`. +In order to run :sgclass:`LinearMulticlassMachine`, we need to specify an multi-class strategy from :sgclass:`MulticlassOneVsRestStrategy` and :sgclass:`MulticlassOneVsOneStrategy`. .. sgexample:: linear.sg:choose_strategy -We create an instance of the :sgclass:`CLinearMulticlassMachine` classifier by passing it the strategy, dataset, binary classifer and the labels. +We create an instance of the :sgclass:`LinearMulticlassMachine` classifier by passing it the strategy, dataset, binary classifer and the labels. .. sgexample:: linear.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: linear.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: linear.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/linear_discriminant_analysis.rst b/doc/cookbook/source/examples/multiclass/linear_discriminant_analysis.rst index 0e29f66d5ff..99aacdae196 100644 --- a/doc/cookbook/source/examples/multiclass/linear_discriminant_analysis.rst +++ b/doc/cookbook/source/examples/multiclass/linear_discriminant_analysis.rst @@ -10,16 +10,16 @@ to multi-class classifications. Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: linear_discriminant_analysis.sg:create_features -We create an instance of the :sgclass:`CMCLDA` classifier with feature matrix and label list. -:sgclass:`CMCLDA` also has two default parameters, to set tolerance used in training and mark whether to store the within class covariances. +We create an instance of the :sgclass:`MCLDA` classifier with feature matrix and label list. +:sgclass:`MCLDA` also has two default parameters, to set tolerance used in training and mark whether to store the within class covariances. .. sgexample:: linear_discriminant_analysis.sg:create_instance -Then we train and apply it to the test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to the test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: linear_discriminant_analysis.sg:train_and_apply @@ -28,7 +28,7 @@ If we enabled storing covariance when creating instances, we can also extract th .. sgexample:: linear_discriminant_analysis.sg:extract_mean_and_cov -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: linear_discriminant_analysis.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/logistic_regression.rst b/doc/cookbook/source/examples/multiclass/logistic_regression.rst index a3bf8c036c5..b7871e5202f 100644 --- a/doc/cookbook/source/examples/multiclass/logistic_regression.rst +++ b/doc/cookbook/source/examples/multiclass/logistic_regression.rst @@ -25,7 +25,7 @@ where :math:`\mathbf{a}_{ik}` denotes the :math:`i`-th sample for the :math:`k`- Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: logistic_regression.sg:create_features @@ -33,11 +33,11 @@ We create an instance of the :sgclass:`CMulticlassLogisticRegression` classifier .. sgexample:: logistic_regression.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: logistic_regression.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: logistic_regression.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/quadratic_discriminant_analysis.rst b/doc/cookbook/source/examples/multiclass/quadratic_discriminant_analysis.rst index 6aafdc05fb2..73786f885dc 100644 --- a/doc/cookbook/source/examples/multiclass/quadratic_discriminant_analysis.rst +++ b/doc/cookbook/source/examples/multiclass/quadratic_discriminant_analysis.rst @@ -15,17 +15,17 @@ QDA is a generalization of linear discriminant analysis (LDA). See Chapter 16 in Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: quadratic_discriminant_analysis.sg:create_features -We create an instance of the :sgclass:`CQDA` classifier with feature matrix and label list. -:sgclass:`CQDA` also has two default arguments, to set tolerance used in training and mark whether to store the within class covariances +We create an instance of the :sgclass:`QDA` classifier with feature matrix and label list. +:sgclass:`QDA` also has two default arguments, to set tolerance used in training and mark whether to store the within class covariances .. sgexample:: quadratic_discriminant_analysis.sg:create_instance -We run the train QDA algorithm and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +We run the train QDA algorithm and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: quadratic_discriminant_analysis.sg:train_and_apply @@ -34,7 +34,7 @@ If we enabled storing covariance when creating instances, we can also extract th .. sgexample:: quadratic_discriminant_analysis.sg:extract_mean_and_cov -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: quadratic_discriminant_analysis.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/random_forest.rst b/doc/cookbook/source/examples/multiclass/random_forest.rst index 4d9ee742ffc..1b4a7934a67 100644 --- a/doc/cookbook/source/examples/multiclass/random_forest.rst +++ b/doc/cookbook/source/examples/multiclass/random_forest.rst @@ -10,11 +10,11 @@ See :cite:`Breiman2001` for a detailed introduction. Example ------- -CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` are created from training and test data file +DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` are created from training and test data file .. sgexample:: random_forest.sg:create_features -Combination rules to be used for prediction are derived form the :sgclass:`CCombinationRule` class. Here we create a :sgclass:`CMajorityVote` class to be used as a combination rule. +Combination rules to be used for prediction are derived form the :sgclass:`CombinationRule` class. Here we create a :sgclass:`MajorityVote` class to be used as a combination rule. .. sgexample:: random_forest.sg:create_combination_rule @@ -22,11 +22,11 @@ Next an instance of :sgclass:`CRandomForest` is created. The parameters provided .. sgexample:: random_forest.sg:create_instance -Then we run the train random forest and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we run the train random forest and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: random_forest.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy` as well as get the "out of bag error". +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy` as well as get the "out of bag error". .. sgexample:: random_forest.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/relaxed_tree.rst b/doc/cookbook/source/examples/multiclass/relaxed_tree.rst index ebb13887877..a9b4803c484 100644 --- a/doc/cookbook/source/examples/multiclass/relaxed_tree.rst +++ b/doc/cookbook/source/examples/multiclass/relaxed_tree.rst @@ -15,7 +15,7 @@ See :cite:`gao2011discriminative` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: relaxed_tree.sg:create_features @@ -28,11 +28,11 @@ We use confusion matrix to estimate the initial partition of the dataset and the .. sgexample:: relaxed_tree.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: relaxed_tree.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: relaxed_tree.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/shareboost.rst b/doc/cookbook/source/examples/multiclass/shareboost.rst index b200bf2d417..8246585c815 100644 --- a/doc/cookbook/source/examples/multiclass/shareboost.rst +++ b/doc/cookbook/source/examples/multiclass/shareboost.rst @@ -9,7 +9,7 @@ See :cite:`shalev2011shareboost` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: shareboost.sg:create_features @@ -17,11 +17,11 @@ We create an instance of the :sgclass:`CShareBoost` classifier by setting the nu .. sgexample:: shareboost.sg:create_instance -Then we train and apply it to test data, which gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which gives :sgclass:`MulticlassLabels`. .. sgexample:: shareboost.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +We can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: shareboost.sg:evaluate_accuracy diff --git a/doc/cookbook/source/examples/multiclass/support_vector_machine.rst b/doc/cookbook/source/examples/multiclass/support_vector_machine.rst index 65e64cd2f2e..657152bb707 100644 --- a/doc/cookbook/source/examples/multiclass/support_vector_machine.rst +++ b/doc/cookbook/source/examples/multiclass/support_vector_machine.rst @@ -8,11 +8,11 @@ The multi-class support vector machine is a multi-class classifier which uses :s Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: support_vector_machine.sg:create_features -In order to run :sgclass:`CMulticlassLibSVM`, we need to initialize a kernel like :sgclass:`CGaussianKernel` with training features and some parameters like :math:`C` and epsilon i.e. residual convergence parameter which is optional. +In order to run :sgclass:`CMulticlassLibSVM`, we need to initialize a kernel like :sgclass:`GaussianKernel` with training features and some parameters like :math:`C` and epsilon i.e. residual convergence parameter which is optional. .. sgexample:: support_vector_machine.sg:set_parameters @@ -20,11 +20,11 @@ We create an instance of the :sgclass:`CMulticlassLibSVM` classifier by passing .. sgexample:: support_vector_machine.sg:create_instance -Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`. +Then we train and apply it to test data, which here gives :sgclass:`MulticlassLabels`. .. sgexample:: support_vector_machine.sg:train_and_apply -Finally, we can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`. +Finally, we can evaluate test performance via e.g. :sgclass:`MulticlassAccuracy`. .. sgexample:: support_vector_machine.sg:evaluate_error diff --git a/doc/cookbook/source/examples/neural_nets/convolutional_net_classification.rst b/doc/cookbook/source/examples/neural_nets/convolutional_net_classification.rst index beba627fec3..6d7b39f8af2 100644 --- a/doc/cookbook/source/examples/neural_nets/convolutional_net_classification.rst +++ b/doc/cookbook/source/examples/neural_nets/convolutional_net_classification.rst @@ -17,17 +17,17 @@ See chapter 9 in :cite:`Goodfellow-et-al-2016-Book` 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:`CMulticlassLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`MulticlassLabels` as .. sgexample:: convolutional_net_classification.sg:create_features -We create a :sgclass:`CNeuralNetwork` instance and randomly initialize the network parameters by sampling from a gaussian distribution. We also set appropriate parameters like regularization coefficient, dropout probabilities, learning rate, etc. as shown below. More parameters can be found in the documentation of :sgclass:`CNeuralNetwork`. +We create a :sgclass:`NeuralNetwork` instance and randomly initialize the network parameters by sampling from a gaussian distribution. We also set appropriate parameters like regularization coefficient, dropout probabilities, learning rate, etc. as shown below. More parameters can be found in the documentation of :sgclass:`NeuralNetwork`. .. sgexample:: convolutional_net_classification.sg:set_parameters .. sgexample:: convolutional_net_classification.sg:create_instance -We create instances of :sgclass:`CNeuralInputLayer`, :sgclass:`CNeuralConvolutionalLayer` and :sgclass:`NeuralSoftmaxLayer` which are building blocks of :sgclass:`CNeuralNetwork` +We create instances of :sgclass:`NeuralInputLayer`, :sgclass:`NeuralConvolutionalLayer` and :sgclass:`NeuralSoftmaxLayer` which are building blocks of :sgclass:`NeuralNetwork` .. sgexample:: convolutional_net_classification.sg:add_layers diff --git a/doc/cookbook/source/examples/neural_nets/feedforward_net_classification.rst b/doc/cookbook/source/examples/neural_nets/feedforward_net_classification.rst index 845878bdf2f..43f30e3b56b 100644 --- a/doc/cookbook/source/examples/neural_nets/feedforward_net_classification.rst +++ b/doc/cookbook/source/examples/neural_nets/feedforward_net_classification.rst @@ -18,16 +18,16 @@ See chapter 6 in :cite:`Goodfellow-et-al-2016-Book` 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:: feedforward_net_classification.sg:create_features -We create a :sgclass:`CNeuralNetwork` instance by using the above layers and randomly initialize the network parameters by sampling from a gaussian distribution.We set appropriate parameters like regularization coefficient, dropout probabilities, learning rate, etc. as shown below. More parameters can be found in the documentation of :sgclass:`CNeuralNetwork`. +We create a :sgclass:`NeuralNetwork` instance by using the above layers and randomly initialize the network parameters by sampling from a gaussian distribution.We set appropriate parameters like regularization coefficient, dropout probabilities, learning rate, etc. as shown below. More parameters can be found in the documentation of :sgclass:`NeuralNetwork`. .. sgexample:: feedforward_net_classification.sg:create_instance -We create instances of :sgclass:`CNeuralInputLayer`, :sgclass:`CNeuralLinearLayer` and :sgclass:`NeuralSoftmaxLayer` which are building blocks of :sgclass:`CNeuralNetwork` +We create instances of :sgclass:`NeuralInputLayer`, :sgclass:`NeuralLinearLayer` and :sgclass:`NeuralSoftmaxLayer` which are building blocks of :sgclass:`NeuralNetwork` .. sgexample:: feedforward_net_classification.sg:add_layers diff --git a/doc/cookbook/source/examples/neural_nets/feedforward_net_regression.rst b/doc/cookbook/source/examples/neural_nets/feedforward_net_regression.rst index 065ce986383..d4718de90bd 100644 --- a/doc/cookbook/source/examples/neural_nets/feedforward_net_regression.rst +++ b/doc/cookbook/source/examples/neural_nets/feedforward_net_regression.rst @@ -8,15 +8,15 @@ This page illustrates the usage of feedforward networks for regression. For more Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as .. sgexample:: feedforward_net_regression.sg:create_features -We create a :sgclass:`CNeuralNetwork` instance by using the above layers and randomly initialize the network parameters by sampling from a gaussian distribution. We set appropriate parameters like regularization coefficient, number of epochs, learning rate, etc. as shown below. More parameters can be found in the documentation of :sgclass:`CNeuralNetwork`. +We create a :sgclass:`NeuralNetwork` instance by using the above layers and randomly initialize the network parameters by sampling from a gaussian distribution. We set appropriate parameters like regularization coefficient, number of epochs, learning rate, etc. as shown below. More parameters can be found in the documentation of :sgclass:`NeuralNetwork`. .. sgexample:: feedforward_net_regression.sg:create_instance -We create instances of :sgclass:`CNeuralLayers` and add an input layer, hidden layer and output layer which are building blocks of :sgclass:`CNeuralNetwork` +We create instances of :sgclass:`NeuralLayers` and add an input layer, hidden layer and output layer which are building blocks of :sgclass:`NeuralNetwork` .. sgexample:: feedforward_net_regression.sg:add_layers @@ -28,7 +28,7 @@ We can extract the parameters of the trained network. .. sgexample:: feedforward_net_regression.sg:get_params -Finally, we compute :sgclass:`CMeanSquaredError`. +Finally, we compute :sgclass:`MeanSquaredError`. .. sgexample:: feedforward_net_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/pipeline/pipeline.rst b/doc/cookbook/source/examples/pipeline/pipeline.rst index 55bd4eb4cb5..18bf1b31eaa 100644 --- a/doc/cookbook/source/examples/pipeline/pipeline.rst +++ b/doc/cookbook/source/examples/pipeline/pipeline.rst @@ -9,7 +9,7 @@ Features are transformed by transformers and fed into the next stage sequentiall ------- Example ------- -Imagine we have files with training data. We create :sgclass:`CDenseFeatures` (here 64 bit floats aka RealFeatures) as +Imagine we have files with training data. We create :sgclass:`DenseFeatures` (here 64 bit floats aka RealFeatures) as .. sgexample:: pipeline:create_features @@ -17,7 +17,7 @@ To project the feature to a lower dimension, we create an instance of transforme .. sgexample:: pipeline:create_transformers -We then perform clustering using :sgclass:`CKMeans`. +We then perform clustering using :sgclass:`KMeans`. .. sgexample:: pipeline:create_machine diff --git a/doc/cookbook/source/examples/regression/kernel_ridge_regression.rst b/doc/cookbook/source/examples/regression/kernel_ridge_regression.rst index 505dbfa7181..1a6bd0ac5ff 100644 --- a/doc/cookbook/source/examples/regression/kernel_ridge_regression.rst +++ b/doc/cookbook/source/examples/regression/kernel_ridge_regression.rst @@ -18,19 +18,19 @@ See Chapter 17 in :cite:`barber2012bayesian` 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:`CRegressionLabels` as +Imagine we have files with training and test data. We create `DenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as .. sgexample:: kernel_ridge_regression.sg:create_features -Choose an appropriate :sgclass:`CKernel` and instantiate it. Here we use a :sgclass:`CGaussianKernel`. +Choose an appropriate :sgclass:`Kernel` and instantiate it. Here we use a :sgclass:`GaussianKernel`. .. sgexample:: kernel_ridge_regression.sg:create_appropriate_kernel -We create an instance of :sgclass:`CKernelRidgeRegression` classifier by passing it :math:`\tau`, the kernel and labels. +We create an instance of :sgclass:`KernelRidgeRegression` classifier by passing it :math:`\tau`, the kernel and labels. .. sgexample:: kernel_ridge_regression.sg:create_instance -Then we train the regression model and apply it to test data to get the predicted :sgclass:`CRegressionLabels`. +Then we train the regression model and apply it to test data to get the predicted :sgclass:`RegressionLabels`. .. sgexample:: kernel_ridge_regression.sg:train_and_apply @@ -38,7 +38,7 @@ After training, we can extract :math:`\alpha`. .. sgexample:: kernel_ridge_regression.sg:extract_alpha -Finally, we can evaluate the :sgclass:`CMeanSquaredError`. +Finally, we can evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: kernel_ridge_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/regression/kernel_ridge_regression_nystrom.rst b/doc/cookbook/source/examples/regression/kernel_ridge_regression_nystrom.rst index 0de0c30f340..8776042f12d 100644 --- a/doc/cookbook/source/examples/regression/kernel_ridge_regression_nystrom.rst +++ b/doc/cookbook/source/examples/regression/kernel_ridge_regression_nystrom.rst @@ -33,14 +33,14 @@ See :cite:`williams2001using` or :cite:`rudi2015less` for more details. Example ------- -The API is very similar to :sgclass:`CKernelRidgeRegression`. We create -:sgclass:`CDenseFeatures` (here 64 bit floats aka RealFeatures) and -:sgclass:`CRegressionLabels` as +The API is very similar to :sgclass:`KernelRidgeRegression`. We create +:sgclass:`DenseFeatures` (here 64 bit floats aka RealFeatures) and +:sgclass:`RegressionLabels` as .. sgexample:: kernel_ridge_regression_nystrom.sg:create_features -Choose an appropriate :sgclass:`CKernel` and instantiate it. Here we use a -:sgclass:`CGaussianKernel`. +Choose an appropriate :sgclass:`Kernel` and instantiate it. Here we use a +:sgclass:`GaussianKernel`. .. sgexample:: kernel_ridge_regression_nystrom.sg:create_appropriate_kernel @@ -51,7 +51,7 @@ the labels. .. sgexample:: kernel_ridge_regression_nystrom.sg:create_instance Then we train the regression model and apply it to test data to get the -predicted :sgclass:`CRegressionLabels`. +predicted :sgclass:`RegressionLabels`. .. sgexample:: kernel_ridge_regression_nystrom.sg:train_and_apply @@ -59,7 +59,7 @@ After training, we can extract :math:`\alpha`. .. sgexample:: kernel_ridge_regression_nystrom.sg:extract_alpha -Finally, we can evaluate the :sgclass:`CMeanSquaredError`. +Finally, we can evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: kernel_ridge_regression_nystrom.sg:evaluate_error diff --git a/doc/cookbook/source/examples/regression/least_angle_regression.rst b/doc/cookbook/source/examples/regression/least_angle_regression.rst index 624a9cb894b..03fee8f1af7 100644 --- a/doc/cookbook/source/examples/regression/least_angle_regression.rst +++ b/doc/cookbook/source/examples/regression/least_angle_regression.rst @@ -19,7 +19,7 @@ Pre-processing of :math:`X` and :math:`y` are needed to ensure the correctness o Example ------- -Imagine we have files with training and test data. We create `CDenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as +Imagine we have files with training and test data. We create `DenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as .. sgexample:: least_angle_regression.sg:create_features @@ -27,11 +27,11 @@ To normalize and center the features, we create an instance of preprocessors :sg .. sgexample:: least_angle_regression:preprocess_features -We create an instance of :sgclass:`CLeastAngleRegression` by selecting to disable the LASSO solution, setting the penalty :math:`\lambda` for l1 norm and setting training data and labels. +We create an instance of :sgclass:`LeastAngleRegression` by selecting to disable the LASSO solution, setting the penalty :math:`\lambda` for l1 norm and setting training data and labels. .. sgexample:: least_angle_regression:create_instance -Then we train the regression model and apply it to test data to get the predicted :sgclass:`CRegressionLabels` . +Then we train the regression model and apply it to test data to get the predicted :sgclass:`RegressionLabels` . .. sgexample:: linear_ridge_regression.sg:train_and_apply @@ -39,7 +39,7 @@ After training, we can extract :math:`{\bf w}`. .. sgexample:: linear_ridge_regression.sg:extract_w -Finally, we can evaluate the :sgclass:`CMeanSquaredError`. +Finally, we can evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: linear_ridge_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/regression/linear_ridge_regression.rst b/doc/cookbook/source/examples/regression/linear_ridge_regression.rst index 1be1a72552c..dadc9e0f033 100644 --- a/doc/cookbook/source/examples/regression/linear_ridge_regression.rst +++ b/doc/cookbook/source/examples/regression/linear_ridge_regression.rst @@ -18,13 +18,13 @@ In practice, an additional bias :math:`b=\frac{1}{N}\sum_{i=1}^{N}y_{i}-{\bf w}\ :math:`\bar{\mathbf{x}}=\frac{1}{N}\sum_{i=1}^{N}{\bf x}_{i}` can also be included, which effectively centers the :math:`X` before computing the solution. -For the special case when :math:`\tau = 0`, a wrapper class :sgclass:`CLeastSquaresRegression` is available. +For the special case when :math:`\tau = 0`, a wrapper class :sgclass:`LeastSquaresRegression` is available. ------- Example ------- -Imagine we have files with training and test data. We create `CDenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as +Imagine we have files with training and test data. We create `DenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as .. sgexample:: linear_ridge_regression.sg:create_features @@ -32,7 +32,7 @@ We create an instance of :sgclass:`CLinearRidgeRegression` classifier, passing i .. sgexample:: linear_ridge_regression.sg:create_instance -Then we train the regression model and apply it to test data to get the predicted :sgclass:`CRegressionLabels`. +Then we train the regression model and apply it to test data to get the predicted :sgclass:`RegressionLabels`. .. sgexample:: linear_ridge_regression.sg:train_and_apply @@ -44,7 +44,7 @@ We could also have trained without bias and set it manually. .. sgexample:: linear_ridge_regression.sg:manual_bias -Finally, we can evaluate the :sgclass:`CMeanSquaredError`. +Finally, we can evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: linear_ridge_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/regression/multiple_kernel_learning.rst b/doc/cookbook/source/examples/regression/multiple_kernel_learning.rst index e69e7b1e705..5f76bd69511 100644 --- a/doc/cookbook/source/examples/regression/multiple_kernel_learning.rst +++ b/doc/cookbook/source/examples/regression/multiple_kernel_learning.rst @@ -10,7 +10,7 @@ Multiple kernel learning (MKL) is based on convex combinations of arbitrary kern where :math:`\beta_k > 0`, :math:`\sum_{k=1}^{K} \beta_k = 1`, :math:`K` is the number of sub-kernels, :math:`\bf{k}` is a combined kernel, :math:`{\bf k}_i` is an individual kernel and :math:`{x_i}_i` are the training data. -Regression is done by using :sgclass:`CSVMLight`. See :doc:`support_vector_regression` for more details. +Regression is done by using :sgclass:`SVMLight`. See :doc:`support_vector_regression` for more details. See :cite:`sonnenburg2006large` for more information about MKL. @@ -19,19 +19,19 @@ See :cite:`sonnenburg2006large` for more information about MKL. Example ------- -Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as +Imagine we have files with training and test data. We create DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as .. sgexample:: multiple_kernel_learning.sg:create_features -Then we create indvidual kernels like :sgclass:`CPolyKernel` and :sgclass:`CGaussianKernel` which will be later combined in one :sgclass:`CCombinedKernel`. +Then we create indvidual kernels like :sgclass:`CPolyKernel` and :sgclass:`GaussianKernel` which will be later combined in one :sgclass:`CombinedKernel`. .. sgexample:: multiple_kernel_learning.sg:create_kernel -We create an instance of :sgclass:`CCombinedKernel` and append the :sgclass:`CKernel` objects. +We create an instance of :sgclass:`CombinedKernel` and append the :sgclass:`Kernel` objects. .. sgexample:: multiple_kernel_learning.sg:create_combined_train -:sgclass:`CMKLRegression` needs an SVM solver as input. We here use :sgclass:`SVMLight`. We create an object of :sgclass:`SVMLight` and :sgclass:`CMKLRegression`, provide the combined kernel and labels before training it. +:sgclass:`MKLRegression` needs an SVM solver as input. We here use :sgclass:`SVMLight`. We create an object of :sgclass:`SVMLight` and :sgclass:`MKLRegression`, provide the combined kernel and labels before training it. .. sgexample:: multiple_kernel_learning.sg:train_mkl @@ -39,11 +39,11 @@ After training, we can extract :math:`\beta` and SVM coefficients :math:`\alpha` .. sgexample:: multiple_kernel_learning.sg:extract_weights -We set the updated kernel and predict :sgclass:`CRegressionLabels` for test data. +We set the updated kernel and predict :sgclass:`RegressionLabels` for test data. .. sgexample:: multiple_kernel_learning.sg:mkl_apply -Finally, we can evaluate the :sgclass:`CMeanSquaredError`. +Finally, we can evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: multiple_kernel_learning.sg:evaluate_error diff --git a/doc/cookbook/source/examples/regression/random_forest_regression.rst b/doc/cookbook/source/examples/regression/random_forest_regression.rst index c0a1f66b3e7..91394d4b536 100644 --- a/doc/cookbook/source/examples/regression/random_forest_regression.rst +++ b/doc/cookbook/source/examples/regression/random_forest_regression.rst @@ -11,11 +11,11 @@ See :cite:`Breiman2001` for a detailed introduction. Example ------- -CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` are created from training and test data file +DenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` are created from training and test data file .. sgexample:: random_forest_regression.sg:create_features -Combination rules to be used for prediction are derived form the :sgclass:`CCombinationRule` class. Here we create a :sgclass:`CMeanRule` class to be used as a combination rule. +Combination rules to be used for prediction are derived form the :sgclass:`CombinationRule` class. Here we create a :sgclass:`MeanRule` class to be used as a combination rule. .. sgexample:: random_forest_regression.sg:create_combination_rule @@ -23,11 +23,11 @@ Next an instance of :sgclass:`CRandomForest` is created. The parameters provided .. sgexample:: random_forest_regression.sg:create_instance -Then we train the random forest and apply it to test data, which here gives :sgclass:`CRegressionLabels`. +Then we train the random forest and apply it to test data, which here gives :sgclass:`RegressionLabels`. .. sgexample:: random_forest_regression.sg:train_and_apply -We can evaluate test performance via e.g. :sgclass:`CMeanSquaredError` as well as get the "out of bag error". +We can evaluate test performance via e.g. :sgclass:`MeanSquaredError` as well as get the "out of bag error". .. sgexample:: random_forest_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/regression/support_vector_regression.rst b/doc/cookbook/source/examples/regression/support_vector_regression.rst index aa01c98c9ad..1b23c481ba5 100644 --- a/doc/cookbook/source/examples/regression/support_vector_regression.rst +++ b/doc/cookbook/source/examples/regression/support_vector_regression.rst @@ -15,19 +15,19 @@ See :cite:`scholkopf2002learning` for a more detailed introduction. :sgclass:`Li Example ------- -Imagine we have files with training and test data. We create `CDenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`CRegressionLabels` as +Imagine we have files with training and test data. We create `DenseFeatures` (here 64 bit floats aka RealFeatures) and :sgclass:`RegressionLabels` as .. sgexample:: support_vector_regression.sg:create_features -Choose an appropriate :sgclass:`CKernel` and instantiate it. Here we use a :sgclass:`CGaussianKernel`. +Choose an appropriate :sgclass:`Kernel` and instantiate it. Here we use a :sgclass:`GaussianKernel`. .. sgexample:: support_vector_regression.sg:create_appropriate_kernel -We create an instance of :sgclass:`CLibSVR` classifier by passing it the kernel, labels, solver type and some more parameters. More solver types are available in :sgclass:`CLibSVR`. See :cite:`chang2002training` for more details. +We create an instance of :sgclass:`LibSVR` classifier by passing it the kernel, labels, solver type and some more parameters. More solver types are available in :sgclass:`LibSVR`. See :cite:`chang2002training` for more details. .. sgexample:: support_vector_regression.sg:create_instance -Then we train the regression model and apply it to test data to get the predicted :sgclass:`CRegressionLabels`. +Then we train the regression model and apply it to test data to get the predicted :sgclass:`RegressionLabels`. .. sgexample:: support_vector_regression.sg:train_and_apply @@ -35,7 +35,7 @@ After training, we can extract :math:`\alpha`. .. sgexample:: support_vector_regression.sg:extract_alpha -Finally, we can evaluate the :sgclass:`CMeanSquaredError`. +Finally, we can evaluate the :sgclass:`MeanSquaredError`. .. sgexample:: support_vector_regression.sg:evaluate_error diff --git a/doc/cookbook/source/examples/statistical_testing/linear_time_maximum_mean_discrepancy.rst b/doc/cookbook/source/examples/statistical_testing/linear_time_maximum_mean_discrepancy.rst index 33c4992a3be..f261ee07dd1 100644 --- a/doc/cookbook/source/examples/statistical_testing/linear_time_maximum_mean_discrepancy.rst +++ b/doc/cookbook/source/examples/statistical_testing/linear_time_maximum_mean_discrepancy.rst @@ -17,9 +17,9 @@ Example ------- Imagine we have samples from :math:`p` and :math:`q`. -As the linear time MMD is a streaming statistic, we need to pass it :sgclass:`CStreamingFeatures`. -Here, we use synthetic data generators, but it is possible to construct :sgclass:`CStreamingFeatures` from (large) files. -We create an instance of :sgclass:`CLinearTimeMMD`, passing it data and the kernel to use, +As the linear time MMD is a streaming statistic, we need to pass it :sgclass:`StreamingFeatures`. +Here, we use synthetic data generators, but it is possible to construct :sgclass:`StreamingFeatures` from (large) files. +We create an instance of :sgclass:`LinearTimeMMD`, passing it data and the kernel to use, .. sgexample:: linear_time_maximum_mean_discrepancy.sg:create_instance @@ -63,7 +63,7 @@ If all kernels have the same type, we can convert the result into that type, for Note that in order to extract particular kernel parameters, we need to cast the kernel to its actual type. -Similarly, a convex combination of kernels, in the form of :sgclass:`CCombinedKernel` can be learned and extracted as +Similarly, a convex combination of kernels, in the form of :sgclass:`CombinedKernel` can be learned and extracted as .. sgexample:: linear_time_maximum_mean_discrepancy.sg:select_kernel_combined diff --git a/doc/cookbook/source/examples/statistical_testing/quadratic_time_maximum_mean_discrepancy.rst b/doc/cookbook/source/examples/statistical_testing/quadratic_time_maximum_mean_discrepancy.rst index 4e4053aa8a0..72d9876ccf5 100644 --- a/doc/cookbook/source/examples/statistical_testing/quadratic_time_maximum_mean_discrepancy.rst +++ b/doc/cookbook/source/examples/statistical_testing/quadratic_time_maximum_mean_discrepancy.rst @@ -17,20 +17,20 @@ See :cite:`gretton2012kernel` for a detailed introduction. Example ------- -Imagine we have samples from :math:`p` and :math:`q`, here in the form of CDenseFeatures (here 64 bit floats aka RealFeatures). +Imagine we have samples from :math:`p` and :math:`q`, here in the form of DenseFeatures (here 64 bit floats aka RealFeatures). .. sgexample:: quadratic_time_maximum_mean_discrepancy.sg:create_features -We create an instance of :sgclass:`CQuadraticTimeMMD`, passing it data the kernel. +We create an instance of :sgclass:`QuadraticTimeMMD`, passing it data the kernel. .. sgexample:: quadratic_time_maximum_mean_discrepancy.sg:create_instance -We can select multiple ways to compute the test statistic, see :sgclass:`CQuadraticTimeMMD` for details. +We can select multiple ways to compute the test statistic, see :sgclass:`QuadraticTimeMMD` for details. The biased statistic is computed as .. sgexample:: quadratic_time_maximum_mean_discrepancy.sg:estimate_mmd -There are multiple ways to perform the actual hypothesis test, see :sgclass:`CQuadraticTimeMMD` for details. +There are multiple ways to perform the actual hypothesis test, see :sgclass:`QuadraticTimeMMD` for details. The permutation version simulates from :math:`H_0` via repeatedly permuting the samples from :math:`p` and :math:`q`. We can perform the test via computing a test threshold for a given :math:`\alpha`, or by directly computing a p-value. @@ -40,7 +40,7 @@ We can perform the test via computing a test threshold for a given :math:`\alpha Multiple kernels ---------------- -It is possible to perform all operations (computing statistics, performing test, etc) for multiple kernels at once, via the :sgclass:`CMultiKernelQuadraticTimeMMD` interface. +It is possible to perform all operations (computing statistics, performing test, etc) for multiple kernels at once, via the :sgclass:`MultiKernelQuadraticTimeMMD` interface. .. sgexample:: quadratic_time_maximum_mean_discrepancy.sg:multi_kernel @@ -76,7 +76,7 @@ If all kernels have the same type, we can convert the result into that type, for Note that in order to extract particular kernel parameters, we need to cast the kernel to its actual type. -Similarly, a convex combination of kernels, in the form of :sgclass:`CCombinedKernel` can be learned and extracted as +Similarly, a convex combination of kernels, in the form of :sgclass:`CombinedKernel` can be learned and extracted as .. sgexample:: quadratic_time_maximum_mean_discrepancy.sg:select_kernel_combined diff --git a/doc/ipython-notebooks/classification/Classification.ipynb b/doc/ipython-notebooks/classification/Classification.ipynb index 4d2ee01277b..63f117aeb01 100644 --- a/doc/ipython-notebooks/classification/Classification.ipynb +++ b/doc/ipython-notebooks/classification/Classification.ipynb @@ -75,7 +75,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Transformation of features to Shogun format using RealFeatures and BinaryLables classes." + "Transformation of features to Shogun format using RealFeatures and BinaryLables classes." ] }, { @@ -182,21 +182,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Support Vector Machine" + "## Support Vector Machine" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Linear SVM" + "Linear SVM" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun provide Liblinear which is a library for large-scale linear learning focusing on SVM used for classification" + "Shogun provide Liblinear which is a library for large-scale linear learning focusing on SVM used for classification" ] }, { @@ -245,14 +245,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun provides many options for using kernel functions. Kernels in Shogun are based on two classes which are CKernel and CKernelMachine base class." + "Shogun provides many options for using kernel functions. Kernels in Shogun are based on two classes which are Kernel and KernelMachine base class." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Gaussian Kernel" + "Gaussian Kernel" ] }, { @@ -370,7 +370,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Naive Bayes" + "## Naive Bayes" ] }, { @@ -421,7 +421,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Nearest Neighbors" + "## Nearest Neighbors" ] }, { @@ -495,7 +495,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Quadratic Discriminant Analysis" + "## Quadratic Discriminant Analysis" ] }, { @@ -528,7 +528,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Gaussian Process" + "## Gaussian Process" ] }, { @@ -542,7 +542,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun's CLogitLikelihood and CEPInferenceMethod classes are used." + "Shogun's LogitLikelihood and EPInferenceMethod classes are used." ] }, { @@ -593,7 +593,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun's CProbitLikelihood class is used." + "Shogun's ProbitLikelihood class is used." ] }, { diff --git a/doc/ipython-notebooks/classification/HashedDocDotFeatures.ipynb b/doc/ipython-notebooks/classification/HashedDocDotFeatures.ipynb index 690d7628649..29238572fc1 100644 --- a/doc/ipython-notebooks/classification/HashedDocDotFeatures.ipynb +++ b/doc/ipython-notebooks/classification/HashedDocDotFeatures.ipynb @@ -179,7 +179,7 @@ "The hashed_feats object now has all the required parameters and knows how to communicate and provide the hashed representation only when it is needed by the various algorithms of Shogun.

\n", "So how do we proceed to actually learn a model that can classify documents?
\n", "We said before that the idea after we have taken care of the hashing is to use a linear classifier.
\n", - "Shogun has many linear classifiers available, but for the moment we will consider SVMOcas and we will see how to use it:" + "Shogun has many linear classifiers available, but for the moment we will consider SVMOcas and we will see how to use it:" ] }, { @@ -413,7 +413,7 @@ "\n", "The hashing trick has also been implemented in Shogun for the DenseFeatures and SparseFeatures classes, as HashedDenseFeatures and HashedSparseFeatures classes. These classes also support quadratic features and provide the option to maintain or drop the linear terms.
\n", "For online algorithms or for cases when the data do not fit in memory there exist similar classes with the prefix \"Streaming\" that support reading examples from the disk and providing them to some algorithms one at a time. The classes specifically are StreamingHashedDocDotFeatures, StreamingHashedDenseFeatures and StreamingHashedSparseFeatures. If one has mixed features, that are not just numerical or not just text, then he can use the CombinedDotFeatures class to combine objects of the aforementioned classes!
Another option is to use the Vw* algorithms and the VwExample class that require the input to be in vw format and are a bit trickier to use.
\n", - "In addition, a HashedDocConverter class exists that allows the user to get the hashed BoW representation of a document collection as a CSparseFeatures object." + "In addition, a HashedDocConverter class exists that allows the user to get the hashed BoW representation of a document collection as a SparseFeatures object." ] }, { diff --git a/doc/ipython-notebooks/classification/MKL.ipynb b/doc/ipython-notebooks/classification/MKL.ipynb index 112374d97df..04db1de98cf 100644 --- a/doc/ipython-notebooks/classification/MKL.ipynb +++ b/doc/ipython-notebooks/classification/MKL.ipynb @@ -68,7 +68,7 @@ "Selecting the kernel function\n", "$k()$ and it's parameters is an important issue in training. Kernels designed by humans usually capture one aspect of data. Choosing one kernel means to select exactly one such aspect. Which means combining such aspects is often better than selecting.\n", "\n", - "In shogun the [CMKL](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKL.html) is the base class for MKL. We can do classifications: [binary](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLClassification.html), [one-class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLOneClass.html), [multiclass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLMulticlass.html) and regression too: [regression](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLRegression.html)." + "In shogun the [MKL](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKL.html) is the base class for MKL. We can do classifications: [binary](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLClassification.html), [one-class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLOneClass.html), [multiclass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLMulticlass.html) and regression too: [regression](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLRegression.html)." ] }, { @@ -106,7 +106,7 @@ "Within shogun this optimization problem is solved using [semi-infinite programming](http://en.wikipedia.org/wiki/Semi-infinite_programming). For 1-norm MKL one of the two approaches described in [1] is used.\n", "The first approach (also called the wrapper algorithm) wraps around a single kernel SVMs, alternatingly solving for $\\alpha$ and $\\beta$. It is using a traditional SVM to generate new violated constraints and thus requires a single kernel SVM and any of the SVMs contained in shogun can be used. In the MKL step either a linear program is solved via [glpk](http://en.wikipedia.org/wiki/GNU_Linear_Programming_Kit) or cplex or analytically or a newton (for norms>1) step is performed.\n", "\n", - "The second much faster but also more memory demanding approach performing interleaved optimization, is integrated into the chunking-based [SVMlight](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSVMLight.html).\n", + "The second much faster but also more memory demanding approach performing interleaved optimization, is integrated into the chunking-based [SVMlight](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SVMLight.html).\n", "\n" ] }, @@ -121,7 +121,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun provides an easy way to make combination of kernels using the [CombinedKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCombinedKernel.html) class, to which we can append any [kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernel.html) from the many options shogun provides. It is especially useful to combine kernels working on different domains and to combine kernels looking at independent features and requires [CombinedFeatures](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCombinedFeatures.html) to be used. Similarly the CombinedFeatures is used to combine a number of feature objects into a single CombinedFeatures object" + "Shogun provides an easy way to make combination of kernels using the [CombinedKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CombinedKernel.html) class, to which we can append any [kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Kernel.html) from the many options shogun provides. It is especially useful to combine kernels working on different domains and to combine kernels looking at independent features and requires [CombinedFeatures](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CombinedFeatures.html) to be used. Similarly the CombinedFeatures is used to combine a number of feature objects into a single CombinedFeatures object" ] }, { @@ -230,7 +230,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Just to help us visualize let's use two gaussian kernels ([CGaussianKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianKernel.html)) with considerably different widths. As required in MKL, we need to append them to the Combined kernel. To generate the optimal weights (i.e $\\beta$s in the above equation), training of [MKL](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLClassification.html) is required. This generates the weights as seen in this example." + "Just to help us visualize let's use two gaussian kernels ([GaussianKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianKernel.html)) with considerably different widths. As required in MKL, we need to append them to the Combined kernel. To generate the optimal weights (i.e $\\beta$s in the above equation), training of [MKL](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLClassification.html) is required. This generates the weights as seen in this example." ] }, { @@ -601,7 +601,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "MKL can be used for multiclass classification using the [MKLMulticlass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLMulticlass.html) class. It is based on the GMNPSVM Multiclass SVM. Its termination criterion is set by `set_mkl_epsilon(float64_t eps )` and the maximal number of MKL iterations is set by `set_max_num_mkliters(int32_t maxnum)`. The epsilon termination criterion is the L2 norm between the current MKL weights and their counterpart from the previous iteration. We set it to 0.001 as we want pretty accurate weights.\n", + "MKL can be used for multiclass classification using the [MKLMulticlass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLMulticlass.html) class. It is based on the GMNPSVM Multiclass SVM. Its termination criterion is set by `set_mkl_epsilon(float64_t eps )` and the maximal number of MKL iterations is set by `set_max_num_mkliters(int32_t maxnum)`. The epsilon termination criterion is the L2 norm between the current MKL weights and their counterpart from the previous iteration. We set it to 0.001 as we want pretty accurate weights.\n", "\n", "To see this in action let us compare it to the normal [GMNPSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMNPSVM.html) example as in the [KNN notebook](http://www.shogun-toolbox.org/static/notebook/current/KNN.html#Comparison-to-Multiclass-Support-Vector-Machines), just to see how MKL fares in object recognition. We use the [USPS digit recognition dataset](http://www.gaussianprocess.org/gpml/data/)." ] @@ -670,7 +670,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We combine a [Gaussian kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianKernel.html) and a [PolyKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPolyKernel.html). To test, examples not included in training data are used.\n", + "We combine a [Gaussian kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianKernel.html) and a [PolyKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPolyKernel.html). To test, examples not included in training data are used.\n", "\n", "This is just a demonstration but we can see here how MKL is working behind the scene. What we have is two kernels with significantly different properties. The gaussian kernel defines a function space that is a lot larger than that of the linear kernel or the polynomial kernel. The gaussian kernel has a low width, so it will be able to represent more and more complex relationships between the training data. But it requires enough data to train on. The number of training examples here is 1000, which seems a bit less as total examples are 10000. We hope the polynomial kernel can counter this problem, since it will fit the polynomial for you using a lot less data than the squared exponential. The kernel weights are printed below to add some insight." ] @@ -818,7 +818,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "[One-class classification](http://en.wikipedia.org/wiki/One-class_classification) can be done using MKL in shogun. This is demonstrated in the following simple example using [CMKLOneClass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMKLOneClass.html). We will see how abnormal data is detected. This is also known as novelty detection. Below we generate some toy data and initialize combined kernels and features." + "[One-class classification](http://en.wikipedia.org/wiki/One-class_classification) can be done using MKL in shogun. This is demonstrated in the following simple example using [MKLOneClass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MKLOneClass.html). We will see how abnormal data is detected. This is also known as novelty detection. Below we generate some toy data and initialize combined kernels and features." ] }, { diff --git a/doc/ipython-notebooks/classification/SupportVectorMachines.ipynb b/doc/ipython-notebooks/classification/SupportVectorMachines.ipynb index 46fa4f4df97..53d8bef233c 100644 --- a/doc/ipython-notebooks/classification/SupportVectorMachines.ipynb +++ b/doc/ipython-notebooks/classification/SupportVectorMachines.ipynb @@ -147,7 +147,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "[Liblinear](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibLinear.html), a library for large- scale linear learning focusing on SVM, is used to do the classification. It supports different [solver types](http://www.shogun-toolbox.org/doc/en/latest/namespaceshogun.html#a6e99d1864c93fc2d41b1fa0fc253f471)." + "[Liblinear](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LibLinear.html), a library for large- scale linear learning focusing on SVM, is used to do the classification. It supports different [solver types](http://www.shogun-toolbox.org/doc/en/latest/namespaceshogun.html#a6e99d1864c93fc2d41b1fa0fc253f471)." ] }, { @@ -284,11 +284,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun provides many options for the above mentioned kernel functions. [CKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernel.html) is the base class for kernels. Some commonly used kernels : \n", + "Shogun provides many options for the above mentioned kernel functions. [Kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Kernel.html) is the base class for kernels. Some commonly used kernels : \n", "\n", - "* [Gaussian kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianKernel.html) : Popular Gaussian kernel computed as $k({\\bf x},{\\bf x'})= exp(-\\frac{||{\\bf x}-{\\bf x'}||^2}{\\tau})$\n", + "* [Gaussian kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianKernel.html) : Popular Gaussian kernel computed as $k({\\bf x},{\\bf x'})= exp(-\\frac{||{\\bf x}-{\\bf x'}||^2}{\\tau})$\n", "\n", - "* [Linear kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLinearKernel.html) : Computes $k({\\bf x},{\\bf x'})= {\\bf x}\\cdot {\\bf x'}$\n", + "* [Linear kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LinearKernel.html) : Computes $k({\\bf x},{\\bf x'})= {\\bf x}\\cdot {\\bf x'}$\n", "* [Polynomial kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPolyKernel.html) : Polynomial kernel computed as $k({\\bf x},{\\bf x'})= ({\\bf x}\\cdot {\\bf x'}+c)^d$\n", "\n", "* [Simgmoid Kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSigmoidKernel.html) : Computes $k({\\bf x},{\\bf x'})=\\mbox{tanh}(\\gamma {\\bf x}\\cdot{\\bf x'}+c)$\n", @@ -351,7 +351,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now we train an SVM with a Gaussian Kernel. We use LibSVM but we could use any of the [other SVM](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CSVM.html) from Shogun. They all utilize the same kernel framework and so are drop-in replacements." + "Now we train an SVM with a Gaussian Kernel. We use LibSVM but we could use any of the [other SVM](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1SVM.html) from Shogun. They all utilize the same kernel framework and so are drop-in replacements." ] }, { @@ -456,7 +456,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Calibrated probabilities can be generated in addition to class predictions using `scores_to_probabilities()` method of [BinaryLabels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CBinaryLabels.html), which implements the method described in [3]. This should only be used in conjunction with SVM. A parameteric form of a [sigmoid function](http://en.wikipedia.org/wiki/Sigmoid_function) $$\\frac{1}{{1+}exp(af(x) + b)}$$ is used to fit the outputs. Here $f(x)$ is the signed distance of a sample from the hyperplane, $a$ and $b$ are parameters to the sigmoid. This gives us the posterier probabilities $p(y=1|f(x))$." + "Calibrated probabilities can be generated in addition to class predictions using `scores_to_probabilities()` method of [BinaryLabels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1BinaryLabels.html), which implements the method described in [3]. This should only be used in conjunction with SVM. A parameteric form of a [sigmoid function](http://en.wikipedia.org/wiki/Sigmoid_function) $$\\frac{1}{{1+}exp(af(x) + b)}$$ is used to fit the outputs. Here $f(x)$ is the signed distance of a sample from the hyperplane, $a$ and $b$ are parameters to the sigmoid. This gives us the posterier probabilities $p(y=1|f(x))$." ] }, { @@ -754,15 +754,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Kernel normalizers post-process kernel values by carrying out normalization in feature space. Since kernel based SVMs use a non-linear mapping, in most cases any normalization in input space is lost in feature space. Kernel normalizers are a possible solution to this. Kernel Normalization is not strictly-speaking a form of preprocessing since it is not applied directly on the input vectors but can be seen as a kernel interpretation of the preprocessing. The [CKernelNormalizer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelNormalizer.html) class provides tools for kernel normalization. Some of the kernel normalizers in Shogun:\n", + "Kernel normalizers post-process kernel values by carrying out normalization in feature space. Since kernel based SVMs use a non-linear mapping, in most cases any normalization in input space is lost in feature space. Kernel normalizers are a possible solution to this. Kernel Normalization is not strictly-speaking a form of preprocessing since it is not applied directly on the input vectors but can be seen as a kernel interpretation of the preprocessing. The [KernelNormalizer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelNormalizer.html) class provides tools for kernel normalization. Some of the kernel normalizers in Shogun:\n", "\n", "* [SqrtDiagKernelNormalizer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSqrtDiagKernelNormalizer.html) : This normalization in the feature space amounts to defining a new kernel $k'({\\bf x},{\\bf x'}) = \\frac{k({\\bf x},{\\bf x'})}{\\sqrt{k({\\bf x},{\\bf x})k({\\bf x'},{\\bf x'})}}$\n", "\n", "* [AvgDiagKernelNormalizer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CAvgDiagKernelNormalizer.html) : Scaling with a constant $k({\\bf x},{\\bf x'})= \\frac{1}{c}\\cdot k({\\bf x},{\\bf x'})$\n", "\n", - "* [ZeroMeanCenterKernelNormalizer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CZeroMeanCenterKernelNormalizer.html) : Centers the kernel in feature space and ensures each feature must have zero mean after centering.\n", + "* [ZeroMeanCenterKernelNormalizer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1ZeroMeanCenterKernelNormalizer.html) : Centers the kernel in feature space and ensures each feature must have zero mean after centering.\n", "\n", - "The `set_normalizer()` method of [CKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernel.html) is used to add a normalizer.\n", + "The `set_normalizer()` method of [Kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Kernel.html) is used to add a normalizer.\n", "Let us try it out on the [ionosphere dataset](https://archive.ics.uci.edu/ml/datasets/Ionosphere) where we use a small training set of 30 samples to train our SVM. Gaussian kernel with and without normalization is used. See reference [1] for details." ] }, @@ -843,7 +843,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Multiclass classification can be done using SVM by reducing the problem to binary classification. More on multiclass reductions in [this notebook](http://www.shogun-toolbox.org/static/notebook/current/multiclass_reduction.html). [CGMNPSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMNPSVM.html) class provides a built in [one vs rest multiclass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMulticlassOneVsRestStrategy.html) classification using [GMNPlib](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMNPLib.html). Let us see classification using it on four classes. [CGMM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMM.html) class is used to sample the data." + "Multiclass classification can be done using SVM by reducing the problem to binary classification. More on multiclass reductions in [this notebook](http://www.shogun-toolbox.org/static/notebook/current/multiclass_reduction.html). [CGMNPSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMNPSVM.html) class provides a built in [one vs rest multiclass](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MulticlassOneVsRestStrategy.html) classification using [GMNPlib](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMNPLib.html). Let us see classification using it on four classes. [CGMM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMM.html) class is used to sample the data." ] }, { diff --git a/doc/ipython-notebooks/clustering/GMM.ipynb b/doc/ipython-notebooks/clustering/GMM.ipynb index 2baa8691451..ee76d52e7d8 100644 --- a/doc/ipython-notebooks/clustering/GMM.ipynb +++ b/doc/ipython-notebooks/clustering/GMM.ipynb @@ -95,7 +95,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The main class for GMM in Shogun is CGMM, which contains an interface for setting up a model and sampling from it, but also to learn the model (the $\\pi_i$ and parameters $\\theta$) via EM. It inherits from the base class for distributions in Shogun, CDistribution, and combines multiple single distribution instances to a mixture.\n", + "The main class for GMM in Shogun is CGMM, which contains an interface for setting up a model and sampling from it, but also to learn the model (the $\\pi_i$ and parameters $\\theta$) via EM. It inherits from the base class for distributions in Shogun, Distribution, and combines multiple single distribution instances to a mixture.\n", "\n", "We start by creating a GMM instance, sampling from it, and computing the log-likelihood of the model for some points, and the log-likelihood of each individual component for some points. All these things are done in two dimensions to be able to plot them, but they generalise to higher (or lower) dimensions easily.\n", "\n", @@ -195,7 +195,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sampling is extremely easy since every instance of the CDistribution class in Shogun allows to sample from it (if implemented)" + "Sampling is extremely easy since every instance of the Distribution class in Shogun allows to sample from it (if implemented)" ] }, { @@ -239,7 +239,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Next, let us visualise the density of the joint model (which is a convex sum of the densities of the individual distributions). Note the similarity between the calls since all distributions implement the CDistribution interface, including the mixture." + "Next, let us visualise the density of the joint model (which is a convex sum of the densities of the individual distributions). Note the similarity between the calls since all distributions implement the Distribution interface, including the mixture." ] }, { @@ -380,7 +380,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It seems that three comonents give a density that is closest to the original one. While two components also do a reasonable job here, it might sometimes happen (KMeans is used to initialise the cluster centres if not done by hand, using a random cluster initialisation) that the upper two Gaussians are grouped, re-run for a couple of times to see this. This illustrates how EM might get stuck in a local minimum. We will do this below, where it might well happen that all runs produce the same or different results - no guarantees.\n", + "It seems that three comonents give a density that is closest to the original one. While two components also do a reasonable job here, it might sometimes happen (KMeans is used to initialise the cluster centres if not done by hand, using a random cluster initialisation) that the upper two Gaussians are grouped, re-run for a couple of times to see this. This illustrates how EM might get stuck in a local minimum. We will do this below, where it might well happen that all runs produce the same or different results - no guarantees.\n", "Note that it is easily possible to initialise EM via specifying the parameters of the mixture components as did to create the original model above.\n", "\n", "One way to decide which of multiple convergenced EM instances to use is to simply compute many of them (with different initialisations) and then choose the one with the largest likelihood. *WARNING* Do not select the number of components like this as the model will overfit." diff --git a/doc/ipython-notebooks/clustering/KMeans.ipynb b/doc/ipython-notebooks/clustering/KMeans.ipynb index 1cb8a59dea1..ae8a2436816 100644 --- a/doc/ipython-notebooks/clustering/KMeans.ipynb +++ b/doc/ipython-notebooks/clustering/KMeans.ipynb @@ -33,7 +33,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The KMeans clustering algorithm is used to partition a space of n observations into k partitions (or clusters). Each of these clusters is denoted by the mean of the observation vectors belonging to it and a unique label which is attached to all the observations belonging to it. Thus, in general, the algorithm takes parameter k and an observation matrix (along with the notion of distance between points ie distance metric) as input and returns mean of each of the k clusters along with labels indicating belongingness of each observations. Let us construct a simple example to understand how it is done in Shogun using the CKMeans class. " + "The KMeans clustering algorithm is used to partition a space of n observations into k partitions (or clusters). Each of these clusters is denoted by the mean of the observation vectors belonging to it and a unique label which is attached to all the observations belonging to it. Thus, in general, the algorithm takes parameter k and an observation matrix (along with the notion of distance between points ie distance metric) as input and returns mean of each of the k clusters along with labels indicating belongingness of each observations. Let us construct a simple example to understand how it is done in Shogun using the KMeans class. " ] }, { diff --git a/doc/ipython-notebooks/computer_vision/Scene_classification.ipynb b/doc/ipython-notebooks/computer_vision/Scene_classification.ipynb index 37793100f69..379d39b4909 100644 --- a/doc/ipython-notebooks/computer_vision/Scene_classification.ipynb +++ b/doc/ipython-notebooks/computer_vision/Scene_classification.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook is about performing Object Categorization using SIFT descriptors of keypoints as features, and SVMs to predict the category of the object present in the image. Shogun's K-Means clustering is employed for generating the bag of keypoints and its k-nearest neighbours module is extensively used to construct the feature vectors. " + "This notebook is about performing Object Categorization using SIFT descriptors of keypoints as features, and SVMs to predict the category of the object present in the image. Shogun's K-Means clustering is employed for generating the bag of keypoints and its k-nearest neighbours module is extensively used to construct the feature vectors. " ] }, { @@ -168,7 +168,7 @@ "***2. Group similar descriptors into an arbitrary number of clusters***.\n", "\n", "We take all the descriptors that we got from the three images above and find similarity in between them.\n", - "Here, similarity is decided by Euclidean distance between the 128-element SIFT descriptors. Similar descriptors are clustered into **k** number of groups. This can be done using Shogun's **KMeans class**. These clusters are called **bags of keypoints** or **visual words** and they collectively represent the **vocabulary** of the program. Each cluster has a **cluster center**, which can be thought of as the representative descriptor of all the descriptors belonging to that cluster. These cluster centers can be found using the **get_cluster_centers()** method.\n", + "Here, similarity is decided by Euclidean distance between the 128-element SIFT descriptors. Similar descriptors are clustered into **k** number of groups. This can be done using Shogun's **KMeans class**. These clusters are called **bags of keypoints** or **visual words** and they collectively represent the **vocabulary** of the program. Each cluster has a **cluster center**, which can be thought of as the representative descriptor of all the descriptors belonging to that cluster. These cluster centers can be found using the **get_cluster_centers()** method.\n", "\n", "To perform clustering into **k** groups, we define the **get_similar_descriptors()** function below." ] @@ -222,7 +222,7 @@ " * We begin by computing **SIFT** descriptors for each training image.\n", " \n", " \n", - " * For each training image, associate each of its descriptors with one of the clusters in the vocabulary. The simplest way to do this is by using **k-Nearest Neighbour** approach. This can be done using Shogun's KNN class. Euclidean distance measure is used here for finding out the neighbours.\n", + " * For each training image, associate each of its descriptors with one of the clusters in the vocabulary. The simplest way to do this is by using **k-Nearest Neighbour** approach. This can be done using Shogun's KNN class. Euclidean distance measure is used here for finding out the neighbours.\n", " \n", " \n", " * Making a histogram from this association. This histogram has as many bins as there are clusters in the vocabulary. Each bin counts how many descriptors in the training image are associated with the cluster corresponding to that bin. Intuitively, this histogram describes the image in the **visual words** of the **vocabulary,** and is called the **bag of visual words descriptor** of the image. \n", @@ -403,7 +403,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We have to solve a multiclass classification problem here. In Shogun these are implemented in: MulticlassMachine \n", + "We have to solve a multiclass classification problem here. In Shogun these are implemented in: MulticlassMachine \n", "\n", "***4. We train a one-vs-all SVM for each category of object using the training data***:\n", "\n", diff --git a/doc/ipython-notebooks/distributions/KernelDensity.ipynb b/doc/ipython-notebooks/distributions/KernelDensity.ipynb index ba49e1d683d..f030f303374 100644 --- a/doc/ipython-notebooks/distributions/KernelDensity.ipynb +++ b/doc/ipython-notebooks/distributions/KernelDensity.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook is on using the Shogun Machine Learning Toolbox for [kernel density estimation](http://en.wikipedia.org/wiki/Kernel_density_estimation) (KDE). We start with a brief overview of KDE. Then we demonstrate the use of Shogun's [$KernelDensity$](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelDensity.html) class on a toy example. Finally, we apply KDE to a real world example, thus demonstrating the its prowess as a non-parametric statistical method." + "This notebook is on using the Shogun Machine Learning Toolbox for [kernel density estimation](http://en.wikipedia.org/wiki/Kernel_density_estimation) (KDE). We start with a brief overview of KDE. Then we demonstrate the use of Shogun's [$KernelDensity$](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelDensity.html) class on a toy example. Finally, we apply KDE to a real world example, thus demonstrating the its prowess as a non-parametric statistical method." ] }, { diff --git a/doc/ipython-notebooks/evaluation/xval_modelselection.ipynb b/doc/ipython-notebooks/evaluation/xval_modelselection.ipynb index 96c6a31c1c7..8d0f241b94b 100644 --- a/doc/ipython-notebooks/evaluation/xval_modelselection.ipynb +++ b/doc/ipython-notebooks/evaluation/xval_modelselection.ipynb @@ -98,7 +98,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As said earlier Cross-validation is based upon splitting the data into multiple partitions. Shogun has various strategies for this. The base class for them is [CSplittingStrategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSplittingStrategy.html)." + "As said earlier Cross-validation is based upon splitting the data into multiple partitions. Shogun has various strategies for this. The base class for them is [SplittingStrategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SplittingStrategy.html)." ] }, { @@ -136,7 +136,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "On classificaiton data, the best choice is [stratified cross-validation](http://en.wikipedia.org/wiki/Cross-validation_%28statistics%29#Common_types_of_cross-validation). This divides the data in such way that the fraction of labels in each partition is roughly the same, which reduces the variance of the performance estimate quite a bit, in particular for data with more than two classes. In Shogun this is implemented by [CStratifiedCrossValidationSplitting](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CStratifiedCrossValidationSplitting.html) class." + "On classificaiton data, the best choice is [stratified cross-validation](http://en.wikipedia.org/wiki/Cross-validation_%28statistics%29#Common_types_of_cross-validation). This divides the data in such way that the fraction of labels in each partition is roughly the same, which reduces the variance of the performance estimate quite a bit, in particular for data with more than two classes. In Shogun this is implemented by [StratifiedCrossValidationSplitting](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1StratifiedCrossValidationSplitting.html) class." ] }, { @@ -298,7 +298,7 @@ "source": [ "Now, the training error is zero. This seems good at first. But is this setting of the parameters a good idea? No! A good performance on the training data alone does not mean anything. A simple look up table is able to produce zero error on training data. What we want is that our methods generalises the input data somehow to perform well on unseen data. We will now use cross-validation to estimate the performance on such.\n", "\n", - " We will use [CStratifiedCrossValidationSplitting](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CStratifiedCrossValidationSplitting.html), which accepts a reference to the labels and the number of partitions as parameters. This instance is then passed to the class [CCrossValidation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCrossValidation.html), which does the estimation using the desired splitting strategy. The latter class can take all algorithms that are implemented against the [CMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMachine.html) interface." + " We will use [StratifiedCrossValidationSplitting](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1StratifiedCrossValidationSplitting.html), which accepts a reference to the labels and the number of partitions as parameters. This instance is then passed to the class [CrossValidation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CrossValidation.html), which does the estimation using the desired splitting strategy. The latter class can take all algorithms that are implemented against the [Machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Machine.html) interface." ] }, { @@ -403,7 +403,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This gives a brute-force way to select paramters of any algorithm implemented under the [CMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMachine.html) interface. The cool thing about this is, that it is also possible to compare different model families against each other. Below, we compare a a number of regression models in Shogun on the Boston Housing dataset." + "This gives a brute-force way to select paramters of any algorithm implemented under the [Machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Machine.html) interface. The cool thing about this is, that it is also possible to compare different model families against each other. Below, we compare a a number of regression models in Shogun on the Boston Housing dataset." ] }, { @@ -452,7 +452,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let us use cross-validation to compare various values of tau paramter for ridge regression ([Regression notebook](http://www.shogun-toolbox.org/static/notebook/current/Regression.html)). We will use [MeanSquaredError](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMeanSquaredError.html) as the performance metric. Note that normal splitting is used since it might be impossible to generate \"good\" splits using Stratified splitting in case of regression since we have continous values for labels. " + "Let us use cross-validation to compare various values of tau paramter for ridge regression ([Regression notebook](http://www.shogun-toolbox.org/static/notebook/current/Regression.html)). We will use [MeanSquaredError](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MeanSquaredError.html) as the performance metric. Note that normal splitting is used since it might be impossible to generate \"good\" splits using Stratified splitting in case of regression since we have continous values for labels. " ] }, { @@ -512,7 +512,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A low value of error certifies a good pick for the tau paramter which should be easy to conclude from the plots. In case of Ridge Regression the value of tau i.e. the amount of regularization doesn't seem to matter but does seem to in case of Kernel Ridge Regression. One interpretation of this could be the lack of over fitting in the feature space for ridge regression and the occurence of over fitting in the new kernel space in which Kernel Ridge Regression operates.
Next we will compare a range of values for the width of [Gaussian Kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianKernel.html) used in [Kernel Ridge Regression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelRidgeRegression.html)" + "A low value of error certifies a good pick for the tau paramter which should be easy to conclude from the plots. In case of Ridge Regression the value of tau i.e. the amount of regularization doesn't seem to matter but does seem to in case of Kernel Ridge Regression. One interpretation of this could be the lack of over fitting in the feature space for ridge regression and the occurence of over fitting in the new kernel space in which Kernel Ridge Regression operates.
Next we will compare a range of values for the width of [Gaussian Kernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianKernel.html) used in [Kernel Ridge Regression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelRidgeRegression.html)" ] }, { @@ -647,7 +647,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "A standard way of selecting the best parameters of a learning algorithm is by Grid Search. This is done by an exhaustive search of a specified parameter space. [CModelSelectionParameters](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CModelSelectionParameters.html) is used to select various parameters and their ranges to be used for model selection. A tree like structure is used where the nodes can be [CSGObject](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSGObject.html) or the parameters to the object. The range of values to be searched for the parameters is set using `build_values()` method." + "A standard way of selecting the best parameters of a learning algorithm is by Grid Search. This is done by an exhaustive search of a specified parameter space. [ModelSelectionParameters](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1ModelSelectionParameters.html) is used to select various parameters and their ranges to be used for model selection. A tree like structure is used where the nodes can be [SGObject](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGObject.html) or the parameters to the object. The range of values to be searched for the parameters is set using `build_values()` method." ] }, { @@ -676,7 +676,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Next we will create [CModelSelectionParameters](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CModelSelectionParameters.html) instance with a kernel object which has to be appended the root node. The kernel object itself will be append with a kernel width parameter which is the parameter we wish to search." + "Next we will create [ModelSelectionParameters](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1ModelSelectionParameters.html) instance with a kernel object which has to be appended the root node. The kernel object itself will be append with a kernel width parameter which is the parameter we wish to search." ] }, { diff --git a/doc/ipython-notebooks/gaussian_process/gaussian_processes.ipynb b/doc/ipython-notebooks/gaussian_process/gaussian_processes.ipynb index d31187395a2..1144d3b1238 100644 --- a/doc/ipython-notebooks/gaussian_process/gaussian_processes.ipynb +++ b/doc/ipython-notebooks/gaussian_process/gaussian_processes.ipynb @@ -84,7 +84,7 @@ "\n", "In order to solve the above integrals, Shogun offers a variety of approximations. Don't worry, you will not have to deal with these nasty integrals on your own, but everything is hidden within Shogun. Though, if you like to play with these objects, you will be able to compute only parts.\n", "\n", - "Note that in the above description, we did not make any assumptions on the input space $\\mathcal{X}$. As long as you define mean and covariance functions, and a likelihood, your data can have *any* form you like. Shogun in fact is able to deal with standard dense numerical data, sparse data, and strings of *any* type, and many more *out of the box*. We will provide some examples below.\n", + "Note that in the above description, we did not make any assumptions on the input space $\\mathcal{X}$. As long as you define mean and covariance functions, and a likelihood, your data can have *any* form you like. Shogun in fact is able to deal with standard dense numerical data, sparse data, and strings of *any* type, and many more *out of the box*. We will provide some examples below.\n", "\n", "To gain some intuition how these latent Gaussian variables behave, and how to model data with them, see the regression part of this notebook." ] @@ -100,11 +100,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Bayesian regression with Gaussian Processes is among the most fundamental applications of latent Gaussian models. As usual, the oberved data come from a contintous space, i.e. $\\mathbf{y}\\in\\mathbb{R}^n$, which is represented in the Shogun class CRegressionLabels. We assume that these observations come from some distribution $p(\\mathbf{y}|\\mathbf{f)}$ that is based on a fixed state of latent Gaussian response variables $\\mathbf{f}\\in\\mathbb{R}^n$. In fact, we assume that the true model *is* the latent Gaussian response variable (which defined a *distribution* over functions; plus some Gaussian observation noise which is modelled by the likelihood as\n", + "Bayesian regression with Gaussian Processes is among the most fundamental applications of latent Gaussian models. As usual, the oberved data come from a contintous space, i.e. $\\mathbf{y}\\in\\mathbb{R}^n$, which is represented in the Shogun class RegressionLabels. We assume that these observations come from some distribution $p(\\mathbf{y}|\\mathbf{f)}$ that is based on a fixed state of latent Gaussian response variables $\\mathbf{f}\\in\\mathbb{R}^n$. In fact, we assume that the true model *is* the latent Gaussian response variable (which defined a *distribution* over functions; plus some Gaussian observation noise which is modelled by the likelihood as\n", "\n", " $p(\\mathbf{y}|\\mathbf{f})=\\mathcal{N}(\\mathbf{f},\\sigma^2\\mathbf{I})$\n", " \n", - " This simple likelihood is implemented in the Shogun class CGaussianLikelihood. It is the well known bell curve. Below, we plot the likelihood as a function of $\\mathbf{y}$, for $n=1$." + " This simple likelihood is implemented in the Shogun class GaussianLikelihood. It is the well known bell curve. Below, we plot the likelihood as a function of $\\mathbf{y}$, for $n=1$." ] }, { @@ -171,11 +171,11 @@ "\n", "where $\\mathbf{m}_\\theta$ is the mean function's mean and $\\mathbf{C}_\\theta$ is the pairwise covariance or kernel matrix of the input covariates $\\mathbf{x}_i$. This means, we can easily sample function realisations $\\mathbf{f}^{(j)}$ from the Gaussian Process, and more important, visualise them.\n", "\n", - "To this end, let us consider the well-known and often used *Gaussian Kernel* or *squared exponential covariance*, which is implemented in the Shogun class CGaussianKernel in the parametric from (note that there are other forms in the literature)\n", + "To this end, let us consider the well-known and often used *Gaussian Kernel* or *squared exponential covariance*, which is implemented in the Shogun class GaussianKernel in the parametric from (note that there are other forms in the literature)\n", "\n", "$ k(\\mathbf{x}, \\mathbf{x}')=\\exp\\left( -\\frac{||\\mathbf{x}-\\mathbf{x}'||_2^2}{\\tau}\\right),$\n", "\n", - "where $\\tau$ is a hyper-parameter of the kernel. We will also use the constant CZeroMean mean function, which is suitable if the data's mean is zero (which can be achieved via removing it).\n", + "where $\\tau$ is a hyper-parameter of the kernel. We will also use the constant ZeroMean mean function, which is suitable if the data's mean is zero (which can be achieved via removing it).\n", "\n", "Let us consider some toy regression data in the form of a sine wave, which is observed at random points with some observations noise." ] @@ -218,7 +218,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "First, we compute the kernel matrix $\\mathbf{C}_\\boldsymbol{\\theta}$ using the CGaussianKernel with hyperparameter $\\boldsymbol{\\theta}=\\{\\tau\\}$ with a few differnt values. Note that in Gaussian Processes, kernels usually have a scaling parameter. We skip this one for now and cover it later." + "First, we compute the kernel matrix $\\mathbf{C}_\\boldsymbol{\\theta}$ using the GaussianKernel with hyperparameter $\\boldsymbol{\\theta}=\\{\\tau\\}$ with a few differnt values. Note that in Gaussian Processes, kernels usually have a scaling parameter. We skip this one for now and cover it later." ] }, { @@ -256,7 +256,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This matrix, as any kernel or covariance matrix, is positive semi-definite and symmetric. It can be viewed as a similarity matrix. Here, elements on the diagonal (corresponding to $\\mathbf{x}=\\mathbf{x}'$) have largest similarity. For increasing kernel bandwidth $\\tau$, more and more elements are similar. This matrix fully specifies a distribution over functions $f(\\mathbf{x}):\\mathcal{X}\\rightarrow\\mathbb{R}$ over a finite set of latent Gaussian variables $\\mathbf{f}$, which we can sample from and plot. To this end, we use the Shogun class CStatistics, which offers a method to sample from multivariate Gaussians." + "This matrix, as any kernel or covariance matrix, is positive semi-definite and symmetric. It can be viewed as a similarity matrix. Here, elements on the diagonal (corresponding to $\\mathbf{x}=\\mathbf{x}'$) have largest similarity. For increasing kernel bandwidth $\\tau$, more and more elements are similar. This matrix fully specifies a distribution over functions $f(\\mathbf{x}):\\mathcal{X}\\rightarrow\\mathbb{R}$ over a finite set of latent Gaussian variables $\\mathbf{f}$, which we can sample from and plot. To this end, we use the Shogun class Statistics, which offers a method to sample from multivariate Gaussians." ] }, { @@ -291,7 +291,7 @@ "source": [ "Note how the functions are exactly evaluated at the training covariates $\\mathbf{x}_i$ which are randomly distributed on the x-axis. Even though these points do not visualise the full functions (we can only evaluate them at a *finite* number of points, but we connected the points with lines to make it more clear), this reveils that larger values of the kernel bandwidth $\\tau$ lead to smoother latent Gaussian functions.\n", "\n", - "In the above plots all functions are equally possible. That is, the prior of the latent Gaussian variables $\\mathbf{f}|\\boldsymbol{\\theta}$ does not favour any particular function setups. Computing the posterior given our training data, the distribution ober $\\mathbf{f}|\\mathbf{y},\\boldsymbol{\\theta}$ then corresponds to restricting the above distribution over functions to those that explain the training data (up to observation noise). We will now use the Shogun class CExactInferenceMethod to do exactly this. The class is the general basis of exact GP regression in Shogun. We have to define all parts of the Gaussian Process for the inference method." + "In the above plots all functions are equally possible. That is, the prior of the latent Gaussian variables $\\mathbf{f}|\\boldsymbol{\\theta}$ does not favour any particular function setups. Computing the posterior given our training data, the distribution ober $\\mathbf{f}|\\mathbf{y},\\boldsymbol{\\theta}$ then corresponds to restricting the above distribution over functions to those that explain the training data (up to observation noise). We will now use the Shogun class ExactInferenceMethod to do exactly this. The class is the general basis of exact GP regression in Shogun. We have to define all parts of the Gaussian Process for the inference method." ] }, { @@ -330,7 +330,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Note how the above function samples are constrained to go through our training data labels (up to observation noise), as much as their smoothness allows them. In fact, these are already samples from the predictive distribution, which gives a probability for a label $\\mathbf{y}^*$ for any covariate $\\mathbf{x}^*$. These distributions are Gaussian (!), nice to look at and extremely useful to understand the GP's underlying model. Let's plot them. We finally use the Shogun class CGaussianProcessRegression to represent the whole GP under an interface to perform inference with. In addition, we use the helper class class CGaussianDistribution to evaluate the log-likelihood for every test point's $\\mathbf{x}^*_j$ value $\\mathbf{y}_j^*$." + "Note how the above function samples are constrained to go through our training data labels (up to observation noise), as much as their smoothness allows them. In fact, these are already samples from the predictive distribution, which gives a probability for a label $\\mathbf{y}^*$ for any covariate $\\mathbf{x}^*$. These distributions are Gaussian (!), nice to look at and extremely useful to understand the GP's underlying model. Let's plot them. We finally use the Shogun class GaussianProcessRegression to represent the whole GP under an interface to perform inference with. In addition, we use the helper class class GaussianDistribution to evaluate the log-likelihood for every test point's $\\mathbf{x}^*_j$ value $\\mathbf{y}_j^*$." ] }, { @@ -475,13 +475,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In binary classification, the observed data comes from a space of discrete, binary labels, i.e. $\\mathbf{y}\\in\\mathcal{Y}^n=\\{-1,+1\\}^n$, which are represented via the Shogun class CBinaryLabels. To model these observations with a GP, we need a likelihood function $p(\\mathbf{y}|\\mathbf{f})$ that maps a set of such discrete observations to a probability, given a fixed response $\\mathbf{f}$ of the Gaussian Process.\n", + "In binary classification, the observed data comes from a space of discrete, binary labels, i.e. $\\mathbf{y}\\in\\mathcal{Y}^n=\\{-1,+1\\}^n$, which are represented via the Shogun class BinaryLabels. To model these observations with a GP, we need a likelihood function $p(\\mathbf{y}|\\mathbf{f})$ that maps a set of such discrete observations to a probability, given a fixed response $\\mathbf{f}$ of the Gaussian Process.\n", "\n", "In regression, this way straight-forward, as we could simply use the response variable $\\mathbf{f}$ itself, plus some Gaussian noise, which gave rise to a probability distribution. However, now that the $\\mathbf{y}$ are discrete, we cannot do the same thing. We rather need a function that squashes the Gaussian response variable itself to a probability, given some data. This is a common problem in Machine Learning and Statistics and is usually done with some sort of *Sigmoid* function of the form $\\sigma:\\mathbb{R}\\rightarrow[0,1]$. One popular choicefor such a function is the *Logit* likelihood, given by\n", "\n", "$p(\\mathbf{y}|\\mathbf{f})=\\prod_{i=1}^n p(y_i|f_i)=\\prod_{i=1}^n \\frac{1}{1-\\exp(-y_i f_i)}.$\n", "\n", - "This likelihood is implemented in Shogun under CLogitLikelihood and using it is sometimes refered to as *logistic regression*. Using it with GPs results in non-linear Bayesian logistic regression. We can easily use the class to illustrate the sigmoid function for a 1D example and a fixed data point with label $+1$" + "This likelihood is implemented in Shogun under LogitLikelihood and using it is sometimes refered to as *logistic regression*. Using it with GPs results in non-linear Bayesian logistic regression. We can easily use the class to illustrate the sigmoid function for a 1D example and a fixed data point with label $+1$" ] }, { @@ -532,7 +532,7 @@ "\n", "$p(\\mathbf{y}|\\mathbf{f})=\\prod_{i=1}^n p(y_i|f_i)=\\prod_{i=1}^n \\Phi(y_i f_i),$\n", "\n", - "where $\\Phi:\\mathbb{R}\\rightarrow [0,1]$ is the cumulative distribution function (CDF) of the standard Gaussian distribution $\\mathcal{N}(0,1)$. It is implemented in the Shogun class CProbitLikelihood and using it is refered to as *probit regression*. While the Gaussian CDF has some convinient properties for integrating over it (and thus allowing some different modelling decisions), it doesn not really matter what you use in Shogun in most cases. However, for the sake of completeness, it is also potted above, being very similar to the logit likelihood." + "where $\\Phi:\\mathbb{R}\\rightarrow [0,1]$ is the cumulative distribution function (CDF) of the standard Gaussian distribution $\\mathcal{N}(0,1)$. It is implemented in the Shogun class ProbitLikelihood and using it is refered to as *probit regression*. While the Gaussian CDF has some convinient properties for integrating over it (and thus allowing some different modelling decisions), it doesn not really matter what you use in Shogun in most cases. However, for the sake of completeness, it is also potted above, being very similar to the logit likelihood." ] }, { @@ -554,7 +554,7 @@ "\n", "$p(\\mathbf{y}|\\boldsymbol{\\theta})=\\int p(\\mathbf{y}|\\mathbf{f})p(\\mathbf{f}|\\boldsymbol{\\theta})d\\mathbf{f}|\\boldsymbol{\\theta}$.\n", "\n", - "In classification, the second integral is not available in closed form since it is the convolution of a Gaussian, $p(\\mathbf{f}|\\boldsymbol{\\theta})$, and a non-Gaussian, $p(\\mathbf{y}|\\mathbf{f})$, distribution. Therefore, we have to rely on approximations in order to compute and integrate over the posterior $p(\\mathbf{f}|\\mathbf{y},\\boldsymbol{\\theta})$. Shogun offers various standard methods from the literature to deal with this problem, including the Laplace approximation (CLaplacianInferenceMethod), Expectation Propagation (CEPInferenceMethod) for inference and evaluatiing the marginal likelihood. These two approximations give rise to a Gaussian posterior $p(\\mathbf{f}|\\mathbf{y},\\boldsymbol{\\theta})$, which can then be easily computed and integrated over (all this is done by Shogun for you).\n", + "In classification, the second integral is not available in closed form since it is the convolution of a Gaussian, $p(\\mathbf{f}|\\boldsymbol{\\theta})$, and a non-Gaussian, $p(\\mathbf{y}|\\mathbf{f})$, distribution. Therefore, we have to rely on approximations in order to compute and integrate over the posterior $p(\\mathbf{f}|\\mathbf{y},\\boldsymbol{\\theta})$. Shogun offers various standard methods from the literature to deal with this problem, including the Laplace approximation (CLaplacianInferenceMethod), Expectation Propagation (EPInferenceMethod) for inference and evaluatiing the marginal likelihood. These two approximations give rise to a Gaussian posterior $p(\\mathbf{f}|\\mathbf{y},\\boldsymbol{\\theta})$, which can then be easily computed and integrated over (all this is done by Shogun for you).\n", "\n", "While the Laplace approximation is quite fast, EP usually has a better accuracy, in particular if one is not just interetsed in binary decisions but also in certainty values for these predictions. Go for Laplace if interested in binary decisions, and for EP otherwise.\n", "\n", @@ -614,7 +614,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We will now pass this data into Shogun representation, and use the standard Gaussian kernel (or squared exponential covariance function (CGaussianKernel)) and the Laplace approximation to obtain a decision boundary for the two classes. You can easily exchange different likelihood models and inference methods." + "We will now pass this data into Shogun representation, and use the standard Gaussian kernel (or squared exponential covariance function (GaussianKernel)) and the Laplace approximation to obtain a decision boundary for the two classes. You can easily exchange different likelihood models and inference methods." ] }, { @@ -682,7 +682,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This is already quite nice. The nice thing about Gaussian Processes now is that they are Bayesian, which means that have a full predictive distribution, i.e., we can plot the probability for a point belonging to a class. These can be obtained via the interface of CGaussianProcessClassification" + "This is already quite nice. The nice thing about Gaussian Processes now is that they are Bayesian, which means that have a full predictive distribution, i.e., we can plot the probability for a point belonging to a class. These can be obtained via the interface of GaussianProcessClassification" ] }, { @@ -803,7 +803,7 @@ "\n", "is the probability of the data given the model parameters $\\boldsymbol{\\theta}$. Note that this is averaged over *all* possible configurations of the latent Gaussian variables $\\mathbf{f}|\\boldsymbol{\\theta}$ given a fixed configuration of parameters. However, since this is probability distribution, it has to integrate to $1$. This means that models that are too complex (and thus being able to explain too many different data configutations) and models that are too simple (and thus not able to explain the current data) give rise to a small marginal likelihood. Only when the model is just complex enough to explain the data well (but not more complex), the marginal likelihood is maximised. This is an implementation of a concept called Occam's razor, and is a nice motivation why you should be Bayesian if you can -- overfitting doesn't happen that quickly.\n", "\n", - "As mentioned before, Shogun is able to automagically learn all of the hyper-parameters $\\boldsymbol{\\theta}$ using gradient based optimisation on the marginal likelihood (whose derivatives are computed internally). To this is, we use the class CGradientModelSelection. Note that we could also use CGridSearchModelSelection to do a standard grid-search, such as is done for Support Vector Machines. However, this is highly ineffective, in particular when the number of parameters grows. In addition, order to evaluate parameter states, we have to use the classes CGradientEvaluation, and GradientCriterion, which is also much cheaper than the usual CCrossValidation, since it just evaluates the gradient of the marginal likelihood rather than performing many training and testing runs. This is another very nice motivation for using Gaussian Processes: optimising parameters is much easier. In the following, we demonstrate how to select *all* parameters of the used model. In Shogun, parameter configurations (corresponding to $\\boldsymbol{\\theta}$ are stored in instances of CParameterCombination, which can be applied to machines.\n", + "As mentioned before, Shogun is able to automagically learn all of the hyper-parameters $\\boldsymbol{\\theta}$ using gradient based optimisation on the marginal likelihood (whose derivatives are computed internally). To this is, we use the class CGradientModelSelection. Note that we could also use CGridSearchModelSelection to do a standard grid-search, such as is done for Support Vector Machines. However, this is highly ineffective, in particular when the number of parameters grows. In addition, order to evaluate parameter states, we have to use the classes CGradientEvaluation, and GradientCriterion, which is also much cheaper than the usual CrossValidation, since it just evaluates the gradient of the marginal likelihood rather than performing many training and testing runs. This is another very nice motivation for using Gaussian Processes: optimising parameters is much easier. In the following, we demonstrate how to select *all* parameters of the used model. In Shogun, parameter configurations (corresponding to $\\boldsymbol{\\theta}$ are stored in instances of CParameterCombination, which can be applied to machines.\n", "\n", "This approach is known as *maximum likelihood II* (the 2 is for the second level, averaging over all possible $\\mathbf{f}|\\boldsymbol{\\theta}$), or *evidence maximisation*." ] @@ -845,7 +845,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This now gives us a trained Gaussian Process with the best hyper-parameters. In the above setting, this is the s CGaussianKernel bandwith, and its scale (which is stored in the GP itself since Shogun kernels do not support scalling). We can now again visualise the predictive distribution, and also output the best parameters." + "This now gives us a trained Gaussian Process with the best hyper-parameters. In the above setting, this is the s GaussianKernel bandwith, and its scale (which is stored in the GP itself since Shogun kernels do not support scalling). We can now again visualise the predictive distribution, and also output the best parameters." ] }, { @@ -935,7 +935,7 @@ "\n", "The optimal way to specify inducing points is to densely and uniformly place them in the input space. However, this might be quickly become infeasible in high dimensions. In this case, a random subset of the training data might be a good idea.\n", "\n", - "In Shogun, the class CFITCInferenceMethod handles inference for regression with the CGaussianLikelihood. Below, we demonstrate its usage on a toy example and compare to exact regression. Note that CGradientModelSelection still works as before. We compare the runtime for inference with both GP.\n", + "In Shogun, the class FITCInferenceMethod handles inference for regression with the GaussianLikelihood. Below, we demonstrate its usage on a toy example and compare to exact regression. Note that CGradientModelSelection still works as before. We compare the runtime for inference with both GP.\n", "\n", "First, note that changing the inference method only requires the change of a single line of code" ] diff --git a/doc/ipython-notebooks/gaussian_process/variational_classifier.ipynb b/doc/ipython-notebooks/gaussian_process/variational_classifier.ipynb index d8dfc86f6b7..852765472e6 100644 --- a/doc/ipython-notebooks/gaussian_process/variational_classifier.ipynb +++ b/doc/ipython-notebooks/gaussian_process/variational_classifier.ipynb @@ -326,14 +326,14 @@ "\n", "\n", "Gaussian Numerical Integration for One-dimensional Space\n", - " LogitVGLikelihood (Bernoulli-logistic function) \n", + " LogitVGLikelihood (Bernoulli-logistic function) \n", "\n", "\n", "\n", - "Laplace Method,\n", - "Covariance Variational Method,\n", - "Cholesky Variational Method,\n", - "Mean-field Variational Method\n", + "Laplace Method,\n", + "Covariance Variational Method,\n", + "Cholesky Variational Method,\n", + "Mean-field Variational Method\n", "\n", "\n", "\n", @@ -341,12 +341,12 @@ "\n", "\n", "The Piecewise Variational Bound for Bernoulli-logistic function\n", - "LogitVGPiecewiseBoundLikelihood\n", + "LogitVGPiecewiseBoundLikelihood\n", "\n", - "Laplace Method,\n", - "Covariance Variational Method,\n", - "Mean-field Variational Method,\n", - "Cholesky Variational Method\n", + "Laplace Method,\n", + "Covariance Variational Method,\n", + "Mean-field Variational Method,\n", + "Cholesky Variational Method\n", "\n", "\n", "\n", @@ -400,23 +400,23 @@ "Idea of Approximation\n", "\n", "\n", - "Laplace Method\n", + "Laplace Method\n", "Using the second-order Taylor expansion in the mode of the true posterior\n", "\n", "\n", - "Covariance Variational Method\n", + "Covariance Variational Method\n", "Minimizing KL(approximated distribution || true posterior), \n", "where the approximated Gaussian distribution has a complete covariance matrix\n", "\n", "\n", "\n", - "Mean-field Variational Method\n", + "Mean-field Variational Method\n", "Minimizing KL(approximated distribution || true posterior), \n", "where the approximated Gaussian distribution has a diagonal covariance matrix\n", "\n", "\n", "\n", - "Cholesky Variational Method\n", + "Cholesky Variational Method\n", "Minimizing KL(approximated distribution || true posterior), \n", "where the approximated Gaussian distribution has a complete covariance matrix in \n", "Cholesky representation\n", @@ -429,7 +429,7 @@ "\n", "\n", "\n", - "Expectation Propagation Method\n", + "Expectation Propagation Method\n", "Minimizing KL(true posterior || approximated distribution), where the approximated distribution is a Gaussian distribution\n", "\n", "\n", @@ -456,16 +456,16 @@ "\n", "Limited-memory BFGS (LBFGS)\n", "\n", - "Laplace Method,\n", - "Covariance Variational Method (default optimizer),\n", - "Mean-field Variational Method (default optimizer),\n", + "Laplace Method,\n", + "Covariance Variational Method (default optimizer),\n", + "Mean-field Variational Method (default optimizer),\n", "Dual Variational Method (default optimizer),\n", - "Cholesky Variational Method (default optimizer)\n", + "Cholesky Variational Method (default optimizer)\n", "\n", "\n", "\n", "Newton-Raphson\n", - "Laplace Method (default optimizer)\n", + "Laplace Method (default optimizer)\n", "\n", "\n", "\n", @@ -497,10 +497,10 @@ "\n", "MoreThuente method\n", "\n", - "Laplace Method,\n", - "Covariance Variational Method,\n", - "Mean-field Variational Method,\n", - "Cholesky Variational Method,\n", + "Laplace Method,\n", + "Covariance Variational Method,\n", + "Mean-field Variational Method,\n", + "Cholesky Variational Method,\n", "(For now, **NOT** support Dual Variational Method)\n", "\n", "0\n", @@ -510,11 +510,11 @@ "\n", "\n", "\n", - "Laplace Method,\n", - "Covariance Variational Method,\n", - "Mean-field Variational Method,\n", + "Laplace Method,\n", + "Covariance Variational Method,\n", + "Mean-field Variational Method,\n", "Dual Variational Method,\n", - "Cholesky Variational Method\n", + "Cholesky Variational Method\n", "\n", "1\n", "\n", @@ -522,11 +522,11 @@ "Backtracking method with the (regular or strong) Wolfe condition\n", "\n", "\n", - "Laplace Method,\n", - "Covariance Variational Method,\n", - "Mean-field Variational Method,\n", + "Laplace Method,\n", + "Covariance Variational Method,\n", + "Mean-field Variational Method,\n", "Dual Variational Method,\n", - "Cholesky Variational Method\n", + "Cholesky Variational Method\n", "\n", "2 (regular),3 (strong, default line search method)\n", "\n", @@ -1962,9 +1962,9 @@ "metadata": {}, "source": [ "Now, we discuss how to approximate $E_{q_i}[ln(p(\\mathbf{y_i}|\\mathbf{f_i})]$. Please see section Dealing with the Non-closed Form Issue in GPC Models if you are not familar with variational bounds.\n", - "Since this term is about one-dimensional integration, for GPC we can use Gauss–Hermite quadrature to appromxiate this term. In Shogun, the corresponding implementation of Bernoulli-logistic likelihood for variational inference is called LogitVGLikelihood . This likelihood can be used for all variational methods (primal form) except the dual variational method (dual form).\n", + "Since this term is about one-dimensional integration, for GPC we can use Gauss–Hermite quadrature to appromxiate this term. In Shogun, the corresponding implementation of Bernoulli-logistic likelihood for variational inference is called LogitVGLikelihood . This likelihood can be used for all variational methods (primal form) except the dual variational method (dual form).\n", "\n", - "The piecewise variational bound proposed at the paper of Benjamin M. Marlin et. al. in 2011 is also implemented in Shogun, which is called LogitVGPiecewiseBoundLikelihood. This likelihood can be used for all variational methods except the dual variational method.\n", + "The piecewise variational bound proposed at the paper of Benjamin M. Marlin et. al. in 2011 is also implemented in Shogun, which is called LogitVGPiecewiseBoundLikelihood. This likelihood can be used for all variational methods except the dual variational method.\n", "\n", "For dual variational method, we use the variational bound in the paper of DM Blei et. al. in 2007. The corresponding implementation in Shogun is called LogitDVGLikelihood. Note that when using LogitDVGLikelihood, the bound is only enabled for the dual variational method. When other variational methods use this class, it uses one-dimensional integration instead of the variational bound. For detailed discussion about variational bounds for GPC models, please refer to Khan's work." ] diff --git a/doc/ipython-notebooks/ica/bss_audio.ipynb b/doc/ipython-notebooks/ica/bss_audio.ipynb index 9093b02dd32..70b1362ede8 100644 --- a/doc/ipython-notebooks/ica/bss_audio.ipynb +++ b/doc/ipython-notebooks/ica/bss_audio.ipynb @@ -18,7 +18,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook illustrates Blind Source Seperation(BSS) on audio signals using Independent Component Analysis (ICA) in Shogun. We generate a mixed signal and try to seperate it out using Shogun's implementation of ICA & BSS called JADE." + "This notebook illustrates Blind Source Seperation(BSS) on audio signals using Independent Component Analysis (ICA) in Shogun. We generate a mixed signal and try to seperate it out using Shogun's implementation of ICA & BSS called JADE." ] }, { diff --git a/doc/ipython-notebooks/intro/Introduction.ipynb b/doc/ipython-notebooks/intro/Introduction.ipynb index 577a5cf154b..27513cdbf73 100644 --- a/doc/ipython-notebooks/intro/Introduction.ipynb +++ b/doc/ipython-notebooks/intro/Introduction.ipynb @@ -30,7 +30,7 @@ "3. [Feature representations](#Feature-representations)\n", "4. [Labels](#Assigning-labels)\n", "5. [Preprocessing data](#Preprocessing-data)\n", - "6. [Supervised Learning with Shogun's CMachine interface](#supervised)\n", + "6. [Supervised Learning with Shogun's Machine interface](#supervised)\n", "7. [Evaluating performance and Model selection](#Evaluating-performance-and-Model-selection)\n", "8. [Example: Regression](#More-predictions:-Regression)" ] @@ -92,7 +92,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun provides the capability to load datasets of different formats using [CFile](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CFile.html).
A real world dataset: [Pima Indians Diabetes data set](http://archive.ics.uci.edu/ml/datasets/Pima+Indians+Diabetes) is used now. We load the `LibSVM` format file using Shogun's [LibSVMFile](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVMFile.html) class. The `LibSVM` format is: $$\\space \\text {label}\\space \\text{attribute1:value1 attribute2:value2 }...$$$$\\space.$$$$\\space .$$ LibSVM uses the so called \"sparse\" format where zero values do not need to be stored." + "Shogun provides the capability to load datasets of different formats using [File](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1File.html).
A real world dataset: [Pima Indians Diabetes data set](http://archive.ics.uci.edu/ml/datasets/Pima+Indians+Diabetes) is used now. We load the `LibSVM` format file using Shogun's [LibSVMFile](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVMFile.html) class. The `LibSVM` format is: $$\\space \\text {label}\\space \\text{attribute1:value1 attribute2:value2 }...$$$$\\space.$$$$\\space .$$ LibSVM uses the so called \"sparse\" format where zero values do not need to be stored." ] }, { @@ -123,7 +123,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To get off the mark, let us see how Shogun handles the attributes of the data using [CFeatures](http://shogun-toolbox.org/api/latest/classshogun_1_1CFeatures.html) class. Shogun supports wide range of feature representations. We believe it is a good idea to have different forms of data, rather than converting them all into matrices. Among these are: $\\hspace {20mm}$
  • [String features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CStringFeatures.html): Implements a list of strings. Not limited to character strings, but could also be sequences of floating point numbers etc. Have varying dimensions.
  • [Dense features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CDenseFeatures.html): Implements dense feature matrices
  • [Sparse features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CSparseFeatures.html): Implements sparse matrices.
  • [Streaming features](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CStreamingFeatures.html): For algorithms working on data streams (which are too large to fit into memory)
" + "To get off the mark, let us see how Shogun handles the attributes of the data using [Features](http://shogun-toolbox.org/api/latest/classshogun_1_1Features.html) class. Shogun supports wide range of feature representations. We believe it is a good idea to have different forms of data, rather than converting them all into matrices. Among these are: $\\hspace {20mm}$
  • [String features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CStringFeatures.html): Implements a list of strings. Not limited to character strings, but could also be sequences of floating point numbers etc. Have varying dimensions.
  • [Dense features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1DenseFeatures.html): Implements dense feature matrices
  • [Sparse features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1SparseFeatures.html): Implements sparse matrices.
  • [Streaming features](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1StreamingFeatures.html): For algorithms working on data streams (which are too large to fit into memory)
" ] }, { @@ -157,7 +157,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In numpy, this is a matrix of 2 row-vectors of dimension 768. However, in Shogun, this will be a matrix of 768 column vectors of dimension 2. This is beacuse each data sample is stored in a column-major fashion, meaning each column here corresponds to an individual sample and each row in it to an atribute like BMI, Glucose concentration etc. To convert the extracted matrix into Shogun format, `RealFeatures` are used which are nothing but the above mentioned [Dense features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CDenseFeatures.html) of `64bit Float` type. To do this call the factory method, `features` with the matrix (this should be a 64bit 2D numpy array) as the argument. " + "In numpy, this is a matrix of 2 row-vectors of dimension 768. However, in Shogun, this will be a matrix of 768 column vectors of dimension 2. This is beacuse each data sample is stored in a column-major fashion, meaning each column here corresponds to an individual sample and each row in it to an atribute like BMI, Glucose concentration etc. To convert the extracted matrix into Shogun format, `RealFeatures` are used which are nothing but the above mentioned [Dense features](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1DenseFeatures.html) of `64bit Float` type. To do this call the factory method, `features` with the matrix (this should be a 64bit 2D numpy array) as the argument. " ] }, { @@ -210,7 +210,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In supervised learning problems, training data is labelled. Shogun provides various types of labels to do this through [Clabels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLabels.html). Some of these are:
  • [Binary labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CBinaryLabels.html): Binary Labels for binary classification which can have values +1 or -1.
  • [Multiclass labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMulticlassLabels.html): Multiclass Labels for multi-class classification which can have values from 0 to (num. of classes-1).
  • [Regression labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CRegressionLabels.html): Real-valued labels used for regression problems and are returned as output of classifiers.
  • [Structured labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CStructuredLabels.html): Class of the labels used in Structured Output (SO) problems

In this particular problem, our data can be of two types: diabetic or non-diabetic, so we need [binary labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CBinaryLabels.html). This makes it a [Binary Classification problem](http://en.wikipedia.org/wiki/Binary_classification), where the data has to be classified in two groups." + "In supervised learning problems, training data is labelled. Shogun provides various types of labels to do this through [Clabels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Labels.html). Some of these are:
  • [Binary labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1BinaryLabels.html): Binary Labels for binary classification which can have values +1 or -1.
  • [Multiclass labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MulticlassLabels.html): Multiclass Labels for multi-class classification which can have values from 0 to (num. of classes-1).
  • [Regression labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1RegressionLabels.html): Real-valued labels used for regression problems and are returned as output of classifiers.
  • [Structured labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1StructuredLabels.html): Class of the labels used in Structured Output (SO) problems

In this particular problem, our data can be of two types: diabetic or non-diabetic, so we need [binary labels](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1BinaryLabels.html). This makes it a [Binary Classification problem](http://en.wikipedia.org/wiki/Binary_classification), where the data has to be classified in two groups." ] }, { @@ -251,7 +251,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It is usually better to preprocess data to a standard form rather than handling it in raw form. The reasons are having a well behaved-scaling, many algorithms assume centered data, and that sometimes one wants to de-noise data (with say PCA). Preprocessors do not change the domain of the input features. It is possible to do various type of preprocessing using methods provided by [CPreprocessor](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPreprocessor.html) class. Some of these are:
  • [Norm one](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNormOne.html): Normalize vector to have norm 1.
  • [PruneVarSubMean](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html): Substract the mean and remove features that have zero variance.
  • [Dimension Reduction](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDimensionReductionPreprocessor.html): Lower the dimensionality of given simple features.
    • [PCA](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPCA.html): Principal component analysis.
    • [Kernel PCA](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelPCA.html): PCA using kernel methods.
The training data will now be preprocessed using [CPruneVarSubMean](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html). This will basically remove data with zero variance and subtract the mean. Passing a `True` to the constructor makes the class normalise the varaince of the variables. It basically dividies every dimension through its standard-deviation. This is the reason behind removing dimensions with constant values. It is required to initialize the preprocessor by passing the feature object to `init` before doing anything else. The raw and processed data is now plotted." + "It is usually better to preprocess data to a standard form rather than handling it in raw form. The reasons are having a well behaved-scaling, many algorithms assume centered data, and that sometimes one wants to de-noise data (with say PCA). Preprocessors do not change the domain of the input features. It is possible to do various type of preprocessing using methods provided by [Preprocessor](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Preprocessor.html) class. Some of these are:
  • [Norm one](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNormOne.html): Normalize vector to have norm 1.
  • [PruneVarSubMean](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html): Substract the mean and remove features that have zero variance.
  • [Dimension Reduction](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDimensionReductionPreprocessor.html): Lower the dimensionality of given simple features.
    • [PCA](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPCA.html): Principal component analysis.
    • [Kernel PCA](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelPCA.html): PCA using kernel methods.
The training data will now be preprocessed using [CPruneVarSubMean](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html). This will basically remove data with zero variance and subtract the mean. Passing a `True` to the constructor makes the class normalise the varaince of the variables. It basically dividies every dimension through its standard-deviation. This is the reason behind removing dimensions with constant values. It is required to initialize the preprocessor by passing the feature object to `init` before doing anything else. The raw and processed data is now plotted." ] }, { @@ -314,21 +314,21 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Supervised Learning with Shogun's CMachine interface" + "### Supervised Learning with Shogun's Machine interface" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "[CMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMachine.html) is Shogun's interface for general learning machines. Basically one has to ` train()` the machine on some training data to be able to learn from it. Then we `apply()` it to test data to get predictions. Some of these are:
  • [Kernel machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelMachine.html): Kernel based learning tools.
  • [Linear machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLinearMachine.html): Interface for all kinds of linear machines like classifiers.
  • [Distance machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDistanceMachine.html): A distance machine is based on a a-priori choosen distance.
  • [Gaussian process machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianProcessMachine.html): A base class for Gaussian Processes.
  • And many more
" + "[Machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Machine.html) is Shogun's interface for general learning machines. Basically one has to ` train()` the machine on some training data to be able to learn from it. Then we `apply()` it to test data to get predictions. Some of these are:
  • [Kernel machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelMachine.html): Kernel based learning tools.
  • [Linear machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LinearMachine.html): Interface for all kinds of linear machines like classifiers.
  • [Distance machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DistanceMachine.html): A distance machine is based on a a-priori choosen distance.
  • [Gaussian process machine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianProcessMachine.html): A base class for Gaussian Processes.
  • And many more
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Moving on to the prediction part, [Liblinear](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1CLibLinear.html), a linear SVM is used to do the classification (more on SVMs in [this notebook](http://www.shogun-toolbox.org/static/notebook/current/SupportVectorMachines.html)). A linear SVM will find a linear separation with the largest possible margin. Here C is a penalty parameter on the loss function. " + "Moving on to the prediction part, [Liblinear](http://www.shogun-toolbox.org/doc/en/current/classshogun_1_1LibLinear.html), a linear SVM is used to do the classification (more on SVMs in [this notebook](http://www.shogun-toolbox.org/static/notebook/current/SupportVectorMachines.html)). A linear SVM will find a linear separation with the largest possible margin. Here C is a penalty parameter on the loss function. " ] }, { @@ -433,7 +433,7 @@ "metadata": {}, "source": [ "For this problem, a linear classifier does a reasonable job in distinguishing labelled data. An interpretation could be that individuals below a certain level of BMI and glucose are likely to have no Diabetes. \n", - "For problems where the data cannot be separated linearly, there are more advanced classification methods, as for example all of Shogun's [kernel machines](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelMachine.html), but more on this later. To play with this interactively have a look at this: [web demo](http://demos.shogun-toolbox.org/classifier/binary/) " + "For problems where the data cannot be separated linearly, there are more advanced classification methods, as for example all of Shogun's [kernel machines](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelMachine.html), but more on this later. To play with this interactively have a look at this: [web demo](http://demos.shogun-toolbox.org/classifier/binary/) " ] }, { @@ -450,9 +450,9 @@ "How do you assess the quality of a prediction? Shogun provides various ways to do this using [CEvaluation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CEvaluation.html). The preformance is evaluated by comparing the predicted output and the expected output. Some of the base classes for performance measures are:\n", "\n", "* [Binary class evaluation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CBinaryClassEvaluation.html): used to evaluate binary classification labels. \n", - "* [Clustering evaluation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CClusteringEvaluation.html): used to evaluate clustering.\n", + "* [Clustering evaluation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1ClusteringEvaluation.html): used to evaluate clustering.\n", "* [Mean absolute error](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMeanAbsoluteError.html): used to compute an error of regression model.\n", - "* [Multiclass accuracy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMulticlassAccuracy.html): used to compute accuracy of multiclass classification. \n", + "* [Multiclass accuracy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MulticlassAccuracy.html): used to compute accuracy of multiclass classification. \n", "\n", "Evaluating on training data should be avoided since the learner may adjust to very specific random features of the training data which are not very important to the general relation. This is called [overfitting](http://en.wikipedia.org/wiki/Overfitting). Maximising performance on the training examples usually results in algorithms explaining the noise in data (rather than actual patterns), which leads to bad performance on unseen data. The dataset will now be split into two, we train on one part and evaluate performance on other using [CAccuracyMeasure](http://shogun-toolbox.org/api/latest/classshogun_1_1CAccuracyMeasure.html)." ] @@ -568,7 +568,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The tool we will use here to perform regression is [Kernel ridge regression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelRidgeRegression.html). Kernel Ridge Regression is a non-parametric version of ridge regression where the [kernel trick](http://en.wikipedia.org/wiki/Kernel_trick) is used to solve a related linear ridge regression problem in a higher-dimensional space, whose results correspond to non-linear regression in the data-space. Again we train on the data and apply on the XY grid to get predicitions." + "The tool we will use here to perform regression is [Kernel ridge regression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelRidgeRegression.html). Kernel Ridge Regression is a non-parametric version of ridge regression where the [kernel trick](http://en.wikipedia.org/wiki/Kernel_trick) is used to solve a related linear ridge regression problem in a higher-dimensional space, whose results correspond to non-linear regression in the data-space. Again we train on the data and apply on the XY grid to get predicitions." ] }, { diff --git a/doc/ipython-notebooks/logdet/logdet.ipynb b/doc/ipython-notebooks/logdet/logdet.ipynb index b13c1e302bc..7c53db30a6b 100644 --- a/doc/ipython-notebooks/logdet/logdet.ipynb +++ b/doc/ipython-notebooks/logdet/logdet.ipynb @@ -81,7 +81,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "First, to keep the notion of Krylov subspace, we view the matrix as a linear operator that applies on a vector, resulting a new vector. We use RealSparseMatrixOperator that is suitable for this example. All the solvers work with LinearOperator type objects. For computing the eigenvalues, we use LanczosEigenSolver class. Although computation of the Eigenvalues is done internally within the log-determinant estimator itself (see below), here we explicitely precompute them." + "First, to keep the notion of Krylov subspace, we view the matrix as a linear operator that applies on a vector, resulting a new vector. We use RealSparseMatrixOperator that is suitable for this example. All the solvers work with LinearOperator type objects. For computing the eigenvalues, we use LanczosEigenSolver class. Although computation of the Eigenvalues is done internally within the log-determinant estimator itself (see below), here we explicitely precompute them." ] }, { @@ -134,7 +134,7 @@ "source": [ "

This corresponds to averaging over 13 source vectors rather than one (but has much lower variance as using 13 Gaussian source vectors). A comparison between the convergence behavior of using probing sampler and Gaussian sampler is presented later.

\n", "\n", - "

Then we define LogRationalApproximationCGM operator function class, which internally uses the Eigensolver to compute the Eigenvalues, uses JacobiEllipticFunctions to compute the complex shifts, weights and the constant multiplier in the rational approximation expression, takes the probing vector generated by the trace sampler and then uses CG-M solver (CGMShiftedFamilySolver) to solve the shifted systems. Precompute is not necessary here too.

" + "

Then we define LogRationalApproximationCGM operator function class, which internally uses the Eigensolver to compute the Eigenvalues, uses JacobiEllipticFunctions to compute the complex shifts, weights and the constant multiplier in the rational approximation expression, takes the probing vector generated by the trace sampler and then uses CG-M solver (CGMShiftedFamilySolver) to solve the shifted systems. Precompute is not necessary here too.

" ] }, { @@ -162,7 +162,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Finally, we use the LogDetEstimator class to sample the log-determinant of the matrix." + "Finally, we use the LogDetEstimator class to sample the log-determinant of the matrix." ] }, { @@ -189,7 +189,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To verify the accuracy of the estimate, we compute exact log-determinant of A using Cholesky factorization using Statistics::log_det method." + "To verify the accuracy of the estimate, we compute exact log-determinant of A using Cholesky factorization using Statistics::log_det method." ] }, { @@ -414,7 +414,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "

1. Linear Operators

\n", + "

1. Linear Operators

\n", "All the linear solvers and Eigen solvers work with linear operators. Both real valued and complex valued operators are supported for dense/sparse matrix linear operators." ] }, @@ -448,7 +448,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "

2. Linear Solvers

\n", + "

2. Linear Solvers

\n", "

Conjugate Gradient based iterative solvers, that construct the Krylov subspace in their iteration by computing matrix-vector products are most useful for solving sparse linear systems. Here is an overview of CG based solvers that are currently available in Shogun.

\n", "

Conjugate Gradient Solver

\n", "This solver solves for system $\\mathbf{Qx}=\\mathbf{y}$, where $\\mathbf{Q}$ is real-valued spd linear operator (e.g. dense/sparse matrix operator), and $\\mathbf{y}$ is real vector." @@ -495,7 +495,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "

Conjugate Orthogonal CG Solver

\n", + "

Conjugate Orthogonal CG Solver

\n", "Solves for systems $\\mathbf{Qx}=\\mathbf{z}$, where $\\mathbf{Q}$ is symmetric but non-Hermitian (i.e. having complex entries in its diagonal) and $\\mathbf{z}$ is real valued vector." ] }, @@ -541,7 +541,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "

CG-M Shifted Family Solver

\n", + "

CG-M Shifted Family Solver

\n", "Solves for systems with real valued spd matrices with complex shifts. For using it with log-det, an option to specify the weight of each solution is also there. The solve_shifted_weighted method returns $\\sum\\alpha_{l}\\mathbf{x}_{l}$ where $\\mathbf{x}_{l}=(\\mathbf{A}+\\sigma_{l}\\mathbf{I})^{-1}\\mathbf{y}$, $\\sigma,\\alpha\\in\\mathbb{C}$, $\\mathbf{y}\\in\\mathbb{R}$." ] }, diff --git a/doc/ipython-notebooks/metric/LMNN.ipynb b/doc/ipython-notebooks/metric/LMNN.ipynb index 8472cbfd146..05fa8ca0098 100644 --- a/doc/ipython-notebooks/metric/LMNN.ipynb +++ b/doc/ipython-notebooks/metric/LMNN.ipynb @@ -151,7 +151,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now, let us use the [LMNN class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLMNN.html) implemented in Shogun to find the distance and plot its associated ellipse. If everything goes well, we will see that the new ellipse only overlaps with the data points of the green class." + "Now, let us use the [LMNN class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LMNN.html) implemented in Shogun to find the distance and plot its associated ellipse. If everything goes well, we will see that the new ellipse only overlaps with the data points of the green class." ] }, { @@ -202,7 +202,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "LMNN is an iterative algorithm. The argument given to [`train`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLMNN.html#ab1b8bbdb8390415ac3ae7dc655cb512d) represents the initial state of the solution. By default, if no argument is given, then LMNN uses [PCA](http://en.wikipedia.org/wiki/Principal_component_analysis) to obtain this initial value." + "LMNN is an iterative algorithm. The argument given to [`train`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LMNN.html#ab1b8bbdb8390415ac3ae7dc655cb512d) represents the initial state of the solution. By default, if no argument is given, then LMNN uses [PCA](http://en.wikipedia.org/wiki/Principal_component_analysis) to obtain this initial value." ] }, { @@ -490,7 +490,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "So, 1472 features! Those are quite many features indeed. In other words, the feature vectors at hand lie on a 1472-dimensional space. We cannot visualize in the input feature space how the feature vectors look like. However, in order to gain a little bit more of understanding of the data, we can apply dimension reduction, embed the feature vectors in a two-dimensional space, and plot the vectors in the embedded space. To this end, we are going to use one of the [many](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CEmbeddingConverter.html) methods for dimension reduction included in Shogun. In this case, we are using t-distributed stochastic neighbour embedding (or [t-dsne](http://jmlr.org/papers/v9/vandermaaten08a.html)). This method is particularly suited to produce low-dimensional embeddings (two or three dimensions) that are straightforward to visualize." + "So, 1472 features! Those are quite many features indeed. In other words, the feature vectors at hand lie on a 1472-dimensional space. We cannot visualize in the input feature space how the feature vectors look like. However, in order to gain a little bit more of understanding of the data, we can apply dimension reduction, embed the feature vectors in a two-dimensional space, and plot the vectors in the embedded space. To this end, we are going to use one of the [many](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1EmbeddingConverter.html) methods for dimension reduction included in Shogun. In this case, we are using t-distributed stochastic neighbour embedding (or [t-dsne](http://jmlr.org/papers/v9/vandermaaten08a.html)). This method is particularly suited to produce low-dimensional embeddings (two or three dimensions) that are straightforward to visualize." ] }, { @@ -533,7 +533,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Even before applying LMNN to the ape gut data set, let us apply kNN classification and study how it performs using the typical Euclidean distance. Furthermore, since this data set is rather small in terms of number of examples, the kNN error above may vary considerably (I have observed variation of almost 20% a few times) across different runs. To get a robust estimate of how kNN performs in the data set, we will perform cross-validation using [Shogun's framework](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCrossValidation.html) for evaluation. This will give us a reliable result regarding how well kNN performs in this data set." + "Even before applying LMNN to the ape gut data set, let us apply kNN classification and study how it performs using the typical Euclidean distance. Furthermore, since this data set is rather small in terms of number of examples, the kNN error above may vary considerably (I have observed variation of almost 20% a few times) across different runs. To get a robust estimate of how kNN performs in the data set, we will perform cross-validation using [Shogun's framework](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CrossValidation.html) for evaluation. This will give us a reliable result regarding how well kNN performs in this data set." ] }, { @@ -586,7 +586,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Nonetheless, LMNN may still turn out to be very useful in a data set like this one. Making a small modification of the vanilla LMNN algorithm, we can enforce that the linear transform found by LMNN is diagonal. This means that LMNN can be used to weight each of the features and, once the training is performed, read from these weights which features are relevant to apply kNN and which ones are not. This is indeed a form of *feature selection*. Using Shogun, it is extremely easy to switch to this so-called *diagonal* mode for LMNN: just call the method [`set_diagonal(use_diagonal)`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLMNN.html#ad2f03dbad3ad08ab76aecbb656a486e6) with `use_diagonal` set to `True`." + "Nonetheless, LMNN may still turn out to be very useful in a data set like this one. Making a small modification of the vanilla LMNN algorithm, we can enforce that the linear transform found by LMNN is diagonal. This means that LMNN can be used to weight each of the features and, once the training is performed, read from these weights which features are relevant to apply kNN and which ones are not. This is indeed a form of *feature selection*. Using Shogun, it is extremely easy to switch to this so-called *diagonal* mode for LMNN: just call the method [`set_diagonal(use_diagonal)`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LMNN.html#ad2f03dbad3ad08ab76aecbb656a486e6) with `use_diagonal` set to `True`." ] }, { @@ -666,7 +666,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In order to avoid disastrous situations, in Shogun we have implemented LMNN with really demanding criteria for automatic termination of the training process. Albeit, it is possible to tune the termination criteria using the methods [`set_stepsize_threshold`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLMNN.html#a76b6914cf9d1a53b0c9ecd828c7edbcb) and [`set_obj_threshold`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLMNN.html#af78c7dd9ed2307c0d53e7383cdc01a24). These methods can be used to modify the lower bound required in the step size and the increment in the objective (relative to its absolute value), respectively, to stop training. Also, it is possible to set a hard upper bound on the number of iterations using [`set_maxiter`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLMNN.html#afcf319806eb710a0d9535fbeddf93795) as we have done above. In case the internal termination criteria did not fire before the maximum number of iterations was reached, you will receive a warning message, similar to the one shown above. This is not a synonym that the training went wrong; but it is strongly recommended at this event to have a look at the objective plot as we have done in the previous block of code." + "In order to avoid disastrous situations, in Shogun we have implemented LMNN with really demanding criteria for automatic termination of the training process. Albeit, it is possible to tune the termination criteria using the methods [`set_stepsize_threshold`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LMNN.html#a76b6914cf9d1a53b0c9ecd828c7edbcb) and [`set_obj_threshold`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LMNN.html#af78c7dd9ed2307c0d53e7383cdc01a24). These methods can be used to modify the lower bound required in the step size and the increment in the objective (relative to its absolute value), respectively, to stop training. Also, it is possible to set a hard upper bound on the number of iterations using [`set_maxiter`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LMNN.html#afcf319806eb710a0d9535fbeddf93795) as we have done above. In case the internal termination criteria did not fire before the maximum number of iterations was reached, you will receive a warning message, similar to the one shown above. This is not a synonym that the training went wrong; but it is strongly recommended at this event to have a look at the objective plot as we have done in the previous block of code." ] }, { @@ -802,7 +802,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In order to produce a more fair comparison, we will rescale the data so that all the feature dimensions are within the interval [0,1]. Luckily, there is a [preprocessor](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPreprocessor.html) class in Shogun that makes this straightforward." + "In order to produce a more fair comparison, we will rescale the data so that all the feature dimensions are within the interval [0,1]. Luckily, there is a [preprocessor](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Preprocessor.html) class in Shogun that makes this straightforward." ] }, { diff --git a/doc/ipython-notebooks/multiclass/KNN.ipynb b/doc/ipython-notebooks/multiclass/KNN.ipynb index 769015fc17d..0d4f56bf06b 100644 --- a/doc/ipython-notebooks/multiclass/KNN.ipynb +++ b/doc/ipython-notebooks/multiclass/KNN.ipynb @@ -34,7 +34,7 @@ "source": [ "The training of a KNN model basically does nothing but memorizing all the training points and the associated labels, which is very cheap in computation but costly in storage. The prediction is implemented by finding the K nearest neighbors of the query point, and voting. Here K is a hyper-parameter for the algorithm. Smaller values for K give the model low bias but high variance; while larger values for K give low variance but high bias.\n", "\n", - "In `SHOGUN`, you can use [CKNN](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKNN.html) to perform KNN learning. To construct a KNN machine, you must choose the hyper-parameter K and a distance function. Usually, we simply use the standard [CEuclideanDistance](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CEuclideanDistance.html), but in general, any subclass of [CDistance](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDistance.html) could be used. For demonstration, in this tutorial we select a random subset of 1000 samples from the USPS digit recognition dataset, and run 2-fold cross validation of KNN with varying K." + "In `SHOGUN`, you can use [KNN](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KNN.html) to perform KNN learning. To construct a KNN machine, you must choose the hyper-parameter K and a distance function. Usually, we simply use the standard [EuclideanDistance](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1EuclideanDistance.html), but in general, any subclass of [Distance](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Distance.html) could be used. For demonstration, in this tutorial we select a random subset of 1000 samples from the USPS digit recognition dataset, and run 2-fold cross validation of KNN with varying K." ] }, { diff --git a/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb b/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb index f4dd7f2e638..c70638f680c 100644 --- a/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb +++ b/doc/ipython-notebooks/multiclass/Tree/DecisionTrees.ipynb @@ -968,7 +968,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The CART algorithm is a popular decision tree learning algorithm introduced by Breiman et al. Unlike ID3 and C4.5, the learnt decision tree in this case can be used for both multiclass classification and regression depending on the type of dependent variable. The tree growing process comprises of recursive binary splitting of nodes. To find the best split at each node, all possible splits of all available predictive attributes are considered. The best split is the one that maximises some splitting criterion. For classification tasks, ie. when the dependent attribute is categorical, the [Gini index](http://en.wikipedia.org/wiki/Decision_tree_learning#Gini_impurity) is used as the splitting criterion. For regression tasks, ie. when the dependent variable is continuous, the [least squares deviation](http://en.wikipedia.org/wiki/Least_squares) is used. Let us learn about [Shogun's CART implementation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCARTree.html) by working on two toy problems, one on classification and the other on regression." + "The CART algorithm is a popular decision tree learning algorithm introduced by Breiman et al. Unlike ID3 and C4.5, the learnt decision tree in this case can be used for both multiclass classification and regression depending on the type of dependent variable. The tree growing process comprises of recursive binary splitting of nodes. To find the best split at each node, all possible splits of all available predictive attributes are considered. The best split is the one that maximises some splitting criterion. For classification tasks, ie. when the dependent attribute is categorical, the [Gini index](http://en.wikipedia.org/wiki/Decision_tree_learning#Gini_impurity) is used as the splitting criterion. For regression tasks, ie. when the dependent variable is continuous, the [least squares deviation](http://en.wikipedia.org/wiki/Least_squares) is used. Let us learn about [Shogun's CART implementation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CARTree.html) by working on two toy problems, one on classification and the other on regression." ] }, { @@ -1187,7 +1187,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In this section, we will apply the CART algorithm on the Iris dataset. Remember that the Iris dataset provides us with just a training dataset and no separate test dataset. In case of the C4.5 example discussed earlier, we ourselves divided the entire training dataset into training subset and test subset. In this section, we will employ a different strategy i.e. cross validation. In cross-validation, we divide the training dataset into ```n``` subsets where ```n``` is a user controlled parameter. We perform ```n``` iterations of training and testing in which, at each iteration, we choose one of the ```n``` subsets as our test dataset and the remaining ```n-1``` subsets as our training dataset. The performance of the model is usually taken as the average of the performances in various iterations. [Shogun's cross validation class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCrossValidation.html) makes it really easy to apply cross-validation to any model of our choice. Let us realize this by applying cross-validation to CART-tree trained over Iris dataset. We start by reading the data. " + "In this section, we will apply the CART algorithm on the Iris dataset. Remember that the Iris dataset provides us with just a training dataset and no separate test dataset. In case of the C4.5 example discussed earlier, we ourselves divided the entire training dataset into training subset and test subset. In this section, we will employ a different strategy i.e. cross validation. In cross-validation, we divide the training dataset into ```n``` subsets where ```n``` is a user controlled parameter. We perform ```n``` iterations of training and testing in which, at each iteration, we choose one of the ```n``` subsets as our test dataset and the remaining ```n-1``` subsets as our training dataset. The performance of the model is usually taken as the average of the performances in various iterations. [Shogun's cross validation class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CrossValidation.html) makes it really easy to apply cross-validation to any model of our choice. Let us realize this by applying cross-validation to CART-tree trained over Iris dataset. We start by reading the data. " ] }, { @@ -1291,7 +1291,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We get a mean accuracy of about 0.93-0.94. This number essentially means that a CART-tree trained using this dataset is expected to classify Iris flowers, given their required attributes, with an accuracy of 93-94% in a real world scenario. The parameters required by Shogun's cross-validation class should be noted in the above code snippet. The class requires the model, training features, training labels, [splitting strategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSplittingStrategy.html) and [evaluation method](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CEvaluation.html) to be specified. " + "We get a mean accuracy of about 0.93-0.94. This number essentially means that a CART-tree trained using this dataset is expected to classify Iris flowers, given their required attributes, with an accuracy of 93-94% in a real world scenario. The parameters required by Shogun's cross-validation class should be noted in the above code snippet. The class requires the model, training features, training labels, [splitting strategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SplittingStrategy.html) and [evaluation method](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CEvaluation.html) to be specified. " ] }, { @@ -1336,7 +1336,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The servo dataset is a small training dataset (contains just 167 training vectors) with no separate test dataset, like the Iris dataset. Hence we will apply the same cross-validation strategy we applied in case of the Iris dataset. However, to make things interesting let us play around with a yet-untouched parameter of CART-induced tree i.e. the [maximum allowed tree depth](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCARTree.html#a8ae1a862da33e74ab9391b44dfff708f). As the tree depth increases, the tree becomes more complex and hence fits the training data more closely. By setting a maximum allowed tree depth, we restrict the complexity of trained tree and hence avoid over-fitting. But choosing a low value of the maximum allowed tree depth may lead to early stopping i.e. under-fitting. Let us explore how we can decide the appropriate value of the max-allowed-tree-depth parameter. Let us create a method, which takes max-allowed-tree-depth parameter as input and returns the corresponding cross-validated error as output." + "The servo dataset is a small training dataset (contains just 167 training vectors) with no separate test dataset, like the Iris dataset. Hence we will apply the same cross-validation strategy we applied in case of the Iris dataset. However, to make things interesting let us play around with a yet-untouched parameter of CART-induced tree i.e. the [maximum allowed tree depth](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CARTree.html#a8ae1a862da33e74ab9391b44dfff708f). As the tree depth increases, the tree becomes more complex and hence fits the training data more closely. By setting a maximum allowed tree depth, we restrict the complexity of trained tree and hence avoid over-fitting. But choosing a low value of the maximum allowed tree depth may lead to early stopping i.e. under-fitting. Let us explore how we can decide the appropriate value of the max-allowed-tree-depth parameter. Let us create a method, which takes max-allowed-tree-depth parameter as input and returns the corresponding cross-validated error as output." ] }, { @@ -1589,7 +1589,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In this section, we will try to estimate the quality of wine based on 13 attributes like alcohol content, malic acid, magnesium content, etc. using the [wine dataset](https://archive.ics.uci.edu/ml/datasets/Wine). Let us first read the dataset using Shogun's [CSV file reader](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCSVFile.html)." + "In this section, we will try to estimate the quality of wine based on 13 attributes like alcohol content, malic acid, magnesium content, etc. using the [wine dataset](https://archive.ics.uci.edu/ml/datasets/Wine). Let us first read the dataset using Shogun's [CSV file reader](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSVFile.html)." ] }, { @@ -1670,7 +1670,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In this section, we try to predict the value of houses in Boston using 13 attributes, like per capita crime rate in neighborhood, number of rooms, nitrous oxide concentration in air, proportion of non-retail business in the area etc. Out of the 13 attributes 12 are continuous and 1 (the Charles river dummy variable) is binary nominal. Let us load the dataset as our first step. For this, we can directly use Shogun's [CSV file reader class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCSVFile.html)." + "In this section, we try to predict the value of houses in Boston using 13 attributes, like per capita crime rate in neighborhood, number of rooms, nitrous oxide concentration in air, proportion of non-retail business in the area etc. Out of the 13 attributes 12 are continuous and 1 (the Charles river dummy variable) is binary nominal. Let us load the dataset as our first step. For this, we can directly use Shogun's [CSV file reader class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSVFile.html)." ] }, { diff --git a/doc/ipython-notebooks/multiclass/Tree/TreeEnsemble.ipynb b/doc/ipython-notebooks/multiclass/Tree/TreeEnsemble.ipynb index 14a581d9a59..b3e5f6590ec 100644 --- a/doc/ipython-notebooks/multiclass/Tree/TreeEnsemble.ipynb +++ b/doc/ipython-notebooks/multiclass/Tree/TreeEnsemble.ipynb @@ -101,7 +101,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In the above code snippet, we decided to create a forest using 10 trees in which each split in individual trees will be using a randomly chosen subset of 4 features. Note that 4 here is the square root of the total available features (16) and is hence the usually chosen value as mentioned in the introductory paragraph. The strategy for combination chosen is [Majority Vote](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMajorityVote.html) which, as the name suggests, chooses the mode of all the individual tree outputs. The given features are all continuous in nature and hence feature types are all set false (i.e. not nominal). Next, we train our Random Forest and use it to classify letters in our test dataset. " + "In the above code snippet, we decided to create a forest using 10 trees in which each split in individual trees will be using a randomly chosen subset of 4 features. Note that 4 here is the square root of the total available features (16) and is hence the usually chosen value as mentioned in the introductory paragraph. The strategy for combination chosen is [Majority Vote](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MajorityVote.html) which, as the name suggests, chooses the mode of all the individual tree outputs. The given features are all continuous in nature and hence feature types are all set false (i.e. not nominal). Next, we train our Random Forest and use it to classify letters in our test dataset. " ] }, { @@ -128,7 +128,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We have with us the labels predicted by our Random Forest model. Let us also get the predictions made by a single tree. For this purpose, we train a [CART-flavoured decision tree](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCARTree.html)." + "We have with us the labels predicted by our Random Forest model. Let us also get the predictions made by a single tree. For this purpose, we train a [CART-flavoured decision tree](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CARTree.html)." ] }, { diff --git a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb index 1c3bb651499..6c6ceb079e7 100644 --- a/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb +++ b/doc/ipython-notebooks/multiclass/multiclass_reduction.ipynb @@ -58,7 +58,7 @@ "## One-vs-Rest and One-vs-One\n", "\n", "The *One-vs-Rest* strategy is implemented in\n", - "`CMulticlassOneVsRestStrategy`. As indicated by the name, this\n", + "`MulticlassOneVsRestStrategy`. As indicated by the name, this\n", "strategy reduce a $K$-class problem to $K$ binary sub-problems. For the $k$-th\n", "problem, where $k\\in\\{1,\\ldots,K\\}$, the samples from class $k$ are colored as\n", "$+1$, and the samples from other classes are colored as $-1$. The multiclass\n", @@ -79,7 +79,7 @@ "classifiers are well-tuned regularized classifiers such as support vector\n", "machines.\n", "\n", - "Implemented in [CMulticlassOneVsOneStrategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMulticlassOneVsOneStrategy.html), the \n", + "Implemented in [MulticlassOneVsOneStrategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MulticlassOneVsOneStrategy.html), the \n", "*One-vs-One* strategy is another simple and intuitive \n", "strategy: it basically produces one binary problem for each pair of classes. So there will be $\\binom{K}{2}$ binary problems. At prediction time, the \n", "output of every binary classifiers are collected to do voting for the $K$ \n", @@ -93,7 +93,7 @@ "demonstration, we randomly 200 samples from each class for training and 200 \n", "samples from each class for testing.\n", "\n", - "The [CLibLinear](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibLinear.html) is used as the base binary classifier in a [CLinearMulticlassMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLinearMulticlassMachine.html), with One-vs-Rest and One-vs-One strategies. The running time and performance (on my machine) is reported below:" + "The [LibLinear](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LibLinear.html) is used as the base binary classifier in a [LinearMulticlassMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1LinearMulticlassMachine.html), with One-vs-Rest and One-vs-One strategies. The running time and performance (on my machine) is reported below:" ] }, { @@ -456,9 +456,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Expanding on the idea of creating a generic multiclass machine and then assigning a particular multiclass strategy and a base binary machine, one can also use the [KernelMulticlassMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelMulticlassMachine.html) with a kernel of choice.\n", + "Expanding on the idea of creating a generic multiclass machine and then assigning a particular multiclass strategy and a base binary machine, one can also use the [KernelMulticlassMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelMulticlassMachine.html) with a kernel of choice.\n", "\n", - "Here we will use a [GaussianKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianKernel.html) with [LibSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVM.html) as the classifer.\n", + "Here we will use a [GaussianKernel](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianKernel.html) with [LibSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVM.html) as the classifer.\n", "All we have to do is define a new helper evaluate function with the features defined as in the above examples." ] }, @@ -508,7 +508,7 @@ "source": [ "So we have seen that we can classify multiclass samples using a base binary machine. If we dwell on this a bit more, we can easily spot the intuition behind this.\n", "\n", - "The [MulticlassOneVsRestStrategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMulticlassOneVsOneStrategy.html) classifies one class against the rest of the classes. This is done for each and every class by training a separate classifier for it.So we will have total $k$ classifiers where $k$ is the number of classes.\n", + "The [MulticlassOneVsRestStrategy](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MulticlassOneVsOneStrategy.html) classifies one class against the rest of the classes. This is done for each and every class by training a separate classifier for it.So we will have total $k$ classifiers where $k$ is the number of classes.\n", "\n", "Just to see this in action lets create some data using the gaussian mixture model class ([GMM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGMM.html)) from which we sample the data points.Four different classes are created and plotted." ] @@ -595,7 +595,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The [KernelMulticlassMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelMulticlassMachine.html) is used with [LibSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVM.html) as the classifer just as in the above example.\n", + "The [KernelMulticlassMachine](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelMulticlassMachine.html) is used with [LibSVM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVM.html) as the classifer just as in the above example.\n", "\n", "Now we have four different classes, so as explained above we will have four classifiers which in shogun terms are submachines.\n", "\n", diff --git a/doc/ipython-notebooks/multiclass/naive_bayes.ipynb b/doc/ipython-notebooks/multiclass/naive_bayes.ipynb index 6f6c0feb30b..98fbd981610 100644 --- a/doc/ipython-notebooks/multiclass/naive_bayes.ipynb +++ b/doc/ipython-notebooks/multiclass/naive_bayes.ipynb @@ -45,7 +45,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In `SHOGUN`, [CGaussianNaiveBayes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianNaiveBayes.html) implements the Naive Bayes\n", + "In `SHOGUN`, [GaussianNaiveBayes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianNaiveBayes.html) implements the Naive Bayes\n", "algorithm. It is prefixed with \"Gaussian\" because the probability model for\n", "$P(X=x|Y=k)$ for each $k$ is taken to be a multi-variate Gaussian distribution.\n", "Furthermore, each dimension of the feature vector $X$ is assumed to be\n", @@ -56,7 +56,7 @@ "this demo, we show a simple 2D example. There are 3 linearly\n", "separable classes. The scattered points are training samples with colors\n", "indicating their labels. The filled area indicate the hypothesis learned by\n", - "the [CGaussianNaiveBayes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CGaussianNaiveBayes.html). The training samples are actually\n", + "the [GaussianNaiveBayes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1GaussianNaiveBayes.html). The training samples are actually\n", "generated from three Gaussian distributions. But since the covariance for those\n", "Gaussian distributions are not diagonal (i.e. there are *rotations*), the GNB\n", "algorithm cannot handle them properly.\n", @@ -254,7 +254,7 @@ "closed form solution for the estimated parameters.\n", "\n", "There is no independent implementation of logistic regression in `SHOGUN`, \n", - "but the `CLibLinear` becomes a logistic regression model when\n", + "but the `LibLinear` becomes a logistic regression model when\n", "constructed with the argument `L2R_LR`. This model also include a\n", "regularization term of the $\\ell_2$-norm of $\\boldsymbol\\beta$. If sparsity in\n", "$\\boldsymbol\\beta$ is needed, one can also use `L1R_LR`, which replaces\n", diff --git a/doc/ipython-notebooks/neuralnets/autoencoders.ipynb b/doc/ipython-notebooks/neuralnets/autoencoders.ipynb index 0f4b15ae068..2dd64ee7bb7 100644 --- a/doc/ipython-notebooks/neuralnets/autoencoders.ipynb +++ b/doc/ipython-notebooks/neuralnets/autoencoders.ipynb @@ -87,9 +87,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Similar to regular neural networks in Shogun, we create a [deep autoencoder](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html) using an array of [NeuralLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLayer.html)-based classes, which can be created using the utility class [NeuralLayers](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLayers.html). However, for deep autoencoders there's a restriction that the layer sizes in the network have to be symmetric, that is, the first layer has to have the same size as the last layer, the second layer has to have the same size as the second-to-last layer, and so on. This restriction is necessary for pre-training to work. More details on that can found in the following section.\n", + "Similar to regular neural networks in Shogun, we create a [deep autoencoder](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html) using an array of [NeuralLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLayer.html)-based classes, which can be created using the utility class [NeuralLayers](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLayers.html). However, for deep autoencoders there's a restriction that the layer sizes in the network have to be symmetric, that is, the first layer has to have the same size as the last layer, the second layer has to have the same size as the second-to-last layer, and so on. This restriction is necessary for pre-training to work. More details on that can found in the following section.\n", "\n", - "We'll create a 5-layer deep autoencoder with following layer sizes: 256->512->128->512->256. We'll use [rectified linear neurons](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralRectifiedLinearLayer.html) for the hidden layers and [linear neurons](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLinearLayer.html) for the output layer." + "We'll create a 5-layer deep autoencoder with following layer sizes: 256->512->128->512->256. We'll use [rectified linear neurons](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralRectifiedLinearLayer.html) for the hidden layers and [linear neurons](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLinearLayer.html) for the output layer." ] }, { @@ -121,9 +121,9 @@ "\n", "In pre-training, an autoencoder will formed for each encoding layer (layers up to the middle layer in the network). So here we'll have two autoencoders: L1->L2->L5, and L2->L3->L4. The first autoencoder will be trained on the raw data and used to initialize the weights and biases of layers L2 and L5 in the deep autoencoder. After the first autoencoder is trained, we use it to transform the raw data into the states of L2. These states will then be used to train the second autoencoder, which will be used to initialize the weights and biases of layers L3 and L4 in the deep autoencoder.\n", "\n", - "The operations described above are performed by the the [pre_train()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#acf6896cb166afbba063fd1257cb8bc97) function. Pre-training parameters for each autoencoder can be controlled using the [pt_* public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#a6389a6f19b8854c64e1b6be5aa0c1fc4) of [DeepAutoencoder](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html). Each of those attributes is an [SGVector](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html) whose length is the number of autoencoders in the deep autoencoder (2 in our case). It can be used to set the parameters for each autoencoder indiviually. [SGVector's set_const()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html#a8bce01a1fc41a734d9b5cf1533fd7a2a) method can also be used to assign the same parameter value for all autoencoders.\n", + "The operations described above are performed by the the [pre_train()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#acf6896cb166afbba063fd1257cb8bc97) function. Pre-training parameters for each autoencoder can be controlled using the [pt_* public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#a6389a6f19b8854c64e1b6be5aa0c1fc4) of [DeepAutoencoder](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html). Each of those attributes is an [SGVector](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html) whose length is the number of autoencoders in the deep autoencoder (2 in our case). It can be used to set the parameters for each autoencoder indiviually. [SGVector's set_const()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html#a8bce01a1fc41a734d9b5cf1533fd7a2a) method can also be used to assign the same parameter value for all autoencoders.\n", "\n", - "Different noise types can be used to corrupt the inputs in a denoising autoencoder. Shogun currently supports 2 [noise types](http://www.shogun-toolbox.org/doc/en/latest/namespaceshogun.html#af95cf5d3778127a87c8a67516405d863): dropout noise, where a random portion of the inputs is set to zero at each iteration in training, and gaussian noise, where the inputs are corrupted with random gaussian noise. The noise type and strength can be controlled using [pt_noise_type](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#af6e5d2ade5cb270cc50565d590f929ae) and [pt_noise_parameter](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#adbdff6c07fa7dd70aaf547e192365075). Here, we'll use dropout noise." + "Different noise types can be used to corrupt the inputs in a denoising autoencoder. Shogun currently supports 2 [noise types](http://www.shogun-toolbox.org/doc/en/latest/namespaceshogun.html#af95cf5d3778127a87c8a67516405d863): dropout noise, where a random portion of the inputs is set to zero at each iteration in training, and gaussian noise, where the inputs are corrupted with random gaussian noise. The noise type and strength can be controlled using [pt_noise_type](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#af6e5d2ade5cb270cc50565d590f929ae) and [pt_noise_parameter](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#adbdff6c07fa7dd70aaf547e192365075). Here, we'll use dropout noise." ] }, { @@ -162,7 +162,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "After pre-training, we can train the autoencoder as a whole to fine-tune the parameters. Training the whole autoencoder is performed using the [train()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CAutoencoder.html#ace3eb6cc545affcbfa31d754ffd087dc) function. Training parameters are controlled through the [public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#pub-attribs), same as a regular neural network." + "After pre-training, we can train the autoencoder as a whole to fine-tune the parameters. Training the whole autoencoder is performed using the [train()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Autoencoder.html#ace3eb6cc545affcbfa31d754ffd087dc) function. Training parameters are controlled through the [public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#pub-attribs), same as a regular neural network." ] }, { @@ -195,7 +195,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now we can evaluate the autoencoder that we trained. We'll start by providing it with corrupted inputs and looking at how it will reconstruct them. The function [reconstruct()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#ae8c2d565cf2ea809103d0557c57689c7) is used to obtain the reconstructions:" + "Now we can evaluate the autoencoder that we trained. We'll start by providing it with corrupted inputs and looking at how it will reconstruct them. The function [reconstruct()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#ae8c2d565cf2ea809103d0557c57689c7) is used to obtain the reconstructions:" ] }, { @@ -265,7 +265,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now, we can use the autoencoder to initialize a supervised neural network. The network will have all the layer of the autoencoder up to (and including) the middle layer. We'll also add a softmax output layer. So, the network will look like: L1->L2->L3->Softmax. The network is obtained by calling [convert_to_neural_network()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepAutoencoder.html#a8c179cd9a503b2fa78b9bfe10ae473e5):" + "Now, we can use the autoencoder to initialize a supervised neural network. The network will have all the layer of the autoencoder up to (and including) the middle layer. We'll also add a softmax output layer. So, the network will look like: L1->L2->L3->Softmax. The network is obtained by calling [convert_to_neural_network()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepAutoencoder.html#a8c179cd9a503b2fa78b9bfe10ae473e5):" ] }, { @@ -318,7 +318,7 @@ "source": [ "Convolutional autoencoders [3] are the adaptation of autoencoders to images (or other spacially-structured data). They are built with convolutional layers where each layer consists of a number of feature maps. Each feature map is produced by convolving a small filter with the layer's inputs, adding a bias, and then applying some non-linear activation function. Additionally, a max-pooling operation can be performed on each feature map by dividing it into small non-overlapping regions and taking the maximum over each region. In this section we'll pre-train a [convolutional network](http://deeplearning.net/tutorial/lenet.html) as a stacked autoencoder and use it for classification.\n", "\n", - "In Shogun, convolutional autoencoders are constructed and trained just like regular autoencoders. Except that we build the autoencoder using [CNeuralConvolutionalLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralConvolutionalLayer.html) objects:" + "In Shogun, convolutional autoencoders are constructed and trained just like regular autoencoders. Except that we build the autoencoder using [NeuralConvolutionalLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralConvolutionalLayer.html) objects:" ] }, { diff --git a/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb b/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb index 9036f439194..5dc52dd583e 100644 --- a/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb +++ b/doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb @@ -87,8 +87,8 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To create a neural network in shogun, we'll first create an instance of [NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html) and then [initialize](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html#a8ff6d177c3e2d8977e5fc6920d3e1579) it by telling it how many inputs it has and what type of layers it contains. To specifiy the layers of the network a [DynamicObjectArray](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDynamicObjectArray.html) is used. The array contains instances of [NeuralLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLayer.html)-based classes that determine the type of neurons each layer consists of. Some of the supported layer types are: [NeuralLinearLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLinearLayer.html), [NeuralLogisticLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLogisticLayer.html) and\n", - "[NeuralSoftmaxLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralSoftmaxLayer.html).\n", + "To create a neural network in shogun, we'll first create an instance of [NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html) and then [initialize](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html#a8ff6d177c3e2d8977e5fc6920d3e1579) it by telling it how many inputs it has and what type of layers it contains. To specifiy the layers of the network a [DynamicObjectArray](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DynamicObjectArray.html) is used. The array contains instances of [NeuralLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLayer.html)-based classes that determine the type of neurons each layer consists of. Some of the supported layer types are: [NeuralLinearLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLinearLayer.html), [NeuralLogisticLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLogisticLayer.html) and\n", + "[NeuralSoftmaxLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralSoftmaxLayer.html).\n", "\n", "We'll create a feed-forward, fully connected (every neuron is connected to all neurons in the layer below) neural network with 2 logistic hidden layers and a softmax output layer. The network will have 256 inputs, one for each pixel (16*16 image). The first hidden layer will have 256 neurons, the second will have 128 neurons, and the output layer will have 10 neurons, one for each digit class. Note that we're using a big network, compared with the size of the training set. This is to emphasize the effects of different regularization methods. We'll try training the network with:\n", "\n", @@ -186,18 +186,18 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "[NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html) supports two methods for training: LBFGS (default) and mini-batch gradient descent.\n", + "[NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html) supports two methods for training: LBFGS (default) and mini-batch gradient descent.\n", "\n", "[LBFGS](http://en.wikipedia.org/wiki/Limited-memory_BFGS) is a full-batch optimization methods, it looks at the entire training set each time before it changes the network's parameters. This makes it slow with large datasets. However, it works very well with small/medium size datasets and is very easy to use as it requires no parameter tuning.\n", "\n", "[Mini-batch Gradient Descent](http://en.wikipedia.org/wiki/Stochastic_gradient_descent) looks at only a small portion of the training set (a mini-batch) before each step, which it makes it suitable for large datasets. However, it's a bit harder to use than LBFGS because it requires some tuning for its parameters (learning rate, learning rate decay,..)\n", "\n", - "Training in [NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html) stops when:\n", + "Training in [NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html) stops when:\n", "\n", - "* Number of epochs (iterations over the entire training set) exceeds [max_num_epochs](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html#a7a2132cd0710750d28eaa4cd51d702af)\n", - "* The (percentage) difference in error between the current and previous iterations is smaller than [epsilon](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html#a0bde8d297e19e73b20b99110ba38f7bd), i.e the error is not anymore being reduced by training\n", + "* Number of epochs (iterations over the entire training set) exceeds [max_num_epochs](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html#a7a2132cd0710750d28eaa4cd51d702af)\n", + "* The (percentage) difference in error between the current and previous iterations is smaller than [epsilon](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html#a0bde8d297e19e73b20b99110ba38f7bd), i.e the error is not anymore being reduced by training\n", "\n", - "To see all the options supported for training, check the [documentation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html#pub-attribs)\n", + "To see all the options supported for training, check the [documentation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html#pub-attribs)\n", "\n", "We'll first write a small function to calculate the classification accuracy on the validation set, so that we can compare different models:\n" ] @@ -363,7 +363,7 @@ "\n", "With that in mind, each layer in a convolutional network consists of a number of feature maps. Each feature map is produced by convolving a small filter with the layer's inputs, adding a bias, and then applying some non-linear activation function. The convolution operation satisfies the local connectivity and the weight sharing constraints. Additionally, a max-pooling operation can be performed on each feature map by dividing it into small non-overlapping regions and taking the maximum over each region. This adds some translation invarience and improves the performance.\n", "\n", - "Convolutional nets in Shogun are handled through the [CNeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classes.html) class along with the [CNeuralConvolutionalLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralConvolutionalLayer.html) class. A [CNeuralConvolutionalLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralConvolutionalLayer.html) represents a convolutional layer with multiple feature maps, optional max-pooling, and support for [different types of activation functions](http://www.shogun-toolbox.org/doc/en/latest/namespaceshogun.html#a2b9827281875ee8de764ea86e7735482)\n", + "Convolutional nets in Shogun are handled through the [NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classes.html) class along with the [NeuralConvolutionalLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralConvolutionalLayer.html) class. A [NeuralConvolutionalLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralConvolutionalLayer.html) represents a convolutional layer with multiple feature maps, optional max-pooling, and support for [different types of activation functions](http://www.shogun-toolbox.org/doc/en/latest/namespaceshogun.html#a2b9827281875ee8de764ea86e7735482)\n", "\n", "Now we'll creates a convolutional neural network with two convolutional layers and a softmax output layer. We'll use the [rectified linear](http://en.wikipedia.org/wiki/Rectifier_(neural_networks)) activation function for the convolutional layers:" ] diff --git a/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb b/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb index 1139c414d6b..02114f27973 100644 --- a/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb +++ b/doc/ipython-notebooks/neuralnets/rbms_dbns.ipynb @@ -150,7 +150,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "RBMs in Shogun are handled through the [CRBM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CRBM.html) class. We create one by specifying the number of visible units and their type (binary, Gaussian, and Softmax visible units are supported), and the number of hidden units (only binary hidden units are supported).\n", + "RBMs in Shogun are handled through the [RBM](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1RBM.html) class. We create one by specifying the number of visible units and their type (binary, Gaussian, and Softmax visible units are supported), and the number of hidden units (only binary hidden units are supported).\n", "\n", "In this notebook we'll train a few RBMs on the USPS dataset for handwritten digits. We'll have one RBM for each digit class, making 10 RBMs in total:" ] @@ -199,7 +199,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now we'll move on to training the RBMs using Persistent Contrastive Divergence [2]. Training using regular Contrastive Divergence [3] is also [supported](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CRBM.html#abf0cdced1524987a687535a2a8de5cb3).The optimization is performed using [Gradient Descent](http://en.wikipedia.org/wiki/Gradient_descent). The training progress can be monitored using the reconstruction error or the [psuedo-likelihood](http://en.wikipedia.org/wiki/Pseudolikelihood). Check the [public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CRBM.html#pub-attribs) of the CRBM class for all the available training options. " + "Now we'll move on to training the RBMs using Persistent Contrastive Divergence [2]. Training using regular Contrastive Divergence [3] is also [supported](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1RBM.html#abf0cdced1524987a687535a2a8de5cb3).The optimization is performed using [Gradient Descent](http://en.wikipedia.org/wiki/Gradient_descent). The training progress can be monitored using the reconstruction error or the [psuedo-likelihood](http://en.wikipedia.org/wiki/Pseudolikelihood). Check the [public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1RBM.html#pub-attribs) of the RBM class for all the available training options. " ] }, { @@ -278,7 +278,7 @@ "source": [ "Now we'll create a DBN, pre-train it on the digits dataset, and use it initialize a neural network which we can use for classification.\n", "\n", - "DBNs are handled in Shogun through the [CDeepBeliefNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepBeliefNetwork.html) class. We create a network by specifying the number of visible units it has, and then add the desired number of hidden layers using [add_hidden_layer()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepBeliefNetwork.html#ae765aad072aef83a41906140c9f9a8c5). When done, we call [initialize_neural_network()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepBeliefNetwork.html#aae4e01a7cfd234a1895b40be197ef83a) to initialize the network:" + "DBNs are handled in Shogun through the [DeepBeliefNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepBeliefNetwork.html) class. We create a network by specifying the number of visible units it has, and then add the desired number of hidden layers using [add_hidden_layer()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepBeliefNetwork.html#ae765aad072aef83a41906140c9f9a8c5). When done, we call [initialize_neural_network()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepBeliefNetwork.html#aae4e01a7cfd234a1895b40be197ef83a) to initialize the network:" ] }, { @@ -300,7 +300,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Then we'll pre-train the DBN on the USPS dataset. Since we have 3 layers, the DBN will be pre-trained as two RBMs: one that consists of the first hidden layer and the visible layer, the other consists of the first hidden layer and the second hidden layer. Pre-training parameters can be specified using the [pt_* public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepBeliefNetwork.html#a0753163b619371f111d55ade639752c4) of the class. Each of those attributes is an [SGVector](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html) whose length is the number of RBMs (2 in our case). It can be used to set the parameters for each RBM indiviually. [SGVector's set_const()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html#a8bce01a1fc41a734d9b5cf1533fd7a2a) method can also be used to assign the same parameter value for all RBMs." + "Then we'll pre-train the DBN on the USPS dataset. Since we have 3 layers, the DBN will be pre-trained as two RBMs: one that consists of the first hidden layer and the visible layer, the other consists of the first hidden layer and the second hidden layer. Pre-training parameters can be specified using the [pt_* public attributes](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepBeliefNetwork.html#a0753163b619371f111d55ade639752c4) of the class. Each of those attributes is an [SGVector](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html) whose length is the number of RBMs (2 in our case). It can be used to set the parameters for each RBM indiviually. [SGVector's set_const()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGVector.html#a8bce01a1fc41a734d9b5cf1533fd7a2a) method can also be used to assign the same parameter value for all RBMs." ] }, { @@ -356,7 +356,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now, we'll use the DBN to initialize a [CNeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralNetwork.html). This is done through the [convert_to_neural_network()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CDeepBeliefNetwork.html#a8c179cd9a503b2fa78b9bfe10ae473e5) method. The neural network will consist of a [CNeuralInputLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralInputLayer.html) with 256 neurons, a [CNeuralLogisticLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLogisticLayer.html) with 200 neurons, and another [CNeuralLogisticLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralLogisticLayer.html) with 300 neurons. We'll also add a [CNeuralSoftmaxLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CNeuralSoftmaxLayer.html) as an output layer so that we can train the network in a supervised manner. We'll also train the network on the training set:" + "Now, we'll use the DBN to initialize a [NeuralNetwork](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralNetwork.html). This is done through the [convert_to_neural_network()](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1DeepBeliefNetwork.html#a8c179cd9a503b2fa78b9bfe10ae473e5) method. The neural network will consist of a [NeuralInputLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralInputLayer.html) with 256 neurons, a [NeuralLogisticLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLogisticLayer.html) with 200 neurons, and another [NeuralLogisticLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralLogisticLayer.html) with 300 neurons. We'll also add a [NeuralSoftmaxLayer](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1NeuralSoftmaxLayer.html) as an output layer so that we can train the network in a supervised manner. We'll also train the network on the training set:" ] }, { diff --git a/doc/ipython-notebooks/pca/pca_notebook.ipynb b/doc/ipython-notebooks/pca/pca_notebook.ipynb index a491cd1d02b..27c710365d7 100644 --- a/doc/ipython-notebooks/pca/pca_notebook.ipynb +++ b/doc/ipython-notebooks/pca/pca_notebook.ipynb @@ -196,7 +196,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "CPCA class of Shogun inherits from the [CPreprocessor](http://www.shogun-toolbox.org/doc/en/3.0.0/classshogun_1_1CPreprocessor.html) class. Preprocessors are transformation functions that doesn't change the domain of the input features. Specifically, CPCA performs principal component analysis on the input vectors and keeps only the specified number of eigenvectors. On preprocessing, the stored covariance matrix is used to project vectors into eigenspace.\n", + "CPCA class of Shogun inherits from the [Preprocessor](http://www.shogun-toolbox.org/doc/en/3.0.0/classshogun_1_1Preprocessor.html) class. Preprocessors are transformation functions that doesn't change the domain of the input features. Specifically, CPCA performs principal component analysis on the input vectors and keeps only the specified number of eigenvectors. On preprocessing, the stored covariance matrix is used to project vectors into eigenspace.\n", "\n", "Performance of PCA depends on the algorithm used according to the situation in hand.\n", "Our PCA preprocessor class provides 3 method options to compute the transformation matrix:\n", @@ -1210,7 +1210,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun uses [CEuclideanDistance](http://www.shogun-toolbox.org/doc/en/3.0.0/classshogun_1_1CEuclideanDistance.html) class to compute the familiar Euclidean distance for real valued features. It computes the square root of the sum of squared disparity between the corresponding feature dimensions of two data points.\n", + "Shogun uses [EuclideanDistance](http://www.shogun-toolbox.org/doc/en/3.0.0/classshogun_1_1EuclideanDistance.html) class to compute the familiar Euclidean distance for real valued features. It computes the square root of the sum of squared disparity between the corresponding feature dimensions of two data points.\n", "\n", "$\\mathbf{d(x,x')=}$$\\sqrt{\\mathbf{\\sum\\limits_{i=0}^{n}}|\\mathbf{x_i}-\\mathbf{x'_i}|^2}$" ] diff --git a/doc/ipython-notebooks/regression/Regression.ipynb b/doc/ipython-notebooks/regression/Regression.ipynb index cc5834209c6..dd26a1903c9 100644 --- a/doc/ipython-notebooks/regression/Regression.ipynb +++ b/doc/ipython-notebooks/regression/Regression.ipynb @@ -90,7 +90,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Regression using Least squares is demonstrated below on toy data. Shogun provides the tool to do it using [CLeastSquaresRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CLeastSquaresRegression.html) class. The data is a straight line with lot of noise and having slope 3. Comparing with the mathematical equation above we thus expect $\\text w$ to be around 3 for a good prediction. Once the data is converted to Shogun format, we are ready to train the machine. To label the training data [CRegressionLabels](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CRegressionLabels.html) are used." + "Regression using Least squares is demonstrated below on toy data. Shogun provides the tool to do it using [LeastSquaresRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1LeastSquaresRegression.html) class. The data is a straight line with lot of noise and having slope 3. Comparing with the mathematical equation above we thus expect $\\text w$ to be around 3 for a good prediction. Once the data is converted to Shogun format, we are ready to train the machine. To label the training data [RegressionLabels](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1RegressionLabels.html) are used." ] }, { @@ -131,7 +131,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "[LeastSquaresRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CLeastSquaresRegression.html) has to be initialised with the training features and training labels. Once that is done to learn from data we `train()` it. This also generates the $\\text w$ from the general equation described above. To access $\\text w$ use `get_real_vector('w')`." + "[LeastSquaresRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1LeastSquaresRegression.html) has to be initialised with the training features and training labels. Once that is done to learn from data we `train()` it. This also generates the $\\text w$ from the general equation described above. To access $\\text w$ use `get_real_vector('w')`." ] }, { @@ -334,13 +334,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The mean squared error (MSE) of an estimator measures the average of the squares of the errors. [CMeanSquaredError](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMeanSquaredError.html) class is used to compute the MSE as :\n", + "The mean squared error (MSE) of an estimator measures the average of the squares of the errors. [MeanSquaredError](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1MeanSquaredError.html) class is used to compute the MSE as :\n", "\n", "$$\\frac{1}{|L|} \\sum_{i=1}^{|L|} (L_i - R_i)^2$$\n", "\n", "Here $L$ is the vector of predicted labels and $R$ is the vector of real labels.\n", "\n", - "We use 5-fold [cross-validation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CCrossValidation.html) to compute MSE and have a look at how MSE varies with regularisation." + "We use 5-fold [cross-validation](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CrossValidation.html) to compute MSE and have a look at how MSE varies with regularisation." ] }, { @@ -464,7 +464,7 @@ "\n", "$$\\min \\|X^T\\beta - y\\|^2 \\quad s.t. \\|\\beta\\|_1 \\leq C $$\n", "\n", - "One way to solve this regularized form is by using [Least Angle Regression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CLeastAngleRegression.html) (LARS)." + "One way to solve this regularized form is by using [Least Angle Regression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1LeastAngleRegression.html) (LARS)." ] }, { @@ -486,7 +486,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun provides tools for Least angle regression (LARS) and lasso using [CLeastAngleRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CLeastAngleRegression.html) class. As explained in the mathematical formaulation, LARS is just like [Stepwise Regression](http://en.wikipedia.org/wiki/Stepwise_regression) but increases the estimated variables in a direction equiangular to each one's correlations with the residual. The working of this is shown below by plotting the LASSO path. Data is generated in a similar way to the previous section." + "Shogun provides tools for Least angle regression (LARS) and lasso using [LeastAngleRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1LeastAngleRegression.html) class. As explained in the mathematical formaulation, LARS is just like [Stepwise Regression](http://en.wikipedia.org/wiki/Stepwise_regression) but increases the estimated variables in a direction equiangular to each one's correlations with the residual. The working of this is shown below by plotting the LASSO path. Data is generated in a similar way to the previous section." ] }, { @@ -510,7 +510,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "[CLeastAngleRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CLeastAngleRegression.html) requires the features to be normalized with a zero mean and unit norm. Hence we use two preprocessors: [PruneVarSubMean](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html) and [NormOne](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html)." + "[LeastAngleRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1LeastAngleRegression.html) requires the features to be normalized with a zero mean and unit norm. Hence we use two preprocessors: [PruneVarSubMean](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html) and [NormOne](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CPruneVarSubMean.html)." ] }, { @@ -633,7 +633,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "KRR can be performed in Shogun using [CKernelRidgeRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CKernelRidgeRegression.html) class. Let us apply it on a non linear regression problem from the [Boston Housing Dataset](https://archive.ics.uci.edu/ml/datasets/Housing), where the task is to predict prices of houses by finding a relationship with the various attributes provided. The per capita crime rate attribute is used in this particular example. " + "KRR can be performed in Shogun using [KernelRidgeRegression](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1KernelRidgeRegression.html) class. Let us apply it on a non linear regression problem from the [Boston Housing Dataset](https://archive.ics.uci.edu/ml/datasets/Housing), where the task is to predict prices of houses by finding a relationship with the various attributes provided. The per capita crime rate attribute is used in this particular example. " ] }, { @@ -739,7 +739,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let us try regression using Shogun's [LibSVR](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CLibSVR.html). The dataset from last section is used. The `svr_param` argument is the $\\epsilon$-tube for the $\\epsilon$ version and is the $\\nu$ parameter in other case." + "Let us try regression using Shogun's [LibSVR](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1LibSVR.html). The dataset from last section is used. The `svr_param` argument is the $\\epsilon$-tube for the $\\epsilon$ version and is the $\\nu$ parameter in other case." ] }, { diff --git a/doc/ipython-notebooks/statistical_testing/mmd_two_sample_testing.ipynb b/doc/ipython-notebooks/statistical_testing/mmd_two_sample_testing.ipynb index 67c16c630fc..315a38e7354 100644 --- a/doc/ipython-notebooks/statistical_testing/mmd_two_sample_testing.ipynb +++ b/doc/ipython-notebooks/statistical_testing/mmd_two_sample_testing.ipynb @@ -84,7 +84,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Shogun implements statistical testing in the abstract class CHypothesisTest. All implemented methods will work with this interface at their most basic level. We here focos on CTwoSampleTest. This class offers methods to\n", + "Shogun implements statistical testing in the abstract class HypothesisTest. All implemented methods will work with this interface at their most basic level. We here focos on TwoSampleTest. This class offers methods to\n", "\n", " * compute the implemented test statistic,\n", " * compute p-values for a given value of the test statistic,\n", @@ -122,7 +122,7 @@ " +\\textbf{E}_{y,y'}\\left[ k(y,y')\\right]\n", "\\end{align*}\n", "\n", - "Note that this formulation does not assume any form of the input data, we just need a kernel function whose feature space is a RKHS, see [2, Section 2] for details. This has the consequence that in Shogun, we can do tests on any type of data (CDenseFeatures, CSparseFeatures, CStringFeatures, etc), as long as we or you provide a positive definite kernel function under the interface of CKernel.\n", + "Note that this formulation does not assume any form of the input data, we just need a kernel function whose feature space is a RKHS, see [2, Section 2] for details. This has the consequence that in Shogun, we can do tests on any type of data (DenseFeatures, SparseFeatures, CStringFeatures, etc), as long as we or you provide a positive definite kernel function under the interface of Kernel.\n", "\n", "We here only describe how to use the MMD for two-sample testing. Shogun offers two types of test statistic based on the MMD, one with quadratic costs both in time and space, and one with linear time and constant space costs. Both come in different versions and with different methods how to approximate the null-distribution in order to construct a two-sample test." ] @@ -229,7 +229,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "We now describe the quadratic time MMD, as described in [1, Lemma 6], which is implemented in Shogun. All methods in this section are implemented in CQuadraticTimeMMD, which accepts any type of features in Shogun, and use it on the above toy problem.\n", + "We now describe the quadratic time MMD, as described in [1, Lemma 6], which is implemented in Shogun. All methods in this section are implemented in QuadraticTimeMMD, which accepts any type of features in Shogun, and use it on the above toy problem.\n", "\n", "An unbiased estimate for the MMD expression above can be obtained by estimating expected values with averaging over independent samples\n", "\n", @@ -243,7 +243,7 @@ "\\mmd_b[\\mathcal{F},X,Y]^2=\\frac{1}{m^2}\\sum_{i=1}^m\\sum_{j=1}^mk(x_i,x_j) + \\frac{1}{n^ 2}\\sum_{i=1}^n\\sum_{j=1}^nk(y_i,y_j)-\\frac{2}{mn}\\sum_{i=1}^m\\sum_{j\\neq i}^nk(x_i,y_j)\n", ".$$\n", "\n", - "Computing the test statistic using CQuadraticTimeMMD does exactly this, where it is possible to choose between the two above expressions. Note that some methods for approximating the null-distribution only work with one of both types. Both statistics' computational costs are quadratic both in time and space. Note that the method returns $m\\mmd_b[\\mathcal{F},X,Y]^2$ since null distribution approximations work on $m$ times null distribution. Here is how the test statistic itself is computed." + "Computing the test statistic using QuadraticTimeMMD does exactly this, where it is possible to choose between the two above expressions. Note that some methods for approximating the null-distribution only work with one of both types. Both statistics' computational costs are quadratic both in time and space. Note that the method returns $m\\mmd_b[\\mathcal{F},X,Y]^2$ since null distribution approximations work on $m$ times null distribution. Here is how the test statistic itself is computed." ] }, { @@ -281,7 +281,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Any sub-class of CHypothesisTest can compute approximate the null distribution using permutation/bootstrapping. This way always is guaranteed to produce consistent results, however, it might take a long time as for each sample of the null distribution, the test statistic has to be computed for a different permutation of the data. Shogun's implementation is highly optimized, exploiting low-level CPU caching and multiple available cores." + "Any sub-class of HypothesisTest can compute approximate the null distribution using permutation/bootstrapping. This way always is guaranteed to produce consistent results, however, it might take a long time as for each sample of the null distribution, the test statistic has to be computed for a different permutation of the data. Shogun's implementation is highly optimized, exploiting low-level CPU caching and multiple available cores." ] }, { @@ -322,7 +322,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now let us visualise distribution of MMD statistic under $H_0:p=q$ and $H_A:p\\neq q$. Sample both null and alternative distribution for that. Use the interface of CHypothesisTest to sample from the null distribution (permutations, re-computing of test statistic is done internally). For the alternative distribution, compute the test statistic for a new sample set of $X$ and $Y$ in a loop. Note that the latter is expensive, as the kernel cannot be precomputed, and infinite data is needed. Though it is not needed in practice but only for illustrational purposes here." + "Now let us visualise distribution of MMD statistic under $H_0:p=q$ and $H_A:p\\neq q$. Sample both null and alternative distribution for that. Use the interface of HypothesisTest to sample from the null distribution (permutations, re-computing of test statistic is done internally). For the alternative distribution, compute the test statistic for a new sample set of $X$ and $Y$ in a loop. Note that the latter is expensive, as the kernel cannot be precomputed, and infinite data is needed. Though it is not needed in practice but only for illustrational purposes here." ] }, { @@ -432,7 +432,7 @@ "\n", "If the kernel matrices are diagonal dominant, this method is likely to fail. For that and more details, see the original paper. Computational costs are likely to be larger than permutation testing, due to the efficient implementation of the latter: Eigenvalues of the gram matrix cost $\\mathcal{O}(m^3)$.\n", "\n", - "Below, we illustrate how to sample the null distribution and perform two-sample testing with the Spectrum approximation in the class CQuadraticTimeMMD. This method only works with the biased statistic." + "Below, we illustrate how to sample the null distribution and perform two-sample testing with the Spectrum approximation in the class QuadraticTimeMMD. This method only works with the biased statistic." ] }, { @@ -647,9 +647,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "So far, we basically had to precompute the kernel matrix for reasonable runtimes. This is not possible for more than a few thousand points. The linear time MMD statistic, implemented in CLinearTimeMMD can help here, as it accepts data under the streaming interface CStreamingFeatures, which deliver data one-by-one.\n", + "So far, we basically had to precompute the kernel matrix for reasonable runtimes. This is not possible for more than a few thousand points. The linear time MMD statistic, implemented in LinearTimeMMD can help here, as it accepts data under the streaming interface StreamingFeatures, which deliver data one-by-one.\n", "\n", - "And it can do more cool things, for example choose the best single (or combined) kernel for you. But we need a more fancy dataset for that to show its power. We will use one of Shogun's streaming based data generator, CGaussianBlobsDataGenerator for that. This dataset consists of two distributions which are a grid of Gaussians where in one of them, the Gaussians are stretched and rotated. This dataset is regarded as challenging for two-sample testing." + "And it can do more cool things, for example choose the best single (or combined) kernel for you. But we need a more fancy dataset for that to show its power. We will use one of Shogun's streaming based data generator, GaussianBlobsDataGenerator for that. This dataset consists of two distributions which are a grid of Gaussians where in one of them, the Gaussians are stretched and rotated. This dataset is regarded as challenging for two-sample testing." ] }, { @@ -699,7 +699,7 @@ "\n", "where $ m_2=\\lfloor\\frac{m}{2} \\rfloor$. While the above expression assumes that $m$ data are available from each distribution, the statistic in general works in an online setting where features are obtained one by one. Since only pairs of four points are considered at once, this allows to compute it on data streams. In addition, the computational costs are linear in the number of samples that are considered from each distribution. These two properties make the linear time MMD very applicable for large scale two-sample tests. In theory, any number of samples can be processed -- time is the only limiting factor.\n", "\n", - "We begin by illustrating how to pass data to CLinearTimeMMD. In order not to loose performance due to overhead, it is possible to specify a block size for the data stream." + "We begin by illustrating how to pass data to LinearTimeMMD. In order not to loose performance due to overhead, it is possible to specify a block size for the data stream." ] }, { @@ -733,7 +733,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Sometimes, one might want to use CLinearTimeMMD with data that is stored in memory. In that case, it is easy to data in the form of for example CStreamingDenseFeatures into CDenseFeatures." + "Sometimes, one might want to use LinearTimeMMD with data that is stored in memory. In that case, it is easy to data in the form of for example StreamingDenseFeatures into DenseFeatures." ] }, { @@ -774,7 +774,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "As for any two-sample test in Shogun, bootstrapping can be used to approximate the null distribution. This results in a consistent, but slow test. The number of samples to take is the only parameter. Note that since CLinearTimeMMD operates on streaming features, *new* data is taken from the stream in every iteration.\n", + "As for any two-sample test in Shogun, bootstrapping can be used to approximate the null distribution. This results in a consistent, but slow test. The number of samples to take is the only parameter. Note that since LinearTimeMMD operates on streaming features, *new* data is taken from the stream in every iteration.\n", "\n", "Bootstrapping is not really necessary since there exists a fast and consistent estimate of the null-distribution. However, to ensure that any approximation is accurate, it should always be checked against bootstrapping at least once.\n", "\n", @@ -792,7 +792,7 @@ "\n", "A normal distribution with this variance and zero mean can then be used as an approximation for the null-distribution. This results in a consistent test and is very fast. However, note that it is an approximation and its accuracy depends on the underlying data distributions. It is a good idea to compare to the bootstrapping approach first to determine an appropriate number of samples to use. This number is usually in the tens of thousands.\n", "\n", - "CLinearTimeMMD allows to approximate the null distribution in the same pass as computing the statistic itself (in linear time). This should always be used in practice since seperate calls of computing statistic and p-value will operator on different data from the stream. Below, we compute the test on a large amount of data (impossible to perform quadratic time MMD for this one as the kernel matrices cannot be stored in memory)" + "LinearTimeMMD allows to approximate the null distribution in the same pass as computing the statistic itself (in linear time). This should always be used in practice since seperate calls of computing statistic and p-value will operator on different data from the stream. Below, we compute the test on a large amount of data (impossible to perform quadratic time MMD for this one as the kernel matrices cannot be stored in memory)" ] }, { @@ -829,11 +829,11 @@ "\\DeclareMathOperator*{\\argmax}{arg\\,max}$\n", "Now which kernel do we actually use for our tests? So far, we just plugged in arbritary ones. However, for kernel two-sample testing, it is possible to do something more clever.\n", "\n", - "Shogun's kernel selection methods for MMD based two-sample tests are all based around [3, 4]. For the CLinearTimeMMD, [3] describes a way of selecting the *optimal* kernel in the sense that the test's type II error is minimised. For the linear time MMD, this is the method of choice. It is done via maximising the MMD statistic divided by its standard deviation and it is possible for single kernels and also for convex combinations of them. For the CQuadraticTimeMMD, the best method in literature is choosing the kernel that maximised the MMD statistic [4]. For convex combinations of kernels, this can be achieved via a $L2$ norm constraint. A detailed comparison of all methods on numerous datasets can be found in [5].\n", + "Shogun's kernel selection methods for MMD based two-sample tests are all based around [3, 4]. For the LinearTimeMMD, [3] describes a way of selecting the *optimal* kernel in the sense that the test's type II error is minimised. For the linear time MMD, this is the method of choice. It is done via maximising the MMD statistic divided by its standard deviation and it is possible for single kernels and also for convex combinations of them. For the QuadraticTimeMMD, the best method in literature is choosing the kernel that maximised the MMD statistic [4]. For convex combinations of kernels, this can be achieved via a $L2$ norm constraint. A detailed comparison of all methods on numerous datasets can be found in [5].\n", "\n", - "MMD Kernel selection in Shogun always involves coosing a one of the methods of CGaussianKernel All methods compute their results for a fixed set of these baseline kernels. We later give an example how to use these classes after providing a list of available methods.\n", + "MMD Kernel selection in Shogun always involves coosing a one of the methods of GaussianKernel All methods compute their results for a fixed set of these baseline kernels. We later give an example how to use these classes after providing a list of available methods.\n", "\n", - " * *KSM_MEDIAN_HEURISTIC*: Selects from a set CGaussianKernel instances the one whose width parameter is closest to the median of the pairwise distances in the data. The median is computed on a certain number of points from each distribution that can be specified as a parameter. Since the median is a stable statistic, one does not have to compute all pairwise distances but rather just a few thousands. This method a useful (and fast) heuristic that in many cases gives a good hint on where to start looking for Gaussian kernel widths. It is for example described in [1]. Note that it may fail badly in selecting a good kernel for certain problems.\n", + " * *KSM_MEDIAN_HEURISTIC*: Selects from a set GaussianKernel instances the one whose width parameter is closest to the median of the pairwise distances in the data. The median is computed on a certain number of points from each distribution that can be specified as a parameter. Since the median is a stable statistic, one does not have to compute all pairwise distances but rather just a few thousands. This method a useful (and fast) heuristic that in many cases gives a good hint on where to start looking for Gaussian kernel widths. It is for example described in [1]. Note that it may fail badly in selecting a good kernel for certain problems.\n", "\n", " * *KSM_MAXIMIZE_MMD*: Selects from a set of arbitrary baseline kernels a single one that maximises the used MMD statistic -- more specific its estimate.\n", "$$\n", @@ -847,20 +847,20 @@ "k^*=\\argmax_{k\\in\\mathcal{K}} \\frac{\\hat \\eta_k}{\\hat\\sigma_k+\\lambda},\n", "$$\n", "where $\\eta_k$ is a linear time MMD estimate for using a kernel $k$ and $\\hat\\sigma_k$ is a linear time variance estimate of $\\eta_k$ to which a small number $\\lambda$ is added to prevent division by zero.\n", - "These are estimated in a linear time way with the streaming framework that was described earlier. Therefore, this method is only available for CLinearTimeMMD. Optimal here means that the resulting test's type II error is minimised for a fixed type I error. *Important:* For this method to work, the kernel needs to be selected on *different* data than the test is performed on. Otherwise, the method will produce wrong results.\n", + "These are estimated in a linear time way with the streaming framework that was described earlier. Therefore, this method is only available for LinearTimeMMD. Optimal here means that the resulting test's type II error is minimised for a fixed type I error. *Important:* For this method to work, the kernel needs to be selected on *different* data than the test is performed on. Otherwise, the method will produce wrong results.\n", " \n", - " * CMMDKernelSelectionCombMaxL2 Selects a convex combination of kernels that maximises the MMD statistic. This is the multiple kernel analogous to CMMDKernelSelectionMax. This is done via solving the convex program\n", + " * MMDKernelSelectionCombMaxL2 Selects a convex combination of kernels that maximises the MMD statistic. This is the multiple kernel analogous to MMDKernelSelectionMax. This is done via solving the convex program\n", "$$\n", "\\boldsymbol{\\beta}^*=\\min_{\\boldsymbol{\\beta}} \\{\\boldsymbol{\\beta}^T\\boldsymbol{\\beta} : \\boldsymbol{\\beta}^T\\boldsymbol{\\eta}=\\mathbf{1}, \\boldsymbol{\\beta}\\succeq 0\\},\n", "$$\n", "where $\\boldsymbol{\\beta}$ is a vector of the resulting kernel weights and $\\boldsymbol{\\eta}$ is a vector of which each component contains a MMD estimate for a baseline kernel. See [3] for details. Note that this method is unable to select a single kernel -- even when this would be optimal.\n", "Again, when using the linear time MMD, there are better methods available.\n", "\n", - " * CMMDKernelSelectionCombOpt Selects a convex combination of kernels that maximises the MMD statistic divided by its covariance. This corresponds to \\emph{optimal} kernel selection in the same sense as in class CMMDKernelSelectionOpt and is its multiple kernel analogous. The convex program to solve is\n", + " * MMDKernelSelectionCombOpt Selects a convex combination of kernels that maximises the MMD statistic divided by its covariance. This corresponds to \\emph{optimal} kernel selection in the same sense as in class MMDKernelSelectionOpt and is its multiple kernel analogous. The convex program to solve is\n", "$$\n", "\\boldsymbol{\\beta}^*=\\min_{\\boldsymbol{\\beta}} (\\hat Q+\\lambda I) \\{\\boldsymbol{\\beta}^T\\boldsymbol{\\beta} : \\boldsymbol{\\beta}^T\\boldsymbol{\\eta}=\\mathbf{1}, \\boldsymbol{\\beta}\\succeq 0\\},\n", "$$\n", - "where again $\\boldsymbol{\\beta}$ is a vector of the resulting kernel weights and $\\boldsymbol{\\eta}$ is a vector of which each component contains a MMD estimate for a baseline kernel. The matrix $\\hat Q$ is a linear time estimate of the covariance matrix of the vector $\\boldsymbol{\\eta}$ to whose diagonal a small number $\\lambda$ is added to prevent division by zero. See [3] for details. In contrast to CMMDKernelSelectionCombMaxL2, this method is able to select a single kernel when this gives a lower type II error than a combination. In this sense, it contains CMMDKernelSelectionOpt." + "where again $\\boldsymbol{\\beta}$ is a vector of the resulting kernel weights and $\\boldsymbol{\\eta}$ is a vector of which each component contains a MMD estimate for a baseline kernel. The matrix $\\hat Q$ is a linear time estimate of the covariance matrix of the vector $\\boldsymbol{\\eta}$ to whose diagonal a small number $\\lambda$ is added to prevent division by zero. See [3] for details. In contrast to MMDKernelSelectionCombMaxL2, this method is able to select a single kernel when this gives a lower type II error than a combination. In this sense, it contains MMDKernelSelectionOpt." ] }, { @@ -874,11 +874,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "In order to use one of the above methods for kernel selection, one has to create a new instance of CCombinedKernel append all desired baseline kernels to it. This combined kernel is then passed to the MMD class. Then, an object of any of the above kernel selection methods is created and the MMD instance is passed to it in the constructor. There are then multiple methods to call\n", + "In order to use one of the above methods for kernel selection, one has to create a new instance of CombinedKernel append all desired baseline kernels to it. This combined kernel is then passed to the MMD class. Then, an object of any of the above kernel selection methods is created and the MMD instance is passed to it in the constructor. There are then multiple methods to call\n", "\n", - " * *compute_measures* to compute a vector kernel selection criteria if a single kernel selection method is used. It will return a vector of selected kernel weights if a combined kernel selection method is used. For \\shogunclass{CMMDKernelSelectionMedian}, the method does throw an error.\n", + " * *compute_measures* to compute a vector kernel selection criteria if a single kernel selection method is used. It will return a vector of selected kernel weights if a combined kernel selection method is used. For \\shogunclass{MMDKernelSelectionMedian}, the method does throw an error.\n", "\n", - " * *select\\_kernel* returns the selected kernel of the method. For single kernels this will be one of the baseline kernel instances. For the combined kernel case, this will be the underlying CCombinedKernel instance where the subkernel weights are set to the weights that were selected by the method. \n", + " * *select\\_kernel* returns the selected kernel of the method. For single kernels this will be one of the baseline kernel instances. For the combined kernel case, this will be the underlying CombinedKernel instance where the subkernel weights are set to the weights that were selected by the method. \n", "\n", "In order to utilise the selected kernel, it has to be passed to an MMD instance. We now give an example how to select the optimal single and combined kernel for the Gaussian Blobs dataset." ] diff --git a/doc/ipython-notebooks/structure/FGM.ipynb b/doc/ipython-notebooks/structure/FGM.ipynb index 23053a48585..55999fd8385 100644 --- a/doc/ipython-notebooks/structure/FGM.ipynb +++ b/doc/ipython-notebooks/structure/FGM.ipynb @@ -82,11 +82,11 @@ "\n", "In Shogun's factor graph model, the corresponding implemented functions are:\n", "\n", - "- FactorGraphModel::get_joint_feature_vector() $\\longleftrightarrow \\Phi(\\mathbf{x}_i,\\mathbf{y})$ \n", + "- FactorGraphModel::get_joint_feature_vector() $\\longleftrightarrow \\Phi(\\mathbf{x}_i,\\mathbf{y})$ \n", "\n", - "- FactorGraphModel::argmax() $\\longleftrightarrow H_i(\\mathbf{w})$\n", + "- FactorGraphModel::argmax() $\\longleftrightarrow H_i(\\mathbf{w})$\n", "\n", - "- FactorGraphModel::delta_loss() $\\longleftrightarrow \\Delta(\\mathbf{y}_i,\\mathbf{y})$" + "- FactorGraphModel::delta_loss() $\\longleftrightarrow \\Delta(\\mathbf{y}_i,\\mathbf{y})$" ] }, { diff --git a/doc/ipython-notebooks/structure/multilabel_structured_prediction.ipynb b/doc/ipython-notebooks/structure/multilabel_structured_prediction.ipynb index babb3431d9a..32db218d502 100644 --- a/doc/ipython-notebooks/structure/multilabel_structured_prediction.ipynb +++ b/doc/ipython-notebooks/structure/multilabel_structured_prediction.ipynb @@ -141,7 +141,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To create a multi-label model in shogun, we'll first create an instance of [MultilabelModel](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelModel.html) and initialize it by the features and labels. The labels should be [MultilabelSOLables](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelSOLabels.html). It should be [initialized](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelSOLabels.html#a276b3e36a5b15d5185c913be160ac81c) by providing with the ```n_labels``` (number of examples) and ```n_classes``` (total number of classes) and then individually adding a label using [set\\_sparse\\_label()](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelSOLabels.html#a5a08cc53a1d06c4c797206fbbfaf13b3) method." + "To create a multi-label model in shogun, we'll first create an instance of [MultilabelModel](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1MultilabelModel.html) and initialize it by the features and labels. The labels should be [MultilabelSOLables](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelSOLabels.html). It should be [initialized](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelSOLabels.html#a276b3e36a5b15d5185c913be160ac81c) by providing with the ```n_labels``` (number of examples) and ```n_classes``` (total number of classes) and then individually adding a label using [set\\_sparse\\_label()](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelSOLabels.html#a5a08cc53a1d06c4c797206fbbfaf13b3) method." ] }, { @@ -245,7 +245,7 @@ "source": [ "For measuring accuracy in multi-label classification, *Jaccard Similarity Coefficients* $\\big(J(A, B) = \\frac{|A \\cap B|}{|A \\cup B|}\\big)$ is used : \n", "$Accuracy = \\frac{1}{p}\\sum_{i=1}^{p}\\frac{ |Y_i \\cap h(x_i)|}{|Y_i \\cup h(x_i)|}$ \n", - "This is available in [MultilabelAccuracy](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelAccuracy.html) for ```MultilabelLabels``` and [StructuredAccuracy](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CStructuredAccuracy.html) for ```MultilabelSOLabels```." + "This is available in [MultilabelAccuracy](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1CMultilabelAccuracy.html) for ```MultilabelLabels``` and [StructuredAccuracy](http://shogun-toolbox.org/doc/en/latest/classshogun_1_1StructuredAccuracy.html) for ```MultilabelSOLabels```." ] }, { diff --git a/doc/ipython-notebooks/template.ipynb b/doc/ipython-notebooks/template.ipynb index 8a3c7846fbe..084ee958e02 100644 --- a/doc/ipython-notebooks/template.ipynb +++ b/doc/ipython-notebooks/template.ipynb @@ -76,7 +76,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It is nice idea to include links to Shogun's Doxygen documentation. For example, this a link to the [SGObject class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSGObject.html) and this other one to the [`get_name`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CSGObject.html#a471caa9005cc581cb4514c0705b698ee) of this class." + "It is nice idea to include links to Shogun's Doxygen documentation. For example, this a link to the [SGObject class](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGObject.html) and this other one to the [`get_name`](http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1SGObject.html#a471caa9005cc581cb4514c0705b698ee) of this class." ] }, { diff --git a/examples/meta/generator/targets/cpp.json b/examples/meta/generator/targets/cpp.json index 626b119efeb..b5b79064094 100644 --- a/examples/meta/generator/targets/cpp.json +++ b/examples/meta/generator/targets/cpp.json @@ -1,5 +1,5 @@ { - "Program": "#include \n#include \n${dependencies}\n\nusing namespace shogun;\nusing namespace shogun::io;\n\nint main(int, char*[])\n{\n\n$program\nreturn 0;\n}\n", + "Program": "#include \n#include \n${dependencies}\n\nusing namespace shogun;\nusing namespace shogun::io;\n\nint main(int, char*[])\n{\n\n$program\nreturn 0;\n}\n", "Dependencies": { "IncludeAllClasses": true, "IncludeEnums": true, @@ -10,8 +10,8 @@ "Statement": "$statement;\n", "Comment": "//$comment\n", "Init": { - "Construct": "auto $name = some($arguments)$kwargs", - "Copy": "auto $name = wrap($expr)$kwargs", + "Construct": "auto $name = std::make_shared<$typeName>($arguments)$kwargs", + "Copy": "auto $name = $expr$kwargs", "KeywordArguments": { "List": ";\n$elements", "Element": "$name->put(\"$keyword\", $expr)", @@ -93,7 +93,7 @@ "LongRealMatrix": "SGMatrix", "ComplexMatrix": "SGMatrix", "RealDistance": "RealDistance", - "RealDenseDistance": "CDenseDistance" + "RealDenseDistance": "DenseDistance" }, "Expr": { "StringLiteral": "\"$literal\"", @@ -119,7 +119,7 @@ "get_option": "$object->get($arguments)", "get_string": "$object->get($arguments)" }, - "StaticCall": "C$typeName::$method($arguments)", + "StaticCall": "$typeName::$method($arguments)", "GlobalCall": "$method($arguments)", "Identifier": "$identifier", "Enum":"$typeName::$value" diff --git a/examples/meta/generator/targets/doc.md b/examples/meta/generator/targets/doc.md index 0f4f50391ce..86873356ced 100644 --- a/examples/meta/generator/targets/doc.md +++ b/examples/meta/generator/targets/doc.md @@ -200,7 +200,7 @@ "LongRealMatrix": "SGMatrix", "ComplexMatrix": "SGMatrix", "RealDistance": "RealDistance", - "RealDenseDistance": "CDenseDistance" + "RealDenseDistance": "DenseDistance" }, /** Translation rules for expressions diff --git a/examples/undocumented/libshogun/balanced_conditional_probability_tree.cpp b/examples/undocumented/libshogun/balanced_conditional_probability_tree.cpp index e07fed57ba4..db82dcefb49 100644 --- a/examples/undocumented/libshogun/balanced_conditional_probability_tree.cpp +++ b/examples/undocumented/libshogun/balanced_conditional_probability_tree.cpp @@ -16,13 +16,11 @@ int main(int argc, char **argv) { const char* train_file_name = "../data/7class_example4_train.dense"; const char* test_file_name = "../data/7class_example4_test.dense"; - CStreamingAsciiFile* train_file = new CStreamingAsciiFile(train_file_name); - SG_REF(train_file); + auto train_file = std::make_shared(train_file_name); - CStreamingDenseFeatures* train_features = new CStreamingDenseFeatures(train_file, true, 1024); - SG_REF(train_features); + auto train_features = std::make_shared>(train_file, true, 1024); - CBalancedConditionalProbabilityTree *cpt = new CBalancedConditionalProbabilityTree(); + auto cpt = std::make_shared(); cpt->set_num_passes(1); cpt->set_features(train_features); @@ -35,26 +33,18 @@ int main(int argc, char **argv) } cpt->train(); - cpt->print_tree(); - CStreamingAsciiFile* test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); - CStreamingDenseFeatures* test_features = new CStreamingDenseFeatures(test_file, true, 1024); - SG_REF(test_features); + auto test_file = std::make_shared(test_file_name); + auto test_features = std::make_shared>(test_file, true, 1024); - CMulticlassLabels *pred = cpt->apply_multiclass(test_features); + auto pred = cpt->apply_multiclass(test_features); test_features->reset_stream(); SG_SPRINT("num_labels = %d\n", pred->get_num_labels()); - SG_UNREF(test_features); - SG_UNREF(test_file); - test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); - test_features = new CStreamingDenseFeatures(test_file, true, 1024); - SG_REF(test_features); + test_file = std::make_shared(test_file_name); + test_features = std::make_shared>(test_file, true, 1024); - CMulticlassLabels *gnd = new CMulticlassLabels(pred->get_num_labels()); - SG_REF(gnd); + auto gnd = std::make_shared(pred->get_num_labels()); test_features->start_parser(); for (int32_t i=0; i < pred->get_num_labels(); ++i) { @@ -75,13 +65,5 @@ int main(int argc, char **argv) SG_SPRINT("Multiclass Accuracy = %.2f%%\n", 100.0*n_correct / gnd->get_num_labels()); - SG_UNREF(gnd); - SG_UNREF(train_features); - SG_UNREF(test_features); - SG_UNREF(train_file); - SG_UNREF(test_file); - SG_UNREF(cpt); - SG_UNREF(pred); - return 0; } diff --git a/examples/undocumented/libshogun/classifier_bagging_liblinear.cpp b/examples/undocumented/libshogun/classifier_bagging_liblinear.cpp index cee31f793b1..a845cb17801 100644 --- a/examples/undocumented/libshogun/classifier_bagging_liblinear.cpp +++ b/examples/undocumented/libshogun/classifier_bagging_liblinear.cpp @@ -24,24 +24,24 @@ int main(int argc, char** argv) int32_t bag_size = 25; /* streaming data generator for mean shift distributions */ - CMeanShiftDataGenerator* gen_n = new CMeanShiftDataGenerator(0, dim); - CMeanShiftDataGenerator* gen_p = new CMeanShiftDataGenerator(difference, dim); + MeanShiftDataGenerator* gen_n = new MeanShiftDataGenerator(0, dim); + MeanShiftDataGenerator* gen_p = new MeanShiftDataGenerator(difference, dim); - CFeatures* neg = gen_n->get_streamed_features(num_pos); - CFeatures* pos = gen_p->get_streamed_features(num_neg); - CDenseFeatures* train_feats = - neg->create_merged_copy(pos)->as>(); + Features* neg = gen_n->get_streamed_features(num_pos); + Features* pos = gen_p->get_streamed_features(num_neg); + DenseFeatures* train_feats = + neg->create_merged_copy(pos)->as>(); SGVector tl(num_neg+num_pos); tl.set_const(1); for (index_t i = 0; i < num_neg; ++i) tl[i] = -1; - CBinaryLabels* train_labels = new CBinaryLabels(tl); + BinaryLabels* train_labels = new BinaryLabels(tl); - CBaggingMachine* bm = new CBaggingMachine(train_feats, train_labels); - CLibLinear* ll = new CLibLinear(); + BaggingMachine* bm = new BaggingMachine(train_feats, train_labels); + LibLinear* ll = new LibLinear(); ll->set_bias_enabled(true); - CMajorityVote* mv = new CMajorityVote(); + MajorityVote* mv = new MajorityVote(); bm->set_num_bags(num_bags); bm->set_bag_size(bag_size); @@ -50,27 +50,23 @@ int main(int argc, char** argv) bm->train(); - CBinaryLabels* pred_bagging = bm->apply_binary(train_feats); - CContingencyTableEvaluation* eval = new CContingencyTableEvaluation(); + BinaryLabels* pred_bagging = bm->apply_binary(train_feats); + ContingencyTableEvaluation* eval = new ContingencyTableEvaluation(); pred_bagging->get_int_labels().display_vector(); float64_t bag_accuracy = eval->evaluate(pred_bagging, train_labels); float64_t oob_error = bm->get_oob_error(eval); - CLibLinear* libLin = new CLibLinear(2.0, train_feats, train_labels); + LibLinear* libLin = new LibLinear(2.0, train_feats, train_labels); libLin->set_bias_enabled(true); libLin->train(); - CBinaryLabels* pred_liblin = libLin->apply_binary(train_feats); + BinaryLabels* pred_liblin = libLin->apply_binary(train_feats); pred_liblin->get_int_labels().display_vector(); float64_t liblin_accuracy = eval->evaluate(pred_liblin, train_labels); SG_SPRINT("bagging accuracy: %f (OOB-error: %f)\nLibLinear accuracy: %f\n", bag_accuracy, oob_error, liblin_accuracy); - SG_UNREF(bm); - SG_UNREF(pos); - SG_UNREF(neg); - SG_UNREF(eval); return 0; } diff --git a/examples/undocumented/libshogun/classifier_featureblocklogisticregression.cpp b/examples/undocumented/libshogun/classifier_featureblocklogisticregression.cpp index a7fc44c0b0e..7d3f8923a22 100644 --- a/examples/undocumented/libshogun/classifier_featureblocklogisticregression.cpp +++ b/examples/undocumented/libshogun/classifier_featureblocklogisticregression.cpp @@ -18,10 +18,10 @@ int main(int argc, char** argv) for (int32_t i=0; i<4*4; i++) matrix.matrix[i]=i; - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); // create three labels - CBinaryLabels* labels=new CBinaryLabels(4); + BinaryLabels* labels=new BinaryLabels(4); labels->set_label(0, -1); labels->set_label(1, +1); labels->set_label(2, -1); @@ -48,7 +48,6 @@ int main(int argc, char** argv) regressor->get_w().display_vector(); - SG_UNREF(regressor); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/classifier_gaussian_process_binary_classification.cpp b/examples/undocumented/libshogun/classifier_gaussian_process_binary_classification.cpp index 169db1ae2d8..85bbb8da5f2 100644 --- a/examples/undocumented/libshogun/classifier_gaussian_process_binary_classification.cpp +++ b/examples/undocumented/libshogun/classifier_gaussian_process_binary_classification.cpp @@ -34,43 +34,39 @@ int main(int argc, char** argv) SGVector y_train; // load training features from file - CCSVFile* file_feat_train=new CCSVFile(fname_feat_train); + CSVFile* file_feat_train=new CSVFile(fname_feat_train); X_train.load(file_feat_train); - SG_UNREF(file_feat_train); // load training labels from file - CCSVFile* file_label_train=new CCSVFile(fname_label_train); + CSVFile* file_label_train=new CSVFile(fname_label_train); y_train.load(file_label_train); - SG_UNREF(file_label_train); // testing features SGMatrix X_test; // load testing features from file - CCSVFile* file_feat_test=new CCSVFile(fname_feat_test); + CSVFile* file_feat_test=new CSVFile(fname_feat_test); X_test.load(file_feat_test); - SG_UNREF(file_feat_test); // convert training and testing data into shogun representation - CDenseFeatures* feat_train=new CDenseFeatures(X_train); - CBinaryLabels* lab_train=new CBinaryLabels(y_train); - CDenseFeatures* feat_test=new CDenseFeatures(X_test); - SG_REF(feat_test); + DenseFeatures* feat_train=new DenseFeatures(X_train); + BinaryLabels* lab_train=new BinaryLabels(y_train); + DenseFeatures* feat_test=new DenseFeatures(X_test); // create Gaussian kernel with width = 2.0 - CGaussianKernel* kernel=new CGaussianKernel(10, 2.0); + GaussianKernel* kernel=new GaussianKernel(10, 2.0); // create zero mean function - CZeroMean* mean=new CZeroMean(); + ZeroMean* mean=new ZeroMean(); // you can easily switch between probit and logit likelihood models // by uncommenting/commenting the following lines: // create probit likelihood model - // CProbitLikelihood* lik=new CProbitLikelihood(); + // ProbitLikelihood* lik=new ProbitLikelihood(); // create logit likelihood model - CLogitLikelihood* lik=new CLogitLikelihood(); + LogitLikelihood* lik=new LogitLikelihood(); // you can easily switch between SingleLaplace and EP approximation by // uncommenting/commenting the following lines: @@ -80,16 +76,16 @@ int main(int argc, char** argv) // feat_train, mean, lab_train, lik); // specify EP approximation inference method - CEPInferenceMethod* inf=new CEPInferenceMethod(kernel, feat_train, mean, + EPInferenceMethod* inf=new EPInferenceMethod(kernel, feat_train, mean, lab_train, lik); // create and train GP classifier, which uses SingleLaplace approximation - CGaussianProcessClassification* gpc=new CGaussianProcessClassification(inf); + GaussianProcessClassification* gpc=new GaussianProcessClassification(inf); gpc->train(); // apply binary classification to the test data and get -1/+1 // labels of the predictions - CBinaryLabels* predictions=gpc->apply_binary(feat_test); + BinaryLabels* predictions=gpc->apply_binary(feat_test); predictions->get_labels().display_vector("predictions"); // get probabilities p(y*=1|x*) for each testing feature x* @@ -105,9 +101,6 @@ int main(int argc, char** argv) s2_test.display_vector("predictive variance"); // free up memory - SG_UNREF(gpc); - SG_UNREF(predictions); - SG_UNREF(feat_test); return 0; } diff --git a/examples/undocumented/libshogun/classifier_larank.cpp b/examples/undocumented/libshogun/classifier_larank.cpp index 63c8b4a6179..027223eddc2 100644 --- a/examples/undocumented/libshogun/classifier_larank.cpp +++ b/examples/undocumented/libshogun/classifier_larank.cpp @@ -21,15 +21,15 @@ void test() // create some linearly seperable data SGMatrix matrix(num_class, num_vec); SGMatrix matrix_test(num_class, num_vec); - CMulticlassLabels* labels=new CMulticlassLabels(num_vec); - CMulticlassLabels* labels_test=new CMulticlassLabels(num_vec); + MulticlassLabels* labels=new MulticlassLabels(num_vec); + MulticlassLabels* labels_test=new MulticlassLabels(num_vec); for (index_t i=0; iset_label(i, label); labels_test->set_label(i, label); } @@ -42,16 +42,16 @@ void test() labels->get_int_labels().display_vector("labels"); // shogun will now own the matrix created - CDenseFeatures* features=new CDenseFeatures(matrix); - CDenseFeatures* features_test= - new CDenseFeatures(matrix_test); + DenseFeatures* features=new DenseFeatures(matrix); + DenseFeatures* features_test= + new DenseFeatures(matrix_test); // create three labels for (index_t i=0; iset_label(i, i%num_class); // create gaussian kernel with cache 10MB, width 0.5 - CGaussianKernel* kernel = new CGaussianKernel(10, 0.5); + GaussianKernel* kernel = new GaussianKernel(10, 0.5); kernel->init(features, features); // create libsvm with C=10 and train @@ -60,7 +60,7 @@ void test() svm->train(); // classify on training examples - CMulticlassLabels* output=(CMulticlassLabels*)svm->apply(); + MulticlassLabels* output=(MulticlassLabels*)svm->apply(); output->get_labels().display_vector("batch output"); /* assert that batch apply and apply(index_t) give same result */ @@ -73,8 +73,8 @@ void test() for (index_t i=0; iget_num_labels(); ++i) ASSERT(output->get_label(i)==single_outputs[i]); - CMulticlassLabels* output_test= - (CMulticlassLabels*)svm->apply(features_test); + MulticlassLabels* output_test= + (MulticlassLabels*)svm->apply(features_test); labels_test->get_labels().display_vector("labels_test"); output_test->get_labels().display_vector("output_test"); @@ -82,10 +82,6 @@ void test() ASSERT(labels_test->get_label(i)==output_test->get_label(i)); // free up memory - SG_UNREF(output); - SG_UNREF(labels_test); - SG_UNREF(output_test); - SG_UNREF(svm); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/classifier_latent_svm.cpp b/examples/undocumented/libshogun/classifier_latent_svm.cpp index 5f4b86b875a..ea195925022 100644 --- a/examples/undocumented/libshogun/classifier_latent_svm.cpp +++ b/examples/undocumented/libshogun/classifier_latent_svm.cpp @@ -15,9 +15,9 @@ using namespace shogun; #define MAX_LINE_LENGTH 4096 #define HOG_SIZE 1488 -struct CBoundingBox : public CData +struct CBoundingBox : public Data { - CBoundingBox(int32_t x, int32_t y) : CData(), x_pos(x), y_pos(y) {}; + CBoundingBox(int32_t x, int32_t y) : Data(), x_pos(x), y_pos(y) {}; int32_t x_pos, y_pos; @@ -25,9 +25,9 @@ struct CBoundingBox : public CData virtual const char* get_name() const { return "BoundingBox"; } }; -struct CHOGFeatures : public CData +struct CHOGFeatures : public Data { - CHOGFeatures(int32_t w, int32_t h) : CData(), width(w), height(h) {}; + CHOGFeatures(int32_t w, int32_t h) : Data(), width(w), height(h) {}; int32_t width, height; float64_t ***hog; @@ -36,17 +36,17 @@ struct CHOGFeatures : public CData virtual const char* get_name() const { return "HOGFeatures"; } }; -class CObjectDetector: public CLatentModel +class CObjectDetector: public LatentModel { public: CObjectDetector() {} - CObjectDetector(CLatentFeatures* feat, CLatentLabels* labels) : CLatentModel(feat, labels) {} + CObjectDetector(LatentFeatures* feat, CLatentLabels* labels) : LatentModel(feat, labels) {} virtual ~CObjectDetector() {} virtual int32_t get_dim() const { return HOG_SIZE; } - virtual CDotFeatures* get_psi_feature_vectors() + virtual DotFeatures* get_psi_feature_vectors() { int32_t num_examples = this->get_num_vectors(); int32_t dim = this->get_dim(); @@ -58,21 +58,21 @@ class CObjectDetector: public CLatentModel memcpy(psi_m.matrix+i*dim, hf->hog[bb->x_pos][bb->y_pos], dim*sizeof(float64_t)); } - CDenseFeatures* psi_feats = new CDenseFeatures(psi_m); + DenseFeatures* psi_feats = new DenseFeatures(psi_m); return psi_feats; } - virtual CData* infer_latent_variable(const SGVector& w, index_t idx) + virtual Data* infer_latent_variable(const SGVector& w, index_t idx) { int32_t pos_x = 0, pos_y = 0; - float64_t max_score = -CMath::INFTY; + float64_t max_score = -Math::INFTY; CHOGFeatures* hf = (CHOGFeatures*) m_features->get_sample(idx); for (int i = 0; i < hf->width; ++i) { for (int j = 0; j < hf->height; ++j) { - float64_t score = CMath::dot(w.vector, hf->hog[i][j], w.vlen); + float64_t score = Math::dot(w.vector, hf->hog[i][j], w.vlen); if (score > max_score) { @@ -84,14 +84,13 @@ class CObjectDetector: public CLatentModel } SG_SDEBUG("%d %d %f\n", pos_x, pos_y, max_score); CBoundingBox* h = new CBoundingBox(pos_x, pos_y); - SG_REF(h); return h; } }; -static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& labels) +static void read_dataset(char* fname, LatentFeatures*& feats, CLatentLabels*& labels) { FILE* fd = fopen(fname, "r"); char line[MAX_LINE_LENGTH]; @@ -107,15 +106,13 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l num_examples = atoi(line); labels = new CLatentLabels(num_examples); - SG_REF(labels); - CBinaryLabels* ys = new CBinaryLabels(num_examples); + BinaryLabels* ys = new BinaryLabels(num_examples); - feats = new CLatentFeatures(num_examples); - SG_REF(feats); + feats = new LatentFeatures(num_examples); auto pb = progress(range(num_examples)); - CMath::init_random(); + Math::init_random(); for (int i = 0; (!feof(fd)) && (i < num_examples); ++i) { fgets(line, MAX_LINE_LENGTH, fd); @@ -147,8 +144,8 @@ static void read_dataset(char* fname, CLatentFeatures*& feats, CLatentLabels*& l height = atoi(last_pchar); /* create latent label */ - int x = CMath::random(0, width-1); - int y = CMath::random(0, height-1); + int x = Math::random(0, width-1); + int y = Math::random(0, height-1); CBoundingBox* bb = new CBoundingBox(x,y); labels->add_latent_label(bb); @@ -191,7 +188,7 @@ int main(int argc, char** argv) SG_SERROR("not enough arguements given\n"); } - CLatentFeatures* train_feats = NULL; + LatentFeatures* train_feats = NULL; CLatentLabels* train_labels = NULL; /* read train data set */ read_dataset(argv[1], train_feats, train_labels); @@ -200,10 +197,10 @@ int main(int argc, char** argv) float64_t C = 10.0; CObjectDetector* od = new CObjectDetector(train_feats, train_labels); - CLatentSVM llm(od, C); + LatentSVM llm(od, C); llm.train(); - // CLatentFeatures* test_feats = NULL; + // LatentFeatures* test_feats = NULL; // CLatentLabels* test_labels = NULL; // read_dataset(argv[2], test_feats, test_labels); diff --git a/examples/undocumented/libshogun/classifier_lda.cpp b/examples/undocumented/libshogun/classifier_lda.cpp index ba0e38e6fd7..802677a3e08 100644 --- a/examples/undocumented/libshogun/classifier_lda.cpp +++ b/examples/undocumented/libshogun/classifier_lda.cpp @@ -26,29 +26,26 @@ void test() SGVector< float64_t > lab(CLASSES*NUM); SGMatrix< float64_t > feat(DIMS, CLASSES*NUM); - feat = CDataGenerator::generate_gaussians(NUM,CLASSES,DIMS); + feat = DataGenerator::generate_gaussians(NUM,CLASSES,DIMS); for( int i = 0 ; i < CLASSES ; ++i ) for( int j = 0 ; j < NUM ; ++j ) lab[i*NUM+j] = double(i); // Create train labels - CMulticlassLabels* labels = new CMulticlassLabels(lab); + auto labels = std::make_shared(lab); // Create train features - CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feat); + auto features = std::make_shared>(feat); // Create QDA classifier - CMCLDA* lda = new CMCLDA(features, labels); - SG_REF(lda); + auto lda = std::make_shared(features, labels); lda->train(); // Classify and display output auto output = multiclass_labels(lda->apply()); - SG_REF(output); SGVector::display_vector(output->get_labels().vector, output->get_num_labels()); // Free memory - SG_UNREF(lda); #endif } diff --git a/examples/undocumented/libshogun/classifier_libsvm_probabilities.cpp b/examples/undocumented/libshogun/classifier_libsvm_probabilities.cpp index 5a1a9a5433c..818a20a9f5d 100644 --- a/examples/undocumented/libshogun/classifier_libsvm_probabilities.cpp +++ b/examples/undocumented/libshogun/classifier_libsvm_probabilities.cpp @@ -18,13 +18,13 @@ void gen_rand_data(SGMatrix features, SGVector labels, flo { labels[i]=-1.0; for(int32_t j=0; j(labelVector); // create train features - CDenseFeatures* features = new CDenseFeatures(featureMatrix); - SG_REF(features); + auto features = std::make_shared>(featureMatrix); // create linear kernel - CLinearKernel* kernel = new CLinearKernel(); - SG_REF(kernel); + auto kernel = std::make_shared(); kernel->init(features, features); // create svm classifier by LibSVM - CLibSVM* svm = new CLibSVM(svm_C, kernel, labels); - SG_REF(svm); + auto svm = std::make_shared(svm_C, kernel, labels); svm->train(); // classify data points - CBinaryLabels* out_labels = svm->apply()->as(); + auto out_labels = svm->apply()->as(); /*convert scores to calibrated probabilities by fitting a sigmoid function using the method described in Lin, H., Lin, C., and Weng, R. (2007). A note on Platt's probabilistic outputs for support vector machines. See BinaryLabels documentation for details*/ - CSigmoidCalibration* sigmoid_calibration = new CSigmoidCalibration(); + auto sigmoid_calibration = std::make_shared(); sigmoid_calibration->fit_binary( - out_labels, labels->as()); - CBinaryLabels* calibrated_labels = + out_labels, labels->as()); + auto calibrated_labels = sigmoid_calibration->calibrate_binary(out_labels); // display output labels and probabilities @@ -86,12 +82,6 @@ int main(int argc, char** argv) } // clean up - SG_UNREF(sigmoid_calibration); - SG_UNREF(calibrated_labels); - SG_UNREF(out_labels); - SG_UNREF(kernel); - SG_UNREF(features); - SG_UNREF(svm); return 0; } diff --git a/examples/undocumented/libshogun/classifier_minimal_svm.cpp b/examples/undocumented/libshogun/classifier_minimal_svm.cpp index 5fd54abcd77..9701a8a8846 100644 --- a/examples/undocumented/libshogun/classifier_minimal_svm.cpp +++ b/examples/undocumented/libshogun/classifier_minimal_svm.cpp @@ -16,20 +16,20 @@ int main(int argc, char** argv) // create three 2-dimensional vectors // shogun will now own the matrix created - CDenseFeatures* features= new CDenseFeatures(matrix); + auto features= std::make_shared>(matrix); // create three labels - CBinaryLabels* labels=new CBinaryLabels(3); + auto labels=std::make_shared(3); labels->set_label(0, -1); labels->set_label(1, +1); labels->set_label(2, -1); // create gaussian kernel with cache 10MB, width 0.5 - CGaussianKernel* kernel = new CGaussianKernel(10, 0.5); + auto kernel = std::make_shared(10, 0.5); kernel->init(features, features); // create libsvm with C=10 and train - CLibSVM* svm = new CLibSVM(10, kernel, labels); + auto svm = std::make_shared(10, kernel, labels); svm->train(); // classify on training examples @@ -37,7 +37,6 @@ int main(int argc, char** argv) SG_SPRINT("output[%d]=%f\n", i, svm->apply_one(i)); // free up memory - SG_UNREF(svm); return 0; } diff --git a/examples/undocumented/libshogun/classifier_mkl_svmlight_modelselection_bug.cpp b/examples/undocumented/libshogun/classifier_mkl_svmlight_modelselection_bug.cpp index dbea54ff7eb..25f71926856 100644 --- a/examples/undocumented/libshogun/classifier_mkl_svmlight_modelselection_bug.cpp +++ b/examples/undocumented/libshogun/classifier_mkl_svmlight_modelselection_bug.cpp @@ -23,33 +23,33 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c1=new ModelSelectionParameters("C1"); root->append_child(c1); c1->build_values(-1.0, 1.0, R_EXP); - CModelSelectionParameters* c2=new CModelSelectionParameters("C2"); + ModelSelectionParameters* c2=new ModelSelectionParameters("C2"); root->append_child(c2); c2->build_values(-1.0, 1.0, R_EXP); - CCombinedKernel* kernel1=new CCombinedKernel(); - kernel1->append_kernel(new CGaussianKernel(10, 2)); - kernel1->append_kernel(new CGaussianKernel(10, 3)); - kernel1->append_kernel(new CGaussianKernel(10, 4)); + CombinedKernel* kernel1=new CombinedKernel(); + kernel1->append_kernel(new GaussianKernel(10, 2)); + kernel1->append_kernel(new GaussianKernel(10, 3)); + kernel1->append_kernel(new GaussianKernel(10, 4)); - CModelSelectionParameters* param_kernel1=new CModelSelectionParameters( + ModelSelectionParameters* param_kernel1=new ModelSelectionParameters( "kernel", kernel1); root->append_child(param_kernel1); - CCombinedKernel* kernel2=new CCombinedKernel(); - kernel2->append_kernel(new CGaussianKernel(10, 20)); - kernel2->append_kernel(new CGaussianKernel(10, 30)); - kernel2->append_kernel(new CGaussianKernel(10, 40)); + CombinedKernel* kernel2=new CombinedKernel(); + kernel2->append_kernel(new GaussianKernel(10, 20)); + kernel2->append_kernel(new GaussianKernel(10, 30)); + kernel2->append_kernel(new GaussianKernel(10, 40)); - CModelSelectionParameters* param_kernel2=new CModelSelectionParameters( + ModelSelectionParameters* param_kernel2=new ModelSelectionParameters( "kernel", kernel2); root->append_child(param_kernel2); @@ -65,15 +65,15 @@ void test() /* create some data and labels */ SGMatrix matrix(dim_vectors, num_vectors); - CBinaryLabels* labels=new CBinaryLabels(num_vectors); + BinaryLabels* labels=new BinaryLabels(num_vectors); for (int32_t i=0; i* features=new CDenseFeatures(matrix); + DenseFeatures* features=new DenseFeatures(matrix); /* create combined features */ - CCombinedFeatures* comb_features=new CCombinedFeatures(); + CombinedFeatures* comb_features=new CombinedFeatures(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); @@ -83,28 +83,28 @@ void test() labels->set_label(i, i%2==0 ? 1 : -1); /* works */ - CMKLClassification* classifier=new CMKLClassification(new CLibSVM()); + MKLClassification* classifier=new MKLClassification(new CLibSVM()); classifier->set_interleaved_optimization_enabled(false); /* the above plus this does not work (interleaved only with SVMLight)*/ // classifier->set_interleaved_optimization_enabled(true); /* However, SVMLight does not work */ -// CMKLClassification* classifier=new CMKLClassification(new CSVMLight()); +// MKLClassification* classifier=new MKLClassification(new SVMLight()); // /* any of those */ // classifier->set_interleaved_optimization_enabled(false); // classifier->set_interleaved_optimization_enabled(true); /* splitting strategy */ - CStratifiedCrossValidationSplitting* splitting_strategy= - new CStratifiedCrossValidationSplitting(labels, num_subsets); + StratifiedCrossValidationSplitting* splitting_strategy= + new StratifiedCrossValidationSplitting(labels, num_subsets); /* accuracy evaluation */ - CContingencyTableEvaluation* evaluation_criterium= - new CContingencyTableEvaluation(ACCURACY); + ContingencyTableEvaluation* evaluation_criterium= + new ContingencyTableEvaluation(ACCURACY); /* cross validation class for evaluation in model selection */ - CCrossValidation* cross=new CCrossValidation(classifier, comb_features, + CrossValidation* cross=new CrossValidation(classifier, comb_features, labels, splitting_strategy, evaluation_criterium); cross->set_num_runs(1); /* TODO: remove this once locking is fixed for combined kernels */ @@ -114,8 +114,7 @@ void test() * Dont worry if yours is not included, simply write to the mailing list */ classifier->print_modsel_params(); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=create_param_tree(); + ModelSelectionParameters* param_tree=create_param_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -133,14 +132,11 @@ void test() /* larger number of runs to have tighter confidence intervals */ cross->set_num_runs(10); // cross->set_conf_int_alpha(0.01); - CEvaluationResult* result=cross->evaluate(); + EvaluationResult* result=cross->evaluate(); SG_SPRINT("result: "); result->print_result(); /* clean up destroy result parameter */ - SG_UNREF(best_combination); - SG_UNREF(grid_search); - SG_UNREF(result); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/classifier_mklmulticlass.cpp b/examples/undocumented/libshogun/classifier_mklmulticlass.cpp index 32d13d7bf86..e3d0691d9c3 100644 --- a/examples/undocumented/libshogun/classifier_mklmulticlass.cpp +++ b/examples/undocumented/libshogun/classifier_mklmulticlass.cpp @@ -51,7 +51,7 @@ void getgauss(float64_t & y1, float64_t & y2) void gendata(std::vector & x,std::vector & y, - CMulticlassLabels*& lab) + std::shared_ptr& lab) { int32_t totalsize=240; int32_t class1size=80; @@ -83,7 +83,7 @@ void gendata(std::vector & x,std::vector & y, } //set labels - lab=new CMulticlassLabels(x.size()); + lab.reset(new MulticlassLabels(x.size())); for(size_t i=0; i< x.size();++i) { if((int32_t)i < class1size) @@ -194,11 +194,10 @@ void gentestkernel(float64_t * & ker1 ,float64_t * & ker2,float64_t * & ker3, void tester() { - CMulticlassLabels* lab=NULL; + std::shared_ptr lab; std::vector x,y; gendata(x,y, lab); - SG_REF(lab); float64_t* ker1=NULL; float64_t* ker2=NULL; @@ -212,11 +211,11 @@ void tester() gentrainkernel( ker1 , ker2, ker3 , autosigma, n1, n2, n3,x,y); numdata=x.size(); - CCombinedKernel* ker=new CCombinedKernel(); + auto ker=std::make_shared(); - CCustomKernel* kernel1=new CCustomKernel(); - CCustomKernel* kernel2=new CCustomKernel(); - CCustomKernel* kernel3=new CCustomKernel(); + auto kernel1=std::make_shared(); + auto kernel2=std::make_shared(); + auto kernel3=std::make_shared(); kernel1->set_full_kernel_matrix_from_full(SGMatrix(ker1, numdata,numdata,false)); kernel2->set_full_kernel_matrix_from_full(SGMatrix(ker2, numdata,numdata,false)); @@ -233,7 +232,7 @@ void tester() //here comes the core stuff float64_t regconst=1.0; - CMKLMulticlass* tsvm =new CMKLMulticlass(regconst, ker, lab); + auto tsvm =std::make_shared(regconst, ker, lab); tsvm->set_epsilon(0.0001); // SVM epsilon // MKL parameters @@ -246,7 +245,7 @@ void tester() SG_SPRINT("finished svm training\n"); //starting svm testing on training data - CMulticlassLabels* res = tsvm->apply()->as(); + auto res = tsvm->apply()->as(); ASSERT(res); float64_t err=0; @@ -261,12 +260,11 @@ void tester() SG_SPRINT("random guess error would be: %f \n",2/3.0); //generate test data - CMulticlassLabels* tlab=NULL; + std::shared_ptr tlab; std::vector tx,ty; gendata( tx,ty,tlab); - SG_REF(tlab); float64_t* tker1=NULL; float64_t* tker2=NULL; @@ -275,11 +273,10 @@ void tester() gentestkernel(tker1,tker2,tker3, autosigma, n1,n2,n3, x,y, tx,ty); int32_t numdatatest=tx.size(); - CCombinedKernel* tker=new CCombinedKernel(); - SG_REF(tker); - CCustomKernel* tkernel1=new CCustomKernel(); - CCustomKernel* tkernel2=new CCustomKernel(); - CCustomKernel* tkernel3=new CCustomKernel(); + auto tker=std::make_shared(); + auto tkernel1=std::make_shared(); + auto tkernel2=std::make_shared(); + auto tkernel3=std::make_shared(); tkernel1->set_full_kernel_matrix_from_full(SGMatrix(tker1,numdata, numdatatest, false)); tkernel2->set_full_kernel_matrix_from_full(SGMatrix(tker2,numdata, numdatatest, false)); @@ -308,7 +305,7 @@ void tester() tsvm->set_kernel(tker); //compute classification error, check mem - CMulticlassLabels* tres = tsvm->apply()->as(); + auto tres = tsvm->apply()->as(); float64_t terr=0; for(int32_t i=0; i(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + auto features=std::make_shared>(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + auto label_file = std::make_shared(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + auto labels=std::make_shared(label_vec); // Create liblinear svm classifier with L2-regularized L2-loss - CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); - SG_REF(svm); + auto svm = std::make_shared(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); // Create a multiclass svm classifier that consists of several of the previous one - CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( - new CECOCStrategy(new CECOCOVREncoder(), new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels); - SG_REF(mc_svm); + auto mc_svm = std::make_shared( + std::make_shared(std::make_shared(), std::make_shared()), features->as(), svm, labels); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results - CMulticlassLabels* output = mc_svm->apply()->as(); + auto output = mc_svm->apply()->as(); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::display_vector(out_labels.vector, out_labels.vlen); // Free resources - SG_UNREF(mc_svm); - SG_UNREF(svm); - SG_UNREF(output); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/classifier_multiclass_ecoc_discriminant.cpp b/examples/undocumented/libshogun/classifier_multiclass_ecoc_discriminant.cpp index c6e30dc64fb..607d9a9bfb7 100644 --- a/examples/undocumented/libshogun/classifier_multiclass_ecoc_discriminant.cpp +++ b/examples/undocumented/libshogun/classifier_multiclass_ecoc_discriminant.cpp @@ -19,55 +19,44 @@ const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + CSVFile* feature_file = new CSVFile(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + DenseFeatures* features=new DenseFeatures(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + CSVFile* label_file = new CSVFile(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + MulticlassLabels* labels=new MulticlassLabels(label_vec); // Create liblinear svm classifier with L2-regularized L2-loss - CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); - SG_REF(svm); + LibLinear* svm = new LibLinear(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); - CECOCDiscriminantEncoder *encoder = new CECOCDiscriminantEncoder(); + ECOCDiscriminantEncoder *encoder = new ECOCDiscriminantEncoder(); encoder->set_features(features); encoder->set_labels(labels); // Create a multiclass svm classifier that consists of several of the previous one - CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( - new CECOCStrategy(encoder, new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels); - SG_REF(mc_svm); + LinearMulticlassMachine* mc_svm = new LinearMulticlassMachine( + new CECOCStrategy(encoder, new CECOCHDDecoder()), (DotFeatures*) features, svm, labels); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results - CMulticlassLabels* output = mc_svm->apply()->as(); + MulticlassLabels* output = mc_svm->apply()->as(); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::display_vector(out_labels.vector, out_labels.vlen); // Free resources - SG_UNREF(mc_svm); - SG_UNREF(svm); - SG_UNREF(output); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/classifier_multiclass_ecoc_random.cpp b/examples/undocumented/libshogun/classifier_multiclass_ecoc_random.cpp index 2f8dc1e8ab4..33bd3a8910f 100644 --- a/examples/undocumented/libshogun/classifier_multiclass_ecoc_random.cpp +++ b/examples/undocumented/libshogun/classifier_multiclass_ecoc_random.cpp @@ -20,51 +20,40 @@ const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + CSVFile* feature_file = new CSVFile(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + DenseFeatures* features=new DenseFeatures(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + CSVFile* label_file = new CSVFile(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + MulticlassLabels* labels=new MulticlassLabels(label_vec); // Create liblinear svm classifier with L2-regularized L2-loss - CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); - SG_REF(svm); + LibLinear* svm = new LibLinear(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); // Create a multiclass svm classifier that consists of several of the previous one - CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( - new CECOCStrategy(new CECOCRandomDenseEncoder(), new CECOCHDDecoder()), (CDotFeatures*) features, svm, labels); - SG_REF(mc_svm); + LinearMulticlassMachine* mc_svm = new LinearMulticlassMachine( + new CECOCStrategy(new CECOCRandomDenseEncoder(), new CECOCHDDecoder()), (DotFeatures*) features, svm, labels); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results - CMulticlassLabels* output = mc_svm->apply()->as(); + MulticlassLabels* output = mc_svm->apply()->as(); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector< int32_t >::display_vector(out_labels.vector, out_labels.vlen); // Free resources - SG_UNREF(mc_svm); - SG_UNREF(svm); - SG_UNREF(output); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/classifier_multiclass_prob_heuristics.cpp b/examples/undocumented/libshogun/classifier_multiclass_prob_heuristics.cpp index 4cf3a9f0f23..fd2c62ab777 100644 --- a/examples/undocumented/libshogun/classifier_multiclass_prob_heuristics.cpp +++ b/examples/undocumented/libshogun/classifier_multiclass_prob_heuristics.cpp @@ -19,26 +19,21 @@ const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + auto feature_file = std::make_shared(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + auto features=std::make_shared>(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + auto label_file = std::make_shared(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + auto labels=std::make_shared(label_vec); // Create liblinear svm classifier with L2-regularized L2-loss - CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); - SG_REF(svm); + auto svm = std::make_shared(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); @@ -48,15 +43,14 @@ void test() // There are several heuristics are implemented: // OVA_NORM, OVA_SOFTMAX // OVO_PRICE, OVO_HASTIE, OVO_HAMAMURA - CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( - new CMulticlassOneVsOneStrategy(OVO_HASTIE), (CDotFeatures*) features, svm, labels); - SG_REF(mc_svm); + auto mc_svm = std::make_shared( + std::make_shared(OVO_HASTIE), features->as(), svm, labels); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results - CMulticlassLabels* output = mc_svm->apply()->as(); + auto output = mc_svm->apply()->as(); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector::display_vector(out_labels.vector, out_labels.vlen); @@ -70,11 +64,6 @@ void test() } //Free resources - SG_UNREF(mc_svm); - SG_UNREF(svm); - SG_UNREF(output); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/classifier_multiclass_relaxedtree.cpp b/examples/undocumented/libshogun/classifier_multiclass_relaxedtree.cpp index a0d7c3f3223..8dcb8bef137 100644 --- a/examples/undocumented/libshogun/classifier_multiclass_relaxedtree.cpp +++ b/examples/undocumented/libshogun/classifier_multiclass_relaxedtree.cpp @@ -18,48 +18,36 @@ const char* fname_labels = "../data/7class_example4_train.label"; int main(int argc, char** argv) { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + CSVFile* feature_file = new CSVFile(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + DenseFeatures* features=new DenseFeatures(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + CSVFile* label_file = new CSVFile(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + MulticlassLabels* labels=new MulticlassLabels(label_vec); // Create RelaxedTree Machine CRelaxedTree *machine = new CRelaxedTree(); - SG_REF(machine); machine->set_labels(labels); - CKernel *kernel = new CGaussianKernel(); - SG_REF(kernel); + Kernel *kernel = new GaussianKernel(); machine->set_kernel(kernel); - CMulticlassLibLinear *svm = new CMulticlassLibLinear(); + MulticlassLibLinear *svm = new MulticlassLibLinear(); machine->set_machine_for_confusion_matrix(svm); machine->train(features); - CMulticlassLabels* output = machine->apply()->as(); + MulticlassLabels* output = machine->apply()->as(); - CMulticlassAccuracy *evaluator = new CMulticlassAccuracy(); + MulticlassAccuracy *evaluator = new MulticlassAccuracy(); SG_SPRINT("Accuracy = %.4f\n", evaluator->evaluate(output, labels)); // Free resources - SG_UNREF(machine); - SG_UNREF(output); - SG_UNREF(features); - SG_UNREF(labels); - SG_UNREF(evaluator); - SG_UNREF(kernel); return 0; } diff --git a/examples/undocumented/libshogun/classifier_multiclass_shareboost.cpp b/examples/undocumented/libshogun/classifier_multiclass_shareboost.cpp index 5e358d75be0..e36fe738c65 100644 --- a/examples/undocumented/libshogun/classifier_multiclass_shareboost.cpp +++ b/examples/undocumented/libshogun/classifier_multiclass_shareboost.cpp @@ -15,28 +15,23 @@ const char* fname_labels = "../data/7class_example4_train.label"; int main(int argc, char** argv) { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + CSVFile* feature_file = new CSVFile(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + DenseFeatures* features=new DenseFeatures(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + CSVFile* label_file = new CSVFile(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + MulticlassLabels* labels=new MulticlassLabels(label_vec); SG_SPRINT("Performing ShareBoost on a %d-class problem\n", labels->get_num_classes()); // Create ShareBoost Machine CShareBoost *machine = new CShareBoost(features, labels, 10); - SG_REF(machine); machine->train(); @@ -46,9 +41,8 @@ int main(int argc, char** argv) SG_SPRINT("activeset[%02d] = %d\n", i, activeset[i]); CDenseSubsetFeatures *subset_fea = new CDenseSubsetFeatures(features, machine->get_activeset()); - SG_REF(subset_fea); - CMulticlassLabels* output = - machine->apply(subset_fea)->as(); + MulticlassLabels* output = + machine->apply(subset_fea)->as(); int32_t correct = 0; for (int32_t i=0; i < output->get_num_labels(); ++i) @@ -56,12 +50,5 @@ int main(int argc, char** argv) correct++; SG_SPRINT("Accuracy = %.4f\n", float64_t(correct)/labels->get_num_labels()); - // Free resources - SG_UNREF(machine); - SG_UNREF(output); - SG_UNREF(subset_fea); - SG_UNREF(features); - SG_UNREF(labels); - return 0; } diff --git a/examples/undocumented/libshogun/classifier_multiclasslibsvm.cpp b/examples/undocumented/libshogun/classifier_multiclasslibsvm.cpp index 8ede0824b26..f06005d9599 100644 --- a/examples/undocumented/libshogun/classifier_multiclasslibsvm.cpp +++ b/examples/undocumented/libshogun/classifier_multiclasslibsvm.cpp @@ -17,23 +17,23 @@ int main(int argc, char** argv) // create vectors // shogun will now own the matrix created - CDenseFeatures* features=new CDenseFeatures(matrix); + auto features=std::make_shared>(matrix); // create three labels - CMulticlassLabels* labels=new CMulticlassLabels(num_vec); + auto labels=std::make_shared(num_vec); for (index_t i=0; iset_label(i, i%num_class); // create gaussian kernel with cache 10MB, width 0.5 - CGaussianKernel* kernel = new CGaussianKernel(10, 0.5); + auto kernel = std::make_shared(10, 0.5); kernel->init(features, features); // create libsvm with C=10 and train - CMulticlassLibSVM* svm = new CMulticlassLibSVM(10, kernel, labels); + auto svm = std::make_shared(10, kernel, labels); svm->train(); // classify on training examples - CMulticlassLabels* output = svm->apply()->as(); + auto output = svm->apply()->as(); SGVector::display_vector(output->get_labels().vector, output->get_num_labels(), "batch output"); @@ -44,10 +44,8 @@ int main(int argc, char** argv) SG_SPRINT("single output[%d]=%f\n", i, label); ASSERT(output->get_label(i)==label); } - SG_UNREF(output); // free up memory - SG_UNREF(svm); return 0; } diff --git a/examples/undocumented/libshogun/classifier_multiclasslinearmachine.cpp b/examples/undocumented/libshogun/classifier_multiclasslinearmachine.cpp index 78e0e14e273..f152866830b 100644 --- a/examples/undocumented/libshogun/classifier_multiclasslinearmachine.cpp +++ b/examples/undocumented/libshogun/classifier_multiclasslinearmachine.cpp @@ -18,51 +18,40 @@ const char fname_labels[]="../data/label_train_multiclass.dat"; void test() { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + auto feature_file = std::make_shared(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + auto features=std::make_shared>(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + auto label_file = std::make_shared(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + auto labels=std::make_shared(label_vec); // Create liblinear svm classifier with L2-regularized L2-loss - CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); - SG_REF(svm); + auto svm = std::make_shared(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(true); // Create a multiclass svm classifier that consists of several of the previous one - CLinearMulticlassMachine* mc_svm = new CLinearMulticlassMachine( - new CMulticlassOneVsOneStrategy(), (CDotFeatures*) features, svm, labels); - SG_REF(mc_svm); + auto mc_svm = std::make_shared( + std::make_shared(), features->as(), svm, labels); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); // Classify the training examples and show the results - CMulticlassLabels* output = mc_svm->apply()->as(); + auto output = mc_svm->apply()->as(); SGVector< int32_t > out_labels = output->get_int_labels(); SGVector::display_vector(out_labels.vector, out_labels.vlen); //Free resources - SG_UNREF(mc_svm); - SG_UNREF(svm); - SG_UNREF(output); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/classifier_nearest_centroid.cpp b/examples/undocumented/libshogun/classifier_nearest_centroid.cpp index c8470eb53e2..7905e92c3ad 100644 --- a/examples/undocumented/libshogun/classifier_nearest_centroid.cpp +++ b/examples/undocumented/libshogun/classifier_nearest_centroid.cpp @@ -12,34 +12,32 @@ int main(){ // create some data SGMatrix matrix(num_feat, num_vec); - CMath::range_fill_vector(matrix.matrix, num_feat*num_vec); + Math::range_fill_vector(matrix.matrix, num_feat*num_vec); // Create features ; shogun will now own the matrix created - CDenseFeatures* features=new CDenseFeatures(matrix); + DenseFeatures* features=new DenseFeatures(matrix); - CMath::display_matrix(matrix.matrix,num_feat,num_vec); + Math::display_matrix(matrix.matrix,num_feat,num_vec); //Create labels - CLabels* labels=new CLabels(num_vec); + Labels* labels=new Labels(num_vec); for (index_t i=0; iset_label(i, i%num_class); //Create Euclidean Distance - CEuclideanDistance* distance = new CEuclideanDistance(features,features); + EuclideanDistance* distance = new EuclideanDistance(features,features); //Create Nearest Centroid CNearestCentroid* nearest_centroid = new CNearestCentroid(distance, labels); nearest_centroid->train(); // classify on training examples - CLabels* output=nearest_centroid->apply(); - CMath::display_vector(output->get_labels().vector, output->get_num_labels(), + Labels* output=nearest_centroid->apply(); + Math::display_vector(output->get_labels().vector, output->get_num_labels(), "batch output"); - SG_UNREF(output); // free up memory - SG_UNREF(nearest_centroid); return 0; } \ No newline at end of file diff --git a/examples/undocumented/libshogun/classifier_newtontest.cpp b/examples/undocumented/libshogun/classifier_newtontest.cpp index 38f298a7bd7..2f8f0ed354f 100644 --- a/examples/undocumented/libshogun/classifier_newtontest.cpp +++ b/examples/undocumented/libshogun/classifier_newtontest.cpp @@ -19,20 +19,18 @@ int main(int argc,char *argv[]) for (int i=0; i* features = new CDenseFeatures(fmatrix); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(fmatrix); /*Creating random labels */ - CLabels* labels=new CLabels(x_n); + Labels* labels=new Labels(x_n); // create labels, two classes labels->set_label(0,1); labels->set_label(1,-1); labels->set_label(2,1); labels->set_label(3,1); - SG_REF(labels); /*Working with Newton SVM */ @@ -40,10 +38,7 @@ int main(int argc,char *argv[]) int32_t iter=20; CNewtonSVM *nsvm = new CNewtonSVM(lambda,features,labels,iter); - SG_REF(nsvm); nsvm->train(); - SG_UNREF(labels); - SG_UNREF(nsvm); SG_SPRINT("TEST 2:\n\n"); @@ -54,12 +49,11 @@ int main(int argc,char *argv[]) for (int i=0; iset_feature_matrix(fmatrix2); - SG_REF(features); /*Creating random labels */ - CLabels* labels2=new CLabels(x_n); + Labels* labels2=new Labels(x_n); // create labels, two classes labels2->set_label(0,1); @@ -67,7 +61,6 @@ int main(int argc,char *argv[]) labels2->set_label(2,1); labels2->set_label(3,1); labels2->set_label(4,-1); - SG_REF(labels2); /*Working with Newton SVM */ @@ -75,13 +68,8 @@ int main(int argc,char *argv[]) iter=20; CNewtonSVM *nsvm2 = new CNewtonSVM(lambda,features,labels2,iter); - SG_REF(nsvm2); nsvm2->train(); - - SG_UNREF(labels2); - SG_UNREF(nsvm2); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/classifier_qda.cpp b/examples/undocumented/libshogun/classifier_qda.cpp index d93b7f1ccb7..99023e95abc 100644 --- a/examples/undocumented/libshogun/classifier_qda.cpp +++ b/examples/undocumented/libshogun/classifier_qda.cpp @@ -25,30 +25,26 @@ void test() SGVector< float64_t > lab(CLASSES*NUM); SGMatrix< float64_t > feat(DIMS, CLASSES*NUM); - feat = CDataGenerator::generate_gaussians(NUM,CLASSES,DIMS); + feat = DataGenerator::generate_gaussians(NUM,CLASSES,DIMS); for( int i = 0 ; i < CLASSES ; ++i ) for( int j = 0 ; j < NUM ; ++j ) lab[i*NUM+j] = double(i); // Create train labels - CMulticlassLabels* labels = new CMulticlassLabels(lab); + auto labels = std::make_shared(lab); // Create train features - CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feat); + auto features = std::make_shared>(feat); // Create QDA classifier - CQDA* qda = new CQDA(features, labels); - SG_REF(qda); + auto qda = std::make_shared(features, labels); qda->train(); // Classify and display output - CMulticlassLabels* output = qda->apply()->as(); - SG_REF(output); + auto output = qda->apply()->as(); SGVector::display_vector(output->get_labels().vector, output->get_num_labels()); // Free memory - SG_UNREF(output); - SG_UNREF(qda); #endif // HAVE_LAPACK } diff --git a/examples/undocumented/libshogun/classifier_svmlight_string_features_precomputed_kernel.cpp b/examples/undocumented/libshogun/classifier_svmlight_string_features_precomputed_kernel.cpp index f253219b883..8ba6487b6db 100644 --- a/examples/undocumented/libshogun/classifier_svmlight_string_features_precomputed_kernel.cpp +++ b/examples/undocumented/libshogun/classifier_svmlight_string_features_precomputed_kernel.cpp @@ -25,25 +25,25 @@ void test_svmlight() index_t max_length=100; float64_t p_x=0.5; // probability for class A float64_t mostly_prob=0.8; - CDenseLabels* labels=new CBinaryLabels(num_train+num_test); - CMath::init_random(17); + DenseLabels* labels=new BinaryLabels(num_train+num_test); + Math::init_random(17); SGStringList data(num_train+num_test, max_length); for (index_t i=0; i(length); /* fill with elements and set label */ - if (p_xset_label(i, 1); for (index_t j=0; jset_label(i, -1); for (index_t j=0; j*)feats->copy_subset(test_inds); labels->add_subset(train_inds); - CLabels* labels_train=new CBinaryLabels(labels->get_labels_copy()); + Labels* labels_train=new BinaryLabels(labels->get_labels_copy()); labels->remove_subset(); labels->add_subset(test_inds); - CLabels* labels_test=new CBinaryLabels(labels->get_labels_copy()); + Labels* labels_test=new BinaryLabels(labels->get_labels_copy()); labels->remove_subset(); /* string kernel */ @@ -89,21 +89,21 @@ void test_svmlight() /* SVM training and testing without precomputing the kernel */ float64_t C=1; - CSVM* svm=new CSVMLight(C, kernel, labels_train); -// CSVM* svm=new CLibSVM(C, kernel, labels_train); + SVM* svm=new SVMLight(C, kernel, labels_train); svm->get_global_parallel()->set_num_threads(1); + svm->set_store_model_features(false); svm->train(feats_train); SGVector alphas=svm->get_alphas(); SGVector svs=svm->get_support_vectors(); float64_t bias=svm->get_bias(); - CBinaryLabels* predictions=(CBinaryLabels*)svm->apply(feats_test); + BinaryLabels* predictions=(BinaryLabels*)svm->apply(feats_test); alphas.display_vector("alphas"); svs.display_vector("svs"); SG_SPRINT("bias: %f\n", bias); /* now the same with a precopumputed kernel */ kernel->init(feats, feats); - CCustomKernel* precomputed=new CCustomKernel(kernel); + CustomKernel* precomputed=new CustomKernel(kernel); precomputed->add_row_subset(train_inds); precomputed->add_col_subset(train_inds); SGMatrix km_train=precomputed->get_kernel_matrix(); @@ -132,7 +132,7 @@ void test_svmlight() } /* train and test again on custom kernel */ - svm->set_kernel(new CCustomKernel(km_train)); + svm->set_kernel(new CustomKernel(km_train)); svm->train(); SGVector alphas_precomputed=svm->get_alphas(); SGVector svs_precomputed=svm->get_support_vectors(); @@ -140,25 +140,25 @@ void test_svmlight() alphas_precomputed.display_vector("alphas_precomputed"); svs_precomputed.display_vector("svs_precomputed"); SG_SPRINT("bias_precomputed: %f\n", bias_precomputed); - svm->set_kernel(new CCustomKernel(km_test)); - CBinaryLabels* predictions_precomputed=(CBinaryLabels*)svm->apply(); + svm->set_kernel(new CustomKernel(km_test)); + BinaryLabels* predictions_precomputed=(BinaryLabels*)svm->apply(); /* assert that the SV, alphas and b are equal, sort before (they may have * a different ordering */ - CMath::qsort(alphas.vector, alphas.vlen); - CMath::qsort(alphas_precomputed.vector, alphas_precomputed.vlen); - CMath::qsort(svs.vector, svs.vlen); - CMath::qsort(svs_precomputed.vector, svs_precomputed.vlen); + Math::qsort(alphas.vector, alphas.vlen); + Math::qsort(alphas_precomputed.vector, alphas_precomputed.vlen); + Math::qsort(svs.vector, svs.vlen); + Math::qsort(svs_precomputed.vector, svs_precomputed.vlen); ASSERT(alphas.vlen==alphas_precomputed.vlen); ASSERT(svs.vlen==svs_precomputed.vlen); for (index_t i=0; iget_int_labels().display_vector("predictions"); @@ -173,14 +173,6 @@ void test_svmlight() /* clean up */ SG_SPRINT("cleaning up\n"); - SG_UNREF(svm); - SG_UNREF(precomputed); - SG_UNREF(labels); - SG_UNREF(labels_test); - SG_UNREF(predictions); - SG_UNREF(predictions_precomputed); - SG_UNREF(feats_train); - SG_UNREF(feats_test); } int main() diff --git a/examples/undocumented/libshogun/clustering_kmeans.cpp b/examples/undocumented/libshogun/clustering_kmeans.cpp index cf23a928cc0..1f0b4918ebf 100644 --- a/examples/undocumented/libshogun/clustering_kmeans.cpp +++ b/examples/undocumented/libshogun/clustering_kmeans.cpp @@ -47,35 +47,32 @@ int main(int argc, char **argv) idx+=j; idx+=k*dim_features; float64_t entry=cluster_centers.matrix[i*dim_features+j]; - data.matrix[idx]=CMath::normal_random(entry, cluster_std_dev); + data.matrix[idx]=Math::normal_random(entry, cluster_std_dev); } } } - /* create features, SG_REF to avoid deletion */ - CDenseFeatures* features=new CDenseFeatures (data); - SG_REF(features); + auto features=std::make_shared>(data); /* create labels for cluster centers */ - CMulticlassLabels* labels=new CMulticlassLabels(num_features); + auto labels=std::make_shared(num_features); for (index_t i=0; iset_label(i, i%2==0 ? 0 : 1); /* create distance */ - CEuclideanDistance* distance=new CEuclideanDistance(features, features); + auto distance=std::make_shared(features, features); /* create distance machine */ - CKMeans* clustering=new CKMeans(num_clusters, distance); + auto clustering=std::make_shared(num_clusters, distance); clustering->train(features); /* build clusters */ - CMulticlassLabels* result = clustering->apply()->as(); + auto result = clustering->apply()->as(); for (index_t i=0; iget_num_labels(); ++i) SG_SPRINT("cluster index of vector %i: %f\n", i, result->get_label(i)); /* print cluster centers */ - CDenseFeatures* centers= - (CDenseFeatures*)distance->get_lhs(); + auto centers=distance->get_lhs()->as>(); SGMatrix centers_matrix=centers->get_feature_matrix(); @@ -86,11 +83,6 @@ int main(int argc, char **argv) cluster_centers.num_cols, "real centers"); /* clean up */ - SG_UNREF(result); - SG_UNREF(centers); - SG_UNREF(clustering); - SG_UNREF(labels); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/converter_diffusionmaps.cpp b/examples/undocumented/libshogun/converter_diffusionmaps.cpp index b0f3019a3c9..f2406290928 100644 --- a/examples/undocumented/libshogun/converter_diffusionmaps.cpp +++ b/examples/undocumented/libshogun/converter_diffusionmaps.cpp @@ -20,15 +20,11 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); - CDiffusionMaps* dmaps = new CDiffusionMaps(); + auto features = std::make_shared>(SGMatrix(matrix,dim,N)); + auto dmaps = std::make_shared(); dmaps->set_target_dim(2); dmaps->set_t(10); dmaps->get_global_parallel()->set_num_threads(4); auto embedding = dmaps->transform(features); - SG_UNREF(embedding); - SG_UNREF(dmaps); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/converter_factoranalysis.cpp b/examples/undocumented/libshogun/converter_factoranalysis.cpp index 700e5173a8c..48255d8c354 100644 --- a/examples/undocumented/libshogun/converter_factoranalysis.cpp +++ b/examples/undocumented/libshogun/converter_factoranalysis.cpp @@ -20,12 +20,8 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); - CFactorAnalysis* fa = new CFactorAnalysis(); + auto features = std::make_shared>(SGMatrix(matrix,dim,N)); + auto fa = std::make_shared(); auto embedding = fa->transform(features); - SG_UNREF(embedding); - SG_UNREF(fa); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/converter_hessianlocallylinearembedding.cpp b/examples/undocumented/libshogun/converter_hessianlocallylinearembedding.cpp index 9db6e8b2c58..3d1297c6dde 100644 --- a/examples/undocumented/libshogun/converter_hessianlocallylinearembedding.cpp +++ b/examples/undocumented/libshogun/converter_hessianlocallylinearembedding.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CHessianLocallyLinearEmbedding* hlle = new CHessianLocallyLinearEmbedding(); hlle->set_target_dim(2); hlle->set_k(8); hlle->get_global_parallel()->set_num_threads(4); auto embedding = hlle->transform(features); - SG_UNREF(embedding); - SG_UNREF(hlle); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_isomap.cpp b/examples/undocumented/libshogun/converter_isomap.cpp index 4b95dd28ef5..1363a38216c 100644 --- a/examples/undocumented/libshogun/converter_isomap.cpp +++ b/examples/undocumented/libshogun/converter_isomap.cpp @@ -20,17 +20,13 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); - CIsomap* isomap = new CIsomap(); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); + Isomap* isomap = new Isomap(); isomap->set_target_dim(2); isomap->set_landmark(false); isomap->set_k(4); isomap->get_global_parallel()->set_num_threads(4); auto embedding = isomap->transform(features); - SG_UNREF(embedding); - SG_UNREF(isomap); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_jade_bss.cpp b/examples/undocumented/libshogun/converter_jade_bss.cpp index 66e02aa15d7..93fb88b8b61 100644 --- a/examples/undocumented/libshogun/converter_jade_bss.cpp +++ b/examples/undocumented/libshogun/converter_jade_bss.cpp @@ -27,7 +27,7 @@ using namespace Eigen; void test() { // Generate sample data - CMath::init_random(0); + Math::init_random(0); int n_samples = 2000; VectorXd time(n_samples, true); time.setLinSpaced(n_samples,0,10); @@ -38,11 +38,11 @@ void test() { // Sin wave S(0,i) = sin(2*time[i]); - S(0,i) += 0.2*CMath::randn_double(); + S(0,i) += 0.2*Math::randn_double(); // Square wave S(1,i) = sin(3*time[i]) < 0 ? -1 : 1; - S(1,i) += 0.2*CMath::randn_double(); + S(1,i) += 0.2*Math::randn_double(); } // Standardize data @@ -64,14 +64,12 @@ void test() SGMatrix X(2,n_samples); Map EX(X.matrix,2,n_samples); EX = A * S; - CDenseFeatures< float64_t >* mixed_signals = new CDenseFeatures< float64_t >(X); + auto mixed_signals = std::make_shared>(X); // Separate - CJade* jade = new CJade(); - SG_REF(jade); + auto jade = std::make_shared(); - CFeatures* signals = jade->transform(mixed_signals); - SG_REF(signals); + auto signals = jade->transform(mixed_signals); // Close to a permutation matrix (with random scales) Map EA(jade->get_mixing_matrix().matrix,2,2); @@ -89,9 +87,6 @@ void test() float64_t amari_err = amari_index(jade->get_mixing_matrix(), mixing_matrix, true); std::cout << "Amari Error: " << amari_err << std::endl; - SG_UNREF(jade); - SG_UNREF(mixed_signals); - SG_UNREF(signals); return; } diff --git a/examples/undocumented/libshogun/converter_kernellocallylinearembedding.cpp b/examples/undocumented/libshogun/converter_kernellocallylinearembedding.cpp index 2ba4d500920..68da3620af0 100644 --- a/examples/undocumented/libshogun/converter_kernellocallylinearembedding.cpp +++ b/examples/undocumented/libshogun/converter_kernellocallylinearembedding.cpp @@ -21,18 +21,14 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); - CKernelLocallyLinearEmbedding* klle = new CKernelLocallyLinearEmbedding(); - CKernel* kernel = new CLinearKernel(); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); + KernelLocallyLinearEmbedding* klle = new KernelLocallyLinearEmbedding(); + Kernel* kernel = new LinearKernel(); klle->set_target_dim(2); klle->set_k(4); klle->set_kernel(kernel); klle->get_global_parallel()->set_num_threads(4); auto embedding = klle->transform(features); - SG_UNREF(embedding); - SG_UNREF(klle); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_laplacianeigenmaps.cpp b/examples/undocumented/libshogun/converter_laplacianeigenmaps.cpp index 8674d3c19ad..c1715ff6db7 100644 --- a/examples/undocumented/libshogun/converter_laplacianeigenmaps.cpp +++ b/examples/undocumented/libshogun/converter_laplacianeigenmaps.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CLaplacianEigenmaps* lem = new CLaplacianEigenmaps(); lem->set_target_dim(2); lem->set_k(10); lem->get_global_parallel()->set_num_threads(4); auto embedding = lem->transform(features); - SG_UNREF(embedding); - SG_UNREF(lem); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_linearlocaltangentspacealignment.cpp b/examples/undocumented/libshogun/converter_linearlocaltangentspacealignment.cpp index b9956384e26..c09450ad63d 100644 --- a/examples/undocumented/libshogun/converter_linearlocaltangentspacealignment.cpp +++ b/examples/undocumented/libshogun/converter_linearlocaltangentspacealignment.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CLinearLocalTangentSpaceAlignment* lltsa = new CLinearLocalTangentSpaceAlignment(); lltsa->set_target_dim(2); lltsa->set_k(4); lltsa->get_global_parallel()->set_num_threads(4); auto embedding = lltsa->transform(features); - SG_UNREF(embedding); - SG_UNREF(lltsa); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_localitypreservingprojections.cpp b/examples/undocumented/libshogun/converter_localitypreservingprojections.cpp index 7662824b1dd..fef5fa68a25 100644 --- a/examples/undocumented/libshogun/converter_localitypreservingprojections.cpp +++ b/examples/undocumented/libshogun/converter_localitypreservingprojections.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CLocalityPreservingProjections* lpp = new CLocalityPreservingProjections(); lpp->set_target_dim(2); lpp->set_k(10); lpp->get_global_parallel()->set_num_threads(4); auto embedding = lpp->transform(features); - SG_UNREF(embedding); - SG_UNREF(lpp); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_locallylinearembedding.cpp b/examples/undocumented/libshogun/converter_locallylinearembedding.cpp index b287a5b0432..faf674fc59a 100644 --- a/examples/undocumented/libshogun/converter_locallylinearembedding.cpp +++ b/examples/undocumented/libshogun/converter_locallylinearembedding.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CLocallyLinearEmbedding* lle = new CLocallyLinearEmbedding(); lle->set_target_dim(2); lle->set_k(4); lle->get_global_parallel()->set_num_threads(4); auto embedding = lle->transform(features); - SG_UNREF(embedding); - SG_UNREF(lle); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_localtangentspacealignment.cpp b/examples/undocumented/libshogun/converter_localtangentspacealignment.cpp index 35e6dfa70f4..e3877d6a414 100644 --- a/examples/undocumented/libshogun/converter_localtangentspacealignment.cpp +++ b/examples/undocumented/libshogun/converter_localtangentspacealignment.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CLocalTangentSpaceAlignment* ltsa = new CLocalTangentSpaceAlignment(); ltsa->set_target_dim(2); ltsa->set_k(4); ltsa->get_global_parallel()->set_num_threads(4); auto embedding = ltsa->transform(features); - SG_UNREF(embedding); - SG_UNREF(ltsa); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_multidimensionalscaling.cpp b/examples/undocumented/libshogun/converter_multidimensionalscaling.cpp index ccf330a5cd0..28989fe2591 100644 --- a/examples/undocumented/libshogun/converter_multidimensionalscaling.cpp +++ b/examples/undocumented/libshogun/converter_multidimensionalscaling.cpp @@ -20,15 +20,11 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); - CMultidimensionalScaling* mds = new CMultidimensionalScaling(); + auto features = std::make_shared>(SGMatrix(matrix,dim,N)); + auto mds = std::make_shared(); mds->set_target_dim(2); mds->set_landmark(true); mds->get_global_parallel()->set_num_threads(4); auto embedding = mds->transform(features); - SG_UNREF(embedding); - SG_UNREF(mds); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/converter_neighborhoodpreservingembedding.cpp b/examples/undocumented/libshogun/converter_neighborhoodpreservingembedding.cpp index aa510bb9f03..7e84a92ccfe 100644 --- a/examples/undocumented/libshogun/converter_neighborhoodpreservingembedding.cpp +++ b/examples/undocumented/libshogun/converter_neighborhoodpreservingembedding.cpp @@ -20,16 +20,12 @@ int main(int argc, char** argv) for (int i=0; i* features = new CDenseFeatures(SGMatrix(matrix,dim,N)); - SG_REF(features); + DenseFeatures* features = new DenseFeatures(SGMatrix(matrix,dim,N)); CNeighborhoodPreservingEmbedding* npe = new CNeighborhoodPreservingEmbedding(); npe->set_target_dim(2); npe->set_k(15); npe->get_global_parallel()->set_num_threads(4); auto embedding = npe->transform(features); - SG_UNREF(embedding); - SG_UNREF(npe); - SG_UNREF(features); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/converter_stochasticproximityembedding.cpp b/examples/undocumented/libshogun/converter_stochasticproximityembedding.cpp index 2c93207e5a2..47790db94c2 100644 --- a/examples/undocumented/libshogun/converter_stochasticproximityembedding.cpp +++ b/examples/undocumented/libshogun/converter_stochasticproximityembedding.cpp @@ -23,33 +23,25 @@ int main() for (int i=0; i* features = new CDenseFeatures(matrix); - SG_REF(features); + DenseFeatures< float64_t >* features = new DenseFeatures(matrix); // Create embedding and set parameters for global strategy CStochasticProximityEmbedding* spe = new CStochasticProximityEmbedding(); spe->set_target_dim(2); spe->set_strategy(SPE_GLOBAL); spe->set_nupdates(40); - SG_REF(spe); // Apply embedding with global strategy auto embedding = spe->transform(features); - SG_REF(embedding); // Set parameters for local strategy spe->set_strategy(SPE_LOCAL); spe->set_k(12); // Apply embedding with local strategy - SG_UNREF(embedding); embedding = spe->transform(features); - SG_REF(embedding); // Free memory - SG_UNREF(embedding); - SG_UNREF(spe); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_classification.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_classification.cpp index 9c0208ce736..9607e345d9d 100644 --- a/examples/undocumented/libshogun/evaluation_cross_validation_classification.cpp +++ b/examples/undocumented/libshogun/evaluation_cross_validation_classification.cpp @@ -38,50 +38,46 @@ void test_cross_validation() for (index_t j=0; j* features= - new CDenseFeatures(train_dat); - SG_REF(features); + auto features= + std::make_shared>(train_dat); /* training labels +/- 1 for each cluster */ SGVector lab(num_vectors); for (index_t i=0; i(lab); /* gaussian kernel */ int32_t kernel_cache=100; int32_t width=10; - CGaussianKernel* kernel=new CGaussianKernel(kernel_cache, width); + auto kernel=std::make_shared(kernel_cache, width); kernel->init(features, features); /* create svm via libsvm */ float64_t svm_C=10; float64_t svm_eps=0.0001; - CLibSVM* svm=new CLibSVM(svm_C, kernel, labels); + auto svm=std::make_shared(svm_C, kernel, labels); svm->set_epsilon(svm_eps); /* train and output */ svm->train(features); - CBinaryLabels* output = svm->apply(features)->as(); - SG_REF(output); + auto output = svm->apply(features)->as(); for (index_t i=0; iget_label(i)); /* evaluation criterion */ - CContingencyTableEvaluation* eval_crit= - new CContingencyTableEvaluation(ACCURACY); + auto eval_crit= + std::make_shared(ACCURACY); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training error: %f\n", eval_result); - SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going @@ -90,30 +86,23 @@ void test_cross_validation() /* splitting strategy */ index_t n_folds=5; - CStratifiedCrossValidationSplitting* splitting= - new CStratifiedCrossValidationSplitting(labels, n_folds); + auto splitting= + std::make_shared(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ - CCrossValidation* cross=new CCrossValidation(svm, features, labels, + auto cross=std::make_shared(svm, features, labels, splitting, eval_crit); cross->set_num_runs(10); // cross->set_conf_int_alpha(0.05); /* actual evaluation */ - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + auto result=cross->evaluate()->as(); if (result->get_result_type() != CROSSVALIDATION_RESULT) SG_SERROR("Evaluation result is not of type CrossValidationResult!"); result->print_result(); - - /* clean up */ - SG_UNREF(labels); - SG_UNREF(output) - SG_UNREF(result); - SG_UNREF(cross); - SG_UNREF(features); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_knn.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_knn.cpp index 8099ad4898f..69f3c3a7a0d 100644 --- a/examples/undocumented/libshogun/evaluation_cross_validation_knn.cpp +++ b/examples/undocumented/libshogun/evaluation_cross_validation_knn.cpp @@ -27,40 +27,35 @@ void test_cross_validation() { index_t k =4; /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + CSVFile* feature_file = new CSVFile(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + DenseFeatures* features=new DenseFeatures(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + CSVFile* label_file = new CSVFile(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + MulticlassLabels* labels=new MulticlassLabels(label_vec); /* create knn */ - CEuclideanDistance* distance = new CEuclideanDistance(features, features); - CKNN* knn=new CKNN (k, distance, labels); + EuclideanDistance* distance = new EuclideanDistance(features, features); + KNN* knn=new KNN (k, distance, labels); /* train and output */ knn->train(features); - CMulticlassLabels* output = knn->apply(features)->as(); + MulticlassLabels* output = knn->apply(features)->as(); for (index_t i=0; iget_num_vectors(); ++i) SG_SPRINT("i=%d, class=%f,\n", i, output->get_label(i)); /* evaluation criterion */ - CMulticlassAccuracy* eval_crit = new CMulticlassAccuracy (); + MulticlassAccuracy* eval_crit = new MulticlassAccuracy (); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training accuracy: %f\n", eval_result); - SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going @@ -69,29 +64,25 @@ void test_cross_validation() /* splitting strategy */ index_t n_folds=5; - CStratifiedCrossValidationSplitting* splitting= - new CStratifiedCrossValidationSplitting(labels, n_folds); + StratifiedCrossValidationSplitting* splitting= + new StratifiedCrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ - CCrossValidation* cross=new CCrossValidation(knn, features, labels, + CrossValidation* cross=new CrossValidation(knn, features, labels, splitting, eval_crit); cross->set_num_runs(1); // cross->set_conf_int_alpha(0.05); /* actual evaluation */ - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); result->print_result(); /* clean up */ - SG_UNREF(result); - SG_UNREF(cross); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_locked_comparison.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_locked_comparison.cpp new file mode 100644 index 00000000000..42ab91b8c69 --- /dev/null +++ b/examples/undocumented/libshogun/evaluation_cross_validation_locked_comparison.cpp @@ -0,0 +1,156 @@ +/* + * This software is distributed under BSD 3-clause license (see LICENSE file). + * + * Authors: Heiko Strathmann, Soeren Sonnenburg, Evgeniy Andreev, Soumyajit De, + * Jacob Walker, Sergey Lisitsyn + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace shogun; + +void print_message(FILE* target, const char* str) +{ + fprintf(target, "%s", str); +} + +void test_cross_validation() +{ + /* data matrix dimensions */ + index_t num_vectors=50; + index_t num_features=5; + + /* data means -1, 1 in all components, std deviation of sigma */ + SGVector mean_1(num_features); + SGVector mean_2(num_features); + SGVector::fill_vector(mean_1.vector, mean_1.vlen, -1.0); + SGVector::fill_vector(mean_2.vector, mean_2.vlen, 1.0); + float64_t sigma=1.5; + + /* fill data matrix around mean */ + SGMatrix train_dat(num_features, num_vectors); + for (index_t i=0; i>(train_dat); + + /* training labels +/- 1 for each cluster */ + SGVector lab(num_vectors); + for (index_t i=0; i(lab); + + /* gaussian kernel */ + auto kernel=std::make_shared(); + kernel->set_width(10); + kernel->init(features, features); + + /* create svm via libsvm */ + float64_t svm_C=1; + float64_t svm_eps=0.0001; + auto svm=std::make_shared(svm_C, kernel, labels); + svm->set_epsilon(svm_eps); + + /* train and output the normal way */ + SG_SPRINT("starting normal training\n"); + svm->train(features); + auto output = svm->apply(features)->as(); + + /* evaluation criterion */ + auto eval_crit= + std::make_shared(ACCURACY); + + /* evaluate training error */ + float64_t eval_result=eval_crit->evaluate(output, labels); + SG_SPRINT("training accuracy: %f\n", eval_result); + + /* assert that regression "works". this is not guaranteed to always work + * but should be a really coarse check to see if everything is going + * approx. right */ + ASSERT(eval_result<2); + + /* splitting strategy */ + index_t n_folds=3; + auto splitting= + std::make_shared(labels, n_folds); + + /* cross validation instance, 10 runs, 95% confidence interval */ + auto cross=std::make_shared(svm, features, labels, + splitting, eval_crit); + + cross->set_num_runs(5); +// cross->set_conf_int_alpha(0.05); + + std::shared_ptr tmp; + /* no locking */ + index_t repetitions=5; + SG_SPRINT("unlocked x-val\n"); + kernel->init(features, features); + cross->set_autolock(false); + Time time; + time.start(); + for (index_t i=0; ievaluate()->as(); + } + + time.stop(); + SG_SPRINT("%f sec\n", time.cur_time_diff()); + + /* auto_locking in every iteration of this loop (better, not so nice) */ + SG_SPRINT("locked in every iteration x-val\n"); + cross->set_autolock(true); + time.start(); + + for (index_t i=0; ievaluate()->as(); + } + + time.stop(); + SG_SPRINT("%f sec\n", time.cur_time_diff()); + + /* lock once before, (no locking/unlocking in this loop) */ + svm->data_lock(labels, features); + SG_SPRINT("locked x-val\n"); + time.start(); + + for (index_t i=0; ievaluate()->as(); + } + + time.stop(); + SG_SPRINT("%f sec\n", time.cur_time_diff()); + + /* clean up */ +} + +int main(int argc, char **argv) +{ + init_shogun(&print_message, &print_message, &print_message); + + test_cross_validation(); + + exit_shogun(); + + return 0; +} + diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_mkl_weight_storage.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_mkl_weight_storage.cpp index 8b150f21af5..3723147b915 100644 --- a/examples/undocumented/libshogun/evaluation_cross_validation_mkl_weight_storage.cpp +++ b/examples/undocumented/libshogun/evaluation_cross_validation_mkl_weight_storage.cpp @@ -32,14 +32,14 @@ void gen_rand_data(SGVector lab, SGMatrix feat, lab[i]=-1.0; for (int32_t j=0; j lab, SGMatrix feat, } SGMatrix calculate_weights( - CParameterObserverCV& obs, int32_t folds, int32_t run, int32_t len) + ParameterObserverCV& obs, int32_t folds, int32_t run, int32_t len) { int32_t column = 0; SGMatrix weights(len, folds * run); @@ -57,10 +57,9 @@ SGMatrix calculate_weights( for (auto i : range(obs_storage->get("num_folds"))) { auto fold = obs_storage->get("folds", i); - CMKLClassification* machine = - (CMKLClassification*)fold->get("trained_machine"); - SG_REF(machine) - CCombinedKernel* k = (CCombinedKernel*)machine->get_kernel(); + auto machine = + fold->get("trained_machine")->as(); + auto k = machine->get_kernel()->as(); auto w = k->get_subkernel_weights(); /* Copy the weights inside the matrix */ @@ -70,12 +69,8 @@ SGMatrix calculate_weights( weights.set_element(w[j], j, column); } - SG_UNREF(k) - SG_UNREF(machine) - SG_UNREF(fold) column++; } - SG_UNREF(obs_storage) } return weights; } @@ -91,85 +86,74 @@ void test_mkl_cross_validation() gen_rand_data(lab, feat, dist); /*create train labels */ - CLabels* labels=new CBinaryLabels(lab); + auto labels=std::make_shared(lab); /* create train features */ - CDenseFeatures* features=new CDenseFeatures(feat); - SG_REF(features); + auto features=std::make_shared>(feat); /* create combined features */ - CCombinedFeatures* comb_features=new CCombinedFeatures(); + auto comb_features=std::make_shared(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); - SG_REF(comb_features); /* create multiple gaussian kernels */ - CCombinedKernel* kernel=new CCombinedKernel(); - kernel->append_kernel(new CGaussianKernel(10, 0.1)); - kernel->append_kernel(new CGaussianKernel(10, 1)); - kernel->append_kernel(new CGaussianKernel(10, 2)); + auto kernel=std::make_shared(); + kernel->append_kernel(std::make_shared(10, 0.1)); + kernel->append_kernel(std::make_shared(10, 1)); + kernel->append_kernel(std::make_shared(10, 2)); kernel->init(comb_features, comb_features); - SG_REF(kernel); /* create mkl using libsvm, due to a mem-bug, interleaved is not possible */ - CMKLClassification* svm=new CMKLClassification(new CLibSVM()); + auto svm=std::make_shared(std::make_shared()); svm->set_interleaved_optimization_enabled(false); svm->set_kernel(kernel); - SG_REF(svm); /* create cross-validation instance */ index_t num_folds=3; - CSplittingStrategy* split=new CStratifiedCrossValidationSplitting(labels, + auto split=std::make_shared(labels, num_folds); - CEvaluation* eval=new CContingencyTableEvaluation(ACCURACY); - CCrossValidation* cross=new CCrossValidation(svm, comb_features, labels, split, eval, false); + auto eval=std::make_shared(ACCURACY); + auto cross=std::make_shared(svm, comb_features, labels, split, eval, false); /* add print output listener and mkl storage listener */ - CParameterObserverCV mkl_obs{true}; + auto mkl_obs = std::make_shared(true); cross->subscribe(&mkl_obs); /* perform cross-validation, this will print loads of information */ - CEvaluationResult* result=cross->evaluate(); + auto result=cross->evaluate(); /* print mkl weights */ - auto weights = calculate_weights(mkl_obs, num_folds, 1, 3); + auto weights = calculate_weights(*mkl_obs, num_folds, 1, 3); weights.display_matrix("mkl weights"); /* print mean and variance of each kernel weight. These could for example * been used to compute confidence intervals */ - CStatistics::matrix_mean(weights, false).display_vector("mean per kernel"); - CStatistics::matrix_variance(weights, false).display_vector("variance per kernel"); - CStatistics::matrix_std_deviation(weights, false).display_vector("std-dev per kernel"); + Statistics::matrix_mean(weights, false).display_vector("mean per kernel"); + Statistics::matrix_variance(weights, false).display_vector("variance per kernel"); + Statistics::matrix_std_deviation(weights, false).display_vector("std-dev per kernel"); /* Clear */ - mkl_obs.clear(); - SG_UNREF(result); + mkl_obs->clear(); /* again for two runs */ cross->set_num_runs(2); result=cross->evaluate(); /* print mkl weights */ - SGMatrix weights_2 = calculate_weights(mkl_obs, num_folds, 2, 3); + SGMatrix weights_2 = calculate_weights(*mkl_obs, num_folds, 2, 3); weights_2.display_matrix("mkl weights"); /* print mean and variance of each kernel weight. These could for example * been used to compute confidence intervals */ - CStatistics::matrix_mean(weights_2, false) + Statistics::matrix_mean(weights_2, false) .display_vector("mean per kernel"); - CStatistics::matrix_variance(weights_2, false) + Statistics::matrix_variance(weights_2, false) .display_vector("variance per kernel"); - CStatistics::matrix_std_deviation(weights_2, false) + Statistics::matrix_std_deviation(weights_2, false) .display_vector("std-dev per kernel"); /* clean up */ - SG_UNREF(result); - SG_UNREF(cross); - SG_UNREF(kernel); - SG_UNREF(features); - SG_UNREF(comb_features); - SG_UNREF(svm); } int main() diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_multiclass.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_multiclass.cpp index 6233cd60f3d..f71e2435657 100644 --- a/examples/undocumented/libshogun/evaluation_cross_validation_multiclass.cpp +++ b/examples/undocumented/libshogun/evaluation_cross_validation_multiclass.cpp @@ -25,42 +25,37 @@ const char fname_labels[] = "../data/label_train_multiclass.dat"; void test_cross_validation() { /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + auto feature_file = std::make_shared(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + auto features=std::make_shared>(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + auto label_file = std::make_shared(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + auto labels=std::make_shared(label_vec); /* create svm via libsvm */ float64_t svm_C=10; float64_t svm_eps=0.0001; - CMulticlassLibLinear* svm=new CMulticlassLibLinear(svm_C, features, labels); + auto svm=std::make_shared(svm_C, features, labels); svm->set_epsilon(svm_eps); /* train and output */ svm->train(features); - CMulticlassLabels* output = svm->apply(features)->as(); + auto output = svm->apply(features)->as(); for (index_t i=0; iget_num_vectors(); ++i) SG_SPRINT("i=%d, class=%f,\n", i, output->get_label(i)); /* evaluation criterion */ - CMulticlassAccuracy* eval_crit = new CMulticlassAccuracy (); + auto eval_crit = std::make_shared(); /* evaluate training error */ float64_t eval_result=eval_crit->evaluate(output, labels); SG_SPRINT("training accuracy: %f\n", eval_result); - SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going @@ -69,29 +64,25 @@ void test_cross_validation() /* splitting strategy */ index_t n_folds=5; - CStratifiedCrossValidationSplitting* splitting= - new CStratifiedCrossValidationSplitting(labels, n_folds); + auto splitting= + std::make_shared(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ - CCrossValidation* cross=new CCrossValidation(svm, features, labels, + auto cross=std::make_shared(svm, features, labels, splitting, eval_crit); cross->set_num_runs(1); // cross->set_conf_int_alpha(0.05); /* actual evaluation */ - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + auto result=cross->evaluate()->as(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); result->print_result(); /* clean up */ - SG_UNREF(result); - SG_UNREF(cross); - SG_UNREF(features); - SG_UNREF(labels); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_multiclass_mkl.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_multiclass_mkl.cpp index 821fab61562..544c17961d7 100644 --- a/examples/undocumented/libshogun/evaluation_cross_validation_multiclass_mkl.cpp +++ b/examples/undocumented/libshogun/evaluation_cross_validation_multiclass_mkl.cpp @@ -33,46 +33,39 @@ void test_multiclass_mkl_cv() sg_rand->set_seed(12); /* dense features from matrix */ - CCSVFile* feature_file = new CCSVFile(fname_feats); + auto feature_file = std::make_shared(fname_feats); SGMatrix mat=SGMatrix(); mat.load(feature_file); - SG_UNREF(feature_file); - CDenseFeatures* features=new CDenseFeatures(mat); - SG_REF(features); + auto features=std::make_shared>(mat); /* labels from vector */ - CCSVFile* label_file = new CCSVFile(fname_labels); + auto label_file = std::make_shared(fname_labels); SGVector label_vec; label_vec.load(label_file); - SG_UNREF(label_file); - CMulticlassLabels* labels=new CMulticlassLabels(label_vec); - SG_REF(labels); + auto labels=std::make_shared(label_vec); /* combined features and kernel */ - CCombinedFeatures *cfeats=new CCombinedFeatures(); - CCombinedKernel *cker=new CCombinedKernel(); - SG_REF(cfeats); - SG_REF(cker); + auto cfeats=std::make_shared(); + auto cker=std::make_shared(); /** 1st kernel: gaussian */ cfeats->append_feature_obj(features); - cker->append_kernel(new CGaussianKernel(features, features, 1.2, 10)); + cker->append_kernel(std::make_shared(features, features, 1.2, 10)); /** 2nd kernel: linear */ cfeats->append_feature_obj(features); - cker->append_kernel(new CLinearKernel(features, features)); + cker->append_kernel(std::make_shared(features, features)); /** 3rd kernel: poly */ cfeats->append_feature_obj(features); - cker->append_kernel(new CPolyKernel(features, features, 2, 1.0, 1.0, 10)); + cker->append_kernel(std::make_shared(features, features, 2, 1.0, 1.0, 10)); cker->init(cfeats, cfeats); /* create mkl instance */ - CMKLMulticlass* mkl=new CMKLMulticlass(1.2, cker, labels); - SG_REF(mkl); + auto mkl=std::make_shared(1.2, cker, labels); mkl->set_epsilon(0.00001); mkl->get_global_parallel()->set_num_threads(1); mkl->set_mkl_epsilon(0.001); @@ -82,17 +75,17 @@ void test_multiclass_mkl_cv() mkl->train(); cker->get_subkernel_weights().display_vector("weights"); - CMulticlassAccuracy* eval_crit=new CMulticlassAccuracy(); - CStratifiedCrossValidationSplitting* splitting= - new CStratifiedCrossValidationSplitting(labels, n_folds); - CCrossValidation *cross=new CCrossValidation(mkl, cfeats, labels, splitting, + auto eval_crit=std::make_shared(); + auto splitting= + std::make_shared(labels, n_folds); + auto cross=std::make_shared(mkl, cfeats, labels, splitting, eval_crit); cross->set_autolock(false); cross->set_num_runs(n_runs); // cross->set_conf_int_alpha(0.05); /* perform x-val and print result */ - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + auto result=cross->evaluate()->as(); SG_SPRINT( "mean of %d %d-fold x-val runs: %f\n", n_runs, n_folds, result->get_mean()); @@ -101,13 +94,6 @@ void test_multiclass_mkl_cv() ASSERT(result->get_mean() > 0.81); /* clean up */ - SG_UNREF(features); - SG_UNREF(labels); - SG_UNREF(cfeats); - SG_UNREF(cker); - SG_UNREF(mkl); - SG_UNREF(cross); - SG_UNREF(result); } int main(int argc, char** argv){ diff --git a/examples/undocumented/libshogun/evaluation_cross_validation_regression.cpp b/examples/undocumented/libshogun/evaluation_cross_validation_regression.cpp index 8c8ebb4ae72..6f1428a4b31 100644 --- a/examples/undocumented/libshogun/evaluation_cross_validation_regression.cpp +++ b/examples/undocumented/libshogun/evaluation_cross_validation_regression.cpp @@ -31,34 +31,30 @@ void test_cross_validation() for (index_t i=0; i* features= - new CDenseFeatures(train_dat); - SG_REF(features); + auto features=std::make_shared>(train_dat); /* training labels */ - CRegressionLabels* labels=new CRegressionLabels(lab); + auto labels=std::make_shared(lab); /* kernel */ - CLinearKernel* kernel=new CLinearKernel(); + auto kernel=std::make_shared(); kernel->init(features, features); /* kernel ridge regression*/ float64_t tau=0.0001; - CKernelRidgeRegression* krr=new CKernelRidgeRegression(tau, kernel, labels); + auto krr=std::make_shared(tau, kernel, labels); /* evaluation criterion */ - CMeanSquaredError* eval_crit= - new CMeanSquaredError(); + auto eval_crit=std::make_shared(); /* train and output */ krr->train(features); - CRegressionLabels* output = krr->apply()->as(); - SG_REF(output); + auto output = krr->apply()->as(); for (index_t i=0; ievaluate(output, labels); SG_SPRINT("training error: %f\n", eval_result); - SG_UNREF(output); /* assert that regression "works". this is not guaranteed to always work * but should be a really coarse check to see if everything is going @@ -77,21 +72,21 @@ void test_cross_validation() /* splitting strategy */ index_t n_folds=5; - CCrossValidationSplitting* splitting= - new CCrossValidationSplitting(labels, n_folds); + auto splitting= + std::make_shared(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ - CCrossValidation* cross=new CCrossValidation(krr, features, labels, + auto cross=std::make_shared(krr, features, labels, splitting, eval_crit); cross->set_num_runs(100); // cross->set_conf_int_alpha(0.05); /* actual evaluation */ - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + auto result=cross->evaluate()->as(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); SG_SPRINT("cross_validation estimate:\n"); result->print_result(); @@ -99,10 +94,6 @@ void test_cross_validation() /* same crude assertion as for above evaluation */ ASSERT(result->get_mean() < 2); - /* clean up */ - SG_UNREF(result); - SG_UNREF(cross); - SG_UNREF(features); #endif /* HAVE_LAPACK */ } diff --git a/examples/undocumented/libshogun/features_copy_subset_simple_features.cpp b/examples/undocumented/libshogun/features_copy_subset_simple_features.cpp index b1b4bcd8e64..29065242d24 100644 --- a/examples/undocumented/libshogun/features_copy_subset_simple_features.cpp +++ b/examples/undocumented/libshogun/features_copy_subset_simple_features.cpp @@ -13,7 +13,7 @@ void test() { SGMatrix data(3, 10); - CDenseFeatures* f=new CDenseFeatures(data); + auto f=std::make_shared>(data); SGVector::range_fill_vector(data.matrix, data.num_cols*data.num_rows, 1.0); SGMatrix::display_matrix(data.matrix, data.num_rows, data.num_cols, "original feature data"); @@ -42,8 +42,8 @@ void test() SGVector::display_vector(feature_copy_subset.vector, feature_copy_subset.vlen, "indices that are to be copied"); - CDenseFeatures* subset_copy= - (CDenseFeatures*)f->copy_subset(feature_copy_subset); + auto subset_copy= + f->copy_subset(feature_copy_subset)->as>(); SGMatrix subset_copy_matrix=subset_copy->get_feature_matrix(); SGMatrix::display_matrix(subset_copy_matrix.matrix, @@ -57,8 +57,6 @@ void test() ASSERT(subset_copy_matrix.matrix[i]==data.matrix[idx]); } - SG_UNREF(f); - SG_UNREF(subset_copy); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/features_copy_subset_sparse_features.cpp b/examples/undocumented/libshogun/features_copy_subset_sparse_features.cpp index 9ccaee93a66..179c01ead15 100644 --- a/examples/undocumented/libshogun/features_copy_subset_sparse_features.cpp +++ b/examples/undocumented/libshogun/features_copy_subset_sparse_features.cpp @@ -33,7 +33,7 @@ void test() data.sparse_matrix[i].features[j].feat_index=3*j; } } - CSparseFeatures* f=new CSparseFeatures(data); + auto f=std::make_shared>(data); /* display sparse matrix */ SG_SPRINT("original data\n"); @@ -77,8 +77,8 @@ void test() "indices that are to be copied"); /* copy a subset of features */ - CSparseFeatures* subset_copy= - (CSparseFeatures*)f->copy_subset(feature_copy_subset); + auto subset_copy= + f->copy_subset(feature_copy_subset)->as>(); /* print copied subset */ SG_SPRINT("copied features:\n"); @@ -113,8 +113,6 @@ void test() subset_copy->free_sparse_feature_vector(i); } - SG_UNREF(f); - SG_UNREF(subset_copy); } int main(int argc, char **argv) { diff --git a/examples/undocumented/libshogun/features_dense_real_modular.cpp b/examples/undocumented/libshogun/features_dense_real_modular.cpp index 672e2549ae6..99602e7aa80 100644 --- a/examples/undocumented/libshogun/features_dense_real_modular.cpp +++ b/examples/undocumented/libshogun/features_dense_real_modular.cpp @@ -15,11 +15,9 @@ int main(int argc, char** argv) matrix.matrix[i]=i; // shogun will now own the matrix created - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); ASSERT(features->parameter_hash_changed()); - SG_UNREF(features); - return 0; } diff --git a/examples/undocumented/libshogun/features_subset_labels.cpp b/examples/undocumented/libshogun/features_subset_labels.cpp index 1ddd8477b0a..7407dda5492 100644 --- a/examples/undocumented/libshogun/features_subset_labels.cpp +++ b/examples/undocumented/libshogun/features_subset_labels.cpp @@ -14,14 +14,13 @@ const int32_t num_classes=3; void test() { - const int32_t num_subset_idx=CMath::random(1, num_labels); + const int32_t num_subset_idx=Math::random(1, num_labels); /* create labels */ - CMulticlassLabels* labels=new CMulticlassLabels(num_labels); + MulticlassLabels* labels=new MulticlassLabels(num_labels); for (index_t i=0; iset_label(i, i%num_classes); - SG_REF(labels); /* print labels */ SGVector labels_data=labels->get_labels(); @@ -30,7 +29,7 @@ void test() /* create subset indices */ SGVector subset_idx(num_subset_idx); subset_idx.range_fill(); - CMath::permute(subset_idx); + Math::permute(subset_idx); /* print subset indices */ SGVector::display_vector(subset_idx.vector, subset_idx.vlen, "subset indices"); @@ -66,7 +65,6 @@ void test() SG_SPRINT("label %f:\n", label); ASSERT(label==labels_data.vector[i]); } - SG_UNREF(labels); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/features_subset_simple_features.cpp b/examples/undocumented/libshogun/features_subset_simple_features.cpp index e3db6a5c676..5e394ed5c40 100644 --- a/examples/undocumented/libshogun/features_subset_simple_features.cpp +++ b/examples/undocumented/libshogun/features_subset_simple_features.cpp @@ -16,10 +16,10 @@ void print_message(FILE* target, const char* str) fprintf(target, "%s", str); } -void check_transposed(CDenseFeatures* features) +void check_transposed(DenseFeatures* features) { - CDenseFeatures* transposed=features->get_transposed(); - CDenseFeatures* double_transposed=transposed->get_transposed(); + DenseFeatures* transposed=features->get_transposed(); + DenseFeatures* double_transposed=transposed->get_transposed(); for (index_t i=0; iget_num_vectors(); ++i) { @@ -36,8 +36,6 @@ void check_transposed(CDenseFeatures* features) double_transposed->free_feature_vector(new_vec, i); } - SG_UNREF(transposed); - SG_UNREF(double_transposed); } const int32_t num_vectors=6; @@ -45,7 +43,7 @@ const int32_t dim_features=6; void test() { - const int32_t num_subset_idx=CMath::random(1, num_vectors); + const int32_t num_subset_idx=Math::random(1, num_vectors); /* create feature data matrix */ SGMatrix data(dim_features, num_vectors); @@ -54,12 +52,11 @@ void test() for (index_t i=0; i* features=new CDenseFeatures (data); - SG_REF(features); + DenseFeatures* features=new DenseFeatures (data); /* print feature matrix */ SGMatrix::display_matrix(data.matrix, data.num_rows, data.num_cols, @@ -68,7 +65,7 @@ void test() /* create subset indices */ SGVector subset_idx(num_subset_idx); subset_idx.range_fill(); - CMath::permute(subset_idx); + Math::permute(subset_idx); /* print subset indices */ SGVector::display_vector(subset_idx.vector, subset_idx.vlen, "subset indices"); @@ -130,7 +127,6 @@ void test() features->free_feature_vector(vec, i); } - SG_UNREF(features); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/features_subset_stack.cpp b/examples/undocumented/libshogun/features_subset_stack.cpp index e8e5643bb62..d89ab483699 100644 --- a/examples/undocumented/libshogun/features_subset_stack.cpp +++ b/examples/undocumented/libshogun/features_subset_stack.cpp @@ -10,8 +10,7 @@ using namespace shogun; void test() { - CSubsetStack* stack=new CSubsetStack(); - SG_REF(stack); + SubsetStack* stack=new SubsetStack(); /* subset indices, each set is shifted by one */ SGVector subset_a(10); @@ -43,7 +42,6 @@ void test() stack->remove_subset(); /* clean up */ - SG_UNREF(stack); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/hashed_features_example.cpp b/examples/undocumented/libshogun/hashed_features_example.cpp index cad424d650d..27ca5559982 100644 --- a/examples/undocumented/libshogun/hashed_features_example.cpp +++ b/examples/undocumented/libshogun/hashed_features_example.cpp @@ -14,24 +14,20 @@ int main() for (index_t v=0; v* h_dense_feats = new CHashedDenseFeatures(mat, hashing_dim); + HashedDenseFeatures* h_dense_feats = new HashedDenseFeatures(mat, hashing_dim); - CSparseFeatures* sparse_feats = new CSparseFeatures(mat); - CHashedSparseFeatures* h_sparse_feats = new CHashedSparseFeatures(sparse_feats, hashing_dim); + SparseFeatures* sparse_feats = new SparseFeatures(mat); + HashedSparseFeatures* h_sparse_feats = new HashedSparseFeatures(sparse_feats, hashing_dim); - SG_REF(h_dense_feats); CPolyKernel* kernel = new CPolyKernel(h_dense_feats, h_dense_feats, 1); SGMatrix dense_mt = kernel->get_kernel_matrix(); - SG_UNREF(kernel); - SG_REF(h_sparse_feats); kernel = new CPolyKernel(h_sparse_feats, h_sparse_feats, 1); SGMatrix sparse_mt = kernel->get_kernel_matrix(); - SG_UNREF(kernel); for (index_t i=0; i(fname); SGSparseVector* feats; SGVector* labels; @@ -51,7 +51,6 @@ void test_libsvmfile_multilabel(const char* fname) SG_SPRINT("Dimention of the feature: %d\n", dim_feat); SG_SPRINT("Number of classes: %d\n", num_classes); - SG_UNREF(svmfile); SG_FREE(feats); SG_FREE(labels); } diff --git a/examples/undocumented/libshogun/io_linereader.cpp b/examples/undocumented/libshogun/io_linereader.cpp index b1ebdec7d5c..140f16f0dd1 100644 --- a/examples/undocumented/libshogun/io_linereader.cpp +++ b/examples/undocumented/libshogun/io_linereader.cpp @@ -11,11 +11,10 @@ int main(int argc, char** argv) { FILE* fin=fopen("../data/label_train_multiclass_digits.dat", "r"); - CDelimiterTokenizer* tokenizer=new CDelimiterTokenizer(); + auto tokenizer=std::make_shared(); tokenizer->delimiters['\n']=1; - SG_REF(tokenizer); - CLineReader* reader=new CLineReader(fin, tokenizer); + auto reader=std::make_shared(fin, tokenizer); int lines_count=0; SGVector tmp_string; @@ -31,8 +30,6 @@ int main(int argc, char** argv) SG_SPRINT("total lines: %d\n", lines_count); tmp_string=SGVector(); - SG_UNREF(reader); - SG_UNREF(tokenizer); fclose(fin); diff --git a/examples/undocumented/libshogun/kernel_custom.cpp b/examples/undocumented/libshogun/kernel_custom.cpp index efce9d5e2de..91d4a1fa755 100644 --- a/examples/undocumented/libshogun/kernel_custom.cpp +++ b/examples/undocumented/libshogun/kernel_custom.cpp @@ -17,15 +17,14 @@ void test_custom_kernel_subsets() { /* create some data */ index_t m=10; - CFeatures* features= - new CDenseFeatures(CDataGenerator::generate_mean_data( + auto features= + std::make_shared>(DataGenerator::generate_mean_data( m, 2, 1)); - SG_REF(features); /* create a custom kernel */ - CKernel* k=new CGaussianKernel(); + auto k=std::make_shared(); k->init(features, features); - CCustomKernel* l=new CCustomKernel(k); + auto l=std::make_shared(k); /* create a random permutation */ SGVector subset(m); @@ -33,7 +32,7 @@ void test_custom_kernel_subsets() for (index_t run=0; run<100; ++run) { subset.range_fill(); - CMath::permute(subset); + Math::permute(subset); // subset.display_vector("permutation"); features->add_subset(subset); k->init(features, features); @@ -47,7 +46,7 @@ void test_custom_kernel_subsets() { SG_SDEBUG("K(%d,%d)=%f, L(%d,%d)=%f\n", i, j, k->kernel(i, j), i, j, l->kernel(i, j)); - ASSERT(CMath::abs(k->kernel(i, j)-l->kernel(i, j))<10E-8); + ASSERT(Math::abs(k->kernel(i, j)-l->kernel(i, j))<10E-8); } } @@ -56,9 +55,6 @@ void test_custom_kernel_subsets() l->remove_col_subset(); } - SG_UNREF(k); - SG_UNREF(l); - SG_UNREF(features); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/kernel_custom_index.cpp b/examples/undocumented/libshogun/kernel_custom_index.cpp index 23447b9ea21..aa44e5e9108 100644 --- a/examples/undocumented/libshogun/kernel_custom_index.cpp +++ b/examples/undocumented/libshogun/kernel_custom_index.cpp @@ -43,29 +43,26 @@ void test_custom_kernel_index_subsets() index_t num_sub_row=3; index_t num_sub_col=2; - CFeatures* features= - new CDenseFeatures(CDataGenerator::generate_mean_data( + Features* features= + new DenseFeatures(DataGenerator::generate_mean_data( m, 2, 1)); - SG_REF(features); /* create a custom kernel */ - CGaussianKernel* gaussian_kernel=new CGaussianKernel(2,10); + GaussianKernel* gaussian_kernel=new GaussianKernel(2,10); gaussian_kernel->init(features, features); - CCustomKernel* custom_kernel=new CCustomKernel(gaussian_kernel); + CustomKernel* custom_kernel=new CustomKernel(gaussian_kernel); /* create random permutations */ SGVector row_subset(num_sub_row); SGVector col_subset(num_sub_col); row_subset.range_fill(); - CMath::permute(row_subset); + Math::permute(row_subset); col_subset.range_fill(); - CMath::permute(col_subset); + Math::permute(col_subset); /* create index features */ - CIndexFeatures* row_idx_feat=new CIndexFeatures(row_subset); - CIndexFeatures* col_idx_feat=new CIndexFeatures(col_subset); - SG_REF(row_idx_feat); - SG_REF(col_idx_feat); + IndexFeatures* row_idx_feat=new IndexFeatures(row_subset); + IndexFeatures* col_idx_feat=new IndexFeatures(col_subset); custom_kernel->init(row_idx_feat, col_idx_feat); @@ -77,11 +74,6 @@ void test_custom_kernel_index_subsets() custom_kernel_matrix.display_matrix("subset"); - SG_UNREF(gaussian_kernel); - SG_UNREF(custom_kernel); - SG_UNREF(row_idx_feat); - SG_UNREF(col_idx_feat); - SG_UNREF(features); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/kernel_custom_kernel.cpp b/examples/undocumented/libshogun/kernel_custom_kernel.cpp index b4b31857410..4043833adc9 100644 --- a/examples/undocumented/libshogun/kernel_custom_kernel.cpp +++ b/examples/undocumented/libshogun/kernel_custom_kernel.cpp @@ -15,15 +15,14 @@ void test_custom_kernel_subsets() { /* create some data */ index_t m=10; - CFeatures* features= - new CDenseFeatures(CDataGenerator::generate_mean_data( + Features* features= + new DenseFeatures(DataGenerator::generate_mean_data( m, 2, 1)); - SG_REF(features); /* create a custom kernel */ - CKernel* k=new CGaussianKernel(); + Kernel* k=new GaussianKernel(); k->init(features, features); - CCustomKernel* l=new CCustomKernel(k); + CustomKernel* l=new CustomKernel(k); /* create a random permutation */ SGVector subset(m); @@ -31,7 +30,7 @@ void test_custom_kernel_subsets() for (index_t run=0; run<100; ++run) { subset.range_fill(); - CMath::permute(subset); + Math::permute(subset); // subset.display_vector("permutation"); features->add_subset(subset); k->init(features, features); @@ -45,7 +44,7 @@ void test_custom_kernel_subsets() { SG_SDEBUG("K(%d,%d)=%f, L(%d,%d)=%f\n", i, j, k->kernel(i, j), i, j, l->kernel(i, j)); - ASSERT(CMath::abs(k->kernel(i, j)-l->kernel(i, j))<10E-8); + ASSERT(Math::abs(k->kernel(i, j)-l->kernel(i, j))<10E-8); } } @@ -54,9 +53,6 @@ void test_custom_kernel_subsets() l->remove_col_subset(); } - SG_UNREF(k); - SG_UNREF(l); - SG_UNREF(features); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/kernel_gaussian.cpp b/examples/undocumented/libshogun/kernel_gaussian.cpp index 7693862cbc3..1e3f6c322f1 100644 --- a/examples/undocumented/libshogun/kernel_gaussian.cpp +++ b/examples/undocumented/libshogun/kernel_gaussian.cpp @@ -15,10 +15,10 @@ int main(int argc, char** argv) // create three 2-dimensional vectors // shogun will now own the matrix created - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); // create gaussian kernel with cache 10MB, width 0.5 - CGaussianKernel* kernel = new CGaussianKernel(features, features, 0.5, 10); + GaussianKernel* kernel = new GaussianKernel(features, features, 0.5, 10); // print kernel matrix for (int32_t i=0; i<3; i++) @@ -31,7 +31,6 @@ int main(int argc, char** argv) } // free up memory - SG_UNREF(kernel); return 0; } diff --git a/examples/undocumented/libshogun/kernel_machine_train_locked.cpp b/examples/undocumented/libshogun/kernel_machine_train_locked.cpp new file mode 100644 index 00000000000..e3deb367269 --- /dev/null +++ b/examples/undocumented/libshogun/kernel_machine_train_locked.cpp @@ -0,0 +1,140 @@ +/* + * This software is distributed under BSD 3-clause license (see LICENSE file). + * + * Authors: Heiko Strathmann, Soeren Sonnenburg, Evgeniy Andreev, + * Sergey Lisitsyn + */ + +#include +#include +#include +#include +#include +#include + +using namespace shogun; + +void print_message(FILE* target, const char* str) +{ + fprintf(target, "%s", str); +} + +void test() +{ + /* data matrix dimensions */ + index_t num_vectors=6; + index_t num_features=2; + + /* data means -1, 1 in all components, small std deviation */ + SGVector mean_1(num_features); + SGVector mean_2(num_features); + SGVector::fill_vector(mean_1.vector, mean_1.vlen, -10.0); + SGVector::fill_vector(mean_2.vector, mean_2.vlen, 10.0); + float64_t sigma=0.5; + + SGVector::display_vector(mean_1.vector, mean_1.vlen, "mean 1"); + SGVector::display_vector(mean_2.vector, mean_2.vlen, "mean 2"); + + /* fill data matrix around mean */ + SGMatrix train_dat(num_features, num_vectors); + for (index_t i=0; i::display_matrix(train_dat.matrix, train_dat.num_rows, train_dat.num_cols, "training data"); + + /* training features */ + DenseFeatures* features= + new DenseFeatures(train_dat); + + /* training labels +/- 1 for each cluster */ + SGVector lab(num_vectors); + for (index_t i=0; i::display_vector(lab.vector, lab.vlen, "training labels"); + + auto labels=std::make_shared(lab); + + /* evaluation instance */ + auto eval=std::make_shared(ACCURACY); + + /* kernel */ + Kernel* kernel=new LinearKernel(); + kernel->init(features, features); + + /* create svm via libsvm */ + float64_t svm_C=10; + float64_t svm_eps=0.0001; + CLibSVM* svm=new CLibSVM(svm_C, kernel, labels); + svm->set_epsilon(svm_eps); + + /* now train a few times on different subsets on data and assert that + * results are correct (data linear separable) */ + + svm->data_lock(labels, features); + + SGVector indices(5); + indices.vector[0]=1; + indices.vector[1]=2; + indices.vector[2]=3; + indices.vector[3]=4; + indices.vector[4]=5; + SGVector::display_vector(indices.vector, indices.vlen, "training indices"); + svm->train_locked(indices); + BinaryLabels* output = svm->apply()->as(); + SGVector::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output"); + SGVector::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); + SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels)); + ASSERT(eval->evaluate(output, labels)==1); + + SG_SPRINT("\n\n"); + indices=SGVector(3); + indices.vector[0]=1; + indices.vector[1]=2; + indices.vector[2]=3; + SGVector::display_vector(indices.vector, indices.vlen, "training indices"); + output = svm->apply()->as(); + SGVector::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output"); + SGVector::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); + SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels)); + ASSERT(eval->evaluate(output, labels)==1); + + SG_SPRINT("\n\n"); + indices=SGVector(4); + indices.range_fill(); + SGVector::display_vector(indices.vector, indices.vlen, "training indices"); + svm->train_locked(indices); + output = svm->apply()->as(); + SGVector::display_vector(output->get_labels().vector, output->get_num_labels(), "apply() output"); + SGVector::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); + SG_SPRINT("accuracy: %f\n", eval->evaluate(output, labels)); + ASSERT(eval->evaluate(output, labels)==1); + + SG_SPRINT("normal train\n"); + svm->data_unlock(); + svm->train(); + output = svm->apply()->as(); + ASSERT(eval->evaluate(output, labels)==1); + SGVector::display_vector(output->get_labels().vector, output->get_num_labels(), "output"); + SGVector::display_vector(labels->get_labels().vector, labels->get_labels().vlen, "training labels"); + + /* clean up */ +} + +int main(int argc, char **argv) +{ + init_shogun(&print_message, &print_message, &print_message); + + test(); + + exit_shogun(); + + return 0; +} + diff --git a/examples/undocumented/libshogun/kernel_revlin.cpp b/examples/undocumented/libshogun/kernel_revlin.cpp index b45b9eb4789..874b615252a 100644 --- a/examples/undocumented/libshogun/kernel_revlin.cpp +++ b/examples/undocumented/libshogun/kernel_revlin.cpp @@ -6,11 +6,11 @@ using namespace shogun; -class CReverseLinearKernel : public CDotKernel +class CReverseLinearKernel : public DotKernel { public: /** default constructor */ - CReverseLinearKernel() : CDotKernel(0) + CReverseLinearKernel() : DotKernel(0) { } @@ -25,9 +25,9 @@ class CReverseLinearKernel : public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(Features* l, Features* r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } @@ -85,9 +85,9 @@ class CReverseLinearKernel : public CDotKernel bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + ((DenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + ((DenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen); @@ -95,8 +95,8 @@ class CReverseLinearKernel : public CDotKernel for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + ((DenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); + ((DenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); return result; } @@ -111,7 +111,7 @@ int main(int argc, char** argv) // create three 2-dimensional vectors // shogun will now own the matrix created - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); // create reverse linear kernel CReverseLinearKernel* kernel = new CReverseLinearKernel(); @@ -127,7 +127,6 @@ int main(int argc, char** argv) } // free up memory - SG_UNREF(kernel); return 0; } diff --git a/examples/undocumented/libshogun/labels_binary_fit_sigmoid.cpp b/examples/undocumented/libshogun/labels_binary_fit_sigmoid.cpp index 4f15521270d..d7063b23e9d 100644 --- a/examples/undocumented/libshogun/labels_binary_fit_sigmoid.cpp +++ b/examples/undocumented/libshogun/labels_binary_fit_sigmoid.cpp @@ -11,8 +11,8 @@ using namespace shogun; void test_sigmoid_fitting() { - CBinaryLabels* labels=new CBinaryLabels(10); - CBinaryLabels* predictions = new CBinaryLabels(10); + BinaryLabels* labels=new BinaryLabels(10); + BinaryLabels* predictions = new BinaryLabels(10); labels->set_values(SGVector(labels->get_num_labels())); for (index_t i=0; iget_num_labels(); ++i) @@ -21,16 +21,12 @@ void test_sigmoid_fitting() labels->set_value(i % 4 == 0 ? 1 : -1, i); } predictions->get_values().display_vector("scores"); - CSigmoidCalibration* sigmoid_calibration = new CSigmoidCalibration(); + SigmoidCalibration* sigmoid_calibration = new SigmoidCalibration(); sigmoid_calibration->fit_binary(predictions, labels); - CBinaryLabels* calibrated_labels = + BinaryLabels* calibrated_labels = sigmoid_calibration->calibrate_binary(predictions); calibrated_labels->get_values().display_vector("probabilities"); - SG_UNREF(predictions); - SG_UNREF(sigmoid_calibration); - SG_UNREF(calibrated_labels); - SG_UNREF(labels); } int main() diff --git a/examples/undocumented/libshogun/library_circularbuffer.cpp b/examples/undocumented/libshogun/library_circularbuffer.cpp index a271abec2ca..1b8e55c637e 100644 --- a/examples/undocumented/libshogun/library_circularbuffer.cpp +++ b/examples/undocumented/libshogun/library_circularbuffer.cpp @@ -14,11 +14,10 @@ int main(int argc, char** argv) { SGVector test_string(const_cast("all your bayes are belong to us! "), 33, false); - CCircularBuffer* buffer=new CCircularBuffer(max_line_length); + CircularBuffer* buffer=new CircularBuffer(max_line_length); - CDelimiterTokenizer* tokenizer=new CDelimiterTokenizer(); + DelimiterTokenizer* tokenizer=new DelimiterTokenizer(); tokenizer->delimiters[' ']=1; - SG_REF(tokenizer); buffer->set_tokenizer(tokenizer); @@ -37,8 +36,6 @@ int main(int argc, char** argv) SG_SPRINT("\n"); } - SG_UNREF(buffer); - SG_UNREF(tokenizer); return 0; } diff --git a/examples/undocumented/libshogun/library_gc_array.cpp b/examples/undocumented/libshogun/library_gc_array.cpp index 5a0475ddf1d..67059a5a9f6 100644 --- a/examples/undocumented/libshogun/library_gc_array.cpp +++ b/examples/undocumented/libshogun/library_gc_array.cpp @@ -12,21 +12,20 @@ const int l=10; int main(int argc, char** argv) { // we need this scope, because exit_shogun() must not be called - // before the destructor of CGCArray kernels! + // before the destructor of CGCArray kernels! { // create array of kernels - CGCArray kernels(l); + CGCArray kernels(l); // fill array with kernels for (int i=0; i::display_matrix(mat, num_feat, num_vec); SG_FREE(mat); - SG_UNREF(hdf); #endif return 0; diff --git a/examples/undocumented/libshogun/library_map.cpp b/examples/undocumented/libshogun/library_map.cpp index e38f8318bf9..3221d2e1723 100644 --- a/examples/undocumented/libshogun/library_map.cpp +++ b/examples/undocumented/libshogun/library_map.cpp @@ -26,6 +26,5 @@ int main(int argc, char** argv) // i, map->index_of(i), map->get_element(i)); } - SG_UNREF(map); return 0; } diff --git a/examples/undocumented/libshogun/library_mldatahdf5.cpp b/examples/undocumented/libshogun/library_mldatahdf5.cpp index 4b536ed1edf..dd53ef873f1 100644 --- a/examples/undocumented/libshogun/library_mldatahdf5.cpp +++ b/examples/undocumented/libshogun/library_mldatahdf5.cpp @@ -10,14 +10,13 @@ using namespace shogun; int main(int argc, char** argv) { #if defined(HAVE_HDF5) && defined( HAVE_CURL) - CMLDataHDF5File* hdf = NULL; + MLDataHDF5File* hdf = NULL; try { - hdf = new CMLDataHDF5File((char *)"australian", "/data/data"); + hdf = new MLDataHDF5File((char *)"australian", "/data/data"); } catch (ShogunException& e) { - SG_UNREF(hdf); return 0; } float64_t* mat=NULL; @@ -34,7 +33,6 @@ int main(int argc, char** argv) } SG_FREE(mat); - SG_UNREF(hdf); #endif // HAVE_CURL && HAVE_HDF5 return 0; } diff --git a/examples/undocumented/libshogun/library_set.cpp b/examples/undocumented/libshogun/library_set.cpp index e60fdc417a7..61e842099fb 100644 --- a/examples/undocumented/libshogun/library_set.cpp +++ b/examples/undocumented/libshogun/library_set.cpp @@ -25,6 +25,5 @@ int main(int argc, char** argv) //SG_SPRINT("%lg contains in set with index %d\n", v[i], set->index_of(v[i])); } - SG_UNREF(set); return 0; } diff --git a/examples/undocumented/libshogun/mathematics_confidence_intervals.cpp b/examples/undocumented/libshogun/mathematics_confidence_intervals.cpp index 52329f10e78..b85c0ef420e 100644 --- a/examples/undocumented/libshogun/mathematics_confidence_intervals.cpp +++ b/examples/undocumented/libshogun/mathematics_confidence_intervals.cpp @@ -18,7 +18,7 @@ void test() float64_t low, up, mean; float64_t error_prob=0.05; - mean=CStatistics::confidence_intervals_mean(data, error_prob, low, up); + mean=Statistics::confidence_intervals_mean(data, error_prob, low, up); SG_SPRINT("sample mean: %f. True mean lies in [%f,%f] with %f%%\n", mean, low, up, 100*(1-error_prob)); diff --git a/examples/undocumented/libshogun/mathematics_lapack.cpp b/examples/undocumented/libshogun/mathematics_lapack.cpp index 688a7a52ec4..18cbd414ac6 100644 --- a/examples/undocumented/libshogun/mathematics_lapack.cpp +++ b/examples/undocumented/libshogun/mathematics_lapack.cpp @@ -17,7 +17,7 @@ using namespace shogun; bool is_equal(float64_t a, float64_t b, float64_t eps) { - return CMath::abs(a-b)<=eps; + return Math::abs(a-b)<=eps; } void test_ev() diff --git a/examples/undocumented/libshogun/metric_lmnnn.cpp b/examples/undocumented/libshogun/metric_lmnnn.cpp index 31aa6b5f713..cae30d36be5 100644 --- a/examples/undocumented/libshogun/metric_lmnnn.cpp +++ b/examples/undocumented/libshogun/metric_lmnnn.cpp @@ -21,7 +21,7 @@ int main() feat_mat(0,3)=-1; feat_mat(1,3)=1; // wrap feat_mat into Shogun features - CDenseFeatures* features=new CDenseFeatures(feat_mat); + DenseFeatures* features=new DenseFeatures(feat_mat); // create labels SGVector lab_vec(4); @@ -30,11 +30,11 @@ int main() lab_vec[2]=1; lab_vec[3]=1; // two-class data, use MulticlassLabels because LMNN works in general for more than two classes - CMulticlassLabels* labels=new CMulticlassLabels(lab_vec); + MulticlassLabels* labels=new MulticlassLabels(lab_vec); // create LMNN metric machine int32_t k=1; // number of target neighbors per example - CLMNN* lmnn=new CLMNN(features,labels,k); + LMNN* lmnn=new LMNN(features,labels,k); // use the identity matrix as initial transform for LMNN SGMatrix init_transform=SGMatrix::create_identity_matrix(2,1); // set number of maximum iterations and train @@ -42,13 +42,11 @@ int main() // lmnn->io->set_loglevel(MSG_DEBUG); lmnn->train(init_transform); // lmnn->get_linear_transform().display_matrix("linear_transform"); - CLMNNStatistics* statistics=lmnn->get_statistics(); + LMNNStatistics* statistics=lmnn->get_statistics(); /* statistics->obj.display_vector("objective"); statistics->stepsize.display_vector("stepsize"); statistics->num_impostors.display_vector("num_impostors"); */ - SG_UNREF(statistics); - SG_UNREF(lmnn); } diff --git a/examples/undocumented/libshogun/minibatchKMeans.cpp b/examples/undocumented/libshogun/minibatchKMeans.cpp index 70edaf4e032..bfd3849084e 100644 --- a/examples/undocumented/libshogun/minibatchKMeans.cpp +++ b/examples/undocumented/libshogun/minibatchKMeans.cpp @@ -30,43 +30,36 @@ int main(int argc, char **argv) data.display_matrix(data.matrix, 2, 4, "rectangle_coordinates"); - CDenseFeatures* features=new CDenseFeatures (data); - SG_REF(features); + DenseFeatures* features=new DenseFeatures (data); - CEuclideanDistance* distance=new CEuclideanDistance(features, features); - CKMeans* clustering=new CKMeans(2, distance, true); + EuclideanDistance* distance=new EuclideanDistance(features, features); + KMeans* clustering=new KMeans(2, distance, true); clustering->train(features); - CMulticlassLabels* result = clustering->apply()->as(); + MulticlassLabels* result = clustering->apply()->as(); for (index_t i=0; iget_num_labels(); ++i) SG_SPRINT("cluster index of vector %i: %f\n", i, result->get_label(i)); - CDenseFeatures* centers=distance->get_lhs()->as>(); + DenseFeatures* centers=distance->get_lhs()->as>(); SGMatrix centers_matrix=centers->get_feature_matrix(); centers_matrix.display_matrix(centers_matrix.matrix, centers_matrix.num_rows, centers_matrix.num_cols, "learnt centers using Lloyd's KMeans"); - SG_UNREF(centers); - SG_UNREF(result); clustering->set_train_method(KMM_MINI_BATCH); clustering->set_mbKMeans_params(2,10); clustering->train(features); - result = clustering->apply()->as(); + result = clustering->apply()->as(); for (index_t i=0; iget_num_labels(); ++i) SG_SPRINT("cluster index of vector %i: %f\n", i, result->get_label(i)); - centers=(CDenseFeatures*)distance->get_lhs(); + centers=(DenseFeatures*)distance->get_lhs(); centers_matrix=centers->get_feature_matrix(); centers_matrix.display_matrix(centers_matrix.matrix, centers_matrix.num_rows, centers_matrix.num_cols, "learnt centers using mini-batch KMeans"); - SG_UNREF(centers); - SG_UNREF(result); - SG_UNREF(clustering); - SG_UNREF(features); return 0; } diff --git a/examples/undocumented/libshogun/modelselection_apply_parameter_tree.cpp b/examples/undocumented/libshogun/modelselection_apply_parameter_tree.cpp index d197a7e5e8f..293f34f2cd1 100644 --- a/examples/undocumented/libshogun/modelselection_apply_parameter_tree.cpp +++ b/examples/undocumented/libshogun/modelselection_apply_parameter_tree.cpp @@ -15,27 +15,27 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c=new ModelSelectionParameters("C1"); root->append_child(c); c->build_values(1.0, 2.0, R_EXP); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(); + GaussianKernel* gaussian_kernel=new GaussianKernel(); /* print all parameter available for modelselection * Dont worry if yours is not included, simply write to the mailing list */ gaussian_kernel->print_modsel_params(); - CModelSelectionParameters* param_gaussian_kernel= - new CModelSelectionParameters("kernel", gaussian_kernel); + ModelSelectionParameters* param_gaussian_kernel= + new ModelSelectionParameters("kernel", gaussian_kernel); root->append_child(param_gaussian_kernel); - CModelSelectionParameters* param_gaussian_kernel_width= - new CModelSelectionParameters("log_width"); + ModelSelectionParameters* param_gaussian_kernel_width= + new ModelSelectionParameters("log_width"); param_gaussian_kernel_width->build_values( 0.0, 0.5 * std::log(2.0), R_LINEAR); param_gaussian_kernel->append_child(param_gaussian_kernel_width); @@ -43,7 +43,7 @@ CModelSelectionParameters* create_param_tree() return root; } -void apply_parameter_tree(CDynamicObjectArray* combinations) +void apply_parameter_tree(DynamicObjectArray* combinations) { /* create some data */ SGMatrix matrix(2,3); @@ -52,19 +52,16 @@ void apply_parameter_tree(CDynamicObjectArray* combinations) /* create three 2-dimensional vectors * to avoid deleting these, REF now and UNREF when finished */ - CDenseFeatures* features=new CDenseFeatures(matrix); - SG_REF(features); + DenseFeatures* features=new DenseFeatures(matrix); /* create three labels, will be handed to svm and automaticall deleted */ - CBinaryLabels* labels=new CBinaryLabels(3); - SG_REF(labels); + BinaryLabels* labels=new BinaryLabels(3); labels->set_label(0, -1); labels->set_label(1, +1); labels->set_label(2, -1); /* create libsvm with C=10 and train */ CLibSVM* svm=new CLibSVM(); - SG_REF(svm); svm->set_labels(labels); for (index_t i=0; iget_num_elements(); ++i) @@ -75,10 +72,8 @@ void apply_parameter_tree(CDynamicObjectArray* combinations) current_combination->print_tree(); Parameter* current_parameters=svm->m_parameters; current_combination->apply_to_modsel_parameter(current_parameters); - SG_UNREF(current_combination); - /* get kernel to set features, get_kernel SG_REF's the kernel */ - CKernel* kernel=svm->get_kernel(); + Kernel* kernel=svm->get_kernel(); kernel->init(features, features); svm->train(); @@ -87,28 +82,23 @@ void apply_parameter_tree(CDynamicObjectArray* combinations) for (index_t j=0; j<3; j++) SG_SPRINT("output[%d]=%f\n", j, svm->apply_one(j)); - /* unset features and SG_UNREF kernel */ kernel->cleanup(); - SG_UNREF(kernel); SG_SPRINT("----------------\n\n"); } /* free up memory */ - SG_UNREF(features); - SG_UNREF(labels); - SG_UNREF(svm); } int main(int argc, char **argv) { /* create example tree */ - CModelSelectionParameters* tree=create_param_tree(); + ModelSelectionParameters* tree=create_param_tree(); tree->print_tree(); SG_SPRINT("----------------------------------\n"); /* build combinations of parameter trees */ - CDynamicObjectArray* combinations=tree->get_combinations(); + DynamicObjectArray* combinations=tree->get_combinations(); apply_parameter_tree(combinations); @@ -117,15 +107,11 @@ int main(int argc, char **argv) { CParameterCombination* combination=(CParameterCombination*) combinations->get_element(i); - SG_UNREF(combination); } - SG_UNREF(combinations); - /* delete example tree (after processing of combinations because CSGObject - * (namely the kernel) of the tree is SG_UNREF'ed (and not REF'ed anywhere + /* delete example tree (after processing of combinations because SGObject * else) */ - SG_UNREF(tree); return 0; } diff --git a/examples/undocumented/libshogun/modelselection_combined_kernel_sub_parameters.cpp b/examples/undocumented/libshogun/modelselection_combined_kernel_sub_parameters.cpp index 9803844968f..d756a99e43a 100644 --- a/examples/undocumented/libshogun/modelselection_combined_kernel_sub_parameters.cpp +++ b/examples/undocumented/libshogun/modelselection_combined_kernel_sub_parameters.cpp @@ -26,63 +26,56 @@ using namespace shogun; * This can be used for modelselection of subkernel parameters of combined * kernels */ -CModelSelectionParameters* build_combined_kernel_parameter_tree() +ModelSelectionParameters* build_combined_kernel_parameter_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); /* kernel a should be Gaussian with certain parameters * kernel b should be polynomial with certain parameters * This will create a list of combined kernels with all parameter combinations - * All CList instances here do reference counting (also the combine_kernels - * method of CCombinedKernel + * All List instances here do reference counting (also the combine_kernels + * method of CombinedKernel */ - CList* kernels_a=new CList(true); - CList* kernels_b=new CList(true); + List* kernels_a=new List(true); + List* kernels_b=new List(true); int32_t cache_size=10; - kernels_a->append_element(new CGaussianKernel(cache_size, 2)); - kernels_a->append_element(new CGaussianKernel(cache_size, 4)); + kernels_a->append_element(new GaussianKernel(cache_size, 2)); + kernels_a->append_element(new GaussianKernel(cache_size, 4)); kernels_b->append_element(new CPolyKernel(cache_size, 4)); kernels_b->append_element(new CPolyKernel(cache_size, 2)); - CList* kernel_list=new CList(); + List* kernel_list=new List(); kernel_list->append_element(kernels_a); kernel_list->append_element(kernels_b); - CList* combinations=CCombinedKernel::combine_kernels(kernel_list); + List* combinations=CombinedKernel::combine_kernels(kernel_list); /* add all created combined kernels to parameters tree */ /* cast is safe since the above method guarantees the type */ - CCombinedKernel* current=(CCombinedKernel*)(combinations->get_first_element()); + CombinedKernel* current=(CombinedKernel*)(combinations->get_first_element()); SG_SPRINT("combined kernel combinations:\n"); index_t i=0; while (current) { /* print out current kernel's subkernels */ SG_SPRINT("combined kernel %d:\n", i++); - CGaussianKernel* gaussian=(CGaussianKernel*)current->get_kernel(0); + GaussianKernel* gaussian=(GaussianKernel*)current->get_kernel(0); CPolyKernel* poly=(CPolyKernel*)current->get_kernel(1); SG_SPRINT("kernel_a type: %s\n", poly->get_name()); SG_SPRINT("kernel_b type: %s\n", gaussian->get_name()); SG_SPRINT("kernel_a parameter: %d\n", poly->get_degree()); SG_SPRINT("kernel_b parameter: %f\n", gaussian->get_width()); - SG_UNREF(poly); - SG_UNREF(gaussian); - CModelSelectionParameters* param_kernel= - new CModelSelectionParameters("kernel", current); + ModelSelectionParameters* param_kernel= + new ModelSelectionParameters("kernel", current); root->append_child(param_kernel); - SG_UNREF(current); - current=(CCombinedKernel*)(combinations->get_next_element()); + current=(CombinedKernel*)(combinations->get_next_element()); } - SG_UNREF(combinations); - SG_UNREF(kernel_list); - SG_UNREF(kernels_a); - SG_UNREF(kernels_b); return root; } @@ -95,16 +88,16 @@ void modelselection_combined_kernel() /* create some data and labels */ SGMatrix matrix(dim_vectors, num_vectors); - CBinaryLabels* labels=new CBinaryLabels(num_vectors); + BinaryLabels* labels=new BinaryLabels(num_vectors); for (int32_t i=0; i* features=new CDenseFeatures(matrix); + DenseFeatures* features=new DenseFeatures(matrix); /* create combined features */ - CCombinedFeatures* comb_features=new CCombinedFeatures(); + CombinedFeatures* comb_features=new CombinedFeatures(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); @@ -113,27 +106,26 @@ void modelselection_combined_kernel() labels->set_label(i, i%2==0 ? 1 : -1); /* create svm */ - CMKL* classifier=new CMKLClassification(new CLibSVM()); + MKL* classifier=new MKLClassification(new CLibSVM()); classifier->set_interleaved_optimization_enabled(false); /* splitting strategy */ - CStratifiedCrossValidationSplitting* splitting_strategy= - new CStratifiedCrossValidationSplitting(labels, num_subsets); + StratifiedCrossValidationSplitting* splitting_strategy= + new StratifiedCrossValidationSplitting(labels, num_subsets); /* accuracy evaluation */ - CContingencyTableEvaluation* evaluation_criterium= - new CContingencyTableEvaluation(ACCURACY); + ContingencyTableEvaluation* evaluation_criterium= + new ContingencyTableEvaluation(ACCURACY); /* cross validation class for evaluation in model selection */ - CCrossValidation* cross=new CCrossValidation(classifier, comb_features, + CrossValidation* cross=new CrossValidation(classifier, comb_features, labels, splitting_strategy, evaluation_criterium); cross->set_num_runs(1); /* TODO: remove this once locking is fixed for combined kernels */ cross->set_autolock(false); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=build_combined_kernel_parameter_tree(); + ModelSelectionParameters* param_tree=build_combined_kernel_parameter_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -148,30 +140,24 @@ void modelselection_combined_kernel() best_combination->apply_to_machine(classifier); /* print subkernel parameters, I know what the subkernel types are here */ - CCombinedKernel* kernel=(CCombinedKernel*)classifier->get_kernel(); - CGaussianKernel* gaussian=(CGaussianKernel*)kernel->get_kernel(0); + CombinedKernel* kernel=(CombinedKernel*)classifier->get_kernel(); + GaussianKernel* gaussian=(GaussianKernel*)kernel->get_kernel(0); CPolyKernel* poly=(CPolyKernel*)kernel->get_kernel(1); SG_SPRINT("gaussian width: %f\n", gaussian->get_width()); SG_SPRINT("poly degree: %d\n", poly->get_degree()); - SG_UNREF(kernel); - SG_UNREF(gaussian); - SG_UNREF(poly); /* larger number of runs to have tighter confidence intervals */ cross->set_num_runs(10); // cross->set_conf_int_alpha(0.01); - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); SG_SPRINT("result: "); result->print_result(); /* clean up destroy result parameter */ - SG_UNREF(result); - SG_UNREF(best_combination); - SG_UNREF(grid_search); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/modelselection_grid_search_kernel.cpp b/examples/undocumented/libshogun/modelselection_grid_search_kernel.cpp index 4d47b6b544d..37002df62d4 100644 --- a/examples/undocumented/libshogun/modelselection_grid_search_kernel.cpp +++ b/examples/undocumented/libshogun/modelselection_grid_search_kernel.cpp @@ -22,28 +22,28 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c1=new ModelSelectionParameters("C1"); root->append_child(c1); c1->build_values(-1.0, 1.0, R_EXP); - CModelSelectionParameters* c2=new CModelSelectionParameters("C2"); + ModelSelectionParameters* c2=new ModelSelectionParameters("C2"); root->append_child(c2); c2->build_values(-1.0, 1.0, R_EXP); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(); + GaussianKernel* gaussian_kernel=new GaussianKernel(); /* print all parameter available for modelselection * Dont worry if yours is not included, simply write to the mailing list */ gaussian_kernel->print_modsel_params(); - CModelSelectionParameters* param_gaussian_kernel= - new CModelSelectionParameters("kernel", gaussian_kernel); - CModelSelectionParameters* gaussian_kernel_width= - new CModelSelectionParameters("log_width"); + ModelSelectionParameters* param_gaussian_kernel= + new ModelSelectionParameters("kernel", gaussian_kernel); + ModelSelectionParameters* gaussian_kernel_width= + new ModelSelectionParameters("log_width"); gaussian_kernel_width->build_values(-std::log(2.0), 0.0, R_LINEAR, 1.0); param_gaussian_kernel->append_child(gaussian_kernel_width); root->append_child(param_gaussian_kernel); @@ -54,13 +54,13 @@ CModelSelectionParameters* create_param_tree() * Dont worry if yours is not included, simply write to the mailing list */ power_kernel->print_modsel_params(); - CModelSelectionParameters* param_power_kernel= - new CModelSelectionParameters("kernel", power_kernel); + ModelSelectionParameters* param_power_kernel= + new ModelSelectionParameters("kernel", power_kernel); root->append_child(param_power_kernel); - CModelSelectionParameters* param_power_kernel_degree= - new CModelSelectionParameters("degree"); + ModelSelectionParameters* param_power_kernel_degree= + new ModelSelectionParameters("degree"); param_power_kernel_degree->build_values(1.0, 2.0, R_LINEAR); param_power_kernel->append_child(param_power_kernel_degree); @@ -70,13 +70,13 @@ CModelSelectionParameters* create_param_tree() * Dont worry if yours is not included, simply write to the mailing list */ m_metric->print_modsel_params(); - CModelSelectionParameters* param_power_kernel_metric1= - new CModelSelectionParameters("distance", m_metric); + ModelSelectionParameters* param_power_kernel_metric1= + new ModelSelectionParameters("distance", m_metric); param_power_kernel->append_child(param_power_kernel_metric1); - CModelSelectionParameters* param_power_kernel_metric1_k= - new CModelSelectionParameters("k"); + ModelSelectionParameters* param_power_kernel_metric1_k= + new ModelSelectionParameters("k"); param_power_kernel_metric1_k->build_values(1.0, 2.0, R_LINEAR); param_power_kernel_metric1->append_child(param_power_kernel_metric1_k); @@ -91,13 +91,13 @@ int main(int argc, char **argv) /* create some data and labels */ SGMatrix matrix(dim_vectors, num_vectors); - CBinaryLabels* labels=new CBinaryLabels(num_vectors); + BinaryLabels* labels=new BinaryLabels(num_vectors); for (int32_t i=0; i* features=new CDenseFeatures(matrix); + DenseFeatures* features=new DenseFeatures(matrix); /* create labels, two classes */ for (index_t i=0; iset_num_runs(1); /* note that this automatically is not necessary since done automatically */ @@ -125,8 +125,7 @@ int main(int argc, char **argv) * Dont worry if yours is not included, simply write to the mailing list */ classifier->print_modsel_params(); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=create_param_tree(); + ModelSelectionParameters* param_tree=create_param_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -143,31 +142,26 @@ int main(int argc, char **argv) /* larger number of runs to have tighter confidence intervals */ cross->set_num_runs(10); // cross->set_conf_int_alpha(0.01); - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); SG_SPRINT("result: "); result->print_result(); /* now again but unlocked */ - SG_UNREF(best_combination); cross->set_autolock(true); best_combination=grid_search->select_model(print_state); best_combination->apply_to_machine(classifier); - SG_UNREF(result); - result=(CCrossValidationResult*)cross->evaluate(); + result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); SG_SPRINT("result (unlocked): "); /* clean up destroy result parameter */ - SG_UNREF(result); - SG_UNREF(best_combination); - SG_UNREF(grid_search); return 0; } diff --git a/examples/undocumented/libshogun/modelselection_grid_search_krr.cpp b/examples/undocumented/libshogun/modelselection_grid_search_krr.cpp index 83ce57cfe4e..e31b03b6d33 100644 --- a/examples/undocumented/libshogun/modelselection_grid_search_krr.cpp +++ b/examples/undocumented/libshogun/modelselection_grid_search_krr.cpp @@ -19,24 +19,24 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* tau=new CModelSelectionParameters("tau"); + ModelSelectionParameters* tau=new ModelSelectionParameters("tau"); root->append_child(tau); tau->build_values(-1.0, 1.0, R_EXP); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(); + GaussianKernel* gaussian_kernel=new GaussianKernel(); /* print all parameter available for modelselection * Dont worry if yours is not included, simply write to the mailing list */ gaussian_kernel->print_modsel_params(); - CModelSelectionParameters* param_gaussian_kernel= - new CModelSelectionParameters("kernel", gaussian_kernel); - CModelSelectionParameters* gaussian_kernel_width= - new CModelSelectionParameters("width"); + ModelSelectionParameters* param_gaussian_kernel= + new ModelSelectionParameters("kernel", gaussian_kernel); + ModelSelectionParameters* gaussian_kernel_width= + new ModelSelectionParameters("width"); gaussian_kernel_width->build_values(5.0, 8.0, R_EXP, 1.0, 2.0); param_gaussian_kernel->append_child(gaussian_kernel_width); root->append_child(param_gaussian_kernel); @@ -47,13 +47,13 @@ CModelSelectionParameters* create_param_tree() * Dont worry if yours is not included, simply write to the mailing list */ poly_kernel->print_modsel_params(); - CModelSelectionParameters* param_poly_kernel= - new CModelSelectionParameters("kernel", poly_kernel); + ModelSelectionParameters* param_poly_kernel= + new ModelSelectionParameters("kernel", poly_kernel); root->append_child(param_poly_kernel); - CModelSelectionParameters* param_poly_kernel_degree= - new CModelSelectionParameters("degree"); + ModelSelectionParameters* param_poly_kernel_degree= + new ModelSelectionParameters("degree"); param_poly_kernel_degree->build_values(2, 3, R_LINEAR); param_poly_kernel->append_child(param_poly_kernel_degree); @@ -71,36 +71,35 @@ void test_cross_validation() /* fill data matrix and labels */ SGMatrix train_dat(num_features, num_vectors); - CMath::range_fill_vector(train_dat.matrix, num_vectors); + Math::range_fill_vector(train_dat.matrix, num_vectors); for (index_t i=0; i* features= - new CDenseFeatures(train_dat); - SG_REF(features); + DenseFeatures* features= + new DenseFeatures(train_dat); /* training labels */ - CLabels* labels=new CLabels(lab); + Labels* labels=new Labels(lab); /* kernel ridge regression, only set labels for now, rest does not matter */ - CKernelRidgeRegression* krr=new CKernelRidgeRegression(0, NULL, labels); + KernelRidgeRegression* krr=new KernelRidgeRegression(0, NULL, labels); /* evaluation criterion */ - CMeanSquaredError* eval_crit= - new CMeanSquaredError(); + MeanSquaredError* eval_crit= + new MeanSquaredError(); /* splitting strategy */ index_t n_folds=5; - CCrossValidationSplitting* splitting= - new CCrossValidationSplitting(labels, n_folds); + CrossValidationSplitting* splitting= + new CrossValidationSplitting(labels, n_folds); /* cross validation instance, 10 runs, 95% confidence interval */ - CCrossValidation* cross=new CCrossValidation(krr, features, labels, + CrossValidation* cross=new CrossValidation(krr, features, labels, splitting, eval_crit); cross->set_num_runs(3); @@ -110,8 +109,7 @@ void test_cross_validation() * Dont worry if yours is not included, simply write to the mailing list */ krr->print_modsel_params(); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=create_param_tree(); + ModelSelectionParameters* param_tree=create_param_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -130,19 +128,15 @@ void test_cross_validation() /* larger number of runs to have tighter confidence intervals */ cross->set_num_runs(10); // cross->set_conf_int_alpha(0.01); - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); SG_SPRINT("result: "); result->print_result(); /* clean up */ - SG_UNREF(features); - SG_UNREF(best_combination); - SG_UNREF(result); - SG_UNREF(grid_search); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/modelselection_grid_search_linear.cpp b/examples/undocumented/libshogun/modelselection_grid_search_linear.cpp index 65ed2c23753..8ea088a946c 100644 --- a/examples/undocumented/libshogun/modelselection_grid_search_linear.cpp +++ b/examples/undocumented/libshogun/modelselection_grid_search_linear.cpp @@ -18,15 +18,15 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c1=new ModelSelectionParameters("C1"); root->append_child(c1); c1->build_values(-2.0, 2.0, R_EXP); - CModelSelectionParameters* c2=new CModelSelectionParameters("C2"); + ModelSelectionParameters* c2=new ModelSelectionParameters("C2"); root->append_child(c2); c2->build_values(-2.0, 2.0, R_EXP); @@ -45,34 +45,33 @@ int main(int argc, char **argv) matrix.matrix[i]=i; /* create num_feautres 2-dimensional vectors */ - CDenseFeatures* features=new CDenseFeatures(matrix); + DenseFeatures* features=new DenseFeatures(matrix); /* create three labels */ - CBinaryLabels* labels=new CBinaryLabels(num_vectors); + BinaryLabels* labels=new BinaryLabels(num_vectors); for (index_t i=0; iset_label(i, i%2==0 ? 1 : -1); /* create linear classifier (use -s 2 option to avoid warnings) */ - CLibLinear* classifier=new CLibLinear(L2R_L2LOSS_SVC); + LibLinear* classifier=new LibLinear(L2R_L2LOSS_SVC); /* splitting strategy */ - CStratifiedCrossValidationSplitting* splitting_strategy= - new CStratifiedCrossValidationSplitting(labels, num_subsets); + StratifiedCrossValidationSplitting* splitting_strategy= + new StratifiedCrossValidationSplitting(labels, num_subsets); /* accuracy evaluation */ - CContingencyTableEvaluation* evaluation_criterium= - new CContingencyTableEvaluation(ACCURACY); + ContingencyTableEvaluation* evaluation_criterium= + new ContingencyTableEvaluation(ACCURACY); /* cross validation class for evaluation in model selection */ - CCrossValidation* cross=new CCrossValidation(classifier, features, labels, + CrossValidation* cross=new CrossValidation(classifier, features, labels, splitting_strategy, evaluation_criterium); /* print all parameter available for modelselection * Dont worry if yours is not included, simply write to the mailing list */ classifier->print_modsel_params(); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=create_param_tree(); + ModelSelectionParameters* param_tree=create_param_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -87,17 +86,14 @@ int main(int argc, char **argv) best_combination->print_tree(); best_combination->apply_to_machine(classifier); - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); result->print_result(); /* clean up */ - SG_UNREF(result); - SG_UNREF(best_combination); - SG_UNREF(grid_search); #endif // HAVE_LAPACK return 0; diff --git a/examples/undocumented/libshogun/modelselection_grid_search_mkl.cpp b/examples/undocumented/libshogun/modelselection_grid_search_mkl.cpp index a7c7082fce9..538db3f08ce 100644 --- a/examples/undocumented/libshogun/modelselection_grid_search_mkl.cpp +++ b/examples/undocumented/libshogun/modelselection_grid_search_mkl.cpp @@ -20,34 +20,34 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c1=new ModelSelectionParameters("C1"); root->append_child(c1); c1->build_values(-1.0, 1.0, R_EXP); - CModelSelectionParameters* c2=new CModelSelectionParameters("C2"); + ModelSelectionParameters* c2=new ModelSelectionParameters("C2"); root->append_child(c2); c2->build_values(-1.0, 1.0, R_EXP); - CCombinedKernel* kernel1=new CCombinedKernel(); - kernel1->append_kernel(new CGaussianKernel(10, 2)); - kernel1->append_kernel(new CGaussianKernel(10, 3)); - kernel1->append_kernel(new CGaussianKernel(10, 4)); + CombinedKernel* kernel1=new CombinedKernel(); + kernel1->append_kernel(new GaussianKernel(10, 2)); + kernel1->append_kernel(new GaussianKernel(10, 3)); + kernel1->append_kernel(new GaussianKernel(10, 4)); - CModelSelectionParameters* param_kernel1= - new CModelSelectionParameters("kernel", kernel1); + ModelSelectionParameters* param_kernel1= + new ModelSelectionParameters("kernel", kernel1); root->append_child(param_kernel1); - CCombinedKernel* kernel2=new CCombinedKernel(); - kernel2->append_kernel(new CGaussianKernel(10, 20)); - kernel2->append_kernel(new CGaussianKernel(10, 30)); - kernel2->append_kernel(new CGaussianKernel(10, 40)); + CombinedKernel* kernel2=new CombinedKernel(); + kernel2->append_kernel(new GaussianKernel(10, 20)); + kernel2->append_kernel(new GaussianKernel(10, 30)); + kernel2->append_kernel(new GaussianKernel(10, 40)); - CModelSelectionParameters* param_kernel2= - new CModelSelectionParameters("kernel", kernel2); + ModelSelectionParameters* param_kernel2= + new ModelSelectionParameters("kernel", kernel2); root->append_child(param_kernel2); return root; @@ -62,46 +62,44 @@ void test() /* create some data and labels */ SGMatrix matrix(dim_vectors, num_vectors); for (int32_t i=0; i* features=new CDenseFeatures (matrix); + DenseFeatures* features=new DenseFeatures (matrix); /* create combined features */ - CCombinedFeatures* comb_features=new CCombinedFeatures(); + CombinedFeatures* comb_features=new CombinedFeatures(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); - SG_REF(comb_features); /* create labels, two classes */ - CBinaryLabels* labels=new CBinaryLabels(num_vectors); - SG_REF(labels); + BinaryLabels* labels=new BinaryLabels(num_vectors); for (index_t i=0; iset_label(i, i%2==0 ? +1 : -1); /* works */ // /* create svm */ -// CMKLClassification* classifier=new CMKLClassification(new CLibSVM()); +// MKLClassification* classifier=new MKLClassification(new CLibSVM()); // classifier->set_interleaved_optimization_enabled(false); /* create svm */ - CMKLClassification* classifier=new CMKLClassification(); + MKLClassification* classifier=new MKLClassification(); // both fail: //classifier->set_interleaved_optimization_enabled(false); classifier->set_interleaved_optimization_enabled(true); /* splitting strategy */ - CStratifiedCrossValidationSplitting* splitting_strategy= - new CStratifiedCrossValidationSplitting(labels, num_subsets); + StratifiedCrossValidationSplitting* splitting_strategy= + new StratifiedCrossValidationSplitting(labels, num_subsets); /* accuracy evaluation */ - CContingencyTableEvaluation* evaluation_criterion= - new CContingencyTableEvaluation(ACCURACY); + ContingencyTableEvaluation* evaluation_criterion= + new ContingencyTableEvaluation(ACCURACY); /* cross validation class for evaluation in model selection */ - CCrossValidation* cross=new CCrossValidation(classifier, comb_features, + CrossValidation* cross=new CrossValidation(classifier, comb_features, labels, splitting_strategy, evaluation_criterion); cross->set_num_runs(1); @@ -109,8 +107,7 @@ void test() * Dont worry if yours is not included, simply write to the mailing list */ classifier->print_modsel_params(); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=create_param_tree(); + ModelSelectionParameters* param_tree=create_param_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -130,15 +127,11 @@ void test() /* larger number of runs to have tighter confidence intervals */ /*cross->set_num_runs(10); cross->set_conf_int_alpha(0.01); - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); SG_SPRINT("result: %f", result->mean);*/ /* clean up */ - SG_UNREF(comb_features); - SG_UNREF(labels); - //SG_UNREF(best_combination); - SG_UNREF(grid_search); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/modelselection_grid_search_multiclass_svm.cpp b/examples/undocumented/libshogun/modelselection_grid_search_multiclass_svm.cpp index 862082b0ea4..cb2b1096d23 100644 --- a/examples/undocumented/libshogun/modelselection_grid_search_multiclass_svm.cpp +++ b/examples/undocumented/libshogun/modelselection_grid_search_multiclass_svm.cpp @@ -21,16 +21,16 @@ using namespace shogun; -CModelSelectionParameters* build_param_tree(CKernel* kernel) +ModelSelectionParameters* build_param_tree(Kernel* kernel) { - CModelSelectionParameters * root=new CModelSelectionParameters(); - CModelSelectionParameters * c=new CModelSelectionParameters("C"); + ModelSelectionParameters * root=new ModelSelectionParameters(); + ModelSelectionParameters * c=new ModelSelectionParameters("C"); root->append_child(c); c->build_values(-1.0, 1.0, R_EXP); - CModelSelectionParameters * params_kernel=new CModelSelectionParameters("kernel", kernel); + ModelSelectionParameters * params_kernel=new ModelSelectionParameters("kernel", kernel); root->append_child(params_kernel); - CModelSelectionParameters * params_kernel_width=new CModelSelectionParameters("log_width"); + ModelSelectionParameters * params_kernel_width=new ModelSelectionParameters("log_width"); params_kernel_width->build_values(-std::log(2.0), 0.0, R_LINEAR); params_kernel->append_child(params_kernel_width); @@ -56,36 +56,36 @@ void test() lab[j]=j%dim_vectors; for (index_t i=0; i * cfeatures=new CDenseFeatures(feat); - CMulticlassLabels * clabels=new CMulticlassLabels(lab); + DenseFeatures * cfeatures=new DenseFeatures(feat); + MulticlassLabels * clabels=new MulticlassLabels(lab); float64_t sigma=2; - CGaussianKernel* kernel=new CGaussianKernel(10, sigma); + GaussianKernel* kernel=new GaussianKernel(10, sigma); const float C=10.; CMulticlassLibSVM* cmachine=new CMulticlassLibSVM(C, kernel, clabels); - CMulticlassAccuracy * eval_crit=new CMulticlassAccuracy(); + MulticlassAccuracy * eval_crit=new MulticlassAccuracy(); /* k-fold stratified x-validation */ index_t k=3; - CStratifiedCrossValidationSplitting * splitting= - new CStratifiedCrossValidationSplitting(clabels, k); + StratifiedCrossValidationSplitting * splitting= + new StratifiedCrossValidationSplitting(clabels, k); - CCrossValidation * cross=new CCrossValidation(cmachine, cfeatures, clabels, + CrossValidation * cross=new CrossValidation(cmachine, cfeatures, clabels, splitting, eval_crit); cross->set_num_runs(10); // cross->set_conf_int_alpha(0.05); /* create peramters for model selection */ - CModelSelectionParameters* root=build_param_tree(kernel); + ModelSelectionParameters* root=build_param_tree(kernel); CGridSearchModelSelection * model_selection=new CGridSearchModelSelection( cross, root); @@ -95,8 +95,6 @@ void test() params->print_tree(); /* clean up memory */ - SG_UNREF(model_selection); - SG_UNREF(params); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/modelselection_grid_search_string_kernel.cpp b/examples/undocumented/libshogun/modelselection_grid_search_string_kernel.cpp index 184763c7c79..74aa9429f42 100644 --- a/examples/undocumented/libshogun/modelselection_grid_search_string_kernel.cpp +++ b/examples/undocumented/libshogun/modelselection_grid_search_string_kernel.cpp @@ -20,15 +20,15 @@ using namespace shogun; -CModelSelectionParameters* create_param_tree() +ModelSelectionParameters* create_param_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c1=new ModelSelectionParameters("C1"); root->append_child(c1); c1->build_values(1.0, 2.0, R_EXP); - CModelSelectionParameters* c2=new CModelSelectionParameters("C2"); + ModelSelectionParameters* c2=new ModelSelectionParameters("C2"); root->append_child(c2); c2->build_values(1.0, 2.0, R_EXP); @@ -39,17 +39,17 @@ CModelSelectionParameters* create_param_tree() ds_kernel->print_modsel_params(); - CModelSelectionParameters* param_ds_kernel= - new CModelSelectionParameters("kernel", ds_kernel); + ModelSelectionParameters* param_ds_kernel= + new ModelSelectionParameters("kernel", ds_kernel); root->append_child(param_ds_kernel); - CModelSelectionParameters* ds_kernel_delta= - new CModelSelectionParameters("delta"); + ModelSelectionParameters* ds_kernel_delta= + new ModelSelectionParameters("delta"); ds_kernel_delta->build_values(1, 2, R_LINEAR); param_ds_kernel->append_child(ds_kernel_delta); - CModelSelectionParameters* ds_kernel_theta= - new CModelSelectionParameters("theta"); + ModelSelectionParameters* ds_kernel_theta= + new ModelSelectionParameters("theta"); ds_kernel_theta->build_values(1, 2, R_LINEAR); param_ds_kernel->append_child(ds_kernel_theta); @@ -68,14 +68,14 @@ int main(int argc, char **argv) for (index_t i=0; i current(len); SG_SPRINT("string %i: \"", i); /* fill with random uppercase letters (ASCII) */ for (index_t j=0; j* features=new CStringFeatures(strings, ALPHANUM); /* create labels, two classes */ - CBinaryLabels* labels=new CBinaryLabels(num_strings); + BinaryLabels* labels=new BinaryLabels(num_strings); for (index_t i=0; iset_label(i, i%2==0 ? 1 : -1); @@ -100,15 +100,15 @@ int main(int argc, char **argv) CLibSVM* classifier=new CLibSVM(); /* splitting strategy */ - CStratifiedCrossValidationSplitting* splitting_strategy= - new CStratifiedCrossValidationSplitting(labels, num_subsets); + StratifiedCrossValidationSplitting* splitting_strategy= + new StratifiedCrossValidationSplitting(labels, num_subsets); /* accuracy evaluation */ - CContingencyTableEvaluation* evaluation_criterium= - new CContingencyTableEvaluation(ACCURACY); + ContingencyTableEvaluation* evaluation_criterium= + new ContingencyTableEvaluation(ACCURACY); /* cross validation class for evaluation in model selection */ - CCrossValidation* cross=new CCrossValidation(classifier, features, labels, + CrossValidation* cross=new CrossValidation(classifier, features, labels, splitting_strategy, evaluation_criterium); cross->set_num_runs(2); @@ -116,8 +116,7 @@ int main(int argc, char **argv) * Dont worry if yours is not included, simply write to the mailing list */ classifier->print_modsel_params(); - /* model parameter selection, deletion is handled by modsel class (SG_UNREF) */ - CModelSelectionParameters* param_tree=create_param_tree(); + ModelSelectionParameters* param_tree=create_param_tree(); param_tree->print_tree(); /* handles all of the above structures in memory */ @@ -135,18 +134,15 @@ int main(int argc, char **argv) /* larger number of runs to have tighter confidence intervals */ cross->set_num_runs(10); // cross->set_conf_int_alpha(0.01); - CCrossValidationResult* result=(CCrossValidationResult*)cross->evaluate(); + CrossValidationResult* result=(CrossValidationResult*)cross->evaluate(); if (result->get_result_type() != CROSSVALIDATION_RESULT) - SG_SERROR("Evaluation result is not of type CCrossValidationResult!"); + SG_SERROR("Evaluation result is not of type CrossValidationResult!"); SG_SPRINT("result: "); result->print_result(); /* clean up */ - SG_UNREF(result); - SG_UNREF(best_combination); - SG_UNREF(grid_search); return 0; } diff --git a/examples/undocumented/libshogun/modelselection_model_selection_parameters_test.cpp b/examples/undocumented/libshogun/modelselection_model_selection_parameters_test.cpp index fde8250befa..4214b984667 100644 --- a/examples/undocumented/libshogun/modelselection_model_selection_parameters_test.cpp +++ b/examples/undocumented/libshogun/modelselection_model_selection_parameters_test.cpp @@ -14,11 +14,11 @@ using namespace shogun; -CModelSelectionParameters* build_complex_example_tree() +ModelSelectionParameters* build_complex_example_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c=new CModelSelectionParameters("C"); + ModelSelectionParameters* c=new ModelSelectionParameters("C"); root->append_child(c); c->build_values(1.0, 1.0, R_EXP); @@ -28,40 +28,40 @@ CModelSelectionParameters* build_complex_example_tree() * Dont worry if yours is not included, simply write to the mailing list */ power_kernel->print_modsel_params(); - CModelSelectionParameters* param_power_kernel= - new CModelSelectionParameters("kernel", power_kernel); + ModelSelectionParameters* param_power_kernel= + new ModelSelectionParameters("kernel", power_kernel); root->append_child(param_power_kernel); - CModelSelectionParameters* param_power_kernel_degree= - new CModelSelectionParameters("degree"); + ModelSelectionParameters* param_power_kernel_degree= + new ModelSelectionParameters("degree"); param_power_kernel_degree->build_values(1.0, 1.0, R_EXP); param_power_kernel->append_child(param_power_kernel_degree); CMinkowskiMetric* m_metric=new CMinkowskiMetric(10); - CModelSelectionParameters* param_power_kernel_metric1= - new CModelSelectionParameters("distance", m_metric); + ModelSelectionParameters* param_power_kernel_metric1= + new ModelSelectionParameters("distance", m_metric); param_power_kernel->append_child(param_power_kernel_metric1); - CModelSelectionParameters* param_power_kernel_metric1_k= - new CModelSelectionParameters("k"); + ModelSelectionParameters* param_power_kernel_metric1_k= + new ModelSelectionParameters("k"); param_power_kernel_metric1_k->build_values(1.0, 12.0, R_LINEAR); param_power_kernel_metric1->append_child(param_power_kernel_metric1_k); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(); + GaussianKernel* gaussian_kernel=new GaussianKernel(); /* print all parameter available for modelselection * Dont worry if yours is not included, simply write to the mailing list */ gaussian_kernel->print_modsel_params(); - CModelSelectionParameters* param_gaussian_kernel= - new CModelSelectionParameters("kernel", gaussian_kernel); + ModelSelectionParameters* param_gaussian_kernel= + new ModelSelectionParameters("kernel", gaussian_kernel); root->append_child(param_gaussian_kernel); - CModelSelectionParameters* param_gaussian_kernel_width= - new CModelSelectionParameters("log_width"); + ModelSelectionParameters* param_gaussian_kernel_width= + new ModelSelectionParameters("log_width"); param_gaussian_kernel_width->build_values( 0.0, 0.5 * std::log(2.0), R_LINEAR); param_gaussian_kernel->append_child(param_gaussian_kernel_width); @@ -72,64 +72,64 @@ CModelSelectionParameters* build_complex_example_tree() * Dont worry if yours is not included, simply write to the mailing list */ ds_kernel->print_modsel_params(); - CModelSelectionParameters* param_ds_kernel=new CModelSelectionParameters("kernel", + ModelSelectionParameters* param_ds_kernel=new ModelSelectionParameters("kernel", ds_kernel); root->append_child(param_ds_kernel); - CModelSelectionParameters* param_ds_kernel_delta= - new CModelSelectionParameters("delta"); + ModelSelectionParameters* param_ds_kernel_delta= + new ModelSelectionParameters("delta"); param_ds_kernel_delta->build_values(1.0, 2.0, R_EXP); param_ds_kernel->append_child(param_ds_kernel_delta); - CModelSelectionParameters* param_ds_kernel_theta= - new CModelSelectionParameters("theta"); + ModelSelectionParameters* param_ds_kernel_theta= + new ModelSelectionParameters("theta"); param_ds_kernel_theta->build_values(1.0, 2.0, R_EXP); param_ds_kernel->append_child(param_ds_kernel_theta); return root; } -CModelSelectionParameters* build_sgobject_no_childs_tree() +ModelSelectionParameters* build_sgobject_no_childs_tree() { CPowerKernel* power_kernel=new CPowerKernel(); - CModelSelectionParameters* param_power_kernel= - new CModelSelectionParameters("kernel", power_kernel); + ModelSelectionParameters* param_power_kernel= + new ModelSelectionParameters("kernel", power_kernel); return param_power_kernel; } -CModelSelectionParameters* build_leaf_node_tree() +ModelSelectionParameters* build_leaf_node_tree() { - CModelSelectionParameters* c_1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c_1=new ModelSelectionParameters("C1"); c_1->build_values(1.0, 1.0, R_EXP); return c_1; } -CModelSelectionParameters* build_root_no_childs_tree() +ModelSelectionParameters* build_root_no_childs_tree() { - return new CModelSelectionParameters(); + return new ModelSelectionParameters(); } -CModelSelectionParameters* build_root_value_childs_tree() +ModelSelectionParameters* build_root_value_childs_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c_1=new CModelSelectionParameters("C1"); + ModelSelectionParameters* c_1=new ModelSelectionParameters("C1"); root->append_child(c_1); c_1->build_values(1.0, 1.0, R_EXP); - CModelSelectionParameters* c_2=new CModelSelectionParameters("C2"); + ModelSelectionParameters* c_2=new ModelSelectionParameters("C2"); root->append_child(c_2); c_2->build_values(1.0, 1.0, R_EXP); return root; } -CModelSelectionParameters* build_root_sg_object_child_tree() +ModelSelectionParameters* build_root_sg_object_child_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); CPowerKernel* power_kernel=new CPowerKernel(); @@ -137,17 +137,17 @@ CModelSelectionParameters* build_root_sg_object_child_tree() * Dont worry if yours is not included, simply write to the mailing list */ power_kernel->print_modsel_params(); - CModelSelectionParameters* param_power_kernel= - new CModelSelectionParameters("kernel", power_kernel); + ModelSelectionParameters* param_power_kernel= + new ModelSelectionParameters("kernel", power_kernel); root->append_child(param_power_kernel); return root; } -CModelSelectionParameters* build_root_sg_object_child_value_child_tree() +ModelSelectionParameters* build_root_sg_object_child_value_child_tree() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); CPowerKernel* power_kernel=new CPowerKernel(); @@ -155,10 +155,10 @@ CModelSelectionParameters* build_root_sg_object_child_value_child_tree() * Dont worry if yours is not included, simply write to the mailing list */ power_kernel->print_modsel_params(); - CModelSelectionParameters* param_power_kernel= - new CModelSelectionParameters("kernel", power_kernel); + ModelSelectionParameters* param_power_kernel= + new ModelSelectionParameters("kernel", power_kernel); - CModelSelectionParameters* c=new CModelSelectionParameters("C"); + ModelSelectionParameters* c=new ModelSelectionParameters("C"); root->append_child(c); c->build_values(1.0, 1.0, R_EXP); @@ -167,12 +167,12 @@ CModelSelectionParameters* build_root_sg_object_child_value_child_tree() return root; } -void test_get_combinations(CModelSelectionParameters* tree) +void test_get_combinations(ModelSelectionParameters* tree) { tree->print_tree(); /* build combinations of parameter trees */ - CDynamicObjectArray* combinations=tree->get_combinations(); + DynamicObjectArray* combinations=tree->get_combinations(); /* print and directly delete them all */ SG_SPRINT("----------------------------------\n"); @@ -181,50 +181,34 @@ void test_get_combinations(CModelSelectionParameters* tree) CParameterCombination* combination=(CParameterCombination*) combinations->get_element(i); combination->print_tree(); - SG_UNREF(combination); } - SG_UNREF(combinations); } int main(int argc, char **argv) { - CModelSelectionParameters* tree; + ModelSelectionParameters* tree; tree=build_root_no_childs_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); tree=build_leaf_node_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); tree=build_sgobject_no_childs_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); tree=build_root_value_childs_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); tree=build_root_sg_object_child_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); tree=build_root_sg_object_child_value_child_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); tree=build_complex_example_tree(); - SG_REF(tree); test_get_combinations(tree); - SG_UNREF(tree); return 0; } diff --git a/examples/undocumented/libshogun/modelselection_parameter_combination_test.cpp b/examples/undocumented/libshogun/modelselection_parameter_combination_test.cpp index 71d5c301631..1b36ac502cc 100644 --- a/examples/undocumented/libshogun/modelselection_parameter_combination_test.cpp +++ b/examples/undocumented/libshogun/modelselection_parameter_combination_test.cpp @@ -61,11 +61,10 @@ void test_leaf_sets_multiplication() SGVector param_vector(6); SGVector::range_fill_vector(param_vector.vector, param_vector.vlen); - CDynamicObjectArray sets; + DynamicObjectArray sets; CParameterCombination* new_root=new CParameterCombination(); - SG_REF(new_root); - CDynamicObjectArray* current=new CDynamicObjectArray(); + DynamicObjectArray* current=new DynamicObjectArray(); sets.append_element(current); Parameter* p=new Parameter(); p->add(¶m_vector.vector[0], "0"); @@ -78,7 +77,7 @@ void test_leaf_sets_multiplication() current->append_element(pc); /* first case: one element */ - CDynamicObjectArray* result_simple= + DynamicObjectArray* result_simple= CParameterCombination::leaf_sets_multiplication(sets, new_root); SG_SPRINT("one set\n"); @@ -87,13 +86,11 @@ void test_leaf_sets_multiplication() CParameterCombination* tpc=(CParameterCombination*) result_simple->get_element(i); tpc->print_tree(); - SG_UNREF(tpc); } - SG_UNREF(result_simple); /* now more elements are created */ - current=new CDynamicObjectArray(); + current=new DynamicObjectArray(); sets.append_element(current); p=new Parameter(); p->add(¶m_vector.vector[2], "2"); @@ -105,7 +102,7 @@ void test_leaf_sets_multiplication() pc=new CParameterCombination(p); current->append_element(pc); - current=new CDynamicObjectArray(); + current=new DynamicObjectArray(); sets.append_element(current); p=new Parameter(); p->add(¶m_vector.vector[4], "4"); @@ -118,7 +115,7 @@ void test_leaf_sets_multiplication() current->append_element(pc); /* second case: more element */ - CDynamicObjectArray* result_complex= + DynamicObjectArray* result_complex= CParameterCombination::leaf_sets_multiplication(sets, new_root); SG_SPRINT("more sets\n"); @@ -127,11 +124,8 @@ void test_leaf_sets_multiplication() CParameterCombination* tpc=(CParameterCombination*) result_complex->get_element(i); tpc->print_tree(); - SG_UNREF(tpc); } - SG_UNREF(result_complex); - SG_UNREF(new_root); } int main(int argc, char **argv) diff --git a/examples/undocumented/libshogun/modelselection_parameter_tree.cpp b/examples/undocumented/libshogun/modelselection_parameter_tree.cpp index 4a5c1f02112..02a3046863d 100644 --- a/examples/undocumented/libshogun/modelselection_parameter_tree.cpp +++ b/examples/undocumented/libshogun/modelselection_parameter_tree.cpp @@ -23,13 +23,13 @@ using namespace shogun; -void test_tree(CModelSelectionParameters* tree) +void test_tree(ModelSelectionParameters* tree) { SG_SPRINT("\n\ntree to process:\n"); tree->print_tree(); /* build combinations of parameter trees */ - CDynamicObjectArray* combinations=tree->get_combinations(); + DynamicObjectArray* combinations=tree->get_combinations(); /* print and directly delete them all */ SG_SPRINT("----------------------------------\n"); @@ -38,17 +38,15 @@ void test_tree(CModelSelectionParameters* tree) CParameterCombination* combination= (CParameterCombination*)combinations->get_element(i); combination->print_tree(); - SG_UNREF(combination); } - SG_UNREF(combinations); } -CModelSelectionParameters* create_param_tree_1() +ModelSelectionParameters* create_param_tree_1() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c=new CModelSelectionParameters("C"); + ModelSelectionParameters* c=new ModelSelectionParameters("C"); root->append_child(c); c->build_values(1, 2, R_EXP); @@ -58,13 +56,13 @@ CModelSelectionParameters* create_param_tree_1() * Dont worry if yours is not included, simply write to the mailing list */ power_kernel->print_modsel_params(); - CModelSelectionParameters* param_power_kernel=new CModelSelectionParameters( + ModelSelectionParameters* param_power_kernel=new ModelSelectionParameters( "kernel", power_kernel); root->append_child(param_power_kernel); - CModelSelectionParameters* param_power_kernel_degree= - new CModelSelectionParameters("degree"); + ModelSelectionParameters* param_power_kernel_degree= + new ModelSelectionParameters("degree"); param_power_kernel_degree->build_values(1, 2, R_EXP); param_power_kernel->append_child(param_power_kernel_degree); @@ -74,32 +72,32 @@ CModelSelectionParameters* create_param_tree_1() * Dont worry if yours is not included, simply write to the mailing list */ m_metric->print_modsel_params(); - CModelSelectionParameters* param_power_kernel_metrikernel_width_sigma_param= - new CModelSelectionParameters("distance", m_metric); + ModelSelectionParameters* param_power_kernel_metrikernel_width_sigma_param= + new ModelSelectionParameters("distance", m_metric); param_power_kernel->append_child( param_power_kernel_metrikernel_width_sigma_param); - CModelSelectionParameters* param_power_kernel_metrikernel_width_sigma_param_k= - new CModelSelectionParameters("k"); + ModelSelectionParameters* param_power_kernel_metrikernel_width_sigma_param_k= + new ModelSelectionParameters("k"); param_power_kernel_metrikernel_width_sigma_param_k->build_values(1, 2, R_LINEAR); param_power_kernel_metrikernel_width_sigma_param->append_child( param_power_kernel_metrikernel_width_sigma_param_k); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(); + GaussianKernel* gaussian_kernel=new GaussianKernel(); /* print all parameter available for modelselection * Dont worry if yours is not included, simply write to the mailing list */ gaussian_kernel->print_modsel_params(); - CModelSelectionParameters* param_gaussian_kernel= - new CModelSelectionParameters("kernel", gaussian_kernel); + ModelSelectionParameters* param_gaussian_kernel= + new ModelSelectionParameters("kernel", gaussian_kernel); root->append_child(param_gaussian_kernel); - CModelSelectionParameters* param_gaussian_kernel_width= - new CModelSelectionParameters("log_width"); + ModelSelectionParameters* param_gaussian_kernel_width= + new ModelSelectionParameters("log_width"); param_gaussian_kernel_width->build_values(0.0, 0.5 * std::log(2), R_LINEAR); param_gaussian_kernel->append_child(param_gaussian_kernel_width); @@ -109,227 +107,227 @@ CModelSelectionParameters* create_param_tree_1() * Dont worry if yours is not included, simply write to the mailing list */ ds_kernel->print_modsel_params(); - CModelSelectionParameters* param_ds_kernel=new CModelSelectionParameters( + ModelSelectionParameters* param_ds_kernel=new ModelSelectionParameters( "kernel", ds_kernel); root->append_child(param_ds_kernel); - CModelSelectionParameters* param_ds_kernel_delta= - new CModelSelectionParameters("delta"); + ModelSelectionParameters* param_ds_kernel_delta= + new ModelSelectionParameters("delta"); param_ds_kernel_delta->build_values(1, 2, R_EXP); param_ds_kernel->append_child(param_ds_kernel_delta); - CModelSelectionParameters* param_ds_kernel_theta= - new CModelSelectionParameters("theta"); + ModelSelectionParameters* param_ds_kernel_theta= + new ModelSelectionParameters("theta"); param_ds_kernel_theta->build_values(1, 2, R_EXP); param_ds_kernel->append_child(param_ds_kernel_theta); return root; } -CModelSelectionParameters* create_param_tree_2() +ModelSelectionParameters* create_param_tree_2() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); CPowerKernel* power_kernel=new CPowerKernel(); - CModelSelectionParameters* param_power_kernel=new CModelSelectionParameters( + ModelSelectionParameters* param_power_kernel=new ModelSelectionParameters( "kernel", power_kernel); root->append_child(param_power_kernel); CMinkowskiMetric* metric=new CMinkowskiMetric(); - CModelSelectionParameters* param_power_kernel_metric= - new CModelSelectionParameters("distance", metric); + ModelSelectionParameters* param_power_kernel_metric= + new ModelSelectionParameters("distance", metric); param_power_kernel->append_child(param_power_kernel_metric); - CModelSelectionParameters* param_metric_k=new CModelSelectionParameters( + ModelSelectionParameters* param_metric_k=new ModelSelectionParameters( "k"); param_metric_k->build_values(2, 3, R_LINEAR); param_power_kernel_metric->append_child(param_metric_k); CDistantSegmentsKernel* ds_kernel=new CDistantSegmentsKernel(); - CModelSelectionParameters* param_ds_kernel=new CModelSelectionParameters( + ModelSelectionParameters* param_ds_kernel=new ModelSelectionParameters( "kernel", ds_kernel); root->append_child(param_ds_kernel); return root; } -CModelSelectionParameters* create_param_tree_3() +ModelSelectionParameters* create_param_tree_3() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); CPowerKernel* power_kernel=new CPowerKernel(); - CModelSelectionParameters* param_power_kernel=new CModelSelectionParameters( + ModelSelectionParameters* param_power_kernel=new ModelSelectionParameters( "kernel", power_kernel); root->append_child(param_power_kernel); CMinkowskiMetric* metric=new CMinkowskiMetric(); - CModelSelectionParameters* param_power_kernel_metric= - new CModelSelectionParameters("distance", metric); + ModelSelectionParameters* param_power_kernel_metric= + new ModelSelectionParameters("distance", metric); param_power_kernel->append_child(param_power_kernel_metric); - CEuclideanDistance* euclidean=new CEuclideanDistance(); - CModelSelectionParameters* param_power_kernel_distance= - new CModelSelectionParameters("distance", euclidean); + EuclideanDistance* euclidean=new EuclideanDistance(); + ModelSelectionParameters* param_power_kernel_distance= + new ModelSelectionParameters("distance", euclidean); param_power_kernel->append_child(param_power_kernel_distance); CDistantSegmentsKernel* ds_kernel=new CDistantSegmentsKernel(); - CModelSelectionParameters* param_ds_kernel=new CModelSelectionParameters( + ModelSelectionParameters* param_ds_kernel=new ModelSelectionParameters( "kernel", ds_kernel); root->append_child(param_ds_kernel); return root; } -CModelSelectionParameters* create_param_tree_4a() +ModelSelectionParameters* create_param_tree_4a() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CDenseFeatures* features=new CDenseFeatures(); - CRegressionLabels* labels=new CRegressionLabels(); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(10, 2); + DenseFeatures* features=new DenseFeatures(); + RegressionLabels* labels=new RegressionLabels(); + GaussianKernel* gaussian_kernel=new GaussianKernel(10, 2); CPowerKernel* power_kernel=new CPowerKernel(); - CZeroMean* mean=new CZeroMean(); - CGaussianLikelihood* lik=new CGaussianLikelihood(); - CExactInferenceMethod* inf=new CExactInferenceMethod(gaussian_kernel, features, + ZeroMean* mean=new ZeroMean(); + GaussianLikelihood* lik=new GaussianLikelihood(); + ExactInferenceMethod* inf=new ExactInferenceMethod(gaussian_kernel, features, mean, labels, lik); CLibSVM* svm=new CLibSVM(); CPowerKernel* power_kernel_svm=new CPowerKernel(); - CGaussianKernel* gaussian_kernel_svm=new CGaussianKernel(10, 2); + GaussianKernel* gaussian_kernel_svm=new GaussianKernel(10, 2); - CModelSelectionParameters* param_inf=new CModelSelectionParameters( + ModelSelectionParameters* param_inf=new ModelSelectionParameters( "inference_method", inf); root->append_child(param_inf); - CModelSelectionParameters* param_inf_gaussian=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_gaussian=new ModelSelectionParameters( "likelihood_model", lik); param_inf->append_child(param_inf_gaussian); - CModelSelectionParameters* param_inf_kernel_1=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_1=new ModelSelectionParameters( "kernel", gaussian_kernel); param_inf->append_child(param_inf_kernel_1); - CModelSelectionParameters* param_inf_kernel_2=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_2=new ModelSelectionParameters( "kernel", power_kernel); param_inf->append_child(param_inf_kernel_2); - CModelSelectionParameters* param_svm=new CModelSelectionParameters( + ModelSelectionParameters* param_svm=new ModelSelectionParameters( "SVM", svm); root->append_child(param_svm); - CModelSelectionParameters* param_svm_kernel_1=new CModelSelectionParameters( + ModelSelectionParameters* param_svm_kernel_1=new ModelSelectionParameters( "kernel", power_kernel_svm); param_svm->append_child(param_svm_kernel_1); - CModelSelectionParameters* param_svm_kernel_2=new CModelSelectionParameters( + ModelSelectionParameters* param_svm_kernel_2=new ModelSelectionParameters( "kernel", gaussian_kernel_svm); param_svm->append_child(param_svm_kernel_2); return root; } -CModelSelectionParameters* create_param_tree_4b() +ModelSelectionParameters* create_param_tree_4b() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CDenseFeatures* features=new CDenseFeatures(); - CRegressionLabels* labels=new CRegressionLabels(); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(10, 2); + DenseFeatures* features=new DenseFeatures(); + RegressionLabels* labels=new RegressionLabels(); + GaussianKernel* gaussian_kernel=new GaussianKernel(10, 2); CPowerKernel* power_kernel=new CPowerKernel(); - CZeroMean* mean=new CZeroMean(); - CGaussianLikelihood* lik=new CGaussianLikelihood(); - CExactInferenceMethod* inf=new CExactInferenceMethod(gaussian_kernel, features, + ZeroMean* mean=new ZeroMean(); + GaussianLikelihood* lik=new GaussianLikelihood(); + ExactInferenceMethod* inf=new ExactInferenceMethod(gaussian_kernel, features, mean, labels, lik); CLibSVM* svm=new CLibSVM(); CPowerKernel* power_kernel_svm=new CPowerKernel(); - CGaussianKernel* gaussian_kernel_svm=new CGaussianKernel(10, 2); + GaussianKernel* gaussian_kernel_svm=new GaussianKernel(10, 2); - CModelSelectionParameters* param_c=new CModelSelectionParameters("C1"); + ModelSelectionParameters* param_c=new ModelSelectionParameters("C1"); root->append_child(param_c); param_c->build_values(1,2,R_EXP); - CModelSelectionParameters* param_inf=new CModelSelectionParameters( + ModelSelectionParameters* param_inf=new ModelSelectionParameters( "inference_method", inf); root->append_child(param_inf); - CModelSelectionParameters* param_inf_gaussian=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_gaussian=new ModelSelectionParameters( "likelihood_model", lik); param_inf->append_child(param_inf_gaussian); - CModelSelectionParameters* param_inf_kernel_1=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_1=new ModelSelectionParameters( "kernel", gaussian_kernel); param_inf->append_child(param_inf_kernel_1); - CModelSelectionParameters* param_inf_kernel_2=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_2=new ModelSelectionParameters( "kernel", power_kernel); param_inf->append_child(param_inf_kernel_2); - CModelSelectionParameters* param_svm=new CModelSelectionParameters( + ModelSelectionParameters* param_svm=new ModelSelectionParameters( "SVM", svm); root->append_child(param_svm); - CModelSelectionParameters* param_svm_kernel_1=new CModelSelectionParameters( + ModelSelectionParameters* param_svm_kernel_1=new ModelSelectionParameters( "kernel", power_kernel_svm); param_svm->append_child(param_svm_kernel_1); - CModelSelectionParameters* param_svm_kernel_2=new CModelSelectionParameters( + ModelSelectionParameters* param_svm_kernel_2=new ModelSelectionParameters( "kernel", gaussian_kernel_svm); param_svm->append_child(param_svm_kernel_2); return root; } -CModelSelectionParameters* create_param_tree_5() +ModelSelectionParameters* create_param_tree_5() { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CDenseFeatures* features=new CDenseFeatures(); - CRegressionLabels* labels=new CRegressionLabels(); - CGaussianKernel* gaussian_kernel=new CGaussianKernel(10, 2); - CLinearKernel* linear_kernel=new CLinearKernel(); + DenseFeatures* features=new DenseFeatures(); + RegressionLabels* labels=new RegressionLabels(); + GaussianKernel* gaussian_kernel=new GaussianKernel(10, 2); + LinearKernel* linear_kernel=new LinearKernel(); CPowerKernel* power_kernel=new CPowerKernel(); - CZeroMean* mean=new CZeroMean(); - CGaussianLikelihood* lik=new CGaussianLikelihood(); - CExactInferenceMethod* inf=new CExactInferenceMethod(gaussian_kernel, features, + ZeroMean* mean=new ZeroMean(); + GaussianLikelihood* lik=new GaussianLikelihood(); + ExactInferenceMethod* inf=new ExactInferenceMethod(gaussian_kernel, features, mean, labels, lik); - CModelSelectionParameters* param_inf=new CModelSelectionParameters( + ModelSelectionParameters* param_inf=new ModelSelectionParameters( "inference_method", inf); root->append_child(param_inf); - CModelSelectionParameters* param_inf_gaussian=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_gaussian=new ModelSelectionParameters( "likelihood_model", lik); param_inf->append_child(param_inf_gaussian); - CModelSelectionParameters* param_inf_gaussian_sigma= - new CModelSelectionParameters("log_sigma"); + ModelSelectionParameters* param_inf_gaussian_sigma= + new ModelSelectionParameters("log_sigma"); param_inf_gaussian->append_child(param_inf_gaussian_sigma); param_inf_gaussian_sigma->build_values( 2.0 * std::log(2.0), 3.0 * std::log(2.0), R_LINEAR); - CModelSelectionParameters* param_inf_kernel_1=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_1=new ModelSelectionParameters( "kernel", gaussian_kernel); param_inf->append_child(param_inf_kernel_1); - CModelSelectionParameters* param_inf_kernel_width= - new CModelSelectionParameters("log_width"); + ModelSelectionParameters* param_inf_kernel_width= + new ModelSelectionParameters("log_width"); param_inf_kernel_1->append_child(param_inf_kernel_width); param_inf_kernel_width->build_values(0.0, 0.5 * std::log(2.0), R_LINEAR); - CModelSelectionParameters* param_inf_kernel_2=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_2=new ModelSelectionParameters( "kernel", linear_kernel); param_inf->append_child(param_inf_kernel_2); - CModelSelectionParameters* param_inf_kernel_3=new CModelSelectionParameters( + ModelSelectionParameters* param_inf_kernel_3=new ModelSelectionParameters( "kernel", power_kernel); param_inf->append_child(param_inf_kernel_3); @@ -340,37 +338,25 @@ int main(int argc, char **argv) { // env()->io()->set_loglevel(MSG_DEBUG); - CModelSelectionParameters* tree=NULL; + ModelSelectionParameters* tree=NULL; tree=create_param_tree_1(); - SG_REF(tree); test_tree(tree); - SG_UNREF(tree); tree=create_param_tree_2(); - SG_REF(tree); test_tree(tree); - SG_UNREF(tree); tree=create_param_tree_3(); - SG_REF(tree); test_tree(tree); - SG_UNREF(tree); tree=create_param_tree_4a(); - SG_REF(tree); test_tree(tree); - SG_UNREF(tree); tree=create_param_tree_4b(); - SG_REF(tree); test_tree(tree); - SG_UNREF(tree); tree=create_param_tree_5(); - SG_REF(tree); test_tree(tree); - SG_UNREF(tree); return 0; } diff --git a/examples/undocumented/libshogun/neuralnets_basic.cpp b/examples/undocumented/libshogun/neuralnets_basic.cpp index fb66b4d9201..b6ad2c86a1e 100644 --- a/examples/undocumented/libshogun/neuralnets_basic.cpp +++ b/examples/undocumented/libshogun/neuralnets_basic.cpp @@ -45,10 +45,10 @@ using namespace shogun; int main(int, char*[]) { -#ifdef HAVE_LAPACK // for CDataGenerator::generate_gaussian() +#ifdef HAVE_LAPACK // for DataGenerator::generate_gaussian() // initialize the random number generator with a fixed seed, for repeatability - CMath::init_random(10); + Math::init_random(10); // Prepare the training data const int num_classes = 4; @@ -59,7 +59,7 @@ int main(int, char*[]) SGVector Y; try { - X = CDataGenerator::generate_gaussians( + X = DataGenerator::generate_gaussians( num_examples_per_class,num_classes,num_features); Y = SGVector(num_classes*num_examples_per_class); } @@ -74,13 +74,13 @@ int main(int, char*[]) for (int32_t j = 0; j < num_examples_per_class; j++) Y[i*num_examples_per_class + j] = i; - CDenseFeatures* features = new CDenseFeatures(X); - CMulticlassLabels* labels = new CMulticlassLabels(Y); + auto features = std::make_shared>(X); + auto labels = std::make_shared(Y); // Create a small network single hidden layer network - CNeuralLayers* layers = new CNeuralLayers(); + auto layers = std::make_shared(); layers->input(num_features)->rectified_linear(10)->softmax(num_classes); - CNeuralNetwork* network = new CNeuralNetwork(layers->done()); + auto network = std::make_shared(layers->done()); // initialize the network network->quick_connect(); @@ -94,18 +94,13 @@ int main(int, char*[]) network->train(features); // evaluate - CMulticlassLabels* predictions = network->apply_multiclass(features); - CMulticlassAccuracy* evaluator = new CMulticlassAccuracy(); + auto predictions = network->apply_multiclass(features); + auto evaluator = std::make_shared(); float64_t accuracy = evaluator->evaluate(predictions, labels); SG_SINFO("Accuracy = %f %\n", accuracy*100); // Clean up - SG_UNREF(network); - SG_UNREF(layers); - SG_UNREF(features); - SG_UNREF(predictions); - SG_UNREF(evaluator); #endif return 0; diff --git a/examples/undocumented/libshogun/neuralnets_convolutional.cpp b/examples/undocumented/libshogun/neuralnets_convolutional.cpp index cd4d43d1807..28d4862d666 100644 --- a/examples/undocumented/libshogun/neuralnets_convolutional.cpp +++ b/examples/undocumented/libshogun/neuralnets_convolutional.cpp @@ -47,10 +47,10 @@ using namespace shogun; int main(int, char*[]) { -#ifdef HAVE_LAPACK // for CDataGenerator::generate_gaussian() +#ifdef HAVE_LAPACK // for DataGenerator::generate_gaussian() // initialize the random number generator with a fixed seed, for repeatability - CMath::init_random(10); + Math::init_random(10); // Prepare the training data const int width = 4; @@ -64,7 +64,7 @@ int main(int, char*[]) SGVector Y; try { - X = CDataGenerator::generate_gaussians( + X = DataGenerator::generate_gaussians( num_examples_per_class,num_classes,num_features); Y = SGVector(num_classes*num_examples_per_class); @@ -80,28 +80,28 @@ int main(int, char*[]) for (int32_t j = 0; j < num_examples_per_class; j++) Y[i*num_examples_per_class + j] = i; - CDenseFeatures* features = new CDenseFeatures(X); - CMulticlassLabels* labels = new CMulticlassLabels(Y); + auto features = std::make_shared>(X); + auto labels = std::make_shared(Y); // prepare the layers - CDynamicObjectArray* layers = new CDynamicObjectArray(); + auto layers = std::make_shared(); // input layer - layers->append_element(new CNeuralInputLayer(width,height,num_channels)); + layers->append_element(std::make_shared(width,height,num_channels)); // first convolutional layer: 3 feature maps, 3x3 masks, 2x2 max-pooling - layers->append_element(new CNeuralConvolutionalLayer( + layers->append_element(std::make_shared( CMAF_RECTIFIED_LINEAR, 3, 1,1, 2,2)); // second convolutional layer: 5 feature maps, 3x3 masks - layers->append_element(new CNeuralConvolutionalLayer( + layers->append_element(std::make_shared( CMAF_RECTIFIED_LINEAR, 5, 1,1)); // output layer - layers->append_element(new CNeuralSoftmaxLayer(num_classes)); + layers->append_element(std::make_shared(num_classes)); // create and initialize the network - CNeuralNetwork* network = new CNeuralNetwork(layers); + auto network = std::make_shared(layers); network->quick_connect(); network->initialize_neural_network(0.1); @@ -113,17 +113,13 @@ int main(int, char*[]) network->train(features); // evaluate - CMulticlassLabels* predictions = network->apply_multiclass(features); - CMulticlassAccuracy* evaluator = new CMulticlassAccuracy(); + auto predictions = network->apply_multiclass(features); + auto evaluator = std::make_shared(); float64_t accuracy = evaluator->evaluate(predictions, labels); SG_SINFO("Accuracy = %f %\n", accuracy*100); // Clean up - SG_UNREF(network); - SG_UNREF(features); - SG_UNREF(predictions); - SG_UNREF(evaluator); #endif diff --git a/examples/undocumented/libshogun/neuralnets_deep_autoencoder.cpp b/examples/undocumented/libshogun/neuralnets_deep_autoencoder.cpp index d1177689e92..f2d558897a4 100644 --- a/examples/undocumented/libshogun/neuralnets_deep_autoencoder.cpp +++ b/examples/undocumented/libshogun/neuralnets_deep_autoencoder.cpp @@ -42,10 +42,10 @@ using namespace shogun; int main(int, char*[]) { -#ifdef HAVE_LAPACK // for CDataGenerator::generate_gaussian() +#ifdef HAVE_LAPACK // for DataGenerator::generate_gaussian() // initialize the random number generator with a fixed seed, for repeatability - CMath::init_random(10); + Math::init_random(10); // Prepare the training data const int num_features = 20; @@ -55,7 +55,7 @@ int main(int, char*[]) SGMatrix X; try { - X = CDataGenerator::generate_gaussians( + X = DataGenerator::generate_gaussians( num_examples_per_class,num_classes,num_features); } catch (ShogunException e) @@ -65,15 +65,15 @@ int main(int, char*[]) return 0; } - CDenseFeatures* features = new CDenseFeatures(X); + DenseFeatures* features = new DenseFeatures(X); // Create a deep autoencoder - CNeuralLayers* layers = new CNeuralLayers(); + NeuralLayers* layers = new NeuralLayers(); layers ->input(num_features) ->rectified_linear(10)->rectified_linear(5)->rectified_linear(10) ->linear(num_features); - CDeepAutoencoder* ae = new CDeepAutoencoder(layers->done()); + DeepAutoencoder* ae = new DeepAutoencoder(layers->done()); // uncomment this line to enable info logging // ae->io->set_loglevel(MSG_INFO); @@ -86,23 +86,19 @@ int main(int, char*[]) ae->train(features); // reconstruct the data - CDenseFeatures* reconstructions = ae->reconstruct(features); + DenseFeatures* reconstructions = ae->reconstruct(features); SGMatrix X_reconstructed = reconstructions->get_feature_matrix(); // find the average difference between the data and the reconstructions float64_t avg_diff = 0; int32_t N = X.num_rows*X.num_cols; for (int32_t i=0; i* features = new CDenseFeatures(X); + DenseFeatures* features = new DenseFeatures(X); // Create a DBN - CDeepBeliefNetwork* dbn = new CDeepBeliefNetwork(num_features, RBMVUT_GAUSSIAN); + DeepBeliefNetwork* dbn = new DeepBeliefNetwork(num_features, RBMVUT_GAUSSIAN); dbn->add_hidden_layer(10); dbn->add_hidden_layer(10); dbn->add_hidden_layer(20); @@ -96,24 +96,21 @@ int main(int, char*[]) dbn->train(features); // draw 1000 samples from the DBN - CDenseFeatures* samples = dbn->sample(100,1000); + DenseFeatures* samples = dbn->sample(100,1000); SGMatrix samples_matrix = samples->get_feature_matrix(); // compute the sample means - SGVector samples_means = CStatistics::matrix_mean(samples_matrix, false); + SGVector samples_means = Statistics::matrix_mean(samples_matrix, false); // compute the average difference between the sample means and the true means float64_t avg_diff = 0; for (int32_t i=0; i matrix(n,n); for(int32_t i=0; i::display_matrix(matrix.matrix, n, n); /* create n n-dimensional feature vectors */ - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); /* create gaussian kernel with cache 10MB, width will be changed later */ - CGaussianKernel* kernel = new CGaussianKernel(10, 2.0); + GaussianKernel* kernel = new GaussianKernel(10, 2.0); kernel->init(features, features); /* create n labels (+1,-1,+1,-1,...) */ - CBinaryLabels* labels=new CBinaryLabels(n); + BinaryLabels* labels=new BinaryLabels(n); for (int32_t i=0; iset_label(i, i%2==0 ? +1 : -1); @@ -46,7 +46,7 @@ int main(int argc, char** argv) for (int32_t k=0; k<10; ++k) { - float64_t width=CMath::pow(2.0,k); + float64_t width=Math::pow(2.0,k); float64_t log_width = std::log(width / 2.0) / 2.0; /* create parameter to change current kernel width */ @@ -76,7 +76,6 @@ int main(int argc, char** argv) } /* free up memory */ - SG_UNREF(svm); return 0; diff --git a/examples/undocumented/libshogun/parameter_iterate_sgobject.cpp b/examples/undocumented/libshogun/parameter_iterate_sgobject.cpp index 746f3ed6d06..9893b20130f 100644 --- a/examples/undocumented/libshogun/parameter_iterate_sgobject.cpp +++ b/examples/undocumented/libshogun/parameter_iterate_sgobject.cpp @@ -22,18 +22,18 @@ int main(int argc, char** argv) /* create some random data and hand it to each kernel */ SGMatrix matrix(n,n); for (int32_t k=0; k::display_matrix(matrix.matrix, n, n); - CDenseFeatures* features=new CDenseFeatures(matrix); + DenseFeatures* features=new DenseFeatures(matrix); /* create n kernels with n features each */ - CGaussianKernel** kernels=SG_MALLOC(CGaussianKernel*, n); + GaussianKernel** kernels=SG_MALLOC(GaussianKernel*, n); for (int32_t i=0; iinit(features, features); @@ -44,11 +44,11 @@ int main(int argc, char** argv) for (int32_t i=0; iadd((CSGObject**)&kernels[i], "kernel", ""); + parameters[i]->add((SGObject**)&kernels[i], "kernel", ""); } /* create n labels (+1,-1,+1,-1,...) */ - CBinaryLabels* labels=new CBinaryLabels(n); + BinaryLabels* labels=new BinaryLabels(n); for (int32_t i=0; iset_label(i, i%2==0 ? +1 : -1); @@ -79,6 +79,5 @@ int main(int argc, char** argv) SG_FREE(parameters); /* this also handles features, labels, and last kernel in kernels[n-1] */ - SG_UNREF(svm); return 0; } diff --git a/examples/undocumented/libshogun/parameter_modsel_parameters.cpp b/examples/undocumented/libshogun/parameter_modsel_parameters.cpp index 6cdf98b30ee..f3d35796cba 100644 --- a/examples/undocumented/libshogun/parameter_modsel_parameters.cpp +++ b/examples/undocumented/libshogun/parameter_modsel_parameters.cpp @@ -15,7 +15,8 @@ using namespace shogun; -void print_modsel_parameters(CSGObject* object) + +void print_modsel_parameters(SGObject* object) { SGStringList modsel_params=object->get_modelsel_names(); @@ -42,31 +43,25 @@ void print_modsel_parameters(CSGObject* object) int main(int argc, char** argv) { #ifndef HAVE_LAPACK - CSGObject* object; + SGObject* object; object=new CLibSVM(); print_modsel_parameters(object); - SG_UNREF(object); - object=new CLibLinear(); + object=new LibLinear(); print_modsel_parameters(object); - SG_UNREF(object); object=new CDistantSegmentsKernel(); print_modsel_parameters(object); - SG_UNREF(object); - object=new CGaussianKernel(); + object=new GaussianKernel(); print_modsel_parameters(object); - SG_UNREF(object); object=new CPowerKernel(); print_modsel_parameters(object); - SG_UNREF(object); object=new CMinkowskiMetric(); print_modsel_parameters(object); - SG_UNREF(object); #endif // HAVE_LAPACK return 0; diff --git a/examples/undocumented/libshogun/preprocessor_fisherlda.cpp b/examples/undocumented/libshogun/preprocessor_fisherlda.cpp index 09bd3a1e787..0cc217052a6 100644 --- a/examples/undocumented/libshogun/preprocessor_fisherlda.cpp +++ b/examples/undocumented/libshogun/preprocessor_fisherlda.cpp @@ -22,32 +22,26 @@ void test() SGVector lab(CLASSES*NUM); SGMatrix feat(DIMS, CLASSES*NUM); - feat=CDataGenerator::generate_gaussians(NUM,CLASSES,DIMS); + feat=DataGenerator::generate_gaussians(NUM,CLASSES,DIMS); for(int i=0; i* features=new CDenseFeatures(feat); - SG_REF(features) + DenseFeatures* features=new DenseFeatures(feat); // Initiate the FisherLDA class CFisherLDA* fisherlda=new CFisherLDA(AUTO_FLDA); - SG_REF(fisherlda) fisherlda->fit(features, labels, 1); SGMatrix y = fisherlda->transform(features) - ->as>() + ->as>() ->get_feature_matrix(); // display output y.display_matrix(); - SG_UNREF(fisherlda) - SG_UNREF(features) - SG_UNREF(labels) } int main(int argc, char ** argv) diff --git a/examples/undocumented/libshogun/preprocessor_randomfouriergauss.cpp b/examples/undocumented/libshogun/preprocessor_randomfouriergauss.cpp index e30f679a474..7914075125f 100644 --- a/examples/undocumented/libshogun/preprocessor_randomfouriergauss.cpp +++ b/examples/undocumented/libshogun/preprocessor_randomfouriergauss.cpp @@ -35,18 +35,18 @@ void gen_rand_data(float64_t* & feat, float64_t* & lab,const int32_t num,const i lab[i]=-1.0; for (int32_t j=0; jset_labels(labtr, numtr); - SG_REF(labelstr); // create train features a=time(NULL); std::cout << "initializing shogun train feature"<* featurestr1 = new CDenseFeatures(feature_cache); - SG_REF(featurestr1); + DenseFeatures* featurestr1 = new DenseFeatures(feature_cache); featurestr1->set_feature_matrix(feattr, dims, numtr); @@ -120,13 +118,11 @@ int main() // create gaussian kernel // std::cout << "computing gaussian train kernel"<init(featurestr1, featurestr1); // create svm via libsvm and train CLibSVM* svm1 = new CLibSVM(svm_C, kerneltr1, labelstr); - SG_REF(svm1); svm1->set_epsilon(svm_eps); a=time(NULL); @@ -141,8 +137,7 @@ int main() a=time(NULL); std::cout << "initializing shogun test feature"<* featureste1 = new CDenseFeatures(feature_cache); - SG_REF(featureste1); + DenseFeatures* featureste1 = new DenseFeatures(feature_cache); featureste1->set_feature_matrix(featte, dims, numte); @@ -151,8 +146,7 @@ int main() //std::cout<< "elapsed time in seconds "<init(featurestr1, featureste1); svm1->set_kernel(kernelte1); @@ -183,7 +177,6 @@ int main() std::cout << "initializing preprocessor"<io()->set_loglevel(MSG_DEBUG); @@ -204,8 +197,7 @@ int main() a=time(NULL); std::cout << "initializing shogun train feature again"<* featurestr2 = new CDenseFeatures(feature_cache); - SG_REF(featurestr2); + DenseFeatures* featurestr2 = new DenseFeatures(feature_cache); featurestr2->set_feature_matrix(feattr2, dims, numtr); std::cout << "finished"<init(featurestr2, featurestr2); // create svm via libsvm and train CLibSVM* svm2 = new CLibSVM(svm_C, kerneltr2, labelstr); - SG_REF(svm2); svm2->set_epsilon(svm_eps); a=time(NULL); std::cout << "training SVM over linear kernel over preprocessed features"<* featureste2 = new CDenseFeatures(feature_cache); - SG_REF(featureste2); + DenseFeatures* featureste2 = new DenseFeatures(feature_cache); featureste2->set_feature_matrix(featte2, dims, numte); std::cout << "finished"<io()->set_loglevel(MSG_DEBUG); @@ -290,8 +278,7 @@ int main() //std::cout << "computing linear test kernel over preprocessed features"<init(featurestr2, featureste2); //std::cout << "finished"<get_width() << " " << kernelte1->get_width()<* featureste3 = new CDenseFeatures(feature_cache); - SG_REF(featureste3); + DenseFeatures* featureste3 = new DenseFeatures(feature_cache); featureste3->set_feature_matrix(featte3, dims, numte); std::cout << "finished"<init(featurestr2, featureste3); //std::cout << "finished"<* train_features = new CStreamingDenseFeatures(train_file, true, 1024); - SG_REF(train_features); + StreamingDenseFeatures* train_features = new StreamingDenseFeatures(train_file, true, 1024); CRandomConditionalProbabilityTree *cpt = new CRandomConditionalProbabilityTree(); cpt->set_num_passes(1); @@ -28,23 +26,17 @@ int main() cpt->train(); cpt->print_tree(); - CStreamingAsciiFile* test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); - CStreamingDenseFeatures* test_features = new CStreamingDenseFeatures(test_file, true, 1024); - SG_REF(test_features); + StreamingAsciiFile* test_file = new StreamingAsciiFile(test_file_name); + StreamingDenseFeatures* test_features = new StreamingDenseFeatures(test_file, true, 1024); - CMulticlassLabels *pred = cpt->apply_multiclass(test_features); + MulticlassLabels *pred = cpt->apply_multiclass(test_features); test_features->reset_stream(); SG_SPRINT("num_labels = %d\n", pred->get_num_labels()); - SG_UNREF(test_features); - SG_UNREF(test_file); - test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); - test_features = new CStreamingDenseFeatures(test_file, true, 1024); - SG_REF(test_features); + test_file = new StreamingAsciiFile(test_file_name); + test_features = new StreamingDenseFeatures(test_file, true, 1024); - CMulticlassLabels *gnd = new CMulticlassLabels(pred->get_num_labels()); + MulticlassLabels *gnd = new MulticlassLabels(pred->get_num_labels()); test_features->start_parser(); for (int32_t i=0; i < pred->get_num_labels(); ++i) { @@ -65,12 +57,6 @@ int main() SG_SPRINT("Multiclass Accuracy = %.2f%%\n", 100.0*n_correct / gnd->get_num_labels()); - SG_UNREF(train_features); - SG_UNREF(test_features); - SG_UNREF(train_file); - SG_UNREF(test_file); - SG_UNREF(cpt); - SG_UNREF(pred); return 0; } diff --git a/examples/undocumented/libshogun/random_fourier_features.cpp b/examples/undocumented/libshogun/random_fourier_features.cpp index 56629e58de4..07358434345 100644 --- a/examples/undocumented/libshogun/random_fourier_features.cpp +++ b/examples/undocumented/libshogun/random_fourier_features.cpp @@ -13,7 +13,7 @@ using namespace shogun; void load_data(int32_t num_dim, int32_t num_vecs, - CDenseFeatures*& feats, CBinaryLabels*& labels) + DenseFeatures*& feats, BinaryLabels*& labels) { SGMatrix mat(num_dim, num_vecs); SGVector labs(num_vecs); @@ -24,17 +24,17 @@ void load_data(int32_t num_dim, int32_t num_vecs, if ((i+j)%2==0) { labs[i] = -1; - mat(j,i) = CMath::random(0,1) + 0.5; + mat(j,i) = Math::random(0,1) + 0.5; } else { labs[i] = 1; - mat(j,i) = CMath::random(0,1) - 0.5; + mat(j,i) = Math::random(0,1) - 0.5; } } } - feats = new CDenseFeatures(mat); - labels = new CBinaryLabels(labs); + feats = new DenseFeatures(mat); + labels = new BinaryLabels(labs); } int main(int argv, char** argc) @@ -42,8 +42,8 @@ int main(int argv, char** argc) int32_t num_dim = 100; int32_t num_vecs = 10000; - CDenseFeatures* dense_feats = 0; - CBinaryLabels* labels = 0; + DenseFeatures* dense_feats = 0; + BinaryLabels* labels = 0; load_data(num_dim, num_vecs, dense_feats, labels); /** Specifying the kernel parameter for the Gaussian approximation of RFFeatures, @@ -68,20 +68,18 @@ int main(int argv, char** argc) * classifier */ - //CLibLinear* lin_svm = new CLibLinear(C, r_feats, labels); + //LibLinear* lin_svm = new LibLinear(C, r_feats, labels); float64_t C = 0.1; float64_t epsilon = 0.01; - CSVMOcas* lin_svm = new CSVMOcas(C, rf_feats, labels); + SVMOcas* lin_svm = new SVMOcas(C, rf_feats, labels); lin_svm->set_epsilon(epsilon); lin_svm->train(); - CBinaryLabels* predicted = lin_svm->apply()->as(); + BinaryLabels* predicted = lin_svm->apply()->as(); CPRCEvaluation* evaluator = new CPRCEvaluation(); float64_t auPRC = evaluator->evaluate(predicted, labels); //SG_SPRINT("Training auPRC = %f\n", auPRC); - SG_UNREF(lin_svm); - SG_UNREF(predicted); } diff --git a/examples/undocumented/libshogun/regression_gaussian_process_ard.cpp b/examples/undocumented/libshogun/regression_gaussian_process_ard.cpp index 9596b9aaaba..0dea7e66f91 100644 --- a/examples/undocumented/libshogun/regression_gaussian_process_ard.cpp +++ b/examples/undocumented/libshogun/regression_gaussian_process_ard.cpp @@ -30,7 +30,7 @@ int32_t num_vectors=4; int32_t dim_vectors=3; void build_matrices(SGMatrix& test, SGMatrix& train, - CRegressionLabels* labels) + RegressionLabels* labels) { /*Fill Matrices with random nonsense*/ train[0] = -1; @@ -57,34 +57,34 @@ void build_matrices(SGMatrix& test, SGMatrix& train, } } -CModelSelectionParameters* build_tree(CInferenceMethod* inf, - CLikelihoodModel* lik, CKernel* kernel, +ModelSelectionParameters* build_tree(CInferenceMethod* inf, + LikelihoodModel* lik, Kernel* kernel, SGVector& weights) { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1 = - new CModelSelectionParameters("inference_method", inf); + ModelSelectionParameters* c1 = + new ModelSelectionParameters("inference_method", inf); root->append_child(c1); - CModelSelectionParameters* c2 = - new CModelSelectionParameters("likelihood_model", lik); + ModelSelectionParameters* c2 = + new ModelSelectionParameters("likelihood_model", lik); c1->append_child(c2); - CModelSelectionParameters* c3=new CModelSelectionParameters("sigma"); + ModelSelectionParameters* c3=new ModelSelectionParameters("sigma"); c2->append_child(c3); c3->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c4=new CModelSelectionParameters("scale"); + ModelSelectionParameters* c4=new ModelSelectionParameters("scale"); c1->append_child(c4); c4->build_values(1.0, 1.0, R_LINEAR); - CModelSelectionParameters* c5 = - new CModelSelectionParameters("kernel", kernel); + ModelSelectionParameters* c5 = + new ModelSelectionParameters("kernel", kernel); c1->append_child(c5); - CModelSelectionParameters* c6 = - new CModelSelectionParameters("weights"); + ModelSelectionParameters* c6 = + new ModelSelectionParameters("weights"); c5->append_child(c6); c6->build_values_sgvector(0.001, 4.0, R_LINEAR, &weights); @@ -102,22 +102,19 @@ int main(int argc, char **argv) SGMatrix matrix2 = SGMatrix(dim_vectors, num_vectors); - CRegressionLabels* labels=new CRegressionLabels(num_vectors); + RegressionLabels* labels=new RegressionLabels(num_vectors); build_matrices(matrix2, matrix, labels); /* create training features */ - CDenseFeatures* features=new CDenseFeatures (); + DenseFeatures* features=new DenseFeatures (); features->set_feature_matrix(matrix); /* create testing features */ - CDenseFeatures* features2=new CDenseFeatures (); + DenseFeatures* features2=new DenseFeatures (); features2->set_feature_matrix(matrix2); - SG_REF(features); - SG_REF(features2); - SG_REF(labels); /*Allocate our Kernel*/ CLinearARDKernel* test_kernel = new CLinearARDKernel(10); @@ -125,26 +122,24 @@ int main(int argc, char **argv) test_kernel->init(features, features); /*Allocate our mean function*/ - CZeroMean* mean = new CZeroMean(); + ZeroMean* mean = new ZeroMean(); /*Allocate our likelihood function*/ - CGaussianLikelihood* lik = new CGaussianLikelihood(); + GaussianLikelihood* lik = new GaussianLikelihood(); /*Allocate our inference method*/ - CExactInferenceMethod* inf = - new CExactInferenceMethod(test_kernel, + ExactInferenceMethod* inf = + new ExactInferenceMethod(test_kernel, features, mean, labels, lik); - SG_REF(inf); /*Finally use these to allocate the Gaussian Process Object*/ - CGaussianProcessRegression* gp = - new CGaussianProcessRegression(inf); + GaussianProcessRegression* gp = + new GaussianProcessRegression(inf); - SG_REF(gp); /*Build the parameter tree for model selection*/ - CModelSelectionParameters* root = build_tree(inf, lik, test_kernel, + ModelSelectionParameters* root = build_tree(inf, lik, test_kernel, weights); /*Criterion for gradient search*/ @@ -179,7 +174,7 @@ int main(int argc, char **argv) best_combination->apply_to_machine(gp); } - CGradientResult* result=(CGradientResult*)grad->evaluate(); + GradientResult* result=(GradientResult*)grad->evaluate(); if(result->get_result_type() != GRADIENTEVALUATION_RESULT) SG_SERROR("Evaluation result not a GradientEvaluationResult!"); @@ -190,7 +185,7 @@ int main(int argc, char **argv) SGVector labe = labels->get_labels(); SGVector diagonal = inf->get_diagonal_vector(); SGMatrix cholesky = inf->get_cholesky(); - CRegressionLabels* predictions=gp->apply_regression(features); + RegressionLabels* predictions=gp->apply_regression(features); SGVector variance_vector=gp->get_variance_vector(features); alpha.display_vector("Alpha Vector"); @@ -203,15 +198,6 @@ int main(int argc, char **argv) matrix2.display_matrix("Testing Features"); /*free memory*/ - SG_UNREF(features); - SG_UNREF(features2); - SG_UNREF(predictions); - SG_UNREF(labels); - SG_UNREF(inf); - SG_UNREF(gp); - SG_UNREF(grad_search); - SG_UNREF(best_combination); - SG_UNREF(result); return 0; } diff --git a/examples/undocumented/libshogun/regression_gaussian_process_fitc.cpp b/examples/undocumented/libshogun/regression_gaussian_process_fitc.cpp index 058f42b0a0e..0096893a206 100644 --- a/examples/undocumented/libshogun/regression_gaussian_process_fitc.cpp +++ b/examples/undocumented/libshogun/regression_gaussian_process_fitc.cpp @@ -32,7 +32,7 @@ int32_t num_vectors=4; int32_t dim_vectors=3; void build_matrices(SGMatrix& test, SGMatrix& train, - CRegressionLabels* labels) + RegressionLabels* labels) { /*Fill Matrices with random nonsense*/ train[0] = -1; @@ -59,34 +59,34 @@ void build_matrices(SGMatrix& test, SGMatrix& train, } } -CModelSelectionParameters* build_tree(CInferenceMethod* inf, - CLikelihoodModel* lik, CKernel* kernel) +ModelSelectionParameters* build_tree(CInferenceMethod* inf, + LikelihoodModel* lik, Kernel* kernel) { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1 = - new CModelSelectionParameters("inference_method", inf); + ModelSelectionParameters* c1 = + new ModelSelectionParameters("inference_method", inf); root->append_child(c1); - CModelSelectionParameters* c2 = new CModelSelectionParameters("scale"); + ModelSelectionParameters* c2 = new ModelSelectionParameters("scale"); c1 ->append_child(c2); c2->build_values(0.01, 4.0, R_LINEAR); - CModelSelectionParameters* c3 = - new CModelSelectionParameters("likelihood_model", lik); + ModelSelectionParameters* c3 = + new ModelSelectionParameters("likelihood_model", lik); c1->append_child(c3); - CModelSelectionParameters* c4=new CModelSelectionParameters("sigma"); + ModelSelectionParameters* c4=new ModelSelectionParameters("sigma"); c3->append_child(c4); c4->build_values(0.01, 4.0, R_LINEAR); - CModelSelectionParameters* c5 = - new CModelSelectionParameters("kernel", kernel); + ModelSelectionParameters* c5 = + new ModelSelectionParameters("kernel", kernel); c1->append_child(c5); - CModelSelectionParameters* c6 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c6 = + new ModelSelectionParameters("width"); c5->append_child(c6); c6->build_values(0.01, 4.0, R_LINEAR); @@ -102,49 +102,46 @@ int main(int argc, char **argv) SGMatrix matrix2 = SGMatrix(dim_vectors, num_vectors); - CRegressionLabels* labels=new CRegressionLabels(num_vectors); + RegressionLabels* labels=new RegressionLabels(num_vectors); build_matrices(matrix2, matrix, labels); /* create training features */ - CDenseFeatures* features=new CDenseFeatures (); + DenseFeatures* features=new DenseFeatures (); features->set_feature_matrix(matrix); /* create testing features */ - CDenseFeatures* features2=new CDenseFeatures (); + DenseFeatures* features2=new DenseFeatures (); features2->set_feature_matrix(matrix2); - SG_REF(labels); /*Allocate our Kernel*/ - CGaussianKernel* test_kernel = new CGaussianKernel(10, 2); + GaussianKernel* test_kernel = new GaussianKernel(10, 2); test_kernel->init(features, features); /*Allocate our mean function*/ - CZeroMean* mean = new CZeroMean(); + ZeroMean* mean = new ZeroMean(); /*Allocate our likelihood function*/ - CGaussianLikelihood* lik = new CGaussianLikelihood(); + GaussianLikelihood* lik = new GaussianLikelihood(); //SG_SPRINT("features2 bef inf rc= %d\n",features2->ref_count()); /*Allocate our inference method*/ - CFITCInferenceMethod* inf = - new CFITCInferenceMethod(test_kernel, + FITCInferenceMethod* inf = + new FITCInferenceMethod(test_kernel, features, mean, labels, lik, features2); //SG_SPRINT("features2 aft inf rc= %d\n",features2->ref_count()); - SG_REF(inf); /*Finally use these to allocate the Gaussian Process Object*/ - CGaussianProcessRegression* gp = - new CGaussianProcessRegression(inf); + GaussianProcessRegression* gp = + new GaussianProcessRegression(inf); - SG_REF(gp); /*Build the parameter tree for model selection*/ - CModelSelectionParameters* root = build_tree(inf, lik, test_kernel); + ModelSelectionParameters* root = build_tree(inf, lik, test_kernel); /*Criterion for gradient search*/ CGradientCriterion* crit = new CGradientCriterion(); @@ -178,7 +175,7 @@ int main(int argc, char **argv) best_combination->apply_to_machine(gp); } - CGradientResult* result=(CGradientResult*)grad->evaluate(); + GradientResult* result=(GradientResult*)grad->evaluate(); if(result->get_result_type() != GRADIENTEVALUATION_RESULT) SG_SERROR("Evaluation result not a GradientEvaluationResult!"); @@ -189,7 +186,7 @@ int main(int argc, char **argv) SGVector labe = labels->get_labels(); SGVector diagonal = inf->get_diagonal_vector(); SGMatrix cholesky = inf->get_cholesky(); - CRegressionLabels* predictions=gp->apply_regression(features); + RegressionLabels* predictions=gp->apply_regression(features); SGVector variance_vector=gp->get_variance_vector(features); alpha.display_vector("Alpha Vector"); @@ -202,13 +199,6 @@ int main(int argc, char **argv) matrix2.display_matrix("Testing Features"); /*free memory*/ - SG_UNREF(predictions); - SG_UNREF(labels); - SG_UNREF(inf); - SG_UNREF(gp); - SG_UNREF(grad_search); - SG_UNREF(best_combination); - SG_UNREF(result); return 0; } diff --git a/examples/undocumented/libshogun/regression_gaussian_process_laplace.cpp b/examples/undocumented/libshogun/regression_gaussian_process_laplace.cpp index f1917b72adb..d2a4e7ca979 100644 --- a/examples/undocumented/libshogun/regression_gaussian_process_laplace.cpp +++ b/examples/undocumented/libshogun/regression_gaussian_process_laplace.cpp @@ -30,7 +30,7 @@ int32_t num_vectors=4; int32_t dim_vectors=3; void build_matrices(SGMatrix& test, SGMatrix& train, - CRegressionLabels* labels) + RegressionLabels* labels) { /*Fill Matrices with random nonsense*/ train[0] = -1; @@ -57,40 +57,40 @@ void build_matrices(SGMatrix& test, SGMatrix& train, } } -CModelSelectionParameters* build_tree(CInferenceMethod* inf, - CLikelihoodModel* lik, CKernel* kernel) +ModelSelectionParameters* build_tree(CInferenceMethod* inf, + LikelihoodModel* lik, Kernel* kernel) { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1 = - new CModelSelectionParameters("inference_method", inf); + ModelSelectionParameters* c1 = + new ModelSelectionParameters("inference_method", inf); root->append_child(c1); - CModelSelectionParameters* c2 = new CModelSelectionParameters("scale"); + ModelSelectionParameters* c2 = new ModelSelectionParameters("scale"); c1 ->append_child(c2); c2->build_values(0.5, 4.0, R_LINEAR); - CModelSelectionParameters* c3 = - new CModelSelectionParameters("likelihood_model", lik); + ModelSelectionParameters* c3 = + new ModelSelectionParameters("likelihood_model", lik); c1->append_child(c3); - CModelSelectionParameters* c4=new CModelSelectionParameters("sigma"); + ModelSelectionParameters* c4=new ModelSelectionParameters("sigma"); c3->append_child(c4); c4->build_values(0.01, 4.0, R_LINEAR); - CModelSelectionParameters* c43=new CModelSelectionParameters("df"); + ModelSelectionParameters* c43=new ModelSelectionParameters("df"); c3->append_child(c43); c43->build_values(500.0, 1000.0, R_LINEAR); - CModelSelectionParameters* c5 = - new CModelSelectionParameters("kernel", kernel); + ModelSelectionParameters* c5 = + new ModelSelectionParameters("kernel", kernel); c1->append_child(c5); - CModelSelectionParameters* c6 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c6 = + new ModelSelectionParameters("width"); c5->append_child(c6); c6->build_values(0.01, 4.0, R_LINEAR); @@ -106,49 +106,44 @@ int main(int argc, char **argv) SGMatrix matrix2 = SGMatrix(dim_vectors, num_vectors); - CRegressionLabels* labels=new CRegressionLabels(num_vectors); + RegressionLabels* labels=new RegressionLabels(num_vectors); build_matrices(matrix2, matrix, labels); /* create training features */ - CDenseFeatures* features=new CDenseFeatures (); + DenseFeatures* features=new DenseFeatures (); features->set_feature_matrix(matrix); /* create testing features */ - CDenseFeatures* features2=new CDenseFeatures (); + DenseFeatures* features2=new DenseFeatures (); features2->set_feature_matrix(matrix2); - SG_REF(features); - SG_REF(features2); - SG_REF(labels); /*Allocate our Kernel*/ - CGaussianKernel* test_kernel = new CGaussianKernel(10, 2); + GaussianKernel* test_kernel = new GaussianKernel(10, 2); test_kernel->init(features, features); /*Allocate our mean function*/ - CZeroMean* mean = new CZeroMean(); + ZeroMean* mean = new ZeroMean(); /*Allocate our likelihood function*/ - CStudentsTLikelihood* lik = new CStudentsTLikelihood(); + StudentsTLikelihood* lik = new StudentsTLikelihood(); /*Allocate our inference method*/ CLaplacianInferenceMethod* inf = new CLaplacianInferenceMethod(test_kernel, features, mean, labels, lik); - SG_REF(inf); /*Finally use these to allocate the Gaussian Process Object*/ - CGaussianProcessRegression* gp = - new CGaussianProcessRegression(inf); + GaussianProcessRegression* gp = + new GaussianProcessRegression(inf); - SG_REF(gp); /*Build the parameter tree for model selection*/ - CModelSelectionParameters* root = build_tree(inf, lik, test_kernel); + ModelSelectionParameters* root = build_tree(inf, lik, test_kernel); /*Criterion for gradient search*/ CGradientCriterion* crit = new CGradientCriterion(); @@ -182,7 +177,7 @@ int main(int argc, char **argv) best_combination->apply_to_machine(gp); } - CGradientResult* result=(CGradientResult*)grad->evaluate(); + GradientResult* result=(GradientResult*)grad->evaluate(); if(result->get_result_type() != GRADIENTEVALUATION_RESULT) SG_SERROR("Evaluation result not a GradientEvaluationResult!"); @@ -193,7 +188,7 @@ int main(int argc, char **argv) SGVector labe = labels->get_labels(); SGVector diagonal = inf->get_diagonal_vector(); SGMatrix cholesky = inf->get_cholesky(); - CRegressionLabels* predictions=gp->apply_regression(features); + RegressionLabels* predictions=gp->apply_regression(features); SGVector variance_vector=gp->get_variance_vector(features); alpha.display_vector("Alpha Vector"); @@ -206,15 +201,6 @@ int main(int argc, char **argv) matrix2.display_matrix("Testing Features"); /*free memory*/ - SG_UNREF(features); - SG_UNREF(features2); - SG_UNREF(predictions); - SG_UNREF(labels); - SG_UNREF(inf); - SG_UNREF(gp); - SG_UNREF(grad_search); - SG_UNREF(best_combination); - SG_UNREF(result); return 0; } diff --git a/examples/undocumented/libshogun/regression_gaussian_process_product.cpp b/examples/undocumented/libshogun/regression_gaussian_process_product.cpp index 88f1969a42b..1fef9a9583c 100644 --- a/examples/undocumented/libshogun/regression_gaussian_process_product.cpp +++ b/examples/undocumented/libshogun/regression_gaussian_process_product.cpp @@ -31,7 +31,7 @@ int32_t num_vectors=4; int32_t dim_vectors=3; void build_matrices(SGMatrix& test, SGMatrix& train, - CRegressionLabels* labels) + RegressionLabels* labels) { /*Fill Matrices with random nonsense*/ train[0] = -1; @@ -59,111 +59,101 @@ void build_matrices(SGMatrix& test, SGMatrix& train, } /* HEIKO FIXME -CModelSelectionParameters* build_tree(CInferenceMethod* inf, - CLikelihoodModel* lik, CProductKernel* kernel) +ModelSelectionParameters* build_tree(CInferenceMethod* inf, + LikelihoodModel* lik, ProductKernel* kernel) { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1 = - new CModelSelectionParameters("inference_method", inf); + ModelSelectionParameters* c1 = + new ModelSelectionParameters("inference_method", inf); root->append_child(c1); - CModelSelectionParameters* c2 = new CModelSelectionParameters("scale"); + ModelSelectionParameters* c2 = new ModelSelectionParameters("scale"); c1 ->append_child(c2); c2->build_values(0.99, 1.01, R_LINEAR); - CModelSelectionParameters* c3 = - new CModelSelectionParameters("likelihood_model", lik); + ModelSelectionParameters* c3 = + new ModelSelectionParameters("likelihood_model", lik); c1->append_child(c3); - CModelSelectionParameters* c4=new CModelSelectionParameters("sigma"); + ModelSelectionParameters* c4=new ModelSelectionParameters("sigma"); c3->append_child(c4); c4->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c5 = - new CModelSelectionParameters("kernel", kernel); + ModelSelectionParameters* c5 = + new ModelSelectionParameters("kernel", kernel); c1->append_child(c5); - CList* list = kernel->get_list(); - CModelSelectionParameters* cc1 = new CModelSelectionParameters("kernel_list", list); + List* list = kernel->get_list(); + ModelSelectionParameters* cc1 = new ModelSelectionParameters("kernel_list", list); c5->append_child(cc1); - CListElement* first = NULL; - CSGObject* k = list->get_first_element(first); - SG_UNREF(k); - SG_REF(first); + ListElement* first = NULL; + SGObject* k = list->get_first_element(first); - CModelSelectionParameters* cc2 = new CModelSelectionParameters("first", first); + ModelSelectionParameters* cc2 = new ModelSelectionParameters("first", first); cc1->append_child(cc2); - CKernel* sub_kernel1 = kernel->get_kernel(0); - CModelSelectionParameters* cc3 = new CModelSelectionParameters("data", sub_kernel1); + Kernel* sub_kernel1 = kernel->get_kernel(0); + ModelSelectionParameters* cc3 = new ModelSelectionParameters("data", sub_kernel1); cc2->append_child(cc3); - SG_UNREF(sub_kernel1); - CListElement* second = first; + ListElement* second = first; k = list->get_next_element(second); - SG_UNREF(k); - SG_REF(second); - CModelSelectionParameters* cc4 = new CModelSelectionParameters("next", second); + ModelSelectionParameters* cc4 = new ModelSelectionParameters("next", second); cc2->append_child(cc4); - CKernel* sub_kernel2 = kernel->get_kernel(1); - CModelSelectionParameters* cc5 = new CModelSelectionParameters("data", sub_kernel2); + Kernel* sub_kernel2 = kernel->get_kernel(1); + ModelSelectionParameters* cc5 = new ModelSelectionParameters("data", sub_kernel2); cc4->append_child(cc5); - SG_UNREF(sub_kernel2); - CListElement* third = second; + ListElement* third = second; k = list->get_next_element(third); - SG_UNREF(k); - SG_REF(third); - CModelSelectionParameters* cc6 = new CModelSelectionParameters("next", third); + ModelSelectionParameters* cc6 = new ModelSelectionParameters("next", third); cc4->append_child(cc6); - CKernel* sub_kernel3 = kernel->get_kernel(2); - CModelSelectionParameters* cc7 = new CModelSelectionParameters("data", sub_kernel3); + Kernel* sub_kernel3 = kernel->get_kernel(2); + ModelSelectionParameters* cc7 = new ModelSelectionParameters("data", sub_kernel3); cc6->append_child(cc7); - SG_UNREF(sub_kernel3); - CModelSelectionParameters* c6 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c6 = + new ModelSelectionParameters("width"); cc3->append_child(c6); c6->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c66 = - new CModelSelectionParameters("combined_kernel_weight"); + ModelSelectionParameters* c66 = + new ModelSelectionParameters("combined_kernel_weight"); cc3->append_child(c66); c66->build_values(0.001, 1.0, R_LINEAR); - CModelSelectionParameters* c7 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c7 = + new ModelSelectionParameters("width"); cc5->append_child(c7); c7->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c77 = - new CModelSelectionParameters("combined_kernel_weight"); + ModelSelectionParameters* c77 = + new ModelSelectionParameters("combined_kernel_weight"); cc5->append_child(c77); c77->build_values(0.001, 1.0, R_LINEAR); - CModelSelectionParameters* c8 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c8 = + new ModelSelectionParameters("width"); cc7->append_child(c8); c8->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c88 = - new CModelSelectionParameters("combined_kernel_weight"); + ModelSelectionParameters* c88 = + new ModelSelectionParameters("combined_kernel_weight"); cc7->append_child(c88); c88->build_values(0.001, 1.0, R_LINEAR); - SG_UNREF(list); return root; } @@ -178,52 +168,48 @@ int main(int argc, char **argv) SGMatrix matrix2 = SGMatrix(dim_vectors, num_vectors); - CRegressionLabels* labels=new CRegressionLabels(num_vectors); + RegressionLabels* labels=new RegressionLabels(num_vectors); build_matrices(matrix2, matrix, labels); /* create training features */ - CDenseFeatures* features=new CDenseFeatures (); + DenseFeatures* features=new DenseFeatures (); features->set_feature_matrix(matrix); - CCombinedFeatures* comb_features=new CCombinedFeatures(); + CombinedFeatures* comb_features=new CombinedFeatures(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); - CProductKernel* test_kernel = new CProductKernel(); - CGaussianKernel* sub_kernel1 = new CGaussianKernel(10, 2); - CGaussianKernel* sub_kernel2 = new CGaussianKernel(10, 2); - CGaussianKernel* sub_kernel3 = new CGaussianKernel(10, 2); + ProductKernel* test_kernel = new ProductKernel(); + GaussianKernel* sub_kernel1 = new GaussianKernel(10, 2); + GaussianKernel* sub_kernel2 = new GaussianKernel(10, 2); + GaussianKernel* sub_kernel3 = new GaussianKernel(10, 2); test_kernel->append_kernel(sub_kernel1); test_kernel->append_kernel(sub_kernel2); test_kernel->append_kernel(sub_kernel3); - SG_REF(comb_features); - SG_REF(labels); /*Allocate our Mean Function*/ - CZeroMean* mean = new CZeroMean(); + ZeroMean* mean = new ZeroMean(); /*Allocate our Likelihood Model*/ - CGaussianLikelihood* lik = new CGaussianLikelihood(); + GaussianLikelihood* lik = new GaussianLikelihood(); /*Allocate our inference method*/ - CExactInferenceMethod* inf = - new CExactInferenceMethod(test_kernel, + ExactInferenceMethod* inf = + new ExactInferenceMethod(test_kernel, comb_features, mean, labels, lik); - SG_REF(inf); /*Finally use these to allocate the Gaussian Process Object*/ - CGaussianProcessRegression* gp = - new CGaussianProcessRegression(inf); + GaussianProcessRegression* gp = + new GaussianProcessRegression(inf); - SG_REF(gp); - //CModelSelectionParameters* root = build_tree(inf, lik, test_kernel); + //ModelSelectionParameters* root = build_tree(inf, lik, test_kernel); // ///*Criterion for gradient search*/ //CGradientCriterion* crit = new CGradientCriterion(); @@ -257,7 +243,7 @@ int main(int argc, char **argv) // best_combination->apply_to_machine(gp); //} - //CGradientResult* result=(CGradientResult*)grad->evaluate(); + //GradientResult* result=(GradientResult*)grad->evaluate(); //if(result->get_result_type() != GRADIENTEVALUATION_RESULT) // SG_SERROR("Evaluation result not a GradientEvaluationResult!"); @@ -268,13 +254,13 @@ int main(int argc, char **argv) //SGVector labe = labels->get_labels(); //SGVector diagonal = inf->get_diagonal_vector(); //SGMatrix cholesky = inf->get_cholesky(); - //gp->set_return_type(CGaussianProcessRegression::GP_RETURN_COV); + //gp->set_return_type(GaussianProcessRegression::GP_RETURN_COV); - //CRegressionLabels* covariance = gp->apply_regression(comb_features); + //RegressionLabels* covariance = gp->apply_regression(comb_features); - //gp->set_return_type(CGaussianProcessRegression::GP_RETURN_MEANS); + //gp->set_return_type(GaussianProcessRegression::GP_RETURN_MEANS); // - //CRegressionLabels* predictions = gp->apply_regression(); + //RegressionLabels* predictions = gp->apply_regression(); //alpha.display_vector("Alpha Vector"); //labe.display_vector("Labels"); @@ -286,15 +272,6 @@ int main(int argc, char **argv) //matrix2.display_matrix("Testing Features"); ///*free memory*/ - //SG_UNREF(predictions); - //SG_UNREF(covariance); - SG_UNREF(labels); - SG_UNREF(comb_features); - SG_UNREF(inf); - SG_UNREF(gp); - //SG_UNREF(grad_search); - //SG_UNREF(best_combination); - //SG_UNREF(result); return 0; diff --git a/examples/undocumented/libshogun/regression_gaussian_process_simple_exact.cpp b/examples/undocumented/libshogun/regression_gaussian_process_simple_exact.cpp index 456773ae5c2..0b052536557 100644 --- a/examples/undocumented/libshogun/regression_gaussian_process_simple_exact.cpp +++ b/examples/undocumented/libshogun/regression_gaussian_process_simple_exact.cpp @@ -30,33 +30,31 @@ void test() for (index_t i=0; i* feat_train=new CDenseFeatures(X); - CDenseFeatures* feat_test=new CDenseFeatures(X_test); - CRegressionLabels* label_train=new CRegressionLabels(Y); + DenseFeatures* feat_train=new DenseFeatures(X); + DenseFeatures* feat_test=new DenseFeatures(X_test); + RegressionLabels* label_train=new RegressionLabels(Y); /* specity GPR with exact inference */ float64_t sigma=1; float64_t shogun_sigma=sigma*sigma*2; - CGaussianKernel* kernel=new CGaussianKernel(10, shogun_sigma); - CZeroMean* mean=new CZeroMean(); - CGaussianLikelihood* lik=new CGaussianLikelihood(); + GaussianKernel* kernel=new GaussianKernel(10, shogun_sigma); + ZeroMean* mean=new ZeroMean(); + GaussianLikelihood* lik=new GaussianLikelihood(); lik->set_sigma(1); - CExactInferenceMethod* inf=new CExactInferenceMethod(kernel, feat_train, + ExactInferenceMethod* inf=new ExactInferenceMethod(kernel, feat_train, mean, label_train, lik); - CGaussianProcessRegression* gpr=new CGaussianProcessRegression(inf); + GaussianProcessRegression* gpr=new GaussianProcessRegression(inf); /* perform inference */ - CRegressionLabels* predictions=gpr->apply_regression(feat_test); + RegressionLabels* predictions=gpr->apply_regression(feat_test); predictions->get_labels().display_vector("predictions"); - SG_UNREF(predictions); - SG_UNREF(gpr); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/regression_gaussian_process_sum.cpp b/examples/undocumented/libshogun/regression_gaussian_process_sum.cpp index 1f5c8d64bbe..66a60708db7 100644 --- a/examples/undocumented/libshogun/regression_gaussian_process_sum.cpp +++ b/examples/undocumented/libshogun/regression_gaussian_process_sum.cpp @@ -31,7 +31,7 @@ int32_t num_vectors=4; int32_t dim_vectors=3; void build_matrices(SGMatrix& test, SGMatrix& train, - CRegressionLabels* labels) + RegressionLabels* labels) { /*Fill Matrices with random nonsense*/ train[0] = -1; @@ -59,110 +59,100 @@ void build_matrices(SGMatrix& test, SGMatrix& train, } /* HEIKO FIXME -CModelSelectionParameters* build_tree(CInferenceMethod* inf, - CLikelihoodModel* lik, CCombinedKernel* kernel) +ModelSelectionParameters* build_tree(CInferenceMethod* inf, + LikelihoodModel* lik, CombinedKernel* kernel) { - CModelSelectionParameters* root=new CModelSelectionParameters(); + ModelSelectionParameters* root=new ModelSelectionParameters(); - CModelSelectionParameters* c1 = - new CModelSelectionParameters("inference_method", inf); + ModelSelectionParameters* c1 = + new ModelSelectionParameters("inference_method", inf); root->append_child(c1); - CModelSelectionParameters* c2 = new CModelSelectionParameters("scale"); + ModelSelectionParameters* c2 = new ModelSelectionParameters("scale"); c1 ->append_child(c2); c2->build_values(0.99, 1.01, R_LINEAR); - CModelSelectionParameters* c3 = - new CModelSelectionParameters("likelihood_model", lik); + ModelSelectionParameters* c3 = + new ModelSelectionParameters("likelihood_model", lik); c1->append_child(c3); - CModelSelectionParameters* c4=new CModelSelectionParameters("sigma"); + ModelSelectionParameters* c4=new ModelSelectionParameters("sigma"); c3->append_child(c4); c4->build_values(0.001, 1.0, R_LINEAR); - CModelSelectionParameters* c5 = - new CModelSelectionParameters("kernel", kernel); + ModelSelectionParameters* c5 = + new ModelSelectionParameters("kernel", kernel); c1->append_child(c5); - CList* list = kernel->get_list(); - CModelSelectionParameters* cc1 = new CModelSelectionParameters("kernel_list", list); + List* list = kernel->get_list(); + ModelSelectionParameters* cc1 = new ModelSelectionParameters("kernel_list", list); c5->append_child(cc1); - CListElement* first = NULL; - CSGObject* k = list->get_first_element(first); - SG_UNREF(k); - SG_REF(first); + ListElement* first = NULL; + SGObject* k = list->get_first_element(first); - CModelSelectionParameters* cc2 = new CModelSelectionParameters("first", first); + ModelSelectionParameters* cc2 = new ModelSelectionParameters("first", first); cc1->append_child(cc2); - CKernel* sub_kernel1 = kernel->get_kernel(0); - CModelSelectionParameters* cc3 = new CModelSelectionParameters("data", sub_kernel1); + Kernel* sub_kernel1 = kernel->get_kernel(0); + ModelSelectionParameters* cc3 = new ModelSelectionParameters("data", sub_kernel1); cc2->append_child(cc3); - SG_UNREF(sub_kernel1); - CListElement* second = first; + ListElement* second = first; k = list->get_next_element(second); - SG_UNREF(k); - SG_REF(second); - CModelSelectionParameters* cc4 = new CModelSelectionParameters("next", second); + ModelSelectionParameters* cc4 = new ModelSelectionParameters("next", second); cc2->append_child(cc4); - CKernel* sub_kernel2 = kernel->get_kernel(1); - CModelSelectionParameters* cc5 = new CModelSelectionParameters("data", sub_kernel2); + Kernel* sub_kernel2 = kernel->get_kernel(1); + ModelSelectionParameters* cc5 = new ModelSelectionParameters("data", sub_kernel2); cc4->append_child(cc5); - SG_UNREF(sub_kernel2); - CListElement* third = second; + ListElement* third = second; k = list->get_next_element(third); - SG_UNREF(k); - SG_REF(third); - CModelSelectionParameters* cc6 = new CModelSelectionParameters("next", third); + ModelSelectionParameters* cc6 = new ModelSelectionParameters("next", third); cc4->append_child(cc6); - CKernel* sub_kernel3 = kernel->get_kernel(2); - CModelSelectionParameters* cc7 = new CModelSelectionParameters("data", sub_kernel3); + Kernel* sub_kernel3 = kernel->get_kernel(2); + ModelSelectionParameters* cc7 = new ModelSelectionParameters("data", sub_kernel3); cc6->append_child(cc7); - SG_UNREF(sub_kernel3); - CModelSelectionParameters* c6 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c6 = + new ModelSelectionParameters("width"); cc3->append_child(c6); c6->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c66 = - new CModelSelectionParameters("combined_kernel_weight"); + ModelSelectionParameters* c66 = + new ModelSelectionParameters("combined_kernel_weight"); cc3->append_child(c66); c66->build_values(0.001, 1.0, R_LINEAR); - CModelSelectionParameters* c7 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c7 = + new ModelSelectionParameters("width"); cc5->append_child(c7); c7->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c77 = - new CModelSelectionParameters("combined_kernel_weight"); + ModelSelectionParameters* c77 = + new ModelSelectionParameters("combined_kernel_weight"); cc5->append_child(c77); c77->build_values(0.001, 1.0, R_LINEAR); - CModelSelectionParameters* c8 = - new CModelSelectionParameters("width"); + ModelSelectionParameters* c8 = + new ModelSelectionParameters("width"); cc7->append_child(c8); c8->build_values(1.0, 4.0, R_LINEAR); - CModelSelectionParameters* c88 = - new CModelSelectionParameters("combined_kernel_weight"); + ModelSelectionParameters* c88 = + new ModelSelectionParameters("combined_kernel_weight"); cc7->append_child(c88); c88->build_values(0.001, 1.0, R_LINEAR); - SG_UNREF(list); return root; } @@ -177,52 +167,48 @@ int main(int argc, char **argv) SGMatrix matrix2 = SGMatrix(dim_vectors, num_vectors); - CRegressionLabels* labels=new CRegressionLabels(num_vectors); + RegressionLabels* labels=new RegressionLabels(num_vectors); build_matrices(matrix2, matrix, labels); /* create training features */ - CDenseFeatures* features=new CDenseFeatures (); + DenseFeatures* features=new DenseFeatures (); features->set_feature_matrix(matrix); - CCombinedFeatures* comb_features=new CCombinedFeatures(); + CombinedFeatures* comb_features=new CombinedFeatures(); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); comb_features->append_feature_obj(features); - CCombinedKernel* test_kernel = new CCombinedKernel(); - CGaussianKernel* sub_kernel1 = new CGaussianKernel(10, 2); - CGaussianKernel* sub_kernel2 = new CGaussianKernel(10, 2); - CGaussianKernel* sub_kernel3 = new CGaussianKernel(10, 2); + CombinedKernel* test_kernel = new CombinedKernel(); + GaussianKernel* sub_kernel1 = new GaussianKernel(10, 2); + GaussianKernel* sub_kernel2 = new GaussianKernel(10, 2); + GaussianKernel* sub_kernel3 = new GaussianKernel(10, 2); test_kernel->append_kernel(sub_kernel1); test_kernel->append_kernel(sub_kernel2); test_kernel->append_kernel(sub_kernel3); - SG_REF(comb_features); - SG_REF(labels); /*Allocate our Mean Function*/ - CZeroMean* mean = new CZeroMean(); + ZeroMean* mean = new ZeroMean(); /*Allocate our Likelihood Model*/ - CGaussianLikelihood* lik = new CGaussianLikelihood(); + GaussianLikelihood* lik = new GaussianLikelihood(); /*Allocate our inference method*/ - CExactInferenceMethod* inf = - new CExactInferenceMethod(test_kernel, + ExactInferenceMethod* inf = + new ExactInferenceMethod(test_kernel, comb_features, mean, labels, lik); - SG_REF(inf); /*Finally use these to allocate the Gaussian Process Object*/ - CGaussianProcessRegression* gp = - new CGaussianProcessRegression(inf); + GaussianProcessRegression* gp = + new GaussianProcessRegression(inf); - SG_REF(gp); - //CModelSelectionParameters* root = build_tree(inf, lik, test_kernel); + //ModelSelectionParameters* root = build_tree(inf, lik, test_kernel); // ///*Criterion for gradient search*/ //CGradientCriterion* crit = new CGradientCriterion(); @@ -256,7 +242,7 @@ int main(int argc, char **argv) // best_combination->apply_to_machine(gp); //} - //CGradientResult* result=(CGradientResult*)grad->evaluate(); + //GradientResult* result=(GradientResult*)grad->evaluate(); //if(result->get_result_type() != GRADIENTEVALUATION_RESULT) // SG_SERROR("Evaluation result not a GradientEvaluationResult!"); @@ -267,13 +253,13 @@ int main(int argc, char **argv) //SGVector labe = labels->get_labels(); //SGVector diagonal = inf->get_diagonal_vector(); //SGMatrix cholesky = inf->get_cholesky(); - //gp->set_return_type(CGaussianProcessRegression::GP_RETURN_COV); + //gp->set_return_type(GaussianProcessRegression::GP_RETURN_COV); - //CRegressionLabels* covariance = gp->apply_regression(comb_features); + //RegressionLabels* covariance = gp->apply_regression(comb_features); - //gp->set_return_type(CGaussianProcessRegression::GP_RETURN_MEANS); + //gp->set_return_type(GaussianProcessRegression::GP_RETURN_MEANS); // - //CRegressionLabels* predictions = gp->apply_regression(); + //RegressionLabels* predictions = gp->apply_regression(); //alpha.display_vector("Alpha Vector"); //labe.display_vector("Labels"); @@ -285,15 +271,6 @@ int main(int argc, char **argv) //matrix2.display_matrix("Testing Features"); ///*free memory*/ - //SG_UNREF(predictions); - //SG_UNREF(covariance); - SG_UNREF(labels); - SG_UNREF(comb_features); - SG_UNREF(inf); - SG_UNREF(gp); - //SG_UNREF(grad_search); - //SG_UNREF(best_combination); - //SG_UNREF(result); return 0; diff --git a/examples/undocumented/libshogun/regression_libsvr.cpp b/examples/undocumented/libshogun/regression_libsvr.cpp index 349c41f81c6..ace1e7e03d3 100644 --- a/examples/undocumented/libshogun/regression_libsvr.cpp +++ b/examples/undocumented/libshogun/regression_libsvr.cpp @@ -29,46 +29,38 @@ void test_libsvr() for (index_t i=0; i* features_train=new CDenseFeatures( + auto labels_train=std::make_shared(lab_train); + auto labels_test=std::make_shared(lab_test); + auto features_train=std::make_shared>( feat_train); - CDenseFeatures* features_test=new CDenseFeatures( + auto features_test=std::make_shared>( feat_test); - CGaussianKernel* kernel=new CGaussianKernel(rbf_width); + auto kernel=std::make_shared(rbf_width); kernel->init(features_train, features_train); // also epsilon svr possible here LIBSVR_SOLVER_TYPE st=LIBSVR_NU_SVR; - CLibSVR* svm=new CLibSVR(svm_C, svm_nu, kernel, labels_train, st); + auto svm=std::make_shared(svm_C, svm_nu, kernel, labels_train, st); svm->train(); /* predict */ - CRegressionLabels* predicted_labels = - svm->apply(features_test)->as(); - SG_REF(predicted_labels); + auto predicted_labels = + svm->apply(features_test)->as(); /* evaluate */ - CEvaluation* eval=new CMeanSquaredError(); + auto eval=std::make_shared(); SG_SPRINT("mean squared error: %f\n", eval->evaluate(predicted_labels, labels_test)); /* clean up */ - SG_UNREF(eval); - SG_UNREF(labels_test) - SG_UNREF(predicted_labels); - SG_UNREF(svm); - SG_UNREF(labels_train); } int main() diff --git a/examples/undocumented/libshogun/so_factorgraph.cpp b/examples/undocumented/libshogun/so_factorgraph.cpp index bfceaac0ee2..96ae3b80d43 100644 --- a/examples/undocumented/libshogun/so_factorgraph.cpp +++ b/examples/undocumented/libshogun/so_factorgraph.cpp @@ -34,13 +34,11 @@ void create_tree_graph(int hh, int ww) w[2] = 0.5; // 0,1 w[3] = 0.0; // 1,1 int32_t tid = 0; - CTableFactorType* factortype = new CTableFactorType(tid, card, w); - SG_REF(factortype); + TableFactorType* factortype = new TableFactorType(tid, card, w); SGVector vc(hh*ww); SGVector::fill_vector(vc.vector, vc.vlen, 2); - CFactorGraph* fg = new CFactorGraph(vc); - SG_REF(fg); + FactorGraph* fg = new FactorGraph(vc); // Add factors for (int32_t x = 0; x < ww; x++) @@ -53,7 +51,7 @@ void create_tree_graph(int hh, int ww) SGVector var_index(2); var_index[0] = grid_to_index(x,y,ww); var_index[1] = grid_to_index(x-1,y,ww); - CFactor* fac1 = new CFactor(factortype, var_index, data); + Factor* fac1 = new Factor(factortype, var_index, data); fg->add_factor(fac1); } @@ -63,12 +61,11 @@ void create_tree_graph(int hh, int ww) SGVector var_index(2); var_index[0] = grid_to_index(x,y-1,ww); var_index[1] = grid_to_index(x,y,ww); - CFactor* fac1 = new CFactor(factortype, var_index, data); + Factor* fac1 = new Factor(factortype, var_index, data); fg->add_factor(fac1); } } } - SG_UNREF(factortype); fg->connect_components(); @@ -79,16 +76,14 @@ void create_tree_graph(int hh, int ww) fg->compute_energies(); - CMAPInference infer_met(fg, TREE_MAX_PROD); + MAPInference infer_met(fg, TREE_MAX_PROD); infer_met.inference(); - CFactorGraphObservation* fg_observ = infer_met.get_structured_outputs(); + FactorGraphObservation* fg_observ = infer_met.get_structured_outputs(); SGVector assignment = fg_observ->get_data(); - SG_UNREF(fg_observ); assignment.display_vector(); - SG_UNREF(fg); } int main(int argc, char** argv) diff --git a/examples/undocumented/libshogun/so_fg_model.cpp b/examples/undocumented/libshogun/so_fg_model.cpp index 235d4f7c863..25ddf903a95 100644 --- a/examples/undocumented/libshogun/so_fg_model.cpp +++ b/examples/undocumented/libshogun/so_fg_model.cpp @@ -17,7 +17,7 @@ using namespace shogun; void test(int32_t num_samples) { - CMath::init_random(17); + Math::init_random(17); // define factor type SGVector card(2); @@ -33,14 +33,11 @@ void test(int32_t num_samples) w[6] = -0.2; // 1,1 w[7] = 0.75; // 1,1 int32_t tid = 0; - CTableFactorType* factortype = new CTableFactorType(tid, card, w); - SG_REF(factortype); + TableFactorType* factortype = new TableFactorType(tid, card, w); // create features and labels - CFactorGraphFeatures* instances = new CFactorGraphFeatures(num_samples); - SG_REF(instances); - CFactorGraphLabels* labels = new CFactorGraphLabels(num_samples); - SG_REF(labels); + FactorGraphFeatures* instances = new FactorGraphFeatures(num_samples); + FactorGraphLabels* labels = new FactorGraphLabels(num_samples); for (int32_t n = 0; n < num_samples; ++n) { @@ -48,25 +45,25 @@ void test(int32_t num_samples) SGVector vc(3); SGVector::fill_vector(vc.vector, vc.vlen, 2); - CFactorGraph* fg = new CFactorGraph(vc); + FactorGraph* fg = new FactorGraph(vc); // add factors SGVector data1(2); - data1[0] = 2.0 * CMath::random(0.0, 1.0) - 1.0; - data1[1] = 2.0 * CMath::random(0.0, 1.0) - 1.0; + data1[0] = 2.0 * Math::random(0.0, 1.0) - 1.0; + data1[1] = 2.0 * Math::random(0.0, 1.0) - 1.0; SGVector var_index1(2); var_index1[0] = 0; var_index1[1] = 1; - CFactor* fac1 = new CFactor(factortype, var_index1, data1); + Factor* fac1 = new Factor(factortype, var_index1, data1); fg->add_factor(fac1); SGVector data2(2); - data2[0] = 2.0 * CMath::random(0.0, 1.0) - 1.0; - data2[1] = 2.0 * CMath::random(0.0, 1.0) - 1.0; + data2[0] = 2.0 * Math::random(0.0, 1.0) - 1.0; + data2[1] = 2.0 * Math::random(0.0, 1.0) - 1.0; SGVector var_index2(2); var_index2[0] = 1; var_index2[1] = 2; - CFactor* fac2 = new CFactor(factortype, var_index2, data2); + Factor* fac2 = new Factor(factortype, var_index2, data2); fg->add_factor(fac2); // add factor graph instance @@ -75,32 +72,29 @@ void test(int32_t num_samples) fg->connect_components(); fg->compute_energies(); - CMAPInference infer_met(fg, TREE_MAX_PROD); + MAPInference infer_met(fg, TREE_MAX_PROD); infer_met.inference(); - CFactorGraphObservation* fg_observ = infer_met.get_structured_outputs(); + FactorGraphObservation* fg_observ = infer_met.get_structured_outputs(); // add ground truth states labels->add_label(fg_observ); - SG_UNREF(fg_observ); } #ifdef SHOW_DATA // show labels for (unsigned int n = 0; n < num_samples; ++n) { - CFactorGraphObservation* fg_observ = CFactorGraphObservation::obtain_from_generic(labels->get_label(n)); + FactorGraphObservation* fg_observ = FactorGraphObservation::obtain_from_generic(labels->get_label(n)); SG_SPRINT("- sample %d:\n", n); SGVector fst = fg_observ->get_data(); SGVector::display_vector(fst.vector, fst.vlen); - SG_UNREF(fg_observ); } #endif SG_SPRINT("----------------------------------------------------\n"); - CFactorGraphModel* model = new CFactorGraphModel(instances, labels, TREE_MAX_PROD, false); - SG_REF(model); + FactorGraphModel* model = new FactorGraphModel(instances, labels, TREE_MAX_PROD, false); // initialize model parameters SGVector w_truth = w.clone(); @@ -112,30 +106,26 @@ void test(int32_t num_samples) #ifdef USE_MOSEK // create primal mosek solver CPrimalMosekSOSVM* primcp = new CPrimalMosekSOSVM(model, labels); - SG_REF(primcp); primcp->set_regularization(0.01); // TODO: check 1000 #endif // create BMRM solver CDualLibQPBMSOSVM* bmrm = new CDualLibQPBMSOSVM(model, labels, 0.01); bmrm->set_verbose(false); - SG_REF(bmrm); // create SGD solver CStochasticSOSVM* sgd = new CStochasticSOSVM(model, labels); sgd->set_num_iter(100); sgd->set_lambda(0.01); - SG_REF(sgd); // create FW solver CFWSOSVM* fw = new CFWSOSVM(model, labels); fw->set_num_iter(100); fw->set_lambda(0.01); fw->set_gap_threshold(0.01); - SG_REF(fw); // timer - CTime start; + Time start; float64_t t1 = start.cur_time_diff(false); #ifdef USE_MOSEK @@ -173,19 +163,16 @@ void test(int32_t num_samples) #ifdef USE_MOSEK // Evaluation PrimalMosek - CStructuredLabels* labels_primcp = primcp->apply()->as(); - SG_REF(labels_primcp); + StructuredLabels* labels_primcp = primcp->apply()->as(); float64_t acc_loss_primcp = 0.0; float64_t ave_loss_primcp = 0.0; for (int32_t i=0; iget_label(i); - CStructuredData* y_truth = labels->get_label(i); + StructuredData* y_pred = labels_primcp->get_label(i); + StructuredData* y_truth = labels->get_label(i); acc_loss_primcp += model->delta_loss(y_truth, y_pred); - SG_UNREF(y_pred); - SG_UNREF(y_truth); } ave_loss_primcp = acc_loss_primcp / static_cast(num_samples); @@ -193,76 +180,55 @@ void test(int32_t num_samples) #endif // Evaluation BMRM - CStructuredLabels* labels_bmrm = bmrm->apply()->as(); - SG_REF(labels_bmrm); + StructuredLabels* labels_bmrm = bmrm->apply()->as(); float64_t acc_loss_bmrm = 0.0; float64_t ave_loss_bmrm = 0.0; for (int32_t i=0; iget_label(i); - CStructuredData* y_truth = labels->get_label(i); + StructuredData* y_pred = labels_bmrm->get_label(i); + StructuredData* y_truth = labels->get_label(i); acc_loss_bmrm += model->delta_loss(y_truth, y_pred); - SG_UNREF(y_pred); - SG_UNREF(y_truth); } ave_loss_bmrm = acc_loss_bmrm / static_cast(num_samples); SG_SPRINT("bmrm solver: average training loss = %f\n", ave_loss_bmrm); // Evaluation SGD - CStructuredLabels* labels_sgd = sgd->apply()->as(); - SG_REF(labels_sgd); + StructuredLabels* labels_sgd = sgd->apply()->as(); float64_t acc_loss_sgd = 0.0; float64_t ave_loss_sgd = 0.0; for (int32_t i=0; iget_label(i); - CStructuredData* y_truth = labels->get_label(i); + StructuredData* y_pred = labels_sgd->get_label(i); + StructuredData* y_truth = labels->get_label(i); acc_loss_sgd += model->delta_loss(y_truth, y_pred); - SG_UNREF(y_pred); - SG_UNREF(y_truth); } ave_loss_sgd = acc_loss_sgd / static_cast(num_samples); SG_SPRINT("sgd solver: average training loss = %f\n", ave_loss_sgd); // Evaluation FW - CStructuredLabels* labels_fw = fw->apply()->as(); - SG_REF(labels_fw); + StructuredLabels* labels_fw = fw->apply()->as(); float64_t acc_loss_fw = 0.0; float64_t ave_loss_fw = 0.0; for (int32_t i=0; iget_label(i); - CStructuredData* y_truth = labels->get_label(i); + StructuredData* y_pred = labels_fw->get_label(i); + StructuredData* y_truth = labels->get_label(i); acc_loss_fw += model->delta_loss(y_truth, y_pred); - SG_UNREF(y_pred); - SG_UNREF(y_truth); } ave_loss_fw = acc_loss_fw / static_cast(num_samples); SG_SPRINT("fw solver: average training loss = %f\n", ave_loss_fw); #ifdef USE_MOSEK - SG_UNREF(labels_primcp); - SG_UNREF(primcp); #endif - SG_UNREF(labels_fw); - SG_UNREF(labels_sgd); - SG_UNREF(labels_bmrm); - SG_UNREF(fw); - SG_UNREF(sgd); - SG_UNREF(bmrm); - SG_UNREF(model); - SG_UNREF(labels); - SG_UNREF(instances); - SG_UNREF(factortype); } int main(int argc, char * argv[]) diff --git a/examples/undocumented/libshogun/so_fg_multilabel.cpp b/examples/undocumented/libshogun/so_fg_multilabel.cpp index 66d167f9431..777332bcd39 100644 --- a/examples/undocumented/libshogun/so_fg_multilabel.cpp +++ b/examples/undocumented/libshogun/so_fg_multilabel.cpp @@ -91,7 +91,6 @@ void read_data(const char * fname, SGMatrix& labels, SGMatrix get_edge_list(EGraphStructure graph_type, int32_t num_classes) } void build_factor_graph(MultilabelParameter param, SGMatrix feats, SGMatrix labels, - CFactorGraphFeatures * fg_feats, CFactorGraphLabels * fg_labels, - const DynArray& v_ftp_u, - const DynArray& v_ftp_t) + FactorGraphFeatures * fg_feats, FactorGraphLabels * fg_labels, + const DynArray& v_ftp_u, + const DynArray& v_ftp_t) { int32_t num_sample = labels.num_cols; int32_t num_classes = labels.num_rows; @@ -186,7 +185,7 @@ void build_factor_graph(MultilabelParameter param, SGMatrix feats, SG SGVector vc(num_classes); SGVector::fill_vector(vc.vector, vc.vlen, NUM_STATUS); - CFactorGraph * fg = new CFactorGraph(vc); + FactorGraph * fg = new FactorGraph(vc); float64_t * pfeat = feats.get_column_vector(n); SGVector feat_i(dim); @@ -197,7 +196,7 @@ void build_factor_graph(MultilabelParameter param, SGMatrix feats, SG { SGVector var_index_u(1); var_index_u[0] = u; - CFactor * fac_u = new CFactor(v_ftp_u[u], var_index_u, feat_i); + Factor * fac_u = new Factor(v_ftp_u[u], var_index_u, feat_i); fg->add_factor(fac_u); } @@ -207,7 +206,7 @@ void build_factor_graph(MultilabelParameter param, SGMatrix feats, SG SGVector data_t(1); data_t[0] = 1.0; SGVector var_index_t = mat_edges.get_row_vector(t); - CFactor * fac_t = new CFactor(v_ftp_t[t], var_index_t, data_t); + Factor * fac_t = new Factor(v_ftp_t[t], var_index_t, data_t); fg->add_factor(fac_t); } @@ -220,24 +219,22 @@ void build_factor_graph(MultilabelParameter param, SGMatrix feats, SG memcpy(states_gt.vector, plabs, num_classes * sizeof(int32_t)); SGVector loss_weights(num_classes); SGVector::fill_vector(loss_weights.vector, loss_weights.vlen, 1.0/num_classes); - CFactorGraphObservation * fg_obs = new CFactorGraphObservation(states_gt, loss_weights); + FactorGraphObservation * fg_obs = new FactorGraphObservation(states_gt, loss_weights); fg_labels->add_label(fg_obs); } } -void evaluate(CFactorGraphModel * model, int32_t num_samples, CStructuredLabels * labels_sgd, \ - CFactorGraphLabels * fg_labels, float64_t & ave_error) +void evaluate(FactorGraphModel * model, int32_t num_samples, StructuredLabels * labels_sgd, \ + FactorGraphLabels * fg_labels, float64_t & ave_error) { float64_t acc_loss_sgd = 0.0; for (int32_t i = 0; i < num_samples; ++i) { - CStructuredData * y_pred = labels_sgd->get_label(i); - CStructuredData * y_truth = fg_labels->get_label(i); + StructuredData * y_pred = labels_sgd->get_label(i); + StructuredData * y_truth = fg_labels->get_label(i); acc_loss_sgd += model->delta_loss(y_truth, y_pred); - SG_UNREF(y_pred); - SG_UNREF(y_truth); } ave_error = acc_loss_sgd / static_cast(num_samples); @@ -257,7 +254,7 @@ void test(MultilabelParameter param, SGMatrix labels_train, SGMatrix v_ftp_u; + DynArray v_ftp_u; for (int32_t u = 0; u < num_classes; u++) { @@ -266,12 +263,12 @@ void test(MultilabelParameter param, SGMatrix labels_train, SGMatrix w_u(dim * NUM_STATUS); w_u.zero(); - v_ftp_u.append_element(new CTableFactorType(tid, card_u, w_u)); + v_ftp_u.append_element(new TableFactorType(tid, card_u, w_u)); } // define factor type: tree edge factor // note that each edge is a new type - DynArray v_ftp_t; + DynArray v_ftp_t; for (int32_t t = 0; t < num_edges; t++) { @@ -281,21 +278,18 @@ void test(MultilabelParameter param, SGMatrix labels_train, SGMatrix w_t(NUM_STATUS * NUM_STATUS); w_t.zero(); - v_ftp_t.append_element(new CTableFactorType(tid, card_t, w_t)); + v_ftp_t.append_element(new TableFactorType(tid, card_t, w_t)); } // prepare features and labels in factor graph - CFactorGraphFeatures * fg_feats_train = new CFactorGraphFeatures(num_sample_train); - SG_REF(fg_feats_train); - CFactorGraphLabels * fg_labels_train = new CFactorGraphLabels(num_sample_train); - SG_REF(fg_labels_train); + FactorGraphFeatures * fg_feats_train = new FactorGraphFeatures(num_sample_train); + FactorGraphLabels * fg_labels_train = new FactorGraphLabels(num_sample_train); build_factor_graph(param, feats_train, labels_train, fg_feats_train, fg_labels_train, v_ftp_u, v_ftp_t); SG_SPRINT("----------------------------------------------------\n"); - CFactorGraphModel * model = new CFactorGraphModel(fg_feats_train, fg_labels_train, param.infer_type, false); - SG_REF(model); + FactorGraphModel * model = new FactorGraphModel(fg_feats_train, fg_labels_train, param.infer_type, false); // initialize model parameters for (int32_t u = 0; u < num_classes; u++) @@ -308,10 +302,9 @@ void test(MultilabelParameter param, SGMatrix labels_train, SGMatrixset_num_iter(param.sgd_num_iter); sgd->set_lambda(param.sgd_lambda); - SG_REF(sgd); // timer - CTime start; + Time start; // train SGD sgd->train(); float64_t t2 = start.cur_time_diff(false); @@ -319,44 +312,32 @@ void test(MultilabelParameter param, SGMatrix labels_train, SGMatrixapply()->as(); - SG_REF(labels_sgd); + StructuredLabels* labels_sgd = sgd->apply()->as(); float64_t ave_loss_sgd = 0.0; evaluate(model, num_sample_train, labels_sgd, fg_labels_train, ave_loss_sgd); SG_SPRINT("sgd solver: average training loss = %f\n", ave_loss_sgd); - SG_UNREF(labels_sgd); if(labels_test.num_cols > 0) { // prepare features and labels in factor graph int32_t num_sample_test = labels_test.num_cols; - CFactorGraphFeatures * fg_feats_test = new CFactorGraphFeatures(num_sample_test); - SG_REF(fg_feats_test); - CFactorGraphLabels * fg_labels_test = new CFactorGraphLabels(num_sample_test); - SG_REF(fg_labels_test); + FactorGraphFeatures * fg_feats_test = new FactorGraphFeatures(num_sample_test); + FactorGraphLabels * fg_labels_test = new FactorGraphLabels(num_sample_test); build_factor_graph(param, feats_test, labels_test, fg_feats_test, fg_labels_test, v_ftp_u, v_ftp_t); sgd->set_features(fg_feats_test); sgd->set_labels(fg_labels_test); - labels_sgd = sgd->apply()->as(); + labels_sgd = sgd->apply()->as(); evaluate(model, num_sample_test, labels_sgd, fg_labels_test, ave_loss_sgd); - SG_REF(labels_sgd); SG_SPRINT("sgd solver: average testing error = %f\n", ave_loss_sgd); - SG_UNREF(fg_feats_test); - SG_UNREF(fg_labels_test); } - SG_UNREF(labels_sgd); - SG_UNREF(sgd); - SG_UNREF(model); - SG_UNREF(fg_feats_train); - SG_UNREF(fg_labels_train); } int main(int argc, char * argv[]) diff --git a/examples/undocumented/libshogun/so_hmsvm_mosek_simple.cpp b/examples/undocumented/libshogun/so_hmsvm_mosek_simple.cpp index f81fba3e892..82ff3541a1b 100644 --- a/examples/undocumented/libshogun/so_hmsvm_mosek_simple.cpp +++ b/examples/undocumented/libshogun/so_hmsvm_mosek_simple.cpp @@ -46,9 +46,7 @@ int main(int argc, char ** argv) features->set_feature_vector(SGMatrix< float64_t >(mat5, 3, 4, false), 4); CHMSVMModel* model = new CHMSVMModel(features, labels, SMT_TWO_STATE, 3); - SG_REF(model); CPrimalMosekSOSVM* sosvm = new CPrimalMosekSOSVM(model, labels); - SG_REF(sosvm); sosvm->train(); @@ -56,8 +54,6 @@ int main(int argc, char ** argv) sosvm->get_slacks().display_vector("slacks"); // Free memory - SG_UNREF(sosvm); - SG_UNREF(model); #endif /* USE_MOSEK */ diff --git a/examples/undocumented/libshogun/so_multiclass.cpp b/examples/undocumented/libshogun/so_multiclass.cpp index 33339c48aab..b027d792157 100644 --- a/examples/undocumented/libshogun/so_multiclass.cpp +++ b/examples/undocumented/libshogun/so_multiclass.cpp @@ -39,8 +39,8 @@ void gen_rand_data(SGVector< float64_t > labs, SGMatrix< float64_t > feats) { for ( int32_t j = 0 ; j < DIMS ; ++j ) { - means[j] = CMath::random(-100, 100); - stds[j] = CMath::random( 1, 5); + means[j] = Math::random(-100, 100); + stds[j] = Math::random( 1, 5); } for ( int32_t i = 0 ; i < NUM_SAMPLES ; ++i ) @@ -52,7 +52,7 @@ void gen_rand_data(SGVector< float64_t > labs, SGMatrix< float64_t > feats) for ( int32_t j = 0 ; j < DIMS ; ++j ) { feats[(c*NUM_SAMPLES+i)*DIMS + j] = - CMath::normal_random(means[j], stds[j]); + Math::normal_random(means[j], stds[j]); fprintf(pfile, " %f", feats[(c*NUM_SAMPLES+i)*DIMS + j]); } @@ -99,10 +99,10 @@ int main(int argc, char ** argv) // Create train labels CMulticlassSOLabels* labels = new CMulticlassSOLabels(labs); - CMulticlassLabels* mlabels = new CMulticlassLabels(labs); + MulticlassLabels* mlabels = new MulticlassLabels(labs); // Create train features - CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feats); + DenseFeatures< float64_t >* features = new DenseFeatures< float64_t >(feats); // Create structured model CMulticlassModel* model = new CMulticlassModel(features, labels); @@ -112,11 +112,8 @@ int main(int argc, char ** argv) CDualLibQPBMSOSVM* bundle = new CDualLibQPBMSOSVM(model, labels, 100); CStochasticSOSVM* sgd = new CStochasticSOSVM(model, labels); bundle->set_verbose(false); - SG_REF(sosvm); - SG_REF(bundle); - SG_REF(sgd); - CTime start; + Time start; sosvm->train(); float64_t t1 = start.cur_time_diff(false); bundle->train(); @@ -126,26 +123,25 @@ int main(int argc, char ** argv) SG_SPRINT(">>>> PrimalMosekSOSVM trained in %9.4f\n", t1); SG_SPRINT(">>>> BMRM trained in %9.4f\n", t2-t1); SG_SPRINT(">>>> SGD trained in %9.4f\n", t3-t2); - CStructuredLabels* out = sosvm->apply()->as(); - CStructuredLabels* bout = bundle->apply()->as(); - CStructuredLabels* sout = sgd->apply()->as(); + StructuredLabels* out = sosvm->apply()->as(); + StructuredLabels* bout = bundle->apply()->as(); + StructuredLabels* sout = sgd->apply()->as(); // Create liblinear svm classifier with L2-regularized L2-loss - CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC); + LibLinear* svm = new LibLinear(L2R_L2LOSS_SVC); // Add some configuration to the svm svm->set_epsilon(EPSILON); svm->set_bias_enabled(false); // Create a multiclass svm classifier that consists of several of the previous one - CLinearMulticlassMachine* mc_svm = - new CLinearMulticlassMachine( new CMulticlassOneVsRestStrategy(), - (CDotFeatures*) features, svm, mlabels); - SG_REF(mc_svm); + LinearMulticlassMachine* mc_svm = + new LinearMulticlassMachine( new MulticlassOneVsRestStrategy(), + (DotFeatures*) features, svm, mlabels); // Train the multiclass machine using the data passed in the constructor mc_svm->train(); - CMulticlassLabels* mout = mc_svm->apply()->as(); + MulticlassLabels* mout = mc_svm->apply()->as(); SGVector< float64_t > w = sosvm->get_w(); for ( int32_t i = 0 ; i < w.vlen ; ++i ) @@ -154,36 +150,21 @@ int main(int argc, char ** argv) for ( int32_t i = 0 ; i < NUM_CLASSES ; ++i ) { - CLinearMachine* lm = (CLinearMachine*) mc_svm->get_machine(i); + LinearMachine* lm = (LinearMachine*) mc_svm->get_machine(i); SGVector< float64_t > mw = lm->get_w(); for ( int32_t j = 0 ; j < mw.vlen ; ++j ) SG_SPRINT("%10f ", mw[j]); - SG_UNREF(lm); // because of CLinearMulticlassMachine::get_machine() } SG_SPRINT("\n"); - CStructuredAccuracy* structured_evaluator = new CStructuredAccuracy(); - CMulticlassAccuracy* multiclass_evaluator = new CMulticlassAccuracy(); - SG_REF(structured_evaluator); - SG_REF(multiclass_evaluator); + StructuredAccuracy* structured_evaluator = new StructuredAccuracy(); + MulticlassAccuracy* multiclass_evaluator = new MulticlassAccuracy(); SG_SPRINT("SO-SVM: %5.2f%\n", 100.0*structured_evaluator->evaluate(out, labels)); SG_SPRINT("BMRM: %5.2f%\n", 100.0*structured_evaluator->evaluate(bout, labels)); SG_SPRINT("SGD: %5.2f%\n", 100.0*structured_evaluator->evaluate(sout, labels)); SG_SPRINT("MC: %5.2f%\n", 100.0*multiclass_evaluator->evaluate(mout, mlabels)); - // Free memory - SG_UNREF(multiclass_evaluator); - SG_UNREF(structured_evaluator); - SG_UNREF(mout); - SG_UNREF(mc_svm); - SG_UNREF(sgd); - SG_UNREF(bundle); - SG_UNREF(sosvm); - SG_UNREF(sout); - SG_UNREF(bout); - SG_UNREF(out); - return 0; } diff --git a/examples/undocumented/libshogun/so_multiclass_BMRM.cpp b/examples/undocumented/libshogun/so_multiclass_BMRM.cpp index bec600c7046..24776be2802 100644 --- a/examples/undocumented/libshogun/so_multiclass_BMRM.cpp +++ b/examples/undocumented/libshogun/so_multiclass_BMRM.cpp @@ -43,12 +43,10 @@ char FNAME[] = "data.svmlight"; */ void read_data(const char fname[], uint32_t DIM, uint32_t N, SGVector labs, SGMatrix feats) { - CStreamingAsciiFile* file=new CStreamingAsciiFile(fname); - SG_REF(file); + StreamingAsciiFile* file=new StreamingAsciiFile(fname); - CStreamingSparseFeatures< float64_t >* stream_features= - new CStreamingSparseFeatures< float64_t >(file, true, 1024); - SG_REF(stream_features); + StreamingSparseFeatures< float64_t >* stream_features= + new StreamingSparseFeatures< float64_t >(file, true, 1024); SGVector vec(DIM); @@ -72,7 +70,6 @@ void read_data(const char fname[], uint32_t DIM, uint32_t N, SGVector stream_features->end_parser(); - SG_UNREF(stream_features); } /** Generates random multiclass training data and stores them in svmlight format @@ -87,14 +84,14 @@ void gen_rand_data(SGVector< float64_t > labs, SGMatrix< float64_t > feats) FILE* pfile = fopen(FNAME, "w"); - CMath::init_random(17); + Math::init_random(17); for ( int32_t c = 0 ; c < NUM_CLASSES ; ++c ) { for ( int32_t j = 0 ; j < DIMS ; ++j ) { - means[j] = CMath::random(-100, 100); - stds[j] = CMath::random( 1, 5); + means[j] = Math::random(-100, 100); + stds[j] = Math::random( 1, 5); } for ( int32_t i = 0 ; i < NUM_SAMPLES ; ++i ) @@ -106,7 +103,7 @@ void gen_rand_data(SGVector< float64_t > labs, SGMatrix< float64_t > feats) for ( int32_t j = 0 ; j < DIMS ; ++j ) { feats[(c*NUM_SAMPLES+i)*DIMS + j] = - CMath::normal_random(means[j], stds[j]); + Math::normal_random(means[j], stds[j]); fprintf(pfile, " %d:%f", j+1, feats[(c*NUM_SAMPLES+i)*DIMS + j]); } @@ -191,8 +188,8 @@ int main(int argc, char * argv[]) CMulticlassSOLabels* labels = new CMulticlassSOLabels(labs); // Create train features - CDenseFeatures< float64_t >* features = - new CDenseFeatures< float64_t >(feats); + DenseFeatures< float64_t >* features = + new DenseFeatures< float64_t >(feats); // Create structured model CMulticlassModel* model = new CMulticlassModel(features, labels); @@ -203,7 +200,6 @@ int main(int argc, char * argv[]) model, labels, lambda); - SG_REF(sosvm); sosvm->set_cleanAfter(10); sosvm->set_cleanICP(icp); @@ -224,8 +220,7 @@ int main(int argc, char * argv[]) SG_SPRINT("result = { Fp=%lf, Fd=%lf, nIter=%d, nCP=%d, nzA=%d, exitflag=%d }\n", res.Fp, res.Fd, res.nIter, res.nCP, res.nzA, res.exitflag); - CStructuredLabels* out = sosvm->apply()->as(); - SG_REF(out); + StructuredLabels* out = sosvm->apply()->as(); SG_SPRINT("\n"); @@ -235,17 +230,14 @@ int main(int argc, char * argv[]) for (uint32_t i=0; iget_label(i) ); + RealNumber* rn = RealNumber::obtain_from_generic( out->get_label(i) ); error+=(rn->value==labs.get_element(i)) ? 0.0 : 1.0; - SG_UNREF(rn); // because of out->get_label(i) above } SG_SPRINT("Error = %lf %% \n", error/num_feat*100); // Free memory - SG_UNREF(sosvm); - SG_UNREF(out); return 0; } diff --git a/examples/undocumented/libshogun/so_multilabel.cpp b/examples/undocumented/libshogun/so_multilabel.cpp index 32f6716e46b..42b523d9edc 100644 --- a/examples/undocumented/libshogun/so_multilabel.cpp +++ b/examples/undocumented/libshogun/so_multilabel.cpp @@ -30,7 +30,6 @@ void load_data(const char * file_name, { CLibSVMFile * file = new CLibSVMFile(file_name); ASSERT(file != NULL); - SG_REF(file); SGSparseVector * feats; SGVector * labels; @@ -63,12 +62,11 @@ void load_data(const char * file_name, for (index_t j = 0; j < label_sample.vlen; j++) multilabel_sample[j] = label_sample[j]; - CMath::qsort(multilabel_sample); + Math::qsort(multilabel_sample); multilabels[i] = multilabel_sample; } - SG_UNREF(file); SG_FREE(feats); SG_FREE(labels); } @@ -102,28 +100,21 @@ int main(int argc, char ** argv) CMultilabelSOLabels * mlabels = new CMultilabelSOLabels(num_samples, num_classes); - SG_REF(mlabels); mlabels->set_sparse_labels(multilabels); - CSparseFeatures * features = new CSparseFeatures( + SparseFeatures * features = new SparseFeatures( feats_matrix); - SG_REF(features); - CMultilabelModel * model = new CMultilabelModel(features, mlabels); - SG_REF(model); + MultilabelModel * model = new MultilabelModel(features, mlabels); CStochasticSOSVM * sgd = new CStochasticSOSVM(model, mlabels); - SG_REF(sgd); CDualLibQPBMSOSVM * bundle = new CDualLibQPBMSOSVM(model, mlabels, 100); bundle->set_verbose(false); - SG_REF(bundle); CPrimalMosekSOSVM * sosvm = new CPrimalMosekSOSVM(model, mlabels); - SG_REF(sosvm); - CTime * start = new CTime(); - SG_REF(start); + Time * start = new Time(); sgd->train(); float64_t t1 = start->cur_time_diff(false); bundle->train(); @@ -149,25 +140,22 @@ int main(int argc, char ** argv) test_multilabels, num_classes); - CSparseFeatures * test_features = new CSparseFeatures( + SparseFeatures * test_features = new SparseFeatures( test_feats_matrix); - SG_REF(test_features); CMultilabelSOLabels * test_labels = new CMultilabelSOLabels(num_samples, num_classes); - SG_REF(test_labels); test_labels->set_sparse_labels(test_multilabels); - CStructuredLabels* out = sgd->apply(test_features)->as(); + StructuredLabels* out = sgd->apply(test_features)->as(); - CStructuredLabels* bout = - bundle->apply(test_features)->as(); + StructuredLabels* bout = + bundle->apply(test_features)->as(); - CStructuredLabels* sout = - sosvm->apply(test_features)->as(); + StructuredLabels* sout = + sosvm->apply(test_features)->as(); - CStructuredAccuracy * evaluator = new CStructuredAccuracy(); - SG_REF(evaluator); + StructuredAccuracy * evaluator = new StructuredAccuracy(); SG_SPRINT(">>> Accuracy of multilabel classification using %s = %f\n", sgd->get_name(), evaluator->evaluate(out, test_labels)); @@ -177,19 +165,6 @@ int main(int argc, char ** argv) SG_SPRINT(">>> Accuracy of multilabel classification using %s = %f\n", sosvm->get_name(), evaluator->evaluate(sout, test_labels)); - SG_UNREF(bout); - SG_UNREF(bundle); - SG_UNREF(evaluator); - SG_UNREF(features); - SG_UNREF(mlabels); - SG_UNREF(model); - SG_UNREF(out); - SG_UNREF(sgd); - SG_UNREF(sosvm); - SG_UNREF(sout); - SG_UNREF(start); - SG_UNREF(test_features); - SG_UNREF(test_labels); SG_FREE(multilabels); SG_FREE(test_multilabels); diff --git a/examples/undocumented/libshogun/splitting_LOO_crossvalidation.cpp b/examples/undocumented/libshogun/splitting_LOO_crossvalidation.cpp index bdb953c5c28..c64272430cd 100644 --- a/examples/undocumented/libshogun/splitting_LOO_crossvalidation.cpp +++ b/examples/undocumented/libshogun/splitting_LOO_crossvalidation.cpp @@ -16,15 +16,15 @@ int main(int argc, char **argv) while (runs-->0) { - num_labels=CMath::random(10, 50); + num_labels=Math::random(10, 50); //SG_SPRINT("num_labels=%d\n\n", num_labels); /* build labels */ - CRegressionLabels* labels=new CRegressionLabels(num_labels); + RegressionLabels* labels=new RegressionLabels(num_labels); for (index_t i=0; iset_label(i, CMath::random(-10.0, 10.0)); + labels->set_label(i, Math::random(-10.0, 10.0)); // SG_SPRINT("label(%d)=%.18g\n", i, labels->get_label(i)); } @@ -61,7 +61,6 @@ int main(int argc, char **argv) } /* clean up */ - SG_UNREF(splitting); } diff --git a/examples/undocumented/libshogun/splitting_standard_crossvalidation.cpp b/examples/undocumented/libshogun/splitting_standard_crossvalidation.cpp index ba5b8d36df5..be1858790ce 100644 --- a/examples/undocumented/libshogun/splitting_standard_crossvalidation.cpp +++ b/examples/undocumented/libshogun/splitting_standard_crossvalidation.cpp @@ -17,9 +17,9 @@ int main(int argc, char **argv) while (runs-->0) { - num_labels=CMath::random(10, 150); - num_subsets=CMath::random(1, 5); - index_t desired_size=CMath::round( + num_labels=Math::random(10, 150); + num_subsets=Math::random(1, 5); + index_t desired_size=Math::round( (float64_t)num_labels/(float64_t)num_subsets); /* this will throw an error */ @@ -29,17 +29,17 @@ int main(int argc, char **argv) SG_SPRINT("num_labels=%d\nnum_subsets=%d\n\n", num_labels, num_subsets); /* build labels */ - CRegressionLabels* labels=new CRegressionLabels(num_labels); + RegressionLabels* labels=new RegressionLabels(num_labels); for (index_t i=0; iset_label(i, CMath::random(-10.0, 10.0)); + labels->set_label(i, Math::random(-10.0, 10.0)); SG_SPRINT("label(%d)=%.18g\n", i, labels->get_label(i)); } SG_SPRINT("\n"); /* build splitting strategy */ - CCrossValidationSplitting* splitting= - new CCrossValidationSplitting(labels, num_subsets); + CrossValidationSplitting* splitting= + new CrossValidationSplitting(labels, num_subsets); /* build index sets (twice to ensure memory is not leaking) */ splitting->build_subsets(); @@ -58,7 +58,7 @@ int main(int argc, char **argv) SG_SPRINT("checking subset size: %d vs subset desired size %d\n", subset.vlen, desired_size); - ASSERT(CMath::abs(subset.vlen-desired_size)<=1); + ASSERT(Math::abs(subset.vlen-desired_size)<=1); ASSERT(subset.vlen+inverse.vlen==num_labels); for (index_t j=0; j0) { - num_labels=CMath::random(5, 100); - num_classes=CMath::random(2, 10); - num_subsets=CMath::random(1, 10); + num_labels=Math::random(5, 100); + num_classes=Math::random(2, 10); + num_subsets=Math::random(1, 10); /* this will throw an error */ if (num_labelsset_label(i, CMath::random()%num_classes); + labels->set_label(i, Math::random()%num_classes); SG_SPRINT("label(%d)=%.18g\n", i, labels->get_label(i)); } SG_SPRINT("\n"); @@ -41,8 +41,8 @@ int main(int argc, char **argv) SGVector::display_vector(classes.vector, classes.vlen, "classes"); /* build splitting strategy */ - CStratifiedCrossValidationSplitting* splitting= - new CStratifiedCrossValidationSplitting(labels, num_subsets); + StratifiedCrossValidationSplitting* splitting= + new StratifiedCrossValidationSplitting(labels, num_subsets); /* build index sets (twice to ensure memory is not leaking) */ splitting->build_subsets(); @@ -93,12 +93,11 @@ int main(int argc, char **argv) /* at most one difference */ SG_SPRINT("number in subset %d: %d\n", j, temp_count); - ASSERT(CMath::abs(temp_count-count)<=1); + ASSERT(Math::abs(temp_count-count)<=1); } } /* clean up */ - SG_UNREF(splitting); } return 0; diff --git a/examples/undocumented/libshogun/streaming_from_dense.cpp b/examples/undocumented/libshogun/streaming_from_dense.cpp index d8139f92c3e..5bceffce580 100644 --- a/examples/undocumented/libshogun/streaming_from_dense.cpp +++ b/examples/undocumented/libshogun/streaming_from_dense.cpp @@ -28,7 +28,7 @@ void gen_rand_data(SGMatrix feat, SGVector lab) if (i feat, SGVector lab) else { for (int32_t j=0; j* features=new CDenseFeatures(feat); - SG_REF(features); + auto features=std::make_shared>(feat); // Create a StreamingDenseFeatures object which uses the above as input; // labels (float64_t*) are optional - CStreamingDenseFeatures* streaming=new CStreamingDenseFeatures< - float64_t>(features, lab); - SG_REF(streaming); + auto streaming=std::make_shared>(features, lab); // Start parsing of the examples; in this case, it is trivial - returns each vector from the DenseFeatures object streaming->start_parser(); @@ -104,8 +102,6 @@ void test_general() // Now that all examples are used, end the parser. streaming->end_parser(); - SG_UNREF(streaming); - SG_UNREF(features); } void test_get_streamed_features() @@ -120,30 +116,25 @@ void test_get_streamed_features() gen_rand_data(feat, lab); // Create features - CDenseFeatures* features=new CDenseFeatures(feat); - SG_REF(features); + auto features=std::make_shared>(feat); // Create a StreamingDenseFeatures object which uses the above as input; // labels (float64_t*) are optional - CStreamingDenseFeatures* streaming=new CStreamingDenseFeatures< - float64_t>(features, lab); - SG_REF(streaming); + auto streaming=std::make_shared>(features, lab); streaming->start_parser(); - CDenseFeatures* dense= - (CDenseFeatures*)streaming->get_streamed_features(NUM); + auto dense= + streaming->get_streamed_features(NUM)->as>(); streaming->end_parser(); /* assert that matrices are equal */ ASSERT(dense->get_feature_matrix().equals(feat)); - SG_UNREF(dense); - SG_UNREF(features); - SG_UNREF(streaming); } void test_get_streamed_features_too_many() @@ -158,32 +149,27 @@ void test_get_streamed_features_too_many() gen_rand_data(feat, lab); // Create features - CDenseFeatures* features=new CDenseFeatures(feat); - SG_REF(features); + auto features=std::make_shared>(feat); // Create a StreamingDenseFeatures object which uses the above as input; // labels (float64_t*) are optional - CStreamingDenseFeatures* streaming=new CStreamingDenseFeatures< - float64_t>(features, lab); - SG_REF(streaming); + auto streaming=std::make_shared>(features, lab); streaming->start_parser(); /* request more features than available */ - CDenseFeatures* dense= - (CDenseFeatures*)streaming->get_streamed_features(NUM+10); + auto dense= + streaming->get_streamed_features(NUM+10)->as>(); streaming->end_parser(); /* assert that matrices are equal */ ASSERT(dense->get_feature_matrix().equals(feat)); - SG_UNREF(dense); - SG_UNREF(features); - SG_UNREF(streaming); } int main() diff --git a/examples/undocumented/libshogun/streaming_onlineliblinear_dense.cpp b/examples/undocumented/libshogun/streaming_onlineliblinear_dense.cpp index bc650fa41bb..118af649875 100644 --- a/examples/undocumented/libshogun/streaming_onlineliblinear_dense.cpp +++ b/examples/undocumented/libshogun/streaming_onlineliblinear_dense.cpp @@ -16,16 +16,14 @@ int main() { // Create a StreamingAsciiFile from the training data const char* train_file_name = "../data/train_densereal.light"; - CStreamingAsciiFile* train_file = new CStreamingAsciiFile(train_file_name); - SG_REF(train_file); + auto train_file = std::make_shared(train_file_name); // The bool value is true if examples are labelled. // 1024 is a good standard value for the number of examples for the parser to hold at a time. - CStreamingDenseFeatures* train_features = new CStreamingDenseFeatures(train_file, true, 1024); - SG_REF(train_features); + auto train_features = std::make_shared>(train_file, true, 1024); // Create an OnlineLiblinear object from the features. The first parameter is 'C'. - COnlineLibLinear* svm = new COnlineLibLinear(1, train_features); + auto svm = std::make_shared(1, train_features); svm->set_bias_enabled(false); // Enable/disable bias svm->train(); // Train @@ -34,25 +32,17 @@ int main() // Now we want to test on other data const char* test_file_name = "../data/fm_test_densereal.dat"; - CStreamingAsciiFile* test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); + auto test_file = std::make_shared(test_file_name); // Similar, but 'false' since the file contains unlabelled examples - CStreamingDenseFeatures* test_features = new CStreamingDenseFeatures(test_file, false, 1024); - SG_REF(test_features); + auto test_features = std::make_shared>(test_file, false, 1024); - // Apply on all examples and return a CLabels* - CRegressionLabels* test_labels = svm->apply_regression(test_features); + // Apply on all examples and return a Labels* + auto test_labels = svm->apply_regression(test_features); for (int32_t i=0; iget_num_labels(); i++) SG_SPRINT("For example %d, predicted label is %f.\n", i, test_labels->get_label(i)); - SG_UNREF(test_features); - SG_UNREF(test_labels); - SG_UNREF(test_file); - SG_UNREF(train_features); - SG_UNREF(train_file); - SG_UNREF(svm); return 0; } diff --git a/examples/undocumented/libshogun/streaming_onlineliblinear_sparse.cpp b/examples/undocumented/libshogun/streaming_onlineliblinear_sparse.cpp index 06de32e0685..fbda01fe143 100644 --- a/examples/undocumented/libshogun/streaming_onlineliblinear_sparse.cpp +++ b/examples/undocumented/libshogun/streaming_onlineliblinear_sparse.cpp @@ -50,29 +50,25 @@ int main(int argc, char* argv[]) fprintf(stderr, "*** training file %s with C %g\n", train_file_name, C); // Create an OnlineLiblinear object from the features. The first parameter is 'C'. - COnlineLibLinear *svm = new COnlineLibLinear(C); + auto svm = std::make_shared(C); svm->set_bias_enabled(true); { - CTime train_time; + Time train_time; train_time.start(); // Create a StreamingAsciiFile from the training data - CStreamingAsciiFile *train_file = new CStreamingAsciiFile(train_file_name); - SG_REF(train_file); + auto train_file = std::make_shared(train_file_name); // The bool value is true if examples are labelled. // 1024 is a good standard value for the number of examples for the parser to hold at a time. - CStreamingSparseFeatures < float32_t > *train_features = - new CStreamingSparseFeatures < float32_t > (train_file, true, 1024); - SG_REF(train_features); + auto train_features = + std::make_shared> (train_file, true, 1024); svm->set_features(train_features); svm->train(); train_file->close(); - SG_UNREF(train_file); - SG_UNREF(train_features); train_time.stop(); @@ -89,29 +85,24 @@ int main(int argc, char* argv[]) { - CTime test_time; + Time test_time; test_time.start(); // Now we want to test on holdout data - CStreamingAsciiFile *test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); + auto test_file = std::make_shared(test_file_name); // Set second parameter to 'false' if the file contains unlabelled examples - CStreamingSparseFeatures < float32_t > *test_features = - new CStreamingSparseFeatures < float32_t > (test_file, true, 1024); - SG_REF(test_features); + auto test_features = + std::make_shared> (test_file, true, 1024); - // Apply on all examples and return a CBinaryLabels* - CBinaryLabels *test_binary_labels = svm->apply_binary(test_features); - SG_REF(test_binary_labels); + // Apply on all examples and return a BinaryLabels* + auto test_binary_labels = svm->apply_binary(test_features); test_time.stop(); uint64_t test_time_int = test_time.cur_time_diff(); fprintf(stderr, "*** testing took %llum%llus (or %.1f sec)\n", test_time_int / 60, test_time_int % 60, test_time.cur_time_diff()); - SG_UNREF(test_features); - SG_UNREF(test_file); // Writing labels for evaluation fprintf(stderr, "*** writing labels to file %s\n", test_labels_file_name); @@ -122,11 +113,8 @@ int main(int argc, char* argv[]) fprintf(fh, "%d\n", test_binary_labels->get_int_label(j)); fclose(fh); - SG_UNREF(test_binary_labels); unlink(test_labels_file_name); } - SG_UNREF(svm); - return 0; } diff --git a/examples/undocumented/libshogun/streaming_onlinesvmsgd.cpp b/examples/undocumented/libshogun/streaming_onlinesvmsgd.cpp index bf02b797f82..dbc779056b2 100644 --- a/examples/undocumented/libshogun/streaming_onlinesvmsgd.cpp +++ b/examples/undocumented/libshogun/streaming_onlinesvmsgd.cpp @@ -16,14 +16,12 @@ int main() { // Create a StreamingAsciiFile from the training data char* train_file_name = "../data/train_sparsereal.light"; - CStreamingAsciiFile* train_file = new CStreamingAsciiFile(train_file_name); - SG_REF(train_file); + StreamingAsciiFile* train_file = new StreamingAsciiFile(train_file_name); // Create a StreamingSparseFeatures from the StreamingAsciiFile. // The bool value is true if examples are labelled. // 1024 is a good standard value for the number of examples for the parser to hold at a time. - CStreamingSparseFeatures* train_features = new CStreamingSparseFeatures(train_file, true, 1024); - SG_REF(train_features); + StreamingSparseFeatures* train_features = new StreamingSparseFeatures(train_file, true, 1024); // Create an OnlineSVMSGD object from the features. The first parameter is 'C'. COnlineSVMSGD* sgd = new COnlineSVMSGD(1, train_features); @@ -36,24 +34,17 @@ int main() // Now we want to test on other data char* test_file_name = "../data/fm_test_sparsereal.dat"; - CStreamingAsciiFile* test_file = new CStreamingAsciiFile(test_file_name); - SG_REF(test_file); + StreamingAsciiFile* test_file = new StreamingAsciiFile(test_file_name); // Similar, but 'false' since the file contains unlabelled examples - CStreamingSparseFeatures* test_features = new CStreamingSparseFeatures(test_file, false, 1024); - SG_REF(test_features); + StreamingSparseFeatures* test_features = new StreamingSparseFeatures(test_file, false, 1024); - // Apply on all examples and return a CLabels* - CLabels* test_labels = sgd->apply(test_features); + // Apply on all examples and return a Labels* + Labels* test_labels = sgd->apply(test_features); for (int32_t i=0; iget_num_labels(); i++) SG_SPRINT("For example %d, predicted label is %f.\n", i, test_labels->get_label(i)); - SG_UNREF(test_features); - SG_UNREF(test_file); - SG_UNREF(train_features); - SG_UNREF(train_file); - SG_UNREF(sgd); return 0; } diff --git a/examples/undocumented/libshogun/streaming_stringfeatures.cpp b/examples/undocumented/libshogun/streaming_stringfeatures.cpp index f12f6805b8e..7ad9dbbb286 100644 --- a/examples/undocumented/libshogun/streaming_stringfeatures.cpp +++ b/examples/undocumented/libshogun/streaming_stringfeatures.cpp @@ -23,12 +23,10 @@ void display_vector(const SGString &vec) int main(int argc, char **argv) { // Create a StreamingAsciiFile from our input file - CStreamingAsciiFile* file = new CStreamingAsciiFile("../data/fm_train_dna.dat"); - SG_REF(file); + auto file = std::make_shared("../data/fm_train_dna.dat"); // This file contains unlabelled data, so the second arg is `false'. - CStreamingStringFeatures* feat = new CStreamingStringFeatures(file, false, 1024); - SG_REF(feat); + auto feat = std::make_shared>(file, false, 1024); // Alphabet to use is DNA feat->use_alphabet(DNA); @@ -43,12 +41,10 @@ int main(int argc, char **argv) feat->end_parser(); // Get the alphabet and display the histogram - CAlphabet* alpha = feat->get_alphabet(); + auto alpha = feat->get_alphabet(); printf("\nThe histogram is:\n"); alpha->print_histogram(); - SG_UNREF(alpha); - SG_UNREF(feat); return 0; } diff --git a/examples/undocumented/libshogun/structure_discrete_hmsvm_bmrm.cpp b/examples/undocumented/libshogun/structure_discrete_hmsvm_bmrm.cpp index 2bb40c6b14a..de0f50bddf1 100644 --- a/examples/undocumented/libshogun/structure_discrete_hmsvm_bmrm.cpp +++ b/examples/undocumented/libshogun/structure_discrete_hmsvm_bmrm.cpp @@ -21,6 +21,5 @@ int main() CDualLibQPBMSOSVM* sosvm = new CDualLibQPBMSOSVM(model, labels, 5000,0); sosvm->train(); - SG_UNREF(sosvm); return 0; } diff --git a/examples/undocumented/libshogun/structure_hmsvm_mosek.cpp b/examples/undocumented/libshogun/structure_hmsvm_mosek.cpp index 1da673e775e..3efcee6efd1 100644 --- a/examples/undocumented/libshogun/structure_hmsvm_mosek.cpp +++ b/examples/undocumented/libshogun/structure_hmsvm_mosek.cpp @@ -13,18 +13,17 @@ int main(int argc, char ** argv) int32_t example_length = 250; int32_t num_features = 10; int32_t num_noise_features = 2; - CHMSVMModel* model = CTwoStateModel::simulate_data(num_examples, example_length, num_features, num_noise_features); + CHMSVMModel* model = TwoStateModel::simulate_data(num_examples, example_length, num_features, num_noise_features); - CStructuredLabels* labels = model->get_labels(); - CFeatures* features = model->get_features(); + StructuredLabels* labels = model->get_labels(); + Features* features = model->get_features(); CPrimalMosekSOSVM* sosvm = new CPrimalMosekSOSVM(model, labels); - SG_REF(sosvm); sosvm->train(); // sosvm->get_w().display_vector("w"); - CStructuredLabels* out = sosvm->apply()->as(); + StructuredLabels* out = sosvm->apply()->as(); ASSERT( out->get_num_labels() == labels->get_num_labels() ); @@ -32,14 +31,8 @@ int main(int argc, char ** argv) { CSequence* pred_seq = CSequence::obtain_from_generic( out->get_label(i) ); CSequence* true_seq = CSequence::obtain_from_generic( labels->get_label(i) ); - SG_UNREF(pred_seq); - SG_UNREF(true_seq); } - SG_UNREF(out); - SG_UNREF(features); // because model->get_features() increased the count - SG_UNREF(labels); // because model->get_labels() increased the count - SG_UNREF(sosvm); #endif /* USE_MOSEK */ diff --git a/examples/undocumented/libshogun/structure_plif_hmsvm_bmrm.cpp b/examples/undocumented/libshogun/structure_plif_hmsvm_bmrm.cpp index e559fb02e7f..56285ae8afe 100644 --- a/examples/undocumented/libshogun/structure_plif_hmsvm_bmrm.cpp +++ b/examples/undocumented/libshogun/structure_plif_hmsvm_bmrm.cpp @@ -6,14 +6,11 @@ using namespace shogun; int main() { - CTwoStateModel* tsm = new CTwoStateModel(); + TwoStateModel* tsm = new TwoStateModel(); CHMSVMModel* model = tsm->simulate_data(100,250,3,1); - CStructuredLabels* labels = model->get_labels(); + StructuredLabels* labels = model->get_labels(); CDualLibQPBMSOSVM* sosvm = new CDualLibQPBMSOSVM(model, labels, 5000.0); sosvm->train(); - SG_UNREF(sosvm); - SG_UNREF(labels); - SG_UNREF(tsm); return 0; } diff --git a/examples/undocumented/libshogun/transfer_multitaskleastsquaresregression.cpp b/examples/undocumented/libshogun/transfer_multitaskleastsquaresregression.cpp index b30820a27a7..f963e1e2d92 100644 --- a/examples/undocumented/libshogun/transfer_multitaskleastsquaresregression.cpp +++ b/examples/undocumented/libshogun/transfer_multitaskleastsquaresregression.cpp @@ -18,10 +18,10 @@ int main(int argc, char** argv) for (int32_t i=0; i<2*4; i++) matrix.matrix[i]=i; - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); // create three labels - CRegressionLabels* labels=new CRegressionLabels(4); + RegressionLabels* labels=new RegressionLabels(4); labels->set_label(0, -1.4); labels->set_label(1, +1.5); labels->set_label(2, -1.2); @@ -39,7 +39,6 @@ int main(int argc, char** argv) regressor->set_current_task(0); regressor->get_w().display_vector(); - SG_UNREF(regressor); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/transfer_multitasklogisticregression.cpp b/examples/undocumented/libshogun/transfer_multitasklogisticregression.cpp index 74a87f56eab..01c79563dd6 100644 --- a/examples/undocumented/libshogun/transfer_multitasklogisticregression.cpp +++ b/examples/undocumented/libshogun/transfer_multitasklogisticregression.cpp @@ -23,10 +23,10 @@ int main(int argc, char** argv) for (int32_t i=0; i<2*4; i++) matrix.matrix[i]=i; - CDenseFeatures* features= new CDenseFeatures(matrix); + DenseFeatures* features= new DenseFeatures(matrix); // create three labels - CBinaryLabels* labels=new CBinaryLabels(4); + BinaryLabels* labels=new BinaryLabels(4); labels->set_label(0, -1); labels->set_label(1, +1); labels->set_label(2, -1); @@ -55,7 +55,6 @@ int main(int argc, char** argv) regressor->set_current_task(0); regressor->get_w().display_vector(); - SG_UNREF(regressor); return 0; } #else //USE_GPL_SHOGUN diff --git a/examples/undocumented/libshogun/variational_approx_example.cpp b/examples/undocumented/libshogun/variational_approx_example.cpp index 510ffba39c1..82e6e459ec0 100644 --- a/examples/undocumented/libshogun/variational_approx_example.cpp +++ b/examples/undocumented/libshogun/variational_approx_example.cpp @@ -55,10 +55,9 @@ using namespace shogun; SGMatrix init_piecewise_bound(const char * fname) { SGMatrix bound; - CCSVFile* bound_file = new CCSVFile(fname); + CSVFile* bound_file = new CSVFile(fname); bound_file->set_delimiter('\t'); bound.load(bound_file); - SG_UNREF(bound_file); return bound; } @@ -67,10 +66,9 @@ SGMatrix init_piecewise_bound(const char * fname) SGVector load_m_from_matlab(const char * fname) { SGVector m_from_matlab; - CCSVFile* m_file = new CCSVFile(fname); + CSVFile* m_file = new CSVFile(fname); m_file->set_delimiter('\t'); m_from_matlab.load(m_file); - SG_UNREF(m_file); return m_from_matlab; } @@ -79,10 +77,9 @@ SGVector load_m_from_matlab(const char * fname) float64_t load_loglik_from_matlab(const char * fname) { SGVector f_from_matlab; - CCSVFile* f_file = new CCSVFile(fname); + CSVFile* f_file = new CSVFile(fname); f_file->set_delimiter('\t'); f_from_matlab.load(f_file); - SG_UNREF(f_file); REQUIRE(f_from_matlab.vlen == 1, "logLik is a scalar"); return f_from_matlab[0]; } @@ -102,9 +99,9 @@ SGMatrix create_feature(const char *fname, index_t num_sample, for(index_t j = 0; j < num_dim; j++) { if (i < num_sample/2) - X(i, j) = CMath::random(0,1)*5.0; + X(i, j) = Math::random(0,1)*5.0; else - X(i, j) = CMath::random(0,1)*-5.0; + X(i, j) = Math::random(0,1)*-5.0; } } */ @@ -112,10 +109,9 @@ SGMatrix create_feature(const char *fname, index_t num_sample, //The following pre-init value is used to verify the correctness //The following code will be removed. SGMatrix X; - CCSVFile* X_file = new CCSVFile(fname); + CSVFile* X_file = new CSVFile(fname); X_file->set_delimiter('\t'); X.load(X_file); - SG_UNREF(X_file); return X; } @@ -134,7 +130,7 @@ SGVector create_label(const char * fname, SGVector mu, Eigen::Map eigen_sigma(sigma.matrix, sigma.num_rows, sigma.num_cols); //y = mvnrnd(mu, Sigma, 1); - CProbabilityDistribution * dist = new CGaussianDistribution(mu, sigma); + ProbabilityDistribution * dist = new GaussianDistribution(mu, sigma); y = dist->sample(); //y = (y(:)>0); //Note that Shogun uses -1 and 1 as labels @@ -145,17 +141,15 @@ SGVector create_label(const char * fname, SGVector mu, else y[i] = -1; } - SG_UNREF(dist); */ //The following pre-init value is used to verify the correctness //The following code will be removed. //Note that Shogun uses -1 and 1 as labels SGVector y; - CCSVFile* y_file = new CCSVFile(fname); + CSVFile* y_file = new CSVFile(fname); y_file->set_delimiter('\t'); y.load(y_file); - SG_UNREF(y_file); for(index_t i = 0; i < y.vlen; i++) { @@ -173,7 +167,7 @@ SGVector create_label(const char * fname, SGVector mu, //The following struct is used to pass information when using the build-in L-BFGS component struct Shared { - CLogitVGPiecewiseBoundLikelihood *lik; + LogitVGPiecewiseBoundLikelihood *lik; SGVector y; SGVector mu; lbfgs_parameter_t lbfgs_param; @@ -214,7 +208,7 @@ float64_t evaluate(void *obj, const float64_t *variable, float64_t *gradient, { Shared * obj_prt = static_cast(obj); - CBinaryLabels lab(obj_prt->y); + BinaryLabels lab(obj_prt->y); obj_prt->lik->set_variational_distribution(obj_prt->m0, obj_prt->v, &lab); Eigen::Map eigen_mu(obj_prt->mu.vector, obj_prt->mu.vlen); Eigen::Map eigen_m(obj_prt->m0.vector, obj_prt->m0.vlen); @@ -302,7 +296,7 @@ void run(const char * x_file, const char * y_file, const char * bound_file, //load('llp.mat'); obj.bound = init_piecewise_bound(bound_file); - obj.lik = new CLogitVGPiecewiseBoundLikelihood(); + obj.lik = new LogitVGPiecewiseBoundLikelihood(); obj.lik->set_variational_bound(obj.bound); //m0 = mu; % initial value all zero @@ -339,15 +333,14 @@ void run(const char * x_file, const char * y_file, const char * bound_file, float64_t relative_diff; if (m_from_matlab[i] != 0.0) - relative_diff = CMath::abs(obj.m0[i]/m_from_matlab[i] - 1); + relative_diff = Math::abs(obj.m0[i]/m_from_matlab[i] - 1); else - relative_diff = CMath::abs(obj.m0[i]); + relative_diff = Math::abs(obj.m0[i]); SG_SPRINT("m[%d] from Shogun =%.10f from Matlab = %.10f relative_diff = %.10f\n", i+1, obj.m0[i], m_from_matlab[i], relative_diff); } - SG_UNREF(obj.lik); } void test_datasets() diff --git a/scripts/light-scrubber.sh b/scripts/light-scrubber.sh index 679168eb102..0fcc82fff6c 100755 --- a/scripts/light-scrubber.sh +++ b/scripts/light-scrubber.sh @@ -63,7 +63,7 @@ touch -r ${_file}.orig ${_file} && \ rm -rf ${_file}.orig _file="src/interfaces/modular/Machine.i" && \ -sed -i.orig -e '/.*CSVRLight.*/d' ${_file} && \ +sed -i.orig -e '/.*SVRLight.*/d' ${_file} && \ touch -r ${_file}.orig ${_file} && \ rm -rf ${_file}.orig diff --git a/src/.scrub_docstrings.py b/src/.scrub_docstrings.py index de84a829539..db68df21bde 100644 --- a/src/.scrub_docstrings.py +++ b/src/.scrub_docstrings.py @@ -17,7 +17,7 @@ [' int16_t', ' int'], [' uint16_t', ' int'], [' char', ' str'], - ['-> CFeatures', '-> Features'], + ['-> Features', '-> Features'], ] class Scrub: diff --git a/src/interfaces/perl/swig_typemaps.i b/src/interfaces/perl/swig_typemaps.i index 5e8e90dda98..7a446cd24dc 100644 --- a/src/interfaces/perl/swig_typemaps.i +++ b/src/interfaces/perl/swig_typemaps.i @@ -387,7 +387,7 @@ fail: %} -/* CFeatures to ... */ +/* Features to ... */ %define FEATURES_BY_TYPECODE(obj, f, type, typecode) switch (typecode) { case F_BOOL: @@ -427,25 +427,25 @@ fail: obj=SWIG_NewPointerObj(f, $descriptor(type *), SWIG_POINTER_EXCEPTION); break; default: - obj=SWIG_NewPointerObj(f, $descriptor(shogun::CFeatures*), SWIG_POINTER_EXCEPTION); + obj=SWIG_NewPointerObj(f, $descriptor(shogun::Features*), SWIG_POINTER_EXCEPTION); break; } %enddef -%typemap(out) shogun::CFeatures* +%typemap(out) shogun::Features* { int feats_class=$1->get_feature_class(); int feats_type=$1->get_feature_type(); switch (feats_class){ case C_DENSE: { - FEATURES_BY_TYPECODE($result, $1, shogun::CDenseFeatures, feats_type) + FEATURES_BY_TYPECODE($result, $1, shogun::DenseFeatures, feats_type) break; } case C_SPARSE: { - FEATURES_BY_TYPECODE($result, $1, shogun::CSparseFeatures, feats_type) + FEATURES_BY_TYPECODE($result, $1, shogun::SparseFeatures, feats_type) break; } case C_STRING: @@ -454,7 +454,7 @@ fail: break; } case C_COMBINED: - $result=SWIG_NewPointerObj($1, $descriptor(shogun::CCombinedFeatures*), SWIG_POINTER_EXCEPTION); + $result=SWIG_NewPointerObj($1, $descriptor(shogun::CombinedFeatures*), SWIG_POINTER_EXCEPTION); break; case C_COMBINED_DOT: $result=SWIG_NewPointerObj($1, $descriptor(shogun::CCombinedDotFeatures*), SWIG_POINTER_EXCEPTION); @@ -473,12 +473,12 @@ fail: break; case C_STREAMING_DENSE: { - FEATURES_BY_TYPECODE($result, $1, shogun::CStreamingDenseFeatures, feats_type) + FEATURES_BY_TYPECODE($result, $1, shogun::StreamingDenseFeatures, feats_type) break; } case C_STREAMING_SPARSE: { - FEATURES_BY_TYPECODE($result, $1, shogun::CStreamingSparseFeatures, feats_type) + FEATURES_BY_TYPECODE($result, $1, shogun::StreamingSparseFeatures, feats_type) break; } case C_STREAMING_STRING: @@ -491,10 +491,10 @@ fail: $result=SWIG_NewPointerObj($1, $descriptor(shogun::CBinnedDotFeatures*), SWIG_POINTER_EXCEPTION); break; case C_DIRECTOR_DOT: - $result=SWIG_NewPointerObj($1, $descriptor(shogun::CDirectorDotFeatures*), SWIG_POINTER_EXCEPTION); + $result=SWIG_NewPointerObj($1, $descriptor(shogun::DirectorDotFeatures*), SWIG_POINTER_EXCEPTION); break; default: - $result=SWIG_NewPointerObj($1, $descriptor(shogun::CFeatures*), SWIG_POINTER_EXCEPTION); + $result=SWIG_NewPointerObj($1, $descriptor(shogun::Features*), SWIG_POINTER_EXCEPTION); break; } argvi++; diff --git a/src/interfaces/python/CustomKernel_protocols.i b/src/interfaces/python/CustomKernel_protocols.i index f2400556088..faa286aa4f4 100644 --- a/src/interfaces/python/CustomKernel_protocols.i +++ b/src/interfaces/python/CustomKernel_protocols.i @@ -15,21 +15,21 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject * { PyObject* resultobj=NULL; - CCustomKernel* arg1=NULL; // self in c++ repr + CustomKernel* arg1=NULL; // self in c++ repr int res1=0; // result for self's casting void* argp1=NULL; // pointer to self PyObject* kernel_narray=NULL; SGMatrix< type_name > kernel_matrix; - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CCustomKernel"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CustomKernel"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" "inplace operator_name" "', argument " "1"" of type '" "CCustomKernel *""'"); + "in method '" "inplace operator_name" "', argument " "1"" of type '" "CustomKernel *""'"); } - arg1=reinterpret_cast< CCustomKernel* >(argp1); + arg1=reinterpret_cast< CustomKernel* >(argp1); kernel_matrix=arg1->get_float32_kernel_matrix(); kernel_narray=PySequence_GetSlice(self, 0, kernel_matrix.num_rows); @@ -54,7 +54,7 @@ fail: /* used by PyObject_GetBuffer */ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags) { - CCustomKernel* arg1=(CCustomKernel *) 0; // self in c++ repr + CustomKernel* arg1=(CustomKernel *) 0; // self in c++ repr void* argp1=0; // pointer to self int res1=0; // result for self's casting @@ -67,11 +67,11 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags) static char* format=(char *) format_str; // http://docs.python.org/dev/library/struct.html#module-struct - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CCustomKernel"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CustomKernel"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" "getbuffer" "', argument " "1"" of type '" "CCustomKernel *""'"); + "in method '" "getbuffer" "', argument " "1"" of type '" "CustomKernel *""'"); } if ((flags & PyBUF_C_CONTIGUOUS)==PyBUF_C_CONTIGUOUS) @@ -87,7 +87,7 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags) goto fail; } - arg1=reinterpret_cast< CCustomKernel* >(argp1); + arg1=reinterpret_cast< CustomKernel* >(argp1); info=(buffer_matrix_ ## type_name ## _info*) malloc(sizeof(buffer_matrix_ ## type_name ## _info)); new (&info->buf) SGMatrix< type_name >(); @@ -156,7 +156,7 @@ static void class_name ## _releasebuffer(PyObject *self, Py_buffer *view) /* used by PySequence_GetItem */ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx) { - CCustomKernel* arg1=0; // self in c++ repr + CustomKernel* arg1=0; // self in c++ repr void* argp1=0; // pointer to self int res1=0; // result for self's casting @@ -172,14 +172,14 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx) PyArrayObject* ret; PyArray_Descr* descr=PyArray_DescrFromType(typecode); - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CCustomKernel"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CustomKernel"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" " class_name _getitem" "', argument " "1"" of type '" "CCustomKernel *""'"); + "in method '" " class_name _getitem" "', argument " "1"" of type '" "CustomKernel *""'"); } - arg1=reinterpret_cast< CCustomKernel* >(argp1); + arg1=reinterpret_cast< CustomKernel* >(argp1); kernel_matrix=arg1->get_float32_kernel_matrix(); num_rows=kernel_matrix.num_rows; @@ -247,7 +247,7 @@ fail: /* used by PySequence_GetSlice */ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssize_t ihigh) { - CCustomKernel* arg1=0; // self in c++ repr + CustomKernel* arg1=0; // self in c++ repr void* argp1=0; // pointer to self int res1=0 ; // result for self's casting @@ -262,14 +262,14 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi PyArrayObject* ret; PyArray_Descr* descr=PyArray_DescrFromType(typecode); - res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CCustomKernel"), 0 | 0 ); + res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CustomKernel"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" " class_name _slice" "', argument " "1"" of type '" "CCustomKernel *""'"); + "in method '" " class_name _slice" "', argument " "1"" of type '" "CustomKernel *""'"); } - arg1=reinterpret_cast< CCustomKernel* >(argp1); + arg1=reinterpret_cast< CustomKernel* >(argp1); kernel_matrix=arg1->get_float32_kernel_matrix(); num_rows=kernel_matrix.num_rows; @@ -337,7 +337,7 @@ static PyObject* class_name ## _getsubscript_helper(PyObject *self, PyObject *ke // key is tuple, like (PySlice or PyLong, PySlice or PyLong) // or only PySlice/PyLong - CCustomKernel* arg1=0; // self in c++ repr + CustomKernel* arg1=0; // self in c++ repr void* argp1=0; // pointer to self int res1=0 ; // result for self's casting @@ -370,14 +370,14 @@ static PyObject* class_name ## _getsubscript_helper(PyObject *self, PyObject *ke PyObject* tmp; // temporary object for tuple's item - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CCustomKernel"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CustomKernel"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" " class_name _subscript" "', argument " "1"" of type '" "CCustomKernel *""'"); + "in method '" " class_name _subscript" "', argument " "1"" of type '" "CustomKernel *""'"); } - arg1=reinterpret_cast< CCustomKernel* >(argp1); + arg1=reinterpret_cast< CustomKernel* >(argp1); kernel_matrix=arg1->get_float32_kernel_matrix(); num_rows=kernel_matrix.num_rows; @@ -522,18 +522,18 @@ fail: static PyObject* class_name ## _cleanup_custom(PyObject *self, PyObject *args) { PyObject* resultobj=NULL; - CCustomKernel* arg1=NULL; + CustomKernel* arg1=NULL; void* argp1=NULL; int res1=0; - res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CCustomKernel"), 0 | 0 ); + res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CustomKernel"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), "in method '" - "cleanup" "', argument " "1"" of type '" "shogun::CCustomKernel *""'"); + "cleanup" "', argument " "1"" of type '" "shogun::CustomKernel *""'"); } - arg1=reinterpret_cast< CCustomKernel * >(argp1); + arg1=reinterpret_cast< CustomKernel * >(argp1); { try { @@ -586,10 +586,10 @@ static long class_name ## _flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFE %init %{ // overload flags slot in DenseFatures proxy class -SwigPyBuiltin__shogun__CCustomKernel_type.ht_type.tp_flags = class_name ## _flags; +SwigPyBuiltin__shogun__CustomKernel_type.ht_type.tp_flags = class_name ## _flags; // overload free_feature_matrix in DenseFeatures -set_method(SwigPyBuiltin__shogun__CCustomKernel_methods, "cleanup_custom", (PyCFunction) class_name ## _cleanup_custom); +set_method(SwigPyBuiltin__shogun__CustomKernel_methods, "cleanup_custom", (PyCFunction) class_name ## _cleanup_custom); %} @@ -597,30 +597,30 @@ set_method(SwigPyBuiltin__shogun__CCustomKernel_methods, "cleanup_custom", (PyCF %{ #include -static std::map< CCustomKernel*, Py_buffer*> extend_ ## class_name ## _info; +static std::map< CustomKernel*, Py_buffer*> extend_ ## class_name ## _info; %} -%feature("python:bf_getbuffer") CCustomKernel #class_name "_getbuffer" -%feature("python:bf_releasebuffer") CCustomKernel #class_name "_releasebuffer" +%feature("python:bf_getbuffer") CustomKernel #class_name "_getbuffer" +%feature("python:bf_releasebuffer") CustomKernel #class_name "_releasebuffer" -%feature("python:nb_inplace_add") CCustomKernel #class_name "_inplaceadd" -%feature("python:nb_inplace_subtract") CCustomKernel #class_name "_inplacesub" -%feature("python:nb_inplace_multiply") CCustomKernel #class_name "_inplacemul" +%feature("python:nb_inplace_add") CustomKernel #class_name "_inplaceadd" +%feature("python:nb_inplace_subtract") CustomKernel #class_name "_inplacesub" +%feature("python:nb_inplace_multiply") CustomKernel #class_name "_inplacemul" -%feature("python:sq_item") CCustomKernel #class_name "_getitem" -%feature("python:sq_ass_item") CCustomKernel #class_name "_setitem" -%feature("python:sq_slice") CCustomKernel #class_name "_getslice" -%feature("python:sq_ass_slice") CCustomKernel #class_name "_setslice" +%feature("python:sq_item") CustomKernel #class_name "_getitem" +%feature("python:sq_ass_item") CustomKernel #class_name "_setitem" +%feature("python:sq_slice") CustomKernel #class_name "_getslice" +%feature("python:sq_ass_slice") CustomKernel #class_name "_setslice" -%feature("python:mp_subscript") CCustomKernel #class_name "_getsubscript" -%feature("python:mp_ass_subscript") CCustomKernel #class_name "_setsubscript" +%feature("python:mp_subscript") CustomKernel #class_name "_getsubscript" +%feature("python:mp_ass_subscript") CustomKernel #class_name "_setsubscript" %enddef /* PROTOCOLS_CUSTOMKERNEL */ %define EXTEND_CUSTOMKERNEL(class_name, type_name, typecode) -%extend shogun::CCustomKernel +%extend shogun::CustomKernel { int frombuffer(PyObject* exporter, bool copy) diff --git a/src/interfaces/python/DenseFeatures_protocols.i b/src/interfaces/python/DenseFeatures_protocols.i index d9bdace7620..6c90ae3a5a6 100644 --- a/src/interfaces/python/DenseFeatures_protocols.i +++ b/src/interfaces/python/DenseFeatures_protocols.i @@ -15,20 +15,20 @@ PyObject* class_name ## _inplace ## operator_name ## (PyObject *self, PyObject * { PyObject* resultobj=0; - CDenseFeatures< type_name >* arg1=(CDenseFeatures< type_name > *) 0; // self in c++ repr + DenseFeatures< type_name >* arg1=(DenseFeatures< type_name > *) 0; // self in c++ repr int res1=0; // result for self's casting void* argp1=0; // pointer to self PyObject* internal_data=0; - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::DenseFeatures"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" "inplace operator_name" "', argument " "1"" of type '" "CDenseFeatures< type_name > *""'"); + "in method '" "inplace operator_name" "', argument " "1"" of type '" "DenseFeatures< type_name > *""'"); } - arg1=reinterpret_cast< CDenseFeatures < type_name >* >(argp1); + arg1=reinterpret_cast< DenseFeatures < type_name >* >(argp1); internal_data=PySequence_GetSlice(self, 0, arg1->get_num_features()); PyNumber_InPlace ## operator ## (internal_data, o2); @@ -52,7 +52,7 @@ fail: /* used by PyObject_GetBuffer */ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags) { - CDenseFeatures< type_name >* arg1=(CDenseFeatures< type_name > *) 0; // self in c++ repr + DenseFeatures< type_name >* arg1=(DenseFeatures< type_name > *) 0; // self in c++ repr void* argp1=0; // pointer to self int res1=0; // result for self's casting @@ -64,11 +64,11 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags) static char* format=(char *) format_str; // http://docs.python.org/dev/library/struct.html#module-struct - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::DenseFeatures"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" "getbuffer" "', argument " "1"" of type '" "CDenseFeatures< type_name > *""'"); + "in method '" "getbuffer" "', argument " "1"" of type '" "DenseFeatures< type_name > *""'"); } if ((flags & PyBUF_C_CONTIGUOUS)==PyBUF_C_CONTIGUOUS) @@ -84,7 +84,7 @@ static int class_name ## _getbuffer(PyObject *self, Py_buffer *view, int flags) goto fail; } - arg1=reinterpret_cast< CDenseFeatures < type_name >* >(argp1); + arg1=reinterpret_cast< DenseFeatures < type_name >* >(argp1); info=(buffer_matrix_ ## type_name ## _info*) malloc(sizeof(buffer_matrix_ ## type_name ## _info)); new (&info->buf) SGMatrix< type_name >(); @@ -151,7 +151,7 @@ static void class_name ## _releasebuffer(PyObject *self, Py_buffer *view) /* used by PySequence_GetItem */ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx) { - CDenseFeatures< type_name >* arg1=0; // self in c++ repr + DenseFeatures< type_name >* arg1=0; // self in c++ repr void* argp1=0; // pointer to self int res1=0; // result for self's casting @@ -166,14 +166,14 @@ static PyObject* class_name ## _getitem(PyObject *self, Py_ssize_t idx) PyArrayObject* ret; PyArray_Descr* descr=PyArray_DescrFromType(typecode); - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::DenseFeatures"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" " class_name _getitem" "', argument " "1"" of type '" "CDenseFeatures< type_name > *""'"); + "in method '" " class_name _getitem" "', argument " "1"" of type '" "DenseFeatures< type_name > *""'"); } - arg1=reinterpret_cast< CDenseFeatures< type_name >* >(argp1); + arg1=reinterpret_cast< DenseFeatures< type_name >* >(argp1); temp=arg1->get_feature_matrix(); num_feat=arg1->get_num_features(); @@ -241,7 +241,7 @@ fail: /* used by PySequence_GetSlice */ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssize_t ihigh) { - CDenseFeatures< type_name >* arg1=0; // self in c++ repr + DenseFeatures< type_name >* arg1=0; // self in c++ repr void* argp1=0; // pointer to self int res1=0 ; // result for self's casting @@ -256,14 +256,14 @@ static PyObject* class_name ## _getslice(PyObject *self, Py_ssize_t ilow, Py_ssi PyArrayObject* ret; PyArray_Descr* descr=PyArray_DescrFromType(typecode); - res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures"), 0 | 0 ); + res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::DenseFeatures"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" " class_name _slice" "', argument " "1"" of type '" "CDenseFeatures< type_name > *""'"); + "in method '" " class_name _slice" "', argument " "1"" of type '" "DenseFeatures< type_name > *""'"); } - arg1=reinterpret_cast< CDenseFeatures< type_name >* >(argp1); + arg1=reinterpret_cast< DenseFeatures< type_name >* >(argp1); temp=arg1->get_feature_matrix(); num_feat=arg1->get_num_features(); @@ -331,7 +331,7 @@ static PyObject* class_name ## _getsubscript_helper(PyObject *self, PyObject *ke // key is tuple, like (PySlice or PyLong, PySlice or PyLong) // or only PySlice or PyLong - CDenseFeatures< type_name >* arg1=0; // self in c++ repr + DenseFeatures< type_name >* arg1=0; // self in c++ repr void* argp1=0; // pointer to self int res1=0 ; // result for self's casting @@ -364,14 +364,14 @@ static PyObject* class_name ## _getsubscript_helper(PyObject *self, PyObject *ke PyObject* tmp; // temporary object for tuple's item - res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures"), 0 | 0 ); + res1 = SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::DenseFeatures"), 0 | 0 ); if (!SWIG_IsOK(res1)) { SWIG_exception_fail(SWIG_ArgError(res1), - "in method '" " class_name _subscript" "', argument " "1"" of type '" "CDenseFeatures< type_name > *""'"); + "in method '" " class_name _subscript" "', argument " "1"" of type '" "DenseFeatures< type_name > *""'"); } - arg1=reinterpret_cast< CDenseFeatures< type_name >* >(argp1); + arg1=reinterpret_cast< DenseFeatures< type_name >* >(argp1); temp=arg1->get_feature_matrix(); num_feat=arg1->get_num_features(); @@ -516,17 +516,17 @@ fail: static PyObject* class_name ## _free_feature_matrix(PyObject *self, PyObject *args) { PyObject* resultobj=0; - CDenseFeatures< type_name > *arg1=(CDenseFeatures< type_name > *) 0; + DenseFeatures< type_name > *arg1=(DenseFeatures< type_name > *) 0; void* argp1=0; int res1=0; - res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::CDenseFeatures"), 0 | 0 ); + res1=SWIG_ConvertPtr(self, &argp1, SWIG_TypeQuery("shogun::DenseFeatures"), 0 | 0 ); if (!SWIG_IsOK(res1)) { - SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "BoolFeatures_free_feature_matrix" "', argument " "1"" of type '" "shogun::CDenseFeatures< bool > *""'"); + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "BoolFeatures_free_feature_matrix" "', argument " "1"" of type '" "shogun::DenseFeatures< bool > *""'"); } - arg1=reinterpret_cast< CDenseFeatures< type_name > * >(argp1); + arg1=reinterpret_cast< DenseFeatures< type_name > * >(argp1); { try { @@ -579,10 +579,10 @@ static long class_name ## _flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_NEWBUFFE %init %{ // overload flags slot in DenseFatures proxy class -SwigPyBuiltin__shogun__CDenseFeaturesT_ ## type_name ## _t_type.ht_type.tp_flags = class_name ## _flags; +SwigPyBuiltin__shogun__DenseFeaturesT_ ## type_name ## _t_type.ht_type.tp_flags = class_name ## _flags; // overload free_feature_matrix in DenseFeatures -set_method(SwigPyBuiltin__shogun__CDenseFeaturesT_ ## type_name ## _t_methods, "free_feature_matrix", (PyCFunction) class_name ## _free_feature_matrix); +set_method(SwigPyBuiltin__shogun__DenseFeaturesT_ ## type_name ## _t_methods, "free_feature_matrix", (PyCFunction) class_name ## _free_feature_matrix); %} @@ -590,30 +590,30 @@ set_method(SwigPyBuiltin__shogun__CDenseFeaturesT_ ## type_name ## _t_methods, " %{ #include -static std::map*, Py_buffer*> extend_ ## class_name ## _info; +static std::map*, Py_buffer*> extend_ ## class_name ## _info; %} -%feature("python:bf_getbuffer") CDenseFeatures< type_name > #class_name "_getbuffer" -%feature("python:bf_releasebuffer") CDenseFeatures< type_name > #class_name "_releasebuffer" +%feature("python:bf_getbuffer") DenseFeatures< type_name > #class_name "_getbuffer" +%feature("python:bf_releasebuffer") DenseFeatures< type_name > #class_name "_releasebuffer" -%feature("python:nb_inplace_add") CDenseFeatures< type_name > #class_name "_inplaceadd" -%feature("python:nb_inplace_subtract") CDenseFeatures< type_name > #class_name "_inplacesub" -%feature("python:nb_inplace_multiply") CDenseFeatures< type_name > #class_name "_inplacemul" +%feature("python:nb_inplace_add") DenseFeatures< type_name > #class_name "_inplaceadd" +%feature("python:nb_inplace_subtract") DenseFeatures< type_name > #class_name "_inplacesub" +%feature("python:nb_inplace_multiply") DenseFeatures< type_name > #class_name "_inplacemul" -%feature("python:sq_item") CDenseFeatures< type_name > #class_name "_getitem" -%feature("python:sq_ass_item") CDenseFeatures< type_name > #class_name "_setitem" -%feature("python:sq_slice") CDenseFeatures< type_name > #class_name "_getslice" -%feature("python:sq_ass_slice") CDenseFeatures< type_name > #class_name "_setslice" +%feature("python:sq_item") DenseFeatures< type_name > #class_name "_getitem" +%feature("python:sq_ass_item") DenseFeatures< type_name > #class_name "_setitem" +%feature("python:sq_slice") DenseFeatures< type_name > #class_name "_getslice" +%feature("python:sq_ass_slice") DenseFeatures< type_name > #class_name "_setslice" -%feature("python:mp_subscript") CDenseFeatures< type_name > #class_name "_getsubscript" -%feature("python:mp_ass_subscript") CDenseFeatures< type_name > #class_name "_setsubscript" +%feature("python:mp_subscript") DenseFeatures< type_name > #class_name "_getsubscript" +%feature("python:mp_ass_subscript") DenseFeatures< type_name > #class_name "_setsubscript" %enddef /* PROTOCOLS_DENSEFEATURES */ %define EXTEND_DENSEFEATURES(class_name, type_name, typecode) -%extend CDenseFeatures< type_name > +%extend DenseFeatures< type_name > { int frombuffer(PyObject* exporter, bool copy) diff --git a/src/interfaces/python/swig_typemaps.i b/src/interfaces/python/swig_typemaps.i index d7dfa9c0e3d..bb2025061b2 100644 --- a/src/interfaces/python/swig_typemaps.i +++ b/src/interfaces/python/swig_typemaps.i @@ -561,7 +561,7 @@ static bool spvector_to_numpy(PyObject* &obj, SGSparseVector sg_vector, in %} -/* CFeatures to ... */ +/* Features to ... */ %define FEATURES_BY_TYPECODE(obj, f, type, typecode) switch (typecode) { case F_BOOL: @@ -601,7 +601,7 @@ static bool spvector_to_numpy(PyObject* &obj, SGSparseVector sg_vector, in obj=SWIG_NewPointerObj(f, $descriptor(type *), SWIG_POINTER_EXCEPTION); break; default: - obj=SWIG_NewPointerObj(f, $descriptor(shogun::CFeatures*), SWIG_POINTER_EXCEPTION); + obj=SWIG_NewPointerObj(f, $descriptor(shogun::Features*), SWIG_POINTER_EXCEPTION); break; } %enddef diff --git a/src/interfaces/ruby/swig_typemaps.i b/src/interfaces/ruby/swig_typemaps.i index 81be6329034..d6ec2743602 100644 --- a/src/interfaces/ruby/swig_typemaps.i +++ b/src/interfaces/ruby/swig_typemaps.i @@ -186,14 +186,14 @@ TYPEMAP_SGMATRIX(float64_t, NUM2DBL, rb_float_new) strings.back().vlen = len; const char *str = StringValuePtr(arr); - max_len = shogun::CMath::max(len, max_len); + max_len = shogun::Math::max(len, max_len); sg_memcpy(strings.back().vector, str, len + 1); } else { if (TYPE(arr) == T_ARRAY) { len = RARRAY_LEN(arr); - max_len = shogun::CMath::max(len, max_len); + max_len = shogun::Math::max(len, max_len); strings.emplace_back(len); for (j = 0; j < len; j++) { diff --git a/src/interfaces/swig/Classifier.i b/src/interfaces/swig/Classifier.i index 942e507fb01..355d8644c91 100644 --- a/src/interfaces/swig/Classifier.i +++ b/src/interfaces/swig/Classifier.i @@ -12,7 +12,7 @@ #if defined(USE_SWIG_DIRECTORS) && defined(SWIGPYTHON) %feature("director") shogun::CDirectorLinearMachine; -%feature("director") shogun::CDirectorKernelMachine; +%feature("director") shogun::DirectorKernelMachine; %feature("director:except") { if ($error != NULL) { throw Swig::DirectorMethodException(); @@ -21,51 +21,45 @@ #endif /* Remove C Prefix */ -%rename(Machine) CMachine; -%rename(KernelMachine) CKernelMachine; -%rename(GNPPSVM) CGNPPSVM; +%shared_ptr(shogun::Machine) +%shared_ptr(shogun::KernelMachine) +%shared_ptr(shogun::SVM) +%shared_ptr(shogun::GNPPSVM) #ifdef USE_GPL_SHOGUN -%rename(GPBTSVM) CGPBTSVM; +%shared_ptr(shogun::GPBTSVM) #endif //USE_GPL_SHOGUN -%rename(LDA) CLDA; -%rename(LinearMachine) CLinearMachine; -%rename(OnlineLinearMachine) COnlineLinearMachine; -%rename(LPBoost) CLPBoost; -%rename(LPM) CLPM; -%rename(MPDSVM) CMPDSVM; -%rename(OnlineSVMSGD) COnlineSVMSGD; -%rename(Perceptron) CPerceptron; -%rename(AveragedPerceptron) CAveragedPerceptron; +%shared_ptr(shogun::LDA) +%shared_ptr(shogun::LinearMachine) +%shared_ptr(shogun::OnlineLinearMachine) +%shared_ptr(shogun::LPBoost) +%shared_ptr(shogun::LPM) +%shared_ptr(shogun::MPDSVM) +%shared_ptr(shogun::OnlineSVMSGD) +%shared_ptr(shogun::Perceptron) +%shared_ptr(shogun::AveragedPerceptron) #ifndef HAVE_PYTHON -%rename(SVM) CSVM; +%shared_ptr(shogun::SVM) #endif #ifdef USE_GPL_SHOGUN -%rename(SVMLin) CSVMLin; -%rename(SVMOcas) CSVMOcas; +%shared_ptr(shogun::SVMLin) +%shared_ptr(shogun::SVMOcas) #endif //USE_GPL_SHOGUN -%rename(SVMSGD) CSVMSGD; -%rename(SGDQN) CSGDQN; +%shared_ptr(shogun::SVMSGD) +%shared_ptr(shogun::SGDQN) #ifdef USE_GPL_SHOGUN -%rename(WDSVMOcas) CWDSVMOcas; +%shared_ptr(shogun::WDSVMOcas) #endif //USE_GPL_SHOGUN -%rename(PluginEstimate) CPluginEstimate; -%rename(MKL) CMKL; -%rename(MKLClassification) CMKLClassification; -%rename(MKLOneClass) CMKLOneClass; -%rename(VowpalWabbit) CVowpalWabbit; +%shared_ptr(shogun::PluginEstimate) +%shared_ptr(shogun::MKL) +%shared_ptr(shogun::MKLClassification) +%shared_ptr(shogun::MKLOneClass) +%shared_ptr(shogun::VowpalWabbit) #ifdef USE_GPL_SHOGUN -%rename(FeatureBlockLogisticRegression) CFeatureBlockLogisticRegression; +%shared_ptr(shogun::FeatureBlockLogisticRegression) #endif //USE_GPL_SHOGUN -%rename(DirectorLinearMachine) CDirectorLinearMachine; -%rename(DirectorKernelMachine) CDirectorKernelMachine; -%rename(BaggingMachine) CBaggingMachine; - -/* These functions return new Objects */ -%newobject apply(); -%newobject apply(CFeatures* data); -%newobject apply_locked(const SGVector& indices); -%newobject classify(); -%newobject classify(CFeatures* data); +%shared_ptr(shogun::DirectorLinearMachine) +%shared_ptr(shogun::DirectorKernelMachine) +%shared_ptr(shogun::BaggingMachine) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Clustering.i b/src/interfaces/swig/Clustering.i index 28deb9b29cd..11ecaf86858 100644 --- a/src/interfaces/swig/Clustering.i +++ b/src/interfaces/swig/Clustering.i @@ -12,12 +12,12 @@ #endif /* Remove C Prefix */ -%rename(DistanceMachine) CDistanceMachine; -%rename(Hierarchical) CHierarchical; -%rename(KMeans) CKMeans; -%rename(KMeansBase) CKMeansBase; -%rename(KMeansMiniBatch) CKMeansMiniBatch; -%rename(GMM) CGMM; +%shared_ptr(shogun::DistanceMachine) +%shared_ptr(shogun::Hierarchical) +%shared_ptr(shogun::KMeans) +%shared_ptr(shogun::KMeansBase) +%shared_ptr(shogun::KMeansMiniBatch) +%shared_ptr(shogun::GMM) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Converter.i b/src/interfaces/swig/Converter.i index 66ba4844b69..c9bdb9bc238 100644 --- a/src/interfaces/swig/Converter.i +++ b/src/interfaces/swig/Converter.i @@ -4,10 +4,8 @@ * Authors: Sergey Lisitsyn */ -%rename(EmbeddingConverter) CEmbeddingConverter; - -%newobject shogun::*::embed_kernel; -%newobject shogun::*::embed_distance; +%shared_ptr(shogun::Converter) +%shared_ptr(shogun::EmbeddingConverter) %include %include diff --git a/src/interfaces/swig/Distance.i b/src/interfaces/swig/Distance.i index f04b410e1e3..a3ce6301336 100644 --- a/src/interfaces/swig/Distance.i +++ b/src/interfaces/swig/Distance.i @@ -9,7 +9,7 @@ #endif #if defined(USE_SWIG_DIRECTORS) && defined(SWIGPYTHON) -%feature("director") shogun::CDirectorDistance; +%feature("director") shogun::DirectorDistance; %feature("director:except") { if ($error != NULL) { throw Swig::DirectorMethodException(); @@ -18,8 +18,33 @@ #endif /* Remove C Prefix */ -%rename(Distance) CDistance; -%rename(CustomDistance) CCustomDistance; +%shared_ptr(shogun::Distance) +%shared_ptr(shogun::CustomDistance) +%shared_ptr(shogun::RealDistance) +#ifdef USE_CHAR + %shared_ptr(shogun::DenseDistance) + %shared_ptr(shogun::SparseDistance) + %shared_ptr(shogun::StringDistance) +#endif +#ifdef USE_UINT16 + %shared_ptr(shogun::DenseDistance) + %shared_ptr(shogun::SparseDistance) + %shared_ptr(shogun::StringDistance) +#endif +#ifdef USE_INT32 + %shared_ptr(shogun::DenseDistance) + %shared_ptr(shogun::SparseDistance) + %shared_ptr(shogun::StringDistance) +#endif +#ifdef USE_UINT64 + %shared_ptr(shogun::StringDistance) +#endif +#ifdef USE_FLOAT64 + %shared_ptr(shogun::DenseDistance) + %shared_ptr(shogun::SparseDistance) + %shared_ptr(shogun::StringDistance) +#endif + /* Include Class Headers to make them visible from within the target language */ %include @@ -30,16 +55,16 @@ namespace shogun { #ifdef USE_CHAR - %template(DenseCharDistance) CDenseDistance; + %template(DenseCharDistance) DenseDistance; #endif #ifdef USE_UINT16 - %template(DenseWordDistance) CDenseDistance; + %template(DenseWordDistance) DenseDistance; #endif #ifdef USE_INT32 - %template(DenseIntDistance) CDenseDistance; + %template(DenseIntDistance) DenseDistance; #endif #ifdef USE_FLOAT64 - %template(DenseRealDistance) CDenseDistance; + %template(DenseRealDistance) DenseDistance; #endif } @@ -49,16 +74,16 @@ namespace shogun namespace shogun { #ifdef USE_CHAR - %template(SparseCharDistance) CSparseDistance; + %template(SparseCharDistance) SparseDistance; #endif #ifdef USE_UINT16 - %template(SparseWordDistance) CSparseDistance; + %template(SparseWordDistance) SparseDistance; #endif #ifdef USE_INT32 - %template(SparseIntDistance) CSparseDistance; + %template(SparseIntDistance) SparseDistance; #endif #ifdef USE_FLOAT64 - %template(SparseRealDistance) CSparseDistance; + %template(SparseRealDistance) SparseDistance; #endif } @@ -67,19 +92,19 @@ namespace shogun namespace shogun { #ifdef USE_CHAR - %template(StringCharDistance) CStringDistance; + %template(StringCharDistance) StringDistance; #endif #ifdef USE_UINT16 - %template(StringWordDistance) CStringDistance; + %template(StringWordDistance) StringDistance; #endif #ifdef USE_INT32 - %template(StringIntDistance) CStringDistance; + %template(StringIntDistance) StringDistance; #endif #ifdef USE_UINT64 - %template(StringUlongDistance) CStringDistance; + %template(StringUlongDistance) StringDistance; #endif #ifdef USE_FLOAT64 - %template(StringRealDistance) CStringDistance; + %template(StringRealDistance) StringDistance; #endif } diff --git a/src/interfaces/swig/Distribution.i b/src/interfaces/swig/Distribution.i index 82841e9b0d4..3f3a62fa8dc 100644 --- a/src/interfaces/swig/Distribution.i +++ b/src/interfaces/swig/Distribution.i @@ -11,14 +11,13 @@ %feature("autodoc", "get_transition_probs(self) -> numpy 1dim array of %float") get_transition_probs; #endif -/* Remove C Prefix */ -%rename(Distribution) CDistribution; -%rename(HMM) CHMM; -%rename(PositionalPWM) CPositionalPWM; -%rename(Gaussian) CGaussian; -%rename(GMM) CGMM; -%rename(KernelDensity) CKernelDensity; -%rename(GaussianDistribution) CGaussianDistribution; +%shared_ptr(shogun::Distribution) +%shared_ptr(shogun::HMM) +%shared_ptr(shogun::PositionalPWM) +%shared_ptr(shogun::Gaussian) +%shared_ptr(shogun::GMM) +%shared_ptr(shogun::KernelDensity) +%shared_ptr(shogun::GaussianDistribution) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Ensemble.i b/src/interfaces/swig/Ensemble.i index 367c64b63ca..9ad27129932 100644 --- a/src/interfaces/swig/Ensemble.i +++ b/src/interfaces/swig/Ensemble.i @@ -5,13 +5,13 @@ */ /* Remove C Prefix */ -%rename(CombinationRule) CCombinationRule; +%shared_ptr(shogun::CombinationRule) -%rename(WeightedMajorityVote) CWeightedMajorityVote; +%shared_ptr(shogun::WeightedMajorityVote) -%rename(MajorityVote) CMajorityVote; +%shared_ptr(shogun::MajorityVote) -%rename(MeanRule) CMeanRule; +%shared_ptr(shogun::MeanRule) /* Include Class Headers to make them visible from within the target language */ diff --git a/src/interfaces/swig/Evaluation.i b/src/interfaces/swig/Evaluation.i index 23d6dcf2057..684e1adf1af 100644 --- a/src/interfaces/swig/Evaluation.i +++ b/src/interfaces/swig/Evaluation.i @@ -4,21 +4,17 @@ * Authors: Giovanni De Toni, Sahil Chaddha, Sergey Lisitsyn */ -/* These functions return new Objects */ -%newobject CMachineEvaluation::evaluate(); - #if defined(USE_SWIG_DIRECTORS) && defined(SWIGPYTHON) -%feature("director") shogun::CDirectorContingencyTableEvaluation; +%feature("director") shogun::DirectorContingencyTableEvaluation; #endif -/* Remove C Prefix */ -%rename(Evaluation) CEvaluation; -%rename(EvaluationResult) CEvaluationResult; -%rename(ClusteringAccuracy) CClusteringAccuracy; -%rename(ClusteringMutualInformation) CClusteringMutualInformation; -%rename(DifferentiableFunction) CDifferentiableFunction; -%rename(MachineEvaluation) CMachineEvaluation; -%rename(SplittingStrategy) CSplittingStrategy; +%shared_ptr(shogun::Evaluation) +%shared_ptr(shogun::EvaluationResult) +%shared_ptr(shogun::ClusteringAccuracy) +%shared_ptr(shogun::ClusteringMutualInformation) +%shared_ptr(shogun::DifferentiableFunction) +%shared_ptr(shogun::MachineEvaluation) +%shared_ptr(shogun::SplittingStrategy) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Features.i b/src/interfaces/swig/Features.i index 33f6b16937e..253e02a5c07 100644 --- a/src/interfaces/swig/Features.i +++ b/src/interfaces/swig/Features.i @@ -13,15 +13,8 @@ %feature("autodoc", "get_labels(self) -> numpy 1dim array of float") get_labels; #endif -/* These functions return new Objects */ -%newobject get_transposed(); -%newobject create_merged_copy(CFeatures* other); -%newobject copy_subset(SGVector indices); -%newobject copy_dimension_subset(SGVector indices); -%newobject get_streamed_features(index_t num_elements); - #if defined(USE_SWIG_DIRECTORS) && defined(SWIGPYTHON) -%feature("director") shogun::CDirectorDotFeatures; +%feature("director") shogun::DirectorDotFeatures; %feature("director:except") { if ($error != NULL) { throw Swig::DirectorMethodException(); @@ -35,55 +28,178 @@ #define EXTEND_DENSEFEATURES(class_name, type_name, typecode) #endif -/* Remove C Prefix */ -%rename(Alphabet) CAlphabet; -%rename(Features) CFeatures; -%rename(StreamingFeatures) CStreamingFeatures; -%rename(DotFeatures) CDotFeatures; -%rename(DirectorDotFeatures) CDirectorDotFeatures; -%rename(BinnedDotFeatures) CBinnedDotFeatures; -%rename(StreamingDotFeatures) CStreamingDotFeatures; +%shared_ptr(shogun::Alphabet) +%shared_ptr(shogun::Features) +%shared_ptr(shogun::AttributeFeatures) +%shared_ptr(shogun::StreamingFeatures) +%shared_ptr(shogun::DotFeatures) +%shared_ptr(shogun::DirectorDotFeatures) +%shared_ptr(shogun::BinnedDotFeatures) +%shared_ptr(shogun::StreamingDotFeatures) + +%shared_ptr(shogun::DummyFeatures) +%shared_ptr(shogun::IndexFeatures) +%shared_ptr(shogun::AttributeFeatures) +%shared_ptr(shogun::CombinedFeatures) +%shared_ptr(shogun::CombinedDotFeatures) +%shared_ptr(shogun::HashedDocDotFeatures) +%shared_ptr(shogun::StreamingHashedDocDotFeatures) +%shared_ptr(shogun::RandomKitchenSinksDotFeatures) +%shared_ptr(shogun::RandomFourierDotFeatures) +%shared_ptr(shogun::Labels) -%rename(DummyFeatures) CDummyFeatures; -%rename(IndexFeatures) CIndexFeatures; -%rename(AttributeFeatures) CAttributeFeatures; -%rename(CombinedFeatures) CCombinedFeatures; -%rename(CombinedDotFeatures) CCombinedDotFeatures; -%rename(HashedDocDotFeatures) CHashedDocDotFeatures; -%rename(StreamingHashedDocDotFeatures) CStreamingHashedDocDotFeatures; -%rename(RandomKitchenSinksDotFeatures) CRandomKitchenSinksDotFeatures; -%rename(RandomFourierDotFeatures) CRandomFourierDotFeatures; -%rename(Labels) CLabels; +PROTOCOLS_DENSELABELS(DenseLabels, DenseLabels, float64_t, "d\0", NPY_FLOAT64) +%shared_ptr(shogun::DenseLabels) -PROTOCOLS_DENSELABELS(CDenseLabels, DenseLabels, float64_t, "d\0", NPY_FLOAT64) -%rename(DenseLabels) CDenseLabels; +PROTOCOLS_DENSELABELS(BinaryLabels, BinaryLabels, float64_t, "d\0", NPY_FLOAT64) +%shared_ptr(shogun::BinaryLabels) -PROTOCOLS_DENSELABELS(CBinaryLabels, BinaryLabels, float64_t, "d\0", NPY_FLOAT64) -%rename(BinaryLabels) CBinaryLabels; +PROTOCOLS_DENSELABELS(MulticlassLabels, MulticlassLabels, float64_t, "d\0", NPY_FLOAT64) +%shared_ptr(shogun::MulticlassLabels) -PROTOCOLS_DENSELABELS(CMulticlassLabels, MulticlassLabels, float64_t, "d\0", NPY_FLOAT64) -%rename(MulticlassLabels) CMulticlassLabels; +PROTOCOLS_DENSELABELS(RegressionLabels, RegressionLabels, float64_t, "d\0", NPY_FLOAT64) +%shared_ptr(shogun::RegressionLabels) -PROTOCOLS_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY_FLOAT64) -%rename(RegressionLabels) CRegressionLabels; +%shared_ptr(shogun::StructuredLabels) +%shared_ptr(shogun::LatentLabels) +%shared_ptr(shogun::MultilabelLabels) +%shared_ptr(shogun::RealFileFeatures) +%shared_ptr(shogun::FKFeatures) +%shared_ptr(shogun::TOPFeatures) +%shared_ptr(shogun::SNPFeatures) +%shared_ptr(shogun::WDFeatures) +%shared_ptr(shogun::HashedWDFeatures) +%shared_ptr(shogun::HashedWDFeaturesTransposed) +%shared_ptr(shogun::PolyFeatures) +%shared_ptr(shogun::SparsePolyFeatures) +%shared_ptr(shogun::LBPPyrDotFeatures) +%shared_ptr(shogun::ExplicitSpecFeatures) +%shared_ptr(shogun::ImplicitWeightedSpecFeatures) +%shared_ptr(shogun::DataGenerator) +%shared_ptr(shogun::LatentFeatures) -%rename(StructuredLabels) CStructuredLabels; -%rename(LatentLabels) CLatentLabels; -%rename(MultilabelLabels) CMultilabelLabels; -%rename(RealFileFeatures) CRealFileFeatures; -%rename(FKFeatures) CFKFeatures; -%rename(TOPFeatures) CTOPFeatures; -%rename(SNPFeatures) CSNPFeatures; -%rename(WDFeatures) CWDFeatures; -%rename(HashedWDFeatures) CHashedWDFeatures; -%rename(HashedWDFeaturesTransposed) CHashedWDFeaturesTransposed; -%rename(PolyFeatures) CPolyFeatures; -%rename(SparsePolyFeatures) CSparsePolyFeatures; -%rename(LBPPyrDotFeatures) CLBPPyrDotFeatures; -%rename(ExplicitSpecFeatures) CExplicitSpecFeatures; -%rename(ImplicitWeightedSpecFeatures) CImplicitWeightedSpecFeatures; -%rename(DataGenerator) CDataGenerator; -%rename(LatentFeatures) CLatentFeatures; +#ifdef USE_BOOL + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_CHAR + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_UINT8 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_INT16 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_UINT16 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_INT32 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_UINT32 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_INT64 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_UINT64 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_FLOAT32 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif +#ifdef USE_FLOAT64 + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) + %shared_ptr(shogun::DenseFeatures); +#endif +#ifdef USE_FLOATMAX + %shared_ptr(shogun::StringFeatures) + %shared_ptr(shogun::StreamingStringFeatures) + %shared_ptr(shogun::StringFileFeatures) + %shared_ptr(shogun::SparseFeatures) + %shared_ptr(shogun::StreamingSparseFeatures) + %shared_ptr(shogun::StreamingDenseFeatures) + %shared_ptr(shogun::DenseSubsetFeatures) + %shared_ptr(shogun::MatrixFeatures) +#endif /* Include Class Headers to make them visible from within the target language */ %include @@ -99,8 +215,8 @@ PROTOCOLS_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY %include /* FIXME: DenseFeatures are still included/defined since certain classes - * inherit from them (e.g. TOPKernel : CDenseFeatures), as otherwise - * SWIG will not treat those as subclasses of CFeatures. Once all those classes + * inherit from them (e.g. TOPKernel : DenseFeatures), as otherwise + * SWIG will not treat those as subclasses of Features. Once all those classes * are instantiated w factories, this hack can be removed. In order prevent use * of RealFeatures etc, they are named differently (it only needs to be visible, * but is not used) @@ -109,7 +225,7 @@ PROTOCOLS_DENSELABELS(CRegressionLabels, RegressionLabels, float64_t, "d\0", NPY namespace shogun { #ifdef USE_FLOAT64 - %template(RealFeaturesDEPRECATED) CDenseFeatures; + %template(RealFeaturesDEPRECATED) DenseFeatures; #endif } @@ -118,40 +234,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(StringBoolFeatures) CStringFeatures; + %template(StringBoolFeatures) StringFeatures; #endif #ifdef USE_CHAR - %template(StringCharFeatures) CStringFeatures; + %template(StringCharFeatures) StringFeatures; #endif #ifdef USE_UINT8 - %template(StringByteFeatures) CStringFeatures; + %template(StringByteFeatures) StringFeatures; #endif #ifdef USE_INT16 - %template(StringShortFeatures) CStringFeatures; + %template(StringShortFeatures) StringFeatures; #endif #ifdef USE_UINT16 - %template(StringWordFeatures) CStringFeatures; + %template(StringWordFeatures) StringFeatures; #endif #ifdef USE_INT32 - %template(StringIntFeatures) CStringFeatures; + %template(StringIntFeatures) StringFeatures; #endif #ifdef USE_UINT32 - %template(StringUIntFeatures) CStringFeatures; + %template(StringUIntFeatures) StringFeatures; #endif #ifdef USE_INT64 - %template(StringLongFeatures) CStringFeatures; + %template(StringLongFeatures) StringFeatures; #endif #ifdef USE_UINT64 - %template(StringUlongFeatures) CStringFeatures; + %template(StringUlongFeatures) StringFeatures; #endif #ifdef USE_FLOAT32 - %template(StringShortRealFeatures) CStringFeatures; + %template(StringShortRealFeatures) StringFeatures; #endif #ifdef USE_FLOAT64 - %template(StringRealFeatures) CStringFeatures; + %template(StringRealFeatures) StringFeatures; #endif #ifdef USE_FLOATMAX - %template(StringLongRealFeatures) CStringFeatures; + %template(StringLongRealFeatures) StringFeatures; #endif } @@ -160,40 +276,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(StreamingStringBoolFeatures) CStreamingStringFeatures; + %template(StreamingStringBoolFeatures) StreamingStringFeatures; #endif #ifdef USE_CHAR - %template(StreamingStringCharFeatures) CStreamingStringFeatures; + %template(StreamingStringCharFeatures) StreamingStringFeatures; #endif #ifdef USE_UINT8 - %template(StreamingStringByteFeatures) CStreamingStringFeatures; + %template(StreamingStringByteFeatures) StreamingStringFeatures; #endif #ifdef USE_INT16 - %template(StreamingStringShortFeatures) CStreamingStringFeatures; + %template(StreamingStringShortFeatures) StreamingStringFeatures; #endif #ifdef USE_UINT16 - %template(StreamingStringWordFeatures) CStreamingStringFeatures; + %template(StreamingStringWordFeatures) StreamingStringFeatures; #endif #ifdef USE_INT32 - %template(StreamingStringIntFeatures) CStreamingStringFeatures; + %template(StreamingStringIntFeatures) StreamingStringFeatures; #endif #ifdef USE_UINT32 - %template(StreamingStringUIntFeatures) CStreamingStringFeatures; + %template(StreamingStringUIntFeatures) StreamingStringFeatures; #endif #ifdef USE_INT64 - %template(StreamingStringLongFeatures) CStreamingStringFeatures; + %template(StreamingStringLongFeatures) StreamingStringFeatures; #endif #ifdef USE_UINT64 - %template(StreamingStringUlongFeatures) CStreamingStringFeatures; + %template(StreamingStringUlongFeatures) StreamingStringFeatures; #endif #ifdef USE_FLOAT32 - %template(StreamingStringShortRealFeatures) CStreamingStringFeatures; + %template(StreamingStringShortRealFeatures) StreamingStringFeatures; #endif #ifdef USE_FLOAT64 - %template(StreamingStringRealFeatures) CStreamingStringFeatures; + %template(StreamingStringRealFeatures) StreamingStringFeatures; #endif #ifdef USE_FLOATMAX - %template(StreamingStringLongRealFeatures) CStreamingStringFeatures; + %template(StreamingStringLongRealFeatures) StreamingStringFeatures; #endif } @@ -202,40 +318,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(StringFileBoolFeatures) CStringFileFeatures; + %template(StringFileBoolFeatures) StringFileFeatures; #endif #ifdef USE_CHAR - %template(StringFileCharFeatures) CStringFileFeatures; + %template(StringFileCharFeatures) StringFileFeatures; #endif #ifdef USE_UINT8 - %template(StringFileByteFeatures) CStringFileFeatures; + %template(StringFileByteFeatures) StringFileFeatures; #endif #ifdef USE_INT16 - %template(StringFileShortFeatures) CStringFileFeatures; + %template(StringFileShortFeatures) StringFileFeatures; #endif #ifdef USE_UINT16 - %template(StringFileWordFeatures) CStringFileFeatures; + %template(StringFileWordFeatures) StringFileFeatures; #endif #ifdef USE_INT32 - %template(StringFileIntFeatures) CStringFileFeatures; + %template(StringFileIntFeatures) StringFileFeatures; #endif #ifdef USE_UINT32 - %template(StringFileUIntFeatures) CStringFileFeatures; + %template(StringFileUIntFeatures) StringFileFeatures; #endif #ifdef USE_INT64 - %template(StringFileLongFeatures) CStringFileFeatures; + %template(StringFileLongFeatures) StringFileFeatures; #endif #ifdef USE_UINT64 - %template(StringFileUlongFeatures) CStringFileFeatures; + %template(StringFileUlongFeatures) StringFileFeatures; #endif #ifdef USE_FLOAT32 - %template(StringFileShortRealFeatures) CStringFileFeatures; + %template(StringFileShortRealFeatures) StringFileFeatures; #endif #ifdef USE_FLOAT64 - %template(StringFileRealFeatures) CStringFileFeatures; + %template(StringFileRealFeatures) StringFileFeatures; #endif #ifdef USE_FLOATMAX - %template(StringFileLongRealFeatures) CStringFileFeatures; + %template(StringFileLongRealFeatures) StringFileFeatures; #endif } @@ -244,40 +360,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(SparseBoolFeatures) CSparseFeatures; + %template(SparseBoolFeatures) SparseFeatures; #endif #ifdef USE_CHAR - %template(SparseCharFeatures) CSparseFeatures; + %template(SparseCharFeatures) SparseFeatures; #endif #ifdef USE_UINT8 - %template(SparseByteFeatures) CSparseFeatures; + %template(SparseByteFeatures) SparseFeatures; #endif #ifdef USE_INT16 - %template(SparseShortFeatures) CSparseFeatures; + %template(SparseShortFeatures) SparseFeatures; #endif #ifdef USE_UINT16 - %template(SparseWordFeatures) CSparseFeatures; + %template(SparseWordFeatures) SparseFeatures; #endif #ifdef USE_INT32 - %template(SparseIntFeatures) CSparseFeatures; + %template(SparseIntFeatures) SparseFeatures; #endif #ifdef USE_UINT32 - %template(SparseUIntFeatures) CSparseFeatures; + %template(SparseUIntFeatures) SparseFeatures; #endif #ifdef USE_INT64 - %template(SparseLongFeatures) CSparseFeatures; + %template(SparseLongFeatures) SparseFeatures; #endif #ifdef USE_UINT64 - %template(SparseUlongFeatures) CSparseFeatures; + %template(SparseUlongFeatures) SparseFeatures; #endif #ifdef USE_FLOAT32 - %template(SparseShortRealFeatures) CSparseFeatures; + %template(SparseShortRealFeatures) SparseFeatures; #endif #ifdef USE_FLOAT64 - %template(SparseRealFeatures) CSparseFeatures; + %template(SparseRealFeatures) SparseFeatures; #endif #ifdef USE_FLOATMAX - %template(SparseLongRealFeatures) CSparseFeatures; + %template(SparseLongRealFeatures) SparseFeatures; #endif } @@ -286,40 +402,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(StreamingSparseBoolFeatures) CStreamingSparseFeatures; + %template(StreamingSparseBoolFeatures) StreamingSparseFeatures; #endif #ifdef USE_CHAR - %template(StreamingSparseCharFeatures) CStreamingSparseFeatures; + %template(StreamingSparseCharFeatures) StreamingSparseFeatures; #endif #ifdef USE_UINT8 - %template(StreamingSparseByteFeatures) CStreamingSparseFeatures; + %template(StreamingSparseByteFeatures) StreamingSparseFeatures; #endif #ifdef USE_INT16 - %template(StreamingSparseShortFeatures) CStreamingSparseFeatures; + %template(StreamingSparseShortFeatures) StreamingSparseFeatures; #endif #ifdef USE_UINT16 - %template(StreamingSparseWordFeatures) CStreamingSparseFeatures; + %template(StreamingSparseWordFeatures) StreamingSparseFeatures; #endif #ifdef USE_INT32 - %template(StreamingSparseIntFeatures) CStreamingSparseFeatures; + %template(StreamingSparseIntFeatures) StreamingSparseFeatures; #endif #ifdef USE_UINT32 - %template(StreamingSparseUIntFeatures) CStreamingSparseFeatures; + %template(StreamingSparseUIntFeatures) StreamingSparseFeatures; #endif #ifdef USE_INT64 - %template(StreamingSparseLongFeatures) CStreamingSparseFeatures; + %template(StreamingSparseLongFeatures) StreamingSparseFeatures; #endif #ifdef USE_UINT64 - %template(StreamingSparseUlongFeatures) CStreamingSparseFeatures; + %template(StreamingSparseUlongFeatures) StreamingSparseFeatures; #endif #ifdef USE_FLOAT32 - %template(StreamingSparseShortRealFeatures) CStreamingSparseFeatures; + %template(StreamingSparseShortRealFeatures) StreamingSparseFeatures; #endif #ifdef USE_FLOAT64 - %template(StreamingSparseRealFeatures) CStreamingSparseFeatures; + %template(StreamingSparseRealFeatures) StreamingSparseFeatures; #endif #ifdef USE_FLOATMAX - %template(StreamingSparseLongRealFeatures) CStreamingSparseFeatures; + %template(StreamingSparseLongRealFeatures) StreamingSparseFeatures; #endif } @@ -328,52 +444,52 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(StreamingBoolFeatures) CStreamingDenseFeatures; + %template(StreamingBoolFeatures) StreamingDenseFeatures; #endif #ifdef USE_CHAR - %template(StreamingCharFeatures) CStreamingDenseFeatures; + %template(StreamingCharFeatures) StreamingDenseFeatures; #endif #ifdef USE_UINT8 - %template(StreamingByteFeatures) CStreamingDenseFeatures; + %template(StreamingByteFeatures) StreamingDenseFeatures; #endif #ifdef USE_UINT16 - %template(StreamingWordFeatures) CStreamingDenseFeatures; + %template(StreamingWordFeatures) StreamingDenseFeatures; #endif #ifdef USE_INT16 - %template(StreamingShortFeatures) CStreamingDenseFeatures; + %template(StreamingShortFeatures) StreamingDenseFeatures; #endif #ifdef USE_INT32 - %template(StreamingIntFeatures) CStreamingDenseFeatures; + %template(StreamingIntFeatures) StreamingDenseFeatures; #endif #ifdef USE_UINT32 - %template(StreamingUIntFeatures) CStreamingDenseFeatures; + %template(StreamingUIntFeatures) StreamingDenseFeatures; #endif #ifdef USE_INT64 - %template(StreamingLongIntFeatures) CStreamingDenseFeatures; + %template(StreamingLongIntFeatures) StreamingDenseFeatures; #endif #ifdef USE_UINT64 - %template(StreamingULongIntFeatures) CStreamingDenseFeatures; + %template(StreamingULongIntFeatures) StreamingDenseFeatures; #endif #ifdef USE_FLOATMAX - %template(StreamingLongRealFeatures) CStreamingDenseFeatures; + %template(StreamingLongRealFeatures) StreamingDenseFeatures; #endif #ifdef USE_FLOAT32 - %template(StreamingShortRealFeatures) CStreamingDenseFeatures; + %template(StreamingShortRealFeatures) StreamingDenseFeatures; #endif #ifdef USE_FLOAT64 - %template(StreamingRealFeatures) CStreamingDenseFeatures; + %template(StreamingRealFeatures) StreamingDenseFeatures; /** Instantiate RandomMixin */ - %template(SeedableStreamingDense) shogun::Seedable>; - %template(RandomMixinStreamingDense) shogun::RandomMixin, std::mt19937_64>; + %template(SeedableStreamingDense) shogun::Seedable>; + %template(RandomMixinStreamingDense) shogun::RandomMixin, std::mt19937_64>; #endif } /* these classes need the above typed CFeature definitions */ -%rename(MeanShiftDataGenerator) CMeanShiftDataGenerator; +%shared_ptr(shogun::MeanShiftDataGenerator) %include -%rename(GaussianBlobsDataGenerator) CGaussianBlobsDataGenerator; +%shared_ptr(shogun::GaussianBlobsDataGenerator) %include /* Templated Class DenseSubsetFeatures */ @@ -381,40 +497,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(BoolSubsetFeatures) CDenseSubsetFeatures; + %template(BoolSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_CHAR - %template(CharSubsetFeatures) CDenseSubsetFeatures; + %template(CharSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_UINT8 - %template(ByteSubsetFeatures) CDenseSubsetFeatures; + %template(ByteSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_UINT16 - %template(WordSubsetFeatures) CDenseSubsetFeatures; + %template(WordSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_INT16 - %template(ShortSubsetFeatures) CDenseSubsetFeatures; + %template(ShortSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_INT32 - %template(IntSubsetFeatures) CDenseSubsetFeatures; + %template(IntSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_UINT32 - %template(UIntSubsetFeatures) CDenseSubsetFeatures; + %template(UIntSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_INT64 - %template(LongIntSubsetFeatures) CDenseSubsetFeatures; + %template(LongIntSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_UINT64 - %template(ULongIntSubsetFeatures) CDenseSubsetFeatures; + %template(ULongIntSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_FLOATMAX - %template(LongRealSubsetFeatures) CDenseSubsetFeatures; + %template(LongRealSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_FLOAT32 - %template(ShortRealSubsetFeatures) CDenseSubsetFeatures; + %template(ShortRealSubsetFeatures) DenseSubsetFeatures; #endif #ifdef USE_FLOAT64 - %template(RealSubsetFeatures) CDenseSubsetFeatures; + %template(RealSubsetFeatures) DenseSubsetFeatures; #endif } @@ -457,39 +573,39 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(BoolMatrixFeatures) CMatrixFeatures; + %template(BoolMatrixFeatures) MatrixFeatures; #endif #ifdef USE_CHAR - %template(CharMatrixFeatures) CMatrixFeatures; + %template(CharMatrixFeatures) MatrixFeatures; #endif #ifdef USE_UINT8 - %template(ByteMatrixFeatures) CMatrixFeatures; + %template(ByteMatrixFeatures) MatrixFeatures; #endif #ifdef USE_UINT16 - %template(WordMatrixFeatures) CMatrixFeatures; + %template(WordMatrixFeatures) MatrixFeatures; #endif #ifdef USE_INT16 - %template(ShortMatrixFeatures) CMatrixFeatures; + %template(ShortMatrixFeatures) MatrixFeatures; #endif #ifdef USE_INT32 - %template(IntMatrixFeatures) CMatrixFeatures; + %template(IntMatrixFeatures) MatrixFeatures; #endif #ifdef USE_UINT32 - %template(UIntMatrixFeatures) CMatrixFeatures; + %template(UIntMatrixFeatures) MatrixFeatures; #endif #ifdef USE_INT64 - %template(LongIntMatrixFeatures) CMatrixFeatures; + %template(LongIntMatrixFeatures) MatrixFeatures; #endif #ifdef USE_UINT64 - %template(ULongIntMatrixFeatures) CMatrixFeatures; + %template(ULongIntMatrixFeatures) MatrixFeatures; #endif #ifdef USE_FLOATMAX - %template(LongRealMatrixFeatures) CMatrixFeatures; + %template(LongRealMatrixFeatures) MatrixFeatures; #endif #ifdef USE_FLOAT32 - %template(ShortRealMatrixFeatures) CMatrixFeatures; + %template(ShortRealMatrixFeatures) MatrixFeatures; #endif #ifdef USE_FLOAT64 - %template(RealMatrixFeatures) CMatrixFeatures; + %template(RealMatrixFeatures) MatrixFeatures; #endif } diff --git a/src/interfaces/swig/GaussianProcess.i b/src/interfaces/swig/GaussianProcess.i index 613efda5721..07ec23e83c1 100644 --- a/src/interfaces/swig/GaussianProcess.i +++ b/src/interfaces/swig/GaussianProcess.i @@ -5,52 +5,52 @@ */ /* Remove C Prefix */ -%rename(MeanFunction) CMeanFunction; -%rename(ZeroMean) CZeroMean; -%rename(ConstMean) CConstMean; +%shared_ptr(shogun::MeanFunction) +%shared_ptr(shogun::ZeroMean) +%shared_ptr(shogun::ConstMean) -%rename(Inference) CInference; -%rename(ExactInferenceMethod) CExactInferenceMethod; -%rename(LaplaceInference) CLaplaceInference; -%rename(SparseInference) CSparseInference; -%rename(SingleSparseInference) CSingleSparseInference; -%rename(SingleFITCInference) CSingleFITCInference; -%rename(SingleLaplaceInferenceMethod) CSingleLaplaceInferenceMethod; -%rename(MultiLaplaceInferenceMethod) CMultiLaplaceInferenceMethod; -%rename(FITCInferenceMethod) CFITCInferenceMethod; -%rename(SingleFITCLaplaceInferenceMethod) CSingleFITCLaplaceInferenceMethod; -%rename(VarDTCInferenceMethod) CVarDTCInferenceMethod; -%rename(EPInferenceMethod) CEPInferenceMethod; +%shared_ptr(shogun::Inference) +%shared_ptr(shogun::ExactInferenceMethod) +%shared_ptr(shogun::LaplaceInference) +%shared_ptr(shogun::SparseInference) +%shared_ptr(shogun::SingleSparseInference) +%shared_ptr(shogun::SingleFITCInference) +%shared_ptr(shogun::SingleLaplaceInferenceMethod) +%shared_ptr(shogun::MultiLaplaceInferenceMethod) +%shared_ptr(shogun::FITCInferenceMethod) +%shared_ptr(shogun::SingleFITCLaplaceInferenceMethod) +%shared_ptr(shogun::VarDTCInferenceMethod) +%shared_ptr(shogun::EPInferenceMethod) -%rename(LikelihoodModel) CLikelihoodModel; -%rename(ProbitLikelihood) CProbitLikelihood; -%rename(LogitLikelihood) CLogitLikelihood; -%rename(SoftMaxLikelihood) CSoftMaxLikelihood; -%rename(GaussianLikelihood) CGaussianLikelihood; -%rename(StudentsTLikelihood) CStudentsTLikelihood; +%shared_ptr(shogun::LikelihoodModel) +%shared_ptr(shogun::ProbitLikelihood) +%shared_ptr(shogun::LogitLikelihood) +%shared_ptr(shogun::SoftMaxLikelihood) +%shared_ptr(shogun::GaussianLikelihood) +%shared_ptr(shogun::StudentsTLikelihood) -%rename(VariationalLikelihood) CVariationalLikelihood; -%rename(VariationalGaussianLikelihood) CVariationalGaussianLikelihood; -%rename(NumericalVGLikelihood) CNumericalVGLikelihood; -%rename(DualVariationalGaussianLikelihood) CDualVariationalGaussianLikelihood; -%rename(LogitVGLikelihood) CLogitVGLikelihood; -%rename(LogitVGPiecewiseBoundLikelihood) CLogitVGPiecewiseBoundLikelihood; -%rename(LogitDVGLikelihood) CLogitDVGLikelihood; -%rename(ProbitVGLikelihood) CProbitVGLikelihood; -%rename(StudentsTVGLikelihood) CStudentsTVGLikelihood; +%shared_ptr(shogun::VariationalLikelihood) +%shared_ptr(shogun::VariationalGaussianLikelihood) +%shared_ptr(shogun::NumericalVGLikelihood) +%shared_ptr(shogun::DualVariationalGaussianLikelihood) +%shared_ptr(shogun::LogitVGLikelihood) +%shared_ptr(shogun::LogitVGPiecewiseBoundLikelihood) +%shared_ptr(shogun::LogitDVGLikelihood) +%shared_ptr(shogun::ProbitVGLikelihood) +%shared_ptr(shogun::StudentsTVGLikelihood) -%rename(KLInference) CKLInference; -%rename(KLLowerTriangularInference) CKLLowerTriangularInference; -%rename(KLCovarianceInferenceMethod) CKLCovarianceInferenceMethod; -%rename(KLDiagonalInferenceMethod) CKLDiagonalInferenceMethod; -%rename(KLCholeskyInferenceMethod) CKLCholeskyInferenceMethod; -%rename(KLDualInferenceMethod) CKLDualInferenceMethod; +%shared_ptr(shogun::KLInference) +%shared_ptr(shogun::KLLowerTriangularInference) +%shared_ptr(shogun::KLCovarianceInferenceMethod) +%shared_ptr(shogun::KLDiagonalInferenceMethod) +%shared_ptr(shogun::KLCholeskyInferenceMethod) +%shared_ptr(shogun::KLDualInferenceMethod) -%rename(KLDualInferenceMethodMinimizer) CKLDualInferenceMethodMinimizer; +%shared_ptr(shogun::KLDualInferenceMethodMinimizer) -%rename(GaussianProcessMachine) CGaussianProcessMachine; -%rename(GaussianProcessClassification) CGaussianProcessClassification; -%rename(GaussianProcessRegression) CGaussianProcessRegression; +%shared_ptr(shogun::GaussianProcessMachine) +%shared_ptr(shogun::GaussianProcessClassification) +%shared_ptr(shogun::GaussianProcessRegression) /* These functions return new Objects */ diff --git a/src/interfaces/swig/IO.i b/src/interfaces/swig/IO.i index f84cb94a831..80af88e8fe8 100644 --- a/src/interfaces/swig/IO.i +++ b/src/interfaces/swig/IO.i @@ -5,29 +5,77 @@ */ /* Remove C Prefix */ -%rename(IOBuffer) CIOBuffer; +%shared_ptr(shogun::IOBuffer) -%rename(File) CFile; -%rename(StreamingFile) CStreamingFile; -%rename(CSVFile) CCSVFile; -%rename(LibSVMFile) CLibSVMFile; -%rename(Serializer) CSerializer; -%rename(Deserializer) CDeserializer; -%rename(StreamingAsciiFile) CStreamingAsciiFile; -%rename(BitserySerializer) CBitserySerializer; -%rename(BitseryDeserializer) CBitseryDeserializer; -%newobject BitseryDeserializer::read; -%rename(JsonSerializer) CJsonSerializer; -%rename(JsonDeserializer) CJsonDeserializer; -%newobject JsonDeserializer::read; -%rename(ByteArrayInputStream) CByteArrayInputStream; -%rename(ByteArrayOutputStream) CByteArrayOutputStream; +%shared_ptr(shogun::File) +%shared_ptr(shogun::StreamingFile) +%shared_ptr(shogun::CSVFile) +%shared_ptr(shogun::LibSVMFile) +%shared_ptr(shogun::StreamingAsciiFile) +%shared_ptr(shogun::io::Serializer) +%shared_ptr(shogun::io::Deserializer) +%shared_ptr(shogun::io::BitserySerializer) +%shared_ptr(shogun::io::BitseryDeserializer) +%shared_ptr(shogun::io::JsonSerializer) +%shared_ptr(shogun::io::JsonDeserializer) +%shared_ptr(shogun::io::ByteArrayInputStream) +%shared_ptr(shogun::io::ByteArrayOutputStream) -%rename(StreamingFileFromFeatures) CStreamingFileFromFeatures; -%rename(BinaryFile) CBinaryFile; -%rename(HDF5File) CHDF5File; -%rename(SimpleFile) CSimpleFile; -%rename(MemoryMappedFile) CMemoryMappedFile; +%shared_ptr(shogun::StreamingFileFromFeatures) +%shared_ptr(shogun::BinaryFile) +%shared_ptr(shogun::HDF5File) +%shared_ptr(shogun::SimpleFile) +%shared_ptr(shogun::MemoryMappedFile) +%shared_ptr(shogun::Compressor) + +#ifdef USE_BOOL +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_CHAR +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_UINT8 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_INT16 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_UINT16 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_INT32 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_UINT32 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_INT64 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_UINT64 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_FLOAT32 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_FLOAT64 +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif +#ifdef USE_FLOATMAX +%shared_ptr(shogun::StreamingFileFromSparseFeatures) +%shared_ptr(shogun::StreamingFileFromDenseFeatures) +#endif %include %include @@ -38,40 +86,40 @@ namespace shogun { #ifdef USE_BOOL - %template(StreamingFileFromSparseBoolFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseBoolFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_CHAR - %template(StreamingFileFromSparseCharFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseCharFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_UINT8 - %template(StreamingFileFromSparseByteFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseByteFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_INT16 - %template(StreamingFileFromSparseShortFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseShortFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_UINT16 - %template(StreamingFileFromSparseWordFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseWordFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_INT32 - %template(StreamingFileFromSparseIntFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseIntFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_UINT32 - %template(StreamingFileFromSparseUIntFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseUIntFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_INT64 - %template(StreamingFileFromSparseLongFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseLongFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_UINT64 - %template(StreamingFileFromSparseUlongFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseUlongFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_FLOAT32 - %template(StreamingFileFromSparseShortRealFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseShortRealFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_FLOAT64 - %template(StreamingFileFromSparseRealFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseRealFeatures) StreamingFileFromSparseFeatures; #endif #ifdef USE_FLOATMAX - %template(StreamingFileFromSparseLongRealFeatures) CStreamingFileFromSparseFeatures; + %template(StreamingFileFromSparseLongRealFeatures) StreamingFileFromSparseFeatures; #endif } @@ -80,40 +128,40 @@ namespace shogun namespace shogun { #ifdef USE_BOOL - %template(StreamingFileFromBoolFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromBoolFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_CHAR - %template(StreamingFileFromCharFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromCharFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_UINT8 - %template(StreamingFileFromByteFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromByteFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_INT16 - %template(StreamingFileFromShortFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromShortFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_UINT16 - %template(StreamingFileFromWordFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromWordFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_INT32 - %template(StreamingFileFromIntFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromIntFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_UINT32 - %template(StreamingFileFromUIntFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromUIntFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_INT64 - %template(StreamingFileFromLongFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromLongFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_UINT64 - %template(StreamingFileFromUlongFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromUlongFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_FLOAT32 - %template(StreamingFileFromShortRealFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromShortRealFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_FLOAT64 - %template(StreamingFileFromRealFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromRealFeatures) StreamingFileFromDenseFeatures; #endif #ifdef USE_FLOATMAX - %template(StreamingFileFromLongRealFeatures) CStreamingFileFromDenseFeatures; + %template(StreamingFileFromLongRealFeatures) StreamingFileFromDenseFeatures; #endif } diff --git a/src/interfaces/swig/Kernel.i b/src/interfaces/swig/Kernel.i index 26872db1dd2..61074a4ebd0 100644 --- a/src/interfaces/swig/Kernel.i +++ b/src/interfaces/swig/Kernel.i @@ -12,7 +12,7 @@ %ignore CWeightedDegreePositionStringKernel::set_position_weights(float64_t*); #if defined(USE_SWIG_DIRECTORS) && defined(SWIGPYTHON) -%feature("director") shogun::CDirectorKernel; +%feature("director") shogun::DirectorKernel; %feature("director:except") { if ($error != NULL) { throw Swig::DirectorMethodException(); @@ -26,48 +26,112 @@ #endif /* Remove C Prefix */ -%rename(Kernel) CKernel; -%rename(KernelNormalizer) CKernelNormalizer; - -%rename(AvgDiagKernelNormalizer) CAvgDiagKernelNormalizer; -%rename(RidgeKernelNormalizer) CRidgeKernelNormalizer; -%rename(CommUlongStringKernel) CCommUlongStringKernel; -%rename(CommWordStringKernel) CCommWordStringKernel; +%shared_ptr(shogun::Kernel) +%shared_ptr(shogun::KernelNormalizer) +%shared_ptr(shogun::PyramidChi2) +%shared_ptr(shogun::ANOVAKernel) +%shared_ptr(shogun::AUCKernel) +%shared_ptr(shogun::BesselKernel) +%shared_ptr(shogun::AvgDiagKernelNormalizer) +%shared_ptr(shogun::RidgeKernelNormalizer) +%shared_ptr(shogun::CircularKernel) +%shared_ptr(shogun::Chi2Kernel) +%shared_ptr(shogun::CombinedKernel) +%shared_ptr(shogun::ProductKernel) +%shared_ptr(shogun::CommUlongStringKernel) +%shared_ptr(shogun::CommWordStringKernel) +%shared_ptr(shogun::ConstKernel) PROTOCOLS_CUSTOMKERNEL(CustomKernel, float32_t, "f\0", NPY_FLOAT32) -%rename(CustomKernel) CCustomKernel; +%shared_ptr(shogun::CustomKernel) -%rename(DiagKernel) CDiagKernel; +%shared_ptr(shogun::DiagKernel) #ifdef USE_GPL_SHOGUN -%rename(DistantSegmentsKernel) CDistantSegmentsKernel; +%shared_ptr(shogun::DistantSegmentsKernel) #endif //USE_GPL_SHOGUN -%rename(DiceKernelNormalizer) CDiceKernelNormalizer; -%rename(ScatterKernelNormalizer) CScatterKernelNormalizer; -%rename(VarianceKernelNormalizer) CVarianceKernelNormalizer; -%rename(FixedDegreeStringKernel) CFixedDegreeStringKernel; -%rename(DirectorKernel) CDirectorKernel; -%rename(HistogramWordStringKernel) CHistogramWordStringKernel; -%rename(IdentityKernelNormalizer) CIdentityKernelNormalizer; -%rename(LinearStringKernel) CLinearStringKernel; -%rename(SparseSpatialSampleStringKernel) CSparseSpatialSampleStringKernel; -%rename(LocalAlignmentStringKernel) CLocalAlignmentStringKernel; -%rename(LocalityImprovedStringKernel) CLocalityImprovedStringKernel; -%rename(MatchWordStringKernel) CMatchWordStringKernel; -%rename(OligoStringKernel) COligoStringKernel; -%rename(PolyMatchStringKernel) CPolyMatchStringKernel; -%rename(GaussianMatchStringKernel) CGaussianMatchStringKernel; -%rename(SNPStringKernel) CSNPStringKernel; -%rename(RegulatoryModulesStringKernel) CRegulatoryModulesStringKernel; -%rename(PolyMatchWordStringKernel) CPolyMatchWordStringKernel; -%rename(SalzbergWordStringKernel) CSalzbergWordStringKernel; -%rename(SimpleLocalityImprovedStringKernel) CSimpleLocalityImprovedStringKernel; -%rename(SqrtDiagKernelNormalizer) CSqrtDiagKernelNormalizer; -%rename(TanimotoKernelNormalizer) CTanimotoKernelNormalizer; -%rename(WeightedCommWordStringKernel) CWeightedCommWordStringKernel; -%rename(WeightedDegreePositionStringKernel) CWeightedDegreePositionStringKernel; -%rename(WeightedDegreeStringKernel) CWeightedDegreeStringKernel; -%rename(ZeroMeanCenterKernelNormalizer) CZeroMeanCenterKernelNormalizer; -%rename(SubsequenceStringKernel) CSubsequenceStringKernel; +%shared_ptr(shogun::WaveKernel) +%shared_ptr(shogun::CauchyKernel) +%shared_ptr(shogun::DiceKernelNormalizer) +%shared_ptr(shogun::ExponentialKernel) +%shared_ptr(shogun::ScatterKernelNormalizer) +%shared_ptr(shogun::VarianceKernelNormalizer) +%shared_ptr(shogun::DistanceKernel) +%shared_ptr(shogun::FixedDegreeStringKernel) +%shared_ptr(shogun::ShiftInvariantKernel) +%shared_ptr(shogun::GaussianCompactKernel) +%shared_ptr(shogun::DirectorKernel) +%shared_ptr(shogun::WaveletKernel) +%shared_ptr(shogun::GaussianShiftKernel) +%shared_ptr(shogun::GaussianShortRealKernel) +%shared_ptr(shogun::HistogramIntersectionKernel) +%shared_ptr(shogun::HistogramWordStringKernel) +%shared_ptr(shogun::IdentityKernelNormalizer) +%shared_ptr(shogun::InverseMultiQuadricKernel) +%shared_ptr(shogun::LinearKernel) +%shared_ptr(shogun::LinearStringKernel) +%shared_ptr(shogun::SparseSpatialSampleStringKernel) +%shared_ptr(shogun::SplineKernel) +%shared_ptr(shogun::LocalAlignmentStringKernel) +%shared_ptr(shogun::LocalityImprovedStringKernel) +%shared_ptr(shogun::MatchWordStringKernel) +%shared_ptr(shogun::OligoStringKernel) +%shared_ptr(shogun::PolyMatchStringKernel) +%shared_ptr(shogun::PowerKernel) +%shared_ptr(shogun::LogKernel) +%shared_ptr(shogun::GaussianMatchStringKernel) +%shared_ptr(shogun::SNPStringKernel) +%shared_ptr(shogun::RegulatoryModulesStringKernel) +%shared_ptr(shogun::PolyMatchWordStringKernel) +%shared_ptr(shogun::SalzbergWordStringKernel) +%shared_ptr(shogun::SigmoidKernel) +%shared_ptr(shogun::SphericalKernel) +%shared_ptr(shogun::SimpleLocalityImprovedStringKernel) +%shared_ptr(shogun::SqrtDiagKernelNormalizer) +%shared_ptr(shogun::TanimotoKernelNormalizer) +%shared_ptr(shogun::TensorProductPairKernel) +%shared_ptr(shogun::TStudentKernel) +%shared_ptr(shogun::WeightedCommWordStringKernel) +%shared_ptr(shogun::WeightedDegreePositionStringKernel) +%shared_ptr(shogun::WeightedDegreeStringKernel) +%shared_ptr(shogun::WeightedDegreeRBFKernel) +%shared_ptr(shogun::SpectrumMismatchRBFKernel) +%shared_ptr(shogun::ZeroMeanCenterKernelNormalizer) +%shared_ptr(shogun::DotKernel) +%shared_ptr(shogun::RationalQuadraticKernel) +%shared_ptr(shogun::MultiquadricKernel) +%shared_ptr(shogun::JensenShannonKernel) + +%shared_ptr(shogun::ExponentialARDKernel) +%shared_ptr(shogun::GaussianARDKernel) + +%shared_ptr(shogun::GaussianARDSparseKernel) + +%shared_ptr(shogun::SubsequenceStringKernel) +%shared_ptr(shogun::PeriodicKernel) + +#ifdef USE_FLOAT64 + %shared_ptr(shogun::StringKernel) + %shared_ptr(shogun::SparseKernel) +#endif +#ifdef USE_INT16 + %shared_ptr(shogun::StringKernel) +#endif +#ifdef USE_UINT16 + %shared_ptr(shogun::StringKernel) + %shared_ptr(shogun::SparseKernel) +#endif +#ifdef USE_CHAR + %shared_ptr(shogun::StringKernel) +#endif +#ifdef USE_UINT32 + %shared_ptr(shogun::StringKernel) +#endif +#ifdef USE_UINT64 + %shared_ptr(shogun::StringKernel) +#endif +#ifdef USE_UINT8 + %shared_ptr(shogun::StringKernel) +#endif /* Include Class Headers to make them visible from within the target language */ %include @@ -79,10 +143,10 @@ PROTOCOLS_CUSTOMKERNEL(CustomKernel, float32_t, "f\0", NPY_FLOAT32) namespace shogun { #ifdef USE_FLOAT64 - %template(SparseRealKernel) CSparseKernel; + %template(SparseRealKernel) SparseKernel; #endif #ifdef USE_UINT16 - %template(SparseWordKernel) CSparseKernel; + %template(SparseWordKernel) SparseKernel; #endif } @@ -91,25 +155,25 @@ namespace shogun namespace shogun { #ifdef USE_FLOAT64 - %template(StringRealKernel) CStringKernel; + %template(StringRealKernel) StringKernel; #endif #ifdef USE_UINT16 - %template(StringWordKernel) CStringKernel; + %template(StringWordKernel) StringKernel; #endif #ifdef USE_CHAR - %template(StringCharKernel) CStringKernel; + %template(StringCharKernel) StringKernel; #endif #ifdef USE_UINT32 - %template(StringIntKernel) CStringKernel; + %template(StringIntKernel) StringKernel; #endif #ifdef USE_UINT64 - %template(StringUlongKernel) CStringKernel; + %template(StringUlongKernel) StringKernel; #endif -#ifdef USE_UINT16 - %template(StringShortKernel) CStringKernel; +#ifdef USE_INT16 + %template(StringShortKernel) StringKernel; #endif #ifdef USE_UINT8 - %template(StringByteKernel) CStringKernel; + %template(StringByteKernel) StringKernel; #endif } diff --git a/src/interfaces/swig/Latent.i b/src/interfaces/swig/Latent.i index 572dbe3b1ae..a3fb06163d5 100644 --- a/src/interfaces/swig/Latent.i +++ b/src/interfaces/swig/Latent.i @@ -9,13 +9,13 @@ #endif /* Remove C Prefix */ -%rename(LatentModel) CLatentModel; +%shared_ptr(shogun::LatentModel) -%rename(LinearLatentMachine) CLinearLatentMachine; +%shared_ptr(shogun::LinearLatentMachine) -%rename(LatentSVM) CLatentSVM; +%shared_ptr(shogun::LatentSVM) -%rename(DirectorLatentModel) CDirectorLatentModel; +%shared_ptr(shogun::DirectorLatentModel) /* Include Class Headers to make them visible from within the target language */ diff --git a/src/interfaces/swig/Library.i b/src/interfaces/swig/Library.i index 8a7660c6513..916a27fdfe3 100644 --- a/src/interfaces/swig/Library.i +++ b/src/interfaces/swig/Library.i @@ -8,23 +8,54 @@ #define PROTOCOLS_SGVECTOR(class_name, type_name, format_str, typecode) #endif -%rename(Cache) CCache; -%rename(ListElement) CListElement; -%rename(List) CList; -%rename(Signal) CSignal; -%rename(Time) CTime; -%rename(Hash) CHash; -%rename(StructuredData) CStructuredData; -%rename(DynamicObjectArray) CDynamicObjectArray; -%rename(Tokenizer) CTokenizer; -%rename(DelimiterTokenizer) CDelimiterTokenizer; -%rename(NGramTokenizer) CNGramTokenizer; +%shared_ptr(shogun::Cache) +%shared_ptr(shogun::ListElement) +%shared_ptr(shogun::List) +%shared_ptr(shogun::Signal) +%shared_ptr(shogun::Time) +%shared_ptr(shogun::Hash) +%shared_ptr(shogun::StructuredData) +%shared_ptr(shogun::DynamicObjectArray) +%shared_ptr(shogun::Tokenizer) +%shared_ptr(shogun::DelimiterTokenizer) +%shared_ptr(shogun::NGramTokenizer) -%rename(IndexBlock) CIndexBlock; -%rename(IndexBlockRelation) CIndexBlockRelation; -%rename(IndexBlockGroup) CIndexBlockGroup; -%rename(IndexBlockTree) CIndexBlockTree; -%rename(Data) CData; +%shared_ptr(shogun::IndexBlock) +%shared_ptr(shogun::IndexBlockRelation) +%shared_ptr(shogun::IndexBlockGroup) +%shared_ptr(shogun::IndexBlockTree) +%shared_ptr(shogun::Data) + +#ifdef USE_CHAR +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_UINT8 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_INT16 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_UINT16 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_INT32 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_UINT32 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_INT64 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_UINT64 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_FLOAT32 +%shared_ptr(shogun::DynamicArray) +#endif +#ifdef USE_FLOAT64 +%shared_ptr(shogun::DynamicArray) +#endif %ignore RADIX_STACK_SIZE; %ignore NUMTRAPPEDSIGS; @@ -258,44 +289,37 @@ namespace shogun namespace shogun { #ifdef USE_CHAR - %template(DynamicCharArray) CDynamicArray; + %template(DynamicCharArray) DynamicArray; #endif #ifdef USE_UINT8 - %template(DynamicByteArray) CDynamicArray; + %template(DynamicByteArray) DynamicArray; #endif #ifdef USE_INT16 - %template(DynamicShortArray) CDynamicArray; + %template(DynamicShortArray) DynamicArray; #endif #ifdef USE_UINT16 - %template(DynamicWordArray) CDynamicArray; + %template(DynamicWordArray) DynamicArray; #endif #ifdef USE_INT32 - %template(DynamicIntArray) CDynamicArray; + %template(DynamicIntArray) DynamicArray; #endif #ifdef USE_UINT32 - %template(DynamicUIntArray) CDynamicArray; + %template(DynamicUIntArray) DynamicArray; #endif #ifdef USE_INT64 - %template(DynamicLongArray) CDynamicArray; + %template(DynamicLongArray) DynamicArray; #endif #ifdef USE_UINT64 - %template(DynamicULongArray) CDynamicArray; + %template(DynamicULongArray) DynamicArray; #endif #ifdef USE_FLOAT32 - %template(DynamicShortRealArray) CDynamicArray; + %template(DynamicShortRealArray) DynamicArray; #endif #ifdef USE_FLOAT64 - %template(DynamicRealArray) CDynamicArray; + %template(DynamicRealArray) DynamicArray; #endif - %template(DynamicPlifArray) DynArray; -} -/* Template Class GCArray */ -%include -namespace shogun -{ - %template(PlifGCArray) CGCArray; + %template(DynamicPlifArray) DynArray; } - /* Hash */ %include @@ -313,7 +337,7 @@ namespace shogun %include namespace shogun { -%extend CDynamicObjectArray +%extend DynamicObjectArray { template ::value_type>, T>::value>> void append_element_vector(T v, const char* name="") @@ -336,12 +360,12 @@ namespace shogun /* Specialize DynamicObjectArray::append_element function */ #ifdef USE_FLOAT64 - %template(append_element_real_vector) CDynamicObjectArray::append_element_vector, SGVector>; - %template(append_element_real_matrix) CDynamicObjectArray::append_element_matrix, SGMatrix>; - %template(append_element_real) CDynamicObjectArray::append_element; + %template(append_element_real_vector) DynamicObjectArray::append_element_vector, SGVector>; + %template(append_element_real_matrix) DynamicObjectArray::append_element_matrix, SGMatrix>; + %template(append_element_real) DynamicObjectArray::append_element; #ifdef SWIGOCTAVE /* (Octave converts single element arrays to scalars and our typemaps take that for real) */ - %extend CDynamicObjectArray { + %extend DynamicObjectArray { bool append_element_real_vector(float64_t v, const char* name="") { SGVector wrap(1); @@ -352,15 +376,15 @@ namespace shogun #endif #endif #ifdef USE_FLOAT32 - %template(append_element_float_vector) CDynamicObjectArray::append_element_vector, SGVector>; - %template(append_element_float_matrix) CDynamicObjectArray::append_element_matrix, SGMatrix>; - %template(append_element_float) CDynamicObjectArray::append_element; + %template(append_element_float_vector) DynamicObjectArray::append_element_vector, SGVector>; + %template(append_element_float_matrix) DynamicObjectArray::append_element_matrix, SGMatrix>; + %template(append_element_float) DynamicObjectArray::append_element; #endif #ifdef USE_INT32 - %template(append_element_int) CDynamicObjectArray::append_element; + %template(append_element_int) DynamicObjectArray::append_element; #endif - %template(append_element_string_char_list) CDynamicObjectArray::append_element_string_list; - %template(append_element_string_word_list) CDynamicObjectArray::append_element_string_list; + %template(append_element_string_char_list) DynamicObjectArray::append_element_string_list; + %template(append_element_string_word_list) DynamicObjectArray::append_element_string_list; } %include %include diff --git a/src/interfaces/swig/Library_includes.i b/src/interfaces/swig/Library_includes.i index b13c4b1eb55..4d91848828d 100644 --- a/src/interfaces/swig/Library_includes.i +++ b/src/interfaces/swig/Library_includes.i @@ -14,7 +14,6 @@ #include #include #include -#include #include #include #include diff --git a/src/interfaces/swig/Loss.i b/src/interfaces/swig/Loss.i index ae551ed089f..0cd4e0f899a 100644 --- a/src/interfaces/swig/Loss.i +++ b/src/interfaces/swig/Loss.i @@ -5,7 +5,7 @@ */ /* Loss renames */ -%rename(LossFunction) CLossFunction; +%shared_ptr(shogun::LossFunction) /* Loss includes */ %include diff --git a/src/interfaces/swig/Machine.i b/src/interfaces/swig/Machine.i index 49fb45508b7..ef2bc55f399 100644 --- a/src/interfaces/swig/Machine.i +++ b/src/interfaces/swig/Machine.i @@ -7,33 +7,19 @@ /*%warnfilter(302) apply; %warnfilter(302) apply_generic;*/ -%newobject shogun::CPipelineBuilder::then(const std::string&); -%newobject shogun::CPipelineBuilder::then(const std::string&, CMachine*); -%newobject shogun::CPipelineBuilder::build(); - -%rename(Machine) CMachine; -%rename(PipelineBuilder) CPipelineBuilder; -%rename(Pipeline) CPipeline; - -%newobject apply(); -%newobject apply(CFeatures* data); -%newobject apply_binary(); -%newobject apply_binary(CFeatures* data); -%newobject apply_regression(); -%newobject apply_regression(CFeatures* data); -%newobject apply_multiclass(); -%newobject apply_multiclass(CFeatures* data); -%newobject apply_structured(); -%newobject apply_structured(CFeatures* data); -%newobject apply_latent(); -%newobject apply_latent(CFeatures* data); +%shared_ptr(shogun::Machine) +%shared_ptr(shogun::PipelineBuilder) +%shared_ptr(shogun::Pipeline) +%shared_ptr(shogun::LinearMachine) +%shared_ptr(shogun::DistanceMachine) +%shared_ptr(shogun::IterativeMachine) #if defined(SWIGPYTHON) || defined(SWIGOCTAVE) || defined(SWIGRUBY) || defined(SWIGLUA) || defined(SWIGR) %define APPLY_MULTICLASS(CLASS) %extend CLASS { - CMulticlassLabels* apply(CFeatures* data=NULL) + std::shared_ptr apply(std::shared_ptr data=NULL) { return $self->apply_multiclass(data); } @@ -43,7 +29,7 @@ %define APPLY_BINARY(CLASS) %extend CLASS { - CBinaryLabels* apply(CFeatures* data=NULL) + std::shared_ptr apply(std::shared_ptr data=NULL) { return $self->apply_binary(data); } @@ -53,7 +39,7 @@ %define APPLY_REGRESSION(CLASS) %extend CLASS { - CRegressionLabels* apply(CFeatures* data=NULL) + std::shared_ptr apply(std::shared_ptr data=NULL) { return $self->apply_regression(data); } @@ -63,7 +49,7 @@ %define APPLY_STRUCTURED(CLASS) %extend CLASS { - CStructuredLabels* apply(CFeatures* data=NULL) + std::shared_ptr apply(std::shared_ptr data=NULL) { return $self->apply_structured(data); } @@ -73,73 +59,71 @@ %define APPLY_LATENT(CLASS) %extend CLASS { - CLatentLabels* apply(CFeatures* data=NULL) + std::shared_ptr apply(std::shared_ptr data=NULL) { return $self->apply_latent(data); } } %enddef -namespace shogun { -APPLY_MULTICLASS(CMulticlassMachine); -APPLY_MULTICLASS(CKernelMulticlassMachine); -APPLY_MULTICLASS(CLinearMulticlassMachine); -APPLY_MULTICLASS(CDistanceMachine); +APPLY_MULTICLASS(MulticlassMachine); +APPLY_MULTICLASS(KernelMulticlassMachine); +APPLY_MULTICLASS(LinearMulticlassMachine); +APPLY_MULTICLASS(DistanceMachine); -APPLY_BINARY(CLinearMachine); -APPLY_BINARY(CKernelMachine); +APPLY_BINARY(LinearMachine); +APPLY_BINARY(KernelMachine); #ifdef USE_GPL_SHOGUN -APPLY_BINARY(CWDSVMOcas); +APPLY_BINARY(WDSVMOcas); #endif //USE_GPL_SHOGUN -APPLY_BINARY(CPluginEstimate); +APPLY_BINARY(PluginEstimate); #ifdef USE_GPL_SHOGUN -APPLY_BINARY(CGaussianProcessClassification); +APPLY_BINARY(GaussianProcessClassification); #endif //USE_GPL_SHOGUN -APPLY_REGRESSION(CMKLRegression); +APPLY_REGRESSION(MKLRegression); #ifdef HAVE_LAPACK -APPLY_REGRESSION(CLeastSquaresRegression); -APPLY_REGRESSION(CLeastAngleRegression); +APPLY_REGRESSION(LeastSquaresRegression); +APPLY_REGRESSION(LeastAngleRegression); #endif #ifdef USE_GPL_SHOGUN -APPLY_REGRESSION(CGaussianProcessRegression); +APPLY_REGRESSION(GaussianProcessRegression); #endif //USE_GPL_SHOGUN -APPLY_STRUCTURED(CStructuredOutputMachine); -APPLY_STRUCTURED(CLinearStructuredOutputMachine); -APPLY_STRUCTURED(CKernelStructuredOutputMachine); +APPLY_STRUCTURED(StructuredOutputMachine); +APPLY_STRUCTURED(LinearStructuredOutputMachine); +APPLY_STRUCTURED(KernelStructuredOutputMachine); #ifdef USE_MOSEK -APPLY_STRUCTURED(CPrimalMosekSOSVM); +APPLY_STRUCTURED(PrimalMosekSOSVM); #endif #ifdef USE_GPL_SHOGUN -APPLY_STRUCTURED(CDualLibQPBMSOSVM); -APPLY_LATENT(CLatentSVM); +APPLY_STRUCTURED(DualLibQPBMSOSVM); +APPLY_LATENT(LatentSVM); #endif //USE_GPL_SHOGUN -} - -%rename(apply_generic) CMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CMulticlassMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CKernelMulticlassMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CLinearMulticlassMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CCDistanceMachineMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CLinearMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CKernelMachine::apply(CFeatures* data=NULL); + +%rename(apply_generic) Machine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) MulticlassMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) KernelMulticlassMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) LinearMulticlassMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) DistanceMachineMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) LinearMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) KernelMachine::apply(std::shared_ptr data=NULL); #ifdef USE_GPL_SHOGUN -%rename(apply_generic) CWDSVMOcas::apply(CFeatures* data=NULL); +%rename(apply_generic) WDSVMOcas::apply(std::shared_ptr data=NULL); #endif //USE_GPL_SHOGUN -%rename(apply_generic) CPluginEstimate::apply(CFeatures* data=NULL); -%rename(apply_generic) CMKLRegression::apply(CFeatures* data=NULL); +%rename(apply_generic) PluginEstimate::apply(std::shared_ptr data=NULL); +%rename(apply_generic) MKLRegression::apply(std::shared_ptr data=NULL); #ifdef HAVE_LAPACK -%rename(apply_generic) CLeastSquaresRegression::apply(CFeatures* data=NULL); -%rename(apply_generic) CLeastAngleRegression::apply(CFeatures* data=NULL); +%rename(apply_generic) LeastSquaresRegression::apply(std::shared_ptr data=NULL); +%rename(apply_generic) LeastAngleRegression::apply(std::shared_ptr data=NULL); #endif -%rename(apply_generic) CGaussianProcessRegression::apply(CFeatures* data=NULL); +%rename(apply_generic) GaussianProcessRegression::apply(std::shared_ptr data=NULL); -%rename(apply_generic) CStructuredOutputMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CLinearStructuredOutputMachine::apply(CFeatures* data=NULL); -%rename(apply_generic) CKernelStructuredOutputMachine::apply(CFeatures* data=NULL); +%rename(apply_generic) StructuredOutputMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) LinearStructuredOutputMachine::apply(std::shared_ptr data=NULL); +%rename(apply_generic) KernelStructuredOutputMachine::apply(std::shared_ptr data=NULL); #ifdef USE_MOSEK -%rename(apply_generic) CPrimalMosekSOSVM::apply(CFeatures* data=NULL); +%rename(apply_generic) CPrimalMosekSOSVM::apply(std::shared_ptr data=NULL); #endif #undef APPLY_MULTICLASS diff --git a/src/interfaces/swig/Mathematics.i b/src/interfaces/swig/Mathematics.i index 767322e5d3f..ebb0ee77889 100644 --- a/src/interfaces/swig/Mathematics.i +++ b/src/interfaces/swig/Mathematics.i @@ -5,10 +5,39 @@ */ /* Remove C Prefix */ -%rename(Math) CMath; -%rename(Statistics) CStatistics; +%shared_ptr(shogun::Math) +%shared_ptr(shogun::Statistics) + +/* Trace samplers */ +%shared_ptr(shogun::TraceSampler) +%shared_ptr(shogun::NormalSampler) +%shared_ptr(shogun::ProbingSampler) + +#ifdef USE_FLOAT32 +%shared_ptr(shagun::SparseMatrixOperator) +#endif + +#ifdef USE_FLOAT64 +%shared_ptr(shogun::LinearOperator) +%shared_ptr(shogun::MatrixOperator) +%shared_ptr(shagun::SparseMatrixOperator) +%shared_ptr(shogun::DenseMatrixOperator) +%shared_ptr(shogun::LinearSolver) +%shared_ptr(shogun::IterativeLinearSolver) +%shared_ptr(shogun::OperatorFunction) +#endif +#ifdef USE_COMPLEX128 +%shared_ptr(shogun::LinearOperator) +%shared_ptr(shogun::MatrixOperator) +%shared_ptr(shogun::SparseMatrixOperator) +%shared_ptr(shogun::DenseMatrixOperator) +%shared_ptr(shogun::LinearSolver) +%shared_ptr(shogun::IterativeLinearSolver) +%shared_ptr(shogun::IterativeShiftedLinearFamilySolver) +#endif + #ifdef USE_GPL_SHOGUN -%rename(SparseInverseCovariance) CSparseInverseCovariance; +%shared_ptr(shogun::SparseInverseCovariance) #endif //USE_GPL_SHOGUN // fix overloaded methods in Math @@ -17,7 +46,7 @@ namespace shogun { #ifdef USE_INT32 -%rename(pow_int32) CMath::pow(int32_t,int32_t); +%rename(pow_int32) Math::pow(int32_t,int32_t); #endif #ifdef USE_FLOAT32 @@ -25,35 +54,30 @@ namespace shogun #endif #ifdef USE_FLOAT64 -%rename(pow_float64_int32) CMath::pow(float64_t,int32_t); -%rename(pow_float64_float64) CMath::pow(float64_t,float64_t); +%rename(pow_float64_int32) Math::pow(float64_t,int32_t); +%rename(pow_float64_float64) Math::pow(float64_t,float64_t); %rename(sqrt_float64) std::sqrt(float64_t); } #endif #ifdef USE_COMPLEX128 -%rename(pow_complex128_float64) CMath::pow(complex128_t,float64_t); -%rename(pow_complex128_int32) CMath::pow(complex128_t,int32_t); +%rename(shogun::pow_complex128_float64) Math::pow(complex128_t,float64_t) +%rename(shogun::pow_complex128_int32) Math::pow(complex128_t,int32_t) #endif #endif // defined(SWIGLUA) || defined(SWIGR) /* Log-det framework */ -/* Trace samplers */ -%rename(TraceSampler) CTraceSampler; -%rename(NormalSampler) CNormalSampler; -%rename(ProbingSampler) CProbingSampler; - /* Linear operators */ %include namespace shogun { #ifdef USE_FLOAT64 - %template(RealLinearOperator) CLinearOperator; + %template(RealLinearOperator) LinearOperator; #endif #ifdef USE_COMPLEX128 - %template(ComplexLinearOperator) CLinearOperator; + %template(ComplexLinearOperator) LinearOperator; #endif } @@ -61,10 +85,10 @@ namespace shogun namespace shogun { #ifdef USE_FLOAT64 - %template(RealMatrixOperator) CMatrixOperator; + %template(RealMatrixOperator) MatrixOperator; #endif #ifdef USE_COMPLEX128 - %template(ComplexMatrixOperator) CMatrixOperator; + %template(ComplexMatrixOperator) MatrixOperator; #endif } @@ -72,10 +96,10 @@ namespace shogun namespace shogun { #ifdef USE_FLOAT64 - %template(RealSparseMatrixOperator) CSparseMatrixOperator; + %template(RealSparseMatrixOperator) SparseMatrixOperator; #endif #ifdef USE_COMPLEX128 - %template(ComplexSparseMatrixOperator) CSparseMatrixOperator; + %template(ComplexSparseMatrixOperator) SparseMatrixOperator; #endif } @@ -83,10 +107,10 @@ namespace shogun namespace shogun { #ifdef USE_FLOAT64 - %template(RealDenseMatrixOperator) CDenseMatrixOperator; + %template(RealDenseMatrixOperator) DenseMatrixOperator; #endif #ifdef USE_COMPLEX128 - %template(ComplexDenseMatrixOperator) CDenseMatrixOperator; + %template(ComplexDenseMatrixOperator) DenseMatrixOperator; #endif } @@ -95,59 +119,59 @@ namespace shogun namespace shogun { #ifdef USE_FLOAT64 - %template(RealOperatorFunction) COperatorFunction; + %template(RealOperatorFunction) OperatorFunction; #endif } -%rename(RationalApproximation) CRationalApproximation; -%rename(LogRationalApproximationIndividual) CLogRationalApproximationIndividual; -%rename(LogRationalApproximationCGM) CLogRationalApproximationCGM; +%shared_ptr(shogun::RationalApproximation) +%shared_ptr(shogun::LogRationalApproximationIndividual) +%shared_ptr(shogun::LogRationalApproximationCGM) /* Linear solvers */ %include namespace shogun { #if defined(USE_FLOAT64) - %template(RealLinearSolver) CLinearSolver; + %template(RealLinearSolver) LinearSolver; #endif #if defined(USE_FLOAT64) && defined(USE_COMPLEX128) - %template(ComplexRealLinearSolver) CLinearSolver; + %template(ComplexRealLinearSolver) LinearSolver; #endif } -%rename(DirectSparseLinearSolver) CDirectSparseLinearSolver; +%shared_ptr(shogun::DirectSparseLinearSolver) #ifdef USE_COMPLEX128 - %rename(DirectLinearSolverComplex) CDirectLinearSolverComplex; + %shared_ptr(shogun::DirectLinearSolverComplex) #endif %include namespace shogun { #if defined(USE_FLOAT64) - %template(RealIterativeLinearSolver) CIterativeLinearSolver; + %template(RealIterativeLinearSolver) IterativeLinearSolver; #endif #if defined(USE_FLOAT64) && defined(USE_COMPLEX128) - %template(ComplexRealIterativeLinearSolver) CIterativeLinearSolver; + %template(ComplexRealIterativeLinearSolver) IterativeLinearSolver; #endif } -%rename (ConjugateGradientSolver) CConjugateGradientSolver; -%rename (ConjugateOrthogonalCGSolver) CConjugateOrthogonalCGSolver; +%shared_ptr(shogun::ConjugateGradientSolver) +%shared_ptr(shogun::ConjugateOrthogonalCGSolver) %include namespace shogun { #if defined(USE_FLOAT64) && defined(USE_COMPLEX128) - %template(RealComplexIterativeShiftedLinearSolver) CIterativeShiftedLinearFamilySolver; + %template(RealComplexIterativeShiftedLinearSolver) IterativeShiftedLinearFamilySolver; #endif } -%rename(CGMShiftedFamilySolver) CCGMShiftedFamilySolver; +%shared_ptr(shogun::CGMShiftedFamilySolver) -%rename(EigenSolver) CEigenSolver; -%rename(LanczosEigenSolver) CLanczosEigenSolver; +%shared_ptr(shogun::EigenSolver) +%shared_ptr(shogun::LanczosEigenSolver) -%rename(LogDetEstimator) CLogDetEstimator; +%shared_ptr(shogun::LogDetEstimator) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Metric.i b/src/interfaces/swig/Metric.i index 13796d8b091..0819d861133 100644 --- a/src/interfaces/swig/Metric.i +++ b/src/interfaces/swig/Metric.i @@ -5,8 +5,8 @@ */ /* Remove C Prefix */ -%rename(LMNNStatistics) CLMNNStatistics; -%rename(LMNN) CLMNN; +%shared_ptr(shogun::LMNNStatistics) +%shared_ptr(shogun::LMNN) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Minimizer.i b/src/interfaces/swig/Minimizer.i index 75df057f943..bfc04e2e86c 100644 --- a/src/interfaces/swig/Minimizer.i +++ b/src/interfaces/swig/Minimizer.i @@ -31,10 +31,15 @@ /* Remove C Prefix */ -%rename(LBFGSMinimizer) CLBFGSMinimizer; +%shared_ptr(shogun::Minimizer) +%shared_ptr(shogun::LBFGSMinimizer) +%shared_ptr(shogun::FirstOrderMinimizer) +%shared_ptr(shogun::SingleLaplaceNewtonOptimizer) +%shared_ptr(shogun::SingleFITCLaplaceNewtonOptimizer) + #ifdef USE_GPL_SHOGUN #ifdef HAVE_NLOPT -%rename(NLOPTMinimizer) CNLOPTMinimizer; +%shared_ptr(shogun::NLOPTMinimizer) #endif //HAVE_NLOPT #endif //USE_GPL_SHOGUN diff --git a/src/interfaces/swig/ModelSelection.i b/src/interfaces/swig/ModelSelection.i index 701406bbc17..81bc67911d4 100644 --- a/src/interfaces/swig/ModelSelection.i +++ b/src/interfaces/swig/ModelSelection.i @@ -8,20 +8,20 @@ %newobject *::select_model(); %newobject CParameterCombination::copy_tree(); %newobject CParameterCombination::leaf_sets_multiplication(); -%newobject CModelSelectionParameters::get_combinations(); -%newobject CModelSelectionParameters::get_single_combination(); +%newobject ModelSelectionParameters::get_combinations(); +%newobject ModelSelectionParameters::get_single_combination(); /* what about parameter_set_multiplication returns new DynArray? */ -/* Remove C Prefix */ -%rename(GridSearchModelSelection) CGridSearchModelSelection; -%rename(RandomSearchModelSelection) CRandomSearchModelSelection; +%shared_ptr(shogun::ModelSelection) +%shared_ptr(shogun::GridSearchModelSelection) +%shared_ptr(shogun::RandomSearchModelSelection) #ifdef USE_GPL_SHOGUN -%rename(GradientModelSelection) CGradientModelSelection; +%shared_ptr(shogun::GradientModelSelection) #endif //USE_GPL_SHOGUN -%rename(ModelSelectionBase) CModelSelection; -%rename(ModelSelectionParameters) CModelSelectionParameters; -%rename(ParameterCombination) CParameterCombination; +%shared_ptr(shogun::ModelSelectionBase) +%shared_ptr(shogun::ModelSelectionParameters) +%shared_ptr(shogun::ParameterCombination) %include %include diff --git a/src/interfaces/swig/Multiclass.i b/src/interfaces/swig/Multiclass.i index 1b99a7091b9..6f99dda6b17 100644 --- a/src/interfaces/swig/Multiclass.i +++ b/src/interfaces/swig/Multiclass.i @@ -4,65 +4,72 @@ * Authors: Heiko Strathmann, Saloni Nigam, Sergey Lisitsyn */ -/* Remove C Prefix */ -%rename(BalancedConditionalProbabilityTree) CBalancedConditionalProbabilityTree; -%rename(ConditionalProbabilityTree) CConditionalProbabilityTree; -%rename(RandomConditionalProbabilityTree) CRandomConditionalProbabilityTree; -%rename(RelaxedTree) CRelaxedTree; -%rename(TreeMachineNode) CTreeMachineNode; -%rename(ID3ClassifierTree) CID3ClassifierTree; -%rename(C45ClassifierTree) CC45ClassifierTree; -%rename(CARTree) CCARTree; -%rename(CHAIDTree) CCHAIDTree; +%shared_ptr(shogun::TreeMachineNode) +%shared_ptr(shogun::TreeMachine) +%shared_ptr(shogun::TreeMachine) +%shared_ptr(shogun::TreeMachine) +%shared_ptr(shogun::TreeMachine) +%shared_ptr(shogun::TreeMachine) +%shared_ptr(shogun::TreeMachine) +%shared_ptr(shogun::TreeMachine) -%rename(RejectionStrategy) CRejectionStrategy; -%rename(ThresholdRejectionStrategy) CThresholdRejectionStrategy; -%rename(DixonQTestRejectionStrategy) CDixonQTestRejectionStrategy; -%rename(MulticlassStrategy) CMulticlassStrategy; -%rename(MulticlassOneVsRestStrategy) CMulticlassOneVsRestStrategy; -%rename(MulticlassOneVsOneStrategy) CMulticlassOneVsOneStrategy; -%rename(BaseMulticlassMachine) CBaseMulticlassMachine; -%rename(MulticlassMachine) CMulticlassMachine; -%rename(NativeMulticlassMachine) CNativeMulticlassMachine; -%rename(LinearMulticlassMachine) CLinearMulticlassMachine; -%rename(KernelMulticlassMachine) CKernelMulticlassMachine; -%rename(MulticlassSVM) CMulticlassSVM; -%rename(MKLMulticlass) CMKLMulticlass; +%shared_ptr(shogun::BalancedConditionalProbabilityTree) +%shared_ptr(shogun::ConditionalProbabilityTree) +%shared_ptr(shogun::RandomConditionalProbabilityTree) +%shared_ptr(shogun::RelaxedTree) -%newobject apply_multilabel_output(); +%shared_ptr(shogun::ID3ClassifierTree) +%shared_ptr(shogun::C45ClassifierTree) +%shared_ptr(shogun::CARTree) +%shared_ptr(shogun::CHAIDTree) -%rename(ECOCStrategy) CECOCStrategy; -%rename(ECOCEncoder) CECOCEncoder; -%rename(ECOCDecoder) CECOCDecoder; -%rename(ECOCOVREncoder) CECOCOVREncoder; -%rename(ECOCOVOEncoder) CECOCOVOEncoder; -%rename(ECOCRandomSparseEncoder) CECOCRandomSparseEncoder; -%rename(ECOCRandomDenseEncoder) CECOCRandomDenseEncoder; -%rename(ECOCDiscriminantEncoder) CECOCDiscriminantEncoder; -%rename(ECOCForestEncoder) CECOCForestEncoder; -%rename(ECOCSimpleDecoder) CECOCSimpleDecoder; -%rename(ECOCHDDecoder) CECOCHDDecoder; -%rename(ECOCIHDDecoder) CECOCIHDDecoder; -%rename(ECOCEDDecoder) CECOCEDDecoder; -%rename(ECOCAEDDecoder) CECOCAEDDecoder; -%rename(ECOCLLBDecoder) CECOCLLBDecoder; +%shared_ptr(shogun::RejectionStrategy) +%shared_ptr(shogun::ThresholdRejectionStrategy) +%shared_ptr(shogun::DixonQTestRejectionStrategy) +%shared_ptr(shogun::MulticlassStrategy) +%shared_ptr(shogun::MulticlassOneVsRestStrategy) +%shared_ptr(shogun::MulticlassOneVsOneStrategy) +%shared_ptr(shogun::BaseMulticlassMachine) +%shared_ptr(shogun::MulticlassMachine) +%shared_ptr(shogun::NativeMulticlassMachine) +%shared_ptr(shogun::LinearMulticlassMachine) +%shared_ptr(shogun::KernelMulticlassMachine) +%shared_ptr(shogun::MulticlassSVM) +%shared_ptr(shogun::MKLMulticlass) + +%shared_ptr(shogun::ECOCStrategy) +%shared_ptr(shogun::ECOCEncoder) +%shared_ptr(shogun::ECOCDecoder) +%shared_ptr(shogun::ECOCOVREncoder) +%shared_ptr(shogun::ECOCOVOEncoder) +%shared_ptr(shogun::ECOCRandomSparseEncoder) +%shared_ptr(shogun::ECOCRandomDenseEncoder) +%shared_ptr(shogun::ECOCDiscriminantEncoder) +%shared_ptr(shogun::ECOCForestEncoder) +%shared_ptr(shogun::ECOCSimpleDecoder) +%shared_ptr(shogun::ECOCHDDecoder) +%shared_ptr(shogun::ECOCIHDDecoder) +%shared_ptr(shogun::ECOCEDDecoder) +%shared_ptr(shogun::ECOCAEDDecoder) +%shared_ptr(shogun::ECOCLLBDecoder) #ifdef USE_GPL_SHOGUN -%rename(MulticlassTreeGuidedLogisticRegression) CMulticlassTreeGuidedLogisticRegression; -%rename(MulticlassLogisticRegression) CMulticlassLogisticRegression; -%rename(MulticlassOCAS) CMulticlassOCAS; +%shared_ptr(shogun::MulticlassTreeGuidedLogisticRegression) +%shared_ptr(shogun::MulticlassLogisticRegression) +%shared_ptr(shogun::MulticlassOCAS) #endif //USE_GPL_SHOGUN -%rename(MulticlassSVM) CMulticlassSVM; +%shared_ptr(shogun::MulticlassSVM) +%shared_ptr(shogun::MulticlassLibLinear) -%rename(LaRank) CLaRank; -%rename(ScatterSVM) CScatterSVM; -%rename(GMNPSVM) CGMNPSVM; -%rename(KNN) CKNN; -%rename(GaussianNaiveBayes) CGaussianNaiveBayes; -%rename(QDA) CQDA; -%rename(MCLDA) CMCLDA; +%shared_ptr(shogun::LaRank) +%shared_ptr(shogun::ScatterSVM) +%shared_ptr(shogun::GMNPSVM) +%shared_ptr(shogun::KNN) +%shared_ptr(shogun::GaussianNaiveBayes) +%shared_ptr(shogun::QDA) +%shared_ptr(shogun::MCLDA) -%rename(ShareBoost) CShareBoost; +%shared_ptr(shogun::ShareBoost) /* Include Class Headers to make them visible from within the target language */ %include @@ -71,16 +78,16 @@ %include namespace shogun { - %template(TreeMachineWithConditionalProbabilityTreeNodeData) CTreeMachine; - %template(TreeMachineWithRelaxedTreeNodeData) CTreeMachine; - %template(TreeMachineWithID3TreeNodeData) CTreeMachine; - %template(TreeMachineWithC45TreeNodeData) CTreeMachine; - %template(TreeMachineWithCARTreeNodeData) CTreeMachine; - %template(TreeMachineWithCHAIDTreeNodeData) CTreeMachine; + %template(TreeMachineWithConditionalProbabilityTreeNodeData) TreeMachine; + %template(TreeMachineWithRelaxedTreeNodeData) TreeMachine; + %template(TreeMachineWithID3TreeNodeData) TreeMachine; + %template(TreeMachineWithC45TreeNodeData) TreeMachine; + %template(TreeMachineWithCARTreeNodeData) TreeMachine; + %template(TreeMachineWithCHAIDTreeNodeData) TreeMachine; /** Instantiate RandomMixin */ - %template(SeedableTreeMachine) shogun::Seedable>; - %template(RandomMixinTreeMachine) RandomMixin, std::mt19937_64>; + %template(SeedableTreeMachine) shogun::Seedable>; + %template(RandomMixinTreeMachine) RandomMixin, std::mt19937_64>; } %include diff --git a/src/interfaces/swig/NeuralNets.i b/src/interfaces/swig/NeuralNets.i index cd2e618b7af..a1d00d2527a 100644 --- a/src/interfaces/swig/NeuralNets.i +++ b/src/interfaces/swig/NeuralNets.i @@ -1,25 +1,18 @@ -%newobject apply(CFeatures* data); -%newobject apply_multiclass(CFeatures* data); -%newobject visible_state_features(); -%newobject sample_group(int32_t V, int32_t num_gibbs_steps, int32_t batch_size); -%newobject sample_group_with_evidence(int32_t V, int32_t E, CDenseFeatures* evidence,int32_t num_gibbs_steps); -%newobject reconstruct(CDenseFeatures* data); -%newobject transform(CDenseFeatures* data); -%newobject done(); -%newobject input(int32_t size); -%newobject logistic(int32_t size); -%newobject linear(int32_t size); -%newobject rectified_linear(int32_t size); -%newobject leaky_rectified_linear(int32_t size); -%newobject softmax(int32_t size); - /* Remove C Prefix */ -%rename(ConvolutionalFeatureMap) CConvolutionalFeatureMap; -%rename(NeuralLayer) CNeuralLayer; -%rename(RBM) CRBM; -%rename(DeepBeliefNetwork) CDeepBeliefNetwork; -%rename(Autoencoder) CAutoencoder; -%rename(DeepAutoencoder) CDeepAutoencoder; +%shared_ptr(shogun::NeuralNetwork) +%shared_ptr(shogun::NeuralLayer) +%shared_ptr(shogun::NeuralInputLayer) +%shared_ptr(shogun::NeuralLinearLayer) +%shared_ptr(shogun::NeuralLogisticLayer) +%shared_ptr(shogun::NeuralSoftmaxLayer) +%shared_ptr(shogun::NeuralRectifiedLinearLayer) +%shared_ptr(shogun::ConvolutionalFeatureMap) +%shared_ptr(shogun::NeuralConvolutionalLayer) +%shared_ptr(shogun::RBM) +%shared_ptr(shogun::DeepBeliefNetwork) +%shared_ptr(shogun::Autoencoder) +%shared_ptr(shogun::DeepAutoencoder) +%shared_ptr(shogun::NeuralLayers) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/ParameterObserver.i b/src/interfaces/swig/ParameterObserver.i index e6867d6d723..5cbdd2802a2 100644 --- a/src/interfaces/swig/ParameterObserver.i +++ b/src/interfaces/swig/ParameterObserver.i @@ -3,6 +3,8 @@ * * Authors: Giovanni De Toni */ +%shared_ptr(shogun::ParameterObserverInterface) +%shared_ptr(shogun::ParameterObserverCV) %{ #include @@ -10,4 +12,4 @@ %} %include -%include \ No newline at end of file +%include diff --git a/src/interfaces/swig/Preprocessor.i b/src/interfaces/swig/Preprocessor.i index ff74767bb93..5f38adf899e 100644 --- a/src/interfaces/swig/Preprocessor.i +++ b/src/interfaces/swig/Preprocessor.i @@ -6,11 +6,37 @@ /* Remove C Prefix */ /* Feature selection framework */ -%rename(Preprocessor) CPreprocessor; -#%rename(DependenceMaximization) CDependenceMaximization; -#%rename(KernelDependenceMaximization) CDependenceMaximization; +%shared_ptr(shogun::Preprocessor) +#%shared_ptr(shogun::DependenceMaximization) +#%shared_ptr(shogun::KernelDependenceMaximization) + +#ifdef USE_UINT64 +%shared_ptr(shogun::StringPreprocessor) +%shared_ptr(shogun::DecompressString) +%shared_ptr(shogun::FeatureSelection) +#endif +#ifdef USE_UINT16 +%shared_ptr(shogun::StringPreprocessor) +%shared_ptr(shogun::DecompressString) +%shared_ptr(shogun::FeatureSelection) +#endif +#ifdef USE_UINT8 +%shared_ptr(shogun::StringPreprocessor) +%shared_ptr(shogun::DecompressString) +%shared_ptr(shogun::FeatureSelection) +#endif +#ifdef USE_CHAR +%shared_ptr(shogun::StringPreprocessor) +%shared_ptr(shogun::DecompressString) +%shared_ptr(shogun::FeatureSelection) +#endif +#ifdef USE_FLOAT64 +%shared_ptr(shogun::FeatureSelection) +#endif +#ifdef USE_INT16 +%shared_ptr(shogun::FeatureSelection) +#endif -%newobject shogun::CFeatureSelection::remove_feats; /* Include Class Headers to make them visible from within the target language */ %include @@ -21,16 +47,16 @@ namespace shogun { #ifdef USE_UINT64 - %template(StringUlongPreprocessor) CStringPreprocessor; + %template(StringUlongPreprocessor) StringPreprocessor; #endif #ifdef USE_UINT16 - %template(StringWordPreprocessor) CStringPreprocessor; + %template(StringWordPreprocessor) StringPreprocessor; #endif #ifdef USE_UINT8 - %template(StringBytePreprocessor) CStringPreprocessor; + %template(StringBytePreprocessor) StringPreprocessor; #endif #ifdef USE_CHAR - %template(StringCharPreprocessor) CStringPreprocessor; + %template(StringCharPreprocessor) StringPreprocessor; #endif } @@ -39,16 +65,16 @@ namespace shogun namespace shogun { #ifdef USE_UINT64 - %template(DecompressUlongString) CDecompressString; + %template(DecompressUlongString) DecompressString; #endif #ifdef USE_UINT16 - %template(DecompressWordString) CDecompressString; + %template(DecompressWordString) DecompressString; #endif #ifdef USE_UINT8 - %template(DecompressByteString) CDecompressString; + %template(DecompressByteString) DecompressString; #endif #ifdef USE_CHAR - %template(DecompressCharString) CDecompressString; + %template(DecompressCharString) DecompressString; #endif } @@ -57,22 +83,22 @@ namespace shogun namespace shogun { #ifdef USE_FLOAT64 - %template(RealFeatureSelection) CFeatureSelection; + %template(RealFeatureSelection) FeatureSelection; #endif #ifdef USE_UINT64 - %template(UlongFeatureSelection) CFeatureSelection; + %template(UlongFeatureSelection) FeatureSelection; #endif #ifdef USE_UINT16 - %template(WordFeatureSelection) CFeatureSelection; + %template(WordFeatureSelection) FeatureSelection; #endif #ifdef USE_INT16 - %template(ShortFeatureSelection) CFeatureSelection; + %template(ShortFeatureSelection) FeatureSelection; #endif #ifdef USE_UINT8 - %template(ByteFeatureSelection) CFeatureSelection; + %template(ByteFeatureSelection) FeatureSelection; #endif #ifdef USE_CHAR - %template(CharFeatureSelection) CFeatureSelection; + %template(CharFeatureSelection) FeatureSelection; #endif } diff --git a/src/interfaces/swig/Regression.i b/src/interfaces/swig/Regression.i index b6a09927e64..29c6da2a2fe 100644 --- a/src/interfaces/swig/Regression.i +++ b/src/interfaces/swig/Regression.i @@ -5,9 +5,9 @@ */ /* Remove C Prefix */ -%rename(Regression) CRegression; -%rename(MKL) CMKL; -%rename(MKLRegression) CMKLRegression; +%shared_ptr(shogun::Regression) +%shared_ptr(shogun::MKL) +%shared_ptr(shogun::MKLRegression) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/SGBase.i b/src/interfaces/swig/SGBase.i index 1734d91b355..5da3713091d 100644 --- a/src/interfaces/swig/SGBase.i +++ b/src/interfaces/swig/SGBase.i @@ -8,14 +8,12 @@ %include "stdint.i" %include "std_string.i" %include "exception.i" - -%feature("ref") shogun::CSGObject "SG_REF($this);" -%feature("unref") shogun::CSGObject "SG_UNREF($this);" +%include "std_shared_ptr.i" #ifdef SWIGJAVA -%typemap(javainterfaces) shogun::CSGObject "java.io.Externalizable" +%typemap(javainterfaces) shogun::SGObject "java.io.Externalizable" -%typemap(javaimports) shogun::CSGObject +%typemap(javaimports) shogun::SGObject %{ import org.shogun.JsonSerializer; import org.shogun.JsonDeserializer; @@ -24,10 +22,9 @@ import org.shogun.ByteArrayInputStream; import java.lang.StringBuffer; import org.jblas.*; %} -%typemap(javacode) shogun::CSGObject +%typemap(javacode) shogun::SGObject %{ public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException { -/* TODO: enable once shared_ptr is merged ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); JsonSerializer jsonSerializer = new JsonSerializer(); jsonSerializer.attach(byteArrayOS); @@ -39,7 +36,6 @@ public void writeExternal(java.io.ObjectOutput out) throws java.io.IOException { } public void readExternal(java.io.ObjectInput in) throws java.io.IOException, java.lang.ClassNotFoundException { -/* TODO: enable once shared_ptr is merged StringBuffer sb = new StringBuffer(); int ch; while ((ch=in.read()) != -1) { @@ -49,7 +45,6 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav JsonDeserializer jsonDeserializer = new JsonDeserializer(); jsonDeserializer.attach(bis); this.deserialize(jsonDeserializer); -*/ } %} #endif @@ -197,14 +192,14 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav static int print_sgobject(PyObject *pyobj, FILE *f, int flags) { void *argp; int res; - res = SWIG_ConvertPtr(pyobj, &argp, SWIGTYPE_p_shogun__CSGObject, 0); + res = SWIG_ConvertPtr(pyobj, &argp, SWIGTYPE_p_std__shared_ptrT_shogun__SGObject_t, 0); if (!SWIG_IsOK(res)) { - SWIG_Error(SWIG_ArgError(res), "in method 'CSGObject::tp_print', argument 1 of type 'CSGObject *'"); + SWIG_Error(SWIG_ArgError(res), "in method 'SGObject::tp_print', argument 1 of type 'std::shared_ptr'"); return SWIG_ERROR; } - CSGObject *obj = reinterpret_cast(argp); - std::string s = obj->to_string(); + auto obj = reinterpret_cast*>(argp); + std::string s = (*obj)->to_string(); fprintf(f, "%s", s.c_str()); return 0; } @@ -268,12 +263,12 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav static int print_sgobject(SV* pobj, FILE *f, int flags) { void *argp; int res; - res = SWIG_ConvertPtr(pobj, &argp, SWIGTYPE_p_shogun__CSGObject, 0); + res = SWIG_ConvertPtr(pobj, &argp, SWIGTYPE_p_shogun__SGObject, 0); if (!SWIG_IsOK(res)) { - SWIG_Error(SWIG_ArgError(res), "in method 'CSGObject::tp_print', argument 1 of type 'CSGObject *'"); + SWIG_Error(SWIG_ArgError(res), "in method 'SGObject::tp_print', argument 1 of type 'SGObject *'"); return SWIG_ERROR; } - CSGObject *obj = reinterpret_cast(argp); + SGObject *obj = reinterpret_cast(argp); std::string s = obj->to_string(); fprintf(f, "%s", s.c_str()); return 0; @@ -326,7 +321,8 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav %ignore sg_print_error; %ignore sg_cancel_computations; -%rename(SGObject) CSGObject; +%shared_ptr(shogun::SGObject) +%shared_ptr(shogun::StoppableSGObject) %include %include @@ -430,7 +426,7 @@ namespace shogun { namespace shogun { - %extend CSGObject + %extend SGObject { std::vector parameter_names() const { std::vector result; @@ -501,17 +497,15 @@ namespace shogun PyObject* __getstate__() { - io::CSerializer* serializer = nullptr; + std::shared_ptr serializer = nullptr; if (pickle_ascii) - serializer = new io::CJsonSerializer(); + serializer = std::make_shared(); else - serializer = new io::CBitserySerializer(); - auto byte_stream = some(); + serializer = std::make_shared(); + auto byte_stream = std::make_shared(); serializer->attach(byte_stream); serializer->write(wrap($self)); - auto serialized_obj = byte_stream->content(); - SG_UNREF(serializer); #ifdef PYTHON3 PyObject* str=PyBytes_FromStringAndSize(serialized_obj.data(), serialized_obj.size()); #else @@ -536,16 +530,15 @@ namespace shogun #else PyString_AsStringAndSize(py_str, &str, &len); #endif - io::CDeserializer* deser = nullptr; + std::shared_ptr deser = nullptr; if (pickle_ascii) - deser = new io::CJsonDeserializer(); + deser = std::make_shared(); else - deser = new io::CBitseryDeserializer(); + deser = std::make_shared(); - auto byte_input_stream = some(str, len); + auto byte_input_stream = some(str, len); deser->attach(byte_input_stream); $self->deserialize(deser); - SG_UNREF(deser); } /*int getbuffer(PyObject *obj, Py_buffer *view, int flags) { return 0; }*/ diff --git a/src/interfaces/swig/Statistics.i b/src/interfaces/swig/Statistics.i index 0fa85fbaac7..1259f0216ea 100644 --- a/src/interfaces/swig/Statistics.i +++ b/src/interfaces/swig/Statistics.i @@ -4,24 +4,18 @@ * Authors: Sergey Lisitsyn */ -/* These functions return new Objects */ -%newobject shogun::CTwoDistributionTest::compute_distance(CDistance*); -%newobject shogun::CTwoDistributionTest::compute_joint_distance(CDistance*); -%newobject shogun::CQuadraticTimeMMD::get_p_and_q(); - -/* Remove C Prefix */ -%rename(HypothesisTest) CHypothesisTest; -%rename(OneDistributionTest) COneDistributionTest; -%rename(TwoDistributionTest) CTwoDistributionTest; -%rename(IndependenceTest) CIndependenceTest; -%rename(TwoSampleTest) CTwoSampleTest; -%rename(MMD) CMMD; -%rename(StreamingMMD) CStreamingMMD; -%rename(LinearTimeMMD) CLinearTimeMMD; -%rename(BTestMMD) CBTestMMD; -%rename(QuadraticTimeMMD) CQuadraticTimeMMD; -%rename(MultiKernelQuadraticTimeMMD) CMultiKernelQuadraticTimeMMD; -%rename(KernelSelectionStrategy) CKernelSelectionStrategy; +%shared_ptr(shogun::HypothesisTest) +%shared_ptr(shogun::OneDistributionTest) +%shared_ptr(shogun::TwoDistributionTest) +%shared_ptr(shogun::IndependenceTest) +%shared_ptr(shogun::TwoSampleTest) +%shared_ptr(shogun::MMD) +%shared_ptr(shogun::StreamingMMD) +%shared_ptr(shogun::LinearTimeMMD) +%shared_ptr(shogun::BTestMMD) +%shared_ptr(shogun::QuadraticTimeMMD) +%shared_ptr(shogun::MultiKernelQuadraticTimeMMD) +%shared_ptr(shogun::KernelSelectionStrategy) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Structure.i b/src/interfaces/swig/Structure.i index e9de9494f51..02e741d412a 100644 --- a/src/interfaces/swig/Structure.i +++ b/src/interfaces/swig/Structure.i @@ -5,63 +5,64 @@ */ #if defined(USE_SWIG_DIRECTORS) && defined(SWIGPYTHON) -%feature("director") shogun::CDirectorStructuredModel; +%feature("director") shogun::DirectorStructuredModel; #endif /* Remove C Prefix */ -%rename(PlifBase) CPlifBase; -%rename(Plif) CPlif; -%rename(PlifArray) CPlifArray; -%rename(DynProg) CDynProg; -%rename(PlifMatrix) CPlifMatrix; -%rename(SegmentLoss) CSegmentLoss; -%rename(IntronList) CIntronList; - -%rename(StructuredModel) CStructuredModel; -%rename(ResultSet) CResultSet; -%rename(MulticlassModel) CMulticlassModel; -%rename(MulticlassSOLabels) CMulticlassSOLabels; -%rename(RealNumber) CRealNumber; -%rename(HMSVMModel) CHMSVMModel; -%rename(SequenceLabels) CSequenceLabels; -%rename(Sequence) CSequence; -%rename(StateModel) CStateModel; -%rename(TwoStateModel) CTwoStateModel; -%rename(DirectorStructuredModel) CDirectorStructuredModel; -%rename(MultilabelSOLabels) CMultilabelSOLabels; -%rename(SparseMultilabel) CSparseMultilabel; -%rename(MultilabelModel) CMultilabelModel; -%rename(HashedMultilabelModel) CHashedMultilabelModel; -%rename(MultilabelCLRModel) CMultilabelCLRModel; -%rename(HierarchicalMultilabelModel) CHierarchicalMultilabelModel; - -%rename(FactorType) CFactorType; -%rename(TableFactorType) CTableFactorType; -%rename(FactorDataSource) CFactorDataSource; -%rename(Factor) CFactor; -%rename(DisjointSet) CDisjointSet; -%rename(FactorGraph) CFactorGraph; -%rename(FactorGraphObservation) CFactorGraphObservation; -%rename(FactorGraphLabels) CFactorGraphLabels; -%rename(FactorGraphFeatures) CFactorGraphFeatures; -%rename(MAPInference) CMAPInference; -%rename(GraphCut) CGraphCut; -%rename(FactorGraphModel) CFactorGraphModel; - -%rename(SOSVMHelper) CSOSVMHelper; -%rename(StructuredOutputMachine) CStructuredOutputMachine; -%rename(LinearStructuredOutputMachine) CLinearStructuredOutputMachine; -%rename(KernelStructuredOutputMachine) CKernelStructuredOutputMachine; +%shared_ptr(shogun::PlifBase) +%shared_ptr(shogun::Plif) +%shared_ptr(shogun::PlifArray) +%shared_ptr(shogun::DynProg) +%shared_ptr(shogun::PlifMatrix) +%shared_ptr(shogun::SegmentLoss) +%shared_ptr(shogun::IntronList) + +%shared_ptr(shogun::StructuredModel) +%shared_ptr(shogun::ResultSet) +%shared_ptr(shogun::MulticlassModel) +%shared_ptr(shogun::MulticlassSOLabels) +%shared_ptr(shogun::RealNumber) +%shared_ptr(shogun::HMSVMModel) +%shared_ptr(shogun::SequenceLabels) +%shared_ptr(shogun::Sequence) +%shared_ptr(shogun::StateModel) +%shared_ptr(shogun::TwoStateModel) +%shared_ptr(shogun::DirectorStructuredModel) +%shared_ptr(shogun::MultilabelSOLabels) +%shared_ptr(shogun::SparseMultilabel) +%shared_ptr(shogun::MultilabelModel) +%shared_ptr(shogun::HashedMultilabelModel) +%shared_ptr(shogun::MultilabelCLRModel) +%shared_ptr(shogun::HierarchicalMultilabelModel) + +%shared_ptr(shogun::FactorType) +%shared_ptr(shogun::TableFactorType) +%shared_ptr(shogun::FactorDataSource) +%shared_ptr(shogun::Factor) +%shared_ptr(shogun::DisjointSet) +%shared_ptr(shogun::FactorGraph) +%shared_ptr(shogun::FactorGraphObservation) +%shared_ptr(shogun::FactorGraphLabels) +%shared_ptr(shogun::FactorGraphFeatures) +%shared_ptr(shogun::MAPInference) +%shared_ptr(shogun::MAPInferImpl) +%shared_ptr(shogun::GraphCut) +%shared_ptr(shogun::FactorGraphModel) + +%shared_ptr(shogun::SOSVMHelper) +%shared_ptr(shogun::StructuredOutputMachine) +%shared_ptr(shogun::LinearStructuredOutputMachine) +%shared_ptr(shogun::KernelStructuredOutputMachine) #ifdef USE_GPL_SHOGUN -%rename(DualLibQPBMSOSVM) CDualLibQPBMSOSVM; +%shared_ptr(shogun::DualLibQPBMSOSVM) #ifdef USE_MOSEK -%rename(PrimalMosekSOSVM) CPrimalMosekSOSVM; +%shared_ptr(shogun::PrimalMosekSOSVM) #endif /* USE_MOSEK */ #endif //USE_GPL_SHOGUN -%rename(StochasticSOSVM) CStochasticSOSVM; -%rename(FWSOSVM) CFWSOSVM; +%shared_ptr(shogun::StochasticSOSVM) +%shared_ptr(shogun::FWSOSVM) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/Transfer.i b/src/interfaces/swig/Transfer.i index 3f13acd7b94..00f7ae6f807 100644 --- a/src/interfaces/swig/Transfer.i +++ b/src/interfaces/swig/Transfer.i @@ -5,33 +5,36 @@ */ /* Multitask renames */ -%rename(MultitaskKernelNormalizer) CMultitaskKernelNormalizer; -%rename(MultitaskKernelMklNormalizer) CMultitaskKernelMklNormalizer; -%rename(MultitaskKernelTreeNormalizer) CMultitaskKernelTreeNormalizer; -%rename(MultitaskKernelMaskNormalizer) CMultitaskKernelMaskNormalizer; -%rename(MultitaskKernelMaskPairNormalizer) CMultitaskKernelMaskPairNormalizer; -%rename(MultitaskKernelPlifNormalizer) CMultitaskKernelPlifNormalizer; +%shared_ptr(shogun::Node) +%shared_ptr(shogun::MultitaskLinearMachine) +%shared_ptr(shogun::MultitaskKernelNormalizer) +%shared_ptr(shogun::MultitaskKernelMklNormalizer) +%shared_ptr(shogun::MultitaskKernelTreeNormalizer) +%shared_ptr(shogun::MultitaskKernelMaskNormalizer) +%shared_ptr(shogun::MultitaskKernelMaskPairNormalizer) +%shared_ptr(shogun::MultitaskKernelPlifNormalizer) -%rename(Task) CTask; -%rename(TaskRelationBase) CTaskRelation; -%rename(TaskTree) CTaskTree; -%rename(TaskGroup) CTaskGroup; +%shared_ptr(shogun::Task) +%shared_ptr(shogun::TaskRelation) +%shared_ptr(shogun::TaskRelationBase) +%shared_ptr(shogun::TaskTree) +%shared_ptr(shogun::TaskGroup) #ifdef USE_GPL_SHOGUN -%rename(MultitaskLinearMachineBase) CMultitaskLinearMachine; -%rename(MultitaskLeastSquaresRegression) CMultitaskLeastSquaresRegression; -%rename(MultitaskLogisticRegression) CMultitaskLogisticRegression; -%rename(MultitaskL12LogisticRegression) CMultitaskL12LogisticRegression; -%rename(MultitaskTraceLogisticRegression) CMultitaskTraceLogisticRegression; -%rename(MultitaskClusteredLogisticRegression) CMultitaskClusteredLogisticRegression; +%shared_ptr(shogun::MultitaskLinearMachineBase) +%shared_ptr(shogun::MultitaskLeastSquaresRegression) +%shared_ptr(shogun::MultitaskLogisticRegression) +%shared_ptr(shogun::MultitaskL12LogisticRegression) +%shared_ptr(shogun::MultitaskTraceLogisticRegression) +%shared_ptr(shogun::MultitaskClusteredLogisticRegression) #endif //USE_GPL_SHOGUN -%rename(MultitaskROCEvaluation) CMultitaskROCEvaluation; +%shared_ptr(shogun::MultitaskROCEvaluation) -%rename(LibLinearMTL) CLibLinearMTL; +%shared_ptr(shogun::LibLinearMTL) /* Domain adaptation renames */ -%rename(DomainAdaptationSVMLinear) CDomainAdaptationSVMLinear; -%rename(DomainAdaptationMulticlassLibLinear) CDomainAdaptationMulticlassLibLinear; +%shared_ptr(shogun::DomainAdaptationSVMLinear) +%shared_ptr(shogun::DomainAdaptationMulticlassLibLinear) /* Multitask includes */ %include diff --git a/src/interfaces/swig/Transformer.i b/src/interfaces/swig/Transformer.i index 0600afbd55c..9459ab86a7b 100644 --- a/src/interfaces/swig/Transformer.i +++ b/src/interfaces/swig/Transformer.i @@ -4,12 +4,8 @@ * Authors: Wuwei Lin */ -/* These functions return new Objects */ -%newobject shogun::CTransformer::transform(CFeatures*, bool inplace=true); -%newobject shogun::CTransformer::inverse_transform(CFeatures*, bool inplace=true); - /* Remove C Prefix */ -%rename(Transformer) CTransformer; +%shared_ptr(shogun::Transformer) /* Include Class Headers to make them visible from within the target language */ %include diff --git a/src/interfaces/swig/abstract_types_extension.i b/src/interfaces/swig/abstract_types_extension.i index 5dd36de3384..f58a9df1ef2 100644 --- a/src/interfaces/swig/abstract_types_extension.i +++ b/src/interfaces/swig/abstract_types_extension.i @@ -9,36 +9,36 @@ #if defined(SWIGPERL) //PTZ121108 example of classifier in examples/undocumented/libshogun/classifier_latent_svm.cpp - //extention to make use of CData,CLatentModel + //extention to make use of Data,LatentModel //TODO:PTZ121108 put it in another file like classifier_latent_svm.i or %include examples/undocumented/libshogun/classifier_latent_svm.cpp - //or find a clever way to wrap CLatenModel, CData instanciation, bless({}, shogun::LatentModel) - // is not enough and would need a new wrapper, but yet new CLatentModel() is not working, + //or find a clever way to wrap CLatenModel, Data instanciation, bless({}, shogun::LatentModel) + // is not enough and would need a new wrapper, but yet new LatentModel() is not working, // (with error: "cannot allocate an object of abstract type") ? %inline %{ namespace shogun { #define HOG_SIZE 1488 - struct CBoundingBox : public CData + struct CBoundingBox : public Data { - CBoundingBox(int32_t x, int32_t y) : CData(), x_pos(x), y_pos(y) {}; + CBoundingBox(int32_t x, int32_t y) : Data(), x_pos(x), y_pos(y) {}; int32_t x_pos, y_pos; virtual const char* get_name() const { return "BoundingBox"; } }; - struct CHOGFeatures : public CData + struct CHOGFeatures : public Data { - CHOGFeatures(int32_t w, int32_t h) : CData(), width(w), height(h) {}; + CHOGFeatures(int32_t w, int32_t h) : Data(), width(w), height(h) {}; int32_t width, height; float64_t ***hog; virtual const char* get_name() const { return "HOGFeatures"; } }; - class CObjectDetector: public CLatentModel + class CObjectDetector: public LatentModel { public: CObjectDetector() {}; - CObjectDetector(CLatentFeatures* feat, CLatentLabels* labels) - : CLatentModel(feat, labels) {}; + CObjectDetector(LatentFeatures* feat, CLatentLabels* labels) + : LatentModel(feat, labels) {}; virtual ~CObjectDetector() {}; virtual int32_t get_dim() const { return HOG_SIZE; }; - virtual CDotFeatures* get_psi_feature_vectors() + virtual DotFeatures* get_psi_feature_vectors() { int32_t num_examples = this->get_num_vectors(); int32_t dim = this->get_dim(); @@ -49,13 +49,13 @@ CBoundingBox* bb = (CBoundingBox*) m_labels->get_latent_label(i); sg_memcpy(psi_m.matrix+i*dim, hf->hog[bb->x_pos][bb->y_pos], dim*sizeof(float64_t)); } - CDenseFeatures* psi_feats = new CDenseFeatures(psi_m); + DenseFeatures* psi_feats = new DenseFeatures(psi_m); return psi_feats; }; - virtual CData* infer_latent_variable(const SGVector& w, index_t idx) + virtual Data* infer_latent_variable(const SGVector& w, index_t idx) { int32_t pos_x = 0, pos_y = 0; - float64_t max_score = -CMath::INFTY; + float64_t max_score = -Math::INFTY; CHOGFeatures* hf = (CHOGFeatures*) m_features->get_sample(idx); for (int i = 0; i < hf->width; ++i) { diff --git a/src/interfaces/swig/bagging.i b/src/interfaces/swig/bagging.i new file mode 100644 index 00000000000..346f59d1ca2 --- /dev/null +++ b/src/interfaces/swig/bagging.i @@ -0,0 +1,7 @@ +/* remove C prefix */ +%shared_ptr(shogun::BaggingMachine) +%shared_ptr(shogun::RandomForest) + +/* include class headers to make them visible from target language */ +%include +%include diff --git a/src/interfaces/swig/factory.i b/src/interfaces/swig/factory.i index c7249a85ccb..5d7f6b6fa43 100644 --- a/src/interfaces/swig/factory.i +++ b/src/interfaces/swig/factory.i @@ -1,30 +1,3 @@ -%newobject shogun::distance(std::string name); -%newobject shogun::evaluation(const std::string& name); -%newobject shogun::kernel(const std::string& name); -%newobject shogun::machine(const std::string& name); -%newobject shogun::multiclass_strategy(const std::string& name); -%newobject shogun::ecoc_encoder(const std::string& name); -%newobject shogun::ecoc_decoder(const std::string& name); -%newobject shogun::transformer(const std::string& name); -%newobject shogun::layer(const std::string& name); -%newobject shogun::splitting_strategy(const std::string& name); -%newobject shogun::machine_evaluation(const std::string& name); -%newobject shogun::svm(const std::string& name); -%newobject shogun::features; -%newobject shogun::gp_likelihood(const std::string& name); -%newobject shogun::gp_mean(const std::string& name); -%newobject shogun::differentiable(const std::string& name); -%newobject shogun::gp_inference(const std::string& name); -%newobject shogun::loss(const std::string& name); -%newobject shogun::string_features; -%newobject shogun::transformer(const std::string&); -%newobject shogun::csv_file(std::string fname, char rw); -%newobject shogun::libsvm_file(std::string fname, char rw); -%newobject shogun::pipeline; -%newobject shogun::labels; -%newobject shogun::distribution; -%newobject shogun::combination_rule; - %{ #include %} diff --git a/src/interfaces/swig/shogun.i b/src/interfaces/swig/shogun.i index 9e70ef3e764..cffda24526b 100644 --- a/src/interfaces/swig/shogun.i +++ b/src/interfaces/swig/shogun.i @@ -37,10 +37,10 @@ static int print_sgobject(PyObject *pyobj, FILE *f, int flags); %} -%feature("python:slot", "tp_str", functype="reprfunc") shogun::CSGObject::__str__; -%feature("python:slot", "tp_repr", functype="reprfunc") shogun::CSGObject::__repr__; -/*%feature("python:slot", "tp_hash", functype="hashfunc") shogun::CSGObject::myHashFunc;*/ -%feature("python:tp_print") shogun::CSGObject "print_sgobject"; +%feature("python:slot", "tp_str", functype="reprfunc") shogun::SGObject::__str__; +%feature("python:slot", "tp_repr", functype="reprfunc") shogun::SGObject::__repr__; +/*%feature("python:slot", "tp_hash", functype="hashfunc") shogun::SGObject::myHashFunc;*/ +%feature("python:tp_print") shogun::SGObject "print_sgobject"; /*%feature("python:slot", "tp_as_buffer", functype="PyBufferProcs*") shogun::SGObject::tp_as_buffer; %feature("python:slot", "bf_getbuffer", functype="getbufferproc") shogun::SGObject::getbuffer;*/ #endif // SWIGPYTHON @@ -54,6 +54,9 @@ %include "std_vector.i" %include "shogun_ignores.i" %include "RandomMixin.i" +%include "std_shared_ptr.i" +%shared_ptr(shogun::SparseMatrixOperator) + %include "Machine_includes.i" %include "Classifier_includes.i" %include "Clustering_includes.i" @@ -192,7 +195,7 @@ import org.jblas.*; namespace shogun { -%extend CSGObject +%extend SGObject { template ::value>> void put_scalar_dispatcher(const std::string& name, T value) @@ -290,73 +293,73 @@ namespace shogun #endif // SWIGR } -%template(put) CSGObject::put_scalar_dispatcher; +%template(put) SGObject::put_scalar_dispatcher; #ifndef SWIGJAVA -%template(put) CSGObject::put_scalar_dispatcher; +%template(put) SGObject::put_scalar_dispatcher; #endif // SWIGJAVA -%template(put) CSGObject::put_scalar_dispatcher; -%template(put) CSGObject::put_scalar_dispatcher; +%template(put) SGObject::put_scalar_dispatcher; +%template(put) SGObject::put_scalar_dispatcher; #ifndef SWIGR -%template(put) CSGObject::put, SGVector>; +%template(put) SGObject::put, SGVector>; #endif // SWIGR #if !defined(SWIGJAVA) && !defined(SWIGR) -%template(put) CSGObject::put, SGVector>; -%template(put) CSGObject::put, SGVector>; +%template(put) SGObject::put, SGVector>; +%template(put) SGObject::put, SGVector>; #elif defined(SWIGJAVA) -%template(put) CSGObject::put_vector_or_matrix_from_double_matrix_dispatcher, float64_t>; +%template(put) SGObject::put_vector_or_matrix_from_double_matrix_dispatcher, float64_t>; #elif defined(SWIGR) -%template(put) CSGObject::put_vector_scalar_dispatcher, bool>; -%template(put) CSGObject::put_vector_scalar_dispatcher, int32_t>; -%template(put) CSGObject::put_vector_scalar_dispatcher, float64_t>; +%template(put) SGObject::put_vector_scalar_dispatcher, bool>; +%template(put) SGObject::put_vector_scalar_dispatcher, int32_t>; +%template(put) SGObject::put_vector_scalar_dispatcher, float64_t>; #endif #ifndef SWIGJAVA -%template(put) CSGObject::put, SGMatrix>; +%template(put) SGObject::put, SGMatrix>; #endif // SWIGJAVA -%template(get_real) CSGObject::get; -%template(get_int) CSGObject::get; -%template(get_long) CSGObject::get; -%template(get_real_matrix) CSGObject::get, void>; -%template(get_char_string_list) CSGObject::get>, void>; -%template(get_word_string_list) CSGObject::get>, void>; -%template(get_option) CSGObject::get; +%template(get_real) SGObject::get; +%template(get_int) SGObject::get; +%template(get_long) SGObject::get; +%template(get_real_matrix) SGObject::get, void>; +%template(get_char_string_list) SGObject::get>, void>; +%template(get_word_string_list) SGObject::get>, void>; +%template(get_option) SGObject::get; #ifndef SWIGJAVA -%template(get_real_vector) CSGObject::get, void>; -%template(get_int_vector) CSGObject::get, void>; +%template(get_real_vector) SGObject::get, void>; +%template(get_int_vector) SGObject::get, void>; #else // SWIGJAVA -%template(get_real_vector) CSGObject::get_vector_as_matrix_dispatcher, float64_t>; -%template(get_int_vector) CSGObject::get_vector_as_matrix_dispatcher, int32_t>; +%template(get_real_vector) SGObject::get_vector_as_matrix_dispatcher, float64_t>; +%template(get_int_vector) SGObject::get_vector_as_matrix_dispatcher, int32_t>; #endif // SWIGJAVA -%template(put) CSGObject::put; +%template(put) SGObject::put; %define PUT_ADD(sg_class) -%template(put) CSGObject::put; -%template(add) CSGObject::add; +%template(put) SGObject::put; +%template(add) SGObject::add; %enddef -PUT_ADD(CMachine) -PUT_ADD(CKernel) -PUT_ADD(CDistance) -PUT_ADD(CFeatures) -PUT_ADD(CLabels) -PUT_ADD(CECOCEncoder) -PUT_ADD(CECOCDecoder) -PUT_ADD(CMulticlassStrategy) -PUT_ADD(CCombinationRule) -PUT_ADD(CInference) -PUT_ADD(CDifferentiableFunction) -PUT_ADD(CNeuralLayer) -PUT_ADD(CSplittingStrategy) -PUT_ADD(CEvaluation) -PUT_ADD(CSVM) -PUT_ADD(CMeanFunction) -PUT_ADD(CLikelihoodModel) -PUT_ADD(CTokenizer) -PUT_ADD(CLossFunction) +PUT_ADD(Machine) +PUT_ADD(Kernel) +PUT_ADD(Distance) +PUT_ADD(Features) +PUT_ADD(Labels) +PUT_ADD(ECOCEncoder) +PUT_ADD(ECOCDecoder) +PUT_ADD(MulticlassStrategy) +PUT_ADD(CombinationRule) +PUT_ADD(Inference) +PUT_ADD(DifferentiableFunction) +PUT_ADD(NeuralLayer) +PUT_ADD(SplittingStrategy) +PUT_ADD(Evaluation) +PUT_ADD(SVM) +PUT_ADD(MeanFunction) +PUT_ADD(LikelihoodModel) +PUT_ADD(Tokenizer) +PUT_ADD(LossFunction) %template(kernel) kernel; %template(features) features; diff --git a/src/interfaces/swig/shogun_ignores.i b/src/interfaces/swig/shogun_ignores.i index b93e0ac69cb..af22e3fa6ad 100644 --- a/src/interfaces/swig/shogun_ignores.i +++ b/src/interfaces/swig/shogun_ignores.i @@ -8,7 +8,7 @@ %ignore *::operator!=; #endif #if defined(SWIGPERL) -%ignore shogun::CSGObject::next; +%ignore shogun::SGObject::next; %ignore shogun::bmrm_ll::next; #endif @@ -89,7 +89,7 @@ %ignore free_feature_iterator; %ignore compute_sparse_feature_vector; -%ignore shogun::CKNN::m_covertree; +%ignore shogun::KNN::m_covertree; %ignore shogun::KNN_COVERTREE_POINT; %ignore free_feature_vector; %ignore free_sparse_feature_vector; @@ -97,29 +97,29 @@ %ignore shogun::CTaxonomy::get_node; %ignore shogun::CTaxonomy::add_node; %ignore shogun::CTaxonomy::intersect_root_path; -%ignore shogun::SGVector; -%ignore shogun::CGMM::CGMM(const SGVector&, const shogun::SGVector&, bool); +%ignore shogun::SGVector; +%ignore shogun::CGMM::CGMM(const SGVector&, const shogun::SGVector&, bool); %ignore shogun::CGMM::get_comp; %ignore shogun::CGMM::set_comp; -%ignore shogun::CDenseFeatures::dense_feature_iterator;; -%ignore shogun::CDenseFeatures::CDenseFeatures(ST*, int32_t, int32_t); -%ignore shogun::CDenseFeatures::get_feature_vector(int32_t, int32_t&, bool&); -%ignore shogun::CDenseFeatures::set_feature_matrix(ST*, int32_t, int32_t); -%ignore shogun::CDenseFeatures::vector_subset; -%ignore shogun::CDenseFeatures::feature_subset; -%ignore shogun::CDenseFeatures::get_feature_matrix(ST**, int32_t*, int32_t*); -%ignore shogun::CDenseFeatures::get_feature_matrix(int32_t&, int32_t&); -%ignore shogun::CDenseFeatures::get_transposed(int32_t&, int32_t&); -%ignore shogun::CDenseFeatures::dense_dot(int32_t, const float64_t*, int32_t); -%ignore shogun::CDenseFeatures::add_to_dense_vec(float64_t, int32_t, float64_t*, int32_t, bool ); -%ignore shogun::CSparseFeatures::sparse_feature_iterator;; -%ignore shogun::CSparseFeatures::CSparseFeatures(shogun::SGSparseVector*, int32_t, int32_t, bool); -%ignore shogun::CSparseFeatures::get_full_feature_vector(int32_t, int32_t&); -%ignore shogun::CSparseFeatures::get_sparse_feature_matrix(int32_t&, int32_t&); -%ignore shogun::CSparseFeatures::compute_squared; -%ignore shogun::CSparseFeatures::compute_squared_norm; -%ignore shogun::CSparseFeatures::get_transposed(int32_t&, int32_t&); -%ignore shogun::CSparseFeatures::clean_tsparse; +%ignore shogun::DenseFeatures::dense_feature_iterator;; +%ignore shogun::DenseFeatures::DenseFeatures(ST*, int32_t, int32_t); +%ignore shogun::DenseFeatures::get_feature_vector(int32_t, int32_t&, bool&); +%ignore shogun::DenseFeatures::set_feature_matrix(ST*, int32_t, int32_t); +%ignore shogun::DenseFeatures::vector_subset; +%ignore shogun::DenseFeatures::feature_subset; +%ignore shogun::DenseFeatures::get_feature_matrix(ST**, int32_t*, int32_t*); +%ignore shogun::DenseFeatures::get_feature_matrix(int32_t&, int32_t&); +%ignore shogun::DenseFeatures::get_transposed(int32_t&, int32_t&); +%ignore shogun::DenseFeatures::dense_dot(int32_t, const float64_t*, int32_t); +%ignore shogun::DenseFeatures::add_to_dense_vec(float64_t, int32_t, float64_t*, int32_t, bool ); +%ignore shogun::SparseFeatures::sparse_feature_iterator;; +%ignore shogun::SparseFeatures::SparseFeatures(shogun::SGSparseVector*, int32_t, int32_t, bool); +%ignore shogun::SparseFeatures::get_full_feature_vector(int32_t, int32_t&); +%ignore shogun::SparseFeatures::get_sparse_feature_matrix(int32_t&, int32_t&); +%ignore shogun::SparseFeatures::compute_squared; +%ignore shogun::SparseFeatures::compute_squared_norm; +%ignore shogun::SparseFeatures::get_transposed(int32_t&, int32_t&); +%ignore shogun::SparseFeatures::clean_tsparse; %ignore shogun::CStringFeatures::get_feature_vector(int32_t, int32_t&, bool&); %ignore shogun::CStringFeatures::set_features(SGVector*, int32_t, int32_t); @@ -142,23 +142,54 @@ %ignore shogun::CSGDQN_combine_and_clip; %ignore shogun::CSGDQN_compute_ratio; -%ignore shogun::io::CSerializer::attach; -%ignore shogun::io::CSerializer::stream; -%ignore shogun::io::CDeserializer::attach; -%ignore shogun::io::CDeserializer::stream; +%ignore shogun::SerializableFile::read_cont_begin; +%ignore shogun::SerializableFile::read_cont_end; +%ignore shogun::SerializableFile::read_item_begin; +%ignore shogun::SerializableFile::read_item_end; +%ignore shogun::SerializableFile::read_scalar; +%ignore shogun::SerializableFile::read_sgserializable_begin; +%ignore shogun::SerializableFile::read_sgserializable_end; +%ignore shogun::SerializableFile::read_sparse_begin; +%ignore shogun::SerializableFile::read_sparse_end; +%ignore shogun::SerializableFile::read_sparseentry_begin; +%ignore shogun::SerializableFile::read_sparseentry_end; +%ignore shogun::SerializableFile::read_string_begin; +%ignore shogun::SerializableFile::read_string_end; +%ignore shogun::SerializableFile::read_stringentry_begin; +%ignore shogun::SerializableFile::read_stringentry_end; +%ignore shogun::SerializableFile::read_type_begin; +%ignore shogun::SerializableFile::read_type_end; +%ignore shogun::SerializableFile::write_cont_begin; +%ignore shogun::SerializableFile::write_cont_end; +%ignore shogun::SerializableFile::write_item_begin; +%ignore shogun::SerializableFile::write_item_end; +%ignore shogun::SerializableFile::write_scalar; +%ignore shogun::SerializableFile::write_sgserializable_begin; +%ignore shogun::SerializableFile::write_sgserializable_end; +%ignore shogun::SerializableFile::write_sparse_begin; +%ignore shogun::SerializableFile::write_sparse_end; +%ignore shogun::SerializableFile::write_sparseentry_begin; +%ignore shogun::SerializableFile::write_sparseentry_end; +%ignore shogun::SerializableFile::write_string_begin; +%ignore shogun::SerializableFile::write_string_end; +%ignore shogun::SerializableFile::write_stringentry_begin; +%ignore shogun::SerializableFile::write_stringentry_end; +%ignore shogun::SerializableFile::write_type_begin; +%ignore shogun::SerializableFile::write_type_end; +>>>>>>> drop C prefix %ignore shogun::CMosek; -%ignore shogun::CFactorType::CFactorType(); -%ignore shogun::CTableFactorType::CTableFactorType(); -%ignore shogun::CFactorDataSource::CFactorDataSource(); -%ignore shogun::CFactor::CFactor(); -%ignore shogun::CFactor::CFactor(CTableFactorType*, SGVector, SGSparseVector); -%ignore shogun::CFactor::CFactor(CTableFactorType*, SGVector, CFactorDataSource*); +%ignore shogun::FactorType::FactorType(); +%ignore shogun::TableFactorType::TableFactorType(); +%ignore shogun::FactorDataSource::FactorDataSource(); +%ignore shogun::Factor::Factor(); +%ignore shogun::Factor::Factor(TableFactorType*, SGVector, SGSparseVector); +%ignore shogun::Factor::Factor(TableFactorType*, SGVector, FactorDataSource*); %ignore shogun::CDisjointSet::CDisjointSet(); -%ignore shogun::CFactorGraph::CFactorGraph(); -%ignore shogun::CMAPInference::CMAPInference(); +%ignore shogun::FactorGraph::FactorGraph(); +%ignore shogun::MAPInference::MAPInference(); %ignore shogun::CGraphCut::CGraphCut(); -%ignore shogun::CFactorGraphModel::CFactorGraphModel(); +%ignore shogun::FactorGraphModel::FactorGraphModel(); %ignore shogun::Range; diff --git a/src/shogun/base/DynArray.h b/src/shogun/base/DynArray.h index d6484edb98c..8cb9155a28a 100644 --- a/src/shogun/base/DynArray.h +++ b/src/shogun/base/DynArray.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Evgeniy Andreev, Thoralf Klein, - * Evan Shelhamer, Yuyu Zhang, Weijie Lin, Fernando Iglesias, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Evgeniy Andreev, Thoralf Klein, + * Evan Shelhamer, Yuyu Zhang, Weijie Lin, Fernando Iglesias, * Bjoern Esser, Sergey Lisitsyn */ @@ -16,7 +16,7 @@ namespace shogun { -template class CDynamicArray; +template class DynamicArray; /** @brief Template Dynamic array class that creates an array that can * be used like a list or an array. @@ -28,9 +28,9 @@ template class CDynamicArray; */ template class DynArray { - template friend class CDynamicArray; - friend class CDynamicObjectArray; - friend class CCommUlongStringKernel; + template friend class DynamicArray; + friend class DynamicObjectArray; + friend class CommUlongStringKernel; public: typedef RandomIterator iterator; diff --git a/src/shogun/base/Parameter.cpp b/src/shogun/base/Parameter.cpp index d903d1f81eb..1c01dd6c02f 100644 --- a/src/shogun/base/Parameter.cpp +++ b/src/shogun/base/Parameter.cpp @@ -128,7 +128,7 @@ Parameter::add(complex128_t* param, const char* name, } void -Parameter::add(CSGObject** param, +Parameter::add(SGObject** param, const char* name, const char* description) { TSGDataType type(CT_SCALAR, ST_NONE, PT_SGOBJECT); add_type(&type, param, name, description); @@ -348,7 +348,7 @@ Parameter::add_vector( } void -Parameter::add_vector(CSGObject*** param, index_t* length, +Parameter::add_vector(SGObject*** param, index_t* length, const char* name, const char* description) { TSGDataType type(CT_VECTOR, ST_NONE, PT_SGOBJECT, length); @@ -646,7 +646,7 @@ void Parameter::add(SGVector* param, const char* name, add_type(&type, ¶m->vector, name, description); } -void Parameter::add(SGVector* param, const char* name, +void Parameter::add(SGVector* param, const char* name, const char* description) { TSGDataType type(CT_SGVECTOR, ST_NONE, PT_SGOBJECT, ¶m->vlen); @@ -882,7 +882,7 @@ Parameter::add_matrix( void Parameter::add_matrix( - CSGObject*** param, index_t* length_y, index_t* length_x, + SGObject*** param, index_t* length_y, index_t* length_x, const char* name, const char* description) { TSGDataType type(CT_MATRIX, ST_NONE, PT_SGOBJECT, length_y, length_x); @@ -1130,7 +1130,7 @@ void Parameter::add(SGMatrix* param, const char* name, add_type(&type, ¶m->matrix, name, description); } -void Parameter::add(SGMatrix* param, const char* name, +void Parameter::add(SGMatrix* param, const char* name, const char* description) { TSGDataType type(CT_SGMATRIX, ST_NONE, PT_SGOBJECT, ¶m->num_rows, @@ -1362,7 +1362,7 @@ void Parameter::add(SGSparseMatrix* param, add_type(&type, ¶m->sparse_matrix, name, description); } -void Parameter::add(SGSparseMatrix* param, +void Parameter::add(SGSparseMatrix* param, const char* name, const char* description) { TSGDataType type(CT_SGMATRIX, ST_SPARSE, PT_SGOBJECT, ¶m->num_vectors, @@ -1399,12 +1399,13 @@ TParameter::new_prefix(const char* s1, const char* s2) } bool -TParameter::new_sgserial(CSGObject** param, +TParameter::new_sgserial(SGObject** param, EPrimitiveType generic, const char* sgserializable_name, const char* prefix) { - if (*param != NULL) + //FIXME +/* if (*param != NULL) SG_UNREF(*param); *param = create(sgserializable_name, generic); @@ -1428,6 +1429,7 @@ TParameter::new_sgserial(CSGObject** param, } SG_REF(*param); + */ return true; } @@ -1445,7 +1447,7 @@ void TParameter::get_incremental_hash( uint8_t* data = ((uint8_t*) m_parameter); uint32_t size = m_datatype.sizeof_stype(); total_length += size; - CHash::IncrementalMurmurHash3( + Hash::IncrementalMurmurHash3( &hash, &carry, data, size); break; } @@ -1504,7 +1506,7 @@ void TParameter::get_incremental_hash( uint8_t* data = (*(uint8_t**) m_parameter); - CHash::IncrementalMurmurHash3( + Hash::IncrementalMurmurHash3( &hash, &carry, data, size); break; } @@ -1615,28 +1617,30 @@ void Parameter::set_from_parameters(Parameter* params) { if (own->m_datatype.m_ctype==CT_SCALAR) { - CSGObject** to_unref=(CSGObject**) own->m_parameter; - CSGObject** to_ref=(CSGObject**) current->m_parameter; + SGObject** to_unref=(SGObject**) own->m_parameter; + SGObject** to_ref=(SGObject**) current->m_parameter; if ((*to_ref)!=(*to_unref)) { - SG_REF((*to_ref)); - SG_UNREF((*to_unref)); + //FIXME + //SG_REF((*to_ref)); + //SG_UNREF((*to_unref)); } } else { /* unref all SGObjects and reference the new ones */ - CSGObject*** to_unref=(CSGObject***) own->m_parameter; - CSGObject*** to_ref=(CSGObject***) current->m_parameter; + SGObject*** to_unref=(SGObject***) own->m_parameter; + SGObject*** to_ref=(SGObject***) current->m_parameter; for (index_t j=0; jm_datatype.get_num_elements(); ++j) { if ((*to_ref)[j]!=(*to_unref)[j]) { - SG_REF(((*to_ref)[j])); - SG_UNREF(((*to_unref)[j])); + //FIXME + //SG_REF(((*to_ref)[j])); + //SG_UNREF(((*to_unref)[j])); } } } @@ -1655,12 +1659,12 @@ void Parameter::set_from_parameters(Parameter* params) dest=own->m_parameter; source=current->m_parameter; - /* in case of CSGObject, pointers are not equal if CSGObjects are + /* in case of SGObject, pointers are not equal if SGObjects are * equal, so check. For other values, the pointers are equal and * the not-copying is handled below before the memcpy call */ if (own->m_datatype.m_ptype==PT_SGOBJECT) { - if (*((CSGObject**)dest) == *((CSGObject**)source)) + if (*((SGObject**)dest) == *((SGObject**)source)) { dest=NULL; source=NULL; @@ -1680,8 +1684,8 @@ void Parameter::set_from_parameters(Parameter* params) source=*((float64_t**) current->m_parameter); break; case PT_SGOBJECT: - dest=*((CSGObject**) own->m_parameter); - source=*((CSGObject**) current->m_parameter); + dest=*((SGObject**) own->m_parameter); + source=*((SGObject**) current->m_parameter); break; default: not_implemented(SOURCE_LOCATION); diff --git a/src/shogun/base/Parameter.h b/src/shogun/base/Parameter.h index c339116a934..8dc96dbfeeb 100644 --- a/src/shogun/base/Parameter.h +++ b/src/shogun/base/Parameter.h @@ -16,7 +16,7 @@ namespace shogun { -class CSGObject; +class SGObject; template class SGMatrix; template class SGSparseMatrix; template class SGVector; @@ -75,14 +75,14 @@ struct TParameter char* new_prefix(const char* s1, const char* s2); void delete_cont(); void new_cont(SGVector dims); - bool new_sgserial(CSGObject** param, EPrimitiveType generic, + bool new_sgserial(SGObject** param, EPrimitiveType generic, const char* sgserializable_name, const char* prefix); }; /** @brief Parameter class * - * Must not be an CSGObject to prevent a recursive call of + * Must not be an SGObject to prevent a recursive call of * constructors. */ class Parameter @@ -103,9 +103,9 @@ class Parameter /** Takes another Parameter instance and sets all parameters of this * instance (with equal name) to the values of the provided one. - * (Note that if CSGObjects are replaced, the old ones are SG_UNREFed + * (Note that if SGObjects are replaced, the old ones are SG_UNREFed * and the new ones are SG_REFed) - * Currently only works for any float64_t and CSGObject type. + * Currently only works for any float64_t and SGObject type. * * @param params another Parameter instance */ @@ -261,15 +261,15 @@ class Parameter * @param name name of parameter * @param description description of parameter */ - void add(CSGObject** param, + void add(SGObject** param, const char* name, const char* description=""); - template ::value, + template ::value, T>* = nullptr> void add(T** param, const char* name, const char* description = "") { TSGDataType type(CT_SCALAR, ST_NONE, PT_SGOBJECT); - add_type(&type, (CSGObject**)param, name, description); + add_type(&type, (SGObject**)param, name, description); } /** add param @@ -492,7 +492,7 @@ class Parameter * @param name name of parameter * @param description description of parameter */ - void add_vector(CSGObject*** param, index_t* length, + void add_vector(SGObject*** param, index_t* length, const char* name, const char* description=""); /** add vector param * @param param parameter vector itself @@ -815,7 +815,7 @@ class Parameter * @param name name of parameter * @param description description of parameter */ - void add(SGVector* param, const char* name, + void add(SGVector* param, const char* name, const char* description=""); /** add vector param * @param param parameter vector itself @@ -1066,7 +1066,7 @@ class Parameter * @param name name of parameter * @param description description of parameter */ - void add_matrix(CSGObject*** param, + void add_matrix(SGObject*** param, index_t* length_y, index_t* length_x, const char* name, const char* description=""); /** add matrix param @@ -1312,7 +1312,7 @@ class Parameter * @param name name of parameter * @param description description of parameter */ - void add(SGMatrix* param, const char* name, + void add(SGMatrix* param, const char* name, const char* description=""); /** add matrix param * @param param parameter matrix itself @@ -1516,7 +1516,7 @@ class Parameter * @param name name of parameter * @param description description of parameter */ - void add(SGSparseMatrix* param, + void add(SGSparseMatrix* param, const char* name, const char* description=""); protected: diff --git a/src/shogun/base/SGObject.cpp b/src/shogun/base/SGObject.cpp index 59693f2d797..b3807a2b829 100644 --- a/src/shogun/base/SGObject.cpp +++ b/src/shogun/base/SGObject.cpp @@ -7,7 +7,6 @@ * Leon Kuchenbecker, Sanuj Sharma, Wu Lin */ -#include #include #include @@ -61,7 +60,7 @@ namespace shogun typedef std::map ParametersMap; typedef std::unordered_map ObsParamsList; - class CSGObject::Self + class SGObject::Self { public: void create(const BaseTag& tag, const AnyParameter& parameter) @@ -126,81 +125,81 @@ namespace shogun class Parallel; - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_BOOL; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_CHAR; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_INT8; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_UINT8; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_INT16; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_UINT16; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_INT32; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_UINT32; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_INT64; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_UINT64; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_FLOAT32; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_FLOAT64; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_FLOATMAX; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_SGOBJECT; } - template<> void CSGObject::set_generic() + template<> void SGObject::set_generic() { m_generic = PT_COMPLEX128; } - class CSGObject::ParameterObserverList + class SGObject::ParameterObserverList { public: void register_param(std::string_view name, std::string_view description) @@ -220,79 +219,43 @@ namespace shogun using namespace shogun; -CSGObject::CSGObject() : self(), param_obs_list() +SGObject::SGObject() : self(), param_obs_list() { init(); - m_refcount = new RefCount(0); SG_TRACE("SGObject created ({})", fmt::ptr(this)); } -CSGObject::CSGObject(const CSGObject& orig) +SGObject::SGObject(const SGObject& orig) : self(), param_obs_list() { init(); - m_refcount = new RefCount(0); SG_TRACE("SGObject copied ({})", fmt::ptr(this)); } -CSGObject::~CSGObject() +SGObject::~SGObject() { SG_TRACE("SGObject destroyed ({})", fmt::ptr(this)); delete m_parameters; delete m_model_selection_parameters; delete m_gradient_parameters; - delete m_refcount; - delete m_subject_params; - delete m_observable_params; - delete m_subscriber_params; } -int32_t CSGObject::ref() -{ - int32_t count = m_refcount->ref(); - SG_TRACE("ref() refcount {} obj {} ({}) increased", count, this->get_name(), fmt::ptr(this)); - return m_refcount->ref_count(); -} - -int32_t CSGObject::ref_count() -{ - int32_t count = m_refcount->ref_count(); - SG_TRACE("ref_count(): refcount {}, obj {} ({})", count, this->get_name(), fmt::ptr(this)); - return m_refcount->ref_count(); -} - -int32_t CSGObject::unref() -{ - int32_t count = m_refcount->unref(); - if (count<=0) - { - SG_TRACE("unref() refcount {}, obj {} ({}) destroying", count, this->get_name(), fmt::ptr(this)); - delete this; - return 0; - } - else - { - SG_TRACE("unref() refcount {} obj {} ({}) decreased", count, this->get_name(), fmt::ptr(this)); - return m_refcount->ref_count(); - } -} - -CSGObject * CSGObject::shallow_copy() const +std::shared_ptr SGObject::shallow_copy() const { not_implemented(SOURCE_LOCATION); return NULL; } -CSGObject * CSGObject::deep_copy() const +std::shared_ptr SGObject::deep_copy() const { not_implemented(SOURCE_LOCATION); return NULL; } -void CSGObject::update_parameter_hash() const +void SGObject::update_parameter_hash() const { SG_TRACE("entering"); @@ -301,63 +264,63 @@ void CSGObject::update_parameter_hash() const SG_TRACE("leaving"); } -bool CSGObject::parameter_hash_changed() const +bool SGObject::parameter_hash_changed() const { return (m_hash!=hash()); } -Parallel* CSGObject::get_global_parallel() +Parallel* SGObject::get_global_parallel() { return env(); } -bool CSGObject::is_generic(EPrimitiveType* generic) const +bool SGObject::is_generic(EPrimitiveType* generic) const { *generic = m_generic; return m_generic != PT_NOT_GENERIC; } -void CSGObject::unset_generic() +void SGObject::unset_generic() { m_generic = PT_NOT_GENERIC; } -bool CSGObject::serialize(io::CSerializer* ser) +bool SGObject::serialize(std::shared_ptr ser) { require(ser != nullptr, "Serializer format object should be non-null"); - ser->write(wrap(this)); + ser->write(shared_from_this()); return true; } -bool CSGObject::deserialize(io::CDeserializer* deser) +bool SGObject::deserialize(std::shared_ptr deser) { require(deser != nullptr, "Deserializer format object should be non-null"); - deser->read(this); + deser->read(shared_from_this()); return true; } -void CSGObject::load_serializable_pre() noexcept(false) +void SGObject::load_serializable_pre() noexcept(false) { m_load_pre_called = true; } -void CSGObject::load_serializable_post() noexcept(false) +void SGObject::load_serializable_post() noexcept(false) { m_load_post_called = true; } -void CSGObject::save_serializable_pre() noexcept(false) +void SGObject::save_serializable_pre() noexcept(false) { m_save_pre_called = true; } -void CSGObject::save_serializable_post() noexcept(false) +void SGObject::save_serializable_post() noexcept(false) { m_save_post_called = true; } -void CSGObject::init() +void SGObject::init() { m_parameters = new Parameter(); m_model_selection_parameters = new Parameter(); @@ -369,18 +332,18 @@ void CSGObject::init() m_save_post_called = false; m_hash = 0; - m_subject_params = new SGSubject(); - m_observable_params = new SGObservable(m_subject_params->get_observable()); - m_subscriber_params = new SGSubscriber(m_subject_params->get_subscriber()); + m_subject_params = std::make_shared(); + m_observable_params = std::make_shared(m_subject_params->get_observable()); + m_subscriber_params = std::make_shared(m_subject_params->get_subscriber()); m_next_subscription_index = 0; - watch_method("num_subscriptions", &CSGObject::get_num_subscriptions); + watch_method("num_subscriptions", &SGObject::get_num_subscriptions); } #ifdef SWIG -std::string CSGObject::get_description(const std::string& name) const +std::string SGObject::get_description(const std::string& name) const #else -std::string CSGObject::get_description(std::string_view name) const +std::string SGObject::get_description(std::string_view name) const #endif { auto it = self->map.find(BaseTag(name)); @@ -397,7 +360,7 @@ std::string CSGObject::get_description(std::string_view name) const } } -void CSGObject::print_modsel_params() +void SGObject::print_modsel_params() { io::print("parameters available for model selection for {}:\n", get_name()); @@ -421,7 +384,7 @@ void CSGObject::print_modsel_params() } } -void CSGObject::build_gradient_parameter_dictionary(CMap* dict) +void SGObject::build_gradient_parameter_dictionary(std::shared_ptr> dict) { for (index_t i=0; iget_num_parameters(); i++) { @@ -432,7 +395,7 @@ void CSGObject::build_gradient_parameter_dictionary(CMapget_num_parameters(); i++) { TParameter* p=m_model_selection_parameters->get_parameter(i); - CSGObject* child=*(CSGObject**)(p->m_parameter); + SGObject* child=*(SGObject**)(p->m_parameter); if ((p->m_datatype.m_ptype == PT_SGOBJECT) && (p->m_datatype.m_ctype == CT_SCALAR) && child) @@ -442,19 +405,19 @@ void CSGObject::build_gradient_parameter_dictionary(CMap SGObject::clone(ParameterProperties pp) const { SG_DEBUG("Starting to clone {} at {}.", get_name(), fmt::ptr(this)); SG_DEBUG("Constructing an empty instance of {}.", get_name()); - CSGObject* clone = create_empty(); - SG_DEBUG("Empty instance of {} created at {}.", get_name(), fmt::ptr(clone)); + auto clone = create_empty(); + SG_DEBUG("Empty instance of {} created at {}.", get_name(), fmt::ptr(clone.get())); require( clone, "Could not create empty instance of {}. The reason for " "this usually is that get_name() of the class returns something " "wrong, that a class has a wrongly set generic type, or that it " "lies outside the main source tree and does not have " - "CSGObject::create_empty() overridden.\n", + "SGObject::create_empty() overridden.\n", get_name()); for (const auto &it : self->filter(pp)) @@ -477,17 +440,17 @@ CSGObject* CSGObject::clone(ParameterProperties pp) const clone->get_parameter(tag).get_value().clone_from(own); } - SG_DEBUG("Done cloning {} at {}, new object at {}.", get_name(), fmt::ptr(this), fmt::ptr(clone)); + SG_DEBUG("Done cloning {} at {}, new object at {}.", get_name(), fmt::ptr(this), fmt::ptr(clone.get())); return clone; } -void CSGObject::create_parameter( +void SGObject::create_parameter( const BaseTag& _tag, const AnyParameter& parameter) { self->create(_tag, parameter); } -void CSGObject::update_parameter(const BaseTag& _tag, const Any& value) +void SGObject::update_parameter(const BaseTag& _tag, const Any& value) { auto& param = self->at(_tag); auto& pprop = param.get_properties(); @@ -513,7 +476,7 @@ void CSGObject::update_parameter(const BaseTag& _tag, const Any& value) pprop.remove_property(ParameterProperties::AUTO); } -AnyParameter CSGObject::get_parameter(const BaseTag& _tag) const +AnyParameter SGObject::get_parameter(const BaseTag& _tag) const { const auto& parameter = self->get(_tag); if (parameter.get_properties().has_property( @@ -533,7 +496,7 @@ AnyParameter CSGObject::get_parameter(const BaseTag& _tag) const return parameter; } -AnyParameter CSGObject::get_function(const BaseTag& _tag) const +AnyParameter SGObject::get_function(const BaseTag& _tag) const { const auto& parameter = self->get(_tag); if (!parameter.get_properties().has_property( @@ -553,12 +516,12 @@ AnyParameter CSGObject::get_function(const BaseTag& _tag) const return parameter; } -bool CSGObject::has_parameter(const BaseTag& _tag) const +bool SGObject::has_parameter(const BaseTag& _tag) const { return self->has(_tag); } -void CSGObject::add_callback_function( +void SGObject::add_callback_function( std::string_view name, std::function function) { require(function, "Function object is not callable"); @@ -571,7 +534,7 @@ void CSGObject::add_callback_function( param.add_callback_function(std::move(function)); } -void CSGObject::subscribe(ParameterObserver* obs) +void SGObject::subscribe(std::shared_ptr obs) { auto sub = rxcpp::make_subscriber( [obs](TimedObservedValue e) { obs->on_next(e); }, @@ -582,7 +545,7 @@ void CSGObject::subscribe(ParameterObserver* obs) // parameters selected by the observable. rxcpp::subscription subscription = m_observable_params - ->filter([obs](Some v) { + ->filter([obs](std::shared_ptr v) { return obs->filter(v->get("name")); }) .timestamp() @@ -598,7 +561,7 @@ void CSGObject::subscribe(ParameterObserver* obs) m_next_subscription_index++; } -void CSGObject::unsubscribe(ParameterObserver* obs) +void SGObject::unsubscribe(std::shared_ptr obs) { int64_t index = obs->get("subscription_id"); @@ -617,24 +580,18 @@ void CSGObject::unsubscribe(ParameterObserver* obs) obs->put("subscription_id", static_cast(-1)); } -void CSGObject::observe(const Some value) const +void SGObject::observe(std::shared_ptr value) const { m_subscriber_params->on_next(value); } -void CSGObject::observe(ObservedValue* value) const -{ - auto somed_value = Some::from_raw(value); - m_subscriber_params->on_next(somed_value); -} - -void CSGObject::register_observable( +void SGObject::register_observable( std::string_view name, std::string_view description) { param_obs_list->register_param(name, description); } -std::vector CSGObject::observable_names() +std::vector SGObject::observable_names() { std::vector list; std::transform( @@ -644,15 +601,15 @@ std::vector CSGObject::observable_names() } #ifdef SWIG -bool CSGObject::has(const std::string& name) const +bool SGObject::has(const std::string& name) const #else -bool CSGObject::has(std::string_view name) const +bool SGObject::has(std::string_view name) const #endif { return has_parameter(BaseTag(name)); } -std::string CSGObject::to_string() const +std::string SGObject::to_string() const { std::stringstream ss; std::unique_ptr visitor(new ToStringVisitor(&ss)); @@ -684,7 +641,7 @@ std::string CSGObject::to_string() const } #ifndef SWIG // SWIG should skip this part -std::map> CSGObject::get_params() const +std::map> SGObject::get_params() const { std::map> result; for (auto const& each: self->map) { @@ -694,7 +651,7 @@ std::map> CSGObject::get_params } #endif -bool CSGObject::equals(const CSGObject* other) const +bool SGObject::equals(const SGObject* other) const { if (other == this) return true; @@ -761,7 +718,7 @@ bool CSGObject::equals(const CSGObject* other) const } template -void CSGObject::for_each_param_of_type( +void SGObject::for_each_param_of_type( std::function operation) { auto visitor = std::make_unique>(operation); @@ -778,17 +735,20 @@ void CSGObject::for_each_param_of_type( }); } -template void shogun::CSGObject::for_each_param_of_type( - std::function); +template void shogun::SGObject::for_each_param_of_type( + std::function); + +bool SGObject::equals(std::shared_ptr other) const +{ + return this->equals(other.get()); +} -CSGObject* CSGObject::create_empty() const +std::shared_ptr SGObject::create_empty() const { - CSGObject* object = create(this->get_name(), this->m_generic); - SG_REF(object); - return object; + return create(this->get_name(), this->m_generic); } -void CSGObject::init_auto_params() +void SGObject::init_auto_params() { auto params = self->filter(ParameterProperties::AUTO); for (const auto& param : params) @@ -798,12 +758,12 @@ void CSGObject::init_auto_params() } #ifdef SWIG -CSGObject* CSGObject::get(const std::string& name, index_t index) const +std::shared_ptr SGObject::get(const std::string& name, index_t index) const #else -CSGObject* CSGObject::get(std::string_view name, index_t index) const +std::shared_ptr SGObject::get(std::string_view name, index_t index) const #endif { - auto* result = sgo_details::get_by_tag(this, name, sgo_details::GetByNameIndex(index)); + auto result = sgo_details::get_by_tag(shared_from_this(), name, sgo_details::GetByNameIndex(index)); if (!result && has(name)) { error( @@ -815,24 +775,24 @@ CSGObject* CSGObject::get(std::string_view name, index_t index) const } #ifndef SWIG -CSGObject* CSGObject::get(std::string_view name, std::nothrow_t) const +std::shared_ptr SGObject::get(std::string_view name, std::nothrow_t) const noexcept { - return sgo_details::get_by_tag(this, name, sgo_details::GetByName()); + return sgo_details::get_by_tag(shared_from_this(), name, sgo_details::GetByName()); } #endif #ifdef SWIG -CSGObject* CSGObject::get(const std::string& name) const noexcept(false) +std::shared_ptr SGObject::get(const std::string& name) const noexcept(false) #else -CSGObject* CSGObject::get(std::string_view name) const noexcept(false) +std::shared_ptr SGObject::get(std::string_view name) const noexcept(false) #endif { if (!has(name)) { error("Parameter {}::{} does not exist.", get_name(), name.data()); } - if (auto* result = get(name, std::nothrow)) + if (auto result = get(name, std::nothrow)) { return result; } @@ -843,7 +803,7 @@ CSGObject* CSGObject::get(std::string_view name) const noexcept(false) return nullptr; } -std::string CSGObject::string_enum_reverse_lookup( +std::string SGObject::string_enum_reverse_lookup( std::string_view param, machine_int_t value) const { auto param_enum_map = m_string_to_enum_map.at(param); @@ -856,13 +816,13 @@ std::string CSGObject::string_enum_reverse_lookup( return std::string(enum_map_it->first); } -void CSGObject::visit_parameter(const BaseTag& _tag, AnyVisitor* v) const +void SGObject::visit_parameter(const BaseTag& _tag, AnyVisitor* v) const { auto p = get_parameter(_tag); p.get_value().visit(v); } -size_t CSGObject::hash() const +size_t SGObject::hash() const { - return std::hash{}(*this); + return std::hash{}(*this); } diff --git a/src/shogun/base/SGObject.h b/src/shogun/base/SGObject.h index 2bb2862cfaf..80749b416b9 100644 --- a/src/shogun/base/SGObject.h +++ b/src/shogun/base/SGObject.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -38,20 +37,19 @@ */ namespace shogun { -class RefCount; class Parallel; class Parameter; class ParameterObserverInterface; class ObservedValue; class ParameterObserver; -class CDynamicObjectArray; +class DynamicObjectArray; template class ObservedValueTemplated; namespace io { - class CDeserializer; - class CSerializer; + class Deserializer; + class Serializer; } #ifndef SWIG @@ -59,7 +57,7 @@ class ObservedValueTemplated; namespace sgo_details { template -bool dispatch_array_type(const CSGObject* obj, std::string_view name, +bool dispatch_array_type(const SGObject* obj, std::string_view name, T2&& lambda); } #endif // DOXYGEN_SHOULD_SKIP_THIS @@ -72,14 +70,6 @@ template class DynArray; using stringToEnumMapType = std::unordered_map>; -/******************************************************************************* - * define reference counter macros - ******************************************************************************/ - -#define SG_REF(x) { if (x) (x)->ref(); } -#define SG_UNREF(x) { if (x) { if ((x)->unref()==0) (x)=NULL; } } -#define SG_UNREF_NO_NULL(x) { if (x) { (x)->unref(); } } - /******************************************************************************* * Macros for registering parameter properties ******************************************************************************/ @@ -100,8 +90,6 @@ SG_FORCED_INLINE const char* convert_string_to_char(const char* name) { \ auto pprop = \ AnyParameterProperties(description, this->m_default_mask); \ - const char* name_char = convert_string_to_char(name); \ - this->m_parameters->add(param, name_char, description); \ this->watch_param(name, param, pprop); \ } @@ -115,14 +103,7 @@ SG_FORCED_INLINE const char* convert_string_to_char(const char* name) mask |= this->m_default_mask; \ AnyParameterProperties pprop = \ AnyParameterProperties(description, mask); \ - const char* name_char = convert_string_to_char(name); \ - this->m_parameters->add(param, name_char, description); \ this->watch_param(name, param, pprop); \ - if (pprop.has_property(ParameterProperties::HYPER)) \ - this->m_model_selection_parameters->add( \ - param, name_char, description); \ - if (pprop.has_property(ParameterProperties::GRADIENT)) \ - this->m_gradient_parameters->add(param, name_char, description); \ } #define SG_ADD5( \ @@ -132,14 +113,7 @@ SG_FORCED_INLINE const char* convert_string_to_char(const char* name) mask |= this->m_default_mask; \ AnyParameterProperties pprop = \ AnyParameterProperties(description, mask); \ - const char* name_char = convert_string_to_char(name); \ - this->m_parameters->add(param, name_char, description); \ this->watch_param(name, param, auto_or_constraint, pprop); \ - if (pprop.has_property(ParameterProperties::HYPER)) \ - this->m_model_selection_parameters->add( \ - param, name_char, description); \ - if (pprop.has_property(ParameterProperties::GRADIENT)) \ - this->m_gradient_parameters->add(param, name_char, description); \ } #define SG_ADD(...) VARARG(SG_ADD, __VA_ARGS__) @@ -160,58 +134,39 @@ SG_FORCED_INLINE const char* convert_string_to_char(const char* name) * * All objects can be cloned and compared (deep copy, recursively) */ -class CSGObject +class SGObject: public std::enable_shared_from_this { public: /** Definition of observed subject */ - typedef rxcpp::subjects::subject> SGSubject; + typedef rxcpp::subjects::subject> SGSubject; /** Definition of observable */ - typedef rxcpp::observable, - rxcpp::dynamic_observable>> + typedef rxcpp::observable, + rxcpp::dynamic_observable>> SGObservable; /** Definition of subscriber */ typedef rxcpp::subscriber< - Some, - rxcpp::observer, void, void, void, void>> + std::shared_ptr, + rxcpp::observer, void, void, void, void>> SGSubscriber; /** default constructor */ - CSGObject(); + SGObject(); /** copy constructor */ - CSGObject(const CSGObject& orig); + SGObject(const SGObject& orig); /** destructor */ - virtual ~CSGObject(); - - /** increase reference counter - * - * @return reference count - */ - int32_t ref(); - - /** display reference counter - * - * @return reference count - */ - int32_t ref_count(); - - /** decrement reference counter and deallocate object if refcount is zero - * before or after decrementing it - * - * @return reference count - */ - int32_t unref(); + virtual ~SGObject(); /** A shallow copy. * All the SGObject instance variables will be simply assigned and SG_REF-ed. */ - virtual CSGObject *shallow_copy() const; + virtual std::shared_ptr shallow_copy() const; /** A deep copy. * All the instance variables will also be copied. */ - virtual CSGObject *deep_copy() const; + virtual std::shared_ptr deep_copy() const; /** Returns the name of the SGSerializable instance. It MUST BE * the CLASS NAME without the prefixed `C'. @@ -255,7 +210,7 @@ class CSGObject * @param ser where to save the object; * @return TRUE if done, otherwise FALSE */ - virtual bool serialize(io::CSerializer* ser); + virtual bool serialize(std::shared_ptr ser); /** Load this object from file. If it will fail (returning FALSE) * then this object will contain inconsistent data and should not @@ -265,7 +220,7 @@ class CSGObject * * @return TRUE if done, otherwise FALSE */ - virtual bool deserialize(io::CDeserializer* deser); + virtual bool deserialize(std::shared_ptr deser); /** get the parallel object * @@ -292,7 +247,7 @@ class CSGObject * * @param dict dictionary of parameters to be built. */ - void build_gradient_parameter_dictionary(CMap* dict); + void build_gradient_parameter_dictionary(std::shared_ptr> dict); /** Checks if object has a class parameter identified by a name. * @@ -320,7 +275,7 @@ class CSGObject * @param name name of the parameter * @return true if the parameter exists with the input name and type */ - template + template ::value>* = nullptr> bool has(std::string_view name) const noexcept(true) { BaseTag tag(name); @@ -330,6 +285,15 @@ class CSGObject return value.has_type(); } + template ::value>* = nullptr> + bool has(std::string_view name) const noexcept(true) + { + BaseTag tag(name); + if (!has_parameter(tag)) + return false; + const Any value = get_parameter(tag).get_value(); + return value.has_type>(); + } #ifndef SWIG /** Setter for a class parameter, identified by a Tag. * Throws an exception if the class does not have such a parameter. @@ -362,7 +326,6 @@ class CSGObject get_name(), _tag.name().c_str(), exc.actual().c_str(), exc.expected().c_str()); } - ref_value(value); update_parameter(_tag, make_any(value)); } else @@ -415,15 +378,15 @@ class CSGObject * @param value value of the parameter */ template ::value>::type, + class X = typename std::enable_if_t::value>, class Z = void> #ifdef SWIG - void put(const std::string& name, T* value) + void put(const std::string& name, std::shared_ptr value) #else - void put(std::string_view name, T* value) + void put(std::string_view name, std::shared_ptr value) #endif { - put(Tag(name), value); + put(Tag>(name), value); } /** Typed appender for an object class parameter of a Shogun base class @@ -433,11 +396,11 @@ class CSGObject * @param value value of the parameter */ template ::value>::type> + class X = typename std::enable_if_t::value>> #ifdef SWIG - void add(const std::string& name, T* value) + void add(const std::string& name, std::shared_ptr value) #else - void add(std::string_view name, T* value) + void add(std::string_view name, std::shared_ptr value) #endif { require( @@ -468,10 +431,10 @@ class CSGObject * @return desired element */ template ::value>::type> - T* get(std::string_view name, index_t index, std::nothrow_t) const + class X = typename std::enable_if_t::value>> + std::shared_ptr get(std::string_view name, index_t index, std::nothrow_t) const { - CSGObject* result = nullptr; + std::shared_ptr result; auto get_lambda = [&index, &result](auto& array) { result = array.at(index); @@ -487,8 +450,8 @@ class CSGObject } template ::value>::type> - T* get(std::string_view name, index_t index) const + class X = typename std::enable_if_t::value>> + std::shared_ptr get(std::string_view name, index_t index) const { auto result = this->get(name, index, std::nothrow); if (!result) @@ -509,9 +472,9 @@ class CSGObject * @return object parameter */ #ifdef SWIG - CSGObject* get(const std::string& name) const noexcept(false); + std::shared_ptr get(const std::string& name) const noexcept(false); #else - CSGObject* get(std::string_view name) const noexcept(false); + std::shared_ptr get(std::string_view name) const noexcept(false); #endif #ifndef SWIG @@ -522,7 +485,7 @@ class CSGObject * @param name name of the parameter * @return object parameter */ - CSGObject* get(std::string_view name, std::nothrow_t) const noexcept; + std::shared_ptr get(std::string_view name, std::nothrow_t) const noexcept; #endif /** Untyped getter for an object array class parameter, identified by a name @@ -535,9 +498,9 @@ class CSGObject * @return object parameter */ #ifdef SWIG - CSGObject* get(const std::string& name, index_t index) const; + std::shared_ptr get(const std::string& name, index_t index) const; #else - CSGObject* get(std::string_view name, index_t index) const; + std::shared_ptr get(std::string_view name, index_t index) const; #endif #ifndef SWIG @@ -546,12 +509,13 @@ class CSGObject * * @param name name of the parameter * @param value value of the parameter - */ + * template ::value>> - void put(std::string_view name, Some value) + void put(std::string_view name, std::shared_ptr value) { - put(name, value.get()); + put(name, value); } + */ /** Typed appender for an object class parameter of a Shogun base class * type, @@ -559,12 +523,13 @@ class CSGObject * * @param name name of the parameter * @param value value of the parameter - */ + * template ::value>> - void add(std::string_view name, Some value) + void add(std::string_view name, std::shared_ptr value) { - add(name, value.get()); + add(name, value); } + */ #endif // SWIG /** Typed setter for a non-object class parameter, identified by a name. @@ -575,7 +540,7 @@ class CSGObject template ::type>::value, + SGObject, typename std::remove_pointer::type>::value, T>::type> #ifdef SWIG void put(const std::string& name, T value) @@ -596,7 +561,7 @@ class CSGObject * @param _tag name and type information of parameter * @return value of the parameter identified by the input tag */ - template ::value>* = nullptr> + template ::value && !is_sg_base::value>* = nullptr> T get(const Tag& _tag) const noexcept(false) { const Any value = get_parameter(_tag).get_value(); @@ -616,6 +581,27 @@ class CSGObject return any_cast(value); } + template ::value>* = nullptr> + std::shared_ptr get(const Tag& _tag) const noexcept(false) + { + const Any value = get_parameter(_tag).get_value(); + try + { + return any_cast>(value); + } + catch (const TypeMismatchException& exc) + { + error( + "Cannot get parameter {}::{} of type {}, incompatible " + "requested type {} or there are no options for parameter " + "{}::{}.", + get_name(), _tag.name().c_str(), exc.actual().c_str(), + exc.expected().c_str(), get_name(), _tag.name().c_str()); + } + // we won't be there + return nullptr; + } + template ::value>* = nullptr> T get(const Tag& _tag) const noexcept(false) { @@ -646,7 +632,7 @@ class CSGObject * @param name name of the parameter * @return value of the parameter corresponding to the input name and type */ - template + template ::value>> #ifdef SWIG T get(const std::string& name) const noexcept(false) #else @@ -676,6 +662,14 @@ class CSGObject } } +#ifndef SWIG + template ::value>* = nullptr> + std::shared_ptr get(std::string_view name) const noexcept(false) + { + Tag tag(name); + return get(tag); + } +#endif /** Returns string representation of the object that contains * its name and parameters. * @@ -701,10 +695,10 @@ class CSGObject /** Specializes a provided object to the specified type. * Throws exception if the object cannot be specialized. * - * @param sgo object of CSGObject base type + * @param sgo object of SGObject base type * @return The requested type */ - template static T* as(CSGObject* sgo) + template static T* as(std::shared_ptr sgo) { require(sgo, "No object provided!"); return sgo->as(); @@ -715,9 +709,10 @@ class CSGObject * * @return The requested type */ - template T* as() + template + inline std::shared_ptr as() { - auto c = dynamic_cast(this); + auto c = std::dynamic_pointer_cast(shared_from_this()); if (c) return c; @@ -727,26 +722,41 @@ class CSGObject demangled_type().c_str()); return nullptr; } + + template + inline std::shared_ptr as() const + { + auto c = std::dynamic_pointer_cast(shared_from_this()); + if (c) + return c; + + error( + "Object of type {} cannot be converted to type {}.", + this->get_name(), + demangled_type().c_str()); + return nullptr; + } + #ifndef SWIG /** * Get parameters observable * @return RxCpp observable */ - SGObservable* get_parameters_observable() + std::shared_ptr get_parameters_observable() { return m_observable_params; }; #endif /** Subscribe a parameter observer to watch over params */ - void subscribe(ParameterObserver* obs); + void subscribe(std::shared_ptr obs); /** * Detach an observer from the current SGObject. * @param subscription_index the index obtained by calling the subscribe * procedure */ - void unsubscribe(ParameterObserver* obs); + void unsubscribe(std::shared_ptr obs); /** Print to stdout a list of observable parameters */ std::vector observable_names(); @@ -976,7 +986,7 @@ class CSGObject /** Puts a pointer to a (lazily evaluated) function into the parameter map. * The bound function can modify the class members and can only be - * invoked using CSGObject::run(name). + * invoked using SGObject::run(name). * * @param name name of the parameter * @param method pointer to the method @@ -1016,14 +1026,21 @@ class CSGObject * @param other object to compare with * @return true if all parameters are equal */ - virtual bool equals(const CSGObject* other) const; + virtual bool equals(const SGObject* other) const; + + /** Deep comparison of two objects. + * + * @param other object to compare with + * @return true if all parameters are equal + */ + virtual bool equals(std::shared_ptr other) const; /** Creates a clone of the current object. This is done via recursively * traversing all parameters, which corresponds to a deep copy. * * @return Cloned object */ - virtual CSGObject* clone(ParameterProperties pp = ParameterProperties::ALL) const; + virtual std::shared_ptr clone(ParameterProperties pp = ParameterProperties::ALL) const; /** * Looks up the option name of a parameter given the enum value. @@ -1039,14 +1056,14 @@ class CSGObject protected: /** Returns an empty instance of own type. * - * When inheriting from CSGObject from outside the main source tree (i.e. + * When inheriting from SGObject from outside the main source tree (i.e. * customized classes, or in a unit test), then this method has to be * overloaded manually to return an empty instance. * Shogun can only instantiate empty class instances from its source tree. * * @return empty instance of own type */ - virtual CSGObject* create_empty() const; + virtual std::shared_ptr create_empty() const; /** Initialises all parameters with ParameterProperties::AUTO flag */ void init_auto_params(); @@ -1068,23 +1085,6 @@ class CSGObject private: void init(); - /** Overloaded helper to increase reference counter */ - static void ref_value(CSGObject* value) - { - SG_REF(value); - } - - /** Overloaded helper to increase reference counter - * Here a no-op for non CSGobject pointer parameters */ - template ::type>::value, - T>* = nullptr> - static void ref_value(T value) - { - } - /** Checks if object has a parameter identified by a BaseTag. * This only checks for name and not type information. * See its usage in has() and has(). @@ -1144,13 +1144,13 @@ class CSGObject * Observe a parameter value and emit them to observer. * @param value Observed parameter's value */ - void observe(const Some value) const; +// void observe(const std::shared_ptr value) const; /** * Observe a parameter value, given a pointer. * @param value Observed parameter's value */ - void observe(ObservedValue* value) const; + void observe(std::shared_ptr value) const; /** * Observe a parameter value given custom properties for the Any. @@ -1171,7 +1171,7 @@ class CSGObject if (get_num_subscriptions() == 0) return; - auto obs = new ObservedValueTemplated( + auto obs = std::make_shared>( step, name, static_cast(clone_utils::clone(value)), properties); this->observe(obs); } @@ -1260,16 +1260,14 @@ class CSGObject bool m_save_pre_called; bool m_save_post_called; - RefCount* m_refcount; - /** Subject used to create the params observer */ - SGSubject* m_subject_params; + std::shared_ptr m_subject_params; /** Parameter Observable */ - SGObservable* m_observable_params; + std::shared_ptr m_observable_params; /** Subscriber used to call onNext, onComplete etc.*/ - SGSubscriber* m_subscriber_params; + std::shared_ptr m_subscriber_params; /** List of subscription for this SGObject */ std::map m_subscriptions; @@ -1277,21 +1275,21 @@ class CSGObject }; template -T* make_clone(T* orig, ParameterProperties pp = ParameterProperties::ALL) +std::shared_ptr make_clone(std::shared_ptr orig, ParameterProperties pp = ParameterProperties::ALL) { require(orig, "No object provided."); auto clone = orig->clone(pp); ASSERT(clone); - return static_cast(clone); + return std::static_pointer_cast(clone); } template -const T* make_clone(const T* orig, ParameterProperties pp = ParameterProperties::ALL) +std::shared_ptr make_clone(std::shared_ptr orig, ParameterProperties pp = ParameterProperties::ALL) { require(orig, "No object provided."); auto clone = orig->clone(pp); ASSERT(clone); - return static_cast(clone); + return std::static_pointer_cast(clone); } #ifndef SWIG @@ -1299,9 +1297,9 @@ const T* make_clone(const T* orig, ParameterProperties pp = ParameterProperties: namespace sgo_details { template -bool dispatch_array_type(const CSGObject* obj, std::string_view name, T2&& lambda) +bool dispatch_array_type(const SGObject* obj, std::string_view name, T2&& lambda) { - Tag tag_array_sg(name); + Tag> tag_array_sg(name); if (obj->has(tag_array_sg)) { auto dispatched = obj->get(tag_array_sg); @@ -1309,7 +1307,7 @@ bool dispatch_array_type(const CSGObject* obj, std::string_view name, T2&& lambd return true; } - Tag> tag_vector(name); + Tag>> tag_vector(name); if (obj->has(tag_vector)) { auto dispatched = obj->get(tag_vector); @@ -1331,39 +1329,39 @@ struct GetByNameIndex }; template -CSGObject* get_if_possible(const CSGObject* obj, std::string_view name, GetByName) +std::shared_ptr get_if_possible(const std::shared_ptr& obj, std::string_view name, GetByName) { - return obj->has(name) ? obj->get(name) : nullptr; + return obj->has(name) ? obj->get(name) : nullptr; } template -CSGObject* get_if_possible(const CSGObject* obj, std::string_view name, GetByNameIndex how) +std::shared_ptr get_if_possible(const std::shared_ptr& obj, std::string_view name, GetByNameIndex how) { - CSGObject* result = nullptr; + std::shared_ptr result = nullptr; result = obj->get(name, how.m_index, std::nothrow); return result; } template -CSGObject* get_dispatch_all_base_types(const CSGObject* obj, std::string_view name, +std::shared_ptr get_dispatch_all_base_types(const std::shared_ptr& obj, std::string_view name, T&& how) { - if (auto* result = get_if_possible(obj, name, how)) + if (auto result = get_if_possible(obj, name, how)) return result; - if (auto* result = get_if_possible(obj, name, how)) + if (auto result = get_if_possible(obj, name, how)) return result; - if (auto* result = get_if_possible(obj, name, how)) + if (auto result = get_if_possible(obj, name, how)) return result; - if (auto* result = get_if_possible(obj, name, how)) + if (auto result = get_if_possible(obj, name, how)) return result; - if (auto* result = get_if_possible(obj, name, how)) + if (auto result = get_if_possible(obj, name, how)) return result; return nullptr; } template -CSGObject* get_by_tag(const CSGObject* obj, std::string_view name, +std::shared_ptr get_by_tag(const std::shared_ptr& obj, std::string_view name, T&& how) { return get_dispatch_all_base_types(obj, name, how); diff --git a/src/shogun/base/ShogunEnv.cpp b/src/shogun/base/ShogunEnv.cpp index 950609ed136..fbde584d259 100644 --- a/src/shogun/base/ShogunEnv.cpp +++ b/src/shogun/base/ShogunEnv.cpp @@ -35,7 +35,7 @@ ShogunEnv::ShogunEnv() { sg_io = std::make_unique(); sg_linalg = std::make_unique(); - sg_signal = std::make_unique(); + sg_signal = std::make_unique(); sg_fequals_epsilon = 0.0; sg_fequals_tolerant = false; @@ -47,9 +47,9 @@ ShogunEnv::ShogunEnv() ShogunEnv::~ShogunEnv() { - delete CSignal::m_subscriber; - delete CSignal::m_observable; - delete CSignal::m_subject; + delete Signal::m_subscriber; + delete Signal::m_observable; + delete Signal::m_subject; #ifdef HAVE_PROTOBUF ::google::protobuf::ShutdownProtobufLibrary(); @@ -126,7 +126,7 @@ bool ShogunEnv::fequals_tolerant() return sg_fequals_tolerant; } -CSignal* ShogunEnv::signal() +Signal* ShogunEnv::signal() { return sg_signal.get(); } diff --git a/src/shogun/base/ShogunEnv.h b/src/shogun/base/ShogunEnv.h index 0a2d51812d6..efea7c955de 100644 --- a/src/shogun/base/ShogunEnv.h +++ b/src/shogun/base/ShogunEnv.h @@ -23,7 +23,7 @@ namespace shogun class SGIO; } class SGLinalg; - class CSignal; + class Signal; class ShogunEnv : public io::FileSystemRegistry, public Parallel, public Version { @@ -50,7 +50,7 @@ namespace shogun float64_t fequals_epsilon(); /** Globally over-ride the floating point epsilon for CMath::fequals. - * Hack required for CSGObject::equals checks for certain serialization + * Hack required for SGObject::equals checks for certain serialization * formats. * @param fequals_epsilon new epsilon to use */ @@ -61,7 +61,7 @@ namespace shogun bool fequals_tolerant(); /** Globally enable linient check for CMath::fequals. - * Hack required for CSGObject::equals checks for certain serialization + * Hack required for SGObject::equals checks for certain serialization * formats. * @param fequals_tolerant whether or not to use tolerant check */ @@ -78,7 +78,7 @@ namespace shogun * * @return linalg object */ - CSignal* signal(); + Signal* signal(); #endif private: @@ -91,7 +91,7 @@ namespace shogun void init_from_env(); std::unique_ptr sg_io; - std::unique_ptr sg_signal; + std::unique_ptr sg_signal; std::unique_ptr sg_linalg; float64_t sg_fequals_epsilon; bool sg_fequals_tolerant; diff --git a/src/shogun/base/base_types.h b/src/shogun/base/base_types.h index d766e8e8fdb..54e8262f719 100644 --- a/src/shogun/base/base_types.h +++ b/src/shogun/base/base_types.h @@ -7,57 +7,59 @@ #ifndef BASE_TYPES_H #define BASE_TYPES_H +#include + namespace shogun { // all shogun base classes for put/add templates - class CMachine; - class CKernel; - class CDistance; - class CFeatures; - class CLabels; - class CECOCEncoder; - class CECOCDecoder; - class CEvaluation; - class CEvaluationResult; - class CMulticlassStrategy; - class CNeuralLayer; - class CSplittingStrategy; - class CPipeline; - class CSVM; - class CLikelihoodModel; - class CMeanFunction; - class CDifferentiableFunction; - class CInference; - class CLossFunction; - class CTokenizer; - class CCombinationRule; + class Machine; + class Kernel; + class Distance; + class Features; + class Labels; + class ECOCEncoder; + class ECOCDecoder; + class Evaluation; + class EvaluationResult; + class MulticlassStrategy; + class NeuralLayer; + class SplittingStrategy; + class Pipeline; + class SVM; + class LikelihoodModel; + class MeanFunction; + class DifferentiableFunction; + class Inference; + class LossFunction; + class Tokenizer; + class CombinationRule; // type trait to enable certain methods only for shogun base types // FIXME: use sg_interface to populate this trait template struct is_sg_base : std::integral_constant< - bool, std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value || - std::is_same::value> + bool, std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value || + std::is_same::value> { }; @@ -107,12 +109,12 @@ namespace shogun typedef T type; }; - using sg_inferface = type_list; + using sg_inferface = type_list; template constexpr auto find_base(type_list<>) @@ -131,6 +133,21 @@ namespace shogun template using base_type = typename decltype(find_base(sg_inferface{}))::type; + template + struct remove_shared_ptr + { + using type = T; + }; + + template + struct remove_shared_ptr> + { + using type = T; + }; + + template + using remove_shared_ptr_t = typename remove_shared_ptr::type; + } // namespace shogun #endif // BASE_TYPES__H diff --git a/src/shogun/base/class_list.cpp.py b/src/shogun/base/class_list.cpp.py index 1f7b962da71..fbb9fad31fc 100644 --- a/src/shogun/base/class_list.cpp.py +++ b/src/shogun/base/class_list.cpp.py @@ -40,6 +40,29 @@ "USE_BZIP2", "USE_LZMA", "USE_MOSEK", "HAVE_COLPACK", "HAVE_NLOPT", "HAVE_PROTOBUF", "HAVE_VIENNACL", "USE_GPL_SHOGUN", "USE_META_INTEGRATION_TESTS", "HAVE_TFLOGGER"] +# TODO: remove once plugins are working +class_blacklist = ["SGVector", "SGMatrix", "SGSparseVector", "SGSparseMatrix", + "SGStringList", "SGMatrixList", "SGCachedVector", "SGNDArray", + "ObservedValue", "ObservedValueTemplated", "ParameterObserverCV", + "ParameterObserverHistogram", "ParameterObserverScalar", "ParameterObserverTensorBoard", + "TBOutputFormat", "Iterator", "Wrapper", "PIterator", + "BitPackedFlatHashTableError", "TypedAnyPolicy", "NonOwningAnyPolicy", + "PointerValueAnyPolicy", "InvalidStateException", "NotFittedException", + "ShogunException", "BitPackedVectorError", "CompositeHashTableError", + "DataStorageError", "DataTransformationError", "AugmentedHeap", + "HashTableError", "LSHTableError", "LSHFunctionError", + "NearestNeighborQueryError", "StaticProbingHashTableError", "DynamicProbingHashTableError", + "FalconnError", "LSHNearestNeighborTableError", "LSHNNTableWrapper", + "Tag", "LinalgBackendGPUBase", "LinalgBackendEigen", "LinalgBackendViennaCL", + "TypeMismatchException", "FlatHashTableError", "SimpleHeap", "LSHNNTableSetupError", + "KDTREEKNNSolver", "LDACanVarSolver", + "GammaFeatureNumberInit", "StdVectorPrefetcher", "ShogunNotImplementedException", + "ToStringVisitor", "BitseryVisitor", "FileSystem", "PosixFileSystem", + "WindowsFileSystem", "LocalWindowsFileSystem", "LocalPosixFileSystem", + "NullFileSystem", "FilterVisitor", "RandomMixin", "MaxCrossValidation", + "StreamingDataFetcher", "MaxMeasure", "MaxTestPower", + "MedianHeuristic", "WeightedMaxMeasure", "WeightedMaxTestPower", + "Seedable", "ShogunEnv"] SHOGUN_TEMPLATE_CLASS = "SHOGUN_TEMPLATE_CLASS" SHOGUN_BASIC_CLASS = "SHOGUN_BASIC_CLASS" @@ -91,18 +114,15 @@ def extract_class_name(lines, line_nr, line, blacklist): c = c.strip(':').strip() - if not c.startswith('C'): - return if c.endswith(';'): return if '>' in c: return - if not (len(c) > 2 and c[1].isupper()): + if not (len(c) > 2 and c[0].isupper()): return - if check_is_in_blacklist(c[1:], lines, line_nr, blacklist): + if check_is_in_blacklist(c, lines, line_nr, blacklist) or (c in class_blacklist): return - - return c[1:] + return c def get_includes(classes, headers_absolute_fnames): @@ -136,7 +156,7 @@ def get_definitions(classes): definitions.append("#define %s" % SHOGUN_TEMPLATE_CLASS) definitions.append("#define %s" % SHOGUN_BASIC_CLASS) for c, t in classes: - d = "static %s CSGObject* __new_C%s(EPrimitiveType g) { return g == PT_NOT_GENERIC? new C%s(): NULL; }" % (SHOGUN_BASIC_CLASS,c,c) + d = "static %s SGObject* __new_%s(EPrimitiveType g) { return g == PT_NOT_GENERIC? new %s(): NULL; }" % (SHOGUN_BASIC_CLASS,c,c) definitions.append(d) return definitions @@ -145,7 +165,7 @@ def get_template_definitions(classes, supports_complex): definitions = [] for c, t in classes: d = [] - d.append("static %s CSGObject* __new_C%s(EPrimitiveType g)\n{\n\tswitch (g)\n\t{\n" + d.append("static %s SGObject* __new_%s(EPrimitiveType g)\n{\n\tswitch (g)\n\t{\n" % (SHOGUN_TEMPLATE_CLASS, c)) for t in types: if t in ('BOOL', 'CHAR'): @@ -155,7 +175,7 @@ def get_template_definitions(classes, supports_complex): if t == 'COMPLEX128' and not supports_complex: d.append("\t\tcase PT_COMPLEX128: return NULL;\n") else: - d.append("\t\tcase PT_%s: return new C%s<%s%s>();\n" + d.append("\t\tcase PT_%s: return new %s<%s%s>();\n" % (t, c, t.lower(), suffix)) d.append("\t\tcase PT_SGOBJECT:\n") d.append("\t\tcase PT_UNDEFINED: return NULL;\n\t}\n\treturn NULL;\n}") @@ -170,7 +190,7 @@ def get_struct(classes): if template: prefix = SHOGUN_TEMPLATE_CLASS - s = '{"%s", %s __new_C%s},' % (c, prefix, c) + s = '{"%s", %s __new_%s},' % (c, prefix, c) struct.append(s) return struct @@ -234,14 +254,15 @@ def test_candidate(c, lines, line_nr, supports_complex): def extract_classes(HEADERS, template, blacklist, supports_complex): """ - Search in headers for non-template/non-abstract class-names starting - with `C'. + Search in headers for non-template/non-abstract class-names Does not support local nor multiple classes and drops classes with pure virtual functions """ classes = list() for fname in HEADERS: + if fname.find("external") > 0: + continue try: lines = open(fname).readlines() except: # python3 workaround diff --git a/src/shogun/base/class_list.cpp.templ b/src/shogun/base/class_list.cpp.templ index 8b9c811c903..f18c5eca65f 100644 --- a/src/shogun/base/class_list.cpp.templ +++ b/src/shogun/base/class_list.cpp.templ @@ -45,27 +45,22 @@ REPLACE template_definitions THIS REPLACE complex_template_definitions THIS -typedef CSGObject* (*CreateFunction)(EPrimitiveType generic); +typedef SGObject* (*CreateFunction)(EPrimitiveType generic); static const std::map classes = { REPLACE struct THIS }; -CSGObject* shogun::create(const char* classname, EPrimitiveType generic) +std::shared_ptr shogun::create(const char* classname, EPrimitiveType generic) { auto entry = classes.find(classname); if (entry != classes.end()) { - return entry->second(generic); + return std::shared_ptr(entry->second(generic)); } return nullptr; } -void shogun::delete_object(CSGObject* object) -{ - delete object; -} - std::set shogun::available_objects() { std::set result; diff --git a/src/shogun/base/class_list.h b/src/shogun/base/class_list.h index f8502d42a4f..99471c25fc0 100644 --- a/src/shogun/base/class_list.h +++ b/src/shogun/base/class_list.h @@ -19,18 +19,13 @@ #include namespace shogun { - class CSGObject; + class SGObject; /** new shogun instance * @param sgserializable_name * @param generic */ - CSGObject* create(const char* sgserializable_name, EPrimitiveType generic); - - /** deletes object - * @param object pointer to object to be deleted - */ - void delete_object(CSGObject* object); + std::shared_ptr create(const char* sgserializable_name, EPrimitiveType generic); /** Creates new shogun instance, typed. * @@ -39,26 +34,21 @@ namespace shogun { * */ template - T* create_object( + std::shared_ptr create_object( const char* name, EPrimitiveType pt = PT_NOT_GENERIC) noexcept(false) { - auto* object = create(name, pt); + auto object = create(name, pt); if (!object) { error( "Class {} with primitive type {} does not exist.", name, ptype_name(pt).c_str()); } - T* cast = nullptr; - try - { - cast = object->as(); - } - catch (const ShogunException& e) + auto cast = std::dynamic_pointer_cast(object); + if (cast == nullptr) { - delete_object(object); - throw e; + error("could not cst"); } return cast; } diff --git a/src/shogun/base/progress.h b/src/shogun/base/progress.h index 66f16f3aa35..dde15497b25 100644 --- a/src/shogun/base/progress.h +++ b/src/shogun/base/progress.h @@ -90,7 +90,7 @@ namespace shogun : m_max_value(max_value), m_min_value(min_value), m_prefix(prefix), m_mode(mode), m_columns_num(0), m_rows_num(0), m_last_progress(0), m_last_progress_time(0), - m_progress_start_time(CTime::get_curtime()), + m_progress_start_time(Time::get_curtime()), m_current_value(min_value) { } @@ -197,7 +197,7 @@ namespace shogun // "Not enough terminal space to show the progress bar!"); char str[1000]; - float64_t runtime = CTime::get_curtime(); + float64_t runtime = Time::get_curtime(); if (difference > 0.0) v = 100 * (m_current_value.load() - m_min_value) / @@ -293,7 +293,7 @@ namespace shogun // "Not enough terminal space to show the progress bar!"); char str[1000]; - float64_t runtime = CTime::get_curtime(); + float64_t runtime = Time::get_curtime(); if (difference > 0.0) v = 100 * (val - min_value) / (max_value - min_value); diff --git a/src/shogun/base/some.h b/src/shogun/base/some.h deleted file mode 100644 index 577f220f377..00000000000 --- a/src/shogun/base/some.h +++ /dev/null @@ -1,270 +0,0 @@ -#ifndef __SG_SOME_H__ -#define __SG_SOME_H__ - -#include -#include - -namespace shogun -{ - - /** @class Shogun synonym for the std::shared_ptr. Employs - * exactly the same strategy for reference counting - * as std::shared_ptr: any operation involving copying increases - * the count and once deleted this wrapper decreases the counter. - * - */ - template - class Some - { - public: - Some(const Some& other); - template - Some(const Some& other); - - Some(Some&& other); - template - Some(Some&& other); - - Some& operator=(const Some& other); - template - Some& operator=(const Some& other); - - Some& operator=(Some&& other); - template - Some& operator=(Some&& other); - - ~Some(); - - static Some from_raw(T* raw); - - /** Casts the underlying object back to raw pointer - * - * Be careful to SG_REF obtained pointer if you start to own it. - * - * @return raw pointer (without SG_REF) - */ - operator T*() const; - - /** Call member function or access member of T - * - * @return raw pointer (without SG_REF) - */ - T* operator->() const; - - /** Equality operator - * @param other other element to compare with - * @return true iff other's raw pointer equals own raw pointer - */ - bool operator==(const Some& other) const; - - /** Inequality operator - * @param other other element to compare with - * @return false iff other's raw pointer equals own raw pointer - */ - bool operator!=(const Some& other) const; - - /** - * Get the raw pointer - * - * @return raw pointer (without SG_REF) - */ - T* get() const; - - void reset(T* value = nullptr); - - private: - Some(T* other); - Some(); - void unref(); - void ref(); - - private: - T* raw; - }; - - template - Some::Some() : raw(nullptr) - { - } - template - Some::Some(T* other) : raw(other) - { - ref(); - } - template - template - Some::Some(const Some& other) : raw(nullptr) - { - reset(dynamic_cast(other.get())); - ref(); - } - template - Some::Some(const Some& other) : raw(other.get()) - { - ref(); - } - template - template - Some::Some(Some&& other) : raw(nullptr) - { - reset(dynamic_cast(other.get())); - other.raw = nullptr; - } - template - Some::Some(Some&& other) : raw(other.get()) - { - other.raw = nullptr; - } - template - template - Some& Some::operator=(const Some& other) - { - if (get() != other.get()) - { - reset(other.get()); - ref(); - } - return *this; - } - template - Some& Some::operator=(const Some& other) - { - if (get() != other.get()) - { - reset(other.get()); - ref(); - } - return *this; - } - template - template - Some& Some::operator=(Some&& other) - { - if (get() != other.get()) - { - reset(other.get()); - other.raw = nullptr; - } - return *this; - } - template - Some& Some::operator=(Some&& other) - { - if (get() != other.get()) - { - reset(other.get()); - other.raw = nullptr; - } - return *this; - } - - template - Some::~Some() - { - reset(); - } - template - Some::operator T*() const - { - return get(); - } - template - T* Some::operator->() const - { - return get(); - } - template - bool Some::operator==(const Some& other) const - { - return raw == other.raw; - } - template - bool Some::operator!=(const Some& other) const - { - return !((*this) == other); - } - template - T* Some::get() const - { - return raw; - } - template - void Some::reset(T* ptr) - { - unref(); - raw = ptr; - } - template - void Some::ref() - { - if (raw) - { - (raw)->ref(); - } - } - template - void Some::unref() - { - if (raw) - { - if ((raw)->unref() == 0) - (raw) = NULL; - }; - } - template - Some Some::from_raw(T* raw) - { - Some result(raw); - return result; - } - - /** Creates an instance of any class - * that is wrapped with a shared pointer like - * structure @ref Some - * - * @param args arguments to construct instance of T with (T should - * have compatible constructor) - * - * @return a shared pointer that holds created instance of @ref T - * - */ - template - Some some(Args&&... args) - { - T* ptr = new T(args...); - return Some::from_raw(ptr); - } - - template - Some empty() - { - return Some::from_raw(nullptr); - } - -#ifndef SWIG - template - SG_FORCED_INLINE T wrap(const T& value) - { - return value; - } - - SG_FORCED_INLINE const char* wrap(const char* ptr) - { - return ptr; - } - - template - SG_FORCED_INLINE Some wrap(T* ptr) - { - return Some::from_raw(ptr); - } - - template - SG_FORCED_INLINE Some wrap(const Some& other) - { - return other; - } -#endif -}; - -#endif /* __SG_SOME_H__ */ diff --git a/src/shogun/classifier/AveragedPerceptron.cpp b/src/shogun/classifier/AveragedPerceptron.cpp index d62a4d4475a..0198351c750 100644 --- a/src/shogun/classifier/AveragedPerceptron.cpp +++ b/src/shogun/classifier/AveragedPerceptron.cpp @@ -13,16 +13,16 @@ using namespace shogun; -CAveragedPerceptron::CAveragedPerceptron() : CIterativeMachine() +AveragedPerceptron::AveragedPerceptron() : IterativeMachine() { init(); } -CAveragedPerceptron::~CAveragedPerceptron() +AveragedPerceptron::~AveragedPerceptron() { } -void CAveragedPerceptron::init() +void AveragedPerceptron::init() { set_max_iter(1000); set_learn_rate(0.1); @@ -39,14 +39,14 @@ void CAveragedPerceptron::init() ParameterProperties::MODEL) } -void CAveragedPerceptron::init_model(CFeatures* data) +void AveragedPerceptron::init_model(std::shared_ptr data) { ASSERT(m_labels) if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } ASSERT(features) @@ -65,7 +65,7 @@ void CAveragedPerceptron::init_model(CFeatures* data) set_w(w); } -void CAveragedPerceptron::iteration() +void AveragedPerceptron::iteration() { bool converged = true; @@ -82,7 +82,7 @@ void CAveragedPerceptron::iteration() const auto true_label = *(iter_train_labels++); auto predicted_label = feature.dot(cached_w) + cached_bias; - if (CMath::sign(predicted_label) != true_label) + if (Math::sign(predicted_label) != true_label) { converged = false; const auto gradient = learn_rate * true_label; @@ -99,4 +99,4 @@ void CAveragedPerceptron::iteration() } m_complete = converged; -} \ No newline at end of file +} diff --git a/src/shogun/classifier/AveragedPerceptron.h b/src/shogun/classifier/AveragedPerceptron.h index 35bfd770793..6bf15cd8eea 100644 --- a/src/shogun/classifier/AveragedPerceptron.h +++ b/src/shogun/classifier/AveragedPerceptron.h @@ -25,18 +25,18 @@ namespace shogun * algorithm is not guaranteed to converge) and a fixed learning rate, * the result is a linear classifier. * - * \sa CLinearMachine + * \sa LinearMachine */ - class CAveragedPerceptron : public CIterativeMachine + class AveragedPerceptron : public IterativeMachine { public: /** problem type */ MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - CAveragedPerceptron(); + AveragedPerceptron(); - virtual ~CAveragedPerceptron(); + virtual ~AveragedPerceptron(); /** get classifier type * @@ -69,7 +69,7 @@ namespace shogun /** registers and initializes parameters */ void init(); - virtual void init_model(CFeatures* data); + virtual void init_model(std::shared_ptr data); virtual void iteration(); protected: diff --git a/src/shogun/classifier/GaussianProcessClassification.cpp b/src/shogun/classifier/GaussianProcessClassification.cpp index 8be5a63ef90..12fc59662a9 100644 --- a/src/shogun/classifier/GaussianProcessClassification.cpp +++ b/src/shogun/classifier/GaussianProcessClassification.cpp @@ -45,31 +45,30 @@ using namespace shogun; -CGaussianProcessClassification::CGaussianProcessClassification() - : CGaussianProcessMachine() +GaussianProcessClassification::GaussianProcessClassification() + : GaussianProcessMachine() { } -CGaussianProcessClassification::CGaussianProcessClassification( - CInference* method) : CGaussianProcessMachine(method) +GaussianProcessClassification::GaussianProcessClassification( + std::shared_ptr method) : GaussianProcessMachine(method) { // set labels m_labels=method->get_labels(); } -CGaussianProcessClassification::~CGaussianProcessClassification() +GaussianProcessClassification::~GaussianProcessClassification() { } -CMulticlassLabels* CGaussianProcessClassification::apply_multiclass(CFeatures* data) +std::shared_ptr GaussianProcessClassification::apply_multiclass(std::shared_ptr data) { // check whether given combination of inference method and likelihood // function supports classification require(m_method, "Inference method should not be NULL"); - CLikelihoodModel* lik=m_method->get_model(); + auto lik=m_method->get_model(); require(m_method->supports_multiclass(), "{} with {} doesn't support " "multi classification\n", m_method->get_name(), lik->get_name()); - SG_UNREF(lik); // if regression data equals to NULL, then apply classification on training // features @@ -82,8 +81,6 @@ CMulticlassLabels* CGaussianProcessClassification::apply_multiclass(CFeatures* d else data=m_method->get_features(); } - else - SG_REF(data); const index_t n=data->get_num_vectors(); SGVector mean=get_mean_vector(data); @@ -91,27 +88,26 @@ CMulticlassLabels* CGaussianProcessClassification::apply_multiclass(CFeatures* d SGVector lab(n); for (index_t idx=0; idx(); result->set_int_labels(lab); - SG_UNREF(data); + return result; } -CBinaryLabels* CGaussianProcessClassification::apply_binary( - CFeatures* data) +std::shared_ptr GaussianProcessClassification::apply_binary( + std::shared_ptr data) { // check whether given combination of inference method and likelihood // function supports classification require(m_method, "Inference method should not be NULL"); - CLikelihoodModel* lik=m_method->get_model(); + auto lik=m_method->get_model(); require(m_method->supports_binary(), "{} with {} doesn't support " "binary classification\n", m_method->get_name(), lik->get_name()); - SG_UNREF(lik); // if regression data equals to NULL, then apply classification on training // features @@ -120,7 +116,7 @@ CBinaryLabels* CGaussianProcessClassification::apply_binary( if (m_method->get_inference_type()== INF_FITC_LAPLACE_SINGLE) { #ifdef USE_GPL_SHOGUN - CSingleFITCLaplaceInferenceMethod* fitc_method = m_method->as(); + auto fitc_method = m_method->as(); data=fitc_method->get_inducing_features(); #else gpl_only(SOURCE_LOCATION); @@ -129,26 +125,23 @@ CBinaryLabels* CGaussianProcessClassification::apply_binary( else data=m_method->get_features(); } - else - SG_REF(data); - CBinaryLabels* result=new CBinaryLabels(get_mean_vector(data)); + auto result=std::make_shared(get_mean_vector(data)); if (m_compute_variance) result->put("current_values", get_variance_vector(data)); - SG_UNREF(data); + return result; } -bool CGaussianProcessClassification::train_machine(CFeatures* data) +bool GaussianProcessClassification::train_machine(std::shared_ptr data) { // check whether given combination of inference method and likelihood // function supports classification require(m_method, "Inference method should not be NULL"); - CLikelihoodModel* lik=m_method->get_model(); + auto lik=m_method->get_model(); require(m_method->supports_binary() || m_method->supports_multiclass(), "{} with {} doesn't support " "classification\n", m_method->get_name(), lik->get_name()); - SG_UNREF(lik); if (data) { @@ -156,7 +149,7 @@ bool CGaussianProcessClassification::train_machine(CFeatures* data) if (m_method->get_inference_type()==INF_FITC_LAPLACE_SINGLE) { #ifdef USE_GPL_SHOGUN - CSingleFITCLaplaceInferenceMethod* fitc_method = m_method->as(); + auto fitc_method = m_method->as(); fitc_method->set_inducing_features(data); #else error("Single FITC Laplace inference only supported under GPL."); @@ -172,68 +165,65 @@ bool CGaussianProcessClassification::train_machine(CFeatures* data) return true; } -SGVector CGaussianProcessClassification::get_mean_vector( - CFeatures* data) +SGVector GaussianProcessClassification::get_mean_vector( + std::shared_ptr data) { // check whether given combination of inference method and likelihood // function supports classification require(m_method, "Inference method should not be NULL"); - CLikelihoodModel* lik=m_method->get_model(); + auto lik=m_method->get_model(); require(m_method->supports_binary() || m_method->supports_multiclass(), "{} with {} doesn't support classification", m_method->get_name(), lik->get_name()); - SG_REF(data); SGVector mu=get_posterior_means(data); SGVector s2=get_posterior_variances(data); - SG_UNREF(data); + // evaluate mean mu=lik->get_predictive_means(mu, s2); - SG_UNREF(lik); + return mu; } -SGVector CGaussianProcessClassification::get_variance_vector( - CFeatures* data) +SGVector GaussianProcessClassification::get_variance_vector( + std::shared_ptr data) { // check whether given combination of inference method and // likelihood function supports classification require(m_method, "Inference method should not be NULL"); - CLikelihoodModel* lik=m_method->get_model(); + auto lik=m_method->get_model(); require(m_method->supports_binary() || m_method->supports_multiclass(), "{} with {} doesn't support classification", m_method->get_name(), lik->get_name()); - SG_REF(data); SGVector mu=get_posterior_means(data); SGVector s2=get_posterior_variances(data); - SG_UNREF(data); + // evaluate variance s2=lik->get_predictive_variances(mu, s2); - SG_UNREF(lik); + return s2; } -SGVector CGaussianProcessClassification::get_probabilities( - CFeatures* data) +SGVector GaussianProcessClassification::get_probabilities( + std::shared_ptr data) { // check whether given combination of inference method and likelihood // function supports classification require(m_method, "Inference method should not be NULL"); - CLikelihoodModel* lik=m_method->get_model(); + auto lik=m_method->get_model(); require(m_method->supports_binary() || m_method->supports_multiclass(), "{} with {} doesn't support classification", m_method->get_name(), lik->get_name()); - SG_REF(data); SGVector mu=get_posterior_means(data); SGVector s2=get_posterior_variances(data); - SG_UNREF(data); + // evaluate log probabilities SGVector p=lik->get_predictive_log_probabilities(mu, s2); - SG_UNREF(lik); + // evaluate probabilities for (index_t idx=0; idx method); - virtual ~CGaussianProcessClassification(); + virtual ~GaussianProcessClassification(); /** apply machine to data in means of binary classification problem * @@ -72,7 +72,7 @@ class CGaussianProcessClassification : public CGaussianProcessMachine * * @return classified labels (label is either -1 or 1) */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /** returns a vector of of the posterior predictive means * @@ -80,7 +80,7 @@ class CGaussianProcessClassification : public CGaussianProcessMachine * * @return mean vector */ - SGVector get_mean_vector(CFeatures* data); + SGVector get_mean_vector(std::shared_ptr data); /** returns a vector of the posterior predictive variances * @@ -88,7 +88,7 @@ class CGaussianProcessClassification : public CGaussianProcessMachine * * @return variance vector */ - SGVector get_variance_vector(CFeatures* data); + SGVector get_variance_vector(std::shared_ptr data); /** returns probabilities \f$p(y_*=1)\f$ for each (test) feature \f$x_*\f$ * @@ -96,7 +96,7 @@ class CGaussianProcessClassification : public CGaussianProcessMachine * * @return vector of probabilities */ - SGVector get_probabilities(CFeatures* data); + SGVector get_probabilities(std::shared_ptr data); /** get classifier type * @@ -121,7 +121,7 @@ class CGaussianProcessClassification : public CGaussianProcessMachine * * @return classified labels (label starts from 0) */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); protected: /** train classifier @@ -130,7 +130,7 @@ class CGaussianProcessClassification : public CGaussianProcessMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); }; } diff --git a/src/shogun/classifier/LDA.cpp b/src/shogun/classifier/LDA.cpp index bb396348d4c..649b0496eb2 100644 --- a/src/shogun/classifier/LDA.cpp +++ b/src/shogun/classifier/LDA.cpp @@ -21,8 +21,8 @@ using namespace Eigen; using namespace shogun; -CLDA::CLDA(float64_t gamma, ELDAMethod method, bool bdc_svd) - : CDenseRealDispatch() +LDA::LDA(float64_t gamma, ELDAMethod method, bool bdc_svd) + : DenseRealDispatch() { init(); m_method = method; @@ -30,25 +30,21 @@ CLDA::CLDA(float64_t gamma, ELDAMethod method, bool bdc_svd) m_bdc_svd = bdc_svd; } -CLDA::CLDA( - float64_t gamma, CDenseFeatures* traindat, CLabels* trainlab, +LDA::LDA( + float64_t gamma, std::shared_ptr> traindat, std::shared_ptr trainlab, ELDAMethod method, bool bdc_svd) - : CDenseRealDispatch(), m_gamma(gamma) + : DenseRealDispatch(), m_gamma(gamma) { init(); features = traindat; - SG_REF(features) - m_labels = trainlab; - SG_REF(trainlab) - m_method = method; m_gamma = gamma; m_bdc_svd = bdc_svd; } -void CLDA::init() +void LDA::init() { m_method = AUTO_LDA; m_gamma = 0; @@ -66,12 +62,12 @@ void CLDA::init() SG_OPTIONS(AUTO_LDA, SVD_LDA, FLD_LDA)) } -CLDA::~CLDA() +LDA::~LDA() { } template -bool CLDA::train_machine_templated(CDenseFeatures* data) +bool LDA::train_machine_templated(std::shared_ptr> data) { index_t num_feat = data->get_num_features(); index_t num_vec = data->get_num_vectors(); @@ -85,7 +81,7 @@ bool CLDA::train_machine_templated(CDenseFeatures* data) } template -bool CLDA::solver_svd(CDenseFeatures* data) +bool LDA::solver_svd(std::shared_ptr> data) { auto labels = multiclass_labels(m_labels); require( @@ -121,7 +117,7 @@ bool CLDA::solver_svd(CDenseFeatures* data) } template -bool CLDA::solver_classic(CDenseFeatures* data) +bool LDA::solver_classic(std::shared_ptr> data) { auto labels = multiclass_labels(m_labels); require( diff --git a/src/shogun/classifier/LDA.h b/src/shogun/classifier/LDA.h index 5e8f0297eba..34569bf3668 100644 --- a/src/shogun/classifier/LDA.h +++ b/src/shogun/classifier/LDA.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Michele Mazzoni, Heiko Strathmann, - * Fernando Iglesias, Sergey Lisitsyn, Abhijeet Kislay, Bjoern Esser, + * Authors: Soeren Sonnenburg, Michele Mazzoni, Heiko Strathmann, + * Fernando Iglesias, Sergey Lisitsyn, Abhijeet Kislay, Bjoern Esser, * Christopher Goldsworthy, Sanuj Sharma */ @@ -34,10 +34,10 @@ enum ELDAMethod FLD_LDA = 30 }; -template class CDenseFeatures; +template class DenseFeatures; /** @brief Class LDA implements regularized Linear Discriminant Analysis. * - * LDA learns a linear classifier and requires examples to be CDenseFeatures. + * LDA learns a linear classifier and requires examples to be DenseFeatures. * The learned linear classification rule is optimal under the assumption that * both classes a gaussian distributed with equal co-variance. To find a linear * separation \f${\bf w}\f$ in training, the in-between class variance is @@ -89,13 +89,13 @@ template class CDenseFeatures; * Note that even if N > D FLD_LDA may fail being the covariance matrix not * invertible, * in such case one should use SVD_LDA. - * \sa CLinearMachine + * \sa LinearMachine * \sa http://en.wikipedia.org/wiki/Linear_discriminant_analysis */ -class CLDA : public CDenseRealDispatch +class LDA : public DenseRealDispatch { - friend class CDenseRealDispatch; + friend class DenseRealDispatch; public: MACHINE_PROBLEM_TYPE(PT_BINARY); @@ -109,7 +109,7 @@ class CLDA : public CDenseRealDispatch * Jacobi's algorithm, for the differences @see linalg::SVDAlgorithm. * [default = BDC-SVD] */ - CLDA( + LDA( float64_t gamma = 0, ELDAMethod method = AUTO_LDA, bool bdc_svd = true); @@ -125,11 +125,11 @@ class CLDA : public CDenseRealDispatch * Jacobi's algorithm, for the differences @see linalg::SVDAlgorithm. * [default = BDC-SVD] */ - CLDA( - float64_t gamma, CDenseFeatures* traindat, - CLabels* trainlab, ELDAMethod method = AUTO_LDA, + LDA( + float64_t gamma, std::shared_ptr> traindat, + std::shared_ptr trainlab, ELDAMethod method = AUTO_LDA, bool bdc_svd = true); - virtual ~CLDA(); + virtual ~LDA(); /** get classifier type * @@ -154,7 +154,7 @@ class CLDA : public CDenseRealDispatch */ template ::value>> - bool train_machine_templated(CDenseFeatures* data); + bool train_machine_templated(std::shared_ptr> data); /** * Train the machine with the svd-based solver (@see CFisherLDA). @@ -162,7 +162,7 @@ class CLDA : public CDenseRealDispatch * @param labels labels for training data */ template - bool solver_svd(CDenseFeatures* data); + bool solver_svd(std::shared_ptr> data); /** * Train the machine with the classic method based on the cholesky @@ -171,7 +171,7 @@ class CLDA : public CDenseRealDispatch * @param labels labels for training data */ template - bool solver_classic(CDenseFeatures* data); + bool solver_classic(std::shared_ptr> data); protected: diff --git a/src/shogun/classifier/LPBoost.cpp b/src/shogun/classifier/LPBoost.cpp index 0b51e06eba0..7c518c97e11 100644 --- a/src/shogun/classifier/LPBoost.cpp +++ b/src/shogun/classifier/LPBoost.cpp @@ -20,7 +20,7 @@ using namespace shogun; CLPBoost::CLPBoost() -: CLinearMachine(), C1(1), C2(1), use_bias(true), epsilon(1e-3) +: LinearMachine(), C1(1), C2(1), use_bias(true), epsilon(1e-3) { u=NULL; dim=NULL; @@ -41,9 +41,9 @@ bool CLPBoost::init(int32_t num_vec) for (int32_t i=0; i(100000); + dim=new DynamicArray(100000); - sfeat= ((CSparseFeatures*) features)->get_transposed(num_sfeat, num_svec); + sfeat= ((SparseFeatures*) features)->get_transposed(num_sfeat, num_svec); if (sfeat) return true; @@ -56,7 +56,7 @@ void CLPBoost::cleanup() SG_FREE(u); u=NULL; - ((CSparseFeatures*) features)->clean_tsparse(sfeat, num_svec); + ((SparseFeatures*) features)->clean_tsparse(sfeat, num_svec); sfeat=NULL; delete dim; @@ -76,7 +76,7 @@ float64_t CLPBoost::find_max_violator(int32_t& max_dim) for (int32_t j=0; jget_confidence(idx)*sfeat[i].features[j].entry; + float64_t v=u[idx]*((BinaryLabels*)m_labels)->get_confidence(idx)*sfeat[i].features[j].entry; valplus+=v; valminus-=v; } @@ -98,7 +98,7 @@ float64_t CLPBoost::find_max_violator(int32_t& max_dim) return max_val; } -bool CLPBoost::train_machine(CFeatures* data) +bool CLPBoost::train_machine(Features* data) { ASSERT(m_labels) ASSERT(features) @@ -120,7 +120,7 @@ bool CLPBoost::train_machine(CFeatures* data) ASSERT(result) int32_t num_hypothesis=0; - CTime time; + Time time; while (get_max_train_time() <= 0 || time.cur_time_diff() <= get_max_train_time()) @@ -146,13 +146,13 @@ bool CLPBoost::train_machine(CFeatures* data) int32_t len=sfeat[max_dim].num_feat_entries; solver.add_lpboost_constraint(factor, h, len, num_vec, m_labels); solver.optimize(u); - //CMath::display_vector(u, num_vec, "u"); + //Math::display_vector(u, num_vec, "u"); num_hypothesis++; } float64_t* lambda=SG_MALLOC(float64_t, num_hypothesis); solver.optimize(u, lambda); - //CMath::display_vector(lambda, num_hypothesis, "lambda"); + //Math::display_vector(lambda, num_hypothesis, "lambda"); for (int32_t i=0; iget_element(i); diff --git a/src/shogun/classifier/LPBoost.h b/src/shogun/classifier/LPBoost.h index dcae6ce9e73..8d475836eed 100644 --- a/src/shogun/classifier/LPBoost.h +++ b/src/shogun/classifier/LPBoost.h @@ -41,7 +41,7 @@ namespace shogun * * \sa CLPM */ -class CLPBoost : public CLinearMachine +class CLPBoost : public LinearMachine { public: MACHINE_PROBLEM_TYPE(PT_BINARY); @@ -61,13 +61,13 @@ class CLPBoost : public CLinearMachine * * @param feat features to set */ - virtual void set_features(CDotFeatures* feat) + virtual void set_features(DotFeatures* feat) { if (feat->get_feature_class() != C_SPARSE || feat->get_feature_type() != F_DREAL) error("LPBoost requires SPARSE REAL valued features"); - CLinearMachine::set_features(feat); + LinearMachine::set_features(feat); } /** set C @@ -101,7 +101,7 @@ class CLPBoost : public CLinearMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(Features* data=NULL); protected: float64_t C1; @@ -110,7 +110,7 @@ class CLPBoost : public CLinearMachine float64_t epsilon; float64_t* u; - CDynamicArray* dim; + DynamicArray* dim; int32_t num_sfeat; int32_t num_svec; diff --git a/src/shogun/classifier/LPM.cpp b/src/shogun/classifier/LPM.cpp index f9fc0ee80e5..83fc77436b4 100644 --- a/src/shogun/classifier/LPM.cpp +++ b/src/shogun/classifier/LPM.cpp @@ -18,7 +18,7 @@ using namespace shogun; CLPM::CLPM() -: CLinearMachine(), C1(1), C2(1), use_bias(true), epsilon(1e-3) +: LinearMachine(), C1(1), C2(1), use_bias(true), epsilon(1e-3) { } @@ -27,14 +27,14 @@ CLPM::~CLPM() { } -bool CLPM::train_machine(CFeatures* data) +bool CLPM::train_machine(Features* data) { ASSERT(m_labels) if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features((DotFeatures*) data); } ASSERT(features) int32_t num_train_labels=m_labels->get_num_labels(); @@ -52,7 +52,7 @@ bool CLPM::train_machine(CFeatures* data) CCplex solver; solver.init(E_LINEAR); io::info("C={}", C1); - solver.setup_lpm(C1, (CSparseFeatures*) features, (CBinaryLabels*)m_labels, get_bias_enabled()); + solver.setup_lpm(C1, (SparseFeatures*) features, (BinaryLabels*)m_labels, get_bias_enabled()); if (get_max_train_time()>0) solver.set_time_limit(get_max_train_time()); bool result=solver.optimize(params); @@ -64,11 +64,11 @@ bool CLPM::train_machine(CFeatures* data) //#define LPM_DEBUG #ifdef LPM_DEBUG - CMath::display_vector(params,num_params, "params"); + Math::display_vector(params,num_params, "params"); io::print("bias={}\n", bias); - CMath::display_vector(w,w_dim, "w"); - CMath::display_vector(¶ms[1],w_dim, "w+"); - CMath::display_vector(¶ms[1+w_dim],w_dim, "w-"); + Math::display_vector(w,w_dim, "w"); + Math::display_vector(¶ms[1],w_dim, "w+"); + Math::display_vector(¶ms[1+w_dim],w_dim, "w-"); #endif SG_FREE(params); diff --git a/src/shogun/classifier/LPM.h b/src/shogun/classifier/LPM.h index 9bd844c05ef..50b935ffc5e 100644 --- a/src/shogun/classifier/LPM.h +++ b/src/shogun/classifier/LPM.h @@ -36,7 +36,7 @@ namespace shogun * * \sa CLPBoost */ -class CLPM : public CLinearMachine +class CLPM : public LinearMachine { public: MACHINE_PROBLEM_TYPE(PT_BINARY); @@ -53,13 +53,13 @@ class CLPM : public CLinearMachine * * @param feat features to set */ - virtual void set_features(CDotFeatures* feat) + virtual void set_features(DotFeatures* feat) { if (feat->get_feature_class() != C_SPARSE || feat->get_feature_type() != F_DREAL) error("LPM requires SPARSE REAL valued features"); - CLinearMachine::set_features(feat); + LinearMachine::set_features(feat); } /** set C @@ -91,7 +91,7 @@ class CLPM : public CLinearMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(Features* data=NULL); protected: float64_t C1; diff --git a/src/shogun/classifier/NearestCentroid.cpp b/src/shogun/classifier/NearestCentroid.cpp index 3d2ba7c3bda..95cc209a24f 100644 --- a/src/shogun/classifier/NearestCentroid.cpp +++ b/src/shogun/classifier/NearestCentroid.cpp @@ -14,12 +14,12 @@ namespace shogun{ - CNearestCentroid::CNearestCentroid() : CDistanceMachine() + NearestCentroid::NearestCentroid() : DistanceMachine() { init(); } - CNearestCentroid::CNearestCentroid(CDistance* d, CLabels* trainlab) : CDistanceMachine() + NearestCentroid::NearestCentroid(std::shared_ptr d, std::shared_ptr trainlab) : DistanceMachine() { init(); ASSERT(d) @@ -28,18 +28,18 @@ namespace shogun{ set_labels(trainlab); } - CNearestCentroid::~CNearestCentroid() + NearestCentroid::~NearestCentroid() { } - void CNearestCentroid::init() + void NearestCentroid::init() { m_shrinking=0; m_is_trained=false; } - bool CNearestCentroid::train_machine(CFeatures* data) + bool NearestCentroid::train_machine(std::shared_ptr data) { ASSERT(m_labels) ASSERT(distance) @@ -54,8 +54,8 @@ namespace shogun{ data = distance->get_lhs(); } - auto* multiclass_labels = m_labels->as(); - auto* dense_data = data->as>(); + auto multiclass_labels = m_labels->as(); + auto dense_data = data->as>(); int32_t num_classes = multiclass_labels->get_num_classes(); int32_t num_feats = dense_data->get_num_features(); @@ -83,7 +83,7 @@ namespace shogun{ } linalg::scale(centroids, centroids, scale); - auto centroids_feats = some>(centroids); + auto centroids_feats = std::make_shared>(centroids); m_is_trained=true; distance->init(centroids_feats, distance->get_rhs()); diff --git a/src/shogun/classifier/NearestCentroid.h b/src/shogun/classifier/NearestCentroid.h index 36895901a94..4d233621fe4 100644 --- a/src/shogun/classifier/NearestCentroid.h +++ b/src/shogun/classifier/NearestCentroid.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Philippe Tillet, Sergey Lisitsyn, Viktor Gal, Fernando Iglesias, + * Authors: Philippe Tillet, Sergey Lisitsyn, Viktor Gal, Fernando Iglesias, * Bjoern Esser, Soeren Sonnenburg, Saurabh Goyal */ @@ -20,15 +20,15 @@ namespace shogun { -class CDistanceMachine; +class DistanceMachine; /** @brief Class NearestCentroid, an implementation of Nearest Shrunk Centroid classifier * * To define how close examples are - * NearestCentroid requires a CDistance object to work with (e.g., CEuclideanDistance ). + * NearestCentroid requires a Distance object to work with (e.g., EuclideanDistance ). */ -class CNearestCentroid : public CDistanceMachine{ +class NearestCentroid : public DistanceMachine{ public: @@ -38,18 +38,18 @@ class CNearestCentroid : public CDistanceMachine{ /** * Default constructor */ - CNearestCentroid(); + NearestCentroid(); /** constructor * * @param distance distance * @param trainlab labels for training */ - CNearestCentroid(CDistance* distance, CLabels* trainlab); + NearestCentroid(std::shared_ptr distance, std::shared_ptr trainlab); /** Destructor */ - virtual ~CNearestCentroid(); + virtual ~NearestCentroid(); /** Set shrinking constant * @@ -71,7 +71,7 @@ class CNearestCentroid : public CDistanceMachine{ * * @return Matrix containing the centroids */ - CDenseFeatures* get_centroids() const{ + std::shared_ptr> get_centroids() const{ return m_centroids; } @@ -90,7 +90,7 @@ class CNearestCentroid : public CDistanceMachine{ * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** Stores feature data of underlying model. * @@ -108,7 +108,7 @@ class CNearestCentroid : public CDistanceMachine{ float64_t m_shrinking; /// The centroids of the trained features - CDenseFeatures* m_centroids; + std::shared_ptr> m_centroids; /// Tells if the classifier has been trained or not bool m_is_trained; diff --git a/src/shogun/classifier/Perceptron.cpp b/src/shogun/classifier/Perceptron.cpp index e34a7e9e4a9..8e5c2053ec5 100644 --- a/src/shogun/classifier/Perceptron.cpp +++ b/src/shogun/classifier/Perceptron.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Giovanni De Toni, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Giovanni De Toni, * Michele Mazzoni, Heiko Strathmann, Fernando Iglesias */ @@ -18,7 +18,7 @@ using namespace shogun; -CPerceptron::CPerceptron() : CIterativeMachine() +Perceptron::Perceptron() : IterativeMachine() { m_max_iterations = 1000; learn_rate = 0.1; @@ -29,17 +29,17 @@ CPerceptron::CPerceptron() : CIterativeMachine() SG_ADD(&learn_rate, "learn_rate", "Learning rate.", ParameterProperties::HYPER) } -CPerceptron::~CPerceptron() +Perceptron::~Perceptron() { } -void CPerceptron::init_model(CFeatures* data) +void Perceptron::init_model(std::shared_ptr data) { if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } int32_t num_feat = features->get_dim_feature_space(); @@ -56,7 +56,7 @@ void CPerceptron::init_model(CFeatures* data) } } -void CPerceptron::iteration() +void Perceptron::iteration() { bool converged = true; SGVector w = get_w(); @@ -70,7 +70,7 @@ void CPerceptron::iteration() auto predicted_label = v.dot(w) + bias; - if (CMath::sign(predicted_label) != true_label) + if (Math::sign(predicted_label) != true_label) { converged = false; const auto gradient = learn_rate * true_label; @@ -84,12 +84,12 @@ void CPerceptron::iteration() m_complete = converged; } -void CPerceptron::set_initialize_hyperplane(bool initialize_hyperplane) +void Perceptron::set_initialize_hyperplane(bool initialize_hyperplane) { m_initialize_hyperplane = initialize_hyperplane; } -bool CPerceptron::get_initialize_hyperplane() +bool Perceptron::get_initialize_hyperplane() { return m_initialize_hyperplane; -} \ No newline at end of file +} diff --git a/src/shogun/classifier/Perceptron.h b/src/shogun/classifier/Perceptron.h index 37642291707..80420c6023f 100644 --- a/src/shogun/classifier/Perceptron.h +++ b/src/shogun/classifier/Perceptron.h @@ -22,10 +22,10 @@ namespace shogun * not guaranteed to converge) and a fixed lerning rate, the result is a linear * classifier. * - * \sa CLinearMachine + * \sa LinearMachine * \sa http://en.wikipedia.org/wiki/Perceptron */ -class CPerceptron : public CIterativeMachine +class Perceptron : public IterativeMachine { public: @@ -33,9 +33,9 @@ class CPerceptron : public CIterativeMachine MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - CPerceptron(); + Perceptron(); - virtual ~CPerceptron(); + virtual ~Perceptron(); /** get classifier type * @@ -59,7 +59,7 @@ class CPerceptron : public CIterativeMachine virtual const char* get_name() const { return "Perceptron"; } protected: - virtual void init_model(CFeatures* data); + virtual void init_model(std::shared_ptr data); virtual void iteration(); protected: diff --git a/src/shogun/classifier/PluginEstimate.cpp b/src/shogun/classifier/PluginEstimate.cpp index ca9dd0d962b..0ad4266fbde 100644 --- a/src/shogun/classifier/PluginEstimate.cpp +++ b/src/shogun/classifier/PluginEstimate.cpp @@ -16,9 +16,9 @@ using namespace shogun; -CPluginEstimate::CPluginEstimate(float64_t pos_pseudo, float64_t neg_pseudo) - : CMachine(), m_pos_pseudo(1e-10), m_neg_pseudo(1e-10), pos_model(NULL), - neg_model(NULL), features(NULL) +PluginEstimate::PluginEstimate(float64_t pos_pseudo, float64_t neg_pseudo) +: Machine(), m_pos_pseudo(1e-10), m_neg_pseudo(1e-10), + pos_model(NULL), neg_model(NULL), features(NULL) { SG_ADD( &m_pos_pseudo, "pos_pseudo", "pseudo count for positive class", @@ -35,15 +35,11 @@ CPluginEstimate::CPluginEstimate(float64_t pos_pseudo, float64_t neg_pseudo) SG_ADD(&features, "features", "String Features."); } -CPluginEstimate::~CPluginEstimate() +PluginEstimate::~PluginEstimate() { - SG_UNREF(pos_model); - SG_UNREF(neg_model); - - SG_UNREF(features); } -bool CPluginEstimate::train_machine(CFeatures* data) +bool PluginEstimate::train_machine(std::shared_ptr data) { ASSERT(m_labels) ASSERT(m_labels->get_label_type() == LT_BINARY) @@ -55,33 +51,28 @@ bool CPluginEstimate::train_machine(CFeatures* data) error("Features not of class string type word"); } - set_features((CStringFeatures*)data); + set_features(std::static_pointer_cast>(data)); } ASSERT(features) - SG_UNREF(pos_model); - SG_UNREF(neg_model); - pos_model = new CLinearHMM(features); - neg_model = new CLinearHMM(features); - SG_REF(pos_model); - SG_REF(neg_model); + pos_model=std::make_shared(features); + neg_model=std::make_shared(features); - int32_t* pos_indizes = SG_MALLOC( - int32_t, ((CStringFeatures*)features)->get_num_vectors()); - int32_t* neg_indizes = SG_MALLOC( - int32_t, ((CStringFeatures*)features)->get_num_vectors()); + int32_t* pos_indizes=SG_MALLOC(int32_t, std::static_pointer_cast>(features)->get_num_vectors()); + int32_t* neg_indizes=SG_MALLOC(int32_t, std::static_pointer_cast>(features)->get_num_vectors()); ASSERT(m_labels->get_num_labels() == features->get_num_vectors()) int32_t pos_idx = 0; int32_t neg_idx = 0; - for (int32_t i = 0; i < m_labels->get_num_labels(); i++) + auto binary_labels = std::static_pointer_cast(m_labels); + for (int32_t i=0; iget_num_labels(); i++) { - if (((CBinaryLabels*)m_labels)->get_label(i) > 0) - pos_indizes[pos_idx++] = i; + if (binary_labels->get_label(i) > 0) + pos_indizes[pos_idx++]=i; else neg_indizes[neg_idx++] = i; } @@ -96,7 +87,7 @@ bool CPluginEstimate::train_machine(CFeatures* data) return true; } -CBinaryLabels* CPluginEstimate::apply_binary(CFeatures* data) +std::shared_ptr PluginEstimate::apply_binary(std::shared_ptr data) { if (data) { @@ -106,7 +97,7 @@ CBinaryLabels* CPluginEstimate::apply_binary(CFeatures* data) error("Features not of class string type word"); } - set_features((CStringFeatures*)data); + set_features(std::dynamic_pointer_cast>(data)); } ASSERT(features) @@ -115,10 +106,10 @@ CBinaryLabels* CPluginEstimate::apply_binary(CFeatures* data) for (int32_t vec = 0; vec < features->get_num_vectors(); vec++) result[vec] = apply_one(vec); - return new CBinaryLabels(result); + return std::make_shared(result); } -float64_t CPluginEstimate::apply_one(int32_t vec_idx) +float64_t PluginEstimate::apply_one(int32_t vec_idx) { ASSERT(features) diff --git a/src/shogun/classifier/PluginEstimate.h b/src/shogun/classifier/PluginEstimate.h index 17037269ba7..d37f9e5865e 100644 --- a/src/shogun/classifier/PluginEstimate.h +++ b/src/shogun/classifier/PluginEstimate.h @@ -28,9 +28,9 @@ namespace shogun * \f] * * \sa CLinearHMM - * \sa CDistribution + * \sa Distribution * */ -class CPluginEstimate: public CMachine +class PluginEstimate: public Machine { public: @@ -41,24 +41,24 @@ class CPluginEstimate: public CMachine * @param pos_pseudo pseudo for positive model * @param neg_pseudo pseudo for negative model */ - CPluginEstimate(float64_t pos_pseudo=1e-10, float64_t neg_pseudo=1e-10); - virtual ~CPluginEstimate(); + PluginEstimate(float64_t pos_pseudo=1e-10, float64_t neg_pseudo=1e-10); + virtual ~PluginEstimate(); /** classify objects * * @param data (test)data to be classified * @return classified labels */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /** set features * * @param feat features to set */ - virtual void set_features(CStringFeatures* feat) + virtual void set_features(std::shared_ptr> feat) { - SG_REF(feat); - SG_UNREF(features); + + features=feat; } @@ -66,7 +66,7 @@ class CPluginEstimate: public CMachine * * @return features */ - virtual CStringFeatures* get_features() { SG_REF(features); return features; } + virtual std::shared_ptr> get_features() { return features; } /// classify the test feature vector indexed by vec_idx float64_t apply_one(int32_t vec_idx); @@ -159,14 +159,14 @@ class CPluginEstimate: public CMachine { int32_t num_params; - SG_UNREF(pos_model); - pos_model=new CLinearHMM(seq_length, num_symbols); - SG_REF(pos_model); + + pos_model=std::make_shared(seq_length, num_symbols); + - SG_UNREF(neg_model); - neg_model=new CLinearHMM(seq_length, num_symbols); - SG_REF(neg_model); + + neg_model=std::make_shared(seq_length, num_symbols); + num_params=pos_model->get_num_model_parameters(); ASSERT(seq_length*num_symbols==num_params) @@ -206,7 +206,7 @@ class CPluginEstimate: public CMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); protected: /** pseudo count for positive class */ @@ -215,12 +215,12 @@ class CPluginEstimate: public CMachine float64_t m_neg_pseudo; /** positive model */ - CLinearHMM* pos_model; + std::shared_ptr pos_model; /** negative model */ - CLinearHMM* neg_model; + std::shared_ptr neg_model; /** features */ - CStringFeatures* features; + std::shared_ptr> features; }; } #endif diff --git a/src/shogun/classifier/mkl/MKL.cpp b/src/shogun/classifier/mkl/MKL.cpp index d2d415dd067..c183ed957db 100644 --- a/src/shogun/classifier/mkl/MKL.cpp +++ b/src/shogun/classifier/mkl/MKL.cpp @@ -24,7 +24,7 @@ extern "C" { using namespace shogun; -class CMKL::Self +class MKL::Self { public: void init() { @@ -102,18 +102,18 @@ class CMKL::Self float64_t* lin_term=SG_MALLOC(float64_t, num_kernels+1); int* ind=SG_MALLOC(int, num_kernels+1); - //CMath::display_vector(beta, num_kernels, "beta"); - double const_term = 1-CMath::qsq(beta, num_kernels, mkl_norm); + //Math::display_vector(beta, num_kernels, "beta"); + double const_term = 1-Math::qsq(beta, num_kernels, mkl_norm); //io::print("const={}\n", const_term); - ASSERT(CMath::fequal(const_term, 0.0)) + ASSERT(Math::fequal(const_term, 0.0)) for (int32_t i=0; i s) : SVM() { SG_DEBUG("creating MKL object {}", fmt::ptr(this)) register_params(); @@ -244,7 +244,7 @@ CMKL::CMKL(CSVM* s) : CSVM() self->init(); } -CMKL::~CMKL() +MKL::~MKL() { // -- Delete beta_local for ElasticnetMKL SG_FREE(beta_local); @@ -252,10 +252,10 @@ CMKL::~CMKL() SG_DEBUG("deleting MKL object {}", fmt::ptr(this)) if (svm) svm->set_callback_function(NULL, NULL); - SG_UNREF(svm); + } -void CMKL::register_params() +void MKL::register_params() { svm =NULL; C_mkl = 0; @@ -279,7 +279,7 @@ void CMKL::register_params() SG_ADD(&mkl_block_norm, "mkl_block_norm", "mkl sparse trade-off parameter", ParameterProperties::HYPER); - m_parameters->add_vector(&beta_local, &beta_local_size, "beta_local", "subkernel weights on L1 term of elastic net mkl"); + /*m_parameters->add_vector(&beta_local, &beta_local_size, "beta_local", "subkernel weights on L1 term of elastic net mkl");*/ watch_param("beta_local", &beta_local, &beta_local_size, AnyParameterProperties("subkernel weights on L1 term of elastic net mkl", ParameterProperties::MODEL)); @@ -294,9 +294,9 @@ void CMKL::register_params() // Missing: self (3rd party specific, handled in clone()) } -CSGObject* CMKL::clone() const +std::shared_ptr MKL::clone(ParameterProperties pp) const { - CMKL* cloned = (CMKL*) CSGObject::clone(); + auto cloned = std::static_pointer_cast(SGObject::clone(pp)); #ifdef USE_GLPK // GLPK params if (self->lp_glpk_parm != nullptr) @@ -318,7 +318,7 @@ CSGObject* CMKL::clone() const return cloned; } -void CMKL::init_solver() +void MKL::init_solver() { #ifdef USE_CPLEX self->cleanup_cplex(lp_initialized); @@ -335,7 +335,7 @@ void CMKL::init_solver() #endif } -bool CMKL::train_machine(CFeatures* data) +bool MKL::train_machine(std::shared_ptr data) { ASSERT(kernel) ASSERT(m_labels && m_labels->get_num_labels()) @@ -359,7 +359,7 @@ bool CMKL::train_machine(CFeatures* data) if (m_labels) num_label = m_labels->get_num_labels(); - io::info("{} trainlabels ({})", num_label, fmt::ptr(m_labels)); + io::info("{} trainlabels ({})", num_label, fmt::ptr(m_labels.get())); if (mkl_epsilon<=0) mkl_epsilon=1e-2 ; @@ -452,13 +452,10 @@ bool CMKL::train_machine(CFeatures* data) //but if we don't actually unref() the object we might leak memory... //So as a workaround we only unref when the reference count was >1 //before. - int32_t refs=this->ref(); - svm->set_callback_function(this, perform_mkl_step_helper); + svm->set_callback_function(shared_from_this()->as(), perform_mkl_step_helper); svm->train(); io::progress_done(); svm->set_callback_function(NULL, NULL); - if (refs>1) - this->unref(); } else { @@ -509,7 +506,7 @@ bool CMKL::train_machine(CFeatures* data) } -void CMKL::set_mkl_norm(float64_t norm) +void MKL::set_mkl_norm(float64_t norm) { if (norm<1) @@ -518,7 +515,7 @@ void CMKL::set_mkl_norm(float64_t norm) mkl_norm = norm; } -void CMKL::set_elasticnet_lambda(float64_t lambda) +void MKL::set_elasticnet_lambda(float64_t lambda) { if (lambda>1 || lambda<0) error("0<=lambda<=1"); @@ -531,7 +528,7 @@ void CMKL::set_elasticnet_lambda(float64_t lambda) ent_lambda=lambda; } -void CMKL::set_mkl_block_norm(float64_t q) +void MKL::set_mkl_block_norm(float64_t q) { if (q<1) error("1<=q<=inf"); @@ -539,7 +536,7 @@ void CMKL::set_mkl_block_norm(float64_t q) mkl_block_norm=q; } -bool CMKL::perform_mkl_step( +bool MKL::perform_mkl_step( const float64_t* sumw, float64_t suma) { if((training_time_clock.cur_time_diff()>get_max_train_time ())&&(get_max_train_time ()>0)) @@ -566,7 +563,7 @@ bool CMKL::perform_mkl_step( mkl_objective+=old_beta[i]*sumw[i]; } - w_gap = CMath::abs(1-rho/mkl_objective) ; + w_gap = Math::abs(1-rho/mkl_objective) ; if( (w_gap >= mkl_epsilon) || (get_solver_type()==ST_AUTO || get_solver_type()==ST_NEWTON || get_solver_type()==ST_DIRECT ) || get_solver_type()==ST_ELASTICNET || get_solver_type()==ST_BLOCK_NORM) @@ -599,8 +596,8 @@ bool CMKL::perform_mkl_step( else error("Solver type not supported (not compiled in?)"); - w_gap = CMath::abs(1-rho/mkl_objective); - } + w_gap = Math::abs(1-rho/mkl_objective); + } kernel->set_subkernel_weights(SGVector(beta, num_kernels, false)); SG_FREE(beta); @@ -608,7 +605,7 @@ bool CMKL::perform_mkl_step( return converged(); } -float64_t CMKL::compute_optimal_betas_elasticnet( +float64_t MKL::compute_optimal_betas_elasticnet( float64_t* beta, const float64_t* old_beta, const int32_t num_kernels, const float64_t* sumw, const float64_t suma, const float64_t mkl_objective ) @@ -646,7 +643,7 @@ float64_t CMKL::compute_optimal_betas_elasticnet( preR = 0.0; for( p=0; p= 0 ) ) { @@ -656,7 +653,7 @@ float64_t CMKL::compute_optimal_betas_elasticnet( io::print("MKL-direct: eps = {:e}\n", epsRegul ); for( p=0; p= 0 ) } - Z = CMath::pow( Z, -1.0 ); + Z = Math::pow( Z, -1.0 ); ASSERT( Z >= 0 ) for( p=0; pget_num_subkernels(); @@ -742,9 +739,10 @@ float64_t CMKL::compute_elasticnet_dual_objective() int32_t k=0; - for (index_t k_idx=0; k_idx<((CCombinedKernel*) kernel)->get_num_kernels(); k_idx++) + auto combined_kernel = std::static_pointer_cast(kernel); + for (index_t k_idx=0; k_idxget_num_kernels(); k_idx++) { - CKernel* kn = ((CCombinedKernel*) kernel)->get_kernel(k_idx); + auto kn = combined_kernel->get_kernel(k_idx); float64_t sum=0; for (int32_t i=0; ikernel(ii,jj); } } - nm[k]= CMath::pow(sum, 0.5); - del = CMath::max(del, nm[k]); + nm[k]= Math::pow(sum, 0.5); + del = Math::max(del, nm[k]); // io::print("nm[{}]={}\n",k,nm[k]); k++; - SG_UNREF(kn); + } // initial delta del = del / std::sqrt(2 * (1 - ent_lambda)); @@ -772,7 +770,7 @@ float64_t CMKL::compute_elasticnet_dual_objective() k=0; float64_t ff, gg, hh; elasticnet_dual(&ff, &gg, &hh, del, nm, num_kernels, ent_lambda); - while (CMath::abs(gg)>+1e-8 && k<100) + while (Math::abs(gg)>+1e-8 && k<100) { float64_t ff_old = ff; float64_t gg_old = gg; @@ -803,7 +801,7 @@ float64_t CMKL::compute_elasticnet_dual_objective() return -mkl_obj; } -float64_t CMKL::compute_optimal_betas_block_norm( +float64_t MKL::compute_optimal_betas_block_norm( float64_t* beta, const float64_t* old_beta, const int32_t num_kernels, const float64_t* sumw, const float64_t suma, const float64_t mkl_objective ) @@ -817,15 +815,15 @@ float64_t CMKL::compute_optimal_betas_block_norm( { ASSERT(sumw[p]>=0) - beta[p] = CMath::pow( sumw[p], -(2.0-mkl_block_norm)/(2.0-2.0*mkl_block_norm)); - Z+= CMath::pow( sumw[p], -(mkl_block_norm)/(2.0-2.0*mkl_block_norm)); + beta[p] = Math::pow( sumw[p], -(2.0-mkl_block_norm)/(2.0-2.0*mkl_block_norm)); + Z+= Math::pow( sumw[p], -(mkl_block_norm)/(2.0-2.0*mkl_block_norm)); ASSERT( beta[p] >= 0 ) } ASSERT(Z>=0) - Z=1.0/CMath::pow(Z, (2.0-mkl_block_norm)/mkl_block_norm); + Z=1.0/Math::pow(Z, (2.0-mkl_block_norm)/mkl_block_norm); for( p=0; p= 0.0 && old_beta[p] >= 0.0 ) { beta[p] = sumw[p] * old_beta[p]*old_beta[p] / mkl_norm; - beta[p] = CMath::pow( beta[p], 1.0 / (mkl_norm+1.0) ); + beta[p] = Math::pow( beta[p], 1.0 / (mkl_norm+1.0) ); } else { @@ -872,9 +870,9 @@ float64_t CMKL::compute_optimal_betas_directly( // --- normalize Z = 0.0; for( p=0; p= 0 ) for( p=0; p= 0 ) ) @@ -893,7 +891,7 @@ float64_t CMKL::compute_optimal_betas_directly( io::print("MKL-direct: eps = {:e}\n", epsRegul ); for( p=0; p= 0 ) } - Z = CMath::pow( Z, -1.0/mkl_norm ); + Z = Math::pow( Z, -1.0/mkl_norm ); ASSERT( Z >= 0 ) for( p=0; p= 0 ) - //gamma += CMath::pow( sumw[p] * beta[p]*beta[p], r ); - gamma += CMath::pow( sumw[p] * beta[p]*beta[p] / mkl_norm, r ); + //gamma += Math::pow( sumw[p] * beta[p]*beta[p], r ); + gamma += Math::pow( sumw[p] * beta[p]*beta[p] / mkl_norm, r ); } } - gamma = CMath::pow( gamma, 1.0/r ) / mkl_norm; + gamma = Math::pow( gamma, 1.0/r ) / mkl_norm; ASSERT( gamma > -1e-9 ) if( !( gamma > epsGamma ) ) { @@ -1016,7 +1014,7 @@ float64_t CMKL::compute_optimal_betas_newton(float64_t* beta, for( p=0; p= 0.0 ) ) io::warn("negative objective: {:e}. ", obj ); @@ -1033,12 +1031,12 @@ float64_t CMKL::compute_optimal_betas_newton(float64_t* beta, { ASSERT( 0.0 <= beta[p] && beta[p] <= 1.0 ) //const float halfw2p = ( sumw[p] >= 0.0 ) ? sumw[p] : 0.0; - //const float64_t t1 = halfw2p*beta[p] - mkl_norm*gamma*CMath::pow(beta[p],mkl_norm); - //const float64_t t2 = 2.0*halfw2p + gqq1*CMath::pow(beta[p],mkl_norm-1.0); + //const float64_t t1 = halfw2p*beta[p] - mkl_norm*gamma*Math::pow(beta[p],mkl_norm); + //const float64_t t2 = 2.0*halfw2p + gqq1*Math::pow(beta[p],mkl_norm-1.0); const float halfw2p = ( sumw[p] >= 0.0 ) ? (sumw[p]*old_beta[p]*old_beta[p]) : 0.0; - const float64_t t0 = halfw2p*beta[p] - mkl_norm*gamma*CMath::pow(beta[p],mkl_norm+2.0); + const float64_t t0 = halfw2p*beta[p] - mkl_norm*gamma*Math::pow(beta[p],mkl_norm+2.0); const float64_t t1 = ( t0 < 0 ) ? 0.0 : t0; - const float64_t t2 = 2.0*halfw2p + gqq1*CMath::pow(beta[p],mkl_norm+1.0); + const float64_t t2 = 2.0*halfw2p + gqq1*Math::pow(beta[p],mkl_norm+1.0); if( inLogSpace ) newtDir[p] = t1 / ( t1 + t2*beta[p] + hessRidge ); else @@ -1047,7 +1045,7 @@ float64_t CMKL::compute_optimal_betas_newton(float64_t* beta, ASSERT( newtDir[p] == newtDir[p] ) //io::print("newtDir[{}] = {:6.3f} = {:e} / {:e} \n", p, newtDir[p], t1, t2 ); } - //CMath::display_vector( newtDir, num_kernels, "newton direction " ); + //Math::display_vector( newtDir, num_kernels, "newton direction " ); //io::print("Newton step size = {:e}\n", Z ); // --- line search @@ -1067,17 +1065,17 @@ float64_t CMKL::compute_optimal_betas_newton(float64_t* beta, newtBeta[p] = beta[p] + stepSize * newtDir[p]; if( !( newtBeta[p] >= epsBeta ) ) newtBeta[p] = epsBeta; - Z += CMath::pow( newtBeta[p], mkl_norm ); + Z += Math::pow( newtBeta[p], mkl_norm ); } ASSERT( 0.0 <= Z ) - Z = CMath::pow( Z, -1.0/mkl_norm ); + Z = Math::pow( Z, -1.0/mkl_norm ); if( Z == 0.0 ) stepSize /= 2.0; } // --- normalize new beta (wrt p-norm) ASSERT( 0.0 < Z ) - ASSERT( Z < CMath::INFTY ) + ASSERT( Z < Math::INFTY ) for( p=0; p2)) @@ -1395,7 +1393,7 @@ float64_t CMKL::compute_optimal_betas_via_cplex(float64_t* new_beta, const float if (solution_ok) { /* 1 norm mkl */ - float64_t max_slack = -CMath::INFTY ; + float64_t max_slack = -Math::INFTY ; int32_t max_idx = -1 ; int32_t start_row = 1 ; if (C_mkl!=0.0) @@ -1418,7 +1416,7 @@ float64_t CMKL::compute_optimal_betas_via_cplex(float64_t* new_beta, const float } else // 2-norm or general q-norm { - if ((CMath::abs(slack[i])<1e-6)) + if ((Math::abs(slack[i])<1e-6)) num_active_rows++ ; else { @@ -1432,7 +1430,7 @@ float64_t CMKL::compute_optimal_betas_via_cplex(float64_t* new_beta, const float } // have at most max(100,num_active_rows*2) rows, if not, remove one - if ( (num_rows-start_row>CMath::max(100,2*num_active_rows)) && (max_idx!=-1)) + if ( (num_rows-start_row>Math::max(100,2*num_active_rows)) && (max_idx!=-1)) { //io::info("-{}({},{})",max_idx,start_row,num_rows); status = CPXdelrows (self->env, self->lp_cplex, max_idx, max_idx) ; @@ -1440,7 +1438,7 @@ float64_t CMKL::compute_optimal_betas_via_cplex(float64_t* new_beta, const float error("Failed to remove an old row."); } - //CMath::display_vector(x, num_kernels, "beta"); + //Math::display_vector(x, num_kernels, "beta"); rho = -x[2*num_kernels] ; SG_FREE(pi); @@ -1464,7 +1462,7 @@ float64_t CMKL::compute_optimal_betas_via_cplex(float64_t* new_beta, const float return rho; } -float64_t CMKL::compute_optimal_betas_via_glpk(float64_t* beta, const float64_t* old_beta, +float64_t MKL::compute_optimal_betas_via_glpk(float64_t* beta, const float64_t* old_beta, int num_kernels, const float64_t* sumw, float64_t suma, int32_t& inner_iters) { SG_DEBUG("MKL via GLPK") @@ -1580,7 +1578,7 @@ float64_t CMKL::compute_optimal_betas_via_glpk(float64_t* beta, const float64_t* int32_t num_active_rows=0; if(res) { - float64_t max_slack = CMath::INFTY; + float64_t max_slack = Math::INFTY; int32_t max_idx = -1; int32_t start_row = 1; if (C_mkl!=0.0) @@ -1600,7 +1598,7 @@ float64_t CMKL::compute_optimal_betas_via_glpk(float64_t* beta, const float64_t* } } - if ((num_rows-start_row>CMath::max(100, 2*num_active_rows)) && max_idx!=-1) + if ((num_rows-start_row>Math::max(100, 2*num_active_rows)) && max_idx!=-1) { int del_rows[2]; del_rows[1] = max_idx+1; @@ -1618,7 +1616,7 @@ float64_t CMKL::compute_optimal_betas_via_glpk(float64_t* beta, const float64_t* return obj; } -void CMKL::compute_sum_beta(float64_t* sumw) +void MKL::compute_sum_beta(float64_t* sumw) { ASSERT(sumw) ASSERT(svm) @@ -1663,7 +1661,7 @@ void CMKL::compute_sum_beta(float64_t* sumw) // assumes that all constraints are satisfied -float64_t CMKL::compute_mkl_dual_objective() +float64_t MKL::compute_mkl_dual_objective() { if (get_solver_type()==ST_ELASTICNET) { @@ -1676,9 +1674,10 @@ float64_t CMKL::compute_mkl_dual_objective() if (m_labels && kernel && kernel->get_kernel_type() == K_COMBINED) { - for (index_t k_idx=0; k_idx<((CCombinedKernel*) kernel)->get_num_kernels(); k_idx++) + auto combined_kernel = std::static_pointer_cast(kernel); + for (index_t k_idx=0; k_idxget_num_kernels(); k_idx++) { - CKernel* kn = ((CCombinedKernel*) kernel)->get_kernel(k_idx); + auto kn = combined_kernel->get_kernel(k_idx); float64_t sum=0; for (int32_t i=0; i s=NULL); /** Destructor */ - virtual ~CMKL(); + virtual ~MKL(); - virtual CSGObject* clone() const; + virtual std::shared_ptr clone(ParameterProperties pp = ParameterProperties::ALL) const; /** SVM to use as constraint generator in MKL SIP * * @param s svm */ - inline void set_constraint_generator(CSVM* s) + inline void set_constraint_generator(std::shared_ptr s) { set_svm(s); } @@ -106,10 +106,10 @@ class CMKL : public CSVM * * @param s svm */ - inline void set_svm(CSVM* s) + inline void set_svm(std::shared_ptr s) { - SG_REF(s); - SG_UNREF(svm); + + svm=s; } @@ -117,9 +117,9 @@ class CMKL : public CSVM * * @return svm */ - inline CSVM* get_svm() + inline std::shared_ptr get_svm() { - SG_REF(svm); + return svm; } @@ -212,7 +212,7 @@ class CMKL : public CSVM * * given sum of alphas, objectives for current alphas for each kernel * and current kernel weighting compute the corresponding optimal - * kernel weighting (all via get/set_subkernel_weights in CCombinedKernel) + * kernel weighting (all via get/set_subkernel_weights in CombinedKernel) * * @param sumw vector of 1/2*alpha'*K_j*alpha for each kernel j * @param suma scalar sum_i alpha_i etc. @@ -226,7 +226,7 @@ class CMKL : public CSVM * @param sumw vector of 1/2*alpha'*K_j*alpha for each kernel j * @param suma scalar sum_i alpha_i etc. */ - static bool perform_mkl_step_helper (CMKL* mkl, + static bool perform_mkl_step_helper (std::shared_ptr mkl, const float64_t* sumw, const float64_t suma) { return mkl->perform_mkl_step(sumw, suma); @@ -256,7 +256,7 @@ class CMKL : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** check run before starting training (to e.g. check if labeling is * two-class labeling in classification case @@ -402,7 +402,7 @@ class CMKL : public CSVM protected: /** wrapper SVM */ - CSVM* svm; + std::shared_ptr svm; /** C_mkl */ float64_t C_mkl; /** norm used in mkl must be > 0 */ @@ -435,7 +435,7 @@ class CMKL : public CSVM float64_t rho; /** measures training time for use with get_max_train_time() */ - CTime training_time_clock; + Time training_time_clock; /** Opaque parameters of MKL */ class Self; diff --git a/src/shogun/classifier/mkl/MKLClassification.cpp b/src/shogun/classifier/mkl/MKLClassification.cpp index 442dd64614c..cfe8bcc9710 100644 --- a/src/shogun/classifier/mkl/MKLClassification.cpp +++ b/src/shogun/classifier/mkl/MKLClassification.cpp @@ -6,39 +6,39 @@ using namespace shogun; -CMKLClassification::CMKLClassification(CSVM* s) : CMKL(s) +MKLClassification::MKLClassification(std::shared_ptr s) : MKL(s) { if (!s) { #ifdef USE_SVMLIGHT - s=new CSVMLight(); + s=std::make_shared(); #endif //USE_SVMLIGHT if (!s) - s=new CLibSVM(); + s=std::make_shared(); set_svm(s); } } -CMKLClassification::~CMKLClassification() +MKLClassification::~MKLClassification() { } -float64_t CMKLClassification::compute_sum_alpha() +float64_t MKLClassification::compute_sum_alpha() { float64_t suma=0; int32_t nsv=svm->get_num_support_vectors(); for (int32_t i=0; iget_alpha(i)); + suma+=Math::abs(svm->get_alpha(i)); return suma; } -void CMKLClassification::init_training() +void MKLClassification::init_training() { require(m_labels, "Labels not set."); require(m_labels->get_num_labels(), "Number of labels is zero."); } -CMKLClassification* CMKLClassification::obtain_from_generic(CMachine* machine) +std::shared_ptr MKLClassification::obtain_from_generic(std::shared_ptr machine) { if (machine == NULL) return NULL; @@ -46,7 +46,7 @@ CMKLClassification* CMKLClassification::obtain_from_generic(CMachine* machine) if (machine->get_classifier_type() != CT_MKLCLASSIFICATION) error("Provided machine is not of type CMKLClassification!"); - CMKLClassification* casted = dynamic_cast(machine); - SG_REF(casted) + auto casted = std::dynamic_pointer_cast(machine); + return casted; } diff --git a/src/shogun/classifier/mkl/MKLClassification.h b/src/shogun/classifier/mkl/MKLClassification.h index ea165210fa0..070e78cb5e3 100644 --- a/src/shogun/classifier/mkl/MKLClassification.h +++ b/src/shogun/classifier/mkl/MKLClassification.h @@ -18,20 +18,20 @@ namespace shogun * Learns an SVM classifier and its kernel weights. Makes only sense if * multiple kernels are used. * - * \sa CMKL + * \sa MKL */ -class CMKLClassification : public CMKL +class MKLClassification : public MKL { public: /** Constructor * * @param s SVM to use as constraint generator in MKL SILP */ - CMKLClassification(CSVM* s=NULL); + MKLClassification(std::shared_ptr s=NULL); /** Destructor */ - virtual ~CMKLClassification(); + virtual ~MKLClassification(); /** compute beta independent term from objective, e.g., in 2-class MKL * sum_i alpha_i etc @@ -46,7 +46,7 @@ class CMKLClassification : public CMKL #ifndef SWIG [[deprecated("use .as template function")]] #endif - static CMKLClassification* obtain_from_generic(CMachine* machine); + static std::shared_ptr obtain_from_generic(std::shared_ptr machine); /** @return object name */ virtual const char* get_name() const { return "MKLClassification"; } diff --git a/src/shogun/classifier/mkl/MKLMulticlass.cpp b/src/shogun/classifier/mkl/MKLMulticlass.cpp index ce127c25b38..6e9a1458d8e 100644 --- a/src/shogun/classifier/mkl/MKLMulticlass.cpp +++ b/src/shogun/classifier/mkl/MKLMulticlass.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Chiyuan Zhang, Giovanni De Toni, + * Authors: Soeren Sonnenburg, Chiyuan Zhang, Giovanni De Toni, * Heiko Strathmann, Sergey Lisitsyn, Bjoern Esser, Saurabh Goyal */ @@ -16,8 +16,8 @@ using namespace shogun; -CMKLMulticlass::CMKLMulticlass() -: CMulticlassSVM(new CMulticlassOneVsRestStrategy()) +MKLMulticlass::MKLMulticlass() +: MulticlassSVM(std::make_shared()) { svm=NULL; lpw=NULL; @@ -27,8 +27,8 @@ CMKLMulticlass::CMKLMulticlass() pnorm=1; } -CMKLMulticlass::CMKLMulticlass(float64_t C, CKernel* k, CLabels* lab) -: CMulticlassSVM(new CMulticlassOneVsRestStrategy(), C, k, lab) +MKLMulticlass::MKLMulticlass(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: MulticlassSVM(std::make_shared(), C, k, lab) { svm=NULL; lpw=NULL; @@ -39,71 +39,67 @@ CMKLMulticlass::CMKLMulticlass(float64_t C, CKernel* k, CLabels* lab) } -CMKLMulticlass::~CMKLMulticlass() +MKLMulticlass::~MKLMulticlass() { - SG_UNREF(svm); - svm=NULL; - delete lpw; - lpw=NULL; } -CMKLMulticlass::CMKLMulticlass( const CMKLMulticlass & cm) -: CMulticlassSVM(new CMulticlassOneVsRestStrategy()) +MKLMulticlass::MKLMulticlass( const MKLMulticlass & cm) +: MulticlassSVM(std::make_shared()) { svm=NULL; lpw=NULL; error( - " CMKLMulticlass::CMKLMulticlass(const CMKLMulticlass & cm): must " + " MKLMulticlass::MKLMulticlass(const MKLMulticlass & cm): must " "not be called, glpk structure is currently not copyable"); } -CMKLMulticlass CMKLMulticlass::operator=( const CMKLMulticlass & cm) +MKLMulticlass MKLMulticlass::operator=( const MKLMulticlass & cm) { error( - " CMKLMulticlass CMKLMulticlass::operator=(...): must " + " MKLMulticlass MKLMulticlass::operator=(...): must " "not be called, glpk structure is currently not copyable"); return (*this); } -void CMKLMulticlass::initsvm() +void MKLMulticlass::initsvm() { if (!m_labels) { - error("CMKLMulticlass::initsvm(): the set labels is NULL"); + error("MKLMulticlass::initsvm(): the set labels is NULL"); } - SG_UNREF(svm); - svm=new CGMNPSVM; - SG_REF(svm); + + svm=std::make_shared(); + svm->set_C(get_C()); svm->set_epsilon(get_epsilon()); if (m_labels->get_num_labels()<=0) { - error("CMKLMulticlass::initsvm(): the number of labels is " + error("MKLMulticlass::initsvm(): the number of labels is " "nonpositive, do not know how to handle this!\n"); } svm->set_labels(m_labels); } -void CMKLMulticlass::initlpsolver() +void MKLMulticlass::initlpsolver() { if (!m_kernel) { - error("CMKLMulticlass::initlpsolver(): the set kernel is NULL"); + error("MKLMulticlass::initlpsolver(): the set kernel is NULL"); } if (m_kernel->get_kernel_type()!=K_COMBINED) { - error("CMKLMulticlass::initlpsolver(): given kernel is not of type" + error("MKLMulticlass::initlpsolver(): given kernel is not of type" " K_COMBINED {} required by Multiclass Mkl \n", m_kernel->get_kernel_type()); } - int numker=dynamic_cast(m_kernel)->get_num_subkernels(); + int numker=std::dynamic_pointer_cast(m_kernel)->get_num_subkernels(); ASSERT(numker>0) /* @@ -116,19 +112,19 @@ void CMKLMulticlass::initlpsolver() //lpw=new MKLMulticlassGLPK; if(pnorm>1) { - lpw=new MKLMulticlassGradient; + lpw=std::make_shared(); lpw->set_mkl_norm(pnorm); } else { - lpw=new MKLMulticlassGLPK; + lpw=std::make_shared(); } lpw->setup(numker); } -bool CMKLMulticlass::evaluatefinishcriterion(const int32_t +bool MKLMulticlass::evaluatefinishcriterion(const int32_t numberofsilpiterations) { if ( (max_num_mkl_iters>0) && (numberofsilpiterations>=max_num_mkl_iters) ) @@ -190,7 +186,7 @@ bool CMKLMulticlass::evaluatefinishcriterion(const int32_t } else { - io::warn("CMKLMulticlass::evaluatefinishcriterion(...): deltanew<=0.Switching back to weight norsm difference as criterion."); + io::warn("MKLMulticlass::evaluatefinishcriterion(...): deltanew<=0.Switching back to weight norsm difference as criterion."); delta=sqrt(delta); } io::info("weight delta {} ",delta); @@ -206,7 +202,7 @@ bool CMKLMulticlass::evaluatefinishcriterion(const int32_t return false; } -void CMKLMulticlass::addingweightsstep( const std::vector & +void MKLMulticlass::addingweightsstep( const std::vector & curweights) { @@ -233,7 +229,7 @@ void CMKLMulticlass::addingweightsstep( const std::vector & curalphaterm=sumofsignfreealphas; int32_t numkernels= - dynamic_cast(m_kernel)->get_num_subkernels(); + std::dynamic_pointer_cast(m_kernel)->get_num_subkernels(); normweightssquared.resize(numkernels); @@ -245,24 +241,24 @@ void CMKLMulticlass::addingweightsstep( const std::vector & lpw->addconstraint(normweightssquared,sumofsignfreealphas); } -float64_t CMKLMulticlass::getsumofsignfreealphas() +float64_t MKLMulticlass::getsumofsignfreealphas() { std::vector trainlabels2(m_labels->get_num_labels()); - SGVector lab=((CMulticlassLabels*) m_labels)->get_int_labels(); + SGVector lab=(std::static_pointer_cast(m_labels))->get_int_labels(); std::copy(lab.vector,lab.vector+lab.vlen, trainlabels2.begin()); ASSERT (trainlabels2.size()>0) float64_t sum=0; - for (int32_t nc=0; nc< ((CMulticlassLabels*) m_labels)->get_num_classes();++nc) + for (int32_t nc=0; nc< (std::static_pointer_cast(m_labels))->get_num_classes();++nc) { - CSVM * sm=svm->get_svm(nc); + auto sm=svm->get_svm(nc); float64_t bia=sm->get_bias(); sum+= 0.5*bia*bia; - SG_UNREF(sm); + } index_t basealphas_y = 0, basealphas_x = 0; @@ -271,38 +267,38 @@ float64_t CMKLMulticlass::getsumofsignfreealphas() for (size_t lb=0; lb< trainlabels2.size();++lb) { - for (int32_t nc=0; nc< ((CMulticlassLabels*) m_labels)->get_num_classes();++nc) + for (int32_t nc=0; nc< (std::static_pointer_cast(m_labels))->get_num_classes();++nc) { - CSVM * sm=svm->get_svm(nc); + auto sm=svm->get_svm(nc); if ((int)nc!=trainlabels2[lb]) { - CSVM * sm2=svm->get_svm(trainlabels2[lb]); + auto sm2=svm->get_svm(trainlabels2[lb]); float64_t bia1=sm2->get_bias(); float64_t bia2=sm->get_bias(); - SG_UNREF(sm2); + sum+= -basealphas[lb*basealphas_y + nc]*(bia1-bia2-1); } - SG_UNREF(sm); + } } return sum; } -float64_t CMKLMulticlass::getsquarenormofprimalcoefficients( +float64_t MKLMulticlass::getsquarenormofprimalcoefficients( const int32_t ind) { - CKernel * ker=dynamic_cast(m_kernel)->get_kernel(ind); + auto ker=std::dynamic_pointer_cast(m_kernel)->get_kernel(ind); float64_t tmp=0; - for (int32_t classindex=0; classindex< ((CMulticlassLabels*) m_labels)->get_num_classes(); + for (int32_t classindex=0; classindex< (std::static_pointer_cast(m_labels))->get_num_classes(); ++classindex) { - CSVM * sm=svm->get_svm(classindex); + auto sm=svm->get_svm(classindex); for (int32_t i=0; i < sm->get_num_support_vectors(); ++i) { @@ -319,23 +315,23 @@ float64_t CMKLMulticlass::getsquarenormofprimalcoefficients( } } - SG_UNREF(sm); + } - SG_UNREF(ker); + ker=NULL; return tmp; } -bool CMKLMulticlass::train_machine(CFeatures* data) +bool MKLMulticlass::train_machine(std::shared_ptr data) { ASSERT(m_kernel) ASSERT(m_labels && m_labels->get_num_labels()) ASSERT(m_labels->get_label_type() == LT_MULTICLASS) init_strategy(); - int numcl=((CMulticlassLabels*) m_labels)->get_num_classes(); + int numcl=(std::static_pointer_cast(m_labels))->get_num_classes(); if (data) { @@ -353,7 +349,7 @@ bool CMKLMulticlass::train_machine(CFeatures* data) weightshistory.clear(); int32_t numkernels= - dynamic_cast(m_kernel)->get_num_subkernels(); + std::dynamic_pointer_cast(m_kernel)->get_num_subkernels(); ::std::vector curweights(numkernels,1.0/numkernels); weightshistory.push_back(curweights); @@ -394,8 +390,8 @@ bool CMKLMulticlass::train_machine(CFeatures* data) for (int32_t i=0; iget_svm(i); - CSVM* nsvm=new CSVM(osvm->get_num_support_vectors()); + auto osvm=svm->get_svm(i); + auto nsvm=std::make_shared(osvm->get_num_support_vectors()); for (int32_t k=0; kget_num_support_vectors() ; k++) { @@ -405,24 +401,20 @@ bool CMKLMulticlass::train_machine(CFeatures* data) nsvm->set_bias(osvm->get_bias() ); set_svm(i, nsvm); - SG_UNREF(osvm); + osvm=NULL; } - SG_UNREF(svm); + svm=NULL; - if (lpw) - { - delete lpw; - } - lpw=NULL; + lpw.reset(); return true; } -float64_t* CMKLMulticlass::getsubkernelweights(int32_t & numweights) +float64_t* MKLMulticlass::getsubkernelweights(int32_t & numweights) { if ( weightshistory.empty() ) { @@ -438,19 +430,19 @@ float64_t* CMKLMulticlass::getsubkernelweights(int32_t & numweights) return res; } -void CMKLMulticlass::set_mkl_epsilon(float64_t eps ) +void MKLMulticlass::set_mkl_epsilon(float64_t eps ) { mkl_eps=eps; } -void CMKLMulticlass::set_max_num_mkliters(int32_t maxnum) +void MKLMulticlass::set_max_num_mkliters(int32_t maxnum) { max_num_mkl_iters=maxnum; } -void CMKLMulticlass::set_mkl_norm(float64_t norm) +void MKLMulticlass::set_mkl_norm(float64_t norm) { pnorm=norm; if(pnorm<1 ) - error("CMKLMulticlass::set_mkl_norm(float64_t norm) : parameter pnorm<1"); + error("MKLMulticlass::set_mkl_norm(float64_t norm) : parameter pnorm<1"); } diff --git a/src/shogun/classifier/mkl/MKLMulticlass.h b/src/shogun/classifier/mkl/MKLMulticlass.h index 4138b1bd93f..4452d10ca1c 100644 --- a/src/shogun/classifier/mkl/MKLMulticlass.h +++ b/src/shogun/classifier/mkl/MKLMulticlass.h @@ -31,21 +31,21 @@ namespace shogun * set_max_num_mkliters(int32_t maxnum); It passes the regularization * constants C1 and C2 to GMNPSVM. */ -class CMKLMulticlass : public CMulticlassSVM +class MKLMulticlass : public MulticlassSVM { public: /** Class default Constructor */ - CMKLMulticlass(); + MKLMulticlass(); /** Class Constructor commonly used in Shogun Toolbox * @param C constant C * @param k kernel * @param lab labels */ - CMKLMulticlass(float64_t C, CKernel* k, CLabels* lab); + MKLMulticlass(float64_t C, std::shared_ptr k, std::shared_ptr lab); /** Class default Destructor */ - virtual ~CMKLMulticlass(); + virtual ~MKLMulticlass(); /** get classifier type * @@ -92,13 +92,13 @@ class CMKLMulticlass : public CMulticlassSVM * protected to avoid its usage * */ - CMKLMulticlass( const CMKLMulticlass & cm); + MKLMulticlass( const MKLMulticlass & cm); /** Class Assignment operator * protected to avoid its usage * */ - CMKLMulticlass operator=( const CMKLMulticlass & cm); + MKLMulticlass operator=( const MKLMulticlass & cm); /** performs some sanity checks (on the provided kernel), inits the * GLPK-based LP solver @@ -155,7 +155,7 @@ class CMKLMulticlass : public CMulticlassSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** @return object name */ virtual const char* get_name() const { return "MKLMulticlass"; } @@ -165,10 +165,10 @@ class CMKLMulticlass : public CMulticlassSVM * * */ - CGMNPSVM* svm; + std::shared_ptr svm; /** the solver wrapper */ - MKLMulticlassOptimizationBase* lpw; + std::shared_ptr lpw; /** stores the last two mkl iteration weights */ ::std::vector< std::vector< float64_t> > weightshistory; diff --git a/src/shogun/classifier/mkl/MKLMulticlassGradient.cpp b/src/shogun/classifier/mkl/MKLMulticlassGradient.cpp index 710113eccd7..57e2ae962df 100644 --- a/src/shogun/classifier/mkl/MKLMulticlassGradient.cpp +++ b/src/shogun/classifier/mkl/MKLMulticlassGradient.cpp @@ -379,7 +379,7 @@ finalbeta=oldweights; if( oldweights[p] >= 0.0 ) { finalbeta[p] = normsofsubkernels.back()[p] * oldweights[p]*oldweights[p] / pnorm; - finalbeta[p] = CMath::pow( finalbeta[p], 1.0 / (pnorm+1.0) ); + finalbeta[p] = Math::pow( finalbeta[p], 1.0 / (pnorm+1.0) ); } else { @@ -392,9 +392,9 @@ finalbeta=oldweights; // --- normalize float64_t Z = 0.0; for( int32_t p=0; p= 0 ) for( int32_t p=0; p= 0 ) ) @@ -413,7 +413,7 @@ finalbeta=oldweights; io::print("MKL-direct: eps = {:e}\n", epsRegul ); for( int32_t p=0; p= 0 ) } - Z = CMath::pow( Z, -1.0/pnorm ); + Z = Math::pow( Z, -1.0/pnorm ); ASSERT( Z >= 0 ) for( int32_t p=0; p s) : MKL(s) { if (!s) - set_svm(new CLibSVMOneClass()); + set_svm(std::make_shared()); } -CMKLOneClass::~CMKLOneClass() +MKLOneClass::~MKLOneClass() { } -float64_t CMKLOneClass::compute_sum_alpha() +float64_t MKLOneClass::compute_sum_alpha() { return 0.0; } -void CMKLOneClass::init_training() +void MKLOneClass::init_training() { ASSERT(svm) ASSERT(svm->get_classifier_type() == CT_LIBSVMONECLASS) diff --git a/src/shogun/classifier/mkl/MKLOneClass.h b/src/shogun/classifier/mkl/MKLOneClass.h index 1901e36045d..81ba5107af1 100644 --- a/src/shogun/classifier/mkl/MKLOneClass.h +++ b/src/shogun/classifier/mkl/MKLOneClass.h @@ -18,20 +18,20 @@ namespace shogun * Learns a One-Class SVM classifier and its kernel weights. Makes only sense * if multiple kernels are used. * - * \sa CMKL + * \sa MKL */ -class CMKLOneClass : public CMKL +class MKLOneClass : public MKL { public: /** Constructor * * @param s SVM to use as constraint generator in MKL SILP */ - CMKLOneClass(CSVM* s=NULL); + MKLOneClass(std::shared_ptr s=NULL); /** Destructor */ - virtual ~CMKLOneClass(); + virtual ~MKLOneClass(); /** compute beta independent term from objective, e.g., in 2-class MKL * sum_i alpha_i etc diff --git a/src/shogun/classifier/svm/CPLEXSVM.cpp b/src/shogun/classifier/svm/CPLEXSVM.cpp index d0e2378d1d3..ccaca9ce1d1 100644 --- a/src/shogun/classifier/svm/CPLEXSVM.cpp +++ b/src/shogun/classifier/svm/CPLEXSVM.cpp @@ -17,7 +17,7 @@ using namespace shogun; CCPLEXSVM::CCPLEXSVM() -: CSVM() +: SVM() { } @@ -25,7 +25,7 @@ CCPLEXSVM::~CCPLEXSVM() { } -bool CCPLEXSVM::train_machine(CFeatures* data) +bool CCPLEXSVM::train_machine(Features* data) { ASSERT(m_labels) ASSERT(m_labels->get_label_type() == LT_BINARY) @@ -48,7 +48,7 @@ bool CCPLEXSVM::train_machine(CFeatures* data) { int32_t n,m; int32_t num_label=0; - SGVector y=((CBinaryLabels*)m_labels)->get_labels(); + SGVector y=((BinaryLabels*)m_labels)->get_labels(); SGMatrix H=kernel->get_kernel_matrix(); m=H.num_rows; n=H.num_cols; @@ -76,7 +76,7 @@ bool CCPLEXSVM::train_machine(CFeatures* data) if (alphas[i]>0) { //set_alpha(j, alphas[i]*labels->get_label(i)/etas[1]); - set_alpha(j, alphas[i]*((CBinaryLabels*) m_labels)->get_int_label(i)); + set_alpha(j, alphas[i]*((BinaryLabels*) m_labels)->get_int_label(i)); set_support_vector(j, i); j++; } diff --git a/src/shogun/classifier/svm/CPLEXSVM.h b/src/shogun/classifier/svm/CPLEXSVM.h index 3a257c6ea83..ef58213d292 100644 --- a/src/shogun/classifier/svm/CPLEXSVM.h +++ b/src/shogun/classifier/svm/CPLEXSVM.h @@ -19,7 +19,7 @@ namespace shogun { #define IGNORE_IN_CLASSLIST /** @brief CplexSVM a SVM solver implementation based on cplex (unfinished). */ -IGNORE_IN_CLASSLIST class CCPLEXSVM : public CSVM +IGNORE_IN_CLASSLIST class CCPLEXSVM : public SVM { public: CCPLEXSVM(); @@ -36,7 +36,7 @@ IGNORE_IN_CLASSLIST class CCPLEXSVM : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(Features* data=NULL); }; } #endif diff --git a/src/shogun/classifier/svm/GNPPLib.cpp b/src/shogun/classifier/svm/GNPPLib.cpp index d693577d10e..c73161381cd 100644 --- a/src/shogun/classifier/svm/GNPPLib.cpp +++ b/src/shogun/classifier/svm/GNPPLib.cpp @@ -21,7 +21,7 @@ using namespace shogun; #define INDEX(ROW,COL,DIM) ((COL*DIM)+ROW) -CGNPPLib::CGNPPLib() +GNPPLib::GNPPLib() { unstable(SOURCE_LOCATION); @@ -35,9 +35,9 @@ CGNPPLib::CGNPPLib() m_kernel = NULL; } -CGNPPLib::CGNPPLib( - float64_t* vector_y, CKernel* kernel, int32_t num_data, float64_t reg_const) -: CSGObject() +GNPPLib::GNPPLib( + float64_t* vector_y, std::shared_ptr kernel, int32_t num_data, float64_t reg_const) +: SGObject() { m_reg_const = reg_const; m_num_data = num_data; @@ -45,7 +45,7 @@ CGNPPLib::CGNPPLib( m_kernel = kernel; Cache_Size = ((int64_t) kernel->get_cache_size())*1024*1024/(sizeof(float64_t)*num_data); - Cache_Size = CMath::min(Cache_Size, (int64_t) num_data); + Cache_Size = Math::min(Cache_Size, (int64_t) num_data); io::info("using {} kernel cache lines", Cache_Size); ASSERT(Cache_Size>=2) @@ -63,7 +63,7 @@ CGNPPLib::CGNPPLib( } -CGNPPLib::~CGNPPLib() +GNPPLib::~GNPPLib() { for(int32_t i = 0; i < Cache_Size; i++ ) SG_FREE(kernel_columns[i]); @@ -78,7 +78,7 @@ CGNPPLib::~CGNPPLib() Usage: exitflag = gnpp_mdm( diag_H, vector_c, vector_y, dim, tmax, tolabs, tolrel, th, &alpha, &t, &aHa11, &aHa22, &History ); -------------------------------------------------------------- */ -int8_t CGNPPLib::gnpp_mdm(float64_t *diag_H, +int8_t GNPPLib::gnpp_mdm(float64_t *diag_H, float64_t *vector_c, float64_t *vector_y, int32_t dim, @@ -184,7 +184,7 @@ int8_t CGNPPLib::gnpp_mdm(float64_t *diag_H, /* Stopping conditions */ if( UB-LB <= tolabs ) exitflag = 1; - else if(UB-LB <= CMath::abs(UB)*tolrel ) exitflag = 2; + else if(UB-LB <= Math::abs(UB)*tolrel ) exitflag = 2; else if(LB > th) exitflag = 3; else exitflag = -1; @@ -206,7 +206,7 @@ int8_t CGNPPLib::gnpp_mdm(float64_t *diag_H, Huv = col_u[v1]; lambda = delta1/(alpha[v1]*(Huu - 2*Huv + Hvv )); - lambda = CMath::min(1.0,lambda); + lambda = Math::min(1.0,lambda); tmp = lambda*alpha[v1]; @@ -246,7 +246,7 @@ int8_t CGNPPLib::gnpp_mdm(float64_t *diag_H, Huv = col_u[v2]; lambda = delta2/(alpha[v2]*( Huu - 2*Huv + Hvv )); - lambda = CMath::min(1.0,lambda); + lambda = Math::min(1.0,lambda); tmp = lambda*alpha[v2]; aHa22 = aHa22 + 2*tmp*( Ha2[u2]-Ha2[v2]) + tmp*tmp*( Huu - 2*Huv + Hvv); @@ -285,7 +285,7 @@ int8_t CGNPPLib::gnpp_mdm(float64_t *diag_H, /* Stopping conditions */ if( UB-LB <= tolabs ) exitflag = 1; - else if( UB-LB <= CMath::abs(UB)*tolrel ) exitflag = 2; + else if( UB-LB <= Math::abs(UB)*tolrel ) exitflag = 2; else if(LB > th) exitflag = 3; else if(t >= tmax) exitflag = 0; @@ -343,7 +343,7 @@ int8_t CGNPPLib::gnpp_mdm(float64_t *diag_H, Usage: exitflag = gnpp_imdm( diag_H, vector_c, vector_y, dim, tmax, tolabs, tolrel, th, &alpha, &t, &aHa11, &aHa22, &History ); -------------------------------------------------------------- */ -int8_t CGNPPLib::gnpp_imdm(float64_t *diag_H, +int8_t GNPPLib::gnpp_imdm(float64_t *diag_H, float64_t *vector_c, float64_t *vector_y, int32_t dim, @@ -464,7 +464,7 @@ int8_t CGNPPLib::gnpp_imdm(float64_t *diag_H, /* Stopping conditions */ if( UB-LB <= tolabs ) exitflag = 1; - else if(UB-LB <= CMath::abs(UB)*tolrel ) exitflag = 2; + else if(UB-LB <= Math::abs(UB)*tolrel ) exitflag = 2; else if(LB > th) exitflag = 3; else exitflag = -1; @@ -483,7 +483,7 @@ int8_t CGNPPLib::gnpp_imdm(float64_t *diag_H, Huv = col_u[v1]; lambda = delta1/(alpha[v1]*(Huu - 2*Huv + Hvv )); - lambda = CMath::min(1.0,lambda); + lambda = Math::min(1.0,lambda); tmp = lambda*alpha[v1]; @@ -520,7 +520,7 @@ int8_t CGNPPLib::gnpp_imdm(float64_t *diag_H, Huv = col_u[v2]; lambda = delta2/(alpha[v2]*( Huu - 2*Huv + Hvv )); - lambda = CMath::min(1.0,lambda); + lambda = Math::min(1.0,lambda); tmp = lambda*alpha[v2]; aHa22 = aHa22 + 2*tmp*( Ha2[u2]-Ha2[v2]) + tmp*tmp*( Huu - 2*Huv + Hvv); @@ -621,7 +621,7 @@ int8_t CGNPPLib::gnpp_imdm(float64_t *diag_H, /* Stopping conditions */ if( UB-LB <= tolabs ) exitflag = 1; - else if( UB-LB <= CMath::abs(UB)*tolrel ) exitflag = 2; + else if( UB-LB <= Math::abs(UB)*tolrel ) exitflag = 2; else if(LB > th) exitflag = 3; else if(t >= tmax) exitflag = 0; @@ -673,7 +673,7 @@ int8_t CGNPPLib::gnpp_imdm(float64_t *diag_H, } -float64_t* CGNPPLib::get_col(int64_t a, int64_t b) +float64_t* GNPPLib::get_col(int64_t a, int64_t b) { float64_t *col_ptr; float64_t y; diff --git a/src/shogun/classifier/svm/GNPPLib.h b/src/shogun/classifier/svm/GNPPLib.h index e779a882fba..c81c9ae75eb 100644 --- a/src/shogun/classifier/svm/GNPPLib.h +++ b/src/shogun/classifier/svm/GNPPLib.h @@ -19,11 +19,11 @@ namespace shogun /** @brief class GNPPLib, a Library of solvers for Generalized Nearest Point * Problem (GNPP). */ -class CGNPPLib: public CSGObject +class GNPPLib: public SGObject { public: /** default constructor */ - CGNPPLib(); + GNPPLib(); /** constructor * @@ -32,8 +32,8 @@ class CGNPPLib: public CSGObject * @param num_data number of data * @param reg_const reg const */ - CGNPPLib(float64_t* vector_y, CKernel* kernel, int32_t num_data, float64_t reg_const); - virtual ~CGNPPLib(); + GNPPLib(float64_t* vector_y, std::shared_ptr kernel, int32_t num_data, float64_t reg_const); + virtual ~GNPPLib(); /** -------------------------------------------------------------- QP solver based on MDM algorithm. @@ -104,7 +104,7 @@ class CGNPPLib: public CSGObject /** vector y */ float64_t* m_vector_y; /** kernel */ - CKernel* m_kernel; + std::shared_ptr m_kernel; }; } diff --git a/src/shogun/classifier/svm/GNPPSVM.cpp b/src/shogun/classifier/svm/GNPPSVM.cpp index 310cbe0f25b..a22bfce2f90 100644 --- a/src/shogun/classifier/svm/GNPPSVM.cpp +++ b/src/shogun/classifier/svm/GNPPSVM.cpp @@ -10,23 +10,23 @@ #include using namespace shogun; -#define INDEX(ROW,COL,DIM) (((COL)*(DIM))+(ROW)) +#define INDEX(ROW,COL,DIM) (((COL)*(DIM))+(ROW)) -CGNPPSVM::CGNPPSVM() -: CSVM() +GNPPSVM::GNPPSVM() +: SVM() { } -CGNPPSVM::CGNPPSVM(float64_t C, CKernel* k, CLabels* lab) -: CSVM(C, k, lab) +GNPPSVM::GNPPSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: SVM(C, k, lab) { } -CGNPPSVM::~CGNPPSVM() +GNPPSVM::~GNPPSVM() { } -bool CGNPPSVM::train_machine(CFeatures* data) +bool GNPPSVM::train_machine(std::shared_ptr data) { ASSERT(kernel) ASSERT(m_labels && m_labels->get_num_labels()) @@ -43,9 +43,10 @@ bool CGNPPSVM::train_machine(CFeatures* data) io::info("{} trainlabels", num_data); float64_t* vector_y = SG_MALLOC(float64_t, num_data); + auto bl = binary_labels(m_labels); for (int32_t i=0; iget_label(i); + float64_t lab=bl->get_label(i); if (lab==+1) vector_y[i]=1; else if (lab==-1) @@ -78,11 +79,11 @@ bool CGNPPSVM::train_machine(CFeatures* data) int32_t verb=0; float64_t aHa11, aHa22; - CGNPPLib npp(vector_y,kernel,num_data, reg_const); + GNPPLib npp(vector_y,kernel,num_data, reg_const); - npp.gnpp_imdm(diagK, vector_c, vector_y, num_data, - tmax, tolabs, tolrel, thlb, alpha, &t, &aHa11, &aHa22, - &History, verb ); + npp.gnpp_imdm(diagK, vector_c, vector_y, num_data, + tmax, tolabs, tolrel, thlb, alpha, &t, &aHa11, &aHa22, + &History, verb ); int32_t num_sv = 0; float64_t nconst = History[INDEX(1,t,2)]; @@ -91,7 +92,7 @@ bool CGNPPSVM::train_machine(CFeatures* data) for(int32_t i = 0; i < num_data; i++ ) { if( alpha[i] != 0 ) num_sv++; - if(vector_y[i] == 1) + if(vector_y[i] == 1) { alpha[i] = alpha[i]*2/nconst; if( alpha[i]/(2*C) >= 1 ) trnerr++; @@ -106,7 +107,7 @@ bool CGNPPSVM::train_machine(CFeatures* data) float64_t b = 0.5*(aHa22 - aHa11)/nconst;; create_new_model(num_sv); - CSVM::set_objective(nconst); + SVM::set_objective(nconst); set_bias(b); int32_t j = 0; diff --git a/src/shogun/classifier/svm/GNPPSVM.h b/src/shogun/classifier/svm/GNPPSVM.h index e17eeef8d96..670c7792785 100644 --- a/src/shogun/classifier/svm/GNPPSVM.h +++ b/src/shogun/classifier/svm/GNPPSVM.h @@ -15,11 +15,11 @@ namespace shogun { /** @brief class GNPPSVM */ -class CGNPPSVM : public CSVM +class GNPPSVM : public SVM { public: /** default constructor */ - CGNPPSVM(); + GNPPSVM(); /** constructor * @@ -27,9 +27,9 @@ class CGNPPSVM : public CSVM * @param k kernel * @param lab labels */ - CGNPPSVM(float64_t C, CKernel* k, CLabels* lab); + GNPPSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab); - virtual ~CGNPPSVM(); + virtual ~GNPPSVM(); /** get classifier type * @@ -49,7 +49,7 @@ class CGNPPSVM : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); }; } #endif diff --git a/src/shogun/classifier/svm/LibLinear.cpp b/src/shogun/classifier/svm/LibLinear.cpp index 12dfb264738..c4dd3b9ac64 100644 --- a/src/shogun/classifier/svm/LibLinear.cpp +++ b/src/shogun/classifier/svm/LibLinear.cpp @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Giovanni De Toni, Liang Pang, - * Heiko Strathmann, Weijie Lin, Youssef Emad El-Din, Thoralf Klein, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Giovanni De Toni, Liang Pang, + * Heiko Strathmann, Weijie Lin, Youssef Emad El-Din, Thoralf Klein, * Bjoern Esser */ #include @@ -22,19 +22,19 @@ using namespace shogun; -CLibLinear::CLibLinear() : RandomMixin() +LibLinear::LibLinear() : RandomMixin() { init(); } -CLibLinear::CLibLinear(LIBLINEAR_SOLVER_TYPE l) : RandomMixin() +LibLinear::LibLinear(LIBLINEAR_SOLVER_TYPE l) : RandomMixin() { init(); set_liblinear_solver_type(l); } -CLibLinear::CLibLinear(float64_t C, CDotFeatures* traindat, CLabels* trainlab) - : RandomMixin() +LibLinear::LibLinear(float64_t C, std::shared_ptr traindat, std::shared_ptr trainlab) + : RandomMixin() { init(); set_C(C, C); @@ -45,7 +45,7 @@ CLibLinear::CLibLinear(float64_t C, CDotFeatures* traindat, CLabels* trainlab) init_linear_term(); } -void CLibLinear::init() +void LibLinear::init() { set_liblinear_solver_type(L2R_L1LOSS_SVC_DUAL); set_bias_enabled(true); @@ -64,14 +64,14 @@ void CLibLinear::init() "Type of LibLinear solver.", ParameterProperties::SETTING, SG_OPTIONS( L2R_LR, L2R_L2LOSS_SVC_DUAL, L2R_L2LOSS_SVC, L2R_L1LOSS_SVC_DUAL, - L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL)) + L1R_L2LOSS_SVC, L1R_LR, L2R_LR_DUAL)); } -CLibLinear::~CLibLinear() +LibLinear::~LibLinear() { } -bool CLibLinear::train_machine(CFeatures* data) +bool LibLinear::train_machine(std::shared_ptr data) { ASSERT(m_labels) @@ -82,7 +82,7 @@ bool CLibLinear::train_machine(CFeatures* data) if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*)data); + set_features(std::static_pointer_cast(data)); } ASSERT(features) @@ -102,7 +102,7 @@ bool CLibLinear::train_machine(CFeatures* data) "training labels {}", num_feat, num_train_labels); } - CMath::swap(num_feat, num_vec); + Math::swap(num_feat, num_vec); } else { @@ -171,8 +171,8 @@ bool CLibLinear::train_machine(CFeatures* data) case L2R_LR: { fun_obj = new l2r_lr_fun(&prob, Cs); - CTron tron_obj( - fun_obj, get_epsilon() * CMath::min(pos, neg) / prob.l, + Tron tron_obj( + fun_obj, get_epsilon() * Math::min(pos, neg) / prob.l, get_max_iterations()); SG_DEBUG("starting L2R_LR training via tron") tron_obj.tron(w.vector, m_max_train_time); @@ -183,8 +183,8 @@ bool CLibLinear::train_machine(CFeatures* data) case L2R_L2LOSS_SVC: { fun_obj = new l2r_l2_svc_fun(&prob, Cs); - CTron tron_obj( - fun_obj, get_epsilon() * CMath::min(pos, neg) / prob.l, + Tron tron_obj( + fun_obj, get_epsilon() * Math::min(pos, neg) / prob.l, get_max_iterations()); tron_obj.tron(w.vector, m_max_train_time); delete fun_obj; @@ -202,14 +202,14 @@ bool CLibLinear::train_machine(CFeatures* data) { // ASSUME FEATURES ARE TRANSPOSED ALREADY solve_l1r_l2_svc( - w, &prob, get_epsilon() * CMath::min(pos, neg) / prob.l, Cp, Cn); + w, &prob, get_epsilon() * Math::min(pos, neg) / prob.l, Cp, Cn); break; } case L1R_LR: { // ASSUME FEATURES ARE TRANSPOSED ALREADY solve_l1r_lr( - w, &prob, get_epsilon() * CMath::min(pos, neg) / prob.l, Cp, Cn); + w, &prob, get_epsilon() * Math::min(pos, neg) / prob.l, Cp, Cn); break; } case L2R_LR_DUAL: @@ -263,7 +263,7 @@ bool CLibLinear::train_machine(CFeatures* data) #define GETI(i) (y[i] + 1) // To support weights for instances, use GETI(i) (i) -void CLibLinear::solve_l2r_l1l2_svc( +void LibLinear::solve_l2r_l1l2_svc( SGVector& w, const liblinear_problem* prob, double eps, double Cp, double Cn, LIBLINEAR_SOLVER_TYPE st) { @@ -279,8 +279,8 @@ void CLibLinear::solve_l2r_l1l2_svc( // PG: projected gradient, for shrinking and stopping double PG; - double PGmax_old = CMath::INFTY; - double PGmin_old = -CMath::INFTY; + double PGmax_old = Math::INFTY; + double PGmin_old = -Math::INFTY; double PGmax_new, PGmin_new; SGVector linear_term; @@ -291,7 +291,7 @@ void CLibLinear::solve_l2r_l1l2_svc( // default solver_type: L2R_L2LOSS_SVC_DUAL double diag[3] = {0.5 / Cn, 0, 0.5 / Cp}; - double upper_bound[3] = {CMath::INFTY, 0, CMath::INFTY}; + double upper_bound[3] = {Math::INFTY, 0, Math::INFTY}; if (st == L2R_L1LOSS_SVC_DUAL) { diag[0] = 0; @@ -326,7 +326,7 @@ void CLibLinear::solve_l2r_l1l2_svc( } auto pb = SG_PROGRESS(range(10)); - CTime start_time; + Time start_time; while (iter < get_max_iterations()) { COMPUTATION_CONTROLLERS @@ -334,8 +334,8 @@ void CLibLinear::solve_l2r_l1l2_svc( start_time.cur_time_diff() > m_max_train_time) break; - PGmax_new = -CMath::INFTY; - PGmin_new = CMath::INFTY; + PGmax_new = -Math::INFTY; + PGmin_new = Math::INFTY; random::shuffle(index, index+active_size, m_prng); @@ -362,7 +362,7 @@ void CLibLinear::solve_l2r_l1l2_svc( if (G > PGmax_old) { active_size--; - CMath::swap(index[s], index[active_size]); + Math::swap(index[s], index[active_size]); s--; continue; } @@ -374,7 +374,7 @@ void CLibLinear::solve_l2r_l1l2_svc( if (G < PGmin_old) { active_size--; - CMath::swap(index[s], index[active_size]); + Math::swap(index[s], index[active_size]); s--; continue; } @@ -384,13 +384,13 @@ void CLibLinear::solve_l2r_l1l2_svc( else PG = G; - PGmax_new = CMath::max(PGmax_new, PG); - PGmin_new = CMath::min(PGmin_new, PG); + PGmax_new = Math::max(PGmax_new, PG); + PGmin_new = Math::min(PGmin_new, PG); if (fabs(PG) > 1.0e-12) { double alpha_old = alpha[i]; - alpha[i] = CMath::min(CMath::max(alpha[i] - G / QD[i], 0.0), C); + alpha[i] = Math::min(Math::max(alpha[i] - G / QD[i], 0.0), C); d = (alpha[i] - alpha_old) * yi; prob->x->add_to_dense_vec(d, i, w.vector, n); @@ -404,7 +404,7 @@ void CLibLinear::solve_l2r_l1l2_svc( float64_t gap=PGmax_new - PGmin_new; pb.print_absolute( - gap, -CMath::log10(gap), -CMath::log10(1), -CMath::log10(eps)); + gap, -Math::log10(gap), -Math::log10(1), -Math::log10(eps)); if (gap <= eps) { @@ -413,17 +413,17 @@ void CLibLinear::solve_l2r_l1l2_svc( else { active_size = l; - PGmax_old = CMath::INFTY; - PGmin_old = -CMath::INFTY; + PGmax_old = Math::INFTY; + PGmin_old = -Math::INFTY; continue; } } PGmax_old = PGmax_new; PGmin_old = PGmin_new; if (PGmax_old <= 0) - PGmax_old = CMath::INFTY; + PGmax_old = Math::INFTY; if (PGmin_old >= 0) - PGmin_old = -CMath::INFTY; + PGmin_old = -Math::INFTY; } pb.complete_absolute(); @@ -471,7 +471,7 @@ void CLibLinear::solve_l2r_l1l2_svc( #define GETI(i) (y[i] + 1) // To support weights for instances, use GETI(i) (i) -void CLibLinear::solve_l1r_l2_svc( +void LibLinear::solve_l1r_l2_svc( SGVector& w, liblinear_problem* prob_col, double eps, double Cp, double Cn) { @@ -483,7 +483,7 @@ void CLibLinear::solve_l1r_l2_svc( double sigma = 0.01; double d, G_loss, G, H; - double Gmax_old = CMath::INFTY; + double Gmax_old = Math::INFTY; double Gmax_new; double Gmax_init = 0; double d_old, d_diff; @@ -495,7 +495,7 @@ void CLibLinear::solve_l1r_l2_svc( double* b = SG_MALLOC(double, l); // b = 1-ywTx double* xj_sq = SG_MALLOC(double, w_size); - CDotFeatures* x = (CDotFeatures*)prob_col->x; + auto x = std::static_pointer_cast(prob_col->x); void* iterator; int32_t ind; float64_t val; @@ -536,7 +536,7 @@ void CLibLinear::solve_l1r_l2_svc( } auto pb = SG_PROGRESS(range(10)); - CTime start_time; + Time start_time; while (iter < get_max_iterations()) { COMPUTATION_CONTROLLERS @@ -586,7 +586,7 @@ void CLibLinear::solve_l1r_l2_svc( G = G_loss; H *= 2; - H = CMath::max(H, 1e-12); + H = Math::max(H, 1e-12); double Gp = G + 1; double Gn = G - 1; @@ -600,7 +600,7 @@ void CLibLinear::solve_l1r_l2_svc( else if (Gp > Gmax_old / l && Gn < -Gmax_old / l) { active_size--; - CMath::swap(index[s], index[active_size]); + Math::swap(index[s], index[active_size]); s--; continue; } @@ -610,7 +610,7 @@ void CLibLinear::solve_l1r_l2_svc( else violation = fabs(Gn); - Gmax_new = CMath::max(Gmax_new, violation); + Gmax_new = Math::max(Gmax_new, violation); // obtain Newton direction d if (Gp <= H * w.vector[j]) @@ -756,8 +756,8 @@ void CLibLinear::solve_l1r_l2_svc( iter++; pb.print_absolute( - Gmax_new, -CMath::log10(Gmax_new), -CMath::log10(Gmax_init), - -CMath::log10(eps * Gmax_init)); + Gmax_new, -Math::log10(Gmax_new), -Math::log10(Gmax_init), + -Math::log10(eps * Gmax_init)); if (Gmax_new <= eps * Gmax_init) { @@ -766,7 +766,7 @@ void CLibLinear::solve_l1r_l2_svc( else { active_size = w_size; - Gmax_old = CMath::INFTY; + Gmax_old = Math::INFTY; continue; } } @@ -819,7 +819,7 @@ void CLibLinear::solve_l1r_l2_svc( #define GETI(i) (y[i] + 1) // To support weights for instances, use GETI(i) (i) -void CLibLinear::solve_l1r_lr( +void LibLinear::solve_l1r_lr( SGVector& w, const liblinear_problem* prob_col, double eps, double Cp, double Cn) { @@ -832,7 +832,7 @@ void CLibLinear::solve_l1r_lr( double x_min = 0; double sigma = 0.01; double d, G, H; - double Gmax_old = CMath::INFTY; + double Gmax_old = Math::INFTY; double Gmax_new; double Gmax_init = 0; double sum1, appxcond1; @@ -848,7 +848,7 @@ void CLibLinear::solve_l1r_lr( double* xjneg_sum = SG_MALLOC(double, w_size); double* xjpos_sum = SG_MALLOC(double, w_size); - CDotFeatures* x = prob_col->x; + auto x = prob_col->x; void* iterator; int ind; double val; @@ -880,8 +880,8 @@ void CLibLinear::solve_l1r_lr( { for (ind = 0; ind < l; ind++) { - x_min = CMath::min(x_min, 1.0); - xj_max[j] = CMath::max(xj_max[j], 1.0); + x_min = Math::min(x_min, 1.0); + xj_max[j] = Math::max(xj_max[j], 1.0); C_sum[j] += C[GETI(ind)]; if (y[ind] == -1) xjneg_sum[j] += C[GETI(ind)]; @@ -894,8 +894,8 @@ void CLibLinear::solve_l1r_lr( iterator = x->get_feature_iterator(j); while (x->get_next_feature(ind, val, iterator)) { - x_min = CMath::min(x_min, val); - xj_max[j] = CMath::max(xj_max[j], val); + x_min = Math::min(x_min, val); + xj_max[j] = Math::max(xj_max[j], val); C_sum[j] += C[GETI(ind)]; if (y[ind] == -1) xjneg_sum[j] += C[GETI(ind)] * val; @@ -907,7 +907,7 @@ void CLibLinear::solve_l1r_lr( } auto pb = SG_PROGRESS(range(10)); - CTime start_time; + Time start_time; while (iter < get_max_iterations()) { COMPUTATION_CONTROLLERS @@ -969,7 +969,7 @@ void CLibLinear::solve_l1r_lr( else if (Gp > Gmax_old / l && Gn < -Gmax_old / l) { active_size--; - CMath::swap(index[s], index[active_size]); + Math::swap(index[s], index[active_size]); s--; continue; } @@ -979,7 +979,7 @@ void CLibLinear::solve_l1r_lr( else violation = fabs(Gn); - Gmax_new = CMath::max(Gmax_new, violation); + Gmax_new = Math::max(Gmax_new, violation); // obtain Newton direction d if (Gp <= H * w.vector[j]) @@ -992,7 +992,7 @@ void CLibLinear::solve_l1r_lr( if (fabs(d) < 1.0e-12) continue; - d = CMath::min(CMath::max(d, -10.0), 10.0); + d = Math::min(Math::max(d, -10.0), 10.0); double delta = fabs(w.vector[j] + d) - fabs(w.vector[j]) + G * d; int num_linesearch; @@ -1013,7 +1013,7 @@ void CLibLinear::solve_l1r_lr( log(1 + sum2 * (1 / tmp - 1) / xj_max[j] / C_sum[j]) * C_sum[j] + cond + d * xjneg_sum[j]; - if (CMath::min(appxcond1, appxcond2) <= 0) + if (Math::min(appxcond1, appxcond2) <= 0) { if (get_bias_enabled() && j == n) { @@ -1130,8 +1130,8 @@ void CLibLinear::solve_l1r_lr( iter++; pb.print_absolute( - Gmax_new, -CMath::log10(Gmax_new), -CMath::log10(Gmax_init), - -CMath::log10(eps * Gmax_init)); + Gmax_new, -Math::log10(Gmax_new), -Math::log10(Gmax_init), + -Math::log10(eps * Gmax_init)); if (Gmax_new <= eps * Gmax_init) { @@ -1140,7 +1140,7 @@ void CLibLinear::solve_l1r_lr( else { active_size = w_size; - Gmax_old = CMath::INFTY; + Gmax_old = Math::INFTY; continue; } } @@ -1205,7 +1205,7 @@ void CLibLinear::solve_l1r_lr( #define GETI(i) (y[i] + 1) // To support weights for instances, use GETI(i) (i) -void CLibLinear::solve_l2r_lr_dual( +void LibLinear::solve_l2r_lr_dual( SGVector& w, const liblinear_problem* prob, double eps, double Cp, double Cn) { @@ -1219,7 +1219,7 @@ void CLibLinear::solve_l2r_lr_dual( int32_t* y = new int32_t[l]; int max_inner_iter = 100; // for inner Newton double innereps = 1e-2; - double innereps_min = CMath::min(1e-8, eps); + double innereps_min = Math::min(1e-8, eps); double upper_bound[3] = {Cn, 0, Cp}; double Gmax_init = 0; @@ -1240,7 +1240,7 @@ void CLibLinear::solve_l2r_lr_dual( // alpha[2*i] + alpha[2*i+1] = upper_bound[GETI(i)] for (i = 0; i < l; i++) { - alpha[2 * i] = CMath::min(0.001 * upper_bound[GETI(i)], 1e-8); + alpha[2 * i] = Math::min(0.001 * upper_bound[GETI(i)], 1e-8); alpha[2 * i + 1] = upper_bound[GETI(i)] - alpha[2 * i]; } @@ -1295,7 +1295,7 @@ void CLibLinear::solve_l2r_lr_dual( if (C - z < 0.5 * C) z = 0.1 * z; double gp = a * (z - alpha_old) + sign * b + std::log(z / (C - z)); - Gmax = CMath::max(Gmax, CMath::abs(gp)); + Gmax = Math::max(Gmax, Math::abs(gp)); // Newton method on the sub-problem const double eta = 0.1; // xi in the paper @@ -1333,14 +1333,14 @@ void CLibLinear::solve_l2r_lr_dual( iter++; pb.print_absolute( - Gmax, -CMath::log10(Gmax), -CMath::log10(Gmax_init), - -CMath::log10(eps * Gmax_init)); + Gmax, -Math::log10(Gmax), -Math::log10(Gmax_init), + -Math::log10(eps * Gmax_init)); if (Gmax < eps) break; if (newton_iter <= l / 10) - innereps = CMath::max(innereps_min, 0.1 * innereps); + innereps = Math::max(innereps_min, 0.1 * innereps); } pb.complete_absolute(); @@ -1368,7 +1368,7 @@ void CLibLinear::solve_l2r_lr_dual( delete[] index; } -void CLibLinear::set_linear_term(const SGVector linear_term) +void LibLinear::set_linear_term(const SGVector linear_term) { if (!m_labels) error("Please assign labels first!"); @@ -1386,7 +1386,7 @@ void CLibLinear::set_linear_term(const SGVector linear_term) m_linear_term = linear_term; } -SGVector CLibLinear::get_linear_term() +SGVector LibLinear::get_linear_term() { if (!m_linear_term.vlen || !m_linear_term.vector) error("Please assign linear term first!"); @@ -1394,7 +1394,7 @@ SGVector CLibLinear::get_linear_term() return m_linear_term; } -void CLibLinear::init_linear_term() +void LibLinear::init_linear_term() { if (!m_labels) error("Please assign labels first!"); diff --git a/src/shogun/classifier/svm/LibLinear.h b/src/shogun/classifier/svm/LibLinear.h index da5be41669b..5155730510e 100644 --- a/src/shogun/classifier/svm/LibLinear.h +++ b/src/shogun/classifier/svm/LibLinear.h @@ -43,7 +43,7 @@ namespace shogun * large- * scale linear learning focusing on SVM [1]. This is the classification * interface. For - * regression, see CLibLinearRegression. There is also an online version, + * regression, see LibLinearRegression. There is also an online version, * see * COnlineLibLinear. * @@ -59,19 +59,19 @@ namespace shogun * * [1] http://www.csie.ntu.edu.tw/~cjlin/liblinear/ * */ - class CLibLinear : public RandomMixin + class LibLinear : public RandomMixin { public: MACHINE_PROBLEM_TYPE(PT_BINARY) /** default constructor */ - CLibLinear(); + LibLinear(); /** constructor * * @param liblinear_solver_type liblinear_solver_type */ - CLibLinear(LIBLINEAR_SOLVER_TYPE liblinear_solver_type); + LibLinear(LIBLINEAR_SOLVER_TYPE liblinear_solver_type); /** constructor (using L2R_L1LOSS_SVC_DUAL as default) * @@ -79,10 +79,10 @@ namespace shogun * @param traindat training features * @param trainlab training labels */ - CLibLinear(float64_t C, CDotFeatures* traindat, CLabels* trainlab); + LibLinear(float64_t C, std::shared_ptr traindat, std::shared_ptr trainlab); /** destructor */ - virtual ~CLibLinear(); + virtual ~LibLinear(); /** * @return the currently used liblinear solver @@ -222,7 +222,7 @@ namespace shogun * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data = NULL); private: /** set up parameters */ diff --git a/src/shogun/classifier/svm/LibSVM.cpp b/src/shogun/classifier/svm/LibSVM.cpp index 1f3fc6b78e7..85e699d4939 100644 --- a/src/shogun/classifier/svm/LibSVM.cpp +++ b/src/shogun/classifier/svm/LibSVM.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Leon Kuchenbecker, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Leon Kuchenbecker, * Sergey Lisitsyn */ @@ -11,30 +11,30 @@ using namespace shogun; -CLibSVM::CLibSVM() -: CSVM(), solver_type(LIBSVM_C_SVC) +LibSVM::LibSVM() +: SVM(), solver_type(LIBSVM_C_SVC) { register_params(); } -CLibSVM::CLibSVM(LIBSVM_SOLVER_TYPE st) -: CSVM(), solver_type(st) +LibSVM::LibSVM(LIBSVM_SOLVER_TYPE st) +: SVM(), solver_type(st) { register_params(); } -CLibSVM::CLibSVM(float64_t C, CKernel* k, CLabels* lab, LIBSVM_SOLVER_TYPE st) -: CSVM(C, k, lab), solver_type(st) +LibSVM::LibSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab, LIBSVM_SOLVER_TYPE st) +: SVM(C, k, lab), solver_type(st) { register_params(); } -CLibSVM::~CLibSVM() +LibSVM::~LibSVM() { } -void CLibSVM::register_params() +void LibSVM::register_params() { SG_ADD_OPTIONS( (machine_int_t*)&solver_type, "libsvm_solver_type", @@ -42,7 +42,7 @@ void CLibSVM::register_params() SG_OPTIONS(LIBSVM_C_SVC, LIBSVM_NU_SVC)); } -bool CLibSVM::train_machine(CFeatures* data) +bool LibSVM::train_machine(std::shared_ptr data) { svm_problem problem; svm_parameter param; @@ -126,7 +126,7 @@ bool CLibSVM::train_machine(CFeatures* data) param.gamma = 0; // 1/k param.coef0 = 0; param.nu = get_nu(); - param.kernel=kernel; + param.kernel=kernel.get(); param.cache_size = kernel->get_cache_size(); param.max_train_time = m_max_train_time; param.C = get_C1(); @@ -153,7 +153,7 @@ bool CLibSVM::train_machine(CFeatures* data) int32_t num_sv=model->l; create_new_model(num_sv); - CSVM::set_objective(model->objective); + SVM::set_objective(model->objective); float64_t sgn=model->label[0]; diff --git a/src/shogun/classifier/svm/LibSVM.h b/src/shogun/classifier/svm/LibSVM.h index 807373226ed..0ddfaceb53c 100644 --- a/src/shogun/classifier/svm/LibSVM.h +++ b/src/shogun/classifier/svm/LibSVM.h @@ -24,17 +24,17 @@ enum LIBSVM_SOLVER_TYPE }; #endif /** @brief LibSVM */ -class CLibSVM : public CSVM +class LibSVM : public SVM { public: /** Default constructor, create a C-SVC svm */ - CLibSVM(); + LibSVM(); /** Constructor * * @param st solver type C or NU SVC */ - CLibSVM(LIBSVM_SOLVER_TYPE st); + LibSVM(LIBSVM_SOLVER_TYPE st); /** constructor * @@ -43,10 +43,10 @@ class CLibSVM : public CSVM * @param lab labels * @param st solver type to use, C-SVC or nu-SVC */ - CLibSVM(float64_t C, CKernel* k, CLabels* lab, + LibSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab, LIBSVM_SOLVER_TYPE st=LIBSVM_C_SVC); - virtual ~CLibSVM(); + virtual ~LibSVM(); /** get classifier type * @@ -69,7 +69,7 @@ class CLibSVM : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); protected: /** solver type */ diff --git a/src/shogun/classifier/svm/LibSVMOneClass.cpp b/src/shogun/classifier/svm/LibSVMOneClass.cpp index 639cda29681..612f0c69128 100644 --- a/src/shogun/classifier/svm/LibSVMOneClass.cpp +++ b/src/shogun/classifier/svm/LibSVMOneClass.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, * Leon Kuchenbecker */ @@ -10,21 +10,21 @@ using namespace shogun; -CLibSVMOneClass::CLibSVMOneClass() -: CSVM() +LibSVMOneClass::LibSVMOneClass() +: SVM() { } -CLibSVMOneClass::CLibSVMOneClass(float64_t C, CKernel* k) -: CSVM(C, k, NULL) +LibSVMOneClass::LibSVMOneClass(float64_t C, std::shared_ptr k) +: SVM(C, k, NULL) { } -CLibSVMOneClass::~CLibSVMOneClass() +LibSVMOneClass::~LibSVMOneClass() { } -bool CLibSVMOneClass::train_machine(CFeatures* data) +bool LibSVMOneClass::train_machine(std::shared_ptr data) { svm_problem problem; svm_parameter param; @@ -59,7 +59,7 @@ bool CLibSVMOneClass::train_machine(CFeatures* data) param.gamma = 0; // 1/k param.coef0 = 0; param.nu = get_nu(); - param.kernel=kernel; + param.kernel=kernel.get(); param.cache_size = kernel->get_cache_size(); param.max_train_time = m_max_train_time; param.C = get_C1(); @@ -70,12 +70,11 @@ bool CLibSVMOneClass::train_machine(CFeatures* data) param.weight_label = weights_label; param.weight = weights; param.use_bias = get_bias_enabled(); - + const char* error_msg = svm_check_parameter(&problem,¶m); if(error_msg) error("Error: {}",error_msg); - model = svm_train(&problem, ¶m); if (model) @@ -86,7 +85,7 @@ bool CLibSVMOneClass::train_machine(CFeatures* data) int32_t num_sv=model->l; create_new_model(num_sv); - CSVM::set_objective(model->objective); + SVM::set_objective(model->objective); set_bias(-model->rho[0]); for (int32_t i=0; i k); + virtual ~LibSVMOneClass(); /** get classifier type * @@ -52,7 +52,7 @@ class CLibSVMOneClass : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); }; } #endif diff --git a/src/shogun/classifier/svm/MPDSVM.cpp b/src/shogun/classifier/svm/MPDSVM.cpp index 5338ff67805..4e8896dfe0c 100644 --- a/src/shogun/classifier/svm/MPDSVM.cpp +++ b/src/shogun/classifier/svm/MPDSVM.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Giovanni De Toni, Heiko Strathmann, + * Authors: Soeren Sonnenburg, Giovanni De Toni, Heiko Strathmann, * Evan Shelhamer, Sergey Lisitsyn */ @@ -14,21 +14,21 @@ using namespace shogun; -CMPDSVM::CMPDSVM() -: CSVM() +MPDSVM::MPDSVM() +: SVM() { } -CMPDSVM::CMPDSVM(float64_t C, CKernel* k, CLabels* lab) -: CSVM(C, k, lab) +MPDSVM::MPDSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: SVM(C, k, lab) { } -CMPDSVM::~CMPDSVM() +MPDSVM::~MPDSVM() { } -bool CMPDSVM::train_machine(CFeatures* data) +bool MPDSVM::train_machine(std::shared_ptr data) { auto labels = binary_labels(m_labels); @@ -56,7 +56,7 @@ bool CMPDSVM::train_machine(CFeatures* data) const float64_t dualeps=eps*n; //heuristic int64_t niter=0; - kernel_cache = new CCache(kernel->get_cache_size(), n, n); + kernel_cache = std::make_shared>(kernel->get_cache_size(), n, n); float64_t* alphas=SG_MALLOC(float64_t, n); float64_t* dalphas=SG_MALLOC(float64_t, n); //float64_t* hessres=SG_MALLOC(float64_t, 2*n); @@ -99,17 +99,17 @@ bool CMPDSVM::train_machine(CFeatures* data) COMPUTATION_CONTROLLERS int32_t maxpidx=-1; float64_t maxpviol = -1; - //float64_t maxdviol = CMath::abs(detas[0]); - float64_t maxdviol = CMath::abs(detas); + //float64_t maxdviol = Math::abs(detas[0]); + float64_t maxdviol = Math::abs(detas); bool free_alpha=false; - //if (CMath::abs(detas[1])> maxdviol) - //maxdviol=CMath::abs(detas[1]); + //if (Math::abs(detas[1])> maxdviol) + //maxdviol=Math::abs(detas[1]); // compute kkt violations with correct sign ... for (int32_t i=0; i 0 && alphas[i] < d) free_alpha=true; @@ -137,7 +137,7 @@ bool CMPDSVM::train_machine(CFeatures* data) // ... and evaluate stopping conditions //if (nustop) - //stopfac = CMath::max(etas[1], 1e-10); + //stopfac = Math::max(etas[1], 1e-10); //else //stopfac = 1; @@ -264,7 +264,6 @@ bool CMPDSVM::train_machine(CFeatures* data) SG_FREE(dalphas); SG_FREE(hessres); SG_FREE(F); - delete kernel_cache; return true; } diff --git a/src/shogun/classifier/svm/MPDSVM.h b/src/shogun/classifier/svm/MPDSVM.h index fba3b597ebb..372fae48ec0 100644 --- a/src/shogun/classifier/svm/MPDSVM.h +++ b/src/shogun/classifier/svm/MPDSVM.h @@ -17,11 +17,11 @@ namespace shogun { /** @brief class MPDSVM */ -class CMPDSVM : public CSVM +class MPDSVM : public SVM { public: /** default constructor */ - CMPDSVM(); + MPDSVM(); /** constructor * @@ -29,8 +29,8 @@ class CMPDSVM : public CSVM * @param k kernel * @param lab labels */ - CMPDSVM(float64_t C, CKernel* k, CLabels* lab); - virtual ~CMPDSVM(); + MPDSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab); + virtual ~MPDSVM(); /** get classifier type * @@ -50,7 +50,7 @@ class CMPDSVM : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** compute H * @@ -60,8 +60,8 @@ class CMPDSVM : public CSVM */ inline float64_t compute_H(int32_t i, int32_t j) { - return ((CBinaryLabels*) m_labels)->get_label(i)* - ((CBinaryLabels*) m_labels)->get_label(j)*kernel->kernel(i,j); + return (std::static_pointer_cast(m_labels))->get_label(i)* + (std::static_pointer_cast(m_labels))->get_label(j)*kernel->kernel(i,j); } /** lock kernel row @@ -85,7 +85,7 @@ class CMPDSVM : public CSVM ASSERT(line) for (int32_t j=0; jget_num_labels(); j++) - line[j]=(KERNELCACHE_ELEM) ((CBinaryLabels*) m_labels)->get_label(i)*((CBinaryLabels*) m_labels)->get_label(j)*kernel->kernel(i,j); + line[j]=(KERNELCACHE_ELEM) (std::static_pointer_cast(m_labels))->get_label(i)*(std::static_pointer_cast(m_labels))->get_label(j)*kernel->kernel(i,j); } return line; @@ -101,7 +101,7 @@ class CMPDSVM : public CSVM } /** kernel cache */ - CCache* kernel_cache; + std::shared_ptr> kernel_cache; }; } #endif /* _MPDSVM_H___ */ diff --git a/src/shogun/classifier/svm/NewtonSVM.cpp b/src/shogun/classifier/svm/NewtonSVM.cpp index 262e8456284..a7f16f5d1bb 100644 --- a/src/shogun/classifier/svm/NewtonSVM.cpp +++ b/src/shogun/classifier/svm/NewtonSVM.cpp @@ -19,7 +19,7 @@ //#define V_NEWTON using namespace shogun; -CNewtonSVM::CNewtonSVM() : CIterativeMachine() +NewtonSVM::NewtonSVM() : IterativeMachine() { lambda = 1; m_max_iterations = 20; @@ -28,9 +28,9 @@ CNewtonSVM::CNewtonSVM() : CIterativeMachine() t = 0; } -CNewtonSVM::CNewtonSVM( - float64_t c, CDotFeatures* traindat, CLabels* trainlab, int32_t itr) - : CIterativeMachine() +NewtonSVM::NewtonSVM( + float64_t c, std::shared_ptr traindat, std::shared_ptr trainlab, int32_t itr) + : IterativeMachine() { lambda=1/c; num_iter=itr; @@ -42,17 +42,17 @@ CNewtonSVM::CNewtonSVM( } -CNewtonSVM::~CNewtonSVM() +NewtonSVM::~NewtonSVM() { } -void CNewtonSVM::init_model(CFeatures* data) +void NewtonSVM::init_model(std::shared_ptr data) { if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } ASSERT(features) @@ -79,7 +79,7 @@ void CNewtonSVM::init_model(CFeatures* data) grad.set_const(0.0); } -void CNewtonSVM::iteration() +void NewtonSVM::iteration() { obj_fun_linear(); SGVector weights = get_w(); @@ -141,7 +141,7 @@ void CNewtonSVM::iteration() m_complete = true; } -void CNewtonSVM::line_search_linear(const SGVector d) +void NewtonSVM::line_search_linear(const SGVector d) { SGVector Y = binary_labels(m_labels)->get_labels(); SGVector outz(x_n); @@ -211,7 +211,7 @@ void CNewtonSVM::line_search_linear(const SGVector d) out = outz.clone(); } -void CNewtonSVM::obj_fun_linear() +void NewtonSVM::obj_fun_linear() { SGVector weights = get_w(); SGVector v = binary_labels(m_labels)->get_labels(); diff --git a/src/shogun/classifier/svm/NewtonSVM.h b/src/shogun/classifier/svm/NewtonSVM.h index 445b378195d..3fd65be9ba5 100644 --- a/src/shogun/classifier/svm/NewtonSVM.h +++ b/src/shogun/classifier/svm/NewtonSVM.h @@ -22,13 +22,13 @@ namespace shogun * This Implementation is ported from the Olivier Chapelles fast newton based SVM solver, Which could be found here :http://mloss.org/software/view/30/ * For further information on this implementation of SVM refer to this paper: http://www.kyb.mpg.de/publications/attachments/neco_%5B0%5D.pdf */ -class CNewtonSVM : public CIterativeMachine +class NewtonSVM : public IterativeMachine { public: MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - CNewtonSVM(); + NewtonSVM(); /** constructor * @param C constant C @@ -36,9 +36,9 @@ class CNewtonSVM : public CIterativeMachine * @param traindat training features * @param trainlab labels for features */ - CNewtonSVM(float64_t C, CDotFeatures* traindat, CLabels* trainlab, int32_t itr=20); + NewtonSVM(float64_t C, std::shared_ptr traindat, std::shared_ptr trainlab, int32_t itr=20); - virtual ~CNewtonSVM(); + virtual ~NewtonSVM(); /** get classifier type * @@ -93,7 +93,7 @@ class CNewtonSVM : public CIterativeMachine virtual const char* get_name() const { return "NewtonSVM"; } protected: - virtual void init_model(CFeatures* data); + virtual void init_model(std::shared_ptr data); virtual void iteration(); private: diff --git a/src/shogun/classifier/svm/OnlineLibLinear.cpp b/src/shogun/classifier/svm/OnlineLibLinear.cpp index 3ea68f8e820..f6678805b8f 100644 --- a/src/shogun/classifier/svm/OnlineLibLinear.cpp +++ b/src/shogun/classifier/svm/OnlineLibLinear.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Chiyuan Zhang, Sergey Lisitsyn, Thoralf Klein, + * Authors: Soeren Sonnenburg, Chiyuan Zhang, Sergey Lisitsyn, Thoralf Klein, * Viktor Gal, Weijie Lin, Evan Shelhamer, Sanuj Sharma */ @@ -14,14 +14,14 @@ using namespace shogun; -COnlineLibLinear::COnlineLibLinear() - : COnlineLinearMachine() +OnlineLibLinear::OnlineLibLinear() + : OnlineLinearMachine() { init(); } -COnlineLibLinear::COnlineLibLinear(float64_t C_reg) - : COnlineLinearMachine() +OnlineLibLinear::OnlineLibLinear(float64_t C_reg) + : OnlineLinearMachine() { init(); C1=C_reg; @@ -29,9 +29,9 @@ COnlineLibLinear::COnlineLibLinear(float64_t C_reg) use_bias=true; } -COnlineLibLinear::COnlineLibLinear( - float64_t C_reg, CStreamingDotFeatures* traindat) - : COnlineLinearMachine() +OnlineLibLinear::OnlineLibLinear( + float64_t C_reg, std::shared_ptr traindat) + : OnlineLinearMachine() { init(); C1=C_reg; @@ -41,8 +41,8 @@ COnlineLibLinear::COnlineLibLinear( set_features(traindat); } -COnlineLibLinear::COnlineLibLinear(COnlineLibLinear *mch) - : COnlineLinearMachine() +OnlineLibLinear::OnlineLibLinear(std::shared_ptrmch) + : OnlineLinearMachine() { init(); C1 = mch->C1; @@ -55,7 +55,7 @@ COnlineLibLinear::COnlineLibLinear(COnlineLibLinear *mch) } -void COnlineLibLinear::init() +void OnlineLibLinear::init() { C1=1; C2=1; @@ -69,10 +69,10 @@ void COnlineLibLinear::init() &use_bias, "use_bias", "Indicates if bias is used.", ParameterProperties::SETTING); PG = 0; - PGmax_old = CMath::INFTY; - PGmin_old = -CMath::INFTY; - PGmax_new = -CMath::INFTY; - PGmin_new = CMath::INFTY; + PGmax_old = Math::INFTY; + PGmin_old = -Math::INFTY; + PGmax_new = -Math::INFTY; + PGmin_new = Math::INFTY; diag[0]=0;diag[1]=0;diag[2]=0; upper_bound[0]=Cn;upper_bound[1]=0;upper_bound[2]=Cp; @@ -88,20 +88,20 @@ void COnlineLibLinear::init() alpha_current = 0; } -COnlineLibLinear::~COnlineLibLinear() +OnlineLibLinear::~OnlineLibLinear() { } -void COnlineLibLinear::start_train() +void OnlineLibLinear::start_train() { Cp = C1; Cn = C2; bias = false; - PGmax_old = CMath::INFTY; - PGmin_old = -CMath::INFTY; - PGmax_new = -CMath::INFTY; - PGmin_new = CMath::INFTY; + PGmax_old = Math::INFTY; + PGmin_old = -Math::INFTY; + PGmax_new = -Math::INFTY; + PGmin_new = Math::INFTY; diag[0]=0;diag[1]=0;diag[2]=0; upper_bound[0]=Cn;upper_bound[1]=0;upper_bound[2]=Cp; @@ -110,7 +110,7 @@ void COnlineLibLinear::start_train() nSV = 0; } -void COnlineLibLinear::stop_train() +void OnlineLibLinear::stop_train() { float64_t gap = PGmax_new - PGmin_new; @@ -126,7 +126,7 @@ void COnlineLibLinear::stop_train() io::info("gap = {:g}", gap); } -void COnlineLibLinear::train_one(SGVector ex, float64_t label) +void OnlineLibLinear::train_one(SGVector ex, float64_t label) { alpha_current = 0; int32_t y_current = 0; @@ -172,13 +172,13 @@ void COnlineLibLinear::train_one(SGVector ex, float64_t label) else PG = G; - PGmax_new = CMath::max(PGmax_new, PG); - PGmin_new = CMath::min(PGmin_new, PG); + PGmax_new = Math::max(PGmax_new, PG); + PGmin_new = Math::min(PGmin_new, PG); if (fabs(PG) > 1.0e-12) { float64_t alpha_old = alpha_current; - alpha_current = CMath::min(CMath::max(alpha_current - G/QD, 0.0), C); + alpha_current = Math::min(Math::max(alpha_current - G/QD, 0.0), C); d = (alpha_current - alpha_old) * y_current; linalg::add(m_w, ex, m_w, 1.0f, (float32_t)d); @@ -192,7 +192,7 @@ void COnlineLibLinear::train_one(SGVector ex, float64_t label) nSV++; } -void COnlineLibLinear::train_one(SGSparseVector ex, float64_t label) +void OnlineLibLinear::train_one(SGSparseVector ex, float64_t label) { alpha_current = 0; int32_t y_current = 0; @@ -238,13 +238,13 @@ void COnlineLibLinear::train_one(SGSparseVector ex, float64_t label) else PG = G; - PGmax_new = CMath::max(PGmax_new, PG); - PGmin_new = CMath::min(PGmin_new, PG); + PGmax_new = Math::max(PGmax_new, PG); + PGmin_new = Math::min(PGmin_new, PG); if (fabs(PG) > 1.0e-12) { float64_t alpha_old = alpha_current; - alpha_current = CMath::min(CMath::max(alpha_current - G/QD, 0.0), C); + alpha_current = Math::min(Math::max(alpha_current - G/QD, 0.0), C); d = (alpha_current - alpha_old) * y_current; for (int32_t i=0; i < ex.num_feat_entries; i++) @@ -260,21 +260,21 @@ void COnlineLibLinear::train_one(SGSparseVector ex, float64_t label) nSV++; } -void COnlineLibLinear::train_example(CStreamingDotFeatures *feature, float64_t label) +void OnlineLibLinear::train_example(std::shared_ptrfeature, float64_t label) { feature->expand_if_required(m_w.vector, m_w.vlen); if (feature->get_feature_class() == C_STREAMING_DENSE) { - CStreamingDenseFeatures *feat = - dynamic_cast *>(feature); + auto feat = + std::dynamic_pointer_cast>(feature); if (feat == NULL) error("Expected streaming dense feature "); train_one(feat->get_vector(), label); } else if (feature->get_feature_class() == C_STREAMING_SPARSE) { - CStreamingSparseFeatures *feat = - dynamic_cast *>(feature); + auto feat = + std::dynamic_pointer_cast>(feature); if (feat == NULL) error("Expected streaming sparse feature "); diff --git a/src/shogun/classifier/svm/OnlineLibLinear.h b/src/shogun/classifier/svm/OnlineLibLinear.h index 2c1b20d2c6f..f6a0ec89641 100644 --- a/src/shogun/classifier/svm/OnlineLibLinear.h +++ b/src/shogun/classifier/svm/OnlineLibLinear.h @@ -18,20 +18,20 @@ namespace shogun { -/** @brief Class implementing a purely online version of CLibLinear, +/** @brief Class implementing a purely online version of LibLinear, * using the L2R_L1LOSS_SVC_DUAL solver only. */ /** @brief This class provides an interface to the LibLinear library for large- * scale linear learning [1] focusing on SVM. This is the online-classification interface. For - * batch classification, see CLibLinear, for batch regression, see - * CLibLinearRegression. + * batch classification, see LibLinear, for batch regression, see + * LibLinearRegression. * * This class offers ::L2R_L1LOSS_SVC_DUAL only. * See the ::LIBLINEAR_SOLVER_TYPE enum for types of solvers for batch SVM. * * [1] http://www.csie.ntu.edu.tw/~cjlin/liblinear/ * */ -class COnlineLibLinear : public COnlineLinearMachine +class OnlineLibLinear : public OnlineLinearMachine { public: @@ -39,14 +39,14 @@ class COnlineLibLinear : public COnlineLinearMachine MACHINE_PROBLEM_TYPE(PT_BINARY); /** Default constructor */ - COnlineLibLinear(); + OnlineLibLinear(); /** * Constructor * * @param C Cost constant C */ - COnlineLibLinear(float64_t C); + OnlineLibLinear(float64_t C); /** * Constructor @@ -54,16 +54,16 @@ class COnlineLibLinear : public COnlineLinearMachine * @param C Cost constant C * @param traindat Training examples */ - COnlineLibLinear(float64_t C, CStreamingDotFeatures* traindat); + OnlineLibLinear(float64_t C, std::shared_ptr traindat); /** * Copy Constructor - * @param mch another COnlineLibLinear machine + * @param mch another OnlineLibLinear machine */ - COnlineLibLinear(COnlineLibLinear *mch); + OnlineLibLinear(std::shared_ptrmch); /** Destructor */ - virtual ~COnlineLibLinear(); + virtual ~OnlineLibLinear(); /** * Set C1 and C2 constants @@ -119,7 +119,7 @@ class COnlineLibLinear : public COnlineLinearMachine * labels or the caller might want to provide some other labels. * @param label label of this example */ - virtual void train_example(CStreamingDotFeatures *feature, float64_t label); + virtual void train_example(std::shared_ptrfeature, float64_t label); private: /** Set up parameters */ diff --git a/src/shogun/classifier/svm/OnlineSVMSGD.cpp b/src/shogun/classifier/svm/OnlineSVMSGD.cpp index a98400d0111..202c07ef6af 100644 --- a/src/shogun/classifier/svm/OnlineSVMSGD.cpp +++ b/src/shogun/classifier/svm/OnlineSVMSGD.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Shashwat Lal Das, Soeren Sonnenburg, Giovanni De Toni, Sanuj Sharma, + * Authors: Shashwat Lal Das, Soeren Sonnenburg, Giovanni De Toni, Sanuj Sharma, * Thoralf Klein, Viktor Gal, Evan Shelhamer, Bjoern Esser */ @@ -15,14 +15,14 @@ using namespace shogun; -COnlineSVMSGD::COnlineSVMSGD() -: COnlineLinearMachine() +OnlineSVMSGD::OnlineSVMSGD() +: OnlineLinearMachine() { init(); } -COnlineSVMSGD::COnlineSVMSGD(float64_t C) -: COnlineLinearMachine() +OnlineSVMSGD::OnlineSVMSGD(float64_t C) +: OnlineLinearMachine() { init(); @@ -30,8 +30,8 @@ COnlineSVMSGD::COnlineSVMSGD(float64_t C) C2=C; } -COnlineSVMSGD::COnlineSVMSGD(float64_t C, CStreamingDotFeatures* traindat) -: COnlineLinearMachine() +OnlineSVMSGD::OnlineSVMSGD(float64_t C, std::shared_ptr traindat) +: OnlineLinearMachine() { init(); C1=C; @@ -40,25 +40,25 @@ COnlineSVMSGD::COnlineSVMSGD(float64_t C, CStreamingDotFeatures* traindat) set_features(traindat); } -COnlineSVMSGD::~COnlineSVMSGD() +OnlineSVMSGD::~OnlineSVMSGD() { - SG_UNREF(loss); + } -void COnlineSVMSGD::set_loss_function(CLossFunction* loss_func) +void OnlineSVMSGD::set_loss_function(std::shared_ptr loss_func) { - SG_REF(loss_func); - SG_UNREF(loss); + + loss=loss_func; } -bool COnlineSVMSGD::train(CFeatures* data) +bool OnlineSVMSGD::train(std::shared_ptr data) { if (data) { if (!data->has_property(FP_STREAMING_DOT)) error("Specified features are not of type CStreamingDotFeatures"); - set_features((CStreamingDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } features->start_parser(); @@ -74,7 +74,7 @@ bool COnlineSVMSGD::train(CFeatures* data) // This assumes |x| \approx 1. float64_t maxw = 1.0 / sqrt(lambda); float64_t typw = sqrt(maxw); - float64_t eta0 = typw / CMath::max(1.0,-loss->first_derivative(-typw,1)); + float64_t eta0 = typw / Math::max(1.0,-loss->first_derivative(-typw,1)); t = 1 / (eta0 * lambda); io::info("lambda={}, epochs={}, eta0={}", lambda, epochs, eta0); @@ -147,7 +147,7 @@ bool COnlineSVMSGD::train(CFeatures* data) return true; } -void COnlineSVMSGD::calibrate(int32_t max_vec_num) +void OnlineSVMSGD::calibrate(int32_t max_vec_num) { int32_t c_dim=1; float32_t* c=SG_CALLOC(float32_t, c_dim); @@ -167,7 +167,7 @@ void COnlineSVMSGD::calibrate(int32_t max_vec_num) //waste cpu cycles for readability //(only changed dims need checking) - m=CMath::max(c, c_dim); + m=Math::max(c, c_dim); n++; features->release_example(); @@ -188,7 +188,7 @@ void COnlineSVMSGD::calibrate(int32_t max_vec_num) SG_FREE(c); } -void COnlineSVMSGD::init() +void OnlineSVMSGD::init() { t=1; C1=1; @@ -203,8 +203,8 @@ void COnlineSVMSGD::init() use_regularized_bias=false; - loss=new CHingeLoss(); - SG_REF(loss); + loss=std::make_shared(); + SG_ADD(&C1, "C1", "Cost constant 1.", ParameterProperties::HYPER); SG_ADD(&C2, "C2", "Cost constant 2.", ParameterProperties::HYPER); diff --git a/src/shogun/classifier/svm/OnlineSVMSGD.h b/src/shogun/classifier/svm/OnlineSVMSGD.h index 1f7616c9f45..28bc4156b40 100644 --- a/src/shogun/classifier/svm/OnlineSVMSGD.h +++ b/src/shogun/classifier/svm/OnlineSVMSGD.h @@ -18,29 +18,29 @@ namespace shogun { /** @brief class OnlineSVMSGD */ -class COnlineSVMSGD : public COnlineLinearMachine +class OnlineSVMSGD : public OnlineLinearMachine { public: /** returns type of problem machine solves */ MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - COnlineSVMSGD(); + OnlineSVMSGD(); /** constructor * * @param C constant C */ - COnlineSVMSGD(float64_t C); + OnlineSVMSGD(float64_t C); /** constructor * * @param C constant C * @param traindat training features */ - COnlineSVMSGD(float64_t C, CStreamingDotFeatures* traindat); + OnlineSVMSGD(float64_t C, std::shared_ptr traindat); - virtual ~COnlineSVMSGD(); + virtual ~OnlineSVMSGD(); /** get classifier type * @@ -56,7 +56,7 @@ class COnlineSVMSGD : public COnlineLinearMachine * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** set C * @@ -130,13 +130,13 @@ class COnlineSVMSGD : public COnlineLinearMachine * * @param loss_func object derived from CLossFunction */ - void set_loss_function(CLossFunction* loss_func); + void set_loss_function(std::shared_ptr loss_func); /** Return the loss function * * @return loss function as CLossFunction* */ - inline CLossFunction* get_loss_function() { SG_REF(loss); return loss; } + inline std::shared_ptr get_loss_function() { return loss; } /** @return object name */ inline const char* get_name() const { return "OnlineSVMSGD"; } @@ -166,7 +166,7 @@ class COnlineSVMSGD : public COnlineLinearMachine bool use_bias; bool use_regularized_bias; - CLossFunction* loss; + std::shared_ptr loss; }; } #endif diff --git a/src/shogun/classifier/svm/SGDQN.cpp b/src/shogun/classifier/svm/SGDQN.cpp index d8590ebe46e..f7e0674ddc7 100644 --- a/src/shogun/classifier/svm/SGDQN.cpp +++ b/src/shogun/classifier/svm/SGDQN.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Shashwat Lal Das, Giovanni De Toni, + * Authors: Soeren Sonnenburg, Shashwat Lal Das, Giovanni De Toni, * Sergey Lisitsyn, Thoralf Klein, Evan Shelhamer, Bjoern Esser */ @@ -15,14 +15,14 @@ using namespace shogun; -CSGDQN::CSGDQN() -: CLinearMachine() +SGDQN::SGDQN() +: LinearMachine() { init(); } -CSGDQN::CSGDQN(float64_t C) -: CLinearMachine() +SGDQN::SGDQN(float64_t C) +: LinearMachine() { init(); @@ -30,8 +30,8 @@ CSGDQN::CSGDQN(float64_t C) C2=C; } -CSGDQN::CSGDQN(float64_t C, CDotFeatures* traindat, CLabels* trainlab) -: CLinearMachine() +SGDQN::SGDQN(float64_t C, std::shared_ptr traindat, std::shared_ptr trainlab) +: LinearMachine() { init(); C1=C; @@ -41,19 +41,19 @@ CSGDQN::CSGDQN(float64_t C, CDotFeatures* traindat, CLabels* trainlab) set_labels(trainlab); } -CSGDQN::~CSGDQN() +SGDQN::~SGDQN() { - SG_UNREF(loss); + } -void CSGDQN::set_loss_function(CLossFunction* loss_func) +void SGDQN::set_loss_function(std::shared_ptr loss_func) { - SG_REF(loss_func); - SG_UNREF(loss); + + loss=loss_func; } -void CSGDQN::compute_ratio(float64_t* W,float64_t* W_1,float64_t* B,float64_t* dst,int32_t dim,float64_t lambda,float64_t loss_val) +void SGDQN::compute_ratio(float64_t* W,float64_t* W_1,float64_t* B,float64_t* dst,int32_t dim,float64_t lambda,float64_t loss_val) { for (int32_t i=0; i < dim;i++) { @@ -65,19 +65,19 @@ void CSGDQN::compute_ratio(float64_t* W,float64_t* W_1,float64_t* B,float64_t* d } } -void CSGDQN::combine_and_clip(float64_t* Bc,float64_t* B,int32_t dim,float64_t c1,float64_t c2,float64_t v1,float64_t v2) +void SGDQN::combine_and_clip(float64_t* Bc,float64_t* B,int32_t dim,float64_t c1,float64_t c2,float64_t v1,float64_t v2) { for (int32_t i=0; i < dim;i++) { if(B[i]) { Bc[i] = Bc[i] * c1 + B[i] * c2; - Bc[i]= CMath::min(CMath::max(Bc[i],v1),v2); + Bc[i]= Math::min(Math::max(Bc[i],v1),v2); } } } -bool CSGDQN::train(CFeatures* data) +bool SGDQN::train(std::shared_ptr data) { ASSERT(m_labels) @@ -87,7 +87,7 @@ bool CSGDQN::train(CFeatures* data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } ASSERT(features) @@ -108,7 +108,7 @@ bool CSGDQN::train(CFeatures* data) // This assumes |x| \approx 1. float64_t maxw = 1.0 / sqrt(lambda); float64_t typw = sqrt(maxw); - float64_t eta0 = typw / CMath::max(1.0,-loss->first_derivative(-typw,1)); + float64_t eta0 = typw / Math::max(1.0,-loss->first_derivative(-typw,1)); t = 1 / (eta0 * lambda); io::info("lambda={}, epochs={}, eta0={}", lambda, epochs, eta0); @@ -130,6 +130,7 @@ bool CSGDQN::train(CFeatures* data) if ((loss_type == L_LOGLOSS) || (loss_type == L_LOGLOSSMARGIN)) is_log_loss = true; + auto binary_labels = std::static_pointer_cast(m_labels); for (auto e : SG_PROGRESS(range(epochs))) { COMPUTATION_CONTROLLERS @@ -140,7 +141,7 @@ bool CSGDQN::train(CFeatures* data) SGVector v = features->get_computed_dot_feature_vector(i); ASSERT(w.vlen==v.vlen) float64_t eta = 1.0/t; - float64_t y = ((CBinaryLabels*) m_labels)->get_label(i); + float64_t y = binary_labels->get_label(i); float64_t z = y * features->dot(i, w); if(updateB==true) { @@ -192,7 +193,7 @@ bool CSGDQN::train(CFeatures* data) -void CSGDQN::calibrate() +void SGDQN::calibrate() { ASSERT(features) int32_t num_vec=features->get_num_vectors(); @@ -214,7 +215,7 @@ void CSGDQN::calibrate() skip = (int32_t) ((16 * n * c_dim) / r); } -void CSGDQN::init() +void SGDQN::init() { t=0; C1=1; @@ -223,8 +224,8 @@ void CSGDQN::init() skip=1000; count=1000; - loss=new CHingeLoss(); - SG_REF(loss); + loss=std::make_shared(); + SG_ADD(&C1, "C1", "Cost constant 1.", ParameterProperties::HYPER); SG_ADD(&C2, "C2", "Cost constant 2.", ParameterProperties::HYPER); diff --git a/src/shogun/classifier/svm/SGDQN.h b/src/shogun/classifier/svm/SGDQN.h index 4cfd546b8fa..7b8bae59a05 100644 --- a/src/shogun/classifier/svm/SGDQN.h +++ b/src/shogun/classifier/svm/SGDQN.h @@ -19,7 +19,7 @@ namespace shogun { /** @brief class SGDQN */ -class CSGDQN : public CLinearMachine +class SGDQN : public LinearMachine { public: @@ -27,13 +27,13 @@ class CSGDQN : public CLinearMachine MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - CSGDQN(); + SGDQN(); /** constructor * * @param C constant C */ - CSGDQN(float64_t C); + SGDQN(float64_t C); /** constructor * @@ -41,11 +41,11 @@ class CSGDQN : public CLinearMachine * @param traindat training features * @param trainlab labels for training features */ - CSGDQN( - float64_t C, CDotFeatures* traindat, - CLabels* trainlab); + SGDQN( + float64_t C, std::shared_ptr traindat, + std::shared_ptr trainlab); - virtual ~CSGDQN(); + virtual ~SGDQN(); /** get classifier type * @@ -61,7 +61,7 @@ class CSGDQN : public CLinearMachine * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** set C * @@ -105,13 +105,13 @@ class CSGDQN : public CLinearMachine * * @param loss_func object derived from CLossFunction */ - void set_loss_function(CLossFunction* loss_func); + void set_loss_function(std::shared_ptr loss_func); /** Return the loss function * * @return loss function as CLossFunction* */ - inline CLossFunction* get_loss_function() { SG_REF(loss); return loss; } + inline std::shared_ptr get_loss_function() { return loss; } /** @return object name */ virtual const char* get_name() const { return "SGDQN"; } @@ -131,7 +131,7 @@ class CSGDQN : public CLinearMachine int32_t skip; int32_t count; - CLossFunction* loss; + std::shared_ptr loss; }; } #endif diff --git a/src/shogun/classifier/svm/SVM.cpp b/src/shogun/classifier/svm/SVM.cpp index 977dd110eff..94bd5628aad 100644 --- a/src/shogun/classifier/svm/SVM.cpp +++ b/src/shogun/classifier/svm/SVM.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, Weijie Lin, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, Weijie Lin, * Evgeniy Andreev, Viktor Gal, Evan Shelhamer, Thoralf Klein */ @@ -18,14 +18,14 @@ using namespace shogun; -CSVM::CSVM(int32_t num_sv) -: CKernelMachine() +SVM::SVM(int32_t num_sv) +: KernelMachine() { set_defaults(num_sv); } -CSVM::CSVM(float64_t C, CKernel* k, CLabels* lab) -: CKernelMachine() +SVM::SVM(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: KernelMachine() { set_defaults(); set_C(C,C); @@ -33,12 +33,12 @@ CSVM::CSVM(float64_t C, CKernel* k, CLabels* lab) set_kernel(k); } -CSVM::~CSVM() +SVM::~SVM() { - SG_UNREF(mkl); + } -void CSVM::set_defaults(int32_t num_sv) +void SVM::set_defaults(int32_t num_sv) { SG_ADD(&C1, "C1", "", ParameterProperties::HYPER); SG_ADD(&C2, "C2", "", ParameterProperties::HYPER); @@ -50,7 +50,7 @@ void CSVM::set_defaults(int32_t num_sv) SG_ADD(&objective, "objective", "", ParameterProperties::HYPER); SG_ADD(&qpsize, "qpsize", "", ParameterProperties::HYPER); SG_ADD(&use_shrinking, "use_shrinking", "Shrinking shall be used.", ParameterProperties::SETTING); - SG_ADD((CSGObject**) &mkl, "mkl", "MKL object that svm optimizers need."); + SG_ADD((std::shared_ptr*) &mkl, "mkl", "MKL object that svm optimizers need."); SG_ADD(&m_linear_term, "linear_term", "Linear term in qp.", ParameterProperties::MODEL); callback=NULL; @@ -76,7 +76,7 @@ void CSVM::set_defaults(int32_t num_sv) create_new_model(num_sv); } -bool CSVM::load(FILE* modelfl) +bool SVM::load(FILE* modelfl) { bool result=true; char char_buffer[1024]; @@ -192,7 +192,7 @@ bool CSVM::load(FILE* modelfl) return result; } -bool CSVM::save(FILE* modelfl) +bool SVM::save(FILE* modelfl) { SG_SET_LOCALE_C; @@ -209,7 +209,7 @@ bool CSVM::save(FILE* modelfl) for(int32_t i=0; i m, bool (*cb) + (std::shared_ptr mkl, const float64_t* sumw, const float64_t suma)) { - SG_REF(m); - SG_UNREF(mkl); + + mkl=m; callback=cb; } -float64_t CSVM::compute_svm_dual_objective() +float64_t SVM::compute_svm_dual_objective() { int32_t n=get_num_support_vectors(); if (m_labels && kernel) { + auto binary_labels = std::static_pointer_cast(m_labels); objective=0; for (int32_t i=0; iget_label(ii); + objective-=get_alpha(i)*(binary_labels->get_label(ii)); for (int32_t j=0; j(m_labels); if(C2>0) { C2_tmp=get_C2(); @@ -278,7 +280,7 @@ float64_t CSVM::compute_svm_primal_objective() regularizer-=0.5*get_alpha(i)*get_alpha(j)*kernel->kernel(ii,jj); } - loss-=(C1*(-((CBinaryLabels*) m_labels)->get_label(ii)+1)/2.0 + C2_tmp*(((CBinaryLabels*) m_labels)->get_label(ii)+1)/2.0 )*CMath::max(0.0, 1.0-((CBinaryLabels*) m_labels)->get_label(ii)*apply_one(ii)); + loss-=(C1*(-(binary_labels->get_label(ii)+1)/2.0 + C2_tmp*(binary_labels->get_label(ii)+1)/2.0 )*Math::max(0.0, 1.0-binary_labels->get_label(ii)*apply_one(ii))); } } @@ -288,7 +290,7 @@ float64_t CSVM::compute_svm_primal_objective() return regularizer+loss; } -float64_t* CSVM::get_linear_term_array() +float64_t* SVM::get_linear_term_array() { if (m_linear_term.vlen==0) return NULL; @@ -300,7 +302,7 @@ float64_t* CSVM::get_linear_term_array() return a; } -void CSVM::set_linear_term(const SGVector& linear_term) +void SVM::set_linear_term(const SGVector& linear_term) { ASSERT(linear_term.vector) @@ -318,7 +320,7 @@ void CSVM::set_linear_term(const SGVector& linear_term) m_linear_term=linear_term; } -SGVector CSVM::get_linear_term() +SGVector SVM::get_linear_term() { return m_linear_term; } diff --git a/src/shogun/classifier/svm/SVM.h b/src/shogun/classifier/svm/SVM.h index b7cb1db5091..ad07a3a7233 100644 --- a/src/shogun/classifier/svm/SVM.h +++ b/src/shogun/classifier/svm/SVM.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Chiyuan Zhang, Heiko Strathmann, Evgeniy Andreev, + * Authors: Soeren Sonnenburg, Chiyuan Zhang, Heiko Strathmann, Evgeniy Andreev, * Weijie Lin, Fernando Iglesias, Bjoern Esser, Sergey Lisitsyn */ @@ -18,8 +18,8 @@ namespace shogun { -class CMKL; -class CMulticlassSVM; +class MKL; +class MulticlassSVM; /** @brief A generic Support Vector Machine Interface. * @@ -43,7 +43,7 @@ class CMulticlassSVM; * \f} * here C is a pre-specified regularization parameter. */ -class CSVM : public CKernelMachine +class SVM : public KernelMachine { public: @@ -53,7 +53,7 @@ class CSVM : public CKernelMachine /** Create an empty Support Vector Machine Object * @param num_sv with num_sv support vectors */ - CSVM(int32_t num_sv=0); + SVM(int32_t num_sv=0); /** Create a Support Vector Machine Object from a * trained SVM @@ -62,9 +62,9 @@ class CSVM : public CKernelMachine * @param k the Kernel object * @param lab the Label object */ - CSVM(float64_t C, CKernel* k, CLabels* lab); + SVM(float64_t C, std::shared_ptr k, std::shared_ptr lab); - virtual ~CSVM(); + virtual ~SVM(); /** set default values for members a SVM object */ @@ -110,7 +110,7 @@ class CSVM : public CKernelMachine * @param c_pos new C constant for positively labeled examples * * Note that not all SVMs support this (however at least CLibSVM and - * CSVMLight do) + * SVMLight do) */ inline void set_C(float64_t c_neg, float64_t c_pos) { C1=c_neg; C2=c_pos; } @@ -230,7 +230,7 @@ class CSVM : public CKernelMachine inline void set_loaded_status(bool loaded) { svm_loaded = loaded; - }; + }; /** set callback function svm optimizers may call when they have a new * (small) set of alphas @@ -239,8 +239,8 @@ class CSVM : public CKernelMachine * @param cb callback function * * */ - void set_callback_function(CMKL* m, bool (*cb) - (CMKL* mkl, const float64_t* sumw, const float64_t suma)); + void set_callback_function(std::shared_ptr m, bool (*cb) + (std::shared_ptr mkl, const float64_t* sumw, const float64_t suma)); /** @return object name */ virtual const char* get_name() const { return "SVM"; } @@ -278,12 +278,12 @@ class CSVM : public CKernelMachine /** callback function svm optimizers may call when they have a new * (small) set of alphas */ - bool (*callback) (CMKL* mkl, const float64_t* sumw, const float64_t suma); + bool (*callback) (std::shared_ptr mkl, const float64_t* sumw, const float64_t suma); /** mkl object that svm optimizers need to pass when calling the callback * function */ - CMKL* mkl; + std::shared_ptr mkl; - friend class CMulticlassSVM; + friend class MulticlassSVM; }; } #endif diff --git a/src/shogun/classifier/svm/SVMLight.cpp b/src/shogun/classifier/svm/SVMLight.cpp index 0a2fbbcfc15..bd51cfbb196 100644 --- a/src/shogun/classifier/svm/SVMLight.cpp +++ b/src/shogun/classifier/svm/SVMLight.cpp @@ -61,7 +61,7 @@ using namespace shogun; #ifndef DOXYGEN_SHOULD_SKIP_THIS struct S_THREAD_PARAM_REACTIVATE_LINADD { - CKernel* kernel; + std::shared_ptr kernel; float64_t* lin; float64_t* last_lin; int32_t* active; @@ -77,12 +77,12 @@ struct S_THREAD_PARAM_SVMLIGHT int32_t start, end; int32_t * active2dnum ; int32_t * docs ; - CKernel* kernel ; + std::shared_ptr kernel ; }; struct S_THREAD_PARAM_REACTIVATE_VANILLA { - CKernel* kernel; + std::shared_ptr kernel; float64_t* lin; float64_t* aicache; float64_t* a; @@ -99,12 +99,12 @@ struct S_THREAD_PARAM_KERNEL float64_t *Kval ; int32_t *KI, *KJ ; int32_t start, end; - CSVMLight* svmlight; + std::shared_ptr svmlight; }; #endif // DOXYGEN_SHOULD_SKIP_THIS -void* CSVMLight::update_linear_component_linadd_helper(void* p) +void* SVMLight::update_linear_component_linadd_helper(void* p) { S_THREAD_PARAM_SVMLIGHT* params = (S_THREAD_PARAM_SVMLIGHT*) p; @@ -116,7 +116,7 @@ void* CSVMLight::update_linear_component_linadd_helper(void* p) return NULL ; } -void* CSVMLight::compute_kernel_helper(void* p) +void* SVMLight::compute_kernel_helper(void* p) { S_THREAD_PARAM_KERNEL* params = (S_THREAD_PARAM_KERNEL*) p; @@ -127,20 +127,20 @@ void* CSVMLight::compute_kernel_helper(void* p) return NULL ; } -CSVMLight::CSVMLight() -: CSVM() +SVMLight::SVMLight() +: SVM() { init(); set_kernel(NULL); } -CSVMLight::CSVMLight(float64_t C, CKernel* k, CLabels* lab) -: CSVM(C, k, lab) +SVMLight::SVMLight(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: SVM(C, k, lab) { init(); } -void CSVMLight::init() +void SVMLight::init() { //optimizer settings primal=NULL; @@ -165,7 +165,7 @@ void CSVMLight::init() mkl_converged=false; } -CSVMLight::~CSVMLight() +SVMLight::~SVMLight() { SG_FREE(model->supvec); @@ -182,7 +182,7 @@ CSVMLight::~CSVMLight() SG_FREE(primal); } -bool CSVMLight::train_machine(CFeatures* data) +bool SVMLight::train_machine(std::shared_ptr data) { //certain setup params mkl_converged=false; @@ -257,12 +257,12 @@ bool CSVMLight::train_machine(CFeatures* data) if (kernel->get_kernel_type() == K_COMBINED) { - for (index_t k_idx=0; k_idx<((CCombinedKernel*) kernel)->get_num_kernels(); k_idx++) + for (index_t k_idx=0; k_idx<(std::static_pointer_cast(kernel))->get_num_kernels(); k_idx++) { - CKernel* kn = ((CCombinedKernel*) kernel)->get_kernel(k_idx); + auto kn = (std::static_pointer_cast(kernel))->get_kernel(k_idx); // allocate kernel cache but clean up beforehand kn->resize_kernel_cache(kn->get_cache_size()); - SG_UNREF(kn); + } } @@ -293,7 +293,7 @@ bool CSVMLight::train_machine(CFeatures* data) return true ; } -int32_t CSVMLight::get_runtime() +int32_t SVMLight::get_runtime() { clock_t start; start = clock(); @@ -301,7 +301,7 @@ int32_t CSVMLight::get_runtime() } -void CSVMLight::svm_learn() +void SVMLight::svm_learn() { int32_t *inconsistent, i; int32_t misclassified,upsupvecnum; @@ -441,13 +441,13 @@ void CSVMLight::svm_learn() if (use_kernel_cache) { if (callback && - (!((CCombinedKernel*) kernel)->get_append_subkernel_weights()) + (!(std::static_pointer_cast(kernel))->get_append_subkernel_weights()) ) { - CCombinedKernel* k = (CCombinedKernel*) kernel; + auto k = std::static_pointer_cast(kernel); for (index_t k_idx=0; k_idxget_num_kernels(); k_idx++) { - CKernel* kn = k->get_kernel(k_idx); + auto kn = k->get_kernel(k_idx); for (i=0;i0) && (alpha[i]svm_cost[i]) && (kn->kernel_cache_space_available())) @@ -458,7 +458,7 @@ void CSVMLight::svm_learn() && (kn->kernel_cache_space_available())) kn->cache_kernel_row(i); - SG_UNREF(kn); + } } else @@ -547,7 +547,7 @@ void CSVMLight::svm_learn() SG_FREE(docs); } -int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_t totdoc, +int32_t SVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_t totdoc, SHRINK_STATE *shrink_state, int32_t *inconsistent, float64_t *a, float64_t *lin, float64_t *c, @@ -639,7 +639,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ clear_index(working2dnum); /* repeat this loop until we have convergence */ - CTime start_time; + Time start_time; mkl_converged=false; auto pb = SG_PROGRESS(range(10)); @@ -667,7 +667,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ i=0; for (jj=0;(j=working2dnum[jj])>=0;jj++) { /* clear working set */ if((chosen[j]>=(learn_parm->svm_maxqpsize/ - CMath::min(learn_parm->svm_maxqpsize, + Math::min(learn_parm->svm_maxqpsize, learn_parm->svm_newvarsinqp))) || (inconsistent[j]) || (j == heldout)) { @@ -721,13 +721,13 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ if(iteration % 101) { already_chosen=0; - if(CMath::min(learn_parm->svm_newvarsinqp, learn_parm->svm_maxqpsize-choosenum)>=4 && + if(Math::min(learn_parm->svm_newvarsinqp, learn_parm->svm_maxqpsize-choosenum)>=4 && (!(kernel->has_property(KP_LINADD) && get_linadd_enabled()))) { /* select part of the working set from cache */ already_chosen=select_next_qp_subproblem_grad( label,a,lin,c,totdoc, - (int32_t)(CMath::min(learn_parm->svm_maxqpsize-choosenum, + (int32_t)(Math::min(learn_parm->svm_maxqpsize-choosenum, learn_parm->svm_newvarsinqp)/2), inconsistent,active2dnum, working2dnum,selcrit,selexam,1, @@ -736,7 +736,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ } choosenum+=select_next_qp_subproblem_grad( label,a,lin,c,totdoc, - CMath::min(learn_parm->svm_maxqpsize-choosenum, + Math::min(learn_parm->svm_maxqpsize-choosenum, learn_parm->svm_newvarsinqp-already_chosen), inconsistent,active2dnum, working2dnum,selcrit,selexam,0,key, @@ -747,7 +747,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ inaccuracies in the core qp-solver */ choosenum+=select_next_qp_subproblem_rand( label,a,lin,c,totdoc, - CMath::min(learn_parm->svm_maxqpsize-choosenum, + Math::min(learn_parm->svm_maxqpsize-choosenum, learn_parm->svm_newvarsinqp), inconsistent,active2dnum, working2dnum,selcrit,selexam,key, @@ -766,15 +766,15 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ // in case of MKL w/o linadd cache each kernel independently // else if linadd is disabled cache single kernel if ( callback && - (!((CCombinedKernel*) kernel)->get_append_subkernel_weights()) + (!(std::static_pointer_cast(kernel))->get_append_subkernel_weights()) ) { - CCombinedKernel* k = (CCombinedKernel*) kernel; + auto k = std::static_pointer_cast(kernel); for (index_t k_idx=0; k_idxget_num_kernels(); k_idx++) { - CKernel* kn = k->get_kernel(k_idx); + auto kn = k->get_kernel(k_idx); kn->cache_multiple_kernel_rows(working2dnum, choosenum); - SG_UNREF(kn); + } } else @@ -888,16 +888,16 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ if (((iteration % 10) == 0) && (!noshrink) && !callback) { activenum=shrink_problem(shrink_state,active2dnum,last_suboptimal_at,iteration,totdoc, - CMath::max((int32_t)(activenum/10), - CMath::max((int32_t)(totdoc/500),(int32_t) 100)), + Math::max((int32_t)(activenum/10), + Math::max((int32_t)(totdoc/500),(int32_t) 100)), a,inconsistent, c, lin, label); inactivenum=totdoc-activenum; if (use_kernel_cache && (supvecnum>kernel->get_max_elems_cache()) - && ((kernel->get_activenum_cache()-activenum)>CMath::max((int32_t)(activenum/10),(int32_t) 500))) { + && ((kernel->get_activenum_cache()-activenum)>Math::max((int32_t)(activenum/10),(int32_t) 500))) { - kernel->kernel_cache_shrink(totdoc, CMath::min((int32_t) (kernel->get_activenum_cache()-activenum), + kernel->kernel_cache_shrink(totdoc, Math::min((int32_t) (kernel->get_activenum_cache()-activenum), (int32_t) (kernel->get_activenum_cache()-supvecnum)), shrink_state->active); } @@ -907,8 +907,8 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ worstmaxdiff=bestmaxdiff; pb.print_absolute( - bestmaxdiff, -CMath::log10(bestmaxdiff), -CMath::log10(worstmaxdiff), - -CMath::log10(epsilon)); + bestmaxdiff, -Math::log10(bestmaxdiff), -Math::log10(worstmaxdiff), + -Math::log10(epsilon)); /* Terminate loop */ if (m_max_train_time > 0 && start_time.cur_time_diff() > m_max_train_time) { @@ -939,7 +939,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ //use this for our purposes! criterion=compute_objective_function(a,lin,c,learn_parm->eps,label,totdoc); - CSVM::set_objective(criterion); + SVM::set_objective(criterion); SG_FREE(chosen); SG_FREE(last_suboptimal_at); @@ -963,7 +963,7 @@ int32_t CSVMLight::optimize_to_convergence(int32_t* docs, int32_t* label, int32_ return(iteration); } -float64_t CSVMLight::compute_objective_function( +float64_t SVMLight::compute_objective_function( float64_t *a, float64_t *lin, float64_t *c, float64_t* eps, int32_t *label, int32_t totdoc) /* Return value of objective function. */ @@ -979,13 +979,13 @@ float64_t CSVMLight::compute_objective_function( } -void CSVMLight::clear_index(int32_t *index) +void SVMLight::clear_index(int32_t *index) /* initializes and empties index */ { index[0]=-1; } -void CSVMLight::add_to_index(int32_t *index, int32_t elem) +void SVMLight::add_to_index(int32_t *index, int32_t elem) /* initializes and empties index */ { int32_t i; @@ -994,7 +994,7 @@ void CSVMLight::add_to_index(int32_t *index, int32_t elem) index[i+1]=-1; } -int32_t CSVMLight::compute_index( +int32_t SVMLight::compute_index( int32_t *binfeature, int32_t range, int32_t *index) /* create an inverted index of binfeature */ { @@ -1014,7 +1014,7 @@ int32_t CSVMLight::compute_index( } -void CSVMLight::optimize_svm( +void SVMLight::optimize_svm( int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const, float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t totdoc, int32_t *working2dnum, int32_t varnum, float64_t *a, float64_t *lin, @@ -1051,7 +1051,7 @@ void CSVMLight::optimize_svm( a[working2dnum[i]]=a_v[i]; } -void CSVMLight::compute_matrices_for_optimization_parallel( +void SVMLight::compute_matrices_for_optimization_parallel( int32_t* docs, int32_t* label, int32_t *exclude_from_eq_const, float64_t eq_target, int32_t *chosen, int32_t *active2dnum, int32_t *key, float64_t *a, float64_t *lin, float64_t *c, int32_t varnum, int32_t totdoc, @@ -1119,13 +1119,13 @@ void CSVMLight::compute_matrices_for_optimization_parallel( //SG_DEBUG("\nkernel-step size: {}", step) for (int32_t t=0; t(this); params[t].start = t*step; params[t].end = (t+1)*step; params[t].KI=KI ; params[t].KJ=KJ ; params[t].Kval=Kval ; - pthread_create(&threads[t], NULL, CSVMLight::compute_kernel_helper, (void*)¶ms[t]); + pthread_create(&threads[t], NULL, SVMLight::compute_kernel_helper, (void*)¶ms[t]); } for (i=params[num_threads-2].end; isv_num-1); /* have to substract one, since element 0 is empty*/ } -int32_t CSVMLight::check_optimality( +int32_t SVMLight::check_optimality( int32_t* label, float64_t *a, float64_t *lin, float64_t *c, int32_t totdoc, float64_t *maxdiff, float64_t epsilon_crit_org, int32_t *misclassified, int32_t *inconsistent, int32_t *active2dnum, int32_t *last_suboptimal_at, @@ -1440,7 +1440,7 @@ int32_t CSVMLight::check_optimality( return(retrain); } -void CSVMLight::update_linear_component( +void SVMLight::update_linear_component( int32_t* docs, int32_t* label, int32_t *active2dnum, float64_t *a, float64_t *a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin, float64_t *aicache, float64_t* c) @@ -1543,7 +1543,7 @@ void CSVMLight::update_linear_component( } -void CSVMLight::update_linear_component_mkl( +void SVMLight::update_linear_component_mkl( int32_t* docs, int32_t* label, int32_t *active2dnum, float64_t *a, float64_t *a_old, int32_t *working2dnum, int32_t totdoc, float64_t *lin, float64_t *aicache) @@ -1556,15 +1556,15 @@ void CSVMLight::update_linear_component_mkl( ASSERT(num_weights==num_kernels) if ((kernel->get_kernel_type()==K_COMBINED) && - (!((CCombinedKernel*) kernel)->get_append_subkernel_weights()))// for combined kernel + (!(std::static_pointer_cast(kernel))->get_append_subkernel_weights()))// for combined kernel { - CCombinedKernel* k = (CCombinedKernel*) kernel; + auto k = std::static_pointer_cast(kernel); int32_t n = 0, i, j ; for (index_t k_idx=0; k_idxget_num_kernels(); k_idx++) { - CKernel* kn = k->get_kernel(k_idx); + auto kn = k->get_kernel(k_idx); for (i=0;iget_num_vec_rhs(); int32_t num_kernels = kernel->get_num_subkernels() ; @@ -1771,7 +1771,7 @@ void CSVMLight::call_mkl_callback(float64_t* a, int32_t* label, float64_t* lin) /*************************** Working set selection ***************************/ -int32_t CSVMLight::select_next_qp_subproblem_grad( +int32_t SVMLight::select_next_qp_subproblem_grad( int32_t* label, float64_t *a, float64_t *lin, float64_t *c, int32_t totdoc, int32_t qp_size, int32_t *inconsistent, int32_t *active2dnum, int32_t *working2dnum, float64_t *selcrit, int32_t *select, @@ -1863,7 +1863,7 @@ int32_t CSVMLight::select_next_qp_subproblem_grad( return(choosenum); } -int32_t CSVMLight::select_next_qp_subproblem_rand( +int32_t SVMLight::select_next_qp_subproblem_rand( int32_t* label, float64_t *a, float64_t *lin, float64_t *c, int32_t totdoc, int32_t qp_size, int32_t *inconsistent, int32_t *active2dnum, int32_t *working2dnum, float64_t *selcrit, int32_t *select, int32_t *key, @@ -1933,7 +1933,7 @@ int32_t CSVMLight::select_next_qp_subproblem_rand( -void CSVMLight::select_top_n( +void SVMLight::select_top_n( float64_t *selcrit, int32_t range, int32_t* select, int32_t n) { int32_t i,j; @@ -1969,7 +1969,7 @@ void CSVMLight::select_top_n( /******************************** Shrinking *********************************/ -void CSVMLight::init_shrink_state( +void SVMLight::init_shrink_state( SHRINK_STATE *shrink_state, int32_t totdoc, int32_t maxhistory) { int32_t i; @@ -1990,7 +1990,7 @@ void CSVMLight::init_shrink_state( } } -void CSVMLight::shrink_state_cleanup(SHRINK_STATE *shrink_state) +void SVMLight::shrink_state_cleanup(SHRINK_STATE *shrink_state) { SG_FREE(shrink_state->active); SG_FREE(shrink_state->inactive_since); @@ -2001,7 +2001,7 @@ void CSVMLight::shrink_state_cleanup(SHRINK_STATE *shrink_state) SG_FREE((shrink_state->last_lin)); } -int32_t CSVMLight::shrink_problem( +int32_t SVMLight::shrink_problem( SHRINK_STATE *shrink_state, int32_t *active2dnum, int32_t *last_suboptimal_at, int32_t iteration, int32_t totdoc, int32_t minshrink, float64_t *a, int32_t *inconsistent, float64_t* c, @@ -2062,11 +2062,11 @@ int32_t CSVMLight::shrink_problem( return(activenum); } -void* CSVMLight::reactivate_inactive_examples_linadd_helper(void* p) +void* SVMLight::reactivate_inactive_examples_linadd_helper(void* p) { S_THREAD_PARAM_REACTIVATE_LINADD* params = (S_THREAD_PARAM_REACTIVATE_LINADD*) p; - CKernel* k = params->kernel; + auto k = params->kernel; float64_t* lin = params->lin; float64_t* last_lin = params->last_lin; int32_t* active = params->active; @@ -2085,7 +2085,7 @@ void* CSVMLight::reactivate_inactive_examples_linadd_helper(void* p) return NULL; } -void* CSVMLight::reactivate_inactive_examples_vanilla_helper(void* p) +void* SVMLight::reactivate_inactive_examples_vanilla_helper(void* p) { S_THREAD_PARAM_REACTIVATE_VANILLA* params = (S_THREAD_PARAM_REACTIVATE_VANILLA*) p; ASSERT(params) @@ -2098,7 +2098,7 @@ void* CSVMLight::reactivate_inactive_examples_vanilla_helper(void* p) ASSERT(params->inactive2dnum) ASSERT(params->label) - CKernel* k = params->kernel; + auto k = params->kernel; float64_t* lin = params->lin; float64_t* aicache = params->aicache; float64_t* a= params->a; @@ -2122,7 +2122,7 @@ void* CSVMLight::reactivate_inactive_examples_vanilla_helper(void* p) return NULL; } -void CSVMLight::reactivate_inactive_examples( +void SVMLight::reactivate_inactive_examples( int32_t* label, float64_t *a, SHRINK_STATE *shrink_state, float64_t *lin, float64_t *c, int32_t totdoc, int32_t iteration, int32_t *inconsistent, int32_t* docs, float64_t *aicache, float64_t *maxdiff) @@ -2189,7 +2189,7 @@ void CSVMLight::reactivate_inactive_examples( params[t].active=shrink_state->active; params[t].start = t*step; params[t].end = (t+1)*step; - pthread_create(&threads[t], NULL, CSVMLight::reactivate_inactive_examples_linadd_helper, (void*)¶ms[t]); + pthread_create(&threads[t], NULL, SVMLight::reactivate_inactive_examples_linadd_helper, (void*)¶ms[t]); } params[t].kernel=kernel; @@ -2339,7 +2339,7 @@ void CSVMLight::reactivate_inactive_examples( params[thr].label=label; params[thr].start = thr*step; params[thr].end = (thr+1)*step; - pthread_create(&threads[thr], NULL, CSVMLight::reactivate_inactive_examples_vanilla_helper, (void*)¶ms[thr]); + pthread_create(&threads[thr], NULL, SVMLight::reactivate_inactive_examples_vanilla_helper, (void*)¶ms[thr]); } params[thr].kernel=kernel; @@ -2426,7 +2426,7 @@ void CSVMLight::reactivate_inactive_examples( /* start the optimizer and return the optimal values */ -float64_t* CSVMLight::optimize_qp( +float64_t* SVMLight::optimize_qp( QP *qp, float64_t *epsilon_crit, int32_t nx, float64_t *threshold, int32_t& svm_maxqpsize) { @@ -2456,7 +2456,7 @@ float64_t* CSVMLight::optimize_qp( for(margin=init_margin,iter=init_iter; (margin<=0.9999999) && (result!=OPTIMAL_SOLUTION);) { - opt_precision=CMath::max(opt_precision, DEF_PRECISION); + opt_precision=Math::max(opt_precision, DEF_PRECISION); sigdig=-log10(opt_precision); result=pr_loqo((int32_t)qp->opt_n,(int32_t)qp->opt_m, @@ -2468,7 +2468,7 @@ float64_t* CSVMLight::optimize_qp( (float64_t)sigdig,(int32_t)iter, (float64_t)margin,(float64_t)(qp->opt_up[0])/4.0,(int32_t)0); - if(CMath::is_nan(dual[0])) { /* check for choldc problem */ + if(Math::is_nan(dual[0])) { /* check for choldc problem */ if(verbosity>=2) { SG_DEBUG("Restarting PR_LOQO with more conservative parameters.") } @@ -2537,7 +2537,7 @@ float64_t* CSVMLight::optimize_qp( /* if optimizer returned NAN values, reset and retry with smaller */ /* working set. */ - if(CMath::is_nan(obj_after) || CMath::is_nan(model_b)) { + if(Math::is_nan(obj_after) || Math::is_nan(model_b)) { for(i=0;iopt_n;i++) { primal[i]=qp->opt_xinit[i]; } diff --git a/src/shogun/classifier/svm/SVMLight.h b/src/shogun/classifier/svm/SVMLight.h index 8c005fd08cc..7903eb6f687 100644 --- a/src/shogun/classifier/svm/SVMLight.h +++ b/src/shogun/classifier/svm/SVMLight.h @@ -31,7 +31,7 @@ namespace shogun { -class CKernel; +class Kernel; //# define VERSION "V3.50 -- correct??" //# define VERSION_DATE "01.11.00 -- correct??" @@ -56,7 +56,7 @@ int32_t *index; /** number of training documents */ int32_t totdoc; /** kernel */ -CKernel* kernel; +std::shared_ptr kernel; /* the following values are not written to file */ /** leave-one-out estimates */ @@ -222,11 +222,11 @@ struct SHRINK_STATE #endif // DOXYGEN_SHOULD_SKIP_THIS /** @brief class SVMlight */ -class CSVMLight : public CSVM +class SVMLight : public SVM { public: /** default constructor */ - CSVMLight(); + SVMLight(); /** constructor * @@ -234,8 +234,8 @@ class CSVMLight : public CSVM * @param k kernel * @param lab labels */ - CSVMLight(float64_t C, CKernel* k, CLabels* lab); - virtual ~CSVMLight(); + SVMLight(float64_t C, std::shared_ptr k, std::shared_ptr lab); + virtual ~SVMLight(); /** init SVM */ void init(); @@ -646,7 +646,7 @@ class CSVMLight : public CSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); protected: /** model */ diff --git a/src/shogun/classifier/svm/SVMLightOneClass.cpp b/src/shogun/classifier/svm/SVMLightOneClass.cpp index 3fadbda0679..c8cea98cb89 100644 --- a/src/shogun/classifier/svm/SVMLightOneClass.cpp +++ b/src/shogun/classifier/svm/SVMLightOneClass.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, Viktor Gal, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, Viktor Gal, * Bjoern Esser, Evangelos Anagnostopoulos */ @@ -32,19 +32,19 @@ extern "C" { using namespace shogun; -CSVMLightOneClass::CSVMLightOneClass(float64_t C, CKernel* k) -: CSVMLight() +SVMLightOneClass::SVMLightOneClass(float64_t C, std::shared_ptr k) +: SVMLight() { set_C(C,C); set_kernel(k); } -CSVMLightOneClass::CSVMLightOneClass() -: CSVMLight() +SVMLightOneClass::SVMLightOneClass() +: SVMLight() { } -bool CSVMLightOneClass::train_machine(CFeatures* data) +bool SVMLightOneClass::train_machine(std::shared_ptr data) { //certain setup params mkl_converged=false; @@ -86,10 +86,10 @@ bool CSVMLightOneClass::train_machine(CFeatures* data) int32_t num_vec=kernel->get_num_vec_lhs(); io::info("num_vec={}", num_vec); - SG_UNREF(m_labels); - m_labels=new CBinaryLabels(num_vec); - SG_REF(m_labels); - ((CBinaryLabels*) m_labels)->set_to_one(); + + m_labels=std::make_shared(num_vec); + + std::static_pointer_cast(m_labels)->set_to_one(); // in case of LINADD enabled kernels cleanup! if (kernel->has_property(KP_LINADD) && get_linadd_enabled()) @@ -115,12 +115,12 @@ bool CSVMLightOneClass::train_machine(CFeatures* data) if (kernel->get_kernel_type() == K_COMBINED) { - for (index_t k_idx=0; k_idx<((CCombinedKernel*) kernel)->get_num_kernels(); k_idx++) + for (index_t k_idx=0; k_idx<(std::static_pointer_cast(kernel))->get_num_kernels(); k_idx++) { - CKernel* kn = ((CCombinedKernel*) kernel)->get_kernel(k_idx); + auto kn = (std::static_pointer_cast(kernel))->get_kernel(k_idx); // allocate kernel cache but clean up beforehand kn->resize_kernel_cache(kn->get_cache_size()); - SG_UNREF(kn); + } } diff --git a/src/shogun/classifier/svm/SVMLightOneClass.h b/src/shogun/classifier/svm/SVMLightOneClass.h index 8f2c6d9115d..66da55a3dd0 100644 --- a/src/shogun/classifier/svm/SVMLightOneClass.h +++ b/src/shogun/classifier/svm/SVMLightOneClass.h @@ -19,23 +19,23 @@ namespace shogun { /** @brief Trains a one class C SVM * - * \sa CSVMLight + * \sa SVMLight */ -class CSVMLightOneClass: public CSVMLight +class SVMLightOneClass: public SVMLight { public: /** default constructor */ - CSVMLightOneClass(); + SVMLightOneClass(); /** constructor * * @param C constant C * @param k kernel */ - CSVMLightOneClass(float64_t C, CKernel* k); + SVMLightOneClass(float64_t C, std::shared_ptr k); /** default destructor */ - virtual ~CSVMLightOneClass() { } + virtual ~SVMLightOneClass() { } /** get classifier type * @@ -58,7 +58,7 @@ class CSVMLightOneClass: public CSVMLight * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); }; } #endif //USE_SVMLIGHT diff --git a/src/shogun/classifier/svm/SVMOcas.cpp b/src/shogun/classifier/svm/SVMOcas.cpp index 021bb70335e..d3eecdf5a3e 100644 --- a/src/shogun/classifier/svm/SVMOcas.cpp +++ b/src/shogun/classifier/svm/SVMOcas.cpp @@ -19,37 +19,37 @@ using namespace shogun; -CSVMOcas::CSVMOcas() -: CLinearMachine() +SVMOcas::SVMOcas() +: LinearMachine() { init(); } -CSVMOcas::CSVMOcas(E_SVM_TYPE type) -: CLinearMachine() +SVMOcas::SVMOcas(E_SVM_TYPE type) +: LinearMachine() { init(); method=type; } -CSVMOcas::CSVMOcas( - float64_t C, CFeatures* traindat, CLabels* trainlab) -: CLinearMachine() +SVMOcas::SVMOcas( + float64_t C, std::shared_ptr traindat, std::shared_ptr trainlab) +: LinearMachine() { init(); C1=C; C2=C; - set_features(traindat->as()); + set_features(std::dynamic_pointer_cast(traindat)); set_labels(trainlab); } -CSVMOcas::~CSVMOcas() +SVMOcas::~SVMOcas() { } -bool CSVMOcas::train_machine(CFeatures* data) +bool SVMOcas::train_machine(std::shared_ptr data) { io::info("C={}, epsilon={}, bufsize={}", get_C1(), get_epsilon(), bufsize); SG_DEBUG("use_bias = {}", get_bias_enabled()) @@ -60,14 +60,15 @@ bool CSVMOcas::train_machine(CFeatures* data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } ASSERT(features) int32_t num_vec=features->get_num_vectors(); lab = SGVector(num_vec); + auto labels = binary_labels(m_labels); for (int32_t i=0; iget_label(i); + lab[i] = labels->get_label(i); current_w = SGVector(features->get_dim_feature_space()); current_w.zero(); @@ -93,12 +94,12 @@ bool CSVMOcas::train_machine(CFeatures* data) Method = 1; ocas_return_value_T result = svm_ocas_solver( get_C1(), num_vec, get_epsilon(), TolAbs, QPBound, get_max_train_time(), bufsize, Method, - &CSVMOcas::compute_W, - &CSVMOcas::update_W, - &CSVMOcas::add_new_cut, - &CSVMOcas::compute_output, - &CSVMOcas::sort, - &CSVMOcas::print, + &SVMOcas::compute_W, + &SVMOcas::update_W, + &SVMOcas::add_new_cut, + &SVMOcas::compute_output, + &SVMOcas::sort, + &SVMOcas::print, this); io::info("Ocas Converged after {} iterations" @@ -151,10 +152,10 @@ bool CSVMOcas::train_machine(CFeatures* data) sq_norm_W = W'*W; ---------------------------------------------------------------------------------*/ -float64_t CSVMOcas::update_W( float64_t t, void* ptr ) +float64_t SVMOcas::update_W( float64_t t, void* ptr ) { float64_t sq_norm_W = 0; - CSVMOcas* o = (CSVMOcas*) ptr; + auto o = (SVMOcas*)ptr; uint32_t nDim = (uint32_t) o->current_w.vlen; float64_t* W = o->current_w.vector; float64_t* oldW=o->old_w; @@ -165,7 +166,7 @@ float64_t CSVMOcas::update_W( float64_t t, void* ptr ) sq_norm_W += W[j]*W[j]; } o->bias=o->old_bias*(1-t) + t*o->bias; - sq_norm_W += CMath::sq(o->bias); + sq_norm_W += Math::sq(o->bias); return( sq_norm_W ); } @@ -178,12 +179,12 @@ float64_t CSVMOcas::update_W( float64_t t, void* ptr ) sparse_A(:,nSel+1) = new_a; ---------------------------------------------------------------------------------*/ -int CSVMOcas::add_new_cut( +int SVMOcas::add_new_cut( float64_t *new_col_H, uint32_t *new_cut, uint32_t cut_length, uint32_t nSel, void* ptr) { - CSVMOcas* o = (CSVMOcas*) ptr; - CDotFeatures* f = o->features; + auto o = (SVMOcas*)ptr; + auto f = o->features; uint32_t nDim=(uint32_t) o->current_w.vlen; float64_t* y = o->lab.vector; @@ -209,7 +210,7 @@ int CSVMOcas::add_new_cut( /* compute new_a'*new_a and count number of non-zerou dimensions */ nz_dims = 0; - sq_norm_a = CMath::sq(c_bias[nSel]); + sq_norm_a = Math::sq(c_bias[nSel]); for(j=0; j < nDim; j++ ) { if(new_a[j] != 0) { nz_dims++; @@ -248,15 +249,15 @@ int CSVMOcas::add_new_cut( new_col_H[i] = tmp; } - //CMath::display_vector(new_col_H, nSel+1, "new_col_H"); - //CMath::display_vector((int32_t*) c_idx[nSel], (int32_t) nz_dims, "c_idx"); - //CMath::display_vector((float64_t*) c_val[nSel], nz_dims, "c_val"); + //Math::display_vector(new_col_H, nSel+1, "new_col_H"); + //Math::display_vector((int32_t*) c_idx[nSel], (int32_t) nz_dims, "c_idx"); + //Math::display_vector((float64_t*) c_val[nSel], nz_dims, "c_val"); return 0; } -int CSVMOcas::sort(float64_t* vals, float64_t* data, uint32_t size) +int SVMOcas::sort(float64_t* vals, float64_t* data, uint32_t size) { - CMath::qsort_index(vals, data, size); + Math::qsort_index(vals, data, size); return 0; } @@ -265,10 +266,10 @@ int CSVMOcas::sort(float64_t* vals, float64_t* data, uint32_t size) output = data_X'*W; ----------------------------------------------------------------------*/ -int CSVMOcas::compute_output(float64_t *output, void* ptr) +int SVMOcas::compute_output(float64_t *output, void* ptr) { - CSVMOcas* o = (CSVMOcas*) ptr; - CDotFeatures* f=o->features; + auto o = (SVMOcas*)ptr; + auto f=o->features; int32_t nData=f->get_num_vectors(); float64_t* y = o->lab.vector; @@ -289,13 +290,13 @@ int CSVMOcas::compute_output(float64_t *output, void* ptr) dp_WoldW = W'*oldW'; ----------------------------------------------------------------------*/ -void CSVMOcas::compute_W( +void SVMOcas::compute_W( float64_t *sq_norm_W, float64_t *dp_WoldW, float64_t *alpha, uint32_t nSel, void* ptr ) { - CSVMOcas* o = (CSVMOcas*) ptr; + auto o = (SVMOcas*)ptr; uint32_t nDim= (uint32_t) o->current_w.vlen; - CMath::swap(o->current_w.vector, o->old_w); + Math::swap(o->current_w.vector, o->old_w); SGVector W(o->current_w.vector, nDim, false); SGVector oldW(o->old_w, nDim, false); linalg::zero(W); @@ -319,7 +320,7 @@ void CSVMOcas::compute_W( bias += c_bias[i]*alpha[i]; } - *sq_norm_W = linalg::dot(W, W) + CMath::sq(bias); + *sq_norm_W = linalg::dot(W, W) + Math::sq(bias); *dp_WoldW = linalg::dot(W, oldW) + bias*old_bias; //io::print("nSel={} sq_norm_W={} dp_WoldW={}\n", nSel, *sq_norm_W, *dp_WoldW); @@ -327,7 +328,7 @@ void CSVMOcas::compute_W( o->old_bias = old_bias; } -void CSVMOcas::init() +void SVMOcas::init() { use_bias = true; bufsize = 3000; @@ -355,7 +356,7 @@ void CSVMOcas::init() SG_OPTIONS(SVM_OCAS, SVM_BMRM)); } -float64_t CSVMOcas::compute_primal_objective() const +float64_t SVMOcas::compute_primal_objective() const { return primal_objective; } diff --git a/src/shogun/classifier/svm/SVMOcas.h b/src/shogun/classifier/svm/SVMOcas.h index 8c0086abf7d..6317c427b2f 100644 --- a/src/shogun/classifier/svm/SVMOcas.h +++ b/src/shogun/classifier/svm/SVMOcas.h @@ -28,7 +28,7 @@ enum E_SVM_TYPE #endif /** @brief class SVMOcas */ -class CSVMOcas : public CLinearMachine +class SVMOcas : public LinearMachine { public: @@ -36,13 +36,13 @@ class CSVMOcas : public CLinearMachine MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - CSVMOcas(); + SVMOcas(); /** constructor * * @param type a E_SVM_TYPE */ - CSVMOcas(E_SVM_TYPE type); + SVMOcas(E_SVM_TYPE type); /** constructor * @@ -50,10 +50,10 @@ class CSVMOcas : public CLinearMachine * @param traindat training features * @param trainlab labels for training features */ - CSVMOcas( - float64_t C, CFeatures* traindat, - CLabels* trainlab); - virtual ~CSVMOcas(); + SVMOcas( + float64_t C, std::shared_ptr traindat, + std::shared_ptr trainlab); + virtual ~SVMOcas(); /** get classifier type * @@ -187,7 +187,7 @@ class CSVMOcas : public CLinearMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); private: void init(); diff --git a/src/shogun/classifier/svm/WDSVMOcas.cpp b/src/shogun/classifier/svm/WDSVMOcas.cpp index 058bdd30496..de28d9679be 100644 --- a/src/shogun/classifier/svm/WDSVMOcas.cpp +++ b/src/shogun/classifier/svm/WDSVMOcas.cpp @@ -27,14 +27,14 @@ struct wdocas_thread_params_output float32_t* out; int32_t* val; float64_t* output; - CWDSVMOcas* wdocas; + WDSVMOcas* wdocas; int32_t start; int32_t end; }; struct wdocas_thread_params_add { - CWDSVMOcas* wdocas; + WDSVMOcas* wdocas; float32_t* new_a; uint32_t* new_cut; int32_t start; @@ -43,8 +43,8 @@ struct wdocas_thread_params_add }; #endif // DOXYGEN_SHOULD_SKIP_THIS -CWDSVMOcas::CWDSVMOcas() -: CMachine(), use_bias(false), bufsize(3000), C1(1), C2(1), +WDSVMOcas::WDSVMOcas() +: Machine(), use_bias(false), bufsize(3000), C1(1), C2(1), epsilon(1e-3), method(SVM_OCAS) { unstable(SOURCE_LOCATION); @@ -59,8 +59,8 @@ CWDSVMOcas::CWDSVMOcas() normalization_const=1.0; } -CWDSVMOcas::CWDSVMOcas(E_SVM_TYPE type) -: CMachine(), use_bias(false), bufsize(3000), C1(1), C2(1), +WDSVMOcas::WDSVMOcas(E_SVM_TYPE type) +: Machine(), use_bias(false), bufsize(3000), C1(1), C2(1), epsilon(1e-3), method(type) { w=NULL; @@ -73,10 +73,10 @@ CWDSVMOcas::CWDSVMOcas(E_SVM_TYPE type) normalization_const=1.0; } -CWDSVMOcas::CWDSVMOcas( - float64_t C, int32_t d, int32_t from_d, CStringFeatures* traindat, - CLabels* trainlab) -: CMachine(), use_bias(false), bufsize(3000), C1(C), C2(C), epsilon(1e-3), +WDSVMOcas::WDSVMOcas( + float64_t C, int32_t d, int32_t from_d, std::shared_ptr> traindat, + std::shared_ptr trainlab) +: Machine(), use_bias(false), bufsize(3000), C1(C), C2(C), epsilon(1e-3), degree(d), from_degree(from_d) { w=NULL; @@ -90,23 +90,23 @@ CWDSVMOcas::CWDSVMOcas( } -CWDSVMOcas::~CWDSVMOcas() +WDSVMOcas::~WDSVMOcas() { } -CBinaryLabels* CWDSVMOcas::apply_binary(CFeatures* data) +std::shared_ptr WDSVMOcas::apply_binary(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CBinaryLabels(outputs); + return std::make_shared(outputs); } -CRegressionLabels* CWDSVMOcas::apply_regression(CFeatures* data) +std::shared_ptr WDSVMOcas::apply_regression(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CRegressionLabels(outputs); + return std::make_shared(outputs); } -SGVector CWDSVMOcas::apply_get_outputs(CFeatures* data) +SGVector WDSVMOcas::apply_get_outputs(std::shared_ptr data) { if (data) { @@ -116,7 +116,7 @@ SGVector CWDSVMOcas::apply_get_outputs(CFeatures* data) error("Features not of class string type byte"); } - set_features((CStringFeatures*) data); + set_features(std::static_pointer_cast>(data)); } ASSERT(features) @@ -138,7 +138,7 @@ SGVector CWDSVMOcas::apply_get_outputs(CFeatures* data) return outputs; } -int32_t CWDSVMOcas::set_wd_weights() +int32_t WDSVMOcas::set_wd_weights() { ASSERT(degree>0 && degree<=8) SG_FREE(wd_weights); @@ -149,14 +149,14 @@ int32_t CWDSVMOcas::set_wd_weights() for (int32_t i=0; i data) { io::info("C={}, epsilon={}, bufsize={}", get_C1(), get_epsilon(), bufsize); @@ -169,20 +169,20 @@ bool CWDSVMOcas::train_machine(CFeatures* data) { error("Features not of class string type byte"); } - set_features((CStringFeatures*) data); + set_features(std::static_pointer_cast>(data)); } ASSERT(get_features()) - CAlphabet* alphabet=get_features()->get_alphabet(); + auto alphabet=get_features()->get_alphabet(); ASSERT(alphabet && alphabet->get_alphabet()==RAWDNA) alphabet_size=alphabet->get_num_symbols(); string_length=features->get_num_vectors(); - SGVector labvec=((CBinaryLabels*) m_labels)->get_labels(); + SGVector labvec=(std::static_pointer_cast(m_labels))->get_labels(); lab=labvec.vector; w_dim_single_char=set_wd_weights(); - //CMath::display_vector(wd_weights, degree, "wd_weights"); + //Math::display_vector(wd_weights, degree, "wd_weights"); SG_DEBUG("w_dim_single_char={}", w_dim_single_char) w_dim=string_length*w_dim_single_char; SG_DEBUG("cutting plane has {} dims", w_dim) @@ -210,10 +210,10 @@ bool CWDSVMOcas::train_machine(CFeatures* data) /////speed tests///// /*float64_t* tmp = SG_MALLOC(float64_t, num_vec); - float64_t start=CTime::get_curtime(); - CMath::random_vector(w, w_dim, (float32_t) 0, (float32_t) 1000); + float64_t start=Time::get_curtime(); + Math::random_vector(w, w_dim, (float32_t) 0, (float32_t) 1000); compute_output(tmp, this); - start=CTime::get_curtime()-start; + start=Time::get_curtime()-start; io::print("timing:{}\n", start); SG_FREE(tmp); exit(1);*/ @@ -225,12 +225,12 @@ bool CWDSVMOcas::train_machine(CFeatures* data) Method = 1; ocas_return_value_T result = svm_ocas_solver( get_C1(), num_vec, get_epsilon(), TolAbs, QPBound, get_max_train_time(), bufsize, Method, - &CWDSVMOcas::compute_W, - &CWDSVMOcas::update_W, - &CWDSVMOcas::add_new_cut, - &CWDSVMOcas::compute_output, - &CWDSVMOcas::sort, - &CWDSVMOcas::print, + &WDSVMOcas::compute_W, + &WDSVMOcas::update_W, + &WDSVMOcas::add_new_cut, + &WDSVMOcas::compute_output, + &WDSVMOcas::sort, + &WDSVMOcas::print, this); io::info("Ocas Converged after {} iterations" @@ -249,7 +249,7 @@ bool CWDSVMOcas::train_machine(CFeatures* data) SG_FREE(cuts); lab=NULL; - SG_UNREF(alphabet); + return true; } @@ -261,10 +261,10 @@ bool CWDSVMOcas::train_machine(CFeatures* data) sq_norm_W = W'*W; ---------------------------------------------------------------------------------*/ -float64_t CWDSVMOcas::update_W( float64_t t, void* ptr ) +float64_t WDSVMOcas::update_W( float64_t t, void* ptr ) { float64_t sq_norm_W = 0; - CWDSVMOcas* o = (CWDSVMOcas*) ptr; + auto o = (WDSVMOcas*)ptr; uint32_t nDim = (uint32_t) o->w_dim; float32_t* W=o->w; float32_t* oldW=o->old_w; @@ -278,7 +278,7 @@ float64_t CWDSVMOcas::update_W( float64_t t, void* ptr ) } bias=old_bias*(1-t) + t*bias; - sq_norm_W += CMath::sq(bias); + sq_norm_W += Math::sq(bias); o->bias=bias; o->old_bias=old_bias; @@ -294,10 +294,10 @@ float64_t CWDSVMOcas::update_W( float64_t t, void* ptr ) sparse_A(:,nSel+1) = new_a; ---------------------------------------------------------------------------------*/ -void* CWDSVMOcas::add_new_cut_helper( void* ptr) +void* WDSVMOcas::add_new_cut_helper( void* ptr) { wdocas_thread_params_add* p = (wdocas_thread_params_add*) ptr; - CWDSVMOcas* o = p->wdocas; + auto o = p->wdocas; int32_t start = p->start; int32_t end = p->end; int32_t string_length = o->string_length; @@ -309,7 +309,7 @@ void* CWDSVMOcas::add_new_cut_helper( void* ptr) int32_t alphabet_size = o->alphabet_size; float32_t* wd_weights = o->wd_weights; int32_t degree = o->degree; - CStringFeatures* f = o->features; + auto f = o->features; float64_t normalization_const = o->normalization_const; // temporary vector @@ -322,7 +322,7 @@ void* CWDSVMOcas::add_new_cut_helper( void* ptr) { int32_t offs=o->w_dim_single_char*j; memset(val,0,sizeof(int32_t)*cut_length); - int32_t lim=CMath::min(degree, string_length-j); + int32_t lim=Math::min(degree, string_length-j); int32_t len; for (int32_t k=0; kcp_bias; uint32_t nDim=(uint32_t) o->w_dim; @@ -382,7 +382,7 @@ int CWDSVMOcas::add_new_cut( params_add[t].end = step*(t+1); params_add[t].cut_length = cut_length; - if (pthread_create(&threads[t], NULL, &CWDSVMOcas::add_new_cut_helper, (void*)¶ms_add[t]) != 0) + if (pthread_create(&threads[t], NULL, &WDSVMOcas::add_new_cut_helper, (void*)¶ms_add[t]) != 0) { nthreads=t; io::warn("thread creation failed"); @@ -425,19 +425,19 @@ int CWDSVMOcas::add_new_cut( SGVector cut_wrap(cuts[i], nDim, false); new_col_H[i] = linalg::dot(new_a, cut_wrap) + c_bias[nSel]*c_bias[i]; } - new_col_H[nSel] = linalg::dot(new_a, new_a) + CMath::sq(c_bias[nSel]); + new_col_H[nSel] = linalg::dot(new_a, new_a) + Math::sq(c_bias[nSel]); cuts[nSel]=new_a; - //CMath::display_vector(new_col_H, nSel+1, "new_col_H"); - //CMath::display_vector(cuts[nSel], nDim, "cut[nSel]"); + //Math::display_vector(new_col_H, nSel+1, "new_col_H"); + //Math::display_vector(cuts[nSel], nDim, "cut[nSel]"); // return 0; } -int CWDSVMOcas::sort( float64_t* vals, float64_t* data, uint32_t size) +int WDSVMOcas::sort( float64_t* vals, float64_t* data, uint32_t size) { - CMath::qsort_index(vals, data, size); + Math::qsort_index(vals, data, size); return 0; } @@ -446,17 +446,17 @@ int CWDSVMOcas::sort( float64_t* vals, float64_t* data, uint32_t size) output = data_X'*W; ----------------------------------------------------------------------*/ -void* CWDSVMOcas::compute_output_helper(void* ptr) +void* WDSVMOcas::compute_output_helper(void* ptr) { wdocas_thread_params_output* p = (wdocas_thread_params_output*) ptr; - CWDSVMOcas* o = p->wdocas; + WDSVMOcas* o = p->wdocas; int32_t start = p->start; int32_t end = p->end; float32_t* out = p->out; float64_t* output = p->output; int32_t* val = p->val; - CStringFeatures* f=o->get_features(); + auto f=o->get_features(); int32_t degree = o->degree; int32_t string_length = o->string_length; @@ -475,7 +475,7 @@ void* CWDSVMOcas::compute_output_helper(void* ptr) for (int32_t i=start ; ibias + out[i]*y[i]/normalization_const; - //CMath::display_vector(o->w, o->w_dim, "w"); - //CMath::display_vector(output, nData, "out"); + //Math::display_vector(o->w, o->w_dim, "w"); + //Math::display_vector(output, nData, "out"); return NULL; } -int CWDSVMOcas::compute_output( float64_t *output, void* ptr ) +int WDSVMOcas::compute_output( float64_t *output, void* ptr ) { #ifdef HAVE_PTHREAD - CWDSVMOcas* o = (CWDSVMOcas*) ptr; + auto o = (WDSVMOcas*)ptr; int32_t nData=o->num_vec; wdocas_thread_params_output* params_output=SG_MALLOC(wdocas_thread_params_output, env()->get_num_threads()); pthread_t* threads = SG_MALLOC(pthread_t, env()->get_num_threads()); @@ -570,7 +570,7 @@ int CWDSVMOcas::compute_output( float64_t *output, void* ptr ) params_output[t].end = step*(t+1); //io::print("t={} start={} end={} output={}\n", t, params_output[t].start, params_output[t].end, fmt::ptr(params_output[t].output)); - if (pthread_create(&threads[t], NULL, &CWDSVMOcas::compute_output_helper, (void*)¶ms_output[t]) != 0) + if (pthread_create(&threads[t], NULL, &WDSVMOcas::compute_output_helper, (void*)¶ms_output[t]) != 0) { nthreads=t; io::warn("thread creation failed"); @@ -608,13 +608,13 @@ int CWDSVMOcas::compute_output( float64_t *output, void* ptr ) dp_WoldW = W'*oldW'; ----------------------------------------------------------------------*/ -void CWDSVMOcas::compute_W( +void WDSVMOcas::compute_W( float64_t *sq_norm_W, float64_t *dp_WoldW, float64_t *alpha, uint32_t nSel, void* ptr) { - CWDSVMOcas* o = (CWDSVMOcas*) ptr; + auto o = (WDSVMOcas*)ptr; uint32_t nDim= (uint32_t) o->w_dim; - CMath::swap(o->w, o->old_w); + Math::swap(o->w, o->old_w); SGVector W(o->w, nDim, false); linalg::zero(W); SGVector oldW(o->old_w, nDim, false); @@ -631,7 +631,7 @@ void CWDSVMOcas::compute_W( bias += c_bias[i]*alpha[i]; } - *sq_norm_W = linalg::dot(W, W) +CMath::sq(bias); + *sq_norm_W = linalg::dot(W, W) +Math::sq(bias); *dp_WoldW = linalg::dot(W, oldW) + bias*old_bias;; //io::print("nSel={} sq_norm_W={} dp_WoldW={}\n", nSel, *sq_norm_W, *dp_WoldW); diff --git a/src/shogun/classifier/svm/WDSVMOcas.h b/src/shogun/classifier/svm/WDSVMOcas.h index df03efc51fb..034cc556ac1 100644 --- a/src/shogun/classifier/svm/WDSVMOcas.h +++ b/src/shogun/classifier/svm/WDSVMOcas.h @@ -18,23 +18,23 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /** @brief class WDSVMOcas */ -class CWDSVMOcas : public CMachine +class WDSVMOcas : public Machine { public: /** problem type */ MACHINE_PROBLEM_TYPE(PT_BINARY); /** default constructor */ - CWDSVMOcas(); + WDSVMOcas(); /** constructor * * @param type type of SVM */ - CWDSVMOcas(E_SVM_TYPE type); + WDSVMOcas(E_SVM_TYPE type); /** constructor * @@ -44,10 +44,10 @@ class CWDSVMOcas : public CMachine * @param traindat training features * @param trainlab labels for training features */ - CWDSVMOcas( + WDSVMOcas( float64_t C, int32_t d, int32_t from_d, - CStringFeatures* traindat, CLabels* trainlab); - virtual ~CWDSVMOcas(); + std::shared_ptr> traindat, std::shared_ptr trainlab); + virtual ~WDSVMOcas(); /** get classifier type * @@ -91,10 +91,10 @@ class CWDSVMOcas : public CMachine * * @param feat features to set */ - inline void set_features(CStringFeatures* feat) + inline void set_features(std::shared_ptr> feat) { - SG_REF(feat); - SG_UNREF(features); + + features=feat; } @@ -102,9 +102,9 @@ class CWDSVMOcas : public CMachine * * @return features */ - inline CStringFeatures* get_features() + inline std::shared_ptr> get_features() { - SG_REF(features); + return features; } @@ -155,7 +155,7 @@ class CWDSVMOcas : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /** classify objects * for regression problems @@ -163,7 +163,7 @@ class CWDSVMOcas : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); /** classify one example * @@ -223,7 +223,7 @@ class CWDSVMOcas : public CMachine * * @param data features to apply for */ - SGVector apply_get_outputs(CFeatures* data); + SGVector apply_get_outputs(std::shared_ptr data); /** set wd weights * @@ -311,11 +311,11 @@ class CWDSVMOcas : public CMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); protected: /** features */ - CStringFeatures* features; + std::shared_ptr> features; /** if bias shall be used */ bool use_bias; /** buffer size */ diff --git a/src/shogun/clustering/GMM.cpp b/src/shogun/clustering/GMM.cpp index e43ccf2bfdf..23cc1cc8557 100644 --- a/src/shogun/clustering/GMM.cpp +++ b/src/shogun/clustering/GMM.cpp @@ -9,7 +9,6 @@ #include #include -#include #include #include #include @@ -25,27 +24,27 @@ using namespace shogun; using namespace std; -CGMM::CGMM() : RandomMixin(), m_components(), m_coefficients() +GMM::GMM() : RandomMixin(), m_components(), m_coefficients() { register_params(); } -CGMM::CGMM(int32_t n, ECovType cov_type) : RandomMixin(), m_components(), m_coefficients() +GMM::GMM(int32_t n, ECovType cov_type) : RandomMixin(), m_components(), m_coefficients() { m_coefficients = SGVector(n); - m_components = vector(n); + m_components = vector>(n); for (int32_t i=0; i(); + m_components[i]->set_cov_type(cov_type); } register_params(); } -CGMM::CGMM(vector components, SGVector coefficients, bool copy) : RandomMixin() +GMM::GMM(vector> components, SGVector coefficients, bool copy) : RandomMixin() { ASSERT(int32_t(components.size())==coefficients.vlen) @@ -53,20 +52,16 @@ CGMM::CGMM(vector components, SGVector coefficients, bool { m_components=components; m_coefficients=coefficients; - for (int32_t i=0; i(components.size()); + m_components = vector>(components.size()); for (int32_t i=0; i(); + m_components[i]->set_cov_type(components[i]->get_cov_type()); SGVector old_mean=components[i]->get_mean(); @@ -91,22 +86,22 @@ CGMM::CGMM(vector components, SGVector coefficients, bool register_params(); } -CGMM::~CGMM() +GMM::~GMM() { if (!m_components.empty()) cleanup(); } -void CGMM::cleanup() +void GMM::cleanup() { for (int32_t i = 0; i < int32_t(m_components.size()); i++) - SG_UNREF(m_components[i]); - m_components = vector(); + + m_components = vector>(); m_coefficients = SGVector(); } -bool CGMM::train(CFeatures* data) +bool GMM::train(std::shared_ptr data) { ASSERT(m_components.size() != 0) @@ -121,12 +116,12 @@ bool CGMM::train(CFeatures* data) return true; } -float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_change) +float64_t GMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_change) { if (!features) error("No features to train on."); - CDotFeatures* dotdata=(CDotFeatures *) features; + auto dotdata=features->as>(); int32_t num_vectors=dotdata->get_num_vectors(); SGMatrix alpha; @@ -134,14 +129,14 @@ float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_chan /* compute initialization via kmeans if none is present */ if (m_components[0]->get_mean().vector==NULL) { - CKMeans* init_k_means=new CKMeans(int32_t(m_components.size()), new CEuclideanDistance()); + auto init_k_means=std::make_shared(int32_t(m_components.size()), std::make_shared()); seed(init_k_means); init_k_means->train(dotdata); SGMatrix init_means=init_k_means->get_cluster_centers(); alpha=alpha_init(init_means); - SG_UNREF(init_k_means); + max_likelihood(alpha, min_cov); } @@ -193,7 +188,7 @@ float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_chan iter, "log_likelihood", "Log Likelihood", log_likelihood_cur); this->observe>( iter, "coefficients", "Mixture Coefficients", alpha); - this->observe>(iter, "components"); + this->observe>>(iter, "components"); iter++; } @@ -201,7 +196,7 @@ float64_t CGMM::train_em(float64_t min_cov, int32_t max_iter, float64_t min_chan return log_likelihood_cur; } -float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov, int32_t max_em_iter, float64_t min_change) +float64_t GMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov, int32_t max_em_iter, float64_t min_change) { if (!features) error("No features to train on."); @@ -209,7 +204,7 @@ float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov if (m_components.size()<3) error("Can't run SMEM with less than 3 component mixture model."); - CDotFeatures* dotdata = features->as(); + auto dotdata = features->as(); auto num_vectors = dotdata->get_num_vectors(); float64_t cur_likelihood=train_em(min_cov, max_em_iter, min_change); @@ -298,9 +293,9 @@ float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov counter++; } } - CMath::qsort_backward_index( + Math::qsort_backward_index( split_crit.vector, split_ind.vector, int32_t(m_components.size())); - CMath::qsort_backward_index( + Math::qsort_backward_index( merge_crit.vector, merge_ind.vector, int32_t(m_components.size() * (m_components.size() - 1) / 2)); @@ -313,7 +308,7 @@ float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov if (merge_ind[j]/int32_t(m_components.size()) != split_ind[i] && int32_t(merge_ind[j]%m_components.size()) != split_ind[i]) { candidates_checked++; - CGMM* candidate=new CGMM(m_components, m_coefficients, true); + auto candidate=std::make_shared(m_components, m_coefficients, true); seed(candidate); candidate->train(features); candidate->partial_em(split_ind[i], merge_ind[j]/int32_t(m_components.size()), merge_ind[j]%int32_t(m_components.size()), min_cov, max_em_iter, min_change); @@ -326,11 +321,11 @@ float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov set_coef(candidate->get_coef()); better_found=true; - delete candidate; + candidate.reset(); break; } else - delete candidate; + candidate.reset(); if (candidates_checked>=max_cand) break; @@ -345,7 +340,7 @@ float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov this->observe( iter, "log_likelihood", "Log Likelihood", cur_likelihood); this->observe>(iter, "coefficients"); - this->observe>(iter, "components"); + this->observe>>(iter, "components"); iter++; pb.print_progress(); @@ -354,9 +349,9 @@ float64_t CGMM::train_smem(int32_t max_iter, int32_t max_cand, float64_t min_cov return cur_likelihood; } -void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min_cov, int32_t max_em_iter, float64_t min_change) +void GMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min_cov, int32_t max_em_iter, float64_t min_change) { - CDotFeatures* dotdata=(CDotFeatures *) features; + auto dotdata=features->as(); int32_t num_vectors=dotdata->get_num_vectors(); SGVector init_logPxy(num_vectors * m_components.size()); @@ -397,7 +392,7 @@ void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min init_logPx[i])); } - vector components(3); + vector> components(3); SGVector coefficients(3); components[0]=m_components[comp1]; components[1]=m_components[comp2]; @@ -495,7 +490,7 @@ void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min components[2]->get_d().vector[0]=components[0]->get_d().vector[0]; } - CGMM* partial_candidate=new CGMM(components, coefficients); + auto partial_candidate=std::make_shared(components, coefficients); seed(partial_candidate); partial_candidate->train(features); @@ -548,12 +543,12 @@ void CGMM::partial_em(int32_t comp1, int32_t comp2, int32_t comp3, float64_t min m_coefficients.vector[comp2]=coefficients.vector[1]; m_coefficients.vector[comp3]=coefficients.vector[2]; - delete partial_candidate; + partial_candidate.reset(); } -void CGMM::max_likelihood(SGMatrix alpha, float64_t min_cov) +void GMM::max_likelihood(SGMatrix alpha, float64_t min_cov) { - CDotFeatures* dotdata=(CDotFeatures *) features; + auto dotdata=features->as(); int32_t num_dim=dotdata->get_dim_feature_space(); float64_t alpha_sum; @@ -640,7 +635,7 @@ void CGMM::max_likelihood(SGMatrix alpha, float64_t min_cov) linalg::eigen_solver_symmetric(cov_sum, d0, cov_sum); for (auto& v: d0) - v = CMath::max(min_cov, v); + v = Math::max(min_cov, v); m_components[i]->set_d(d0); m_components[i]->set_u(cov_sum); @@ -651,7 +646,7 @@ void CGMM::max_likelihood(SGMatrix alpha, float64_t min_cov) for (int32_t j = 0; j < num_dim; j++) { cov_sum(0, j) /= alpha_sum; - cov_sum(0, j) = CMath::max(min_cov, cov_sum(0, j)); + cov_sum(0, j) = Math::max(min_cov, cov_sum(0, j)); } m_components[i]->set_d(cov_sum.get_row_vector(0)); @@ -659,7 +654,7 @@ void CGMM::max_likelihood(SGMatrix alpha, float64_t min_cov) break; case SPHERICAL: cov_sum[0] /= alpha_sum * num_dim; - cov_sum[0] = CMath::max(min_cov, cov_sum[0]); + cov_sum[0] = Math::max(min_cov, cov_sum[0]); m_components[i]->set_d(cov_sum.get_row_vector(0)); @@ -673,41 +668,41 @@ void CGMM::max_likelihood(SGMatrix alpha, float64_t min_cov) linalg::scale(m_coefficients, m_coefficients, 1.0 / alpha_sum_sum); } -int32_t CGMM::get_num_model_parameters() +int32_t GMM::get_num_model_parameters() { return 1; } -float64_t CGMM::get_log_model_parameter(int32_t num_param) +float64_t GMM::get_log_model_parameter(int32_t num_param) { ASSERT(num_param==1) return std::log(m_components.size()); } -index_t CGMM::get_num_components() const +index_t GMM::get_num_components() const { return m_components.size(); } -CDistribution* CGMM::get_component(index_t index) const +std::shared_ptr GMM::get_component(index_t index) const { return m_components[index]; } -float64_t CGMM::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t GMM::get_log_derivative(int32_t num_param, int32_t num_example) { not_implemented(SOURCE_LOCATION); return 0; } -float64_t CGMM::get_log_likelihood_example(int32_t num_example) +float64_t GMM::get_log_likelihood_example(int32_t num_example) { not_implemented(SOURCE_LOCATION); return 1; } -float64_t CGMM::get_likelihood_example(int32_t num_example) +float64_t GMM::get_likelihood_example(int32_t num_example) { float64_t result=0; @@ -715,9 +710,10 @@ float64_t CGMM::get_likelihood_example(int32_t num_example) ASSERT(features->get_feature_class() == C_DENSE); ASSERT(features->get_feature_type() == F_DREAL); + auto f = features->as>(); for (auto i: range(index_t(m_components.size()))) { - SGVector point= ((CDenseFeatures*) features)->get_feature_vector(num_example); + SGVector point= f->get_feature_vector(num_example); result += std::exp( m_components[i]->compute_log_PDF(point) + std::log(m_coefficients[i])); @@ -726,81 +722,71 @@ float64_t CGMM::get_likelihood_example(int32_t num_example) return result; } -SGVector CGMM::get_nth_mean(int32_t num) +SGVector GMM::get_nth_mean(int32_t num) { ASSERT(numget_mean(); } -void CGMM::set_nth_mean(SGVector mean, int32_t num) +void GMM::set_nth_mean(SGVector mean, int32_t num) { ASSERT(numset_mean(mean); } -SGMatrix CGMM::get_nth_cov(int32_t num) +SGMatrix GMM::get_nth_cov(int32_t num) { ASSERT(numget_cov(); } -void CGMM::set_nth_cov(SGMatrix cov, int32_t num) +void GMM::set_nth_cov(SGMatrix cov, int32_t num) { ASSERT(numset_cov(cov); } -SGVector CGMM::get_coef() +SGVector GMM::get_coef() { return m_coefficients; } -void CGMM::set_coef(const SGVector coefficients) +void GMM::set_coef(const SGVector coefficients) { m_coefficients=coefficients; } -vector CGMM::get_comp() +vector> GMM::get_comp() { return m_components; } -void CGMM::set_comp(vector components) +void GMM::set_comp(vector> components) { - for (int32_t i=0; i CGMM::alpha_init(SGMatrix init_means) +SGMatrix GMM::alpha_init(SGMatrix init_means) { - CDotFeatures* dotdata=(CDotFeatures *) features; + auto dotdata=features->as>(); auto num_vectors=dotdata->get_num_vectors(); SGVector label_num(init_means.num_cols); linalg::range_fill(label_num); - auto knn=some(1, new CEuclideanDistance(), new CMulticlassLabels(label_num)); - knn->train(new CDenseFeatures(init_means)); - auto init_labels = knn->apply(features)->as(); + auto knn=std::make_shared(1, std::make_shared(), std::make_shared(label_num)); + knn->train(std::make_shared>(init_means)); + auto init_labels = knn->apply(features)->as(); SGMatrix alpha(num_vectors, index_t(m_components.size())); for (auto i: range(num_vectors)) alpha[i * m_components.size() + init_labels->get_int_label(i)] = 1; - SG_UNREF(init_labels); + return alpha; } -SGVector CGMM::sample() +SGVector GMM::sample() { require(m_components.size()>0, "Number of mixture components is {} but " "must be positive\n", m_components.size()); @@ -820,7 +806,7 @@ SGVector CGMM::sample() return m_components[m_coefficients.vlen-1]->sample(); } -SGVector CGMM::cluster(SGVector point) +SGVector GMM::cluster(SGVector point) { SGVector answer(m_components.size()+1); answer.vector[m_components.size()]=0; @@ -837,7 +823,7 @@ SGVector CGMM::cluster(SGVector point) return answer; } -void CGMM::register_params() +void GMM::register_params() { this->watch_param( "components", &m_components, diff --git a/src/shogun/clustering/GMM.h b/src/shogun/clustering/GMM.h index 5166f847cb7..cd599e0b3c6 100644 --- a/src/shogun/clustering/GMM.h +++ b/src/shogun/clustering/GMM.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Alesis Novik, Sergey Lisitsyn, Heiko Strathmann, + * Authors: Soeren Sonnenburg, Alesis Novik, Sergey Lisitsyn, Heiko Strathmann, * Evgeniy Andreev, Evan Shelhamer, Wuwei Lin, Yori Zwols */ #ifndef _GMM_H__ @@ -31,26 +31,26 @@ namespace shogun * The SMEM algorithm is described here: * http://mlg.eng.cam.ac.uk/zoubin/papers/uedanc.pdf */ -class CGMM : public RandomMixin +class GMM : public RandomMixin { public: /** default constructor */ - CGMM(); + GMM(); /** constructor * * @param n number of Gaussians * @param cov_type covariance type */ - CGMM(int32_t n, ECovType cov_type=FULL); + GMM(int32_t n, ECovType cov_type=FULL); /** constructor * * @param components GMM components * @param coefficients mixing coefficients * @param copy true if should be copied */ - CGMM(std::vector components, SGVector coefficients, + GMM(std::vector> components, SGVector coefficients, bool copy=false); - virtual ~CGMM(); + virtual ~GMM(); /** cleanup */ void cleanup(); @@ -61,7 +61,7 @@ class CGMM : public RandomMixin * * @return true */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** learn model using EM * @@ -115,7 +115,7 @@ class CGMM : public RandomMixin * @param index index of component * @return component at index */ - CDistribution* get_component(index_t index) const; + std::shared_ptr get_component(index_t index) const; /** get partial derivative of likelihood function (logarithmic) * @@ -190,13 +190,13 @@ class CGMM : public RandomMixin * * @return components */ - virtual std::vector get_comp(); + virtual std::vector> get_comp(); /** set components * * @param components Gaussian components */ - virtual void set_comp(std::vector components); + virtual void set_comp(std::vector> components); /** sample from model * @@ -240,7 +240,7 @@ class CGMM : public RandomMixin protected: /** Mixture components */ - std::vector m_components; + std::vector> m_components; /** Mixture coefficients */ SGVector m_coefficients; }; diff --git a/src/shogun/clustering/Hierarchical.cpp b/src/shogun/clustering/Hierarchical.cpp index ea2ba52dd5a..67af8222acd 100644 --- a/src/shogun/clustering/Hierarchical.cpp +++ b/src/shogun/clustering/Hierarchical.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann, * Giovanni De Toni, Viktor Gal, Evan Shelhamer */ @@ -25,15 +25,15 @@ struct pair }; #endif // DOXYGEN_SHOULD_SKIP_THIS -CHierarchical::CHierarchical() -: CDistanceMachine() +Hierarchical::Hierarchical() +: DistanceMachine() { init(); register_parameters(); } -CHierarchical::CHierarchical(int32_t merges_, CDistance* d) -: CDistanceMachine() +Hierarchical::Hierarchical(int32_t merges_, std::shared_ptr d) +: DistanceMachine() { init(); merges = merges_; @@ -41,7 +41,7 @@ CHierarchical::CHierarchical(int32_t merges_, CDistance* d) register_parameters(); } -void CHierarchical::init() +void Hierarchical::init() { merges = 3; dimensions = 0; @@ -54,7 +54,7 @@ void CHierarchical::init() merge_distance_len = 0; } -void CHierarchical::register_parameters() +void Hierarchical::register_parameters() { watch_param("merges", &merges); watch_param("dimensions", &dimensions); @@ -64,26 +64,26 @@ void CHierarchical::register_parameters() watch_param("merge_distance", &merge_distance, &merge_distance_len); } -CHierarchical::~CHierarchical() +Hierarchical::~Hierarchical() { SG_FREE(merge_distance); SG_FREE(assignment); SG_FREE(pairs); } -EMachineType CHierarchical::get_classifier_type() +EMachineType Hierarchical::get_classifier_type() { return CT_HIERARCHICAL; } -bool CHierarchical::train_machine(CFeatures* data) +bool Hierarchical::train_machine(std::shared_ptr data) { ASSERT(distance) if (data) distance->init(data, data); - CFeatures* lhs=distance->get_lhs(); + auto lhs=distance->get_lhs(); ASSERT(lhs) int32_t num=lhs->get_num_vectors(); @@ -120,8 +120,8 @@ bool CHierarchical::train_machine(CFeatures* data) } } - CMath::qsort_index(distances, index, (num-1)*num/2); - //CMath::display_vector(distances, (num-1)*num/2, "dists"); + Math::qsort_index(distances, index, (num-1)*num/2); + //Math::display_vector(distances, (num-1)*num/2, "dists"); auto pb = SG_PROGRESS(range(0, num_pairs - 1)); int32_t k=-1; @@ -172,19 +172,19 @@ bool CHierarchical::train_machine(CFeatures* data) ASSERT(table_size>0) SG_FREE(distances); SG_FREE(index); - SG_UNREF(lhs) + return true; } -bool CHierarchical::load(FILE* srcfile) +bool Hierarchical::load(FILE* srcfile) { SG_SET_LOCALE_C; SG_RESET_LOCALE; return false; } -bool CHierarchical::save(FILE* dstfile) +bool Hierarchical::save(FILE* dstfile) { SG_SET_LOCALE_C; SG_RESET_LOCALE; @@ -192,22 +192,23 @@ bool CHierarchical::save(FILE* dstfile) } -int32_t CHierarchical::get_merges() +int32_t Hierarchical::get_merges() { return merges; } -SGVector CHierarchical::get_assignment() +SGVector Hierarchical::get_assignment() { return SGVector(assignment,table_size, false); } -SGVector CHierarchical::get_merge_distances() +SGVector Hierarchical::get_merge_distances() { return SGVector(merge_distance,merges, false); } -SGMatrix CHierarchical::get_cluster_pairs() +SGMatrix Hierarchical::get_cluster_pairs() { return SGMatrix(pairs,2,merges, false); } + diff --git a/src/shogun/clustering/Hierarchical.h b/src/shogun/clustering/Hierarchical.h index c453b703b40..13e65f82251 100644 --- a/src/shogun/clustering/Hierarchical.h +++ b/src/shogun/clustering/Hierarchical.h @@ -17,7 +17,7 @@ namespace shogun { -class CDistanceMachine; +class DistanceMachine; /** @brief Agglomerative hierarchical single linkage clustering. * @@ -32,19 +32,19 @@ class CDistanceMachine; * are merged. * * cf e.g. http://en.wikipedia.org/wiki/Data_clustering*/ -class CHierarchical : public CDistanceMachine +class Hierarchical : public DistanceMachine { public: /** default constructor */ - CHierarchical(); + Hierarchical(); /** constructor * * @param merges the merges * @param d distance */ - CHierarchical(int32_t merges, CDistance* d); - virtual ~CHierarchical(); + Hierarchical(int32_t merges, std::shared_ptr d); + virtual ~Hierarchical(); /** problem type */ MACHINE_PROBLEM_TYPE(PT_MULTICLASS); @@ -117,7 +117,7 @@ class CHierarchical : public CDistanceMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); private: /** Initialize attributes */ diff --git a/src/shogun/clustering/KMeans.cpp b/src/shogun/clustering/KMeans.cpp index 9f671a2f24b..34d40727de6 100644 --- a/src/shogun/clustering/KMeans.cpp +++ b/src/shogun/clustering/KMeans.cpp @@ -22,26 +22,26 @@ using namespace shogun; namespace shogun { -CKMeans::CKMeans():CKMeansBase() +KMeans::KMeans():KMeansBase() { } -CKMeans::CKMeans(int32_t k_i, CDistance* d_i, bool use_kmpp_i):CKMeansBase(k_i, d_i, use_kmpp_i) +KMeans::KMeans(int32_t k_i, std::shared_ptr d_i, bool use_kmpp_i):KMeansBase(k_i, d_i, use_kmpp_i) { } -CKMeans::CKMeans(int32_t k_i, CDistance* d_i, SGMatrix centers_i):CKMeansBase(k_i, d_i, centers_i) +KMeans::KMeans(int32_t k_i, std::shared_ptr d_i, SGMatrix centers_i):KMeansBase(k_i, d_i, centers_i) { } -CKMeans::~CKMeans() +KMeans::~KMeans() { } -void CKMeans::Lloyd_KMeans(SGMatrix centers, int32_t num_centers) +void KMeans::Lloyd_KMeans(SGMatrix centers, int32_t num_centers) { - CDenseFeatures* lhs = - distance->get_lhs()->as>(); + auto lhs = + std::dynamic_pointer_cast>(distance->get_lhs()); int32_t lhs_size=lhs->get_num_vectors(); int32_t dim=lhs->get_num_features(); @@ -68,7 +68,7 @@ void CKMeans::Lloyd_KMeans(SGMatrix centers, int32_t num_centers) Terminating. ", iter); changed=0; - auto rhs_mus = some>(centers.clone()); + auto rhs_mus = std::make_shared>(centers.clone()); distance->replace_rhs(rhs_mus); #pragma omp parallel for firstprivate(lhs_size, dim, num_centers) \ @@ -176,19 +176,17 @@ void CKMeans::Lloyd_KMeans(SGMatrix centers, int32_t num_centers) } distance->reset_precompute(); distance->replace_rhs(rhs_cache); - SG_UNREF(lhs); - SG_UNREF(rhs_cache); + + } -bool CKMeans::train_machine(CFeatures* data) +bool KMeans::train_machine(std::shared_ptr data) { initialize_training(data); Lloyd_KMeans(mus, k); compute_cluster_variances(); - auto cluster_centres = new CDenseFeatures(mus); - SG_REF(cluster_centres); - auto old = distance->replace_lhs(cluster_centres); - SG_UNREF(old); + auto cluster_centres = std::make_shared>(mus); + distance->replace_lhs(cluster_centres); return true; } diff --git a/src/shogun/clustering/KMeans.h b/src/shogun/clustering/KMeans.h index 9ffdb55bc08..c5d19f4190b 100644 --- a/src/shogun/clustering/KMeans.h +++ b/src/shogun/clustering/KMeans.h @@ -19,7 +19,7 @@ namespace shogun { -class CKMeansBase; +class KMeansBase; /** @brief KMeans clustering, partitions the data into k (a-priori specified) clusters. * @@ -33,19 +33,19 @@ class CKMeansBase; * * Beware that this algorithm obtains only a local optimum. * - * To use mini-batch based training was see CKMeansMiniBatch + * To use mini-batch based training was see KMeansMiniBatch * * cf. http://en.wikipedia.org/wiki/K-means_algorithm * cf. http://en.wikipedia.org/wiki/Lloyd's_algorithm * * */ -class CKMeans : public CKMeansBase +class KMeans : public KMeansBase { public: /** default constructor */ - CKMeans(); + KMeans(); /** constructor * @@ -53,16 +53,16 @@ class CKMeans : public CKMeansBase * @param d distance * @param kmeanspp Set to true for using KMeans++ (default false) */ - CKMeans(int32_t k, CDistance* d, bool kmeanspp=false); + KMeans(int32_t k, std::shared_ptr d, bool kmeanspp=false); /** constructor for supplying initial centers * @param k_i parameter k * @param d_i distance * @param centers_i initial centers for KMeans algorithm */ - CKMeans(int32_t k_i, CDistance* d_i, SGMatrix centers_i); + KMeans(int32_t k_i, std::shared_ptr d_i, SGMatrix centers_i); - virtual ~CKMeans(); + virtual ~KMeans(); /** @return object name */ virtual const char* get_name() const { return "KMeans"; } @@ -77,7 +77,7 @@ class CKMeans : public CKMeansBase * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** Lloyd's KMeans training method */ diff --git a/src/shogun/clustering/KMeansBase.cpp b/src/shogun/clustering/KMeansBase.cpp index 1769d33d976..bcbf42263ef 100644 --- a/src/shogun/clustering/KMeansBase.cpp +++ b/src/shogun/clustering/KMeansBase.cpp @@ -18,14 +18,14 @@ using namespace shogun; using namespace Eigen; -CKMeansBase::CKMeansBase() -: RandomMixin() +KMeansBase::KMeansBase() +: RandomMixin() { init(); } -CKMeansBase::CKMeansBase(int32_t k_, CDistance* d, bool use_kmpp) -: RandomMixin() +KMeansBase::KMeansBase(int32_t k_, std::shared_ptr d, bool use_kmpp) +: RandomMixin() { init(); k=k_; @@ -33,8 +33,8 @@ CKMeansBase::CKMeansBase(int32_t k_, CDistance* d, bool use_kmpp) use_kmeanspp=use_kmpp; } -CKMeansBase::CKMeansBase(int32_t k_i, CDistance* d_i, SGMatrix centers_i) -: RandomMixin() +KMeansBase::KMeansBase(int32_t k_i, std::shared_ptr d_i, SGMatrix centers_i) +: RandomMixin() { init(); k = k_i; @@ -42,27 +42,27 @@ CKMeansBase::CKMeansBase(int32_t k_i, CDistance* d_i, SGMatrix center set_initial_centers(centers_i); } -CKMeansBase::~CKMeansBase() +KMeansBase::~KMeansBase() { } -void CKMeansBase::set_initial_centers(SGMatrix centers) +void KMeansBase::set_initial_centers(SGMatrix centers) { - CDenseFeatures* lhs=distance->get_lhs()->as>(); + auto lhs=distance->get_lhs()->as>(); dimensions=lhs->get_num_features(); require(centers.num_cols == k, "Expected {} initial cluster centers, got {}", k, centers.num_cols); require(centers.num_rows == dimensions, "Expected {} dimensionional cluster centers, got {}", dimensions, centers.num_rows); mus_initial = centers; - SG_UNREF(lhs); + } -void CKMeansBase::set_random_centers() +void KMeansBase::set_random_centers() { mus.zero(); - CDenseFeatures* lhs= - distance->get_lhs()->as>(); + auto lhs= + distance->get_lhs()->as>(); int32_t lhs_size=lhs->get_num_vectors(); SGVector temp=SGVector(lhs_size); @@ -81,12 +81,9 @@ void CKMeansBase::set_random_centers() } observe>(0, "mus"); - - SG_UNREF(lhs); - } -void CKMeansBase::compute_cluster_variances() +void KMeansBase::compute_cluster_variances() { /* compute the ,,variances'' of the clusters */ for (int32_t i=0; i data) { require(distance, "Distance is not provided"); require( @@ -151,8 +148,8 @@ void CKMeansBase::initialize_training(CFeatures* data) if (data) distance->init(data, data); - CDenseFeatures* lhs= - distance->get_lhs()->as>(); + auto lhs= + distance->get_lhs()->as>(); require(lhs, "Lhs features of distance not provided"); int32_t lhs_size=lhs->get_num_vectors(); @@ -181,32 +178,31 @@ void CKMeansBase::initialize_training(CFeatures* data) { set_random_centers(); } - SG_UNREF(lhs); } -bool CKMeansBase::load(FILE* srcfile) +bool KMeansBase::load(FILE* srcfile) { SG_SET_LOCALE_C; SG_RESET_LOCALE; return false; } -bool CKMeansBase::save(FILE* dstfile) +bool KMeansBase::save(FILE* dstfile) { SG_SET_LOCALE_C; SG_RESET_LOCALE; return false; } -SGMatrix CKMeansBase::get_cluster_centers() const +SGMatrix KMeansBase::get_cluster_centers() const { return mus; } -SGMatrix CKMeansBase::kmeanspp() +SGMatrix KMeansBase::kmeanspp() { int32_t lhs_size; - CDenseFeatures* lhs=distance->get_lhs()->as>(); + auto lhs=distance->get_lhs()->as>(); lhs_size=lhs->get_num_vectors(); SGMatrix centers=SGMatrix(dimensions, k); @@ -227,11 +223,11 @@ SGMatrix CKMeansBase::kmeanspp() default(none) shared(min_dist, mu, lhs_size) \ schedule(static, CPU_CACHE_LINE_SIZE_BYTES) for(int32_t i=0; idistance(i, mu)); + min_dist[i]=Math::sq(distance->distance(i, mu)); #ifdef HAVE_LINALG float64_t sum=linalg::vector_sum(min_dist); #else //HAVE_LINALG - Map eigen_min_dist(min_dist.vector, min_dist.vlen); + Eigen::Map eigen_min_dist(min_dist.vector, min_dist.vlen); float64_t sum=eigen_min_dist.sum(); #endif //HAVE_LINALG int32_t n_rands = 2 + int32_t(std::log(k)); @@ -269,14 +265,14 @@ SGMatrix CKMeansBase::kmeanspp() schedule(static, CPU_CACHE_LINE_SIZE_BYTES) for(int32_t j=0; jdistance(j, new_center)); - temp_min_dist[j]=CMath::min(temp_dist, min_dist[j]); + temp_dist=Math::sq(distance->distance(j, new_center)); + temp_min_dist[j]=Math::min(temp_dist, min_dist[j]); } #ifdef HAVE_LINALG temp_sum=linalg::vector_sum(temp_min_dist); #else //HAVE_LINALG - Map eigen_temp_sum(temp_min_dist.vector, temp_min_dist.vlen); + Eigen::Map eigen_temp_sum(temp_min_dist.vector, temp_min_dist.vlen); temp_sum=eigen_temp_sum.sum(); #endif //HAVE_LINALG if ((temp_sum CKMeansBase::kmeanspp() } distance->reset_precompute(); - SG_UNREF(lhs); + return centers; } -void CKMeansBase::init() +void KMeansBase::init() { max_iter = 300; k = 8; @@ -325,5 +321,5 @@ void CKMeansBase::init() ParameterProperties::HYPER | ParameterProperties::SETTING); SG_ADD(&mus, "mus", "Cluster centers", ParameterProperties::MODEL); - watch_method("cluster_centers", &CKMeansBase::get_cluster_centers); + watch_method("cluster_centers", &KMeansBase::get_cluster_centers); } diff --git a/src/shogun/clustering/KMeansBase.h b/src/shogun/clustering/KMeansBase.h index a43817bb366..b4f0463d92e 100644 --- a/src/shogun/clustering/KMeansBase.h +++ b/src/shogun/clustering/KMeansBase.h @@ -19,16 +19,16 @@ namespace shogun { -class CDistanceMachine; +class DistanceMachine; /** Base Class for different KMeans clustering implementations. */ -class CKMeansBase : public RandomMixin +class KMeansBase : public RandomMixin { public: /** default constructor */ - CKMeansBase(); + KMeansBase(); /** constructor * @@ -36,16 +36,16 @@ class CKMeansBase : public RandomMixin * @param d distance * @param kmeanspp Set to true for using KMeans++ (default false) */ - CKMeansBase(int32_t k, CDistance* d, bool kmeanspp=false); + KMeansBase(int32_t k, std::shared_ptr d, bool kmeanspp=false); /** constructor for supplying initial centers * @param k_i parameter k * @param d_i distance * @param centers_i initial centers for KMeans algorithm */ - CKMeansBase(int32_t k_i, CDistance* d_i, SGMatrix centers_i); + KMeansBase(int32_t k_i, std::shared_ptr d_i, SGMatrix centers_i); - virtual ~CKMeansBase(); + virtual ~KMeansBase(); MACHINE_PROBLEM_TYPE(PT_MULTICLASS) @@ -92,7 +92,7 @@ class CKMeansBase : public RandomMixin protected: /** Initialize training for KMeans algorithms */ - void initialize_training(CFeatures* data=NULL); + void initialize_training(std::shared_ptr data=NULL); /** K-Means++ algorithm to initialize cluster centers * diff --git a/src/shogun/clustering/KMeansMiniBatch.cpp b/src/shogun/clustering/KMeansMiniBatch.cpp index 084946e2cf1..5af5dbf7db2 100644 --- a/src/shogun/clustering/KMeansMiniBatch.cpp +++ b/src/shogun/clustering/KMeansMiniBatch.cpp @@ -22,26 +22,26 @@ using namespace shogun; namespace shogun { -CKMeansMiniBatch::CKMeansMiniBatch():CKMeansBase() +KMeansMiniBatch::KMeansMiniBatch():KMeansBase() { init_mb_params(); } -CKMeansMiniBatch::CKMeansMiniBatch(int32_t k_i, CDistance* d_i, bool use_kmpp_i):CKMeansBase(k_i, d_i, use_kmpp_i) +KMeansMiniBatch::KMeansMiniBatch(int32_t k_i, std::shared_ptr d_i, bool use_kmpp_i):KMeansBase(k_i, d_i, use_kmpp_i) { init_mb_params(); } -CKMeansMiniBatch::CKMeansMiniBatch(int32_t k_i, CDistance* d_i, SGMatrix centers_i):CKMeansBase(k_i, d_i, centers_i) +KMeansMiniBatch::KMeansMiniBatch(int32_t k_i, std::shared_ptr d_i, SGMatrix centers_i):KMeansBase(k_i, d_i, centers_i) { init_mb_params(); } -CKMeansMiniBatch::~CKMeansMiniBatch() +KMeansMiniBatch::~KMeansMiniBatch() { } -void CKMeansMiniBatch::minibatch_KMeans() +void KMeansMiniBatch::minibatch_KMeans() { require(batch_size>0, "batch size not set to positive value. Current batch size {} ", batch_size); @@ -50,10 +50,10 @@ void CKMeansMiniBatch::minibatch_KMeans() "iterations {} ", max_iter); - CDenseFeatures* lhs= - distance->get_lhs()->as>(); - auto rhs_mus=some>(mus); - CFeatures* rhs_cache=distance->replace_rhs(rhs_mus); + auto lhs= + distance->get_lhs()->as>(); + auto rhs_mus=std::make_shared>(mus); + auto rhs_cache=distance->replace_rhs(rhs_mus); int32_t XSize=lhs->get_num_vectors(); int32_t dims=lhs->get_num_features(); @@ -97,11 +97,11 @@ void CKMeansMiniBatch::minibatch_KMeans() mus = rhs_mus->get_feature_matrix(); observe>(i, "mus"); } - SG_UNREF(lhs); + distance->replace_rhs(rhs_cache); } -SGVector CKMeansMiniBatch::mbchoose_rand(int32_t b, int32_t num) +SGVector KMeansMiniBatch::mbchoose_rand(int32_t b, int32_t num) { SGVector chosen=SGVector(num); SGVector ret=SGVector(b); @@ -121,16 +121,16 @@ SGVector CKMeansMiniBatch::mbchoose_rand(int32_t b, int32_t num) return ret; } - void CKMeansMiniBatch::init_mb_params() - { - batch_size = 100; +void KMeansMiniBatch::init_mb_params() +{ + batch_size = 100; - SG_ADD( - &batch_size, "batch_size", "batch size for mini-batch KMeans", - ParameterProperties::HYPER | ParameterProperties::SETTING); - } + SG_ADD( + &batch_size, "batch_size", "batch size for mini-batch KMeans", + ParameterProperties::HYPER | ParameterProperties::SETTING); +} -bool CKMeansMiniBatch::train_machine(CFeatures* data) +bool KMeansMiniBatch::train_machine(std::shared_ptr data) { initialize_training(data); minibatch_KMeans(); diff --git a/src/shogun/clustering/KMeansMiniBatch.h b/src/shogun/clustering/KMeansMiniBatch.h index 1b7b50e9a50..55294885536 100644 --- a/src/shogun/clustering/KMeansMiniBatch.h +++ b/src/shogun/clustering/KMeansMiniBatch.h @@ -17,14 +17,14 @@ namespace shogun { -class CKMeansBase; +class KMeansBase; /** Class for the mini batch KMeans */ -class CKMeansMiniBatch : public CKMeansBase +class KMeansMiniBatch : public KMeansBase { public: /** default constructor */ - CKMeansMiniBatch(); + KMeansMiniBatch(); /** constructor * @@ -32,16 +32,16 @@ class CKMeansMiniBatch : public CKMeansBase * @param d distance * @param kmeanspp true for using KMeans++ (default false) */ - CKMeansMiniBatch(int32_t k, CDistance* d, bool kmeanspp=false); + KMeansMiniBatch(int32_t k, std::shared_ptr d, bool kmeanspp=false); /** constructor for supplying initial centers * @param k_i parameter k * @param d_i distance * @param centers_i initial centers for KMeans aloverride private method c++gorithm */ - CKMeansMiniBatch(int32_t k_i, CDistance* d_i, SGMatrix centers_i); + KMeansMiniBatch(int32_t k_i, std::shared_ptr d_i, SGMatrix centers_i); - virtual ~CKMeansMiniBatch(); + virtual ~KMeansMiniBatch(); /** @return object name */ virtual const char* get_name() const @@ -59,7 +59,7 @@ class CKMeansMiniBatch : public CKMeansBase * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** mini-batch KMeans training method */ diff --git a/src/shogun/converter/Converter.h b/src/shogun/converter/Converter.h index 6989057d1ce..8d26959dfee 100644 --- a/src/shogun/converter/Converter.h +++ b/src/shogun/converter/Converter.h @@ -20,19 +20,19 @@ namespace shogun /** @brief class Converter used to convert data * */ -class CConverter : public CTransformer +class Converter : public Transformer { public: /** constructor */ - CConverter() : CTransformer(){}; + Converter() : Transformer(){}; /** destructor */ - virtual ~CConverter() {}; + virtual ~Converter() {}; /** get name */ virtual const char* get_name() const { return "Converter"; } - virtual CFeatures* transform(CFeatures* features, bool inplace = true) = 0; + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true) = 0; }; } #endif /* CONVERTER_H_ */ diff --git a/src/shogun/converter/DiffusionMaps.cpp b/src/shogun/converter/DiffusionMaps.cpp index ea36973b351..99a28786a3f 100644 --- a/src/shogun/converter/DiffusionMaps.cpp +++ b/src/shogun/converter/DiffusionMaps.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, * Heiko Strathmann, Fernando Iglesias */ @@ -13,72 +13,72 @@ using namespace shogun; -CDiffusionMaps::CDiffusionMaps() : - CEmbeddingConverter() +DiffusionMaps::DiffusionMaps() : + EmbeddingConverter() { m_t = 10; m_width = 1.0; - set_distance(new CEuclideanDistance()); + set_distance(std::make_shared()); init(); } -void CDiffusionMaps::init() +void DiffusionMaps::init() { SG_ADD(&m_t, "t", "number of steps", ParameterProperties::HYPER); SG_ADD(&m_width, "width", "gaussian kernel width", ParameterProperties::HYPER); } -CDiffusionMaps::~CDiffusionMaps() +DiffusionMaps::~DiffusionMaps() { } -void CDiffusionMaps::set_t(int32_t t) +void DiffusionMaps::set_t(int32_t t) { m_t = t; } -int32_t CDiffusionMaps::get_t() const +int32_t DiffusionMaps::get_t() const { return m_t; } -void CDiffusionMaps::set_width(float64_t width) +void DiffusionMaps::set_width(float64_t width) { m_width = width; } -float64_t CDiffusionMaps::get_width() const +float64_t DiffusionMaps::get_width() const { return m_width; } -const char* CDiffusionMaps::get_name() const +const char* DiffusionMaps::get_name() const { return "DiffusionMaps"; }; -CFeatures* CDiffusionMaps::transform(CFeatures* features, bool inplace) +std::shared_ptr DiffusionMaps::transform(std::shared_ptr features, bool inplace) { ASSERT(features) // shorthand for simplefeatures - SG_REF(features); + // compute distance matrix ASSERT(m_distance) m_distance->init(features,features); - CDenseFeatures* embedding = embed_distance(m_distance); + auto embedding = embed_distance(m_distance); m_distance->cleanup(); - SG_UNREF(features); - return (CFeatures*)embedding; + + return std::static_pointer_cast(embedding); } -CDenseFeatures* CDiffusionMaps::embed_distance(CDistance* distance) +std::shared_ptr> DiffusionMaps::embed_distance(std::shared_ptr distance) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_timesteps = m_t; parameters.gaussian_kernel_width = m_width; parameters.method = SHOGUN_DIFFUSION_MAPS; parameters.target_dimension = m_target_dim; - parameters.distance = distance; + parameters.distance = distance.get(); return tapkee_embed(parameters); } diff --git a/src/shogun/converter/DiffusionMaps.h b/src/shogun/converter/DiffusionMaps.h index 1f086cbc1a9..f9b24e77535 100644 --- a/src/shogun/converter/DiffusionMaps.h +++ b/src/shogun/converter/DiffusionMaps.h @@ -14,8 +14,8 @@ namespace shogun { -class CFeatures; -class CKernel; +class Features; +class Kernel; /** @brief class DiffusionMaps used to preprocess given data * using Diffusion Maps dimensionality @@ -32,26 +32,26 @@ class CKernel; * sg('create_converter','diffusion_maps',t,width); * */ -class CDiffusionMaps: public CEmbeddingConverter +class DiffusionMaps: public EmbeddingConverter { public: /** constructor */ - CDiffusionMaps(); + DiffusionMaps(); /** destructor */ - virtual ~CDiffusionMaps(); + virtual ~DiffusionMaps(); /** apply preprocessor to features * @param features */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** embed distance * @param distance to use for embedding * @return embedding simple features */ - virtual CDenseFeatures* embed_distance(CDistance* distance); + virtual std::shared_ptr> embed_distance(std::shared_ptr distance); /** setter for t parameter * @param t t value diff --git a/src/shogun/converter/EmbeddingConverter.cpp b/src/shogun/converter/EmbeddingConverter.cpp index 855a17a1577..9aafeb01649 100644 --- a/src/shogun/converter/EmbeddingConverter.cpp +++ b/src/shogun/converter/EmbeddingConverter.cpp @@ -13,62 +13,62 @@ using namespace shogun; namespace shogun { -CEmbeddingConverter::CEmbeddingConverter() -: CConverter() +EmbeddingConverter::EmbeddingConverter() +: Converter() { m_target_dim = 1; - m_distance = new CEuclideanDistance(); - SG_REF(m_distance); - m_kernel = new CLinearKernel(); - SG_REF(m_kernel); + m_distance = std::make_shared(); + + m_kernel = std::make_shared(); + init(); } -CEmbeddingConverter::~CEmbeddingConverter() +EmbeddingConverter::~EmbeddingConverter() { - SG_UNREF(m_distance); - SG_UNREF(m_kernel); + + } -void CEmbeddingConverter::set_target_dim(int32_t dim) +void EmbeddingConverter::set_target_dim(int32_t dim) { ASSERT(dim>0) m_target_dim = dim; } -int32_t CEmbeddingConverter::get_target_dim() const +int32_t EmbeddingConverter::get_target_dim() const { return m_target_dim; } -void CEmbeddingConverter::set_distance(CDistance* distance) +void EmbeddingConverter::set_distance(std::shared_ptr distance) { - SG_REF(distance); - SG_UNREF(m_distance); + + m_distance = distance; } -CDistance* CEmbeddingConverter::get_distance() const +std::shared_ptr EmbeddingConverter::get_distance() const { - SG_REF(m_distance); + return m_distance; } -void CEmbeddingConverter::set_kernel(CKernel* kernel) +void EmbeddingConverter::set_kernel(std::shared_ptr kernel) { - SG_REF(kernel); - SG_UNREF(m_kernel); + + m_kernel = kernel; } -CKernel* CEmbeddingConverter::get_kernel() const +std::shared_ptr EmbeddingConverter::get_kernel() const { - SG_REF(m_kernel); + return m_kernel; } -void CEmbeddingConverter::init() +void EmbeddingConverter::init() { SG_ADD(&m_target_dim, "target_dim", "target dimensionality of preprocessor", ParameterProperties::HYPER); diff --git a/src/shogun/converter/EmbeddingConverter.h b/src/shogun/converter/EmbeddingConverter.h index c267caba1e6..4207043e2be 100644 --- a/src/shogun/converter/EmbeddingConverter.h +++ b/src/shogun/converter/EmbeddingConverter.h @@ -18,30 +18,30 @@ namespace shogun { -class CFeatures; -class CDistance; -class CKernel; +class Features; +class Distance; +class Kernel; /** @brief class EmbeddingConverter (part of the Efficient Dimensionality * Reduction Toolkit) used to construct embeddings of * features, e.g. construct dense numeric embedding of string features */ -class CEmbeddingConverter: public CConverter +class EmbeddingConverter: public Converter { public: /** constructor */ - CEmbeddingConverter(); + EmbeddingConverter(); /** destructor */ - virtual ~CEmbeddingConverter(); + virtual ~EmbeddingConverter(); /** Apply transformation to features. In-place mode is not supported for * Tapkee converters. * @param features features to embed * @return embedding dense real features */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true) = 0; + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true) = 0; /** setter for target dimension * @param dim target dimension @@ -56,22 +56,22 @@ class CEmbeddingConverter: public CConverter /** setter for distance * @param distance distance to set */ - void set_distance(CDistance* distance); + void set_distance(std::shared_ptr distance); /** getter for distance * @return distance */ - CDistance* get_distance() const; + std::shared_ptr get_distance() const; /** setter for kernel * @param kernel kernel to set */ - void set_kernel(CKernel* kernel); + void set_kernel(std::shared_ptr kernel); /** getter for kernel * @return kernel */ - CKernel* get_kernel() const; + std::shared_ptr get_kernel() const; virtual const char* get_name() const { return "EmbeddingConverter"; }; @@ -86,10 +86,10 @@ class CEmbeddingConverter: public CConverter int32_t m_target_dim; /** distance to be used */ - CDistance* m_distance; + std::shared_ptr m_distance; /** kernel to be used */ - CKernel* m_kernel; + std::shared_ptr m_kernel; }; } diff --git a/src/shogun/converter/FactorAnalysis.cpp b/src/shogun/converter/FactorAnalysis.cpp index 5d9973cf2e5..61946f1100c 100644 --- a/src/shogun/converter/FactorAnalysis.cpp +++ b/src/shogun/converter/FactorAnalysis.cpp @@ -10,8 +10,8 @@ using namespace shogun; -CFactorAnalysis::CFactorAnalysis() : - CEmbeddingConverter() +FactorAnalysis::FactorAnalysis() : + EmbeddingConverter() { // Sentinel value, it will be set appropriately if not modified by set_max_iteration m_max_iteration = 0; @@ -19,50 +19,50 @@ CFactorAnalysis::CFactorAnalysis() : init(); } -void CFactorAnalysis::init() +void FactorAnalysis::init() { SG_ADD(&m_max_iteration, "max_iteration", "maximum number of iterations"); SG_ADD(&m_epsilon, "epsilon", "convergence parameter"); } -CFactorAnalysis::~CFactorAnalysis() +FactorAnalysis::~FactorAnalysis() { } -const char* CFactorAnalysis::get_name() const +const char* FactorAnalysis::get_name() const { return "FactorAnalysis"; } -void CFactorAnalysis::set_max_iteration(const int32_t max_iteration) +void FactorAnalysis::set_max_iteration(const int32_t max_iteration) { m_max_iteration = max_iteration; } -int32_t CFactorAnalysis::get_max_iteration() const +int32_t FactorAnalysis::get_max_iteration() const { return m_max_iteration; } -void CFactorAnalysis::set_epsilon(const float64_t epsilon) +void FactorAnalysis::set_epsilon(const float64_t epsilon) { m_epsilon = epsilon; } -float64_t CFactorAnalysis::get_epsilon() const +float64_t FactorAnalysis::get_epsilon() const { return m_epsilon; } -CFeatures* CFactorAnalysis::transform(CFeatures* features, bool inplace) +std::shared_ptr FactorAnalysis::transform(std::shared_ptr features, bool inplace) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.max_iteration = m_max_iteration; - parameters.features = (CDotFeatures*)features; + parameters.features = (DotFeatures*)features.get(); parameters.fa_epsilon = m_epsilon; parameters.method = SHOGUN_FACTOR_ANALYSIS; parameters.target_dimension = m_target_dim; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + return tapkee_embed(parameters); + } diff --git a/src/shogun/converter/FactorAnalysis.h b/src/shogun/converter/FactorAnalysis.h index 94a70ce21c9..a80b660b2b8 100644 --- a/src/shogun/converter/FactorAnalysis.h +++ b/src/shogun/converter/FactorAnalysis.h @@ -43,15 +43,15 @@ namespace shogun * (http://www.mendeley.com/catalog/general-intelligence-objectively-determined-measured/) * */ -class CFactorAnalysis : public CEmbeddingConverter +class FactorAnalysis : public EmbeddingConverter { public: /** constructor */ - CFactorAnalysis(); + FactorAnalysis(); /** destructor */ - virtual ~CFactorAnalysis(); + virtual ~FactorAnalysis(); /** get name */ virtual const char* get_name() const; @@ -60,7 +60,7 @@ class CFactorAnalysis : public CEmbeddingConverter * * @param features features to embed */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** setter for the maximum number of iterations * @@ -99,7 +99,7 @@ class CFactorAnalysis : public CEmbeddingConverter /** convergence parameter */ float64_t m_epsilon; -}; /* class CFactorAnalysis */ +}; /* class FactorAnalysis */ } /* namespace shogun */ diff --git a/src/shogun/converter/HashedDocConverter.cpp b/src/shogun/converter/HashedDocConverter.cpp index 63d803776f8..354b4699532 100644 --- a/src/shogun/converter/HashedDocConverter.cpp +++ b/src/shogun/converter/HashedDocConverter.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -16,29 +15,29 @@ using namespace shogun; namespace shogun { -CHashedDocConverter::CHashedDocConverter() : CConverter() +HashedDocConverter::HashedDocConverter() : Converter() { init(NULL, 16, false, 1, 0); } -CHashedDocConverter::CHashedDocConverter(int32_t hash_bits, bool normalize, - int32_t n_grams, int32_t skips) : CConverter() +HashedDocConverter::HashedDocConverter(int32_t hash_bits, bool normalize, + int32_t n_grams, int32_t skips) : Converter() { init(NULL, hash_bits, normalize, n_grams, skips); } -CHashedDocConverter::CHashedDocConverter(CTokenizer* tzer, - int32_t hash_bits, bool normalize, int32_t n_grams, int32_t skips) : CConverter() +HashedDocConverter::HashedDocConverter(std::shared_ptr tzer, + int32_t hash_bits, bool normalize, int32_t n_grams, int32_t skips) : Converter() { init(tzer, hash_bits, normalize, n_grams, skips); } -CHashedDocConverter::~CHashedDocConverter() +HashedDocConverter::~HashedDocConverter() { - SG_UNREF(tokenizer); + } -void CHashedDocConverter::init(CTokenizer* tzer, int32_t hash_bits, bool normalize, +void HashedDocConverter::init(std::shared_ptr tzer, int32_t hash_bits, bool normalize, int32_t n_grams, int32_t skips) { num_bits = hash_bits; @@ -48,7 +47,7 @@ void CHashedDocConverter::init(CTokenizer* tzer, int32_t hash_bits, bool normali if (tzer==NULL) { - CDelimiterTokenizer* tk = new CDelimiterTokenizer(); + auto tk = std::make_shared(); tk->delimiters[(uint8_t) ' '] = 1; tk->delimiters[(uint8_t) '\t'] = 1; tokenizer = tk; @@ -56,7 +55,6 @@ void CHashedDocConverter::init(CTokenizer* tzer, int32_t hash_bits, bool normali else tokenizer = tzer; - SG_REF(tokenizer); SG_ADD(&num_bits, "num_bits", "Number of bits of the hash"); SG_ADD(&ngrams, "ngrams", "Number of consecutive tokens"); SG_ADD(&tokens_to_skip, "tokens_to_skip", "Number of tokens to skip"); @@ -64,22 +62,22 @@ void CHashedDocConverter::init(CTokenizer* tzer, int32_t hash_bits, bool normali SG_ADD(&tokenizer, "tokenizer", "Tokenizer"); } -const char* CHashedDocConverter::get_name() const +const char* HashedDocConverter::get_name() const { return "HashedDocConverter"; } -CFeatures* CHashedDocConverter::transform(CFeatures* features, bool inplace) +std::shared_ptr HashedDocConverter::transform(std::shared_ptr features, bool inplace) { ASSERT(features); if (strcmp(features->get_name(), "StringFeatures")!=0) error( - "CHashedConverter::transform() : CFeatures object passed is " - "not of type CStringFeatures."); + "HashedConverter::transform() : Features object passed is " + "not of type StringFeatures."); - CStringFeatures* s_features = (CStringFeatures*) features; + auto s_features = std::static_pointer_cast>(features); - int32_t dim = CMath::pow(2, num_bits); + int32_t dim = Math::pow(2, num_bits); SGSparseMatrix matrix(dim,features->get_num_vectors()); for (index_t vec_idx=0; vec_idxget_num_vectors(); vec_idx++) { @@ -88,15 +86,16 @@ CFeatures* CHashedDocConverter::transform(CFeatures* features, bool inplace) s_features->free_feature_vector(doc, vec_idx); } - return (CFeatures*) new CSparseFeatures(matrix); + return std::make_shared>(matrix); } -SGSparseVector CHashedDocConverter::apply(SGVector document) +SGSparseVector HashedDocConverter::apply(SGVector document) { ASSERT(document.size()>0) const int32_t array_size = 1024*1024; /** the array will contain all the hashes generated from the tokens */ - CDynamicArray hashed_indices(array_size); + std::vector hashed_indices; + hashed_indices.reserve(array_size); /** this vector will maintain the current n+k active tokens * in a circular manner */ @@ -116,7 +115,7 @@ SGSparseVector CHashedDocConverter::apply(SGVector document) while (hashes_endhas_next()) { index_t end = tokenizer->next_token_idx(token_start); - uint32_t token_hash = CHash::MurmurHash3((uint8_t* ) &document.vector[token_start], + uint32_t token_hash = Hash::MurmurHash3((uint8_t* ) &document.vector[token_start], end-token_start, seed); cached_hashes[hashes_end++] = token_hash; } @@ -125,15 +124,15 @@ SGSparseVector CHashedDocConverter::apply(SGVector document) while (tokenizer->has_next()) { index_t end = tokenizer->next_token_idx(token_start); - uint32_t token_hash = CHash::MurmurHash3((uint8_t* ) &document.vector[token_start], + uint32_t token_hash = Hash::MurmurHash3((uint8_t* ) &document.vector[token_start], end-token_start, seed); cached_hashes[hashes_end] = token_hash; - CHashedDocConverter::generate_ngram_hashes(cached_hashes, hashes_start, len, + HashedDocConverter::generate_ngram_hashes(cached_hashes, hashes_start, len, ngram_indices, num_bits, ngrams, tokens_to_skip); for (index_t i=0; i CHashedDocConverter::apply(SGVector document) while (hashes_start!=hashes_end) { len--; - index_t max_idx = CHashedDocConverter::generate_ngram_hashes(cached_hashes, hashes_start, + index_t max_idx = HashedDocConverter::generate_ngram_hashes(cached_hashes, hashes_start, len, ngram_indices, num_bits, ngrams, tokens_to_skip); for (index_t i=0; i CHashedDocConverter::apply(SGVector document) return sparse_doc_rep; } -SGSparseVector CHashedDocConverter::create_hashed_representation(CDynamicArray& hashed_indices) +SGSparseVector HashedDocConverter::create_hashed_representation(std::vector& hashed_indices) { int32_t num_nnz_features = count_distinct_indices(hashed_indices); SGSparseVector sparse_doc_rep(num_nnz_features); index_t sparse_idx = 0; - for (index_t i=0; i CHashedDocConverter::create_hashed_representation(CDyn return sparse_doc_rep; } -index_t CHashedDocConverter::generate_ngram_hashes(SGVector& hashes, index_t hashes_start, +index_t HashedDocConverter::generate_ngram_hashes(SGVector& hashes, index_t hashes_start, index_t len, SGVector& ngram_hashes, int32_t num_bits, int32_t ngrams, int32_t tokens_to_skip) { index_t h_idx = 0; @@ -218,16 +217,16 @@ index_t CHashedDocConverter::generate_ngram_hashes(SGVector& hashes, i return h_idx; } -int32_t CHashedDocConverter::count_distinct_indices(CDynamicArray& hashed_indices) +int32_t HashedDocConverter::count_distinct_indices(std::vector& hashed_indices) { - CMath::qsort(hashed_indices.get_array(), hashed_indices.get_num_elements()); + std::sort(hashed_indices.begin(), hashed_indices.end()); /** Counting nnz features */ int32_t num_nnz_features = 0; - for (index_t i=0; i& has return num_nnz_features; } -void CHashedDocConverter::set_normalization(bool normalize) +void HashedDocConverter::set_normalization(bool normalize) { should_normalize = normalize; } -void CHashedDocConverter::set_k_skip_n_grams(int32_t k, int32_t n) +void HashedDocConverter::set_k_skip_n_grams(int32_t k, int32_t n) { tokens_to_skip = k; ngrams = n; diff --git a/src/shogun/converter/HashedDocConverter.h b/src/shogun/converter/HashedDocConverter.h index 1ae0aa0343d..407d079153c 100644 --- a/src/shogun/converter/HashedDocConverter.h +++ b/src/shogun/converter/HashedDocConverter.h @@ -17,10 +17,10 @@ namespace shogun { -class CFeatures; -class CTokenizer; -class CConverter; -template class CSparseFeatures; +class Features; +class Tokenizer; +class Converter; +template class SparseFeatures; /** @brief This class can be used to convert a document collection contained in a CStringFeatures * object where each document is stored as a single vector into a hashed Bag-of-Words representation. @@ -33,11 +33,11 @@ template class CSparseFeatures; * Eg. for the tokens ["a", "b", "c", "d"], with n_grams = 2 and skips = 2, one would get the following combinations : * ["a", "ab", "ac" (skipped 1), "ad" (skipped 2), "b", "bc", "bd" (skipped 1), "c", "cd", "d"]. */ -class CHashedDocConverter : public CConverter +class HashedDocConverter : public Converter { public: /** Default constructor */ - CHashedDocConverter(); + HashedDocConverter(); /** Constructor * Creates tokens on whitespace @@ -47,7 +47,7 @@ class CHashedDocConverter : public CConverter * @param n_grams the max number of tokens to consider when combining tokens * @param skips the max number of tokens to skip when combining tokens */ - CHashedDocConverter(int32_t hash_bits, bool normalize = false, int32_t n_grams = 1, int32_t skips = 0); + HashedDocConverter(int32_t hash_bits, bool normalize = false, int32_t n_grams = 1, int32_t skips = 0); /** Constructor * @@ -57,18 +57,18 @@ class CHashedDocConverter : public CConverter * @param n_grams the max number of tokens to consider when combining tokens * @param skips the max number of tokens to skip when combining tokens */ - CHashedDocConverter(CTokenizer* tzer, int32_t hash_bits, bool normalize = false, int32_t n_grams = 1, + HashedDocConverter(std::shared_ptr tzer, int32_t hash_bits, bool normalize = false, int32_t n_grams = 1, int32_t skips = 0); /** Destructor */ - virtual ~CHashedDocConverter(); + virtual ~HashedDocConverter(); /** Hashes each string contained in features * * @param features the strings to be hashed. Must be an instance of CStringFeatures. - * @return a CSparseFeatures object containing the hashes of the strings. + * @return a SparseFeatures object containing the hashes of the strings. */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** Hashes the tokens contained in document * @@ -118,7 +118,7 @@ class CHashedDocConverter : public CConverter protected: /** init */ - void init(CTokenizer* tzer, int32_t d, bool normalize, int32_t n_grams, int32_t skips); + void init(std::shared_ptr tzer, int32_t d, bool normalize, int32_t n_grams, int32_t skips); /** This method takes a dynamic array as an argument, sorts it and returns the number * of the distinct elements(indices here) in the array. @@ -126,7 +126,7 @@ class CHashedDocConverter : public CConverter * @param hashed_indices the array to sort and count elements * @return the number of distinct elements */ - int32_t count_distinct_indices(CDynamicArray& hashed_indices); + int32_t count_distinct_indices(std::vector& hashed_indices); /** This method takes the dynamic array containing all the hashed indices of a document and returns a compact * sparse representation with each index found and with the count of such index @@ -134,7 +134,7 @@ class CHashedDocConverter : public CConverter * @param hashed_indices the array containing the hashed indices * @return the compact hashed document representation */ - SGSparseVector create_hashed_representation(CDynamicArray& hashed_indices); + SGSparseVector create_hashed_representation(std::vector& hashed_indices); protected: @@ -142,7 +142,7 @@ class CHashedDocConverter : public CConverter int32_t num_bits; /** the tokenizer */ - CTokenizer* tokenizer; + std::shared_ptr tokenizer; /** whether to normalize or not */ bool should_normalize; diff --git a/src/shogun/converter/HessianLocallyLinearEmbedding.cpp b/src/shogun/converter/HessianLocallyLinearEmbedding.cpp index a6c9224a4a2..7de969c4431 100644 --- a/src/shogun/converter/HessianLocallyLinearEmbedding.cpp +++ b/src/shogun/converter/HessianLocallyLinearEmbedding.cpp @@ -11,32 +11,31 @@ using namespace shogun; -CHessianLocallyLinearEmbedding::CHessianLocallyLinearEmbedding() : - CLocallyLinearEmbedding() +HessianLocallyLinearEmbedding::HessianLocallyLinearEmbedding() : + LocallyLinearEmbedding() { } -CHessianLocallyLinearEmbedding::~CHessianLocallyLinearEmbedding() +HessianLocallyLinearEmbedding::~HessianLocallyLinearEmbedding() { } -const char* CHessianLocallyLinearEmbedding::get_name() const +const char* HessianLocallyLinearEmbedding::get_name() const { return "HessianLocallyLinearEmbedding"; }; -CFeatures* -CHessianLocallyLinearEmbedding::transform(CFeatures* features, bool inplace) +std::shared_ptr +HessianLocallyLinearEmbedding::transform(std::shared_ptr features, bool inplace) { - CKernel* kernel = new CLinearKernel((CDotFeatures*)features,(CDotFeatures*)features); + auto dot_feats = std::static_pointer_cast(features); + std::shared_ptr kernel = std::make_shared(dot_feats, dot_feats); TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.eigenshift = m_nullspace_shift; parameters.method = SHOGUN_HESSIAN_LOCALLY_LINEAR_EMBEDDING; parameters.target_dimension = m_target_dim; - parameters.kernel = kernel; - CDenseFeatures* embedding = tapkee_embed(parameters); - SG_UNREF(kernel); - return embedding; + parameters.kernel = kernel.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/HessianLocallyLinearEmbedding.h b/src/shogun/converter/HessianLocallyLinearEmbedding.h index 35596ee3e92..249c5f74170 100644 --- a/src/shogun/converter/HessianLocallyLinearEmbedding.h +++ b/src/shogun/converter/HessianLocallyLinearEmbedding.h @@ -14,8 +14,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class HessianLocallyLinearEmbedding used to preprocess * data using Hessian Locally Linear Embedding algorithm as described in @@ -34,21 +34,21 @@ class CDistance; * sg('create_converter','hlle',k); * */ -class CHessianLocallyLinearEmbedding: public CLocallyLinearEmbedding +class HessianLocallyLinearEmbedding: public LocallyLinearEmbedding { public: /** constructor */ - CHessianLocallyLinearEmbedding(); + HessianLocallyLinearEmbedding(); /** destructor */ - virtual ~CHessianLocallyLinearEmbedding(); + virtual ~HessianLocallyLinearEmbedding(); /** get name */ virtual const char* get_name() const; /** transform */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); }; } diff --git a/src/shogun/converter/Isomap.cpp b/src/shogun/converter/Isomap.cpp index 9a9755cc120..6e1e08d9659 100644 --- a/src/shogun/converter/Isomap.cpp +++ b/src/shogun/converter/Isomap.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, * Heiko Strathmann, Bjoern Esser */ @@ -12,39 +12,39 @@ using namespace shogun; -CIsomap::CIsomap() : CMultidimensionalScaling() +Isomap::Isomap() : MultidimensionalScaling() { m_k = 3; init(); } -void CIsomap::init() +void Isomap::init() { SG_ADD(&m_k, "k", "number of neighbors", ParameterProperties::HYPER); } -CIsomap::~CIsomap() +Isomap::~Isomap() { } -void CIsomap::set_k(int32_t k) +void Isomap::set_k(int32_t k) { ASSERT(k>0) m_k = k; } -int32_t CIsomap::get_k() const +int32_t Isomap::get_k() const { return m_k; } -const char* CIsomap::get_name() const +const char* Isomap::get_name() const { return "Isomap"; } -CDenseFeatures* CIsomap::embed_distance(CDistance* distance) +std::shared_ptr> Isomap::embed_distance(std::shared_ptr distance) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; if (m_landmark) @@ -58,8 +58,7 @@ CDenseFeatures* CIsomap::embed_distance(CDistance* distance) } parameters.n_neighbors = m_k; parameters.target_dimension = m_target_dim; - parameters.distance = distance; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + parameters.distance = distance.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/Isomap.h b/src/shogun/converter/Isomap.h old mode 100755 new mode 100644 index c06804d8724..9af7a1f978f --- a/src/shogun/converter/Isomap.h +++ b/src/shogun/converter/Isomap.h @@ -16,8 +16,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief The Isomap class is used to embed data using Isomap algorithm * as described in: @@ -51,15 +51,15 @@ class CDistance; * cf. https://en.wikipedia.org/wiki/Isomap * */ -class CIsomap: public CMultidimensionalScaling +class Isomap: public MultidimensionalScaling { public: /* constructor */ - CIsomap(); + Isomap(); /* destructor */ - virtual ~CIsomap(); + virtual ~Isomap(); /** get name */ const char* get_name() const; @@ -75,7 +75,7 @@ class CIsomap: public CMultidimensionalScaling int32_t get_k() const; /** embed distance */ - virtual CDenseFeatures* embed_distance(CDistance* distance); + virtual std::shared_ptr> embed_distance(std::shared_ptr distance); /// HELPERS protected: diff --git a/src/shogun/converter/KernelLocallyLinearEmbedding.cpp b/src/shogun/converter/KernelLocallyLinearEmbedding.cpp index bc2edd51cbc..82fa223b818 100644 --- a/src/shogun/converter/KernelLocallyLinearEmbedding.cpp +++ b/src/shogun/converter/KernelLocallyLinearEmbedding.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, * Fernando Iglesias, Viktor Gal, Evan Shelhamer */ @@ -11,31 +11,31 @@ using namespace shogun; -CKernelLocallyLinearEmbedding::CKernelLocallyLinearEmbedding() : - CLocallyLinearEmbedding() +KernelLocallyLinearEmbedding::KernelLocallyLinearEmbedding() : + LocallyLinearEmbedding() { } -CKernelLocallyLinearEmbedding::CKernelLocallyLinearEmbedding(CKernel* kernel) : - CLocallyLinearEmbedding() +KernelLocallyLinearEmbedding::KernelLocallyLinearEmbedding(std::shared_ptr kernel) : + LocallyLinearEmbedding() { set_kernel(kernel); } -const char* CKernelLocallyLinearEmbedding::get_name() const +const char* KernelLocallyLinearEmbedding::get_name() const { return "KernelLocallyLinearEmbedding"; }; -CKernelLocallyLinearEmbedding::~CKernelLocallyLinearEmbedding() +KernelLocallyLinearEmbedding::~KernelLocallyLinearEmbedding() { } -CFeatures* -CKernelLocallyLinearEmbedding::transform(CFeatures* features, bool inplace) +std::shared_ptr +KernelLocallyLinearEmbedding::transform(std::shared_ptr features, bool inplace) { ASSERT(features) - SG_REF(features); + // get dimensionality and number of vectors of data int32_t N = features->get_num_vectors(); @@ -46,21 +46,20 @@ CKernelLocallyLinearEmbedding::transform(CFeatures* features, bool inplace) // compute kernel matrix ASSERT(m_kernel) m_kernel->init(features,features); - CDenseFeatures* embedding = embed_kernel(m_kernel); + auto embedding = embed_kernel(m_kernel); m_kernel->cleanup(); - SG_UNREF(features); - return (CFeatures*)embedding; + + return embedding; } -CDenseFeatures* CKernelLocallyLinearEmbedding::embed_kernel(CKernel* kernel) +std::shared_ptr> KernelLocallyLinearEmbedding::embed_kernel(std::shared_ptr kernel) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.eigenshift = m_nullspace_shift; parameters.method = SHOGUN_KERNEL_LOCALLY_LINEAR_EMBEDDING; parameters.target_dimension = m_target_dim; - parameters.kernel = kernel; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + parameters.kernel = kernel.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/KernelLocallyLinearEmbedding.h b/src/shogun/converter/KernelLocallyLinearEmbedding.h index 04d3596928e..82a17fa649b 100644 --- a/src/shogun/converter/KernelLocallyLinearEmbedding.h +++ b/src/shogun/converter/KernelLocallyLinearEmbedding.h @@ -15,8 +15,8 @@ namespace shogun { -class CFeatures; -class CKernel; +class Features; +class Kernel; /** @brief class KernelLocallyLinearEmbedding used to construct embeddings * of data using kernel formulation of Locally Linear Embedding algorithm as @@ -36,28 +36,28 @@ class CKernel; * Uses the Tapkee library code. * */ -class CKernelLocallyLinearEmbedding: public CLocallyLinearEmbedding +class KernelLocallyLinearEmbedding: public LocallyLinearEmbedding { public: /** constructor */ - CKernelLocallyLinearEmbedding(); + KernelLocallyLinearEmbedding(); /** constructor * @param kernel kernel to be used */ - CKernelLocallyLinearEmbedding(CKernel* kernel); + KernelLocallyLinearEmbedding(std::shared_ptr kernel); /** destructor */ - virtual ~CKernelLocallyLinearEmbedding(); + virtual ~KernelLocallyLinearEmbedding(); /** transform */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** embed kernel (kernel should be inited) * @param kernel kernel to construct embed */ - CDenseFeatures* embed_kernel(CKernel* kernel); + std::shared_ptr> embed_kernel(std::shared_ptr kernel); /** get name */ virtual const char* get_name() const; diff --git a/src/shogun/converter/LaplacianEigenmaps.cpp b/src/shogun/converter/LaplacianEigenmaps.cpp index 44929e60813..019c828a62f 100644 --- a/src/shogun/converter/LaplacianEigenmaps.cpp +++ b/src/shogun/converter/LaplacianEigenmaps.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, * Heiko Strathmann */ @@ -12,8 +12,8 @@ using namespace shogun; -CLaplacianEigenmaps::CLaplacianEigenmaps() : - CEmbeddingConverter() +LaplacianEigenmaps::LaplacianEigenmaps() : + EmbeddingConverter() { m_k = 3; m_tau = 1.0; @@ -21,46 +21,46 @@ CLaplacianEigenmaps::CLaplacianEigenmaps() : init(); } -void CLaplacianEigenmaps::init() +void LaplacianEigenmaps::init() { SG_ADD(&m_k, "k", "number of neighbors", ParameterProperties::HYPER); SG_ADD(&m_tau, "tau", "heat distribution coefficient", ParameterProperties::HYPER); } -CLaplacianEigenmaps::~CLaplacianEigenmaps() +LaplacianEigenmaps::~LaplacianEigenmaps() { } -void CLaplacianEigenmaps::set_k(int32_t k) +void LaplacianEigenmaps::set_k(int32_t k) { ASSERT(k>0) m_k = k; } -int32_t CLaplacianEigenmaps::get_k() const +int32_t LaplacianEigenmaps::get_k() const { return m_k; } -void CLaplacianEigenmaps::set_tau(float64_t tau) +void LaplacianEigenmaps::set_tau(float64_t tau) { m_tau = tau; } -float64_t CLaplacianEigenmaps::get_tau() const +float64_t LaplacianEigenmaps::get_tau() const { return m_tau; } -const char* CLaplacianEigenmaps::get_name() const +const char* LaplacianEigenmaps::get_name() const { return "LaplacianEigenmaps"; }; -CFeatures* CLaplacianEigenmaps::transform(CFeatures* features, bool inplace) +std::shared_ptr LaplacianEigenmaps::transform(std::shared_ptr features, bool inplace) { // shorthand for simplefeatures - SG_REF(features); + // get dimensionality and number of vectors of data int32_t N = features->get_num_vectors(); @@ -70,19 +70,19 @@ CFeatures* CLaplacianEigenmaps::transform(CFeatures* features, bool inplace) // compute distance matrix ASSERT(m_distance) m_distance->init(features,features); - CDenseFeatures* embedding = embed_distance(m_distance); + auto embedding = embed_distance(m_distance); m_distance->remove_lhs_and_rhs(); - SG_UNREF(features); - return (CFeatures*)embedding; + + return embedding; } -CDenseFeatures* CLaplacianEigenmaps::embed_distance(CDistance* distance) +std::shared_ptr> LaplacianEigenmaps::embed_distance(std::shared_ptr distance) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.gaussian_kernel_width = m_tau; parameters.method = SHOGUN_LAPLACIAN_EIGENMAPS; parameters.target_dimension = m_target_dim; - parameters.distance = distance; + parameters.distance = distance.get(); return tapkee_embed(parameters); } diff --git a/src/shogun/converter/LaplacianEigenmaps.h b/src/shogun/converter/LaplacianEigenmaps.h index 59a22021e57..06147224690 100644 --- a/src/shogun/converter/LaplacianEigenmaps.h +++ b/src/shogun/converter/LaplacianEigenmaps.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, * Evan Shelhamer */ @@ -15,8 +15,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class LaplacianEigenmaps used to construct embeddings of * data using Laplacian Eigenmaps algorithm as described in: @@ -32,26 +32,26 @@ class CDistance; * sg('create_converter','laplacian_eigenmaps',k,width); * */ -class CLaplacianEigenmaps: public CEmbeddingConverter +class LaplacianEigenmaps: public EmbeddingConverter { public: /** constructor */ - CLaplacianEigenmaps(); + LaplacianEigenmaps(); /** destructor */ - virtual ~CLaplacianEigenmaps(); + virtual ~LaplacianEigenmaps(); /** apply to features * @param features to embed * @return embedded features */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** embed distance * @param distance to use for embedding */ - virtual CDenseFeatures* embed_distance(CDistance* distance); + virtual std::shared_ptr> embed_distance(std::shared_ptr distance); /** setter for K parameter * @param k k value diff --git a/src/shogun/converter/LinearLocalTangentSpaceAlignment.cpp b/src/shogun/converter/LinearLocalTangentSpaceAlignment.cpp index 868e1d45f23..33e044965a2 100644 --- a/src/shogun/converter/LinearLocalTangentSpaceAlignment.cpp +++ b/src/shogun/converter/LinearLocalTangentSpaceAlignment.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, * Evan Shelhamer */ @@ -12,33 +12,32 @@ using namespace shogun; -CLinearLocalTangentSpaceAlignment::CLinearLocalTangentSpaceAlignment() : - CLocalTangentSpaceAlignment() +LinearLocalTangentSpaceAlignment::LinearLocalTangentSpaceAlignment() : + LocalTangentSpaceAlignment() { } -CLinearLocalTangentSpaceAlignment::~CLinearLocalTangentSpaceAlignment() +LinearLocalTangentSpaceAlignment::~LinearLocalTangentSpaceAlignment() { } -const char* CLinearLocalTangentSpaceAlignment::get_name() const +const char* LinearLocalTangentSpaceAlignment::get_name() const { return "LinearLocalTangentSpaceAlignment"; } -CFeatures* -CLinearLocalTangentSpaceAlignment::transform(CFeatures* features, bool inplace) +std::shared_ptr +LinearLocalTangentSpaceAlignment::transform(std::shared_ptr features, bool inplace) { - CKernel* kernel = new CLinearKernel((CDotFeatures*)features,(CDotFeatures*)features); + auto dot_feats = std::static_pointer_cast(features); + auto kernel = std::make_shared(dot_feats, dot_feats); TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.eigenshift = m_nullspace_shift; parameters.method = SHOGUN_LINEAR_LOCAL_TANGENT_SPACE_ALIGNMENT; parameters.target_dimension = m_target_dim; - parameters.kernel = kernel; - parameters.features = (CDotFeatures*)features; - CDenseFeatures* embedding = tapkee_embed(parameters); - SG_UNREF(kernel); - return embedding; + parameters.kernel = kernel.get(); + parameters.features = (DotFeatures*)features.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/LinearLocalTangentSpaceAlignment.h b/src/shogun/converter/LinearLocalTangentSpaceAlignment.h index b3adc65437d..93b77ccea2c 100644 --- a/src/shogun/converter/LinearLocalTangentSpaceAlignment.h +++ b/src/shogun/converter/LinearLocalTangentSpaceAlignment.h @@ -15,8 +15,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class LinearLocalTangentSpaceAlignment converter used to * construct embeddings as described in: @@ -35,21 +35,21 @@ class CDistance; * sg('create_converter','lltsa',k); * */ -class CLinearLocalTangentSpaceAlignment: public CLocalTangentSpaceAlignment +class LinearLocalTangentSpaceAlignment: public LocalTangentSpaceAlignment { public: /** constructor */ - CLinearLocalTangentSpaceAlignment(); + LinearLocalTangentSpaceAlignment(); /** destructor */ - virtual ~CLinearLocalTangentSpaceAlignment(); + virtual ~LinearLocalTangentSpaceAlignment(); /** get name */ virtual const char* get_name() const; /** transform */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); }; } diff --git a/src/shogun/converter/LocalTangentSpaceAlignment.cpp b/src/shogun/converter/LocalTangentSpaceAlignment.cpp index 14c0ac16f73..393d64f5e70 100644 --- a/src/shogun/converter/LocalTangentSpaceAlignment.cpp +++ b/src/shogun/converter/LocalTangentSpaceAlignment.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, * Evan Shelhamer */ @@ -12,32 +12,31 @@ using namespace shogun; -CLocalTangentSpaceAlignment::CLocalTangentSpaceAlignment() : - CLocallyLinearEmbedding() +LocalTangentSpaceAlignment::LocalTangentSpaceAlignment() : + LocallyLinearEmbedding() { } -CLocalTangentSpaceAlignment::~CLocalTangentSpaceAlignment() +LocalTangentSpaceAlignment::~LocalTangentSpaceAlignment() { } -const char* CLocalTangentSpaceAlignment::get_name() const +const char* LocalTangentSpaceAlignment::get_name() const { return "LocalTangentSpaceAlignment"; }; -CFeatures* -CLocalTangentSpaceAlignment::transform(CFeatures* features, bool inplace) +std::shared_ptr +LocalTangentSpaceAlignment::transform(std::shared_ptr features, bool inplace) { - CKernel* kernel = new CLinearKernel((CDotFeatures*)features,(CDotFeatures*)features); + auto dot_feats = std::static_pointer_cast(features); + auto kernel = std::make_shared(dot_feats, dot_feats); TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.eigenshift = m_nullspace_shift; parameters.method = SHOGUN_LOCAL_TANGENT_SPACE_ALIGNMENT; parameters.target_dimension = m_target_dim; - parameters.kernel = kernel; - CDenseFeatures* embedding = tapkee_embed(parameters); - SG_UNREF(kernel); - return embedding; + parameters.kernel = kernel.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/LocalTangentSpaceAlignment.h b/src/shogun/converter/LocalTangentSpaceAlignment.h index 8e30483393b..4ab56900878 100644 --- a/src/shogun/converter/LocalTangentSpaceAlignment.h +++ b/src/shogun/converter/LocalTangentSpaceAlignment.h @@ -14,8 +14,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class LocalTangentSpaceAlignment used to embed * data using Local Tangent Space Alignment (LTSA) @@ -33,21 +33,21 @@ class CDistance; * Uses implementation from the Tapkee library. * */ -class CLocalTangentSpaceAlignment: public CLocallyLinearEmbedding +class LocalTangentSpaceAlignment: public LocallyLinearEmbedding { public: /** constructor */ - CLocalTangentSpaceAlignment(); + LocalTangentSpaceAlignment(); /** destructor */ - virtual ~CLocalTangentSpaceAlignment(); + virtual ~LocalTangentSpaceAlignment(); /** get name */ virtual const char* get_name() const; /** transform */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); }; } diff --git a/src/shogun/converter/LocalityPreservingProjections.cpp b/src/shogun/converter/LocalityPreservingProjections.cpp index 960ff43edfb..95681fa57d5 100644 --- a/src/shogun/converter/LocalityPreservingProjections.cpp +++ b/src/shogun/converter/LocalityPreservingProjections.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, * Evan Shelhamer, Bjoern Esser */ @@ -12,22 +12,22 @@ using namespace shogun; -CLocalityPreservingProjections::CLocalityPreservingProjections() : - CLaplacianEigenmaps() +LocalityPreservingProjections::LocalityPreservingProjections() : + LaplacianEigenmaps() { } -CLocalityPreservingProjections::~CLocalityPreservingProjections() +LocalityPreservingProjections::~LocalityPreservingProjections() { } -const char* CLocalityPreservingProjections::get_name() const +const char* LocalityPreservingProjections::get_name() const { return "LocalityPreservingProjections"; }; -CFeatures* -CLocalityPreservingProjections::transform(CFeatures* features, bool inplace) +std::shared_ptr +LocalityPreservingProjections::transform(std::shared_ptr features, bool inplace) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; m_distance->init(features,features); @@ -35,9 +35,8 @@ CLocalityPreservingProjections::transform(CFeatures* features, bool inplace) parameters.gaussian_kernel_width = m_tau; parameters.method = SHOGUN_LOCALITY_PRESERVING_PROJECTIONS; parameters.target_dimension = m_target_dim; - parameters.distance = m_distance; - parameters.features = (CDotFeatures*)features; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + parameters.distance = m_distance.get(); + parameters.features = (DotFeatures*)features.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/LocalityPreservingProjections.h b/src/shogun/converter/LocalityPreservingProjections.h index 263158d7797..fbb1c9c7b2f 100644 --- a/src/shogun/converter/LocalityPreservingProjections.h +++ b/src/shogun/converter/LocalityPreservingProjections.h @@ -12,8 +12,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class LocalityPreservingProjections used to compute * embeddings of data using Locality Preserving Projections method @@ -30,21 +30,21 @@ class CDistance; * sg('create_converter','lpp',k,width); * */ -class CLocalityPreservingProjections: public CLaplacianEigenmaps +class LocalityPreservingProjections: public LaplacianEigenmaps { public: /** constructor */ - CLocalityPreservingProjections(); + LocalityPreservingProjections(); /** destructor */ - virtual ~CLocalityPreservingProjections(); + virtual ~LocalityPreservingProjections(); /** get name */ virtual const char* get_name() const; /** transform */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); }; } diff --git a/src/shogun/converter/LocallyLinearEmbedding.cpp b/src/shogun/converter/LocallyLinearEmbedding.cpp index 3e481cbe94b..2dce20fefa6 100644 --- a/src/shogun/converter/LocallyLinearEmbedding.cpp +++ b/src/shogun/converter/LocallyLinearEmbedding.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Evan Shelhamer, * Heiko Strathmann */ @@ -15,8 +15,8 @@ using namespace shogun; -CLocallyLinearEmbedding::CLocallyLinearEmbedding() : - CEmbeddingConverter() +LocallyLinearEmbedding::LocallyLinearEmbedding() : + EmbeddingConverter() { m_k = 10; m_nullspace_shift = -1e-9; @@ -24,7 +24,7 @@ CLocallyLinearEmbedding::CLocallyLinearEmbedding() : init(); } -void CLocallyLinearEmbedding::init() +void LocallyLinearEmbedding::init() { SG_ADD(&m_k, "k", "number of neighbors", ParameterProperties::HYPER); SG_ADD(&m_nullspace_shift, "nullspace_shift", @@ -34,58 +34,57 @@ void CLocallyLinearEmbedding::init() } -CLocallyLinearEmbedding::~CLocallyLinearEmbedding() +LocallyLinearEmbedding::~LocallyLinearEmbedding() { } -void CLocallyLinearEmbedding::set_k(int32_t k) +void LocallyLinearEmbedding::set_k(int32_t k) { ASSERT(k>0) m_k = k; } -int32_t CLocallyLinearEmbedding::get_k() const +int32_t LocallyLinearEmbedding::get_k() const { return m_k; } -void CLocallyLinearEmbedding::set_nullspace_shift(float64_t nullspace_shift) +void LocallyLinearEmbedding::set_nullspace_shift(float64_t nullspace_shift) { m_nullspace_shift = nullspace_shift; } -float64_t CLocallyLinearEmbedding::get_nullspace_shift() const +float64_t LocallyLinearEmbedding::get_nullspace_shift() const { return m_nullspace_shift; } -void CLocallyLinearEmbedding::set_reconstruction_shift(float64_t reconstruction_shift) +void LocallyLinearEmbedding::set_reconstruction_shift(float64_t reconstruction_shift) { m_reconstruction_shift = reconstruction_shift; } -float64_t CLocallyLinearEmbedding::get_reconstruction_shift() const +float64_t LocallyLinearEmbedding::get_reconstruction_shift() const { return m_reconstruction_shift; } -const char* CLocallyLinearEmbedding::get_name() const +const char* LocallyLinearEmbedding::get_name() const { return "LocallyLinearEmbedding"; } -CFeatures* CLocallyLinearEmbedding::transform(CFeatures* features, bool inplace) +std::shared_ptr LocallyLinearEmbedding::transform(std::shared_ptr features, bool inplace) { // oh my let me dirty cast it - CKernel* kernel = new CLinearKernel((CDotFeatures*)features,(CDotFeatures*)features); + auto dot_feats = std::static_pointer_cast(features); + auto kernel = std::make_shared(dot_feats, dot_feats); TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.eigenshift = m_nullspace_shift; parameters.method = SHOGUN_LOCALLY_LINEAR_EMBEDDING; parameters.target_dimension = m_target_dim; - parameters.kernel = kernel; - CDenseFeatures* embedding = tapkee_embed(parameters); - SG_UNREF(kernel); - return embedding; + parameters.kernel = kernel.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/LocallyLinearEmbedding.h b/src/shogun/converter/LocallyLinearEmbedding.h index f6e0d18910d..4fe323430ba 100644 --- a/src/shogun/converter/LocallyLinearEmbedding.h +++ b/src/shogun/converter/LocallyLinearEmbedding.h @@ -15,8 +15,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class LocallyLinearEmbedding used to embed * data using Locally Linear Embedding algorithm described in @@ -39,20 +39,20 @@ class CDistance; * sg('create_converter','lle',k); * */ -class CLocallyLinearEmbedding: public CEmbeddingConverter +class LocallyLinearEmbedding: public EmbeddingConverter { public: /** constructor */ - CLocallyLinearEmbedding(); + LocallyLinearEmbedding(); /** destructor */ - virtual ~CLocallyLinearEmbedding(); + virtual ~LocallyLinearEmbedding(); /** apply preprocessor to features * @param features */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** setter for k parameter * @param k k value diff --git a/src/shogun/converter/ManifoldSculpting.cpp b/src/shogun/converter/ManifoldSculpting.cpp index 2d9ce009ee8..db3739ebe03 100644 --- a/src/shogun/converter/ManifoldSculpting.cpp +++ b/src/shogun/converter/ManifoldSculpting.cpp @@ -11,8 +11,8 @@ using namespace shogun; -CManifoldSculpting::CManifoldSculpting() : - CEmbeddingConverter() +ManifoldSculpting::ManifoldSculpting() : + EmbeddingConverter() { // Default values m_k = 10; @@ -21,7 +21,7 @@ CManifoldSculpting::CManifoldSculpting() : init(); } -void CManifoldSculpting::init() +void ManifoldSculpting::init() { SG_ADD(&m_k, "k", "number of neighbors"); SG_ADD(&m_squishing_rate, "quishing_rate", @@ -30,68 +30,64 @@ void CManifoldSculpting::init() "maximum number of algorithm's iterations"); } -CManifoldSculpting::~CManifoldSculpting() +ManifoldSculpting::~ManifoldSculpting() { } -const char* CManifoldSculpting::get_name() const +const char* ManifoldSculpting::get_name() const { return "ManifoldSculpting"; } -void CManifoldSculpting::set_k(const int32_t k) +void ManifoldSculpting::set_k(const int32_t k) { ASSERT(k>0) m_k = k; } -int32_t CManifoldSculpting::get_k() const +int32_t ManifoldSculpting::get_k() const { return m_k; } -void CManifoldSculpting::set_squishing_rate(const float64_t squishing_rate) +void ManifoldSculpting::set_squishing_rate(const float64_t squishing_rate) { ASSERT(squishing_rate >= 0 && squishing_rate < 1) m_squishing_rate = squishing_rate; } -float64_t CManifoldSculpting::get_squishing_rate() const +float64_t ManifoldSculpting::get_squishing_rate() const { return m_squishing_rate; } -void CManifoldSculpting::set_max_iteration(const int32_t max_iteration) +void ManifoldSculpting::set_max_iteration(const int32_t max_iteration) { ASSERT(max_iteration > 0) m_max_iteration = max_iteration; } -int32_t CManifoldSculpting::get_max_iteration() const +int32_t ManifoldSculpting::get_max_iteration() const { return m_max_iteration; } -CFeatures* CManifoldSculpting::transform(CFeatures* features, bool inplace) +std::shared_ptr ManifoldSculpting::transform(std::shared_ptr features, bool inplace) { - CDenseFeatures* feats = (CDenseFeatures*)features; - SG_REF(feats); - CDistance* euclidean_distance = - new CEuclideanDistance(feats, feats); + auto feats = std::static_pointer_cast>(features); + + auto euclidean_distance = + std::make_shared(feats, feats); TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.squishing_rate = m_squishing_rate; parameters.max_iteration = m_max_iteration; - parameters.features = feats; - parameters.distance = euclidean_distance; + parameters.features = feats.get(); + parameters.distance = euclidean_distance.get(); parameters.method = SHOGUN_MANIFOLD_SCULPTING; parameters.target_dimension = m_target_dim; - CDenseFeatures* embedding = tapkee_embed(parameters); - - SG_UNREF(euclidean_distance); - - return embedding; + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/ManifoldSculpting.h b/src/shogun/converter/ManifoldSculpting.h index f1bc319c5d1..b811870e3c0 100644 --- a/src/shogun/converter/ManifoldSculpting.h +++ b/src/shogun/converter/ManifoldSculpting.h @@ -14,21 +14,21 @@ namespace shogun { -/** @brief class CManifoldSculpting used to embed +/** @brief class ManifoldSculpting used to embed * data using manifold sculpting embedding algorithm. * * Uses implementation from the Tapkee library. * */ -class CManifoldSculpting : public CEmbeddingConverter +class ManifoldSculpting : public EmbeddingConverter { public: /** constructor */ - CManifoldSculpting(); + ManifoldSculpting(); /** destructor */ - virtual ~CManifoldSculpting(); + virtual ~ManifoldSculpting(); /** get name */ virtual const char* get_name() const; @@ -37,7 +37,7 @@ class CManifoldSculpting : public CEmbeddingConverter * * @param features features to embed */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** setter for the k * @@ -93,7 +93,7 @@ class CManifoldSculpting : public CEmbeddingConverter */ float64_t m_max_iteration; -}; /* class CManifoldSculpting */ +}; /* class ManifoldSculpting */ } /* namespace shogun */ diff --git a/src/shogun/converter/MultidimensionalScaling.cpp b/src/shogun/converter/MultidimensionalScaling.cpp index aba233be767..6961ddb061a 100644 --- a/src/shogun/converter/MultidimensionalScaling.cpp +++ b/src/shogun/converter/MultidimensionalScaling.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, * Evan Shelhamer, Chiyuan Zhang, Bjoern Esser */ @@ -18,7 +18,7 @@ using namespace shogun; -CMultidimensionalScaling::CMultidimensionalScaling() : CEmbeddingConverter() +MultidimensionalScaling::MultidimensionalScaling() : EmbeddingConverter() { m_eigenvalues = SGVector(); m_landmark_number = 3; @@ -27,7 +27,7 @@ CMultidimensionalScaling::CMultidimensionalScaling() : CEmbeddingConverter() init(); } -void CMultidimensionalScaling::init() +void MultidimensionalScaling::init() { SG_ADD(&m_eigenvalues, "eigenvalues", "eigenvalues of last embedding"); SG_ADD(&m_landmark, "landmark", @@ -36,16 +36,16 @@ void CMultidimensionalScaling::init() "the number of landmarks for approximation", ParameterProperties::HYPER); } -CMultidimensionalScaling::~CMultidimensionalScaling() +MultidimensionalScaling::~MultidimensionalScaling() { } -SGVector CMultidimensionalScaling::get_eigenvalues() const +SGVector MultidimensionalScaling::get_eigenvalues() const { return m_eigenvalues; } -void CMultidimensionalScaling::set_landmark_number(int32_t num) +void MultidimensionalScaling::set_landmark_number(int32_t num) { if (num<3) error("Number of landmarks should be greater than 3 to make triangulation possible while {} given.", @@ -53,27 +53,27 @@ void CMultidimensionalScaling::set_landmark_number(int32_t num) m_landmark_number = num; } -int32_t CMultidimensionalScaling::get_landmark_number() const +int32_t MultidimensionalScaling::get_landmark_number() const { return m_landmark_number; } -void CMultidimensionalScaling::set_landmark(bool landmark) +void MultidimensionalScaling::set_landmark(bool landmark) { m_landmark = landmark; } -bool CMultidimensionalScaling::get_landmark() const +bool MultidimensionalScaling::get_landmark() const { return m_landmark; } -const char* CMultidimensionalScaling::get_name() const +const char* MultidimensionalScaling::get_name() const { return "MultidimensionalScaling"; }; -CDenseFeatures* CMultidimensionalScaling::embed_distance(CDistance* distance) +std::shared_ptr> MultidimensionalScaling::embed_distance(std::shared_ptr distance) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; if (m_landmark) @@ -90,22 +90,21 @@ CDenseFeatures* CMultidimensionalScaling::embed_distance(CDistance* d parameters.method = SHOGUN_MULTIDIMENSIONAL_SCALING; } parameters.target_dimension = m_target_dim; - parameters.distance = distance; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + parameters.distance = distance.get(); + return tapkee_embed(parameters); } -CFeatures* -CMultidimensionalScaling::transform(CFeatures* features, bool inplace) +std::shared_ptr +MultidimensionalScaling::transform(std::shared_ptr features, bool inplace) { - SG_REF(features); + ASSERT(m_distance) m_distance->init(features,features); - CDenseFeatures* embedding = embed_distance(m_distance); + auto embedding = embed_distance(m_distance); m_distance->remove_lhs_and_rhs(); - SG_UNREF(features); - return (CFeatures*)embedding; + + return embedding; } diff --git a/src/shogun/converter/MultidimensionalScaling.h b/src/shogun/converter/MultidimensionalScaling.h index 48ba26184b3..931f73d6fdd 100644 --- a/src/shogun/converter/MultidimensionalScaling.h +++ b/src/shogun/converter/MultidimensionalScaling.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Soeren Sonnenburg, * Evan Shelhamer */ @@ -15,8 +15,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief class Multidimensionalscaling is used to perform * multidimensional scaling (capable of landmark approximation @@ -54,28 +54,28 @@ class CDistance; * sg('create_converter','mds'); * */ -class CMultidimensionalScaling: public CEmbeddingConverter +class MultidimensionalScaling: public EmbeddingConverter { public: /* constructor */ - CMultidimensionalScaling(); + MultidimensionalScaling(); /* destructor */ - virtual ~CMultidimensionalScaling(); + virtual ~MultidimensionalScaling(); - /** apply preprocessor to CDistance + /** apply preprocessor to Distance * @param distance (should be approximate euclidean for consistent result) * @return new features with distance similar to given as much as possible */ - virtual CDenseFeatures* embed_distance(CDistance* distance); + virtual std::shared_ptr> embed_distance(std::shared_ptr distance); /** apply preprocessor to feature matrix, * changes feature matrix to the one having target dimensionality * @param features features which feature matrix should be processed * @return new feature matrix */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** get name */ const char* get_name() const; diff --git a/src/shogun/converter/NeighborhoodPreservingEmbedding.cpp b/src/shogun/converter/NeighborhoodPreservingEmbedding.cpp index b8961361a3b..2b446901a36 100644 --- a/src/shogun/converter/NeighborhoodPreservingEmbedding.cpp +++ b/src/shogun/converter/NeighborhoodPreservingEmbedding.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Fernando Iglesias, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Fernando Iglesias, * Evan Shelhamer */ @@ -13,33 +13,32 @@ using namespace shogun; -CNeighborhoodPreservingEmbedding::CNeighborhoodPreservingEmbedding() : - CLocallyLinearEmbedding() +NeighborhoodPreservingEmbedding::NeighborhoodPreservingEmbedding() : + LocallyLinearEmbedding() { } -CNeighborhoodPreservingEmbedding::~CNeighborhoodPreservingEmbedding() +NeighborhoodPreservingEmbedding::~NeighborhoodPreservingEmbedding() { } -const char* CNeighborhoodPreservingEmbedding::get_name() const +const char* NeighborhoodPreservingEmbedding::get_name() const { return "NeighborhoodPreservingEmbedding"; } -CFeatures* -CNeighborhoodPreservingEmbedding::transform(CFeatures* features, bool inplace) +std::shared_ptr +NeighborhoodPreservingEmbedding::transform(std::shared_ptr features, bool inplace) { - CKernel* kernel = new CLinearKernel((CDotFeatures*)features,(CDotFeatures*)features); + auto dot_feats = std::static_pointer_cast(features); + auto kernel = std::make_shared(dot_feats, dot_feats); TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; parameters.eigenshift = m_nullspace_shift; parameters.method = SHOGUN_NEIGHBORHOOD_PRESERVING_EMBEDDING; parameters.target_dimension = m_target_dim; - parameters.kernel = kernel; - parameters.features = (CDotFeatures*)features; - CDenseFeatures* embedding = tapkee_embed(parameters); - SG_UNREF(kernel); - return embedding; + parameters.kernel = kernel.get(); + parameters.features = dot_feats.get(); + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/NeighborhoodPreservingEmbedding.h b/src/shogun/converter/NeighborhoodPreservingEmbedding.h index 02b9eeeef8c..fd786d581c8 100644 --- a/src/shogun/converter/NeighborhoodPreservingEmbedding.h +++ b/src/shogun/converter/NeighborhoodPreservingEmbedding.h @@ -15,8 +15,8 @@ namespace shogun { -class CFeatures; -class CDistance; +class Features; +class Distance; /** @brief NeighborhoodPreservingEmbedding converter used to * construct embeddings as described in: @@ -35,21 +35,21 @@ class CDistance; * sg('create_converter','npe',k); * */ -class CNeighborhoodPreservingEmbedding: public CLocallyLinearEmbedding +class NeighborhoodPreservingEmbedding: public LocallyLinearEmbedding { public: /** constructor */ - CNeighborhoodPreservingEmbedding(); + NeighborhoodPreservingEmbedding(); /** destructor */ - virtual ~CNeighborhoodPreservingEmbedding(); + virtual ~NeighborhoodPreservingEmbedding(); /** get name */ virtual const char* get_name() const; /** transform */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); }; } diff --git a/src/shogun/converter/StochasticProximityEmbedding.cpp b/src/shogun/converter/StochasticProximityEmbedding.cpp index 5e031503268..676f45ed95a 100644 --- a/src/shogun/converter/StochasticProximityEmbedding.cpp +++ b/src/shogun/converter/StochasticProximityEmbedding.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Soeren Sonnenburg, Sergey Lisitsyn, + * Authors: Fernando Iglesias, Soeren Sonnenburg, Sergey Lisitsyn, * Chiyuan Zhang, Heiko Strathmann, Bjoern Esser */ @@ -12,8 +12,8 @@ using namespace shogun; -CStochasticProximityEmbedding::CStochasticProximityEmbedding() : - CEmbeddingConverter() +StochasticProximityEmbedding::StochasticProximityEmbedding() : + EmbeddingConverter() { // Initialize to default values m_k = 12; @@ -25,7 +25,7 @@ CStochasticProximityEmbedding::CStochasticProximityEmbedding() : init(); } -void CStochasticProximityEmbedding::init() +void StochasticProximityEmbedding::init() { SG_ADD(&m_k, "m_k", "Number of neighbors"); SG_ADD(&m_tolerance, "m_tolerance", "Regularization parameter"); @@ -35,11 +35,11 @@ void CStochasticProximityEmbedding::init() ParameterProperties::NONE, SG_OPTIONS(SPE_GLOBAL, SPE_LOCAL)); } -CStochasticProximityEmbedding::~CStochasticProximityEmbedding() +StochasticProximityEmbedding::~StochasticProximityEmbedding() { } -void CStochasticProximityEmbedding::set_k(int32_t k) +void StochasticProximityEmbedding::set_k(int32_t k) { if ( k <= 0 ) error("Number of neighbors k must be greater than 0"); @@ -47,22 +47,22 @@ void CStochasticProximityEmbedding::set_k(int32_t k) m_k = k; } -int32_t CStochasticProximityEmbedding::get_k() const +int32_t StochasticProximityEmbedding::get_k() const { return m_k; } -void CStochasticProximityEmbedding::set_strategy(ESPEStrategy strategy) +void StochasticProximityEmbedding::set_strategy(ESPEStrategy strategy) { m_strategy = strategy; } -ESPEStrategy CStochasticProximityEmbedding::get_strategy() const +ESPEStrategy StochasticProximityEmbedding::get_strategy() const { return m_strategy; } -void CStochasticProximityEmbedding::set_tolerance(float32_t tolerance) +void StochasticProximityEmbedding::set_tolerance(float32_t tolerance) { if ( tolerance <= 0 ) error("Tolerance regularization parameter must be greater " @@ -71,12 +71,12 @@ void CStochasticProximityEmbedding::set_tolerance(float32_t tolerance) m_tolerance = tolerance; } -int32_t CStochasticProximityEmbedding::get_tolerance() const +int32_t StochasticProximityEmbedding::get_tolerance() const { return m_tolerance; } -void CStochasticProximityEmbedding::set_nupdates(int32_t nupdates) +void StochasticProximityEmbedding::set_nupdates(int32_t nupdates) { if ( nupdates <= 0 ) error("The number of updates must be greater than 0"); @@ -84,36 +84,36 @@ void CStochasticProximityEmbedding::set_nupdates(int32_t nupdates) m_nupdates = nupdates; } -int32_t CStochasticProximityEmbedding::get_nupdates() const +int32_t StochasticProximityEmbedding::get_nupdates() const { return m_nupdates; } -void CStochasticProximityEmbedding::set_max_iteration(const int32_t max_iteration) +void StochasticProximityEmbedding::set_max_iteration(const int32_t max_iteration) { m_max_iteration = max_iteration; } -int32_t CStochasticProximityEmbedding::get_max_iteration() const +int32_t StochasticProximityEmbedding::get_max_iteration() const { return m_max_iteration; } -const char * CStochasticProximityEmbedding::get_name() const +const char * StochasticProximityEmbedding::get_name() const { return "StochasticProximityEmbedding"; } -CFeatures* -CStochasticProximityEmbedding::transform(CFeatures* features, bool inplace) +std::shared_ptr +StochasticProximityEmbedding::transform(std::shared_ptr features, bool inplace) { if ( !features ) error("Features are required to apply SPE"); // Shorthand for the DenseFeatures - CDenseFeatures< float64_t >* simple_features = - (CDenseFeatures< float64_t >*) features; - SG_REF(features); + auto simple_features = + std::static_pointer_cast>(features); + // Get and check the number of vectors int32_t N = simple_features->get_num_vectors(); @@ -126,14 +126,14 @@ CStochasticProximityEmbedding::transform(CFeatures* features, bool inplace) "the number of updates ({})", N, m_nupdates); m_distance->init(simple_features, simple_features); - CDenseFeatures< float64_t >* embedding = embed_distance(m_distance); + auto embedding = embed_distance(m_distance); m_distance->remove_lhs_and_rhs(); - SG_UNREF(features); - return (CFeatures*)embedding; + + return embedding; } -CDenseFeatures< float64_t >* CStochasticProximityEmbedding::embed_distance(CDistance* distance) +std::shared_ptr> StochasticProximityEmbedding::embed_distance(std::shared_ptr distance) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.n_neighbors = m_k; @@ -141,10 +141,9 @@ CDenseFeatures< float64_t >* CStochasticProximityEmbedding::embed_distance(CDist parameters.target_dimension = m_target_dim; parameters.spe_num_updates = m_nupdates; parameters.spe_tolerance = m_tolerance; - parameters.distance = distance; + parameters.distance = distance.get(); parameters.spe_global_strategy = (m_strategy==SPE_GLOBAL); parameters.max_iteration = m_max_iteration; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/StochasticProximityEmbedding.h b/src/shogun/converter/StochasticProximityEmbedding.h index 1f1fe5b3f61..f69c738a914 100644 --- a/src/shogun/converter/StochasticProximityEmbedding.h +++ b/src/shogun/converter/StochasticProximityEmbedding.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Sergey Lisitsyn, Chiyuan Zhang, Heiko Strathmann, + * Authors: Fernando Iglesias, Sergey Lisitsyn, Chiyuan Zhang, Heiko Strathmann, * Bjoern Esser, Soeren Sonnenburg */ @@ -52,27 +52,27 @@ enum ESPEStrategy * * Uses implementation from the Tapkee library. * - * Only CEuclideanDistance distance is supported for the moment. + * Only EuclideanDistance distance is supported for the moment. * */ -class CStochasticProximityEmbedding : public CEmbeddingConverter +class StochasticProximityEmbedding : public EmbeddingConverter { public: /** constructor */ - CStochasticProximityEmbedding(); + StochasticProximityEmbedding(); /** destructor */ - virtual ~CStochasticProximityEmbedding(); + virtual ~StochasticProximityEmbedding(); /** apply to features * * @param features features to embed * @return embedding features */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** setter for number of neighbors k in local strategy * @@ -142,11 +142,11 @@ class CStochasticProximityEmbedding : public CEmbeddingConverter /** default init */ void init(); - /** apply embedding to CDistance + /** apply embedding to Distance * @param distance TODO Euclidean works fine, check with others * @return new features in the embedded space */ - virtual CDenseFeatures< float64_t >* embed_distance(CDistance* distance); + virtual std::shared_ptr> embed_distance(std::shared_ptr distance); private: diff --git a/src/shogun/converter/TDistributedStochasticNeighborEmbedding.cpp b/src/shogun/converter/TDistributedStochasticNeighborEmbedding.cpp index d2aed8c9f9b..385933f019b 100644 --- a/src/shogun/converter/TDistributedStochasticNeighborEmbedding.cpp +++ b/src/shogun/converter/TDistributedStochasticNeighborEmbedding.cpp @@ -10,8 +10,8 @@ using namespace shogun; -CTDistributedStochasticNeighborEmbedding::CTDistributedStochasticNeighborEmbedding() : - CEmbeddingConverter() +TDistributedStochasticNeighborEmbedding::TDistributedStochasticNeighborEmbedding() : + EmbeddingConverter() { // Default values m_perplexity = 30.0; @@ -19,53 +19,51 @@ CTDistributedStochasticNeighborEmbedding::CTDistributedStochasticNeighborEmbeddi init(); } -void CTDistributedStochasticNeighborEmbedding::init() +void TDistributedStochasticNeighborEmbedding::init() { SG_ADD(&m_perplexity, "perplexity", "perplexity"); SG_ADD(&m_theta, "theta", "learning rate"); } -CTDistributedStochasticNeighborEmbedding::~CTDistributedStochasticNeighborEmbedding() +TDistributedStochasticNeighborEmbedding::~TDistributedStochasticNeighborEmbedding() { } -const char* CTDistributedStochasticNeighborEmbedding::get_name() const +const char* TDistributedStochasticNeighborEmbedding::get_name() const { return "TDistributedStochasticNeighborEmbedding"; } -void CTDistributedStochasticNeighborEmbedding::set_theta(const float64_t theta) +void TDistributedStochasticNeighborEmbedding::set_theta(const float64_t theta) { m_theta = theta; } -float64_t CTDistributedStochasticNeighborEmbedding::get_theta() const +float64_t TDistributedStochasticNeighborEmbedding::get_theta() const { return m_theta; } -void CTDistributedStochasticNeighborEmbedding::set_perplexity(const float64_t perplexity) +void TDistributedStochasticNeighborEmbedding::set_perplexity(const float64_t perplexity) { m_perplexity = perplexity; } -float64_t CTDistributedStochasticNeighborEmbedding::get_perplexity() const +float64_t TDistributedStochasticNeighborEmbedding::get_perplexity() const { return m_perplexity; } -CFeatures* CTDistributedStochasticNeighborEmbedding::transform( - CFeatures* features, bool inplace) +std::shared_ptr TDistributedStochasticNeighborEmbedding::transform( + std::shared_ptr features, bool inplace) { TAPKEE_PARAMETERS_FOR_SHOGUN parameters; parameters.sne_theta = m_theta; parameters.sne_perplexity = m_perplexity; - parameters.features = (CDotFeatures*)features; - + parameters.features = (DotFeatures*)features.get(); parameters.method = SHOGUN_TDISTRIBUTED_STOCHASTIC_NEIGHBOR_EMBEDDING; parameters.target_dimension = m_target_dim; - CDenseFeatures* embedding = tapkee_embed(parameters); - return embedding; + return tapkee_embed(parameters); } diff --git a/src/shogun/converter/TDistributedStochasticNeighborEmbedding.h b/src/shogun/converter/TDistributedStochasticNeighborEmbedding.h index a7d408e84c4..edace3744da 100644 --- a/src/shogun/converter/TDistributedStochasticNeighborEmbedding.h +++ b/src/shogun/converter/TDistributedStochasticNeighborEmbedding.h @@ -21,15 +21,15 @@ namespace shogun * Uses implementation from the Tapkee library. * */ -class CTDistributedStochasticNeighborEmbedding : public CEmbeddingConverter +class TDistributedStochasticNeighborEmbedding : public EmbeddingConverter { public: /** constructor */ - CTDistributedStochasticNeighborEmbedding(); + TDistributedStochasticNeighborEmbedding(); /** destructor */ - virtual ~CTDistributedStochasticNeighborEmbedding(); + virtual ~TDistributedStochasticNeighborEmbedding(); /** get name */ virtual const char* get_name() const; @@ -38,7 +38,7 @@ class CTDistributedStochasticNeighborEmbedding : public CEmbeddingConverter * * @param features features to embed */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** setter for the learning rate * diff --git a/src/shogun/converter/ica/FFSep.cpp b/src/shogun/converter/ica/FFSep.cpp index 4f150ba1b48..c914e276cf0 100644 --- a/src/shogun/converter/ica/FFSep.cpp +++ b/src/shogun/converter/ica/FFSep.cpp @@ -18,12 +18,12 @@ using namespace Eigen; namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); }; -CFFSep::CFFSep() : CICAConverter() +FFSep::FFSep() : ICAConverter() { init(); } -void CFFSep::init() +void FFSep::init() { m_tau = SGVector(4); m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3; @@ -33,26 +33,26 @@ void CFFSep::init() SG_ADD(&m_tau, "tau", "tau vector", ParameterProperties::HYPER); } -CFFSep::~CFFSep() +FFSep::~FFSep() { } -void CFFSep::set_tau(SGVector tau) +void FFSep::set_tau(SGVector tau) { m_tau = tau; } -SGVector CFFSep::get_tau() const +SGVector FFSep::get_tau() const { return m_tau; } -SGNDArray CFFSep::get_covs() const +SGNDArray FFSep::get_covs() const { return m_covs; } -void CFFSep::fit_dense(CDenseFeatures* features) +void FFSep::fit_dense(std::shared_ptr> features) { ASSERT(features); @@ -62,7 +62,7 @@ void CFFSep::fit_dense(CDenseFeatures* features) int m = X.num_cols; int N = m_tau.vlen; - Map EX(X.matrix,n,m); + Eigen::Map EX(X.matrix,n,m); // Compute Correlation Matrices index_t * M_dims = SG_MALLOC(index_t, 3); @@ -73,17 +73,17 @@ void CFFSep::fit_dense(CDenseFeatures* features) for (int t = 0; t < N; t++) { - Map EM(m_covs.get_matrix(t),n,n); + Eigen::Map EM(m_covs.get_matrix(t),n,n); EM = cor(EX,m_tau[t]); } // Diagonalize - SGMatrix Q = CFFDiag::diagonalize(m_covs, m_mixing_matrix, tol, max_iter); - Map EQ(Q.matrix,n,n); + SGMatrix Q = FFDiag::diagonalize(m_covs, m_mixing_matrix, tol, max_iter); + Eigen::Map EQ(Q.matrix,n,n); // Compute Mixing Matrix m_mixing_matrix = SGMatrix(n,n); - Map C(m_mixing_matrix.matrix,n,n); + Eigen::Map C(m_mixing_matrix.matrix,n,n); C = EQ.inverse(); // Normalize Estimated Mixing Matrix diff --git a/src/shogun/converter/ica/FFSep.h b/src/shogun/converter/ica/FFSep.h index 3557f74a835..5af9b74da82 100644 --- a/src/shogun/converter/ica/FFSep.h +++ b/src/shogun/converter/ica/FFSep.h @@ -15,7 +15,7 @@ namespace shogun { -class CFeatures; +class Features; /** @brief class FFSep * @@ -29,15 +29,15 @@ class CFeatures; * The Journal of Machine Learning Research, 5, 777-800. * */ -class CFFSep: public CICAConverter +class FFSep: public ICAConverter { public: /** constructor */ - CFFSep(); + FFSep(); /** destructor */ - virtual ~CFFSep(); + virtual ~FFSep(); /** getter for tau parameter @@ -62,7 +62,7 @@ class CFFSep: public CICAConverter /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features); + virtual void fit_dense(std::shared_ptr> features); private: diff --git a/src/shogun/converter/ica/FastICA.cpp b/src/shogun/converter/ica/FastICA.cpp index 1aa1bd7f793..86242659ed4 100644 --- a/src/shogun/converter/ica/FastICA.cpp +++ b/src/shogun/converter/ica/FastICA.cpp @@ -42,32 +42,32 @@ namespace { }; -CFastICA::CFastICA() : RandomMixin() +FastICA::FastICA() : RandomMixin() { init(); } -void CFastICA::init() +void FastICA::init() { whiten = true; SG_ADD(&whiten, "whiten", "flag indicating whether to whiten the data"); } -CFastICA::~CFastICA() +FastICA::~FastICA() { } -void CFastICA::set_whiten(bool _whiten) +void FastICA::set_whiten(bool _whiten) { whiten = _whiten; } -bool CFastICA::get_whiten() const +bool FastICA::get_whiten() const { return whiten; } -void CFastICA::fit_dense(CDenseFeatures* features) +void FastICA::fit_dense(std::shared_ptr> features) { auto X = features->get_feature_matrix(); require(X.data(), "Features have not been provided."); @@ -75,7 +75,7 @@ void CFastICA::fit_dense(CDenseFeatures* features) int p = X.num_cols; int m = n; - Map EX(X.matrix,n,p); + Eigen::Map EX(X.matrix,n,p); // Whiten MatrixXd K; @@ -116,7 +116,7 @@ void CFastICA::fit_dense(CDenseFeatures* features) random::fill_array(m_mixing_matrix, NormalDistribution(), m_prng); } - Map W(m_mixing_matrix.matrix, m, m); + Eigen::Map W(m_mixing_matrix.matrix, m, m); W = sym_decorrelation(W); diff --git a/src/shogun/converter/ica/FastICA.h b/src/shogun/converter/ica/FastICA.h index ff431a5fd20..0ea2a60f375 100644 --- a/src/shogun/converter/ica/FastICA.h +++ b/src/shogun/converter/ica/FastICA.h @@ -15,7 +15,7 @@ namespace shogun { -class CFeatures; +class Features; /** @brief class FastICA * @@ -26,15 +26,15 @@ class CFeatures; * Algorithms and Applications, Neural Networks, 13(4-5), 2000, * pp. 411-430` */ -class CFastICA: public RandomMixin +class FastICA: public RandomMixin { public: /** constructor */ - CFastICA(); + FastICA(); /** destructor */ - virtual ~CFastICA(); + virtual ~FastICA(); /** setter for whiten flag * whether to whiten the data or not @@ -55,7 +55,7 @@ class CFastICA: public RandomMixin /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features); + virtual void fit_dense(std::shared_ptr> features); private: diff --git a/src/shogun/converter/ica/ICAConverter.cpp b/src/shogun/converter/ica/ICAConverter.cpp index d43b68b3ad2..64a381ac6d9 100644 --- a/src/shogun/converter/ica/ICAConverter.cpp +++ b/src/shogun/converter/ica/ICAConverter.cpp @@ -12,12 +12,12 @@ using namespace shogun; using namespace Eigen; -CICAConverter::CICAConverter() : CConverter() +ICAConverter::ICAConverter() : Converter() { init(); } -void CICAConverter::init() +void ICAConverter::init() { m_mixing_matrix = SGMatrix(); max_iter = 200; @@ -28,41 +28,41 @@ void CICAConverter::init() SG_ADD(&tol, "tol", "the convergence tolerance"); } -CICAConverter::~CICAConverter() +ICAConverter::~ICAConverter() { } -void CICAConverter::set_mixing_matrix(SGMatrix mixing_matrix) +void ICAConverter::set_mixing_matrix(SGMatrix mixing_matrix) { m_mixing_matrix = mixing_matrix; } -SGMatrix CICAConverter::get_mixing_matrix() const +SGMatrix ICAConverter::get_mixing_matrix() const { return m_mixing_matrix; } -void CICAConverter::set_max_iter(int iter) +void ICAConverter::set_max_iter(int iter) { max_iter = iter; } -int CICAConverter::get_max_iter() const +int ICAConverter::get_max_iter() const { return max_iter; } -void CICAConverter::set_tol(float64_t _tol) +void ICAConverter::set_tol(float64_t _tol) { tol = _tol; } -float64_t CICAConverter::get_tol() const +float64_t ICAConverter::get_tol() const { return tol; } -void CICAConverter::fit(CFeatures* features) +void ICAConverter::fit(std::shared_ptr features) { require(features, "Features are not provided"); require( @@ -72,37 +72,37 @@ void CICAConverter::fit(CFeatures* features) features->get_feature_type() == F_DREAL, "ICA converters only work with real features"); - fit_dense(static_cast*>(features)); + fit_dense(std::dynamic_pointer_cast>(features)); } -CFeatures* CICAConverter::transform(CFeatures* features, bool inplace) +std::shared_ptr ICAConverter::transform(std::shared_ptr features, bool inplace) { require(m_mixing_matrix.matrix, "ICAConverter has not been fitted."); - auto X = features->as>()->get_feature_matrix(); + auto X = std::dynamic_pointer_cast>(features)->get_feature_matrix(); if (!inplace) X = X.clone(); - Map EX(X.matrix, X.num_rows, X.num_cols); - Map C( + Eigen::Map EX(X.matrix, X.num_rows, X.num_cols); + Eigen::Map C( m_mixing_matrix.matrix, m_mixing_matrix.num_rows, m_mixing_matrix.num_cols); // Unmix EX = C.inverse() * EX; - return new CDenseFeatures(X); + return std::make_shared>(X); } -CFeatures* CICAConverter::inverse_transform(CFeatures* features, bool inplace) +std::shared_ptr ICAConverter::inverse_transform(std::shared_ptr features, bool inplace) { require(m_mixing_matrix.matrix, "ICAConverter has not been fitted."); - auto X = features->as>()->get_feature_matrix(); + auto X = std::dynamic_pointer_cast>(features)->get_feature_matrix(); if (!inplace) X = X.clone(); linalg::matrix_prod(m_mixing_matrix, X, X); - return new CDenseFeatures(X); + return std::make_shared>(X); } diff --git a/src/shogun/converter/ica/ICAConverter.h b/src/shogun/converter/ica/ICAConverter.h index e02d56df033..420cf8fc766 100644 --- a/src/shogun/converter/ica/ICAConverter.h +++ b/src/shogun/converter/ica/ICAConverter.h @@ -16,48 +16,48 @@ namespace shogun { template - class CDenseFeatures; + class DenseFeatures; /** @brief class ICAConverter - * Base class for ICA algorithms that apply to CDenseFeatures + * Base class for ICA algorithms that apply to DenseFeatures */ - class CICAConverter : public CConverter + class ICAConverter : public Converter { public: /** constructor */ - CICAConverter(); + ICAConverter(); /** destructor */ - virtual ~CICAConverter(); + virtual ~ICAConverter(); /** Fit the ICA converter to features * Subclasses should implement this method to calculate the mixing * matrix * @param features the training features, should be an instance of - * CDenseFeatures + * DenseFeatures */ - virtual void fit(CFeatures* features); + virtual void fit(std::shared_ptr features); /** Apply the ICA converter to features by multiplying the feature * matrix by the unmixing matirx. * @param features the features to transformed, should be an instance of - * CDenseFeatures + * DenseFeatures * @param inplace transform in place * @return the result feature object after applying the ICA converter */ - virtual CFeatures* transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr transform(std::shared_ptr features, bool inplace = true); /** Inverse apply the ICA converter to features by multiplying the * feature matrix by the mixing matirx. * @param features the features to transformed, should be an instance of - * CDenseFeatures + * DenseFeatures * @param inplace transform in place * @return the result feature object after inverse applying the ICA * converter */ - virtual CFeatures* - inverse_transform(CFeatures* features, bool inplace = true); + virtual std::shared_ptr + inverse_transform(std::shared_ptr features, bool inplace = true); /** setter for mixing matrix, if the mixing matrix is set it will be * used as an initial guess if supported by the algorithm @@ -98,7 +98,7 @@ namespace shogun /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features) = 0; + virtual void fit_dense(std::shared_ptr> features) = 0; /** mixing_matrix */ SGMatrix m_mixing_matrix; diff --git a/src/shogun/converter/ica/Jade.cpp b/src/shogun/converter/ica/Jade.cpp index 0dabe52312b..87f115ea844 100644 --- a/src/shogun/converter/ica/Jade.cpp +++ b/src/shogun/converter/ica/Jade.cpp @@ -20,27 +20,27 @@ using namespace shogun; using namespace Eigen; -CJade::CJade() : CICAConverter() +Jade::Jade() : ICAConverter() { init(); } -void CJade::init() +void Jade::init() { m_cumulant_matrix = SGMatrix(); SG_ADD(&m_cumulant_matrix, "cumulant_matrix", "m_cumulant_matrix"); } -CJade::~CJade() +Jade::~Jade() { } -SGMatrix CJade::get_cumulant_matrix() const +SGMatrix Jade::get_cumulant_matrix() const { return m_cumulant_matrix; } -void CJade::fit_dense(CDenseFeatures* features) +void Jade::fit_dense(std::shared_ptr> features) { ASSERT(features); @@ -50,7 +50,7 @@ void CJade::fit_dense(CDenseFeatures* features) int T = X.num_cols; int m = n; - Map EX(X.matrix,n,T); + Eigen::Map EX(X.matrix,n,T); // Mean center X VectorXd mean = (EX.rowwise().sum() / (float64_t)T); @@ -91,7 +91,7 @@ void CJade::fit_dense(CDenseFeatures* features) int dimsymm = (m * ( m + 1)) / 2; // Dim. of the space of real symm matrices int nbcm = dimsymm; // number of cumulant matrices m_cumulant_matrix = SGMatrix(m,m*nbcm); // Storage for cumulant matrices - Map CM(m_cumulant_matrix.matrix,m,m*nbcm); + Eigen::Map CM(m_cumulant_matrix.matrix,m,m*nbcm); MatrixXd R(m,m); R.setIdentity(); MatrixXd Qij = MatrixXd::Zero(m,m); // Temp for a cum. matrix VectorXd Xim = VectorXd::Zero(m); // Temp @@ -130,13 +130,13 @@ void CJade::fit_dense(CDenseFeatures* features) for (int i = 0; i < nbcm; i++) { - Map EM(M.get_matrix(i),m,m); + Eigen::Map EM(M.get_matrix(i),m,m); EM = CM.block(0,i*m,m,m); } // Diagonalize - SGMatrix Q = CJADiagOrth::diagonalize(M, m_mixing_matrix, tol, max_iter); - Map EQ(Q.matrix,m,m); + SGMatrix Q = JADiagOrth::diagonalize(M, m_mixing_matrix, tol, max_iter); + Eigen::Map EQ(Q.matrix,m,m); EQ = -1 * EQ.inverse(); #ifdef DEBUG_JADE @@ -146,7 +146,7 @@ void CJade::fit_dense(CDenseFeatures* features) // Separating matrix SGMatrix sep_matrix = SGMatrix(m,m); - Map C(sep_matrix.matrix,m,m); + Eigen::Map C(sep_matrix.matrix,m,m); C = EQ.transpose() * B; // Sort @@ -187,6 +187,6 @@ void CJade::fit_dense(CDenseFeatures* features) #endif m_mixing_matrix = SGMatrix(m,m); - Map Emixing_matrix(m_mixing_matrix.matrix,m,m); + Eigen::Map Emixing_matrix(m_mixing_matrix.matrix,m,m); Emixing_matrix = C.inverse(); } diff --git a/src/shogun/converter/ica/Jade.h b/src/shogun/converter/ica/Jade.h index abe46366cd1..a86033b0f89 100644 --- a/src/shogun/converter/ica/Jade.h +++ b/src/shogun/converter/ica/Jade.h @@ -14,7 +14,7 @@ namespace shogun { -class CFeatures; +class Features; //#define DEBUG_JADE @@ -30,15 +30,15 @@ class CFeatures; * (Vol. 140, No. 6, pp. 362-370). IET Digital Library. * */ -class CJade: public CICAConverter +class Jade: public ICAConverter { public: /** constructor */ - CJade(); + Jade(); /** destructor */ - virtual ~CJade(); + virtual ~Jade(); /** getter for cumulant_matrix * @return cumulant_matrix @@ -53,7 +53,7 @@ class CJade: public CICAConverter /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features); + virtual void fit_dense(std::shared_ptr> features); private: diff --git a/src/shogun/converter/ica/JediSep.cpp b/src/shogun/converter/ica/JediSep.cpp index b133ad664d3..38aa30d17da 100644 --- a/src/shogun/converter/ica/JediSep.cpp +++ b/src/shogun/converter/ica/JediSep.cpp @@ -18,12 +18,12 @@ using namespace Eigen; namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); }; -CJediSep::CJediSep() : CICAConverter() +JediSep::JediSep() : ICAConverter() { init(); } -void CJediSep::init() +void JediSep::init() { m_tau = SGVector(4); m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3; @@ -33,26 +33,26 @@ void CJediSep::init() SG_ADD(&m_tau, "tau", "tau vector", ParameterProperties::HYPER); } -CJediSep::~CJediSep() +JediSep::~JediSep() { } -void CJediSep::set_tau(SGVector tau) +void JediSep::set_tau(SGVector tau) { m_tau = tau; } -SGVector CJediSep::get_tau() const +SGVector JediSep::get_tau() const { return m_tau; } -SGNDArray CJediSep::get_covs() const +SGNDArray JediSep::get_covs() const { return m_covs; } -void CJediSep::fit_dense(CDenseFeatures* features) +void JediSep::fit_dense(std::shared_ptr> features) { auto X = features->get_feature_matrix(); @@ -76,7 +76,7 @@ void CJediSep::fit_dense(CDenseFeatures* features) } // Diagonalize - SGMatrix Q = CJediDiag::diagonalize(m_covs, m_mixing_matrix, tol, max_iter); + SGMatrix Q = JediDiag::diagonalize(m_covs, m_mixing_matrix, tol, max_iter); Map EQ(Q.matrix,n,n); // Compute Mixing Matrix diff --git a/src/shogun/converter/ica/JediSep.h b/src/shogun/converter/ica/JediSep.h index a118946670e..ea0bfddc72b 100644 --- a/src/shogun/converter/ica/JediSep.h +++ b/src/shogun/converter/ica/JediSep.h @@ -15,7 +15,7 @@ namespace shogun { -class CFeatures; +class Features; /** @brief class JediSep * @@ -28,15 +28,15 @@ class CFeatures; * Signal Processing, IEEE Transactions on, 57(6), 2222-2231. * */ -class CJediSep: public CICAConverter +class JediSep: public ICAConverter { public: /** constructor */ - CJediSep(); + JediSep(); /** destructor */ - virtual ~CJediSep(); + virtual ~JediSep(); /** getter for tau parameter * @return tau vector @@ -61,7 +61,7 @@ class CJediSep: public CICAConverter /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features); + virtual void fit_dense(std::shared_ptr> features); private: diff --git a/src/shogun/converter/ica/SOBI.cpp b/src/shogun/converter/ica/SOBI.cpp index b89a691f1f5..2d4eb908be0 100644 --- a/src/shogun/converter/ica/SOBI.cpp +++ b/src/shogun/converter/ica/SOBI.cpp @@ -18,12 +18,12 @@ using namespace Eigen; namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); }; -CSOBI::CSOBI() : CICAConverter() +SOBI::SOBI() : ICAConverter() { init(); } -void CSOBI::init() +void SOBI::init() { m_tau = SGVector(4); m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3; @@ -33,26 +33,26 @@ void CSOBI::init() SG_ADD(&m_tau, "tau", "tau vector", ParameterProperties::HYPER); } -CSOBI::~CSOBI() +SOBI::~SOBI() { } -void CSOBI::set_tau(SGVector tau) +void SOBI::set_tau(SGVector tau) { m_tau = tau; } -SGVector CSOBI::get_tau() const +SGVector SOBI::get_tau() const { return m_tau; } -SGNDArray CSOBI::get_covs() const +SGNDArray SOBI::get_covs() const { return m_covs; } -void CSOBI::fit_dense(CDenseFeatures* features) +void SOBI::fit_dense(std::shared_ptr> features) { auto X = features->get_feature_matrix(); @@ -86,7 +86,7 @@ void CSOBI::fit_dense(CDenseFeatures* features) } // Diagonalize - SGMatrix Q = CJADiagOrth::diagonalize(m_covs); + SGMatrix Q = JADiagOrth::diagonalize(m_covs); Map EQ(Q.matrix,n,n); // Compute Mixing Matrix diff --git a/src/shogun/converter/ica/SOBI.h b/src/shogun/converter/ica/SOBI.h index aa70fb75b26..3d0edbceb9b 100644 --- a/src/shogun/converter/ica/SOBI.h +++ b/src/shogun/converter/ica/SOBI.h @@ -15,7 +15,7 @@ namespace shogun { -class CFeatures; +class Features; /** @brief class SOBI * @@ -30,15 +30,15 @@ class CFeatures; * Signal Processing, IEEE Transactions on, 45(2), 434-444. * */ -class CSOBI: public CICAConverter +class SOBI: public ICAConverter { public: /** constructor */ - CSOBI(); + SOBI(); /** destructor */ - virtual ~CSOBI(); + virtual ~SOBI(); /** getter for tau parameter * @return tau vector @@ -63,7 +63,7 @@ class CSOBI: public CICAConverter /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features); + virtual void fit_dense(std::shared_ptr> features); private: diff --git a/src/shogun/converter/ica/UWedgeSep.cpp b/src/shogun/converter/ica/UWedgeSep.cpp index f7166eae0e4..3ab88ca20a4 100644 --- a/src/shogun/converter/ica/UWedgeSep.cpp +++ b/src/shogun/converter/ica/UWedgeSep.cpp @@ -18,12 +18,12 @@ using namespace Eigen; namespace { MatrixXd cor(MatrixXd x, int tau = 0, bool mean_flag = true); }; -CUWedgeSep::CUWedgeSep() : CICAConverter() +UWedgeSep::UWedgeSep() : ICAConverter() { init(); } -void CUWedgeSep::init() +void UWedgeSep::init() { m_tau = SGVector(4); m_tau[0]=0; m_tau[1]=1; m_tau[2]=2; m_tau[3]=3; @@ -33,26 +33,26 @@ void CUWedgeSep::init() SG_ADD(&m_tau, "tau", "tau vector", ParameterProperties::HYPER); } -CUWedgeSep::~CUWedgeSep() +UWedgeSep::~UWedgeSep() { } -void CUWedgeSep::set_tau(SGVector tau) +void UWedgeSep::set_tau(SGVector tau) { m_tau = tau; } -SGVector CUWedgeSep::get_tau() const +SGVector UWedgeSep::get_tau() const { return m_tau; } -SGNDArray CUWedgeSep::get_covs() const +SGNDArray UWedgeSep::get_covs() const { return m_covs; } -void CUWedgeSep::fit_dense(CDenseFeatures* features) +void UWedgeSep::fit_dense(std::shared_ptr> features) { auto X = features->get_feature_matrix(); @@ -76,7 +76,7 @@ void CUWedgeSep::fit_dense(CDenseFeatures* features) } // Diagonalize - SGMatrix Q = CUWedge::diagonalize(m_covs, m_mixing_matrix, tol, max_iter); + SGMatrix Q = UWedge::diagonalize(m_covs, m_mixing_matrix, tol, max_iter); Map EQ(Q.matrix,n,n); // Compute Mixing Matrix diff --git a/src/shogun/converter/ica/UWedgeSep.h b/src/shogun/converter/ica/UWedgeSep.h index feff464d01b..6c3d2d0fcc1 100644 --- a/src/shogun/converter/ica/UWedgeSep.h +++ b/src/shogun/converter/ica/UWedgeSep.h @@ -15,7 +15,7 @@ namespace shogun { -class CFeatures; +class Features; /** @brief class UWedgeSep * @@ -28,15 +28,15 @@ class CFeatures; * Signal Processing, IEEE Transactions on, 57(3), 878-891. * */ -class CUWedgeSep: public CICAConverter +class UWedgeSep: public ICAConverter { public: /** constructor */ - CUWedgeSep(); + UWedgeSep(); /** destructor */ - virtual ~CUWedgeSep(); + virtual ~UWedgeSep(); /** getter for tau parameter * @return tau vector @@ -61,7 +61,7 @@ class CUWedgeSep: public CICAConverter /** init */ void init(); - virtual void fit_dense(CDenseFeatures* features); + virtual void fit_dense(std::shared_ptr> features); private: diff --git a/src/shogun/distance/AttenuatedEuclideanDistance.cpp b/src/shogun/distance/AttenuatedEuclideanDistance.cpp index 391c5989702..24b87f6027d 100644 --- a/src/shogun/distance/AttenuatedEuclideanDistance.cpp +++ b/src/shogun/distance/AttenuatedEuclideanDistance.cpp @@ -11,50 +11,50 @@ using namespace shogun; -CAttenuatedEuclideanDistance::CAttenuatedEuclideanDistance() : CRealDistance() +AttenuatedEuclideanDistance::AttenuatedEuclideanDistance() : RealDistance() { init(); } -CAttenuatedEuclideanDistance::CAttenuatedEuclideanDistance(CDenseFeatures* l, CDenseFeatures* r) -: CRealDistance() +AttenuatedEuclideanDistance::AttenuatedEuclideanDistance(std::shared_ptr> l, std::shared_ptr> r) +: RealDistance() { init(); init(l, r); } -CAttenuatedEuclideanDistance::~CAttenuatedEuclideanDistance() +AttenuatedEuclideanDistance::~AttenuatedEuclideanDistance() { cleanup(); } -bool CAttenuatedEuclideanDistance::init(CFeatures* l, CFeatures* r) +bool AttenuatedEuclideanDistance::init(std::shared_ptr l, std::shared_ptr r) { - CRealDistance::init(l, r); + RealDistance::init(l, r); return true; } -void CAttenuatedEuclideanDistance::cleanup() +void AttenuatedEuclideanDistance::cleanup() { } -float64_t CAttenuatedEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t AttenuatedEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t result=0; - float64_t* avec=((CDenseFeatures*) lhs)-> + float64_t* avec=(std::static_pointer_cast>(lhs))-> get_feature_vector(idx_a, alen, afree); - float64_t* bvec=((CDenseFeatures*) rhs)-> + float64_t* bvec=(std::static_pointer_cast>(rhs))-> get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); if (disable_sqrt) return result; @@ -62,7 +62,7 @@ float64_t CAttenuatedEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) return std::sqrt(result); } -void CAttenuatedEuclideanDistance::init() +void AttenuatedEuclideanDistance::init() { disable_sqrt=false; diff --git a/src/shogun/distance/AttenuatedEuclideanDistance.h b/src/shogun/distance/AttenuatedEuclideanDistance.h index 183f7adfeaa..98d7e436bcc 100644 --- a/src/shogun/distance/AttenuatedEuclideanDistance.h +++ b/src/shogun/distance/AttenuatedEuclideanDistance.h @@ -33,19 +33,19 @@ namespace shogun * \f] * */ -class CAttenuatedEuclideanDistance: public CRealDistance +class AttenuatedEuclideanDistance: public RealDistance { public: /** default constructor */ - CAttenuatedEuclideanDistance(); + AttenuatedEuclideanDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CAttenuatedEuclideanDistance(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CAttenuatedEuclideanDistance(); + AttenuatedEuclideanDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~AttenuatedEuclideanDistance(); /** init distance * @@ -53,7 +53,7 @@ class CAttenuatedEuclideanDistance: public CRealDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/BrayCurtisDistance.cpp b/src/shogun/distance/BrayCurtisDistance.cpp index 3074dc3e834..337692a8fea 100644 --- a/src/shogun/distance/BrayCurtisDistance.cpp +++ b/src/shogun/distance/BrayCurtisDistance.cpp @@ -12,42 +12,40 @@ using namespace shogun; -CBrayCurtisDistance::CBrayCurtisDistance() -: CDenseDistance() +BrayCurtisDistance::BrayCurtisDistance() +: DenseDistance() { } -CBrayCurtisDistance::CBrayCurtisDistance(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +BrayCurtisDistance::BrayCurtisDistance(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CBrayCurtisDistance::~CBrayCurtisDistance() +BrayCurtisDistance::~BrayCurtisDistance() { cleanup(); } -bool CBrayCurtisDistance::init(CFeatures* l, CFeatures* r) +bool BrayCurtisDistance::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDenseDistance::init(l,r); - - return result; + return DenseDistance::init(l,r); } -void CBrayCurtisDistance::cleanup() +void BrayCurtisDistance::cleanup() { } -float64_t CBrayCurtisDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t BrayCurtisDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -61,8 +59,8 @@ float64_t CBrayCurtisDistance::compute(int32_t idx_a, int32_t idx_b) } } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); // trap division by zero if(s2==0) diff --git a/src/shogun/distance/BrayCurtisDistance.h b/src/shogun/distance/BrayCurtisDistance.h index 79dd88371b5..018b7a3849d 100644 --- a/src/shogun/distance/BrayCurtisDistance.h +++ b/src/shogun/distance/BrayCurtisDistance.h @@ -25,19 +25,19 @@ namespace shogun * \f] * */ -class CBrayCurtisDistance: public CDenseDistance +class BrayCurtisDistance: public DenseDistance { public: /** default constructor */ - CBrayCurtisDistance(); + BrayCurtisDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CBrayCurtisDistance(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CBrayCurtisDistance(); + BrayCurtisDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~BrayCurtisDistance(); /** init distance * @@ -45,7 +45,7 @@ class CBrayCurtisDistance: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/CanberraMetric.cpp b/src/shogun/distance/CanberraMetric.cpp index 1bed12dfd75..c70e4de4424 100644 --- a/src/shogun/distance/CanberraMetric.cpp +++ b/src/shogun/distance/CanberraMetric.cpp @@ -12,43 +12,43 @@ using namespace shogun; -CCanberraMetric::CCanberraMetric() -: CDenseDistance() +CanberraMetric::CanberraMetric() +: DenseDistance() { } -CCanberraMetric::CCanberraMetric(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +CanberraMetric::CanberraMetric(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CCanberraMetric::~CCanberraMetric() +CanberraMetric::~CanberraMetric() { cleanup(); } -bool CCanberraMetric::init(CFeatures* l, CFeatures* r) +bool CanberraMetric::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDenseDistance::init(l,r); + bool result=DenseDistance::init(l,r); return result; } -void CCanberraMetric::cleanup() +void CanberraMetric::cleanup() { } -float64_t CCanberraMetric::compute(int32_t idx_a, int32_t idx_b) +float64_t CanberraMetric::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -64,8 +64,8 @@ float64_t CCanberraMetric::compute(int32_t idx_a, int32_t idx_b) } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/distance/CanberraMetric.h b/src/shogun/distance/CanberraMetric.h index c7933e5bb0c..3282e704d02 100644 --- a/src/shogun/distance/CanberraMetric.h +++ b/src/shogun/distance/CanberraMetric.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evan Shelhamer, Yuyu Zhang, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Evan Shelhamer, Yuyu Zhang, Sergey Lisitsyn, * Viktor Gal */ @@ -16,7 +16,7 @@ namespace shogun { - template class CDenseFeatures; + template class DenseFeatures; /** @brief class CanberraMetric * @@ -31,19 +31,19 @@ namespace shogun * A summation element has range [0,1]. Note that \f$d(x,0)=d(0,x')=n\f$ * and \f$d(0,0)=0\f$. */ -class CCanberraMetric: public CDenseDistance +class CanberraMetric: public DenseDistance { public: /** default constructor */ - CCanberraMetric(); + CanberraMetric(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CCanberraMetric(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CCanberraMetric(); + CanberraMetric(std::shared_ptr> l, std::shared_ptr> r); + virtual ~CanberraMetric(); /** init distance * @@ -51,7 +51,7 @@ class CCanberraMetric: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/CanberraWordDistance.cpp b/src/shogun/distance/CanberraWordDistance.cpp index 5908e5e5fb7..c512d54d9ea 100644 --- a/src/shogun/distance/CanberraWordDistance.cpp +++ b/src/shogun/distance/CanberraWordDistance.cpp @@ -12,43 +12,43 @@ using namespace shogun; -CCanberraWordDistance::CCanberraWordDistance() -: CStringDistance() +CanberraWordDistance::CanberraWordDistance() +: StringDistance() { SG_DEBUG("CCanberraWordDistance created") } -CCanberraWordDistance::CCanberraWordDistance( - CStringFeatures* l, CStringFeatures* r) -: CStringDistance() +CanberraWordDistance::CanberraWordDistance( + std::shared_ptr> l, std::shared_ptr> r) +: StringDistance() { SG_DEBUG("CCanberraWordDistance created") init(l, r); } -CCanberraWordDistance::~CCanberraWordDistance() +CanberraWordDistance::~CanberraWordDistance() { cleanup(); } -bool CCanberraWordDistance::init(CFeatures* l, CFeatures* r) +bool CanberraWordDistance::init(std::shared_ptr l, std::shared_ptr r) { - return CStringDistance::init(l,r); + return StringDistance::init(l,r); } -void CCanberraWordDistance::cleanup() +void CanberraWordDistance::cleanup() { } -float64_t CCanberraWordDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t CanberraWordDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)-> + uint16_t* avec=(std::static_pointer_cast>(lhs))-> get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)-> + uint16_t* bvec=(std::static_pointer_cast>(rhs))-> get_feature_vector(idx_b, blen, free_bvec); float64_t result=0; @@ -71,7 +71,7 @@ float64_t CCanberraWordDistance::compute(int32_t idx_a, int32_t idx_b) right_idx++; result += - CMath::abs((float64_t) + Math::abs((float64_t) ((left_idx-old_left_idx)-(right_idx-old_right_idx)))/ ((float64_t) ((left_idx-old_left_idx) + (right_idx-old_right_idx))); @@ -110,9 +110,9 @@ float64_t CCanberraWordDistance::compute(int32_t idx_a, int32_t idx_b) while (right_idx< blen && bvec[right_idx]==sym) right_idx++; } - ((CStringFeatures*) lhs)-> + (std::static_pointer_cast>(lhs))-> free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)-> + (std::static_pointer_cast>(rhs))-> free_feature_vector(bvec, idx_b, free_bvec); return result; diff --git a/src/shogun/distance/CanberraWordDistance.h b/src/shogun/distance/CanberraWordDistance.h index 2803d287bb3..489f3c23967 100644 --- a/src/shogun/distance/CanberraWordDistance.h +++ b/src/shogun/distance/CanberraWordDistance.h @@ -17,19 +17,19 @@ namespace shogun { /**@brief class CanberraWordDistance */ -class CCanberraWordDistance: public CStringDistance +class CanberraWordDistance: public StringDistance { public: /** default constructor */ - CCanberraWordDistance(); + CanberraWordDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CCanberraWordDistance(CStringFeatures* l, CStringFeatures* r); - virtual ~CCanberraWordDistance(); + CanberraWordDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~CanberraWordDistance(); /** init distance * @@ -37,7 +37,7 @@ class CCanberraWordDistance: public CStringDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/ChebyshewMetric.cpp b/src/shogun/distance/ChebyshewMetric.cpp index b26fcab5898..192e79761fa 100644 --- a/src/shogun/distance/ChebyshewMetric.cpp +++ b/src/shogun/distance/ChebyshewMetric.cpp @@ -12,49 +12,49 @@ using namespace shogun; -CChebyshewMetric::CChebyshewMetric() : CDenseDistance() +ChebyshewMetric::ChebyshewMetric() : DenseDistance() { } -CChebyshewMetric::CChebyshewMetric(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +ChebyshewMetric::ChebyshewMetric(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CChebyshewMetric::~CChebyshewMetric() +ChebyshewMetric::~ChebyshewMetric() { cleanup(); } -bool CChebyshewMetric::init(CFeatures* l, CFeatures* r) +bool ChebyshewMetric::init(std::shared_ptr l, std::shared_ptr r) { - return CDenseDistance::init(l,r); + return DenseDistance::init(l,r); } -void CChebyshewMetric::cleanup() +void ChebyshewMetric::cleanup() { } -float64_t CChebyshewMetric::compute(int32_t idx_a, int32_t idx_b) +float64_t ChebyshewMetric::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result=DBL_MIN; for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/distance/ChebyshewMetric.h b/src/shogun/distance/ChebyshewMetric.h index 0e5b0302097..996df1e9052 100644 --- a/src/shogun/distance/ChebyshewMetric.h +++ b/src/shogun/distance/ChebyshewMetric.h @@ -25,19 +25,19 @@ namespace shogun * * @see Wikipedia: Chebyshev distance */ -class CChebyshewMetric: public CDenseDistance +class ChebyshewMetric: public DenseDistance { public: /** default constructor */ - CChebyshewMetric(); + ChebyshewMetric(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CChebyshewMetric(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CChebyshewMetric(); + ChebyshewMetric(std::shared_ptr> l, std::shared_ptr> r); + virtual ~ChebyshewMetric(); /** init distance * @@ -45,7 +45,7 @@ class CChebyshewMetric: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/ChiSquareDistance.cpp b/src/shogun/distance/ChiSquareDistance.cpp index 9090cc09d6f..a579b61edb3 100644 --- a/src/shogun/distance/ChiSquareDistance.cpp +++ b/src/shogun/distance/ChiSquareDistance.cpp @@ -12,41 +12,41 @@ using namespace shogun; -CChiSquareDistance::CChiSquareDistance() : CDenseDistance() +ChiSquareDistance::ChiSquareDistance() : DenseDistance() { } -CChiSquareDistance::CChiSquareDistance(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +ChiSquareDistance::ChiSquareDistance(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CChiSquareDistance::~CChiSquareDistance() +ChiSquareDistance::~ChiSquareDistance() { cleanup(); } -bool CChiSquareDistance::init(CFeatures* l, CFeatures* r) +bool ChiSquareDistance::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDenseDistance::init(l,r); + bool result=DenseDistance::init(l,r); return result; } -void CChiSquareDistance::cleanup() +void ChiSquareDistance::cleanup() { } -float64_t CChiSquareDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t ChiSquareDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -62,8 +62,8 @@ float64_t CChiSquareDistance::compute(int32_t idx_a, int32_t idx_b) } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/distance/ChiSquareDistance.h b/src/shogun/distance/ChiSquareDistance.h index 9121eae9996..b7f7628eeac 100644 --- a/src/shogun/distance/ChiSquareDistance.h +++ b/src/shogun/distance/ChiSquareDistance.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evan Shelhamer, Yuyu Zhang, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Evan Shelhamer, Yuyu Zhang, Sergey Lisitsyn, * Viktor Gal */ @@ -28,19 +28,19 @@ namespace shogun * @see K. Rieck, P. Laskov. Linear-Time Computation of Similarity Measures * for Sequential Data. Journal of Machine Learning Research, 9:23--48,2008. */ -class CChiSquareDistance: public CDenseDistance +class ChiSquareDistance: public DenseDistance { public: /** default constructor */ - CChiSquareDistance(); + ChiSquareDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CChiSquareDistance(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CChiSquareDistance(); + ChiSquareDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~ChiSquareDistance(); /** init distance * @@ -48,7 +48,7 @@ class CChiSquareDistance: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/CosineDistance.cpp b/src/shogun/distance/CosineDistance.cpp index 52c3038cb23..07756a485ce 100644 --- a/src/shogun/distance/CosineDistance.cpp +++ b/src/shogun/distance/CosineDistance.cpp @@ -12,40 +12,40 @@ using namespace shogun; -CCosineDistance::CCosineDistance() -: CDenseDistance() +CosineDistance::CosineDistance() +: DenseDistance() { } -CCosineDistance::CCosineDistance(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +CosineDistance::CosineDistance(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CCosineDistance::~CCosineDistance() +CosineDistance::~CosineDistance() { cleanup(); } -bool CCosineDistance::init(CFeatures* l, CFeatures* r) +bool CosineDistance::init(std::shared_ptr l, std::shared_ptr r) { - return CDenseDistance::init(l,r); + return DenseDistance::init(l,r); } -void CCosineDistance::cleanup() +void CosineDistance::cleanup() { } -float64_t CCosineDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t CosineDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t s=0; @@ -61,8 +61,8 @@ float64_t CCosineDistance::compute(int32_t idx_a, int32_t idx_b) } } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); s=sqrt(sa)*sqrt(sb); diff --git a/src/shogun/distance/CosineDistance.h b/src/shogun/distance/CosineDistance.h index 89e94da0271..32a2dcf5a43 100644 --- a/src/shogun/distance/CosineDistance.h +++ b/src/shogun/distance/CosineDistance.h @@ -30,19 +30,19 @@ namespace shogun * Cosine similarity * @see CTanimotoDistance */ -class CCosineDistance: public CDenseDistance +class CosineDistance: public DenseDistance { public: /** default constructor */ - CCosineDistance(); + CosineDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CCosineDistance(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CCosineDistance(); + CosineDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~CosineDistance(); /** init distance * @@ -50,7 +50,7 @@ class CCosineDistance: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/CustomDistance.cpp b/src/shogun/distance/CustomDistance.cpp index 9c76432d65e..1399e8d9cda 100644 --- a/src/shogun/distance/CustomDistance.cpp +++ b/src/shogun/distance/CustomDistance.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Fernando Iglesias, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Fernando Iglesias, Sergey Lisitsyn, * Evan Shelhamer */ @@ -14,12 +14,12 @@ using namespace shogun; -CCustomDistance::CCustomDistance() : CDistance() +CustomDistance::CustomDistance() : Distance() { init(); } -CCustomDistance::CCustomDistance(CDistance* d) : CDistance() +CustomDistance::CustomDistance(std::shared_ptr d) : Distance() { init(); @@ -60,8 +60,8 @@ CCustomDistance::CCustomDistance(CDistance* d) : CDistance() dummy_init(num_rows, num_cols); } -CCustomDistance::CCustomDistance(const SGMatrix distance_matrix) -: CDistance() +CustomDistance::CustomDistance(const SGMatrix distance_matrix) +: Distance() { init(); set_full_distance_matrix_from_full(distance_matrix.matrix, @@ -69,33 +69,33 @@ CCustomDistance::CCustomDistance(const SGMatrix distance_matrix) distance_matrix.num_cols); } -CCustomDistance::CCustomDistance(const float64_t* dm, int32_t rows, int32_t cols) -: CDistance() +CustomDistance::CustomDistance(const float64_t* dm, int32_t rows, int32_t cols) +: Distance() { init(); set_full_distance_matrix_from_full(dm, rows, cols); } -CCustomDistance::CCustomDistance(const float32_t* dm, int32_t rows, int32_t cols) -: CDistance() +CustomDistance::CustomDistance(const float32_t* dm, int32_t rows, int32_t cols) +: Distance() { init(); set_full_distance_matrix_from_full(dm, rows, cols); } -CCustomDistance::~CCustomDistance() +CustomDistance::~CustomDistance() { cleanup(); } -bool CCustomDistance::dummy_init(int32_t rows, int32_t cols) +bool CustomDistance::dummy_init(int32_t rows, int32_t cols) { - return init(new CDummyFeatures(rows), new CDummyFeatures(cols)); + return init(std::make_shared(rows), std::make_shared(cols)); } -bool CCustomDistance::init(CFeatures* l, CFeatures* r) +bool CustomDistance::init(std::shared_ptr l, std::shared_ptr r) { - CDistance::init(l, r); + Distance::init(l, r); SG_DEBUG("num_vec_lhs: {} vs num_rows {}", l->get_num_vectors(), num_rows) SG_DEBUG("num_vec_rhs: {} vs num_cols {}", r->get_num_vectors(), num_cols) @@ -105,7 +105,7 @@ bool CCustomDistance::init(CFeatures* l, CFeatures* r) } -void CCustomDistance::cleanup_custom() +void CustomDistance::cleanup_custom() { SG_DEBUG("cleanup up custom distance") SG_FREE(dmatrix); @@ -115,14 +115,14 @@ void CCustomDistance::cleanup_custom() num_rows=0; } -void CCustomDistance::init() +void CustomDistance::init() { dmatrix=NULL; num_rows=0; num_cols=0; upper_diagonal=false; - m_parameters->add_matrix(&dmatrix, &num_rows, &num_cols, "dmatrix", "Distance Matrix"); + /*m_parameters->add_matrix(&dmatrix, &num_rows, &num_cols, "dmatrix", "Distance Matrix")*/; watch_param( "dmatrix", &dmatrix, &num_rows, &num_cols, AnyParameterProperties("Distance Matrix")); @@ -131,12 +131,12 @@ void CCustomDistance::init() &upper_diagonal, "upper_diagonal", "Upper diagonal"); } -void CCustomDistance::cleanup() +void CustomDistance::cleanup() { cleanup_custom(); } -float64_t CCustomDistance::compute(int32_t row, int32_t col) +float64_t CustomDistance::compute(int32_t row, int32_t col) { ASSERT(dmatrix) diff --git a/src/shogun/distance/CustomDistance.h b/src/shogun/distance/CustomDistance.h index e034fd6cbb0..45a3702131c 100644 --- a/src/shogun/distance/CustomDistance.h +++ b/src/shogun/distance/CustomDistance.h @@ -25,23 +25,23 @@ namespace shogun * representation. Also note that values are stored as 32bit floats. * */ -class CCustomDistance: public CDistance +class CustomDistance: public Distance { public: /** default constructor */ - CCustomDistance(); + CustomDistance(); /** constructor * * compute custom distance from given distance matrix * @param d distance matrix */ - CCustomDistance(CDistance* d); + CustomDistance(std::shared_ptr d); /** constructor * @param distance_matrix distance matrix */ - CCustomDistance(const SGMatrix distance_matrix); + CustomDistance(const SGMatrix distance_matrix); /** constructor * @@ -53,7 +53,7 @@ class CCustomDistance: public CDistance * @param cols number of cols in matrix * @return if setting was successful */ - CCustomDistance( + CustomDistance( const float64_t* dm, int32_t rows, int32_t cols); /** constructor @@ -66,10 +66,10 @@ class CCustomDistance: public CDistance * @param cols number of cols in matrix * @return if setting was successful */ - CCustomDistance( + CustomDistance( const float32_t* dm, int32_t rows, int32_t cols); - virtual ~CCustomDistance(); + virtual ~CustomDistance(); /** initialize distance with dummy features * @@ -89,7 +89,7 @@ class CCustomDistance: public CDistance * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up distance */ virtual void cleanup(); diff --git a/src/shogun/distance/CustomMahalanobisDistance.cpp b/src/shogun/distance/CustomMahalanobisDistance.cpp index 74f854ae5cc..be611e57a03 100644 --- a/src/shogun/distance/CustomMahalanobisDistance.cpp +++ b/src/shogun/distance/CustomMahalanobisDistance.cpp @@ -11,49 +11,49 @@ using namespace shogun; using namespace Eigen; -CCustomMahalanobisDistance::CCustomMahalanobisDistance() : CRealDistance() +CustomMahalanobisDistance::CustomMahalanobisDistance() : RealDistance() { register_params(); } -CCustomMahalanobisDistance::CCustomMahalanobisDistance(CFeatures* l, CFeatures* r, SGMatrix m) -: CRealDistance() +CustomMahalanobisDistance::CustomMahalanobisDistance(std::shared_ptr l, std::shared_ptr r, SGMatrix m) +: RealDistance() { register_params(); - CRealDistance::init(l, r); + RealDistance::init(l, r); m_mahalanobis_matrix = m; } -void CCustomMahalanobisDistance::register_params() +void CustomMahalanobisDistance::register_params() { SG_ADD(&m_mahalanobis_matrix, "m_mahalanobis_matrix", "Mahalanobis matrix"); } -CCustomMahalanobisDistance::~CCustomMahalanobisDistance() +CustomMahalanobisDistance::~CustomMahalanobisDistance() { cleanup(); } -void CCustomMahalanobisDistance::cleanup() +void CustomMahalanobisDistance::cleanup() { } -const char* CCustomMahalanobisDistance::get_name() const +const char* CustomMahalanobisDistance::get_name() const { return "CustomMahalanobisDistance"; } -EDistanceType CCustomMahalanobisDistance::get_distance_type() +EDistanceType CustomMahalanobisDistance::get_distance_type() { return D_CUSTOMMAHALANOBIS; } -float64_t CCustomMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t CustomMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) { // Get feature vectors that will be used to compute the distance; casts // are safe, features are checked to be dense in DenseDistance::init - SGVector avec = static_cast*>(lhs)->get_feature_vector(idx_a); - SGVector bvec = static_cast*>(rhs)->get_feature_vector(idx_b); + SGVector avec = std::dynamic_pointer_cast>(lhs)->get_feature_vector(idx_a); + SGVector bvec = std::dynamic_pointer_cast>(rhs)->get_feature_vector(idx_b); require(avec.vlen == bvec.vlen, "In CCustomMahalanobisDistance::compute the " "feature vectors must have the same number of elements"); diff --git a/src/shogun/distance/CustomMahalanobisDistance.h b/src/shogun/distance/CustomMahalanobisDistance.h index 6b106719516..f5b9ae0f63a 100644 --- a/src/shogun/distance/CustomMahalanobisDistance.h +++ b/src/shogun/distance/CustomMahalanobisDistance.h @@ -25,11 +25,11 @@ namespace shogun * Mahalanobis matrix. * */ -class CCustomMahalanobisDistance : public CRealDistance +class CustomMahalanobisDistance : public RealDistance { public: /** default constructor */ - CCustomMahalanobisDistance(); + CustomMahalanobisDistance(); /** standard constructor * @@ -37,12 +37,12 @@ class CCustomMahalanobisDistance : public CRealDistance * @param r features of right hand side * @param m Mahalanobis matrix used to compute distances */ - CCustomMahalanobisDistance(CFeatures* l, CFeatures* r, SGMatrix m); + CustomMahalanobisDistance(std::shared_ptr l, std::shared_ptr r, SGMatrix m); /** destructor */ - virtual ~CCustomMahalanobisDistance(); + virtual ~CustomMahalanobisDistance(); - /** cleanup distance, here only because it is abstract in CDistance. It does nothing */ + /** cleanup distance, here only because it is abstract in Distance. It does nothing */ virtual void cleanup(); /** @return name of SGSerializable */ diff --git a/src/shogun/distance/DenseDistance.cpp b/src/shogun/distance/DenseDistance.cpp index cfe97564204..c677a141b44 100644 --- a/src/shogun/distance/DenseDistance.cpp +++ b/src/shogun/distance/DenseDistance.cpp @@ -2,9 +2,9 @@ namespace shogun { -template bool CDenseDistance::init(CFeatures* l, CFeatures* r) +template bool DenseDistance::init(std::shared_ptr l, std::shared_ptr r) { - CDistance::init(l,r); + Distance::init(l,r); ASSERT(l->get_feature_class()==C_DENSE) ASSERT(r->get_feature_class()==C_DENSE) @@ -12,10 +12,11 @@ template bool CDenseDistance::init(CFeatures* l, CFeatures* r) ASSERT(r->get_feature_type()==this->get_feature_type()) - if ( ((CDenseFeatures*) l)->get_num_features() != ((CDenseFeatures*) r)->get_num_features() ) + if ( (std::static_pointer_cast>(l))->get_num_features() != (std::static_pointer_cast>(r))->get_num_features()) { error("train or test features #dimension mismatch (l:{} vs. r:{})", - ((CDenseFeatures*) l)->get_num_features(),((CDenseFeatures*) r)->get_num_features()); + (std::static_pointer_cast>(l))->get_num_features(), + (std::static_pointer_cast>(r))->get_num_features()); } return true; @@ -25,49 +26,49 @@ template bool CDenseDistance::init(CFeatures* l, CFeatures* r) * * @return feature type DREAL */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_DREAL; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_DREAL; } /** get feature type the ULONG distance can deal with * * @return feature type ULONG */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_ULONG; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_ULONG; } /** get feature type the INT distance can deal with * * @return feature type INT */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_INT; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_INT; } /** get feature type the WORD distance can deal with * * @return feature type WORD */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_WORD; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_WORD; } /** get feature type the SHORT distance can deal with * * @return feature type SHORT */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_SHORT; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_SHORT; } /** get feature type the BYTE distance can deal with * * @return feature type BYTE */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_BYTE; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_BYTE; } /** get feature type the CHAR distance can deal with * * @return feature type CHAR */ -template<> EFeatureType CDenseDistance::get_feature_type() { return F_CHAR; } +template<> EFeatureType DenseDistance::get_feature_type() { return F_CHAR; } -template class CDenseDistance; -template class CDenseDistance; -template class CDenseDistance; -template class CDenseDistance; -template class CDenseDistance; -template class CDenseDistance; -template class CDenseDistance; +template class DenseDistance; +template class DenseDistance; +template class DenseDistance; +template class DenseDistance; +template class DenseDistance; +template class DenseDistance; +template class DenseDistance; } diff --git a/src/shogun/distance/DenseDistance.h b/src/shogun/distance/DenseDistance.h index 0dc6c18ca80..c421abc6020 100644 --- a/src/shogun/distance/DenseDistance.h +++ b/src/shogun/distance/DenseDistance.h @@ -17,11 +17,11 @@ namespace shogun { /** @brief template class DenseDistance */ -template class CDenseDistance : public CDistance +template class DenseDistance : public Distance { public: /** default constructor */ - CDenseDistance() : CDistance() {} + DenseDistance() : Distance() {} /** init distance * @@ -29,7 +29,7 @@ template class CDenseDistance : public CDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** get feature class the distance can deal with * diff --git a/src/shogun/distance/DirectorDistance.h b/src/shogun/distance/DirectorDistance.h index 9bb34fb475f..c07d26c0773 100644 --- a/src/shogun/distance/DirectorDistance.h +++ b/src/shogun/distance/DirectorDistance.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evgeniy Andreev, Yuyu Zhang, Chiyuan Zhang, + * Authors: Soeren Sonnenburg, Evgeniy Andreev, Yuyu Zhang, Chiyuan Zhang, * Viktor Gal, Sergey Lisitsyn, Bjoern Esser */ @@ -20,18 +20,18 @@ namespace shogun { #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance +IGNORE_IN_CLASSLIST class DirectorDistance : public Distance { public: /* default constructor */ - CDirectorDistance(bool is_external_features) - : CDistance(), external_features(is_external_features) + DirectorDistance(bool is_external_features) + : Distance(), external_features(is_external_features) { } /** destructor */ - virtual ~CDirectorDistance() + virtual ~DirectorDistance() { cleanup(); } @@ -55,7 +55,7 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance return 0; if (!external_features) - CDistance::distance(idx_a, idx_b); + Distance::distance(idx_a, idx_b); else return compute(idx_a, idx_b); } @@ -75,7 +75,7 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance */ virtual float64_t distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound) { - return CDistance::distance(idx_a, idx_b); + return Distance::distance(idx_a, idx_b); } /** init distance @@ -87,14 +87,14 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance * @param rhs features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* lhs, CFeatures* rhs) + virtual bool init(std::shared_ptr lhs, std::shared_ptr rhs) { if (env()->get_num_threads()!=1) { io::warn("Enforcing to use only one thread due to restrictions of directors"); env()->set_num_threads(1); } - return CDistance::init(lhs, rhs); + return Distance::init(lhs, rhs); } /** cleanup distance */ @@ -109,7 +109,7 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance */ virtual int32_t get_num_vec_lhs() { - return CDistance::get_num_vec_lhs(); + return Distance::get_num_vec_lhs(); } /** get number of vectors of rhs features @@ -118,7 +118,7 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance */ virtual int32_t get_num_vec_rhs() { - return CDistance::get_num_vec_rhs(); + return Distance::get_num_vec_rhs(); } /** get number of vectors of lhs features @@ -146,7 +146,7 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance virtual bool has_features() { if (!external_features) - return CDistance::has_features(); + return Distance::has_features(); else return true; } @@ -154,19 +154,19 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance /** remove lhs and rhs from distance */ virtual void remove_lhs_and_rhs() { - CDistance::remove_lhs_and_rhs(); + Distance::remove_lhs_and_rhs(); } /// takes all necessary steps if the lhs is removed from distance matrix virtual void remove_lhs() { - CDistance::remove_lhs(); + Distance::remove_lhs(); } /// takes all necessary steps if the rhs is removed from distance matrix virtual void remove_rhs() { - CDistance::remove_rhs(); + Distance::remove_rhs(); } /** get distance type we are @@ -200,7 +200,7 @@ IGNORE_IN_CLASSLIST class CDirectorDistance : public CDistance */ virtual void set_precompute_matrix(bool flag) { - CDistance::set_precompute_matrix(flag); + Distance::set_precompute_matrix(flag); } protected: diff --git a/src/shogun/distance/Distance.cpp b/src/shogun/distance/Distance.cpp index 7c280d19ebe..23f8af205a8 100644 --- a/src/shogun/distance/Distance.cpp +++ b/src/shogun/distance/Distance.cpp @@ -34,19 +34,19 @@ using namespace shogun; -CDistance::CDistance() : CSGObject() +Distance::Distance() : SGObject() { init(); } -CDistance::CDistance(CFeatures* p_lhs, CFeatures* p_rhs) : CSGObject() +Distance::Distance(std::shared_ptr p_lhs, std::shared_ptr p_rhs) : SGObject() { init(); init(p_lhs, p_rhs); } -CDistance::~CDistance() +Distance::~Distance() { SG_FREE(precomputed_matrix); precomputed_matrix=NULL; @@ -54,13 +54,13 @@ CDistance::~CDistance() remove_lhs_and_rhs(); } -bool CDistance::init(CFeatures* l, CFeatures* r) +bool Distance::init(std::shared_ptr l, std::shared_ptr r) { require(check_compatibility(l, r), "Features are not compatible!"); //increase reference counts - SG_REF(l); - SG_REF(r); + + //remove references to previous features remove_lhs_and_rhs(); @@ -77,7 +77,7 @@ bool CDistance::init(CFeatures* l, CFeatures* r) return true; } -bool CDistance::check_compatibility(CFeatures* l, CFeatures* r) +bool Distance::check_compatibility(std::shared_ptr l, std::shared_ptr r) { require(l, "Left hand side features must be set!"); require(r, "Right hand side features must be set!"); @@ -108,51 +108,51 @@ bool CDistance::check_compatibility(CFeatures* l, CFeatures* r) return true; } -void CDistance::load(CFile* loader) +void Distance::load(std::shared_ptr loader) { SG_SET_LOCALE_C; SG_RESET_LOCALE; } -void CDistance::save(CFile* writer) +void Distance::save(std::shared_ptr writer) { SG_SET_LOCALE_C; SG_RESET_LOCALE; } -void CDistance::remove_lhs_and_rhs() +void Distance::remove_lhs_and_rhs() { - SG_UNREF(rhs); + rhs = NULL; num_rhs=0; - SG_UNREF(lhs); + lhs = NULL; num_lhs=0; } -void CDistance::remove_lhs() +void Distance::remove_lhs() { - SG_UNREF(lhs); + lhs = NULL; num_lhs=0; } /// takes all necessary steps if the rhs is removed from distance -void CDistance::remove_rhs() +void Distance::remove_rhs() { - SG_UNREF(rhs); + rhs = NULL; num_rhs=0; } -CFeatures* CDistance::replace_rhs(CFeatures* r) +std::shared_ptr Distance::replace_rhs(std::shared_ptr r) { //make sure features are compatible require(check_compatibility(lhs, r), "Features are not compatible!"); //remove references to previous rhs features - CFeatures* tmp=rhs; + auto tmp=rhs; rhs=r; num_rhs=r->get_num_vectors(); @@ -164,13 +164,13 @@ CFeatures* CDistance::replace_rhs(CFeatures* r) return tmp; } -CFeatures* CDistance::replace_lhs(CFeatures* l) +std::shared_ptr Distance::replace_lhs(std::shared_ptr l) { //make sure features are compatible require(check_compatibility(l, rhs), "Features are not compatible!"); //remove references to previous rhs features - CFeatures* tmp=lhs; + auto tmp=lhs; lhs=l; num_lhs=l->get_num_vectors(); @@ -182,7 +182,7 @@ CFeatures* CDistance::replace_lhs(CFeatures* l) return tmp; } -float64_t CDistance::distance(int32_t idx_a, int32_t idx_b) +float64_t Distance::distance(int32_t idx_a, int32_t idx_b) { require(idx_a < lhs->get_num_vectors() && idx_b < rhs->get_num_vectors() && \ idx_a >= 0 && idx_b >= 0, @@ -218,19 +218,19 @@ float64_t CDistance::distance(int32_t idx_a, int32_t idx_b) return compute(idx_a, idx_b); } -void CDistance::run_distance_rhs(SGVector& result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_a) +void Distance::run_distance_rhs(SGVector& result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_a) { for(index_t i=idx_r_start; idx_start < idx_stop; ++i,++idx_start) result.vector[i] = this->distance(idx_a,idx_start); } -void CDistance::run_distance_lhs(SGVector& result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_b) +void Distance::run_distance_lhs(SGVector& result, const index_t idx_r_start, index_t idx_start, const index_t idx_stop, const index_t idx_b) { for(index_t i=idx_r_start; idx_start < idx_stop; ++i,++idx_start) result.vector[i] = this->distance(idx_start,idx_b); } -void CDistance::do_precompute_matrix() +void Distance::do_precompute_matrix() { int32_t num_left=lhs->get_num_vectors(); int32_t num_right=rhs->get_num_vectors(); @@ -250,7 +250,7 @@ void CDistance::do_precompute_matrix() } } -void CDistance::init() +void Distance::init() { precomputed_matrix = NULL; precompute_matrix = false; @@ -264,7 +264,7 @@ void CDistance::init() } template -SGMatrix CDistance::get_distance_matrix() +SGMatrix Distance::get_distance_matrix() { T* result = NULL; @@ -333,7 +333,7 @@ SGMatrix CDistance::get_distance_matrix() pb.print_progress(); // TODO: replace with new signal - // if (CSignal::cancel_computations()) + // if (Signal::cancel_computations()) // break; } } @@ -344,5 +344,5 @@ SGMatrix CDistance::get_distance_matrix() return SGMatrix(result,m,n,true); } -template SGMatrix CDistance::get_distance_matrix(); -template SGMatrix CDistance::get_distance_matrix(); +template SGMatrix Distance::get_distance_matrix(); +template SGMatrix Distance::get_distance_matrix(); diff --git a/src/shogun/distance/Distance.h b/src/shogun/distance/Distance.h index 43c1d5b5df3..0a558135f41 100644 --- a/src/shogun/distance/Distance.h +++ b/src/shogun/distance/Distance.h @@ -22,9 +22,9 @@ namespace shogun { -class CFile; -class CMath; -class CFeatures; +class File; +class Math; +class Features; /** type of distance */ enum EDistanceType @@ -66,14 +66,14 @@ enum EDistanceType * * - \f$ d(x,y) \leq d(x,z) + d(z,y) \f$ * - * Currently distance inherited from the CDistance class should be + * Currently distance inherited from the Distance class should be * symmetric. * * The simplest example of a distance function is the Euclidean - * distance: @see CEuclideanDistance + * distance: @see EuclideanDistance * * In the means of Shogun toolbox the distance function is defined - * on the 'space' of CFeatures. + * on the 'space' of Features. * * Precomputations can be done for left hand side and right hand side features. * This has to be implemented in overloaded methods for precompute_lhs() and @@ -82,11 +82,11 @@ enum EDistanceType * when features or feature matrix are changed. * */ -class CDistance : public CSGObject +class Distance : public SGObject { public: /** default constructor */ - CDistance(); + Distance(); /** init distance * @@ -94,8 +94,8 @@ class CDistance : public CSGObject * @param rhs features of right-hand side * @return if init was successful */ - CDistance(CFeatures* lhs, CFeatures* rhs); - virtual ~CDistance(); + Distance(std::shared_ptr lhs, std::shared_ptr rhs); + virtual ~Distance(); /** get distance function for lhs feature vector a * and rhs feature vector b @@ -173,8 +173,8 @@ class CDistance : public CSGObject int32_t i_start; if (symmetric) - i_start = (int32_t)CMath::floor( - n - std::sqrt(CMath::sq((float64_t)n) - offs)); + i_start = (int32_t)Math::floor( + n - std::sqrt(Math::sq((float64_t)n) - offs)); else i_start=(int32_t) (offs/int64_t(n)); @@ -190,7 +190,7 @@ class CDistance : public CSGObject * @param rhs features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* lhs, CFeatures* rhs); + virtual bool init(std::shared_ptr lhs, std::shared_ptr rhs); /** cleanup distance * @@ -202,25 +202,25 @@ class CDistance : public CSGObject * * @param loader File object via which to load data */ - void load(CFile* loader); + void load(std::shared_ptr loader); /** save kernel matrix * * @param writer File object via which to save data */ - void save(CFile* writer); + void save(std::shared_ptr writer); /** get left-hand side features used in distance matrix * * @return left-hand side features */ - inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; }; + inline std::shared_ptr get_lhs() { return lhs; }; /** get right-hand side features used in distance matrix * * @return right-hand side features */ - inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; }; + inline std::shared_ptr get_rhs() { return rhs; }; /** replace right-hand side features used in distance matrix * @@ -230,7 +230,7 @@ class CDistance : public CSGObject * @param rhs features of right-hand side * @return replaced right-hand side features */ - virtual CFeatures* replace_rhs(CFeatures* rhs); + virtual std::shared_ptr replace_rhs(std::shared_ptr rhs); /** replace left-hand side features used in distance matrix * @@ -240,7 +240,7 @@ class CDistance : public CSGObject * @param lhs features of right-hand side * @return replaced left-hand side features */ - virtual CFeatures* replace_lhs(CFeatures* lhs); + virtual std::shared_ptr replace_lhs(std::shared_ptr lhs); /** remove lhs and rhs from distance */ virtual void remove_lhs_and_rhs(); @@ -376,7 +376,7 @@ class CDistance : public CSGObject * @param r right hand side features * @return true if the features are compatible */ - virtual bool check_compatibility(CFeatures* l, CFeatures* r); + virtual bool check_compatibility(std::shared_ptr l, std::shared_ptr r); private: void init(); @@ -393,9 +393,9 @@ class CDistance : public CSGObject bool precompute_matrix; /// feature vectors to occur on the left hand side - CFeatures* lhs; + std::shared_ptr lhs; /// feature vectors to occur on the right hand side - CFeatures* rhs; + std::shared_ptr rhs; /** number of feature vectors on the left hand side */ int32_t num_lhs; diff --git a/src/shogun/distance/EuclideanDistance.cpp b/src/shogun/distance/EuclideanDistance.cpp index 3ad3518fe91..b67d0ca2d9e 100644 --- a/src/shogun/distance/EuclideanDistance.cpp +++ b/src/shogun/distance/EuclideanDistance.cpp @@ -14,32 +14,32 @@ using namespace shogun; -CEuclideanDistance::CEuclideanDistance() : CDistance() +EuclideanDistance::EuclideanDistance() : Distance() { register_params(); } -CEuclideanDistance::CEuclideanDistance(CDotFeatures* l, CDotFeatures* r) : CDistance() +EuclideanDistance::EuclideanDistance(std::shared_ptr l, std::shared_ptr r) : Distance() { register_params(); init(l, r); } -CEuclideanDistance::~CEuclideanDistance() +EuclideanDistance::~EuclideanDistance() { cleanup(); } -bool CEuclideanDistance::init(CFeatures* l, CFeatures* r) +bool EuclideanDistance::init(std::shared_ptr l, std::shared_ptr r) { cleanup(); - CDistance::init(l, r); + Distance::init(l, r); require(l->has_property(FP_DOT), "Left hand side features must support dot property!"); require(r->has_property(FP_DOT), "Right hand side features must support dot property!"); - CDotFeatures* casted_l=static_cast(l); - CDotFeatures* casted_r=static_cast(r); + auto casted_l=std::dynamic_pointer_cast(l); + auto casted_r=std::dynamic_pointer_cast(r); require(casted_l->get_dim_feature_space()==casted_r->get_dim_feature_space(), "Number of dimension mismatch (l:{} vs. r:{})!", @@ -54,16 +54,16 @@ bool CEuclideanDistance::init(CFeatures* l, CFeatures* r) return true; } -void CEuclideanDistance::cleanup() +void EuclideanDistance::cleanup() { reset_precompute(); } -float64_t CEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t EuclideanDistance::compute(int32_t idx_a, int32_t idx_b) { float64_t result=0; - CDotFeatures* casted_lhs=static_cast(lhs); - CDotFeatures* casted_rhs=static_cast(rhs); + auto casted_lhs=std::dynamic_pointer_cast(lhs); + auto casted_rhs=std::dynamic_pointer_cast(rhs); if (lhs->get_feature_class()==rhs->get_feature_class() || lhs->support_compatible_class()) result=casted_lhs->dot(idx_a, casted_rhs, idx_b); @@ -76,7 +76,7 @@ float64_t CEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) return std::sqrt(result); } -void CEuclideanDistance::precompute_lhs() +void EuclideanDistance::precompute_lhs() { require(lhs, "Left hand side feature cannot be NULL!"); const index_t num_vec=lhs->get_num_vectors(); @@ -84,13 +84,13 @@ void CEuclideanDistance::precompute_lhs() if (m_lhs_squared_norms.vlen!=num_vec) m_lhs_squared_norms=SGVector(num_vec); - CDotFeatures* casted_lhs=static_cast(lhs); + auto casted_lhs=std::dynamic_pointer_cast(lhs); #pragma omp parallel for schedule(static, CPU_CACHE_LINE_SIZE_BYTES) for(index_t i =0; idot(i, casted_lhs, i); } -void CEuclideanDistance::precompute_rhs() +void EuclideanDistance::precompute_rhs() { require(rhs, "Right hand side feature cannot be NULL!"); const index_t num_vec=rhs->get_num_vectors(); @@ -98,30 +98,30 @@ void CEuclideanDistance::precompute_rhs() if (m_rhs_squared_norms.vlen!=num_vec) m_rhs_squared_norms=SGVector(num_vec); - CDotFeatures* casted_rhs=static_cast(rhs); + auto casted_rhs=std::dynamic_pointer_cast(rhs); #pragma omp parallel for schedule(static, CPU_CACHE_LINE_SIZE_BYTES) for(index_t i =0; idot(i, casted_rhs, i); } -void CEuclideanDistance::reset_precompute() +void EuclideanDistance::reset_precompute() { m_lhs_squared_norms=SGVector(); m_rhs_squared_norms=SGVector(); } -CFeatures* CEuclideanDistance::replace_lhs(CFeatures* l) +std::shared_ptr EuclideanDistance::replace_lhs(std::shared_ptr l) { - CFeatures* previous_lhs=CDistance::replace_lhs(l); + auto previous_lhs=Distance::replace_lhs(l); precompute_lhs(); if (lhs==rhs) m_rhs_squared_norms=m_lhs_squared_norms; return previous_lhs; } -CFeatures* CEuclideanDistance::replace_rhs(CFeatures* r) +std::shared_ptr EuclideanDistance::replace_rhs(std::shared_ptr r) { - CFeatures* previous_rhs=CDistance::replace_rhs(r); + auto previous_rhs=Distance::replace_rhs(r); if (lhs==rhs) m_rhs_squared_norms=m_lhs_squared_norms; else @@ -129,7 +129,7 @@ CFeatures* CEuclideanDistance::replace_rhs(CFeatures* r) return previous_rhs; } -void CEuclideanDistance::register_params() +void EuclideanDistance::register_params() { disable_sqrt=false; reset_precompute(); @@ -138,20 +138,20 @@ void CEuclideanDistance::register_params() SG_ADD(&m_lhs_squared_norms, "m_lhs_squared_norms", "Squared norms from features of left hand side"); } -float64_t CEuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound) +float64_t EuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_b, float64_t upper_bound) { require(lhs->get_feature_class()==C_DENSE, - "Left hand side (was {}) has to be CDenseFeatures instance!", lhs->get_name()); + "Left hand side (was {}) has to be DenseFeatures instance!", lhs->get_name()); require(rhs->get_feature_class()==C_DENSE, - "Right hand side (was {}) has to be CDenseFeatures instance!", rhs->get_name()); + "Right hand side (was {}) has to be DenseFeatures instance!", rhs->get_name()); require(lhs->get_feature_type()==F_DREAL, "Left hand side (was {}) has to be of double type!", lhs->get_name()); require(rhs->get_feature_type()==F_DREAL, "Right hand side (was {}) has to be double type!", rhs->get_name()); - CDenseFeatures* casted_lhs=static_cast*>(lhs); - CDenseFeatures* casted_rhs=static_cast*>(rhs); + auto casted_lhs=std::dynamic_pointer_cast>(lhs); + auto casted_rhs=std::dynamic_pointer_cast>(rhs); upper_bound*=upper_bound; @@ -163,7 +163,7 @@ float64_t CEuclideanDistance::distance_upper_bounded(int32_t idx_a, int32_t idx_ float64_t result=0; for (int32_t i=0; iupper_bound) break; } diff --git a/src/shogun/distance/EuclideanDistance.h b/src/shogun/distance/EuclideanDistance.h index 739e7756a74..5aa7cc82e45 100644 --- a/src/shogun/distance/EuclideanDistance.h +++ b/src/shogun/distance/EuclideanDistance.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Saurabh Mahindre, Chiyuan Zhang, Yuyu Zhang, Bjoern Esser, + * Authors: Saurabh Mahindre, Chiyuan Zhang, Yuyu Zhang, Bjoern Esser, * Soeren Sonnenburg, Soumyajit De, Sanuj Sharma */ @@ -14,8 +14,8 @@ namespace shogun { -class CFeatures; -class CDotFeatures; +class Features; +class DotFeatures; template class SGVector; /** @brief class EuclideanDistance @@ -51,21 +51,21 @@ template class SGVector; * @see * Wikipedia: Distance in Euclidean space */ -class CEuclideanDistance: public CDistance +class EuclideanDistance: public Distance { public: /** default constructor */ - CEuclideanDistance(); + EuclideanDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CEuclideanDistance(CDotFeatures* l, CDotFeatures* r); + EuclideanDistance(std::shared_ptr l, std::shared_ptr r); /** destructor */ - virtual ~CEuclideanDistance(); + virtual ~EuclideanDistance(); /** init distance * @@ -73,7 +73,7 @@ class CEuclideanDistance: public CDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); @@ -158,7 +158,7 @@ class CEuclideanDistance: public CDistance * @param rhs features of right-hand side * @return replaced right-hand side features */ - virtual CFeatures* replace_rhs(CFeatures* rhs); + virtual std::shared_ptr replace_rhs(std::shared_ptr rhs); /** replace left-hand side features used in distance matrix * @@ -168,7 +168,7 @@ class CEuclideanDistance: public CDistance * @param lhs features of right-hand side * @return replaced left-hand side features */ - virtual CFeatures* replace_lhs(CFeatures* lhs); + virtual std::shared_ptr replace_lhs(std::shared_ptr lhs); protected: /// compute kernel function for features a and b diff --git a/src/shogun/distance/GeodesicMetric.cpp b/src/shogun/distance/GeodesicMetric.cpp index 38d42eac7dc..e8ed432e092 100644 --- a/src/shogun/distance/GeodesicMetric.cpp +++ b/src/shogun/distance/GeodesicMetric.cpp @@ -12,41 +12,41 @@ using namespace shogun; -CGeodesicMetric::CGeodesicMetric() : CDenseDistance() +GeodesicMetric::GeodesicMetric() : DenseDistance() { } -CGeodesicMetric::CGeodesicMetric(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +GeodesicMetric::GeodesicMetric(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CGeodesicMetric::~CGeodesicMetric() +GeodesicMetric::~GeodesicMetric() { cleanup(); } -bool CGeodesicMetric::init(CFeatures* l, CFeatures* r) +bool GeodesicMetric::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDenseDistance::init(l,r); + bool result=DenseDistance::init(l,r); return result; } -void CGeodesicMetric::cleanup() +void GeodesicMetric::cleanup() { } -float64_t CGeodesicMetric::compute(int32_t idx_a, int32_t idx_b) +float64_t GeodesicMetric::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + lhs->as>()->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + rhs->as>()->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -64,8 +64,8 @@ float64_t CGeodesicMetric::compute(int32_t idx_a, int32_t idx_b) } } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + lhs->as>()->free_feature_vector(avec, idx_a, afree); + rhs->as>()->free_feature_vector(bvec, idx_b, bfree); // trap division by zero @@ -75,8 +75,8 @@ float64_t CGeodesicMetric::compute(int32_t idx_a, int32_t idx_b) d /= std::sqrt(nx * ny); // can only happen due to numerical problems - if (CMath::abs(d)>1.0) - d=CMath::sign(d); + if (Math::abs(d)>1.0) + d=Math::sign(d); return acos(d); } diff --git a/src/shogun/distance/GeodesicMetric.h b/src/shogun/distance/GeodesicMetric.h index 2b899213036..cf311e1fd64 100644 --- a/src/shogun/distance/GeodesicMetric.h +++ b/src/shogun/distance/GeodesicMetric.h @@ -29,19 +29,19 @@ namespace shogun * Geodesic distance * */ -class CGeodesicMetric: public CDenseDistance +class GeodesicMetric: public DenseDistance { public: /** default constructor */ - CGeodesicMetric(); + GeodesicMetric(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CGeodesicMetric(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CGeodesicMetric(); + GeodesicMetric(std::shared_ptr> l, std::shared_ptr> r); + virtual ~GeodesicMetric(); /** init distance * @@ -49,7 +49,7 @@ class CGeodesicMetric: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/HammingWordDistance.cpp b/src/shogun/distance/HammingWordDistance.cpp index 16ba0500d31..cb34cf30b5d 100644 --- a/src/shogun/distance/HammingWordDistance.cpp +++ b/src/shogun/distance/HammingWordDistance.cpp @@ -15,13 +15,13 @@ using namespace shogun; -CHammingWordDistance::CHammingWordDistance() +HammingWordDistance::HammingWordDistance() { init(); } -CHammingWordDistance::CHammingWordDistance(bool sign) -: CStringDistance() +HammingWordDistance::HammingWordDistance(bool sign) +: StringDistance() { init(); use_sign=sign; @@ -29,9 +29,9 @@ CHammingWordDistance::CHammingWordDistance(bool sign) SG_DEBUG("CHammingWordDistance with sign: {} created", (sign) ? 1 : 0) } -CHammingWordDistance::CHammingWordDistance( - CStringFeatures* l, CStringFeatures* r, bool sign) -: CStringDistance() +HammingWordDistance::HammingWordDistance( + std::shared_ptr> l, std::shared_ptr> r, bool sign) +: StringDistance() { init(); use_sign=sign; @@ -41,29 +41,29 @@ CHammingWordDistance::CHammingWordDistance( init(l, r); } -CHammingWordDistance::~CHammingWordDistance() +HammingWordDistance::~HammingWordDistance() { cleanup(); } -bool CHammingWordDistance::init(CFeatures* l, CFeatures* r) +bool HammingWordDistance::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CStringDistance::init(l,r); + bool result=StringDistance::init(l,r); return result; } -void CHammingWordDistance::cleanup() +void HammingWordDistance::cleanup() { } -float64_t CHammingWordDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t HammingWordDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)-> + uint16_t* avec=(std::static_pointer_cast>(lhs))-> get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)-> + uint16_t* bvec=(std::static_pointer_cast>(rhs))-> get_feature_vector(idx_b, blen, free_bvec); int32_t result=0; @@ -158,15 +158,15 @@ float64_t CHammingWordDistance::compute(int32_t idx_a, int32_t idx_b) right_idx++; } - ((CStringFeatures*) lhs)-> + (std::static_pointer_cast>(lhs))-> free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)-> + (std::static_pointer_cast>(rhs))-> free_feature_vector(bvec, idx_b, free_bvec); return result; } -void CHammingWordDistance::init() +void HammingWordDistance::init() { use_sign = false; SG_ADD( diff --git a/src/shogun/distance/HammingWordDistance.h b/src/shogun/distance/HammingWordDistance.h index bc6e30aa19c..69a4c232af7 100644 --- a/src/shogun/distance/HammingWordDistance.h +++ b/src/shogun/distance/HammingWordDistance.h @@ -16,20 +16,20 @@ namespace shogun { - template class CStringFeatures; + template class StringFeatures; /** @brief class HammingWordDistance */ -class CHammingWordDistance: public CStringDistance +class HammingWordDistance: public StringDistance { public: /** default constructor */ - CHammingWordDistance(); + HammingWordDistance(); /** constructor * * @param use_sign if sign shall be used */ - CHammingWordDistance(bool use_sign); + HammingWordDistance(bool use_sign); /** constructor * @@ -37,8 +37,8 @@ class CHammingWordDistance: public CStringDistance * @param r features of right-hand side * @param use_sign if sign shall be used */ - CHammingWordDistance(CStringFeatures* l, CStringFeatures* r, bool use_sign); - virtual ~CHammingWordDistance(); + HammingWordDistance(std::shared_ptr> l, std::shared_ptr> r, bool use_sign); + virtual ~HammingWordDistance(); /** init distance * @@ -46,7 +46,7 @@ class CHammingWordDistance: public CStringDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/JensenMetric.cpp b/src/shogun/distance/JensenMetric.cpp index e264b780ec0..63f4bb8c16c 100644 --- a/src/shogun/distance/JensenMetric.cpp +++ b/src/shogun/distance/JensenMetric.cpp @@ -12,39 +12,39 @@ using namespace shogun; -CJensenMetric::CJensenMetric() : CDenseDistance() +JensenMetric::JensenMetric() : DenseDistance() { } -CJensenMetric::CJensenMetric(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +JensenMetric::JensenMetric(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CJensenMetric::~CJensenMetric() +JensenMetric::~JensenMetric() { cleanup(); } -bool CJensenMetric::init(CFeatures* l, CFeatures* r) +bool JensenMetric::init(std::shared_ptr l, std::shared_ptr r) { - return CDenseDistance::init(l,r); + return DenseDistance::init(l,r); } -void CJensenMetric::cleanup() +void JensenMetric::cleanup() { } -float64_t CJensenMetric::compute(int32_t idx_a, int32_t idx_b) +float64_t JensenMetric::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -65,8 +65,8 @@ float64_t CJensenMetric::compute(int32_t idx_a, int32_t idx_b) } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; diff --git a/src/shogun/distance/JensenMetric.h b/src/shogun/distance/JensenMetric.h index bc1a57e8dea..27a4818cbbf 100644 --- a/src/shogun/distance/JensenMetric.h +++ b/src/shogun/distance/JensenMetric.h @@ -29,19 +29,19 @@ namespace shogun * @see * Wikipedia: Kullback-Leibler divergence */ -class CJensenMetric: public CDenseDistance +class JensenMetric: public DenseDistance { public: /** default constructor */ - CJensenMetric(); + JensenMetric(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CJensenMetric(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CJensenMetric(); + JensenMetric(std::shared_ptr> l, std::shared_ptr> r); + virtual ~JensenMetric(); /** init distance * @@ -49,7 +49,7 @@ class CJensenMetric: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/KernelDistance.cpp b/src/shogun/distance/KernelDistance.cpp index db838c79388..8cf50d8c378 100644 --- a/src/shogun/distance/KernelDistance.cpp +++ b/src/shogun/distance/KernelDistance.cpp @@ -13,58 +13,58 @@ using namespace shogun; -CKernelDistance::CKernelDistance() : CDistance() +KernelDistance::KernelDistance() : Distance() { init(); } -CKernelDistance::CKernelDistance(float64_t w, CKernel* k) -: CDistance() +KernelDistance::KernelDistance(float64_t w, std::shared_ptr k) +: Distance() { init(); kernel=k; width=w; ASSERT(kernel) - SG_REF(kernel); + } -CKernelDistance::CKernelDistance( - CFeatures *l, CFeatures *r, float64_t w , CKernel* k) -: CDistance() +KernelDistance::KernelDistance( + std::shared_ptrl, std::shared_ptrr, float64_t w , std::shared_ptr k) +: Distance() { init(); kernel=k; width=w; ASSERT(kernel) - SG_REF(kernel); + init(l, r); } -CKernelDistance::~CKernelDistance() +KernelDistance::~KernelDistance() { - // important to have the cleanup of CDistance first, it calls get_name which + // important to have the cleanup of Distance first, it calls get_name which // uses the distance cleanup(); - SG_UNREF(kernel); + } -bool CKernelDistance::init(CFeatures* l, CFeatures* r) +bool KernelDistance::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(kernel) kernel->init(l,r); - return CDistance::init(l,r); + return Distance::init(l,r); } -float64_t CKernelDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t KernelDistance::compute(int32_t idx_a, int32_t idx_b) { float64_t result=kernel->kernel(idx_a, idx_b); return exp(-result/width); } -void CKernelDistance::init() +void KernelDistance::init() { kernel = NULL; width = 0.0; diff --git a/src/shogun/distance/KernelDistance.h b/src/shogun/distance/KernelDistance.h index 5f9c677f2ae..403f8fdb663 100644 --- a/src/shogun/distance/KernelDistance.h +++ b/src/shogun/distance/KernelDistance.h @@ -18,7 +18,7 @@ namespace shogun { - class CDistance; + class Distance; /** @brief The Kernel distance takes a distance as input. * @@ -28,18 +28,18 @@ namespace shogun * d({\bf x}, {\bf x'}) = e^{-\frac{k({\bf x}, {\bf x'})}{width}} * \f] */ -class CKernelDistance: public CDistance +class KernelDistance: public Distance { public: /** default constructor */ - CKernelDistance(); + KernelDistance(); /** constructor * * @param width width * @param k kernel */ - CKernelDistance(float64_t width, CKernel* k); + KernelDistance(float64_t width, std::shared_ptr k); /** constructor * @@ -48,11 +48,11 @@ class CKernelDistance: public CDistance * @param width width * @param k kernel */ - CKernelDistance( - CFeatures *l, CFeatures *r, float64_t width, CKernel* k); + KernelDistance( + std::shared_ptrl, std::shared_ptrr, float64_t width, std::shared_ptr k); /** destructor */ - virtual ~CKernelDistance(); + virtual ~KernelDistance(); /** initialize kernel * @@ -60,7 +60,7 @@ class CKernelDistance: public CDistance * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * @@ -106,7 +106,7 @@ class CKernelDistance: public CDistance private: /** kernel */ - CKernel* kernel; + std::shared_ptr kernel; /** width */ float64_t width; }; diff --git a/src/shogun/distance/MahalanobisDistance.cpp b/src/shogun/distance/MahalanobisDistance.cpp index 5ed83add6ef..1e7410edac1 100644 --- a/src/shogun/distance/MahalanobisDistance.cpp +++ b/src/shogun/distance/MahalanobisDistance.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Soeren Sonnenburg, Michele Mazzoni, Viktor Gal, + * Authors: Fernando Iglesias, Soeren Sonnenburg, Michele Mazzoni, Viktor Gal, * Evan Shelhamer, Sergey Lisitsyn */ @@ -16,33 +16,33 @@ using namespace shogun; -CMahalanobisDistance::CMahalanobisDistance() : CRealDistance() +MahalanobisDistance::MahalanobisDistance() : RealDistance() { init(); } -CMahalanobisDistance::CMahalanobisDistance(CDenseFeatures* l, CDenseFeatures* r) -: CRealDistance() +MahalanobisDistance::MahalanobisDistance(std::shared_ptr> l, std::shared_ptr> r) +: RealDistance() { init(); init(l, r); } -CMahalanobisDistance::~CMahalanobisDistance() +MahalanobisDistance::~MahalanobisDistance() { cleanup(); } -bool CMahalanobisDistance::init(CFeatures* l, CFeatures* r) +bool MahalanobisDistance::init(std::shared_ptr l, std::shared_ptr r) { // FIXME: See comments in // https://github.com/shogun-toolbox/shogun/pull/4085#discussion_r166254024 - require(CRealDistance::init(l, r), "MahalanobisDistance initialization failed."); + require(RealDistance::init(l, r), "MahalanobisDistance initialization failed."); SGMatrix cov; - auto feat_l = static_cast*>(l); - auto feat_r = static_cast*>(r); + auto feat_l = std::dynamic_pointer_cast>(l); + auto feat_r = std::dynamic_pointer_cast>(r); if ( l == r) { @@ -52,7 +52,7 @@ bool CMahalanobisDistance::init(CFeatures* l, CFeatures* r) else { mean = feat_l->compute_mean(feat_l, feat_r); - cov = CDotFeatures::compute_cov(feat_l, feat_r); + cov = DotFeatures::compute_cov(feat_l, feat_r); } auto num_features = cov.num_rows; @@ -64,14 +64,14 @@ bool CMahalanobisDistance::init(CFeatures* l, CFeatures* r) return true; } -void CMahalanobisDistance::cleanup() +void MahalanobisDistance::cleanup() { } -float64_t CMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t MahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) { - auto feat_l = static_cast*>(lhs); - auto feat_r = static_cast*>(rhs); + auto feat_l = std::dynamic_pointer_cast>(lhs); + auto feat_r = std::dynamic_pointer_cast>(rhs); SGVector bvec = feat_r->get_feature_vector(idx_b); @@ -105,7 +105,7 @@ float64_t CMahalanobisDistance::compute(int32_t idx_a, int32_t idx_b) return std::sqrt(result); } -void CMahalanobisDistance::init() +void MahalanobisDistance::init() { disable_sqrt=false; use_mean=false; diff --git a/src/shogun/distance/MahalanobisDistance.h b/src/shogun/distance/MahalanobisDistance.h index 1344f0f30df..a5cc5c4e3fe 100644 --- a/src/shogun/distance/MahalanobisDistance.h +++ b/src/shogun/distance/MahalanobisDistance.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Soeren Sonnenburg, Yuyu Zhang, Viktor Gal, + * Authors: Fernando Iglesias, Soeren Sonnenburg, Yuyu Zhang, Viktor Gal, * Evan Shelhamer, Sergey Lisitsyn */ @@ -47,19 +47,19 @@ namespace shogun * @see * Wikipedia: Mahalanobis Distance */ - class CMahalanobisDistance : public CRealDistance + class MahalanobisDistance : public RealDistance { public: /** default constructor */ - CMahalanobisDistance(); + MahalanobisDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CMahalanobisDistance(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CMahalanobisDistance(); + MahalanobisDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~MahalanobisDistance(); /** init distance * @@ -67,7 +67,7 @@ namespace shogun * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/ManhattanMetric.cpp b/src/shogun/distance/ManhattanMetric.cpp index 8672323d73a..856bd433f39 100644 --- a/src/shogun/distance/ManhattanMetric.cpp +++ b/src/shogun/distance/ManhattanMetric.cpp @@ -12,42 +12,42 @@ using namespace shogun; -CManhattanMetric::CManhattanMetric() -: CDenseDistance() +ManhattanMetric::ManhattanMetric() +: DenseDistance() { } -CManhattanMetric::CManhattanMetric(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +ManhattanMetric::ManhattanMetric(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CManhattanMetric::~CManhattanMetric() +ManhattanMetric::~ManhattanMetric() { cleanup(); } -bool CManhattanMetric::init(CFeatures* l, CFeatures* r) +bool ManhattanMetric::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDenseDistance::init(l,r); + bool result=DenseDistance::init(l,r); return result; } -void CManhattanMetric::cleanup() +void ManhattanMetric::cleanup() { } -float64_t CManhattanMetric::compute(int32_t idx_a, int32_t idx_b) +float64_t ManhattanMetric::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -61,8 +61,8 @@ float64_t CManhattanMetric::compute(int32_t idx_a, int32_t idx_b) } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/distance/ManhattanMetric.h b/src/shogun/distance/ManhattanMetric.h index 0be30382f27..88792658e7e 100644 --- a/src/shogun/distance/ManhattanMetric.h +++ b/src/shogun/distance/ManhattanMetric.h @@ -30,19 +30,19 @@ namespace shogun * @see * Wikipedia: Manhattan distance */ -class CManhattanMetric: public CDenseDistance +class ManhattanMetric: public DenseDistance { public: /** default constructor */ - CManhattanMetric(); + ManhattanMetric(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CManhattanMetric(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CManhattanMetric(); + ManhattanMetric(std::shared_ptr> l, std::shared_ptr> r); + virtual ~ManhattanMetric(); /** init distance * @@ -50,7 +50,7 @@ class CManhattanMetric: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/ManhattanWordDistance.cpp b/src/shogun/distance/ManhattanWordDistance.cpp index 33da1713fee..00ac99ad121 100644 --- a/src/shogun/distance/ManhattanWordDistance.cpp +++ b/src/shogun/distance/ManhattanWordDistance.cpp @@ -12,44 +12,44 @@ using namespace shogun; -CManhattanWordDistance::CManhattanWordDistance() -: CStringDistance() +ManhattanWordDistance::ManhattanWordDistance() +: StringDistance() { SG_DEBUG("CManhattanWordDistance created") } -CManhattanWordDistance::CManhattanWordDistance( - CStringFeatures* l, CStringFeatures* r) -: CStringDistance() +ManhattanWordDistance::ManhattanWordDistance( + std::shared_ptr> l, std::shared_ptr> r) +: StringDistance() { SG_DEBUG("CManhattanWordDistance created") init(l, r); } -CManhattanWordDistance::~CManhattanWordDistance() +ManhattanWordDistance::~ManhattanWordDistance() { cleanup(); } -bool CManhattanWordDistance::init(CFeatures* l, CFeatures* r) +bool ManhattanWordDistance::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CStringDistance::init(l,r); + bool result=StringDistance::init(l,r); return result; } -void CManhattanWordDistance::cleanup() +void ManhattanWordDistance::cleanup() { } -float64_t CManhattanWordDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t ManhattanWordDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)-> + uint16_t* avec=(std::static_pointer_cast>(lhs))-> get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)-> + uint16_t* bvec=(std::static_pointer_cast>(rhs))-> get_feature_vector(idx_b, blen, free_bvec); int32_t result=0; @@ -71,7 +71,7 @@ float64_t CManhattanWordDistance::compute(int32_t idx_a, int32_t idx_b) while (right_idx< blen && bvec[right_idx]==sym) right_idx++; - result += CMath::abs( (left_idx-old_left_idx) - (right_idx-old_right_idx) ); + result += Math::abs( (left_idx-old_left_idx) - (right_idx-old_right_idx) ); } else if (avec[left_idx]*) lhs)-> + (std::static_pointer_cast>(lhs))-> free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)-> + (std::static_pointer_cast>(rhs))-> free_feature_vector(bvec, idx_b, free_bvec); return result; diff --git a/src/shogun/distance/ManhattanWordDistance.h b/src/shogun/distance/ManhattanWordDistance.h index 02cf814a07c..6525856337d 100644 --- a/src/shogun/distance/ManhattanWordDistance.h +++ b/src/shogun/distance/ManhattanWordDistance.h @@ -17,19 +17,19 @@ namespace shogun { /** @brief class ManhattanWordDistance */ -class CManhattanWordDistance: public CStringDistance +class ManhattanWordDistance: public StringDistance { public: /** default constructor */ - CManhattanWordDistance(); + ManhattanWordDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CManhattanWordDistance(CStringFeatures* l, CStringFeatures* r); - virtual ~CManhattanWordDistance(); + ManhattanWordDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~ManhattanWordDistance(); /** init distance * @@ -37,7 +37,7 @@ class CManhattanWordDistance: public CStringDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/MinkowskiMetric.cpp b/src/shogun/distance/MinkowskiMetric.cpp index 3c436101ddb..a8696a718c5 100644 --- a/src/shogun/distance/MinkowskiMetric.cpp +++ b/src/shogun/distance/MinkowskiMetric.cpp @@ -15,50 +15,50 @@ using namespace shogun; -CMinkowskiMetric::CMinkowskiMetric() : CDenseDistance() +MinkowskiMetric::MinkowskiMetric() : DenseDistance() { init(); } -CMinkowskiMetric::CMinkowskiMetric(float64_t k_) -: CDenseDistance() +MinkowskiMetric::MinkowskiMetric(float64_t k_) +: DenseDistance() { init(); k=k_; } -CMinkowskiMetric::CMinkowskiMetric( - CDenseFeatures* l, CDenseFeatures* r, float64_t k_) -: CDenseDistance() +MinkowskiMetric::MinkowskiMetric( + std::shared_ptr> l, std::shared_ptr> r, float64_t k_) +: DenseDistance() { init(); k=k_; init(l, r); } -CMinkowskiMetric::~CMinkowskiMetric() +MinkowskiMetric::~MinkowskiMetric() { cleanup(); } -bool CMinkowskiMetric::init(CFeatures* l, CFeatures* r) +bool MinkowskiMetric::init(std::shared_ptr l, std::shared_ptr r) { - return CDenseDistance::init(l,r); + return DenseDistance::init(l,r); } -void CMinkowskiMetric::cleanup() +void MinkowskiMetric::cleanup() { } -float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b) +float64_t MinkowskiMetric::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(avec) ASSERT(bvec) @@ -75,13 +75,13 @@ float64_t CMinkowskiMetric::compute(int32_t idx_a, int32_t idx_b) } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return pow(result,1/k); } -void CMinkowskiMetric::init() +void MinkowskiMetric::init() { k = 2.0; SG_ADD(&k, "k", "L_k norm.", ParameterProperties::HYPER); diff --git a/src/shogun/distance/MinkowskiMetric.h b/src/shogun/distance/MinkowskiMetric.h index 24c7fa85b59..4b6f2d18b88 100644 --- a/src/shogun/distance/MinkowskiMetric.h +++ b/src/shogun/distance/MinkowskiMetric.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evan Shelhamer, Yuyu Zhang, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Evan Shelhamer, Yuyu Zhang, Sergey Lisitsyn, * Chiyuan Zhang */ @@ -28,24 +28,24 @@ namespace shogun * * special cases: * -# \f$\displaystyle L_{1} \f$ norm: Manhattan distance @see CManhattanMetric - * -# \f$\displaystyle L_{2} \f$ norm: Euclidean distance @see CEuclideanDistance + * -# \f$\displaystyle L_{2} \f$ norm: Euclidean distance @see EuclideanDistance * * Note that the Minkowski distance tends to the Chebyshew distance for * increasing \f$k\f$. * * @see Wikipedia: Distance */ -class CMinkowskiMetric: public CDenseDistance +class MinkowskiMetric: public DenseDistance { public: /** default constructor */ - CMinkowskiMetric(); + MinkowskiMetric(); /** constructor * * @param k parameter k */ - CMinkowskiMetric(float64_t k); + MinkowskiMetric(float64_t k); /** constructor * @@ -53,15 +53,15 @@ class CMinkowskiMetric: public CDenseDistance * @param r features of right-hand side * @param k parameter k */ - CMinkowskiMetric(CDenseFeatures* l, CDenseFeatures* r, float64_t k); - virtual ~CMinkowskiMetric(); + MinkowskiMetric(std::shared_ptr> l, std::shared_ptr> r, float64_t k); + virtual ~MinkowskiMetric(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/RealDistance.h b/src/shogun/distance/RealDistance.h index 47afd1e5588..7c770d90100 100644 --- a/src/shogun/distance/RealDistance.h +++ b/src/shogun/distance/RealDistance.h @@ -15,11 +15,11 @@ namespace shogun { /** @brief class RealDistance */ -class CRealDistance : public CDenseDistance +class RealDistance : public DenseDistance { public: /** default constructor */ - CRealDistance() : CDenseDistance() {} + RealDistance() : DenseDistance() {} /** init distance * @@ -27,9 +27,9 @@ class CRealDistance : public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - CDenseDistance::init(l,r); + DenseDistance::init(l,r); ASSERT(l->get_feature_type()==F_DREAL) ASSERT(r->get_feature_type()==F_DREAL) diff --git a/src/shogun/distance/SparseDistance.h b/src/shogun/distance/SparseDistance.h index b03060feba0..92c5e8dd2b3 100644 --- a/src/shogun/distance/SparseDistance.h +++ b/src/shogun/distance/SparseDistance.h @@ -15,11 +15,11 @@ namespace shogun { /** @brief template class SparseDistance */ -template class CSparseDistance : public CDistance +template class SparseDistance : public Distance { public: /** default constructor */ - CSparseDistance() : CDistance() {} + SparseDistance() : Distance() {} /** init distance * @@ -27,19 +27,20 @@ template class CSparseDistance : public CDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - CDistance::init(l,r); + Distance::init(l,r); ASSERT(l->get_feature_class()==C_SPARSE) ASSERT(r->get_feature_class()==C_SPARSE) ASSERT(l->get_feature_type()==this->get_feature_type()) ASSERT(r->get_feature_type()==this->get_feature_type()) - if (((CSparseFeatures*) lhs)->get_num_features() != ((CSparseFeatures*) rhs)->get_num_features() ) + if ((std::static_pointer_cast>(lhs))->get_num_features() != (std::static_pointer_cast>(rhs))->get_num_features() ) { error("train or test features #dimension mismatch (l:{} vs. r:{})", - ((CSparseFeatures*) lhs)->get_num_features(),((CSparseFeatures*)rhs)->get_num_features()); + (std::static_pointer_cast>(lhs))->get_num_features(), + (std::static_pointer_cast>(rhs))->get_num_features()); } return true; } @@ -83,42 +84,42 @@ template class CSparseDistance : public CDistance * * @return feature type DREAL */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_DREAL; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_DREAL; } /** get feature type the ULONG distance can deal with * * @return feature type ULONG */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_ULONG; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_ULONG; } /** get feature type the INT distance can deal with * * @return feature type INT */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_INT; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_INT; } /** get feature type the WORD distance can deal with * * @return feature type WORD */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_WORD; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_WORD; } /** get feature type the SHORT distance can deal with * * @return feature type SHORT */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_SHORT; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_SHORT; } /** get feature type the BYTE distance can deal with * * @return feature type BYTE */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_BYTE; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_BYTE; } /** get feature type the CHAR distance can deal with * * @return feature type CHAR */ -template<> inline EFeatureType CSparseDistance::get_feature_type() { return F_CHAR; } +template<> inline EFeatureType SparseDistance::get_feature_type() { return F_CHAR; } } // namespace shogun #endif diff --git a/src/shogun/distance/SparseEuclideanDistance.cpp b/src/shogun/distance/SparseEuclideanDistance.cpp index 1070aa3e6e5..0557e79b6f5 100644 --- a/src/shogun/distance/SparseEuclideanDistance.cpp +++ b/src/shogun/distance/SparseEuclideanDistance.cpp @@ -12,46 +12,46 @@ using namespace shogun; -CSparseEuclideanDistance::CSparseEuclideanDistance() -: CSparseDistance() +SparseEuclideanDistance::SparseEuclideanDistance() +: SparseDistance() { init(); } -CSparseEuclideanDistance::CSparseEuclideanDistance( - CSparseFeatures* l, CSparseFeatures* r) -: CSparseDistance() +SparseEuclideanDistance::SparseEuclideanDistance( + std::shared_ptr> l, std::shared_ptr> r) +: SparseDistance() { init(); init(l, r); } -CSparseEuclideanDistance::~CSparseEuclideanDistance() +SparseEuclideanDistance::~SparseEuclideanDistance() { cleanup(); } -bool CSparseEuclideanDistance::init(CFeatures* l, CFeatures* r) +bool SparseEuclideanDistance::init(std::shared_ptr l, std::shared_ptr r) { - CSparseDistance::init(l, r); + SparseDistance::init(l, r); cleanup(); sq_lhs=SG_MALLOC(float64_t, lhs->get_num_vectors()); - sq_lhs=((CSparseFeatures*) lhs)->compute_squared(sq_lhs); + sq_lhs=(std::static_pointer_cast>(lhs))->compute_squared(sq_lhs); if (lhs==rhs) sq_rhs=sq_lhs; else { sq_rhs=SG_MALLOC(float64_t, rhs->get_num_vectors()); - sq_rhs=((CSparseFeatures*) rhs)->compute_squared(sq_rhs); + sq_rhs=(std::static_pointer_cast>(rhs))->compute_squared(sq_rhs); } return true; } -void CSparseEuclideanDistance::cleanup() +void SparseEuclideanDistance::cleanup() { if (sq_lhs != sq_rhs) SG_FREE(sq_rhs); @@ -61,16 +61,16 @@ void CSparseEuclideanDistance::cleanup() sq_lhs = NULL; } -float64_t CSparseEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t SparseEuclideanDistance::compute(int32_t idx_a, int32_t idx_b) { - float64_t result=((CSparseFeatures*) lhs)->compute_squared_norm( - (CSparseFeatures*) lhs, sq_lhs, idx_a, - (CSparseFeatures*) rhs, sq_rhs, idx_b); + float64_t result=(std::static_pointer_cast>(lhs))->compute_squared_norm( + std::static_pointer_cast>(lhs), sq_lhs, idx_a, + std::static_pointer_cast>(rhs), sq_rhs, idx_b); return std::sqrt(result); } -void CSparseEuclideanDistance::init() +void SparseEuclideanDistance::init() { sq_lhs=NULL; sq_rhs=NULL; diff --git a/src/shogun/distance/SparseEuclideanDistance.h b/src/shogun/distance/SparseEuclideanDistance.h index 34bd227a315..cf47592f70a 100644 --- a/src/shogun/distance/SparseEuclideanDistance.h +++ b/src/shogun/distance/SparseEuclideanDistance.h @@ -15,22 +15,22 @@ namespace shogun { - template class CSparseFeatures; + template class SparseFeatures; /** @brief class SparseEucldeanDistance */ -class CSparseEuclideanDistance: public CSparseDistance +class SparseEuclideanDistance: public SparseDistance { public: /** default constructor */ - CSparseEuclideanDistance(); + SparseEuclideanDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CSparseEuclideanDistance( - CSparseFeatures* l, CSparseFeatures* r); - virtual ~CSparseEuclideanDistance(); + SparseEuclideanDistance( + std::shared_ptr> l, std::shared_ptr> r); + virtual ~SparseEuclideanDistance(); /** init distance * @@ -38,7 +38,7 @@ class CSparseEuclideanDistance: public CSparseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distance/StringDistance.h b/src/shogun/distance/StringDistance.h index 2717b9b5077..c53a203f7c9 100644 --- a/src/shogun/distance/StringDistance.h +++ b/src/shogun/distance/StringDistance.h @@ -15,11 +15,11 @@ namespace shogun { /** @brief template class StringDistance */ -template class CStringDistance : public CDistance +template class StringDistance : public Distance { public: /** default constructor */ - CStringDistance() : CDistance() {} + StringDistance() : Distance() {} /** init distance * @@ -30,9 +30,9 @@ template class CStringDistance : public CDistance /* when training data is supplied as both l and r do_init * should be true */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - CDistance::init(l,r); + Distance::init(l,r); ASSERT(l->get_feature_class()==C_STRING) ASSERT(r->get_feature_class()==C_STRING) @@ -80,43 +80,43 @@ template class CStringDistance : public CDistance * * @return feature type DREAL */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_DREAL; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_DREAL; } /** get feature type the ULONG distance can deal with * * @return feature type ULONG */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_ULONG; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_ULONG; } /** get feature type the INT distance can deal with * * @return feature type INT */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_INT; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_INT; } /** get feature type the WORD distance can deal with * * @return feature type WORD */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_WORD; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_WORD; } /** get feature type the SHORT distance can deal with * * @return feature type SHORT */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_SHORT; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_SHORT; } /** get feature type the BYTE distance can deal with * * @return feature type BYTE */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_BYTE; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_BYTE; } /** get feature type the CHAR distance can deal with * * @return feature type CHAR */ -template<> inline EFeatureType CStringDistance::get_feature_type() { return F_CHAR; } +template<> inline EFeatureType StringDistance::get_feature_type() { return F_CHAR; } } // namespace shogun #endif diff --git a/src/shogun/distance/TanimotoDistance.cpp b/src/shogun/distance/TanimotoDistance.cpp index 23e2abee9e0..a04c727f07a 100644 --- a/src/shogun/distance/TanimotoDistance.cpp +++ b/src/shogun/distance/TanimotoDistance.cpp @@ -12,42 +12,42 @@ using namespace shogun; -CTanimotoDistance::CTanimotoDistance() -: CDenseDistance() +TanimotoDistance::TanimotoDistance() +: DenseDistance() { } -CTanimotoDistance::CTanimotoDistance(CDenseFeatures* l, CDenseFeatures* r) -: CDenseDistance() +TanimotoDistance::TanimotoDistance(std::shared_ptr> l, std::shared_ptr> r) +: DenseDistance() { init(l, r); } -CTanimotoDistance::~CTanimotoDistance() +TanimotoDistance::~TanimotoDistance() { cleanup(); } -bool CTanimotoDistance::init(CFeatures* l, CFeatures* r) +bool TanimotoDistance::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDenseDistance::init(l,r); + bool result=DenseDistance::init(l,r); return result; } -void CTanimotoDistance::cleanup() +void TanimotoDistance::cleanup() { } -float64_t CTanimotoDistance::compute(int32_t idx_a, int32_t idx_b) +float64_t TanimotoDistance::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) @@ -64,8 +64,8 @@ float64_t CTanimotoDistance::compute(int32_t idx_a, int32_t idx_b) } } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); s=nx+ny-d; diff --git a/src/shogun/distance/TanimotoDistance.h b/src/shogun/distance/TanimotoDistance.h index 41703907046..6bd2e99198e 100644 --- a/src/shogun/distance/TanimotoDistance.h +++ b/src/shogun/distance/TanimotoDistance.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Yuyu Zhang, Evan Shelhamer, Ariane Paola Gomes, + * Authors: Soeren Sonnenburg, Yuyu Zhang, Evan Shelhamer, Ariane Paola Gomes, * Sergey Lisitsyn */ @@ -30,19 +30,19 @@ namespace shogun * Tanimoto coefficient * @see CCosineDistance */ -class CTanimotoDistance: public CDenseDistance +class TanimotoDistance: public DenseDistance { public: /** default constructor */ - CTanimotoDistance(); + TanimotoDistance(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CTanimotoDistance(CDenseFeatures* l, CDenseFeatures* r); - virtual ~CTanimotoDistance(); + TanimotoDistance(std::shared_ptr> l, std::shared_ptr> r); + virtual ~TanimotoDistance(); /** init distance * @@ -50,7 +50,7 @@ class CTanimotoDistance: public CDenseDistance * @param r features of right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup distance */ virtual void cleanup(); diff --git a/src/shogun/distributions/DiscreteDistribution.h b/src/shogun/distributions/DiscreteDistribution.h index 7fc08fcd9a2..daf7c0943b5 100644 --- a/src/shogun/distributions/DiscreteDistribution.h +++ b/src/shogun/distributions/DiscreteDistribution.h @@ -38,14 +38,14 @@ namespace shogun /** @brief This is the base interface class for all discrete distributions. */ -class CDiscreteDistribution : public CDistribution +class DiscreteDistribution : public Distribution { public: /* constructor */ - CDiscreteDistribution() : CDistribution() { }; + DiscreteDistribution() : Distribution() { }; /* destructor */ - ~CDiscreteDistribution() { }; + ~DiscreteDistribution() { }; /** learn distribution * @@ -53,7 +53,7 @@ class CDiscreteDistribution : public CDistribution * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL)=0; + virtual bool train(Features* data=NULL)=0; /** get number of parameters in model * diff --git a/src/shogun/distributions/Distribution.cpp b/src/shogun/distributions/Distribution.cpp index bf06a4a3f4d..31fe1126d43 100644 --- a/src/shogun/distributions/Distribution.cpp +++ b/src/shogun/distributions/Distribution.cpp @@ -9,18 +9,18 @@ using namespace shogun; -CDistribution::CDistribution() -: CSGObject(), features(NULL), pseudo_count(1e-10) +Distribution::Distribution() +: SGObject(), features(NULL), pseudo_count(1e-10) { SG_ADD(&features, "features", "features to be used"); } -CDistribution::~CDistribution() +Distribution::~Distribution() { - SG_UNREF(features); + } -float64_t CDistribution::get_log_likelihood_sample() +float64_t Distribution::get_log_likelihood_sample() { ASSERT(features) @@ -31,7 +31,7 @@ float64_t CDistribution::get_log_likelihood_sample() return sum/features->get_num_vectors(); } -SGVector CDistribution::get_log_likelihood() +SGVector Distribution::get_log_likelihood() { ASSERT(features) @@ -44,20 +44,20 @@ SGVector CDistribution::get_log_likelihood() return SGVector(vec,num); } -int32_t CDistribution::get_num_relevant_model_parameters() +int32_t Distribution::get_num_relevant_model_parameters() { int32_t total_num=get_num_model_parameters(); int32_t num=0; for (int32_t i=0; iCMath::ALMOST_NEG_INFTY) + if (get_log_model_parameter(i)>Math::ALMOST_NEG_INFTY) num++; } return num; } -SGVector CDistribution::get_likelihood_for_all_examples() +SGVector Distribution::get_likelihood_for_all_examples() { ASSERT(features); int32_t num=features->get_num_vectors(); @@ -70,19 +70,19 @@ SGVector CDistribution::get_likelihood_for_all_examples() return result; } -float64_t CDistribution::update_params_em(const SGVector alpha_k) +float64_t Distribution::update_params_em(const SGVector alpha_k) { io::warn("Not implemented in this class. This class cannot be used for Mixture models."); not_implemented(SOURCE_LOCATION); return -1; } -CDistribution* CDistribution::obtain_from_generic(CSGObject* object) +std::shared_ptr Distribution::obtain_from_generic(std::shared_ptr object) { if (!object) return NULL; - CDistribution* casted=dynamic_cast(object); + auto casted=std::dynamic_pointer_cast(object); if (!casted) return NULL; diff --git a/src/shogun/distributions/Distribution.h b/src/shogun/distributions/Distribution.h index 50518e61273..1ec2ba19161 100644 --- a/src/shogun/distributions/Distribution.h +++ b/src/shogun/distributions/Distribution.h @@ -16,8 +16,8 @@ namespace shogun { -class CFeatures; -class CMath; +class Features; +class Math; /** @brief Base class Distribution from which all methods implementing a * distribution are derived. * @@ -32,19 +32,19 @@ class CMath; * get_log_likelihood_example() - for the likelihood for the * n-th example * - * This way methods building on CDistribution, might enumerate over all possible + * This way methods building on Distribution, might enumerate over all possible * model parameters and obtain the parameter vector and the gradient. This is - * used to compute e.g. the TOP and Fisher Kernel (cf. CPluginEstimate, CHistogramKernel, + * used to compute e.g. the TOP and Fisher Kernel (cf. PluginEstimate, CHistogramKernel, * CTOPFeatures and CFKFeatures ). */ -class CDistribution : public CSGObject +class Distribution : public SGObject { public: /** default constructor */ - CDistribution(); + Distribution(); /** destructor */ - virtual ~CDistribution(); + virtual ~Distribution(); /** learn distribution * @@ -54,7 +54,7 @@ class CDistribution : public CSGObject * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL)=0; + virtual bool train(std::shared_ptr data=NULL)=0; /** get number of parameters in model * @@ -153,20 +153,17 @@ class CDistribution : public CSGObject * * @param f new feature vectors */ - virtual void set_features(CFeatures* f) + virtual void set_features(std::shared_ptr f) { - SG_UNREF(features); features=f; - SG_REF(features); } /** get feature vectors * * @return feature vectors */ - virtual CFeatures* get_features() + virtual std::shared_ptr get_features() { - SG_REF(features); return features; } @@ -200,11 +197,11 @@ class CDistribution : public CSGObject #ifndef SWIG [[deprecated("use .as template function")]] #endif - static CDistribution* obtain_from_generic(CSGObject* object); + static std::shared_ptr obtain_from_generic(std::shared_ptr object); protected: /** feature vectors */ - CFeatures* features; + std::shared_ptr features; /** pseudo count */ float64_t pseudo_count; }; diff --git a/src/shogun/distributions/EMBase.h b/src/shogun/distributions/EMBase.h index ef9707bdf61..af955bb1b93 100644 --- a/src/shogun/distributions/EMBase.h +++ b/src/shogun/distributions/EMBase.h @@ -42,14 +42,14 @@ namespace shogun * this base class. This is a template class having a template member called data which can be used to store all * parameters used and results calculated by the expectation and maximization steps of EM. */ -template class CEMBase : public CSGObject +template class EMBase : public SGObject { public: /** constructor */ - CEMBase() : CSGObject() { }; + EMBase() : SGObject() { }; /** destructor */ - virtual ~CEMBase() { }; + virtual ~EMBase() { }; /** returns the name of the class */ virtual const char* get_name() const { return "EMBase"; } diff --git a/src/shogun/distributions/EMMixtureModel.cpp b/src/shogun/distributions/EMMixtureModel.cpp index 822bb61a4aa..faf5586341b 100644 --- a/src/shogun/distributions/EMMixtureModel.cpp +++ b/src/shogun/distributions/EMMixtureModel.cpp @@ -34,14 +34,14 @@ using namespace shogun; -CEMMixtureModel::CEMMixtureModel() -: CEMBase () +EMMixtureModel::EMMixtureModel() +: EMBase () { } -CEMMixtureModel::~CEMMixtureModel() +EMMixtureModel::~EMMixtureModel() { } -float64_t CEMMixtureModel::expectation_step() +float64_t EMMixtureModel::expectation_step() { float64_t log_likelihood=0; // for each data point @@ -51,13 +51,13 @@ float64_t CEMMixtureModel::expectation_step() // for each component for (int32_t j=0;jget_element(j)->as(); + auto jth_component=data.components->get_element(j); alpha_ij[j] = std::log(data.weights[j]) + jth_component->get_log_likelihood_example(i); - SG_UNREF(jth_component); + }; - float64_t normalize=CMath::log_sum_exp(alpha_ij); + float64_t normalize=Math::log_sum_exp(alpha_ij); log_likelihood+=normalize; // fill row of alpha @@ -68,13 +68,13 @@ float64_t CEMMixtureModel::expectation_step() return log_likelihood; } -void CEMMixtureModel::maximization_step() +void EMMixtureModel::maximization_step() { // for each component float64_t sum_weights=0; for (int32_t j=0;jget_element(j)->as(); + auto jth_component=data.components->get_element(j); // update mean covariance of components SGVector alpha_j(data.alpha.matrix+j*data.alpha.num_rows, data.alpha.num_rows, false); @@ -84,7 +84,7 @@ void CEMMixtureModel::maximization_step() sum_weights+=weight_j; data.weights[j]=weight_j; - SG_UNREF(jth_component); + } // update weights - normalization diff --git a/src/shogun/distributions/EMMixtureModel.h b/src/shogun/distributions/EMMixtureModel.h index e84d0ac96c1..f0a09c4bcba 100644 --- a/src/shogun/distributions/EMMixtureModel.h +++ b/src/shogun/distributions/EMMixtureModel.h @@ -41,14 +41,14 @@ namespace shogun /** @brief This is the implementation of EM specialized for Mixture models. */ -class CEMMixtureModel : public CEMBase +class EMMixtureModel : public EMBase { public: /** constructor */ - CEMMixtureModel(); + EMMixtureModel(); /** destructor */ - virtual ~CEMMixtureModel(); + virtual ~EMMixtureModel(); /** returns name of class */ virtual const char* get_name() const { return "EMMixtureModel"; } diff --git a/src/shogun/distributions/Gaussian.cpp b/src/shogun/distributions/Gaussian.cpp index 800fa69b616..7b0ab593cd2 100644 --- a/src/shogun/distributions/Gaussian.cpp +++ b/src/shogun/distributions/Gaussian.cpp @@ -17,14 +17,14 @@ using namespace shogun; using namespace linalg; -CGaussian::CGaussian() : RandomMixin(), m_constant(0), m_d(), m_u(), m_mean(), m_cov_type(FULL) +Gaussian::Gaussian() : RandomMixin(), m_constant(0), m_d(), m_u(), m_mean(), m_cov_type(FULL) { register_params(); } -CGaussian::CGaussian( +Gaussian::Gaussian( const SGVector mean, SGMatrix cov, ECovType cov_type) - : RandomMixin() + : RandomMixin() { ASSERT(mean.vlen==cov.num_rows) ASSERT(cov.num_rows==cov.num_cols) @@ -42,7 +42,7 @@ CGaussian::CGaussian( register_params(); } -void CGaussian::init() +void Gaussian::init() { m_constant = std::log(2 * M_PI) * m_mean.vlen; switch (m_cov_type) @@ -58,11 +58,11 @@ void CGaussian::init() } } -CGaussian::~CGaussian() +Gaussian::~Gaussian() { } -bool CGaussian::train(CFeatures* data) +bool Gaussian::train(std::shared_ptr data) { // init features with data if necessary and assure type is correct if (data) @@ -72,7 +72,7 @@ bool CGaussian::train(CFeatures* data) set_features(data); } - CDotFeatures* dotdata=(CDotFeatures *) data; + auto dotdata=data->as(); m_mean=dotdata->get_mean(); SGMatrix cov=dotdata->get_cov(); decompose_cov(cov); @@ -80,7 +80,7 @@ bool CGaussian::train(CFeatures* data) return true; } -int32_t CGaussian::get_num_model_parameters() +int32_t Gaussian::get_num_model_parameters() { switch (m_cov_type) { @@ -94,29 +94,29 @@ int32_t CGaussian::get_num_model_parameters() return 0; } -float64_t CGaussian::get_log_model_parameter(int32_t num_param) +float64_t Gaussian::get_log_model_parameter(int32_t num_param) { not_implemented(SOURCE_LOCATION); return 0; } -float64_t CGaussian::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t Gaussian::get_log_derivative(int32_t num_param, int32_t num_example) { not_implemented(SOURCE_LOCATION); return 0; } -float64_t CGaussian::get_log_likelihood_example(int32_t num_example) +float64_t Gaussian::get_log_likelihood_example(int32_t num_example) { ASSERT(features->has_property(FP_DOT)) - SGVector v=((CDotFeatures *)features)->get_computed_dot_feature_vector(num_example); + SGVector v=features->as()->get_computed_dot_feature_vector(num_example); float64_t answer=compute_log_PDF(v); return answer; } -float64_t CGaussian::update_params_em(const SGVector alpha_k) +float64_t Gaussian::update_params_em(const SGVector alpha_k) { - CDotFeatures* dotdata=features->as(); + auto dotdata=features->as(); int32_t num_dim=dotdata->get_dim_feature_space(); // compute mean @@ -229,7 +229,7 @@ float64_t CGaussian::update_params_em(const SGVector alpha_k) return alpha_k_sum; } -float64_t CGaussian::compute_log_PDF(SGVector point) +float64_t Gaussian::compute_log_PDF(SGVector point) { ASSERT(m_mean.vector && m_d.vector) ASSERT(point.vlen == m_mean.vlen) @@ -268,12 +268,12 @@ float64_t CGaussian::compute_log_PDF(SGVector point) return -0.5 * answer; } -SGVector CGaussian::get_mean() +SGVector Gaussian::get_mean() { return m_mean; } -void CGaussian::set_mean(SGVector mean) +void Gaussian::set_mean(SGVector mean) { if (mean.vlen==1) m_cov_type=SPHERICAL; @@ -281,7 +281,7 @@ void CGaussian::set_mean(SGVector mean) m_mean=mean; } -void CGaussian::set_cov(SGMatrix cov) +void Gaussian::set_cov(SGMatrix cov) { ASSERT(cov.num_rows==cov.num_cols) ASSERT(cov.num_rows==m_mean.vlen) @@ -289,13 +289,13 @@ void CGaussian::set_cov(SGMatrix cov) init(); } -void CGaussian::set_d(const SGVector d) +void Gaussian::set_d(const SGVector d) { m_d = d; init(); } -SGMatrix CGaussian::get_cov() +SGMatrix Gaussian::get_cov() { SGMatrix cov(m_mean.vlen, m_mean.vlen); @@ -336,7 +336,7 @@ SGMatrix CGaussian::get_cov() return cov; } -void CGaussian::register_params() +void Gaussian::register_params() { SG_ADD(&m_u, "m_u", "Unitary matrix."); SG_ADD(&m_d, "m_d", "Diagonal."); @@ -347,7 +347,7 @@ void CGaussian::register_params() ParameterProperties::NONE, SG_OPTIONS(FULL, DIAG, SPHERICAL)); } -void CGaussian::decompose_cov(SGMatrix cov) +void Gaussian::decompose_cov(SGMatrix cov) { switch (m_cov_type) { @@ -383,7 +383,7 @@ void CGaussian::decompose_cov(SGMatrix cov) } } -SGVector CGaussian::sample() +SGVector Gaussian::sample() { SG_TRACE("Entering"); SGMatrix r_matrix(m_mean.vlen, m_mean.vlen); @@ -439,16 +439,16 @@ SGVector CGaussian::sample() return samp; } -CGaussian* CGaussian::obtain_from_generic(CDistribution* distribution) +std::shared_ptr Gaussian::obtain_from_generic(std::shared_ptr distribution) { if (!distribution) return NULL; - CGaussian* casted=dynamic_cast(distribution); + auto casted=std::dynamic_pointer_cast(distribution); if (!casted) return NULL; /* since an additional reference is returned */ - SG_REF(casted); + return casted; } diff --git a/src/shogun/distributions/Gaussian.h b/src/shogun/distributions/Gaussian.h index e19135f8488..734326624de 100644 --- a/src/shogun/distributions/Gaussian.h +++ b/src/shogun/distributions/Gaussian.h @@ -21,7 +21,7 @@ namespace shogun { -class CDotFeatures; +class DotFeatures; /** Covariance type */ enum ECovType @@ -41,19 +41,19 @@ enum ECovType * Likelihood is computed using the Gaussian PDF \f$(2\pi)^{-\frac{k}{2}}|\Sigma|^{-\frac{1}{2}}e^{-\frac{1}{2}(x-\mu)'\Sigma^{-1}(x-\mu)}\f$ * The actual computations depend on the type of covariance used. */ -class CGaussian : public RandomMixin +class Gaussian : public RandomMixin { public: /** default constructor */ - CGaussian(); + Gaussian(); /** constructor * * @param mean mean of the Gaussian * @param cov covariance of the Gaussian * @param cov_type covariance type (full, diagonal or shperical) */ - CGaussian(const SGVector mean, SGMatrix cov, ECovType cov_type=FULL); - virtual ~CGaussian(); + Gaussian(const SGVector mean, SGMatrix cov, ECovType cov_type=FULL); + virtual ~Gaussian(); /** Compute the constant part */ void init(); @@ -64,7 +64,7 @@ class CGaussian : public RandomMixin * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** get number of parameters in model * @@ -207,14 +207,14 @@ class CGaussian : public RandomMixin */ SGVector sample(); - /** @param distribution is casted to CGaussian, NULL if not possible + /** @param distribution is casted to Gaussian, NULL if not possible * Note that the object is SG_REF'ed - * @return casted CGaussian object + * @return casted Gaussian object */ #ifndef SWIG [[deprecated("use .as template function")]] #endif - static CGaussian* obtain_from_generic(CDistribution* distribution); + static std::shared_ptr obtain_from_generic(std::shared_ptr distribution); /** @return object name */ virtual const char* get_name() const { return "Gaussian"; } diff --git a/src/shogun/distributions/HMM.cpp b/src/shogun/distributions/HMM.cpp index 20e251049fc..580720a56eb 100644 --- a/src/shogun/distributions/HMM.cpp +++ b/src/shogun/distributions/HMM.cpp @@ -30,22 +30,22 @@ using namespace shogun; // Construction/Destruction ////////////////////////////////////////////////////////////////////// -const int32_t CHMM::GOTN= (1<<1); -const int32_t CHMM::GOTM= (1<<2); -const int32_t CHMM::GOTO= (1<<3); -const int32_t CHMM::GOTa= (1<<4); -const int32_t CHMM::GOTb= (1<<5); -const int32_t CHMM::GOTp= (1<<6); -const int32_t CHMM::GOTq= (1<<7); - -const int32_t CHMM::GOTlearn_a= (1<<1); -const int32_t CHMM::GOTlearn_b= (1<<2); -const int32_t CHMM::GOTlearn_p= (1<<3); -const int32_t CHMM::GOTlearn_q= (1<<4); -const int32_t CHMM::GOTconst_a= (1<<5); -const int32_t CHMM::GOTconst_b= (1<<6); -const int32_t CHMM::GOTconst_p= (1<<7); -const int32_t CHMM::GOTconst_q= (1<<8); +const int32_t HMM::GOTN= (1<<1); +const int32_t HMM::GOTM= (1<<2); +const int32_t HMM::GOTO= (1<<3); +const int32_t HMM::GOTa= (1<<4); +const int32_t HMM::GOTb= (1<<5); +const int32_t HMM::GOTp= (1<<6); +const int32_t HMM::GOTq= (1<<7); + +const int32_t HMM::GOTlearn_a= (1<<1); +const int32_t HMM::GOTlearn_b= (1<<2); +const int32_t HMM::GOTlearn_p= (1<<3); +const int32_t HMM::GOTlearn_q= (1<<4); +const int32_t HMM::GOTconst_a= (1<<5); +const int32_t HMM::GOTconst_b= (1<<6); +const int32_t HMM::GOTconst_p= (1<<7); +const int32_t HMM::GOTconst_q= (1<<8); enum E_STATE { @@ -74,7 +74,7 @@ enum E_STATE const char Model::FIX_DISALLOWED=0 ; const char Model::FIX_ALLOWED=1 ; const char Model::FIX_DEFAULT=-1 ; -const float64_t Model::DISALLOWED_PENALTY=CMath::ALMOST_NEG_INFTY ; +const float64_t Model::DISALLOWED_PENALTY=Math::ALMOST_NEG_INFTY ; #endif Model::Model() @@ -139,7 +139,7 @@ Model::~Model() } -CHMM::CHMM() +HMM::HMM() { N=0; M=0; @@ -180,8 +180,8 @@ CHMM::CHMM() mem_initialized = false; } -CHMM::CHMM(CHMM* h) -: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) +HMM::HMM(std::shared_ptr h) +: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) { #ifdef USE_HMMPARALLEL_STRUCTURES io::info("hmm is using {} separate tables", env()->get_num_threads()); @@ -194,8 +194,8 @@ CHMM::CHMM(CHMM* h) set_observations(h->p_observations); } -CHMM::CHMM(int32_t p_N, int32_t p_M, Model* p_model, float64_t p_PSEUDO) -: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) +HMM::HMM(int32_t p_N, int32_t p_M, Model* p_model, float64_t p_PSEUDO) +: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) { this->N=p_N; this->M=p_M; @@ -208,10 +208,10 @@ CHMM::CHMM(int32_t p_N, int32_t p_M, Model* p_model, float64_t p_PSEUDO) status=initialize_hmm(p_model, p_PSEUDO); } -CHMM::CHMM( - CStringFeatures* obs, int32_t p_N, int32_t p_M, +HMM::HMM( + std::shared_ptr> obs, int32_t p_N, int32_t p_M, float64_t p_PSEUDO) -: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) +: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) { this->N=p_N; this->M=p_M; @@ -225,8 +225,8 @@ CHMM::CHMM( set_observations(obs); } -CHMM::CHMM(int32_t p_N, float64_t* p, float64_t* q, float64_t* a) -: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) +HMM::HMM(int32_t p_N, float64_t* p, float64_t* q, float64_t* a) +: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) { this->N=p_N; this->M=0; @@ -275,10 +275,10 @@ CHMM::CHMM(int32_t p_N, float64_t* p, float64_t* q, float64_t* a) // this->invalidate_model(); } -CHMM::CHMM( +HMM::HMM( int32_t p_N, float64_t* p, float64_t* q, int32_t num_trans, float64_t* a_trans) -: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) +: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) { model=NULL ; @@ -385,8 +385,8 @@ CHMM::CHMM( } -CHMM::CHMM(FILE* model_file, float64_t p_PSEUDO) -: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) +HMM::HMM(FILE* model_file, float64_t p_PSEUDO) +: RandomMixin(), iterations(150), epsilon(1e-4), conv_it(5) { #ifdef USE_HMMPARALLEL_STRUCTURES io::info("hmm is using {} separate tables", env()->get_num_threads()); @@ -395,9 +395,9 @@ CHMM::CHMM(FILE* model_file, float64_t p_PSEUDO) status=initialize_hmm(NULL, p_PSEUDO, model_file); } -CHMM::~CHMM() +HMM::~HMM() { - SG_UNREF(p_observations); + if (trans_list_forward_cnt) SG_FREE(trans_list_forward_cnt); @@ -485,7 +485,7 @@ CHMM::~CHMM() } } -bool CHMM::train(CFeatures* data) +bool HMM::train(std::shared_ptr data) { if (data) { @@ -494,12 +494,12 @@ bool CHMM::train(CFeatures* data) { error("Expected features of class string type word"); } - set_observations((CStringFeatures*) data); + set_observations(std::static_pointer_cast>(data)); } return baum_welch_viterbi_train(BW_NORMAL); } -bool CHMM::alloc_state_dependend_arrays() +bool HMM::alloc_state_dependend_arrays() { if (!transition_matrix_a && !observation_matrix_b && @@ -545,7 +545,7 @@ bool CHMM::alloc_state_dependend_arrays() set_observations(p_observations); else set_observation_nocache(p_observations); - SG_UNREF(p_observations); + } this->invalidate_model(); @@ -556,7 +556,7 @@ bool CHMM::alloc_state_dependend_arrays() (end_state_distribution_q != NULL)); } -void CHMM::free_state_dependend_arrays() +void HMM::free_state_dependend_arrays() { #ifdef USE_HMMPARALLEL_STRUCTURES if (arrayN1 && arrayN2) @@ -594,7 +594,7 @@ void CHMM::free_state_dependend_arrays() end_state_distribution_q=NULL; } -bool CHMM::initialize_hmm(Model* m, float64_t pseudo, FILE* modelfile) +bool HMM::initialize_hmm(Model* m, float64_t pseudo, FILE* modelfile) { //yes optimistic bool files_ok=true; @@ -683,7 +683,7 @@ bool CHMM::initialize_hmm(Model* m, float64_t pseudo, FILE* modelfile) //forward algorithm //calculates Pr[O_0,O_1, ..., O_t, q_time=S_i| lambda] for 0<= time <= T-1 //Pr[O|lambda] for time > T -float64_t CHMM::forward_comp(int32_t time, int32_t state, int32_t dimension) +float64_t HMM::forward_comp(int32_t time, int32_t state, int32_t dimension) { T_ALPHA_BETA_TABLE* alpha_new; T_ALPHA_BETA_TABLE* alpha; @@ -721,11 +721,11 @@ float64_t CHMM::forward_comp(int32_t time, int32_t state, int32_t dimension) for (int32_t j=0; jget_feature(dimension,t)); @@ -748,11 +748,11 @@ float64_t CHMM::forward_comp(int32_t time, int32_t state, int32_t dimension) if (timeget_vector_length(dimension)) { int32_t i, num=trans_list_forward_cnt[state]; - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (i=0; iget_feature(dimension,time)); @@ -762,9 +762,9 @@ float64_t CHMM::forward_comp(int32_t time, int32_t state, int32_t dimension) // termination int32_t i ; float64_t sum ; - sum=-CMath::INFTY; + sum=-Math::INFTY; for (i=0; i T -float64_t CHMM::forward_comp_old(int32_t time, int32_t state, int32_t dimension) +float64_t HMM::forward_comp_old(int32_t time, int32_t state, int32_t dimension) { T_ALPHA_BETA_TABLE* alpha_new; T_ALPHA_BETA_TABLE* alpha; @@ -826,18 +826,18 @@ float64_t CHMM::forward_comp_old(int32_t time, int32_t state, int32_t dimension) int32_t i ; #ifdef USE_LOGSUMARRAY for (i=0; i<(N>>1); i++) - ARRAYS(dimension)[i]=CMath::logarithmic_sum(alpha[i<<1] + get_a(i<<1,j), + ARRAYS(dimension)[i]=Math::logarithmic_sum(alpha[i<<1] + get_a(i<<1,j), alpha[(i<<1)+1] + get_a((i<<1)+1,j)); if (N%2==1) alpha_new[j]=get_b(j, p_observations->get_feature(dimension,t))+ - CMath::logarithmic_sum(alpha[N-1]+get_a(N-1,j), - CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; + Math::logarithmic_sum(alpha[N-1]+get_a(N-1,j), + Math::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; else - alpha_new[j]=get_b(j, p_observations->get_feature(dimension,t))+CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; + alpha_new[j]=get_b(j, p_observations->get_feature(dimension,t))+Math::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; #else //USE_LOGSUMARRAY - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (i=0; iget_feature(dimension,t)); #endif //USE_LOGSUMARRAY @@ -862,18 +862,18 @@ float64_t CHMM::forward_comp_old(int32_t time, int32_t state, int32_t dimension) int32_t i; #ifdef USE_LOGSUMARRAY for (i=0; i<(N>>1); i++) - ARRAYS(dimension)[i]=CMath::logarithmic_sum(alpha[i<<1] + get_a(i<<1,state), + ARRAYS(dimension)[i]=Math::logarithmic_sum(alpha[i<<1] + get_a(i<<1,state), alpha[(i<<1)+1] + get_a((i<<1)+1,state)); if (N%2==1) return get_b(state, p_observations->get_feature(dimension,time))+ - CMath::logarithmic_sum(alpha[N-1]+get_a(N-1,state), - CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; + Math::logarithmic_sum(alpha[N-1]+get_a(N-1,state), + Math::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; else - return get_b(state, p_observations->get_feature(dimension,time))+CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; + return get_b(state, p_observations->get_feature(dimension,time))+Math::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; #else //USE_LOGSUMARRAY - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (i=0; iget_feature(dimension,time)); #endif //USE_LOGSUMARRAY @@ -885,17 +885,17 @@ float64_t CHMM::forward_comp_old(int32_t time, int32_t state, int32_t dimension) float64_t sum ; #ifdef USE_LOGSUMARRAY for (i=0; i<(N>>1); i++) - ARRAYS(dimension)[i]=CMath::logarithmic_sum(alpha[i<<1] + get_q(i<<1), + ARRAYS(dimension)[i]=Math::logarithmic_sum(alpha[i<<1] + get_q(i<<1), alpha[(i<<1)+1] + get_q((i<<1)+1)); if (N%2==1) - sum=CMath::logarithmic_sum(alpha[N-1]+get_q(N-1), - CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; + sum=Math::logarithmic_sum(alpha[N-1]+get_q(N-1), + Math::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; else - sum=CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; + sum=Math::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; #else //USE_LOGSUMARRAY - sum=-CMath::INFTY; + sum=-Math::INFTY; for (i=0; i= T -float64_t CHMM::backward_comp(int32_t time, int32_t state, int32_t dimension) +float64_t HMM::backward_comp(int32_t time, int32_t state, int32_t dimension) { T_ALPHA_BETA_TABLE* beta_new; T_ALPHA_BETA_TABLE* beta; @@ -957,11 +957,11 @@ float64_t CHMM::backward_comp(int32_t time, int32_t state, int32_t dimension) for (int32_t i=0; iget_feature(dimension,t)) + beta[jj]); + sum= Math::logarithmic_sum(sum, get_a(i, jj) + get_b(jj, p_observations->get_feature(dimension,t)) + beta[jj]); } ; beta_new[i]=sum; } @@ -982,11 +982,11 @@ float64_t CHMM::backward_comp(int32_t time, int32_t state, int32_t dimension) if (time>=0) { int32_t j, num=trans_list_backward_cnt[state] ; - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (j=0; jget_feature(dimension,time+1))+beta[jj]); + sum= Math::logarithmic_sum(sum, get_a(state, jj) + get_b(jj, p_observations->get_feature(dimension,time+1))+beta[jj]); } ; return sum; } @@ -994,9 +994,9 @@ float64_t CHMM::backward_comp(int32_t time, int32_t state, int32_t dimension) { if (BETA_CACHE(dimension).table) { - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (int32_t j=0; jget_feature(dimension,0))+beta[j]); + sum= Math::logarithmic_sum(sum, get_p(j) + get_b(j, p_observations->get_feature(dimension,0))+beta[j]); BETA_CACHE(dimension).sum=sum; BETA_CACHE(dimension).dimension=dimension; BETA_CACHE(dimension).updated=true; @@ -1008,9 +1008,9 @@ float64_t CHMM::backward_comp(int32_t time, int32_t state, int32_t dimension) } else { - float64_t sum=-CMath::INFTY; // apply LOG_SUM_ARRAY -- no cache ... does not make very sense anyway... + float64_t sum=-Math::INFTY; // apply LOG_SUM_ARRAY -- no cache ... does not make very sense anyway... for (int32_t j=0; jget_feature(dimension,0))+beta[j]); + sum= Math::logarithmic_sum(sum, get_p(j) + get_b(j, p_observations->get_feature(dimension,0))+beta[j]); return sum; } } @@ -1018,7 +1018,7 @@ float64_t CHMM::backward_comp(int32_t time, int32_t state, int32_t dimension) } -float64_t CHMM::backward_comp_old( +float64_t HMM::backward_comp_old( int32_t time, int32_t state, int32_t dimension) { T_ALPHA_BETA_TABLE* beta_new; @@ -1059,18 +1059,18 @@ float64_t CHMM::backward_comp_old( int32_t j ; #ifdef USE_LOGSUMARRAY for (j=0; j<(N>>1); j++) - ARRAYS(dimension)[j]=CMath::logarithmic_sum( + ARRAYS(dimension)[j]=Math::logarithmic_sum( get_a(i, j<<1) + get_b(j<<1, p_observations->get_feature(dimension,t)) + beta[j<<1], get_a(i, (j<<1)+1) + get_b((j<<1)+1, p_observations->get_feature(dimension,t)) + beta[(j<<1)+1]); if (N%2==1) - beta_new[i]=CMath::logarithmic_sum(get_a(i, N-1) + get_b(N-1, p_observations->get_feature(dimension,t)) + beta[N-1], - CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; + beta_new[i]=Math::logarithmic_sum(get_a(i, N-1) + get_b(N-1, p_observations->get_feature(dimension,t)) + beta[N-1], + Math::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; else - beta_new[i]=CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; + beta_new[i]=Math::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; #else //USE_LOGSUMARRAY - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (j=0; jget_feature(dimension,t)) + beta[j]); + sum= Math::logarithmic_sum(sum, get_a(i, j) + get_b(j, p_observations->get_feature(dimension,t)) + beta[j]); beta_new[i]=sum; #endif //USE_LOGSUMARRAY @@ -1094,18 +1094,18 @@ float64_t CHMM::backward_comp_old( int32_t j ; #ifdef USE_LOGSUMARRAY for (j=0; j<(N>>1); j++) - ARRAYS(dimension)[j]=CMath::logarithmic_sum( + ARRAYS(dimension)[j]=Math::logarithmic_sum( get_a(state, j<<1) + get_b(j<<1, p_observations->get_feature(dimension,time+1)) + beta[j<<1], get_a(state, (j<<1)+1) + get_b((j<<1)+1, p_observations->get_feature(dimension,time+1)) + beta[(j<<1)+1]); if (N%2==1) - return CMath::logarithmic_sum(get_a(state, N-1) + get_b(N-1, p_observations->get_feature(dimension,time+1)) + beta[N-1], - CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; + return Math::logarithmic_sum(get_a(state, N-1) + get_b(N-1, p_observations->get_feature(dimension,time+1)) + beta[N-1], + Math::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; else - return CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; + return Math::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; #else //USE_LOGSUMARRAY - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (j=0; jget_feature(dimension,time+1))+beta[j]); + sum= Math::logarithmic_sum(sum, get_a(state, j) + get_b(j, p_observations->get_feature(dimension,time+1))+beta[j]); return sum; #endif //USE_LOGSUMARRAY @@ -1116,17 +1116,17 @@ float64_t CHMM::backward_comp_old( { #ifdef USE_LOGSUMARRAY//AAA for (int32_t j=0; j<(N>>1); j++) - ARRAYS(dimension)[j]=CMath::logarithmic_sum(get_p(j<<1) + get_b(j<<1, p_observations->get_feature(dimension,0))+beta[j<<1], + ARRAYS(dimension)[j]=Math::logarithmic_sum(get_p(j<<1) + get_b(j<<1, p_observations->get_feature(dimension,0))+beta[j<<1], get_p((j<<1)+1) + get_b((j<<1)+1, p_observations->get_feature(dimension,0))+beta[(j<<1)+1]) ; if (N%2==1) - BETA_CACHE(dimension).sum=CMath::logarithmic_sum(get_p(N-1) + get_b(N-1, p_observations->get_feature(dimension,0))+beta[N-1], - CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; + BETA_CACHE(dimension).sum=Math::logarithmic_sum(get_p(N-1) + get_b(N-1, p_observations->get_feature(dimension,0))+beta[N-1], + Math::logarithmic_sum_array(ARRAYS(dimension), N>>1)) ; else - BETA_CACHE(dimension).sum=CMath::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; + BETA_CACHE(dimension).sum=Math::logarithmic_sum_array(ARRAYS(dimension), N>>1) ; #else //USE_LOGSUMARRAY - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (int32_t j=0; jget_feature(dimension,0))+beta[j]); + sum= Math::logarithmic_sum(sum, get_p(j) + get_b(j, p_observations->get_feature(dimension,0))+beta[j]); BETA_CACHE(dimension).sum=sum; #endif //USE_LOGSUMARRAY BETA_CACHE(dimension).dimension=dimension; @@ -1139,9 +1139,9 @@ float64_t CHMM::backward_comp_old( } else { - float64_t sum=-CMath::INFTY; // apply LOG_SUM_ARRAY -- no cache ... does not make very sense anyway... + float64_t sum=-Math::INFTY; // apply LOG_SUM_ARRAY -- no cache ... does not make very sense anyway... for (int32_t j=0; jget_feature(dimension,0))+beta[j]); + sum= Math::logarithmic_sum(sum, get_p(j) + get_b(j, p_observations->get_feature(dimension,0))+beta[j]); return sum; } } @@ -1150,7 +1150,7 @@ float64_t CHMM::backward_comp_old( //calculates probability of best path through the model lambda AND path itself //using viterbi algorithm -float64_t CHMM::best_path(int32_t dimension) +float64_t HMM::best_path(int32_t dimension) { if (!p_observations) return -1; @@ -1193,7 +1193,7 @@ float64_t CHMM::best_path(int32_t dimension) } #ifdef USE_PATHDEBUG - float64_t worst=-CMath::INFTY/4 ; + float64_t worst=-Math::INFTY/4 ; #endif //recursion for (int32_t t=1; tget_vector_length(dimension); t++) @@ -1233,7 +1233,7 @@ float64_t CHMM::best_path(int32_t dimension) if (delta_new[jj]>best) best=delta_new[jj] ; - if (best<-CMath::INFTY/2) + if (best<-Math::INFTY/2) { SG_DEBUG("worst case at {}: {:e}:{:e}", t, best, worst) worst=best ; @@ -1278,7 +1278,7 @@ float64_t CHMM::best_path(int32_t dimension) } #ifndef USE_HMMPARALLEL -float64_t CHMM::model_probability_comp() +float64_t HMM::model_probability_comp() { //for faster calculation cache model probability mod_prob=0 ; @@ -1394,11 +1394,11 @@ void CHMM::ab_buf_comp( //estimate a for (j=0; jget_vector_length(dim)-1; t++) { - a_sum= CMath::logarithmic_sum(a_sum, forward(t,i,dim)+ + a_sum= Math::logarithmic_sum(a_sum, forward(t,i,dim)+ get_a(i,j)+get_b(j,p_observations->get_feature(dim,t+1))+backward(t+1,j,dim)); } a_buf[N*i+j]=a_sum-dimmodprob ; @@ -1407,12 +1407,12 @@ void CHMM::ab_buf_comp( //estimate b for (j=0; jget_vector_length(dim); t++) { if (p_observations->get_feature(dim,t)==j) - b_sum=CMath::logarithmic_sum(b_sum, forward(t,i,dim)+backward(t, i, dim)); + b_sum=Math::logarithmic_sum(b_sum, forward(t,i,dim)+backward(t, i, dim)); } b_buf[M*i+j]=b_sum-dimmodprob ; @@ -1421,7 +1421,7 @@ void CHMM::ab_buf_comp( } //estimates new model lambda out of lambda_train using baum welch algorithm -void CHMM::estimate_model_baum_welch(CHMM* hmm) +void CHMM::estimate_model_baum_welch(std::shared_ptr hmm) { int32_t i,j,cpu; float64_t fullmodprob=0; //for all dims @@ -1429,22 +1429,22 @@ void CHMM::estimate_model_baum_welch(CHMM* hmm) //clear actual model a,b,p,q are used as numerator for (i=0; iget_p(i)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_p(i)>Math::ALMOST_NEG_INFTY) set_p(i,log(PSEUDO)); else set_p(i,hmm->get_p(i)); - if (hmm->get_q(i)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_q(i)>Math::ALMOST_NEG_INFTY) set_q(i,log(PSEUDO)); else set_q(i,hmm->get_q(i)); for (j=0; jget_a(i,j)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_a(i,j)>Math::ALMOST_NEG_INFTY) set_a(i,j, log(PSEUDO)); else set_a(i,j,hmm->get_a(i,j)); for (j=0; jget_b(i,j)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_b(i,j)>Math::ALMOST_NEG_INFTY) set_b(i,j, log(PSEUDO)); else set_b(i,j,hmm->get_b(i,j)); @@ -1487,16 +1487,16 @@ void CHMM::estimate_model_baum_welch(CHMM* hmm) for (i=0; i estimate) { int32_t i,j,t,dim; float64_t a_sum, b_sum; //numerator @@ -1536,22 +1536,22 @@ void CHMM::estimate_model_baum_welch(CHMM* estimate) //clear actual model a,b,p,q are used as numerator for (i=0; iget_p(i)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_p(i)>Math::ALMOST_NEG_INFTY) set_p(i,log(PSEUDO)); else set_p(i,estimate->get_p(i)); - if (estimate->get_q(i)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_q(i)>Math::ALMOST_NEG_INFTY) set_q(i,log(PSEUDO)); else set_q(i,estimate->get_q(i)); for (j=0; jget_a(i,j)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_a(i,j)>Math::ALMOST_NEG_INFTY) set_a(i,j, log(PSEUDO)); else set_a(i,j,estimate->get_a(i,j)); for (j=0; jget_b(i,j)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_b(i,j)>Math::ALMOST_NEG_INFTY) set_b(i,j, log(PSEUDO)); else set_b(i,j,estimate->get_b(i,j)); @@ -1567,8 +1567,8 @@ void CHMM::estimate_model_baum_welch(CHMM* estimate) for (i=0; iget_p(i)+estimate->get_b(i,p_observations->get_feature(dim,0))+estimate->backward(0,i,dim) - dimmodprob)); - set_q(i, CMath::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+estimate->get_q(i) - dimmodprob )); + set_p(i, Math::logarithmic_sum(get_p(i), estimate->get_p(i)+estimate->get_b(i,p_observations->get_feature(dim,0))+estimate->backward(0,i,dim) - dimmodprob)); + set_q(i, Math::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+estimate->get_q(i) - dimmodprob )); int32_t num = trans_list_backward_cnt[i] ; @@ -1576,28 +1576,28 @@ void CHMM::estimate_model_baum_welch(CHMM* estimate) for (j=0; jget_vector_length(dim)-1; t++) { - a_sum= CMath::logarithmic_sum(a_sum, estimate->forward(t,i,dim)+ + a_sum= Math::logarithmic_sum(a_sum, estimate->forward(t,i,dim)+ estimate->get_a(i,jj)+estimate->get_b(jj,p_observations->get_feature(dim,t+1))+estimate->backward(t+1,jj,dim)); } - set_a(i,jj, CMath::logarithmic_sum(get_a(i,jj), a_sum-dimmodprob)); + set_a(i,jj, Math::logarithmic_sum(get_a(i,jj), a_sum-dimmodprob)); } //estimate b for (j=0; jget_vector_length(dim); t++) { if (p_observations->get_feature(dim,t)==j) - b_sum=CMath::logarithmic_sum(b_sum, estimate->forward(t,i,dim)+estimate->backward(t, i, dim)); + b_sum=Math::logarithmic_sum(b_sum, estimate->forward(t,i,dim)+estimate->backward(t, i, dim)); } - set_b(i,j, CMath::logarithmic_sum(get_b(i,j), b_sum-dimmodprob)); + set_b(i,j, Math::logarithmic_sum(get_b(i,j), b_sum-dimmodprob)); } } } @@ -1612,7 +1612,7 @@ void CHMM::estimate_model_baum_welch(CHMM* estimate) } //estimates new model lambda out of lambda_estimate using baum welch algorithm -void CHMM::estimate_model_baum_welch_old(CHMM* estimate) +void HMM::estimate_model_baum_welch_old(std::shared_ptr estimate) { int32_t i,j,t,dim; float64_t a_sum, b_sum; //numerator @@ -1622,22 +1622,22 @@ void CHMM::estimate_model_baum_welch_old(CHMM* estimate) //clear actual model a,b,p,q are used as numerator for (i=0; iget_p(i)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_p(i)>Math::ALMOST_NEG_INFTY) set_p(i,log(PSEUDO)); else set_p(i,estimate->get_p(i)); - if (estimate->get_q(i)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_q(i)>Math::ALMOST_NEG_INFTY) set_q(i,log(PSEUDO)); else set_q(i,estimate->get_q(i)); for (j=0; jget_a(i,j)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_a(i,j)>Math::ALMOST_NEG_INFTY) set_a(i,j, log(PSEUDO)); else set_a(i,j,estimate->get_a(i,j)); for (j=0; jget_b(i,j)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_b(i,j)>Math::ALMOST_NEG_INFTY) set_b(i,j, log(PSEUDO)); else set_b(i,j,estimate->get_b(i,j)); @@ -1653,34 +1653,34 @@ void CHMM::estimate_model_baum_welch_old(CHMM* estimate) for (i=0; iget_p(i)+estimate->get_b(i,p_observations->get_feature(dim,0))+estimate->backward(0,i,dim) - dimmodprob)); - set_q(i, CMath::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+estimate->get_q(i) - dimmodprob )); + set_p(i, Math::logarithmic_sum(get_p(i), estimate->get_p(i)+estimate->get_b(i,p_observations->get_feature(dim,0))+estimate->backward(0,i,dim) - dimmodprob)); + set_q(i, Math::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+estimate->get_q(i) - dimmodprob )); //estimate a for (j=0; jget_vector_length(dim)-1; t++) { - a_sum= CMath::logarithmic_sum(a_sum, estimate->forward(t,i,dim)+ + a_sum= Math::logarithmic_sum(a_sum, estimate->forward(t,i,dim)+ estimate->get_a(i,j)+estimate->get_b(j,p_observations->get_feature(dim,t+1))+estimate->backward(t+1,j,dim)); } - set_a(i,j, CMath::logarithmic_sum(get_a(i,j), a_sum-dimmodprob)); + set_a(i,j, Math::logarithmic_sum(get_a(i,j), a_sum-dimmodprob)); } //estimate b for (j=0; jget_vector_length(dim); t++) { if (p_observations->get_feature(dim,t)==j) - b_sum=CMath::logarithmic_sum(b_sum, estimate->forward(t,i,dim)+estimate->backward(t, i, dim)); + b_sum=Math::logarithmic_sum(b_sum, estimate->forward(t,i,dim)+estimate->backward(t, i, dim)); } - set_b(i,j, CMath::logarithmic_sum(get_b(i,j), b_sum-dimmodprob)); + set_b(i,j, Math::logarithmic_sum(get_b(i,j), b_sum-dimmodprob)); } } } @@ -1697,7 +1697,7 @@ void CHMM::estimate_model_baum_welch_old(CHMM* estimate) //estimates new model lambda out of lambda_estimate using baum welch algorithm // optimize only p, q, a but not b -void CHMM::estimate_model_baum_welch_trans(CHMM* estimate) +void HMM::estimate_model_baum_welch_trans(std::shared_ptr estimate) { int32_t i,j,t,dim; float64_t a_sum; //numerator @@ -1707,17 +1707,17 @@ void CHMM::estimate_model_baum_welch_trans(CHMM* estimate) //clear actual model a,b,p,q are used as numerator for (i=0; iget_p(i)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_p(i)>Math::ALMOST_NEG_INFTY) set_p(i,log(PSEUDO)); else set_p(i,estimate->get_p(i)); - if (estimate->get_q(i)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_q(i)>Math::ALMOST_NEG_INFTY) set_q(i,log(PSEUDO)); else set_q(i,estimate->get_q(i)); for (j=0; jget_a(i,j)>CMath::ALMOST_NEG_INFTY) + if (estimate->get_a(i,j)>Math::ALMOST_NEG_INFTY) set_a(i,j, log(PSEUDO)); else set_a(i,j,estimate->get_a(i,j)); @@ -1735,22 +1735,22 @@ void CHMM::estimate_model_baum_welch_trans(CHMM* estimate) for (i=0; iget_p(i)+estimate->get_b(i,p_observations->get_feature(dim,0))+estimate->backward(0,i,dim) - dimmodprob)); - set_q(i, CMath::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+estimate->get_q(i) - dimmodprob )); + set_p(i, Math::logarithmic_sum(get_p(i), estimate->get_p(i)+estimate->get_b(i,p_observations->get_feature(dim,0))+estimate->backward(0,i,dim) - dimmodprob)); + set_q(i, Math::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+estimate->get_q(i) - dimmodprob )); int32_t num = trans_list_backward_cnt[i] ; //estimate a for (j=0; jget_vector_length(dim)-1; t++) { - a_sum= CMath::logarithmic_sum(a_sum, estimate->forward(t,i,dim)+ + a_sum= Math::logarithmic_sum(a_sum, estimate->forward(t,i,dim)+ estimate->get_a(i,jj)+estimate->get_b(jj,p_observations->get_feature(dim,t+1))+estimate->backward(t+1,jj,dim)); } - set_a(i,jj, CMath::logarithmic_sum(get_a(i,jj), a_sum-dimmodprob)); + set_a(i,jj, Math::logarithmic_sum(get_a(i,jj), a_sum-dimmodprob)); } } } @@ -1767,12 +1767,12 @@ void CHMM::estimate_model_baum_welch_trans(CHMM* estimate) //estimates new model lambda out of lambda_estimate using baum welch algorithm -void CHMM::estimate_model_baum_welch_defined(CHMM* estimate) +void HMM::estimate_model_baum_welch_defined(std::shared_ptr estimate) { int32_t i,j,old_i,k,t,dim; float64_t a_sum_num, b_sum_num; //numerator float64_t a_sum_denom, b_sum_denom; //denominator - float64_t dimmodprob=-CMath::INFTY; //model probability for dim + float64_t dimmodprob=-Math::INFTY; //model probability for dim float64_t fullmodprob=0; //for all dims float64_t* A=ARRAYN1(0); float64_t* B=ARRAYN2(0); @@ -1845,10 +1845,10 @@ void CHMM::estimate_model_baum_welch_defined(CHMM* estimate) //estimate initial+end state distribution numerator for (k=0; (i=model->get_learn_p(k))!=-1; k++) - set_p(i, CMath::logarithmic_sum(get_p(i), estimate->forward(0,i,dim)+estimate->backward(0,i,dim) - dimmodprob ) ); + set_p(i, Math::logarithmic_sum(get_p(i), estimate->forward(0,i,dim)+estimate->backward(0,i,dim) - dimmodprob ) ); for (k=0; (i=model->get_learn_q(k))!=-1; k++) - set_q(i, CMath::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+ + set_q(i, Math::logarithmic_sum(get_q(i), estimate->forward(p_observations->get_vector_length(dim)-1, i, dim)+ estimate->backward(p_observations->get_vector_length(dim)-1, i, dim) - dimmodprob ) ); //estimate a @@ -1860,23 +1860,23 @@ void CHMM::estimate_model_baum_welch_defined(CHMM* estimate) if (old_i!=i) { old_i=i; - a_sum_denom=-CMath::INFTY; + a_sum_denom=-Math::INFTY; for (t=0; tget_vector_length(dim)-1; t++) - a_sum_denom= CMath::logarithmic_sum(a_sum_denom, estimate->forward(t,i,dim)+estimate->backward(t,i,dim)); + a_sum_denom= Math::logarithmic_sum(a_sum_denom, estimate->forward(t,i,dim)+estimate->backward(t,i,dim)); - A[i]= CMath::logarithmic_sum(A[i], a_sum_denom-dimmodprob); + A[i]= Math::logarithmic_sum(A[i], a_sum_denom-dimmodprob); } j=model->get_learn_a(k,1); - a_sum_num=-CMath::INFTY; + a_sum_num=-Math::INFTY; for (t=0; tget_vector_length(dim)-1; t++) { - a_sum_num= CMath::logarithmic_sum(a_sum_num, estimate->forward(t,i,dim)+ + a_sum_num= Math::logarithmic_sum(a_sum_num, estimate->forward(t,i,dim)+ estimate->get_a(i,j)+estimate->get_b(j,p_observations->get_feature(dim,t+1))+estimate->backward(t+1,j,dim)); } - set_a(i,j, CMath::logarithmic_sum(get_a(i,j), a_sum_num-dimmodprob)); + set_a(i,j, Math::logarithmic_sum(get_a(i,j), a_sum_num-dimmodprob)); } //estimate b @@ -1889,23 +1889,23 @@ void CHMM::estimate_model_baum_welch_defined(CHMM* estimate) if (old_i!=i) { old_i=i; - b_sum_denom=-CMath::INFTY; + b_sum_denom=-Math::INFTY; for (t=0; tget_vector_length(dim); t++) - b_sum_denom= CMath::logarithmic_sum(b_sum_denom, estimate->forward(t,i,dim)+estimate->backward(t,i,dim)); + b_sum_denom= Math::logarithmic_sum(b_sum_denom, estimate->forward(t,i,dim)+estimate->backward(t,i,dim)); - B[i]= CMath::logarithmic_sum(B[i], b_sum_denom-dimmodprob); + B[i]= Math::logarithmic_sum(B[i], b_sum_denom-dimmodprob); } j=model->get_learn_b(k,1); - b_sum_num=-CMath::INFTY; + b_sum_num=-Math::INFTY; for (t=0; tget_vector_length(dim); t++) { if (p_observations->get_feature(dim,t)==j) - b_sum_num=CMath::logarithmic_sum(b_sum_num, estimate->forward(t,i,dim)+estimate->backward(t, i, dim)); + b_sum_num=Math::logarithmic_sum(b_sum_num, estimate->forward(t,i,dim)+estimate->backward(t, i, dim)); } - set_b(i,j, CMath::logarithmic_sum(get_b(i,j), b_sum_num-dimmodprob)); + set_b(i,j, Math::logarithmic_sum(get_b(i,j), b_sum_num-dimmodprob)); } } #ifdef USE_HMMPARALLEL @@ -1943,7 +1943,7 @@ void CHMM::estimate_model_baum_welch_defined(CHMM* estimate) } //estimates new model lambda out of lambda_estimate using viterbi algorithm -void CHMM::estimate_model_viterbi(CHMM* estimate) +void HMM::estimate_model_viterbi(std::shared_ptr estimate) { int32_t i,j,t; float64_t sum; @@ -2070,7 +2070,7 @@ void CHMM::estimate_model_viterbi(CHMM* estimate) } // estimate parameters listed in learn_x -void CHMM::estimate_model_viterbi_defined(CHMM* estimate) +void HMM::estimate_model_viterbi_defined(std::shared_ptr estimate) { int32_t i,j,k,t; float64_t sum; @@ -2252,14 +2252,14 @@ void CHMM::estimate_model_viterbi_defined(CHMM* estimate) //------------------------------------------------------------------------------------// //to give an idea what the model looks like -void CHMM::output_model(bool verbose) +void HMM::output_model(bool verbose) { int32_t i,j; float64_t checksum; //generic info io::info("log(Pr[O|model])={:e}, #states: {}, #observationssymbols: {}, #observations: {}x{}", - (float64_t)((p_observations) ? model_probability() : -CMath::INFTY), + (float64_t)((p_observations) ? model_probability() : -Math::INFTY), N, M, ((p_observations) ? p_observations->get_max_vector_length() : 0), ((p_observations) ? p_observations->get_num_vectors() : 0)); if (verbose) @@ -2271,7 +2271,7 @@ void CHMM::output_model(bool verbose) checksum= get_q(i); for (j=0; jget_max_vector_length() : 0), ((p_observations) ? p_observations->get_num_vectors() : 0)); if (verbose) @@ -2391,7 +2391,7 @@ void CHMM::output_model_defined(bool verbose) //------------------------------------------------------------------------------------// //convert model to log probabilities -void CHMM::convert_to_log() +void HMM::convert_to_log() { int32_t i,j; @@ -2400,7 +2400,7 @@ void CHMM::convert_to_log() if (get_p(i)!=0) set_p(i, log(get_p(i))); else - set_p(i, -CMath::INFTY);; + set_p(i, -Math::INFTY);; } for (i=0; i uniform_real_dist(MIN_RAND, 1.0); @@ -2505,7 +2505,7 @@ void CHMM::init_model_random() } //init model according to const_x -void CHMM::init_model_defined() +void HMM::init_model_defined() { int32_t i,j,k,r; float64_t sum; @@ -2660,7 +2660,7 @@ void CHMM::init_model_defined() invalidate_model(); } -void CHMM::clear_model() +void HMM::clear_model() { int32_t i,j; for (i=0; i l) { int32_t i,j; for (i=0; imod_prob=0.0; @@ -2755,7 +2755,7 @@ void CHMM::invalidate_model() trans_list_forward_cnt[j]= 0 ; trans_list_forward[j]= SG_MALLOC(T_STATES, N); for (int32_t i=0; iCMath::ALMOST_NEG_INFTY) + if (get_a(i,j)>Math::ALMOST_NEG_INFTY) { trans_list_forward[j][trans_list_forward_cnt[j]]=i ; trans_list_forward_cnt[j]++ ; @@ -2770,7 +2770,7 @@ void CHMM::invalidate_model() trans_list_backward_cnt[i]= 0 ; trans_list_backward[i]= SG_MALLOC(T_STATES, N); for (int32_t j=0; jCMath::ALMOST_NEG_INFTY) + if (get_a(i,j)>Math::ALMOST_NEG_INFTY) { trans_list_backward[i][trans_list_backward_cnt[i]]=j ; trans_list_backward_cnt[i]++ ; @@ -2802,7 +2802,7 @@ void CHMM::invalidate_model() #endif // USE_HMMPARALLEL_STRUCTURES } -void CHMM::open_bracket(FILE* file) +void HMM::open_bracket(FILE* file) { int32_t value; while (((value=fgetc(file)) != EOF) && (value!='[')) //skip possible spaces and end if '[' occurs @@ -2823,7 +2823,7 @@ void CHMM::open_bracket(FILE* file) ungetc(value, file); } -void CHMM::close_bracket(FILE* file) +void HMM::close_bracket(FILE* file) { int32_t value; while (((value=fgetc(file)) != EOF) && (value!=']')) //skip possible spaces and end if ']' occurs @@ -2836,7 +2836,7 @@ void CHMM::close_bracket(FILE* file) error_in_line(line, "expected \"]\" in input file"); } -bool CHMM::comma_or_space(FILE* file) +bool HMM::comma_or_space(FILE* file) { int32_t value; while (((value=fgetc(file)) != EOF) && (value!=',') && (value!=';') && (value!=']')) //skip possible spaces and end if ',' or ';' occurs @@ -2863,7 +2863,7 @@ bool CHMM::comma_or_space(FILE* file) return true ; } -bool CHMM::get_numbuffer(FILE* file, char* buffer, int32_t length) +bool HMM::get_numbuffer(FILE* file, char* buffer, int32_t length) { signed char value; @@ -2887,16 +2887,16 @@ bool CHMM::get_numbuffer(FILE* file, char* buffer, int32_t length) switch (value) { case 'A': - value='0' +CAlphabet::B_A; + value='0' +Alphabet::B_A; break; case 'C': - value='0' +CAlphabet::B_C; + value='0' +Alphabet::B_C; break; case 'G': - value='0' +CAlphabet::B_G; + value='0' +Alphabet::B_G; break; case 'T': - value='0' +CAlphabet::B_T; + value='0' +Alphabet::B_T; break; }; @@ -2910,16 +2910,16 @@ bool CHMM::get_numbuffer(FILE* file, char* buffer, int32_t length) switch (value) { case 'A': - value='0' +CAlphabet::B_A; + value='0' +Alphabet::B_A; break; case 'C': - value='0' +CAlphabet::B_C; + value='0' +Alphabet::B_C; break; case 'G': - value='0' +CAlphabet::B_G; + value='0' +Alphabet::B_G; break; case 'T': - value='0' +CAlphabet::B_T; + value='0' +Alphabet::B_T; break; case '1': case '2': case'3': case '4': case'5': case '6': case '7': case'8': case '9': case '0': break ; @@ -2972,7 +2972,7 @@ bool CHMM::get_numbuffer(FILE* file, char* buffer, int32_t length) ]; */ -bool CHMM::load_model(FILE* file) +bool HMM::load_model(FILE* file) { int32_t received_params=0; //a,b,p,N,M,O @@ -3271,7 +3271,7 @@ bool CHMM::load_model(FILE* file) const_p[]=[ [, ], ... , [,], [-1,-1] ]; const_q[]=[ [, ], ... , [,], [-1,-1] ]; */ -bool CHMM::load_definitions(FILE* file, bool verbose, bool _initialize) +bool HMM::load_definitions(FILE* file, bool verbose, bool _initialize) { if (model) delete model ; @@ -3976,11 +3976,11 @@ bool CHMM::load_definitions(FILE* file, bool verbose, bool _initialize) ]; */ -bool CHMM::save_model(FILE* file) +bool HMM::save_model(FILE* file) { bool result=false; int32_t i,j; - const float32_t NAN_REPLACEMENT = (float32_t) CMath::ALMOST_NEG_INFTY ; + const float32_t NAN_REPLACEMENT = (float32_t) Math::ALMOST_NEG_INFTY ; if (file) { @@ -3992,13 +3992,13 @@ bool CHMM::save_model(FILE* file) for (i=0; iget_learn_p(i)>=0; i++) @@ -4234,7 +4234,7 @@ bool CHMM::save_model_bin(FILE* file) io::info("wrote {} parameters for b",num_b); // write id - FLOATWRITE(file, (float32_t)CMath::INFTY); + FLOATWRITE(file, (float32_t)Math::INFTY); FLOATWRITE(file, (float32_t) 3); // write number of parameters @@ -4249,7 +4249,7 @@ bool CHMM::save_model_bin(FILE* file) return true ; } -bool CHMM::save_path_derivatives(FILE* logfile) +bool HMM::save_path_derivatives(FILE* logfile) { int32_t dim,i,j; @@ -4296,7 +4296,7 @@ bool CHMM::save_path_derivatives(FILE* logfile) return true ; } -bool CHMM::save_path_derivatives_bin(FILE* logfile) +bool HMM::save_path_derivatives_bin(FILE* logfile) { bool result=false; int32_t dim,i,j,q; @@ -4379,7 +4379,7 @@ bool CHMM::save_path_derivatives_bin(FILE* logfile) return result; } -bool CHMM::save_model_derivatives_bin(FILE* file) +bool HMM::save_model_derivatives_bin(FILE* file) { bool result=false; int32_t dim,i,j,q ; @@ -4500,7 +4500,7 @@ bool CHMM::save_model_derivatives_bin(FILE* file) return result; } -bool CHMM::save_model_derivatives(FILE* file) +bool HMM::save_model_derivatives(FILE* file) { bool result=false; int32_t dim,i,j; @@ -4548,7 +4548,7 @@ bool CHMM::save_model_derivatives(FILE* file) return result; } -bool CHMM::check_model_derivatives_combined() +bool HMM::check_model_derivatives_combined() { // bool result=false; const float64_t delta=5e-4 ; @@ -4618,7 +4618,7 @@ bool CHMM::check_model_derivatives_combined() return true ; } -bool CHMM::check_model_derivatives() +bool HMM::check_model_derivatives() { bool result=false; const float64_t delta=3e-4 ; @@ -4826,7 +4826,7 @@ bool CHMM::check_path_derivatives() #endif // USE_HMMDEBUG //normalize model (sum to one constraint) -void CHMM::normalize(bool keep_dead_states) +void HMM::normalize(bool keep_dead_states) { int32_t i,j; const float64_t INF=-1e10; @@ -4834,15 +4834,15 @@ void CHMM::normalize(bool keep_dead_states) for (i=0; iCMath::ALMOST_NEG_INFTY/N || (!keep_dead_states) ) + if (sum_a>Math::ALMOST_NEG_INFTY/N || (!keep_dead_states) ) { for (j=0; j app_model) { bool result=false; const int32_t num_states=app_model->get_N(); @@ -4880,14 +4880,14 @@ bool CHMM::append_model(CHMM* app_model) //clear n_x for (i=0; iget_p(j-N), n_a[(N+num_states)*j + i]); + n_a[(N+num_states)*j + i]=Math::logarithmic_sum(get_q(i)+app_model->get_p(j-N), n_a[(N+num_states)*j + i]); } free_state_dependend_arrays(); @@ -4953,7 +4953,7 @@ bool CHMM::append_model(CHMM* app_model) return result; } -bool CHMM::append_model(CHMM* app_model, float64_t* cur_out, float64_t* app_out) +bool HMM::append_model(std::shared_ptr app_model, float64_t* cur_out, float64_t* app_out) { bool result=false; const int32_t num_states=app_model->get_N()+2; @@ -4970,14 +4970,14 @@ bool CHMM::append_model(CHMM* app_model, float64_t* cur_out, float64_t* app_out) //clear n_x for (i=0; i* obs) +void HMM::set_observation_nocache(std::shared_ptr> obs) { ASSERT(obs) p_observations=obs; - SG_REF(obs); + if (obs) if (obs->get_num_symbols() > M) @@ -5309,16 +5309,16 @@ void CHMM::set_observation_nocache(CStringFeatures* obs) invalidate_model(); } -void CHMM::set_observations(CStringFeatures* obs, CHMM* lambda) +void HMM::set_observations(std::shared_ptr> obs, std::shared_ptr lambda) { ASSERT(obs) - SG_REF(obs); + p_observations=obs; /* from Distribution, necessary for calls to base class methods, like * get_log_likelihood_sample(): */ - SG_REF(obs); + features=obs; SG_DEBUG("num symbols alphabet: {}", obs->get_alphabet()->get_num_symbols()) @@ -5454,7 +5454,7 @@ void CHMM::set_observations(CStringFeatures* obs, CHMM* lambda) invalidate_model(); } -bool CHMM::permutation_entropy(int32_t window_width, int32_t sequence_number) +bool HMM::permutation_entropy(int32_t window_width, int32_t sequence_number) { if (p_observations && window_width>0 && ( sequence_number<0 || sequence_number < p_observations->get_num_vectors())) @@ -5512,7 +5512,7 @@ bool CHMM::permutation_entropy(int32_t window_width, int32_t sequence_number) return false; } -float64_t CHMM::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t HMM::get_log_derivative(int32_t num_param, int32_t num_example) { if (num_param(this); + auto working=std::shared_ptr(this); + float64_t prob_max=-Math::INFTY; + float64_t prob=-Math::INFTY; + float64_t prob_train=Math::ALMOST_NEG_INFTY; iteration_count=iterations; // TODO: replace with the new signal - // while (!converged(prob, prob_train) && (!CSignal::cancel_computations())) + // while (!converged(prob, prob_train) && (!Signal::cancel_computations())) while (!converged(prob, prob_train)) { - CMath::swap(working, estimate); + working.swap(estimate); prob=prob_train; switch (type) { @@ -5613,13 +5613,10 @@ bool CHMM::baum_welch_viterbi_train(BaumWelchViterbiType type) prob_max=prob_train; } - if (estimate == this) + if (estimate.get() == this) { estimate->copy_model(working); - delete working; } - else - delete estimate; return true; } diff --git a/src/shogun/distributions/HMM.h b/src/shogun/distributions/HMM.h index 38d2d209355..657d300c957 100644 --- a/src/shogun/distributions/HMM.h +++ b/src/shogun/distributions/HMM.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evan Shelhamer, Elijah Rippeth, Saurabh Goyal, + * Authors: Soeren Sonnenburg, Evan Shelhamer, Elijah Rippeth, Saurabh Goyal, * Bjoern Esser */ @@ -23,8 +23,8 @@ namespace shogun { - class CFeatures; - template class CStringFeatures; + class Features; + template class StringFeatures; /**@name HMM specific types*/ //@{ @@ -91,13 +91,13 @@ class Model /// sorts learn_a matrix inline void sort_learn_a() { - CMath::sort(learn_a,2) ; + Math::sort(learn_a,2) ; } /// sorts learn_b matrix inline void sort_learn_b() { - CMath::sort(learn_b,2) ; + Math::sort(learn_b,2) ; } /**@name read access functions. @@ -363,7 +363,7 @@ class Model * Several functions for tasks such as training,reading/writing models, reading observations, * calculation of derivatives are supplied. */ -class CHMM : public RandomMixin +class HMM : public RandomMixin { private: @@ -380,7 +380,7 @@ class CHMM : public RandomMixin /// Datatype that is used in parrallel computation of viterbi struct S_DIM_THREAD_PARAM { - CHMM* hmm; + std::shared_ptr hmm; int32_t dim; float64_t prob_sum; }; @@ -388,7 +388,7 @@ class CHMM : public RandomMixin /// Datatype that is used in parrallel baum welch model estimation struct S_BW_THREAD_PARAM { - CHMM* hmm; + std::shared_ptr hmm; int32_t dim_start; int32_t dim_stop; @@ -460,7 +460,7 @@ class CHMM : public RandomMixin public: /** default constructor */ - CHMM(); + HMM(); /**@name Constructor/Destructor and helper function */ @@ -472,14 +472,14 @@ class CHMM : public RandomMixin * @param PSEUDO Pseudo Value */ - CHMM( + HMM( int32_t N, int32_t M, Model* model, float64_t PSEUDO); - CHMM( - CStringFeatures* obs, int32_t N, int32_t M, + HMM( + std::shared_ptr> obs, int32_t N, int32_t M, float64_t PSEUDO); - CHMM( + HMM( int32_t N, float64_t* p, float64_t* q, float64_t* a); - CHMM( + HMM( int32_t N, float64_t* p, float64_t* q, int32_t num_trans, float64_t* a_trans); @@ -487,13 +487,13 @@ class CHMM : public RandomMixin * @param model_file Filehandle to a hmm model file (*.mod) * @param PSEUDO Pseudo Value */ - CHMM(FILE* model_file, float64_t PSEUDO); + HMM(FILE* model_file, float64_t PSEUDO); /// Constructor - Clone model h - CHMM(CHMM* h); + HMM(std::shared_ptr h); /// Destructor - Cleanup - virtual ~CHMM(); + virtual ~HMM(); /** learn distribution * @@ -503,7 +503,7 @@ class CHMM : public RandomMixin * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); virtual int32_t get_num_model_parameters() { return N*(N+M+2); } virtual float64_t get_log_model_parameter(int32_t num_param); virtual float64_t get_log_derivative(int32_t num_param, int32_t num_example); @@ -635,31 +635,31 @@ class CHMM : public RandomMixin /** uses baum-welch-algorithm to train a fully connected HMM. * @param train model from which the new model is estimated */ - void estimate_model_baum_welch(CHMM* train); - void estimate_model_baum_welch_trans(CHMM* train); + void estimate_model_baum_welch(std::shared_ptr train); + void estimate_model_baum_welch_trans(std::shared_ptr train); #ifdef USE_HMMPARALLEL_STRUCTURES void ab_buf_comp( float64_t* p_buf, float64_t* q_buf, float64_t* a_buf, float64_t* b_buf, int32_t dim) ; #else - void estimate_model_baum_welch_old(CHMM* train); + void estimate_model_baum_welch_old(std::shared_ptr train); #endif /** uses baum-welch-algorithm to train the defined transitions etc. * @param train model from which the new model is estimated */ - void estimate_model_baum_welch_defined(CHMM* train); + void estimate_model_baum_welch_defined(std::shared_ptr train); /** uses viterbi training to train a fully connected HMM * @param train model from which the new model is estimated */ - void estimate_model_viterbi(CHMM* train); + void estimate_model_viterbi(std::shared_ptr train); /** uses viterbi training to train the defined transitions etc. * @param train model from which the new model is estimated */ - void estimate_model_viterbi_defined(CHMM* train); + void estimate_model_viterbi_defined(std::shared_ptr train); //@} @@ -699,12 +699,12 @@ class CHMM : public RandomMixin /// the other state is the start state of the append_model. /// transition probability from state 1 to states 1 is 1 bool append_model( - CHMM* append_model, float64_t* cur_out, float64_t* app_out); + std::shared_ptr append_model, float64_t* cur_out, float64_t* app_out); /// appends the append_model to the current hmm, here /// no extra states are created. former q_i are multiplied by q_ji /// to give the a_ij from the current hmm to the append_model - bool append_model(CHMM* append_model); + bool append_model(std::shared_ptr append_model); /// set any model parameter with probability smaller than value to ZERO void chop(float64_t value); @@ -729,7 +729,7 @@ class CHMM : public RandomMixin void clear_model_defined(); /// copies the the modelparameters from l - void copy_model(CHMM* l); + void copy_model(std::shared_ptr l); /** invalidates all caches. * this function has to be called when direct changes to the model have been made. @@ -785,17 +785,16 @@ class CHMM : public RandomMixin * sets the observation pointer and initializes observation-dependent caches * if hmm is given, then the caches of the model hmm are used */ - void set_observations(CStringFeatures* obs, CHMM* hmm=NULL); + void set_observations(std::shared_ptr> obs, std::shared_ptr hmm=NULL); /** set new observations * only set the observation pointer and drop caches if there were any */ - void set_observation_nocache(CStringFeatures* obs); + void set_observation_nocache(std::shared_ptr> obs); /// return observation pointer - inline CStringFeatures* get_observations() + inline std::shared_ptr> get_observations() { - SG_REF(p_observations); return p_observations; } //@} @@ -1200,7 +1199,7 @@ class CHMM : public RandomMixin int32_t line; /// observation matrix - CStringFeatures* p_observations; + std::shared_ptr> p_observations; //train definition for HMM Model* model; @@ -1420,9 +1419,9 @@ inline float64_t model_derivative_q(T_STATES i, int32_t dimension) /// computes log dp(lambda)/d a_ij. inline float64_t model_derivative_a(T_STATES i, T_STATES j, int32_t dimension) { - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (int32_t t=0; tget_vector_length(dimension)-1; t++) - sum= CMath::logarithmic_sum(sum, forward(t, i, dimension) + backward(t+1, j, dimension) + get_b(j, p_observations->get_feature(dimension,t+1))); + sum= Math::logarithmic_sum(sum, forward(t, i, dimension) + backward(t+1, j, dimension) + get_b(j, p_observations->get_feature(dimension,t+1))); return sum; } @@ -1431,13 +1430,13 @@ inline float64_t model_derivative_a(T_STATES i, T_STATES j, int32_t dimension) /// computes log dp(lambda)/d b_ij. inline float64_t model_derivative_b(T_STATES i, uint16_t j, int32_t dimension) { - float64_t sum=-CMath::INFTY; + float64_t sum=-Math::INFTY; for (int32_t t=0; tget_vector_length(dimension); t++) { if (p_observations->get_feature(dimension,t)==j) - sum= CMath::logarithmic_sum(sum, forward(t,i,dimension)+backward(t,i,dimension)-get_b(i,p_observations->get_feature(dimension,t))); + sum= Math::logarithmic_sum(sum, forward(t,i,dimension)+backward(t,i,dimension)-get_b(i,p_observations->get_feature(dimension,t))); } - //if (sum==-CMath::INFTY) + //if (sum==-Math::INFTY) // SG_DEBUG("log derivative is -inf: dim={}, state={}, obs={}",dimension, i, j) return sum; } @@ -1560,7 +1559,7 @@ inline float64_t path_derivative_b(T_STATES i, uint16_t j, int32_t dimension) if (timeget_vector_length(dimension)) return BETA_CACHE(dimension).table[time*N+state]; else - return -CMath::INFTY; + return -Math::INFTY; } else return backward_comp(time, state, dimension) ; diff --git a/src/shogun/distributions/Histogram.cpp b/src/shogun/distributions/Histogram.cpp index c1bd157ee7b..510d349f736 100644 --- a/src/shogun/distributions/Histogram.cpp +++ b/src/shogun/distributions/Histogram.cpp @@ -12,25 +12,24 @@ using namespace shogun; -CHistogram::CHistogram() -: CDistribution() +Histogram::Histogram() +: Distribution() { init(); } -CHistogram::CHistogram(CStringFeatures *f) -: CDistribution() +Histogram::Histogram(std::shared_ptr> f) +: Distribution() { init(); features=f; - SG_REF(features); } -CHistogram::~CHistogram() +Histogram::~Histogram() { } -bool CHistogram::train(CFeatures* data) +bool Histogram::train(std::shared_ptr data) { int32_t vec; int32_t feat; @@ -53,19 +52,18 @@ bool CHistogram::train(CFeatures* data) for (i=0; i< (int32_t) (1<<16); i++) hist[i]=0; + auto sf = std::static_pointer_cast>(features); for (vec=0; vecget_num_vectors(); vec++) { int32_t len; bool free_vec; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(vec, len, free_vec); + uint16_t* vector=sf->get_feature_vector(vec, len, free_vec); for (feat=0; feat*) features)-> - free_feature_vector(vector, vec, free_vec); + sf->free_feature_vector(vector, vec, free_vec); } for (i=0; i< (int32_t) (1<<16); i++) @@ -74,7 +72,7 @@ bool CHistogram::train(CFeatures* data) return true; } -float64_t CHistogram::get_log_likelihood_example(int32_t num_example) +float64_t Histogram::get_log_likelihood_example(int32_t num_example) { ASSERT(features) ASSERT(features->get_feature_class()==C_STRING) @@ -84,22 +82,21 @@ float64_t CHistogram::get_log_likelihood_example(int32_t num_example) bool free_vec; float64_t loglik=0; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(num_example, len, free_vec); + auto sf = std::static_pointer_cast>(features); + uint16_t* vector=sf->get_feature_vector(num_example, len, free_vec); for (int32_t i=0; i*) features)-> - free_feature_vector(vector, num_example, free_vec); + sf->free_feature_vector(vector, num_example, free_vec); return loglik; } -float64_t CHistogram::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t Histogram::get_log_derivative(int32_t num_param, int32_t num_example) { - if (hist[num_param] < CMath::ALMOST_NEG_INFTY) - return -CMath::INFTY; + if (hist[num_param] < Math::ALMOST_NEG_INFTY) + return -Math::INFTY; else { ASSERT(features) @@ -110,8 +107,8 @@ float64_t CHistogram::get_log_derivative(int32_t num_param, int32_t num_example) bool free_vec; float64_t deriv=0; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(num_example, len, free_vec); + auto sf = std::static_pointer_cast>(features); + uint16_t* vector=sf->get_feature_vector(num_example, len, free_vec); int32_t num_occurences=0; @@ -123,24 +120,23 @@ float64_t CHistogram::get_log_derivative(int32_t num_param, int32_t num_example) num_occurences++; } - ((CStringFeatures*) features)-> - free_feature_vector(vector, num_example, free_vec); + sf->free_feature_vector(vector, num_example, free_vec); if (num_occurences>0) deriv += std::log((float64_t)num_occurences) - hist[num_param]; else - deriv=-CMath::INFTY; + deriv=-Math::INFTY; return deriv; } } -float64_t CHistogram::get_log_model_parameter(int32_t num_param) +float64_t Histogram::get_log_model_parameter(int32_t num_param) { return hist[num_param]; } -bool CHistogram::set_histogram(const SGVector histogram) +bool Histogram::set_histogram(const SGVector histogram) { ASSERT(histogram.vlen==get_num_model_parameters()) @@ -148,14 +144,14 @@ bool CHistogram::set_histogram(const SGVector histogram) return true; } -SGVector CHistogram::get_histogram() +SGVector Histogram::get_histogram() { return hist; } -void CHistogram::init() +void Histogram::init() { hist = SGVector(1 << 16); SG_ADD(&hist, "histogram", "Histogram array."); -} \ No newline at end of file +} diff --git a/src/shogun/distributions/Histogram.h b/src/shogun/distributions/Histogram.h index 00a5f9352df..81565b8c7d3 100644 --- a/src/shogun/distributions/Histogram.h +++ b/src/shogun/distributions/Histogram.h @@ -14,25 +14,25 @@ namespace shogun { - template class CStringFeatures; + template class StringFeatures; /** @brief Class Histogram computes a histogram over all 16bit unsigned * integers in the features. * * Values in histogram are absolute counts (logarithmic) */ -class CHistogram : public CDistribution +class Histogram : public Distribution { public: /** default constructor */ - CHistogram(); + Histogram(); /** constructor * * @param f histogram's features */ - CHistogram(CStringFeatures* f); - virtual ~CHistogram(); + Histogram(std::shared_ptr> f); + virtual ~Histogram(); /** learn distribution * @@ -42,7 +42,7 @@ class CHistogram : public CDistribution * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** get number of model parameters * diff --git a/src/shogun/distributions/KernelDensity.cpp b/src/shogun/distributions/KernelDensity.cpp index 9ce3e74279d..acd487777f5 100644 --- a/src/shogun/distributions/KernelDensity.cpp +++ b/src/shogun/distributions/KernelDensity.cpp @@ -35,8 +35,8 @@ using namespace shogun; -CKernelDensity::CKernelDensity(float64_t bandwidth, EKernelType kernel, EDistanceType dist, EEvaluationMode eval, int32_t leaf_size, float64_t atol, float64_t rtol) -: CDistribution() +KernelDensity::KernelDensity(float64_t bandwidth, EKernelType kernel, EDistanceType dist, EEvaluationMode eval, int32_t leaf_size, float64_t atol, float64_t rtol) +: Distribution() { init(); @@ -49,37 +49,36 @@ CKernelDensity::CKernelDensity(float64_t bandwidth, EKernelType kernel, EDistanc m_kernel_type=kernel; } -CKernelDensity::~CKernelDensity() +KernelDensity::~KernelDensity() { - SG_UNREF(tree); + } -bool CKernelDensity::train(CFeatures* data) +bool KernelDensity::train(std::shared_ptr data) { require(data,"Data not supplied"); - CDenseFeatures* dense_data=data->as>(); + auto dense_data=std::dynamic_pointer_cast>(data); - SG_UNREF(tree); switch (m_eval) { case EM_KDTREE_SINGLE: { - tree=new CKDTree(m_leaf_size,m_dist); + tree=std::make_shared(m_leaf_size,m_dist); break; } case EM_BALLTREE_SINGLE: { - tree=new CBallTree(m_leaf_size,m_dist); + tree=std::make_shared(m_leaf_size,m_dist); break; } case EM_KDTREE_DUAL: { - tree=new CKDTree(m_leaf_size,m_dist); + tree=std::make_shared(m_leaf_size,m_dist); break; } case EM_BALLTREE_DUAL: { - tree=new CBallTree(m_leaf_size,m_dist); + tree=std::make_shared(m_leaf_size,m_dist); break; } default: @@ -92,63 +91,63 @@ bool CKernelDensity::train(CFeatures* data) return true; } -SGVector CKernelDensity::get_log_density(CDenseFeatures* test, int32_t leaf_size) +SGVector KernelDensity::get_log_density(std::shared_ptr> test, int32_t leaf_size) { require(test,"data not supplied"); if ((m_eval==EM_KDTREE_SINGLE) || (m_eval==EM_BALLTREE_SINGLE)) return tree->log_kernel_density(test->get_feature_matrix(),m_kernel_type,m_bandwidth,m_atol,m_rtol); - CNbodyTree* query_tree=NULL; + std::shared_ptr query_tree=NULL; if (m_eval==EM_KDTREE_DUAL) - query_tree=new CKDTree(leaf_size,m_dist); + query_tree=std::make_shared(leaf_size,m_dist); else if (m_eval==EM_BALLTREE_DUAL) - query_tree=new CBallTree(leaf_size,m_dist); + query_tree=std::make_shared(leaf_size,m_dist); else error("Evaluation mode not identified"); query_tree->build_tree(test); - CBinaryTreeMachineNode* qroot=NULL; - CTreeMachineNode* root=query_tree->get_root(); + std::shared_ptr> qroot=NULL; + auto root=query_tree->get_root(); if (root) - qroot=dynamic_cast*>(root); + qroot=std::dynamic_pointer_cast>(root); else error("Query tree root not found!"); SGVector qid=query_tree->get_rearranged_vector_ids(); SGVector ret=tree->log_kernel_density_dual(test->get_feature_matrix(),qid,qroot,m_kernel_type,m_bandwidth,m_atol,m_rtol); - SG_UNREF(root); - SG_UNREF(query_tree); + + return ret; } -int32_t CKernelDensity::get_num_model_parameters() +int32_t KernelDensity::get_num_model_parameters() { not_implemented(SOURCE_LOCATION);; return 0; } -float64_t CKernelDensity::get_log_model_parameter(int32_t num_param) +float64_t KernelDensity::get_log_model_parameter(int32_t num_param) { not_implemented(SOURCE_LOCATION);; return 0; } -float64_t CKernelDensity::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t KernelDensity::get_log_derivative(int32_t num_param, int32_t num_example) { not_implemented(SOURCE_LOCATION);; return 0; } -float64_t CKernelDensity::get_log_likelihood_example(int32_t num_example) +float64_t KernelDensity::get_log_likelihood_example(int32_t num_example) { not_implemented(SOURCE_LOCATION);; return 0; } -void CKernelDensity::init() +void KernelDensity::init() { m_bandwidth=1.0; m_eval=EM_KDTREE_SINGLE; @@ -163,5 +162,5 @@ void CKernelDensity::init() SG_ADD(&m_leaf_size,"m_leaf_size","leaf size"); SG_ADD(&m_atol,"m_atol","absolute tolerance"); SG_ADD(&m_rtol,"m_rtol","relative tolerance"); - SG_ADD((CSGObject**) &tree,"tree","tree"); -} \ No newline at end of file + SG_ADD((std::shared_ptr*) &tree,"tree","tree"); +} diff --git a/src/shogun/distributions/KernelDensity.h b/src/shogun/distributions/KernelDensity.h index 092e008451e..ddca9d25a8b 100644 --- a/src/shogun/distributions/KernelDensity.h +++ b/src/shogun/distributions/KernelDensity.h @@ -39,7 +39,7 @@ namespace shogun { -/** Evaluation method of KDE, see CKernelDensity */ +/** Evaluation method of KDE, see KernelDensity */ enum EEvaluationMode { EM_KDTREE_SINGLE, @@ -58,7 +58,7 @@ enum EEvaluationMode * faster than ball trees at lower dimensions. In case of high dimensional data, ball tree tends to out-perform KD-tree. * By default, the class used is Ball tree. */ -class CKernelDensity : public CDistribution +class KernelDensity : public Distribution { public : /** Constructor @@ -71,10 +71,10 @@ public : * @param atol absolute tolerance * @param rtol relative tolerance */ - CKernelDensity(float64_t bandwidth=1.0, EKernelType kernel_type=K_GAUSSIAN, EDistanceType dist=D_EUCLIDEAN, EEvaluationMode eval=EM_BALLTREE_SINGLE, int32_t leaf_size=1, float64_t atol=0, float64_t rtol=0); + KernelDensity(float64_t bandwidth=1.0, EKernelType kernel_type=K_GAUSSIAN, EDistanceType dist=D_EUCLIDEAN, EEvaluationMode eval=EM_BALLTREE_SINGLE, int32_t leaf_size=1, float64_t atol=0, float64_t rtol=0); /** destructor */ - ~CKernelDensity(); + ~KernelDensity(); /** return class name * @@ -87,7 +87,7 @@ public : * @param data data points to be used for density estimation * @return true */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** compute kde for given test points * @@ -95,7 +95,7 @@ public : * @param leaf_size leaf size of query tree (ignored in case of single tree evaluation mode) * @return log of estimated kernel density velues at given test points */ - SGVector get_log_density(CDenseFeatures* test, int32_t leaf_size=1); + SGVector get_log_density(std::shared_ptr> test, int32_t leaf_size=1); /** return number of model parameters * NOT IMPLEMENTED @@ -142,7 +142,7 @@ public : { case K_GAUSSIAN: { - return -0.5 * dim * std::log(2 * CMath::PI) - + return -0.5 * dim * std::log(2 * Math::PI) - dim * std::log(width); break; } @@ -203,7 +203,7 @@ private : EDistanceType m_dist; /** Tree */ - CNbodyTree* tree; + std::shared_ptr tree; }; } /* shogun */ diff --git a/src/shogun/distributions/LinearHMM.cpp b/src/shogun/distributions/LinearHMM.cpp index 582b29ca4d8..d87ebfeb0fa 100644 --- a/src/shogun/distributions/LinearHMM.cpp +++ b/src/shogun/distributions/LinearHMM.cpp @@ -14,33 +14,33 @@ using namespace shogun; -CLinearHMM::CLinearHMM() : CDistribution() +LinearHMM::LinearHMM() : Distribution() { init(); } -CLinearHMM::CLinearHMM(CStringFeatures* f) -: CDistribution() +LinearHMM::LinearHMM(std::shared_ptr> f) +: Distribution() { init(); set_features(f); } -void CLinearHMM::set_features(CFeatures* f) +void LinearHMM::set_features(std::shared_ptr f) { - auto* string_feats = f->as>(); + auto string_feats = f->as>(); require(string_feats, "LinearHMM works with string features."); - CDistribution::set_features(f); + Distribution::set_features(f); sequence_length = string_feats->get_vector_length(0); num_symbols = (int32_t) string_feats->get_num_symbols(); num_params = sequence_length*num_symbols; } -CLinearHMM::CLinearHMM(int32_t p_num_features, int32_t p_num_symbols) -: CDistribution() +LinearHMM::LinearHMM(int32_t p_num_features, int32_t p_num_symbols) +: Distribution() { init(); @@ -49,11 +49,11 @@ CLinearHMM::CLinearHMM(int32_t p_num_features, int32_t p_num_symbols) num_params = sequence_length*num_symbols; } -CLinearHMM::~CLinearHMM() +LinearHMM::~LinearHMM() { } -bool CLinearHMM::train(CFeatures* data) +bool LinearHMM::train(std::shared_ptr data) { if (data) { @@ -69,20 +69,19 @@ bool CLinearHMM::train(CFeatures* data) int32_t vec; int32_t i; + auto sf = std::static_pointer_cast>(features); for (vec=0; vecget_num_vectors(); vec++) { int32_t len; bool free_vec; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(vec, len, free_vec); + uint16_t* vector=sf->get_feature_vector(vec, len, free_vec); //just count the symbols per position -> transition_probsogram for (int32_t feat=0; feat*) features)-> - free_feature_vector(vector, vec, free_vec); + sf->free_feature_vector(vector, vec, free_vec); } //trade memory for speed @@ -95,19 +94,16 @@ bool CLinearHMM::train(CFeatures* data) { float64_t sum=0; int32_t offs=i*num_symbols+ - ((CStringFeatures *) features)-> - get_masked_symbols((uint16_t)j,(uint8_t) 254); + sf->get_masked_symbols((uint16_t)j,(uint8_t) 254); int32_t original_num_symbols=(int32_t) - ((CStringFeatures *) features)-> - get_original_num_symbols(); + sf->get_original_num_symbols(); for (int32_t k=0; k *) features)-> - get_original_num_symbols()*pseudo_count); + (sum+sf->get_original_num_symbols()*pseudo_count); log_transition_probs[i*num_symbols+j]= log(transition_probs[i*num_symbols+j]); } @@ -116,25 +112,23 @@ bool CLinearHMM::train(CFeatures* data) return true; } -bool CLinearHMM::train( +bool LinearHMM::train( const int32_t* indizes, int32_t num_indizes, float64_t pseudo) { SGMatrix int_transition_probs(num_symbols, sequence_length); int32_t vec; int32_t i; + auto sf = std::static_pointer_cast>(features); for (vec=0; vec=0 && - indizes[vec]<((CStringFeatures*) features)-> - get_num_vectors()); - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(indizes[vec], len, free_vec); - ((CStringFeatures*) features)-> - free_feature_vector(vector, indizes[vec], free_vec); + indizes[vec]get_num_vectors()); + uint16_t* vector=sf->get_feature_vector(indizes[vec], len, free_vec); + sf->free_feature_vector(vector, indizes[vec], free_vec); //just count the symbols per position -> transition_probsogram // @@ -152,19 +146,16 @@ bool CLinearHMM::train( { float64_t sum=0; int32_t original_num_symbols=(int32_t) - ((CStringFeatures *) features)-> - get_original_num_symbols(); + sf->get_original_num_symbols(); for (int32_t k=0; k*) features)-> - get_masked_symbols((uint16_t)j,(uint8_t) 254)+k]; + sf->get_masked_symbols((uint16_t)j,(uint8_t) 254)+k]; } transition_probs[i*num_symbols+j]= (int_transition_probs[i*num_symbols+j]+pseudo)/ - (sum+((CStringFeatures*) features)-> - get_original_num_symbols()*pseudo); + (sum+sf->get_original_num_symbols()*pseudo); log_transition_probs[i*num_symbols+j]= log(transition_probs[i*num_symbols+j]); } @@ -173,7 +164,7 @@ bool CLinearHMM::train( return true; } -float64_t CLinearHMM::get_log_likelihood_example(uint16_t* vector, int32_t len) +float64_t LinearHMM::get_log_likelihood_example(uint16_t* vector, int32_t len) { float64_t result=log_transition_probs[vector[0]]; @@ -183,21 +174,20 @@ float64_t CLinearHMM::get_log_likelihood_example(uint16_t* vector, int32_t len) return result; } -float64_t CLinearHMM::get_log_likelihood_example(int32_t num_example) +float64_t LinearHMM::get_log_likelihood_example(int32_t num_example) { int32_t len; bool free_vec; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(num_example, len, free_vec); + auto sf = std::static_pointer_cast>(features); + uint16_t* vector=sf->get_feature_vector(num_example, len, free_vec); float64_t result=get_log_likelihood_example(vector, len); - ((CStringFeatures*) features)-> - free_feature_vector(vector, num_example, free_vec); + sf->free_feature_vector(vector, num_example, free_vec); return result; } -float64_t CLinearHMM::get_likelihood_example(uint16_t* vector, int32_t len) +float64_t LinearHMM::get_likelihood_example(uint16_t* vector, int32_t len) { float64_t result=transition_probs[vector[0]]; @@ -207,27 +197,26 @@ float64_t CLinearHMM::get_likelihood_example(uint16_t* vector, int32_t len) return result; } -float64_t CLinearHMM::get_likelihood_example(int32_t num_example) +float64_t LinearHMM::get_likelihood_example(int32_t num_example) { int32_t len; bool free_vec; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(num_example, len, free_vec); + auto sf = std::static_pointer_cast>(features); + uint16_t* vector=sf->get_feature_vector(num_example, len, free_vec); float64_t result=get_likelihood_example(vector, len); - ((CStringFeatures*) features)-> - free_feature_vector(vector, num_example, free_vec); + sf->free_feature_vector(vector, num_example, free_vec); return result; } -float64_t CLinearHMM::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t LinearHMM::get_log_derivative(int32_t num_param, int32_t num_example) { int32_t len; bool free_vec; - uint16_t* vector=((CStringFeatures*) features)-> - get_feature_vector(num_example, len, free_vec); + auto sf = std::static_pointer_cast>(features); + uint16_t* vector=sf->get_feature_vector(num_example, len, free_vec); float64_t result=0; int32_t position=num_param/num_symbols; ASSERT(position>=0 && position*) features)-> - free_feature_vector(vector, num_example, free_vec); + sf->free_feature_vector(vector, num_example, free_vec); return result; } -SGMatrix CLinearHMM::get_transition_probs() +SGMatrix LinearHMM::get_transition_probs() { return transition_probs; } -bool CLinearHMM::set_transition_probs(const SGMatrix& probs) +bool LinearHMM::set_transition_probs(const SGMatrix& probs) { require( probs.num_rows == num_symbols && probs.num_cols == sequence_length, @@ -267,12 +255,12 @@ bool CLinearHMM::set_transition_probs(const SGMatrix& probs) return true; } -SGMatrix CLinearHMM::get_log_transition_probs() +SGMatrix LinearHMM::get_log_transition_probs() { return log_transition_probs; } -bool CLinearHMM::set_log_transition_probs(const SGMatrix& probs) +bool LinearHMM::set_log_transition_probs(const SGMatrix& probs) { require( probs.num_rows == num_symbols && probs.num_cols == sequence_length, @@ -293,14 +281,14 @@ bool CLinearHMM::set_log_transition_probs(const SGMatrix& probs) return true; } -void CLinearHMM::load_serializable_post() noexcept(false) +void LinearHMM::load_serializable_post() noexcept(false) { - CSGObject::load_serializable_post(); + SGObject::load_serializable_post(); num_params = sequence_length*num_symbols; } -void CLinearHMM::init() +void LinearHMM::init() { sequence_length = 0; num_symbols = 0; diff --git a/src/shogun/distributions/LinearHMM.h b/src/shogun/distributions/LinearHMM.h index e08caf979b5..9a40e890b52 100644 --- a/src/shogun/distributions/LinearHMM.h +++ b/src/shogun/distributions/LinearHMM.h @@ -33,26 +33,26 @@ namespace shogun * and Nucleic Acids, 1998 * * */ -class CLinearHMM : public CDistribution +class LinearHMM : public Distribution { public: /** default constructor */ - CLinearHMM(); + LinearHMM(); /** constructor * * @param f features to use */ - CLinearHMM(CStringFeatures* f); + LinearHMM(std::shared_ptr> f); /** constructor * * @param p_num_features number of features * @param p_num_symbols number of symbols in features */ - CLinearHMM(int32_t p_num_features, int32_t p_num_symbols); + LinearHMM(int32_t p_num_features, int32_t p_num_symbols); - virtual ~CLinearHMM(); + virtual ~LinearHMM(); /** estimate LinearHMM distribution * @@ -62,7 +62,7 @@ class CLinearHMM : public CDistribution * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** alternative train distribution * @@ -215,7 +215,7 @@ class CLinearHMM : public CDistribution * * @param f new feature vectors */ - virtual void set_features(CFeatures* f); + virtual void set_features(std::shared_ptr f); protected: virtual void load_serializable_post() noexcept(false); diff --git a/src/shogun/distributions/MixModelData.h b/src/shogun/distributions/MixModelData.h index 343d9c07959..98de91e26f1 100644 --- a/src/shogun/distributions/MixModelData.h +++ b/src/shogun/distributions/MixModelData.h @@ -47,7 +47,7 @@ struct MixModelData /** allocated belongingness matrix */ SGMatrix alpha; /** components of mixture */ - CDynamicObjectArray* components; + std::shared_ptr components; /** weights of mixture */ SGVector weights; diff --git a/src/shogun/distributions/MixtureModel.cpp b/src/shogun/distributions/MixtureModel.cpp index 5ce66bf6702..38a95494c3c 100644 --- a/src/shogun/distributions/MixtureModel.cpp +++ b/src/shogun/distributions/MixtureModel.cpp @@ -36,25 +36,25 @@ using namespace shogun; -CMixtureModel::CMixtureModel() +MixtureModel::MixtureModel() { init(); } -CMixtureModel::CMixtureModel(CDynamicObjectArray* components, SGVector weights) +MixtureModel::MixtureModel(std::shared_ptr components, SGVector weights) { init(); m_components=components; - SG_REF(components); + m_weights=weights; } -CMixtureModel::~CMixtureModel() +MixtureModel::~MixtureModel() { - SG_UNREF(m_components); + } -bool CMixtureModel::train(CFeatures* data) +bool MixtureModel::train(std::shared_ptr data) { require(m_components->get_num_elements()>0,"mixture componenents not specified"); require(m_components->get_num_elements()==m_weights.vlen,"number of weights ({}) does not" @@ -75,18 +75,18 @@ bool CMixtureModel::train(CFeatures* data) // set training points in all components of the mixture for (int32_t i=0;iget_num_elements();i++) { - CDistribution* comp=m_components->get_element(i)->as(); + auto comp=m_components->get_element(i); comp->set_features(features); - SG_UNREF(comp) + } - CDotFeatures* dotdata=dynamic_cast(features); + auto dotdata=std::dynamic_pointer_cast(features); require(dotdata,"dynamic cast from CFeatures to CDotFeatures returned NULL"); int32_t num_vectors=dotdata->get_num_vectors(); // set data for EM - CEMMixtureModel* em=new CEMMixtureModel(); + auto em=std::make_shared(); em->data.alpha=SGMatrix(num_vectors,m_components->get_num_elements()); em->data.components=m_components; em->data.weights=m_weights; @@ -96,11 +96,11 @@ bool CMixtureModel::train(CFeatures* data) if (!is_converged) io::warn("max iterations reached. No convergence yet!"); - SG_UNREF(em) + return true; } -float64_t CMixtureModel::get_log_model_parameter(int32_t num_param) +float64_t MixtureModel::get_log_model_parameter(int32_t num_param) { require(num_param==1,"number of parameters in mixture model is 1" " (i.e. number of components). num_components should be 1. {} supplied",num_param); @@ -108,13 +108,13 @@ float64_t CMixtureModel::get_log_model_parameter(int32_t num_param) return std::log(static_cast(get_num_components())); } -float64_t CMixtureModel::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t MixtureModel::get_log_derivative(int32_t num_param, int32_t num_example) { not_implemented(SOURCE_LOCATION); return 0; } -float64_t CMixtureModel::get_log_likelihood_example(int32_t num_example) +float64_t MixtureModel::get_log_likelihood_example(int32_t num_example) { require(features,"features not set"); require(features->get_feature_class() == C_DENSE,"Dense features required"); @@ -123,96 +123,92 @@ float64_t CMixtureModel::get_log_likelihood_example(int32_t num_example) SGVector log_likelihood_component(m_components->get_num_elements()); for (int32_t i=0;iget_num_elements();i++) { - CDistribution* ith_comp=m_components->get_element(i)->as(); + auto ith_comp=m_components->get_element(i); log_likelihood_component[i] = ith_comp->get_log_likelihood_example(num_example) + std::log(m_weights[i]); - SG_UNREF(ith_comp); + } - return CMath::log_sum_exp(log_likelihood_component); + return Math::log_sum_exp(log_likelihood_component); } -SGVector CMixtureModel::get_weights() const +SGVector MixtureModel::get_weights() const { return m_weights; } -void CMixtureModel::set_weights(SGVector weights) +void MixtureModel::set_weights(SGVector weights) { m_weights=weights; } -CDynamicObjectArray* CMixtureModel::get_components() const +std::shared_ptr MixtureModel::get_components() const { - SG_REF(m_components); + return m_components; } -void CMixtureModel::set_components(CDynamicObjectArray* components) +void MixtureModel::set_components(std::shared_ptr components) { - if (m_components!=NULL) - SG_UNREF(m_components) - m_components=components; - SG_REF(m_components); } -index_t CMixtureModel::get_num_components() const +index_t MixtureModel::get_num_components() const { return m_components->get_num_elements(); } -CDistribution* CMixtureModel::get_component(index_t index) const +std::shared_ptr MixtureModel::get_component(index_t index) const { require(indexget_element(index)->as(); + return m_components->get_element(index); } -void CMixtureModel::set_max_iters(int32_t max_iters) +void MixtureModel::set_max_iters(int32_t max_iters) { m_max_iters=max_iters; } -int32_t CMixtureModel::get_max_iters() const +int32_t MixtureModel::get_max_iters() const { return m_max_iters; } -void CMixtureModel::set_convergence_tolerance(float64_t conv_tol) +void MixtureModel::set_convergence_tolerance(float64_t conv_tol) { m_conv_tol=conv_tol; } -float64_t CMixtureModel::get_convergence_tolerance() const +float64_t MixtureModel::get_convergence_tolerance() const { return m_conv_tol; } -SGVector CMixtureModel::sample() +SGVector MixtureModel::sample() { // TBD not_implemented(SOURCE_LOCATION);; return SGVector(); } -SGVector CMixtureModel::cluster(SGVector point) +SGVector MixtureModel::cluster(SGVector point) { // TBD not_implemented(SOURCE_LOCATION);; return point; } -void CMixtureModel::init() +void MixtureModel::init() { m_components=NULL; m_weights=SGVector(); m_conv_tol=1e-8; m_max_iters=1000; - SG_ADD((CSGObject**)&m_components,"m_components","components of mixture"); + SG_ADD((std::shared_ptr*)&m_components,"m_components","components of mixture"); SG_ADD(&m_weights,"m_weights","weights of components"); SG_ADD(&m_conv_tol,"m_conv_tol","convergence tolerance"); SG_ADD(&m_max_iters,"m_max_iters","max number of iterations"); diff --git a/src/shogun/distributions/MixtureModel.h b/src/shogun/distributions/MixtureModel.h index 525deaeedab..601dfbf2432 100644 --- a/src/shogun/distributions/MixtureModel.h +++ b/src/shogun/distributions/MixtureModel.h @@ -41,11 +41,11 @@ namespace shogun * * \f$ f_X(x) = \Sigma_{m=1}^{K} w_m.g_m(x,\lambda_m)\f$ */ -class CMixtureModel : public CDistribution +class MixtureModel : public Distribution { public: /* default constructor */ - CMixtureModel(); + MixtureModel(); /** constructor * Note: Automatic initialization of parameters of components is not possible yet @@ -53,10 +53,10 @@ class CMixtureModel : public CDistribution * @param components individual distributions forming the mixture (parameters must be initialized) * @param weights initial \f$w_m\f$ ie. weights of individual distributions */ - CMixtureModel(CDynamicObjectArray* components, SGVector weights); + MixtureModel(std::shared_ptr components, SGVector weights); /* destructor */ - ~CMixtureModel(); + ~MixtureModel(); /** @return object name */ virtual const char* get_name() const { return "MixtureModel"; } @@ -66,7 +66,7 @@ class CMixtureModel : public CDistribution * @param data training data * @return whether training was successful */ - bool train(CFeatures* data=NULL); + bool train(std::shared_ptr data=NULL); /** get number of parameters in model * @@ -111,13 +111,13 @@ class CMixtureModel : public CDistribution * * @return components */ - CDynamicObjectArray* get_components() const; + std::shared_ptr get_components() const; /** set components * * @param components mixture components */ - void set_components(CDynamicObjectArray* components); + void set_components(std::shared_ptr components); /** get number of components * @@ -130,7 +130,7 @@ class CMixtureModel : public CDistribution * @param index index of component * @return component at index */ - CDistribution* get_component(index_t index) const; + std::shared_ptr get_component(index_t index) const; /** set max iterations in EM * @@ -175,7 +175,7 @@ class CMixtureModel : public CDistribution private: /** array of components */ - CDynamicObjectArray* m_components; + std::shared_ptr m_components; /** weights */ SGVector m_weights; diff --git a/src/shogun/distributions/PositionalPWM.cpp b/src/shogun/distributions/PositionalPWM.cpp index 0b4567e776a..5d80a971f48 100644 --- a/src/shogun/distributions/PositionalPWM.cpp +++ b/src/shogun/distributions/PositionalPWM.cpp @@ -11,7 +11,7 @@ using namespace shogun; -CPositionalPWM::CPositionalPWM() : CDistribution(), +PositionalPWM::PositionalPWM() : Distribution(), m_sigma(0), m_mean(0) { m_pwm = SGMatrix(); @@ -21,22 +21,22 @@ CPositionalPWM::CPositionalPWM() : CDistribution(), register_params(); } -CPositionalPWM::~CPositionalPWM() +PositionalPWM::~PositionalPWM() { } -bool CPositionalPWM::train(CFeatures* data) +bool PositionalPWM::train(std::shared_ptr data) { not_implemented(SOURCE_LOCATION); return true; } -int32_t CPositionalPWM::get_num_model_parameters() +int32_t PositionalPWM::get_num_model_parameters() { return m_pwm.num_rows*m_pwm.num_cols+2; } -float64_t CPositionalPWM::get_log_model_parameter(int32_t num_param) +float64_t PositionalPWM::get_log_model_parameter(int32_t num_param) { ASSERT(num_param>0 && num_param<=m_pwm.num_rows*m_pwm.num_cols+2) @@ -50,19 +50,19 @@ float64_t CPositionalPWM::get_log_model_parameter(int32_t num_param) return std::log(m_mean); } -float64_t CPositionalPWM::get_log_derivative(int32_t num_param, int32_t num_example) +float64_t PositionalPWM::get_log_derivative(int32_t num_param, int32_t num_example) { not_implemented(SOURCE_LOCATION); return 0; } -float64_t CPositionalPWM::get_log_likelihood_example(int32_t num_example) +float64_t PositionalPWM::get_log_likelihood_example(int32_t num_example) { ASSERT(features) ASSERT(features->get_feature_class() == C_STRING) ASSERT(features->get_feature_type()==F_BYTE) - CStringFeatures* strs=(CStringFeatures*) features; + auto strs=std::dynamic_pointer_cast>(features); float64_t lik=0; int32_t len=0; @@ -80,11 +80,11 @@ float64_t CPositionalPWM::get_log_likelihood_example(int32_t num_example) return lik; } -float64_t CPositionalPWM::get_log_likelihood_window(uint8_t* window, int32_t len, float64_t pos) +float64_t PositionalPWM::get_log_likelihood_window(uint8_t* window, int32_t len, float64_t pos) { ASSERT(m_pwm.num_cols == len) float64_t score = std::log(1 / (m_sigma * std::sqrt(2 * M_PI))) - - CMath::sq(pos - m_mean) / (2 * CMath::sq(m_sigma)); + Math::sq(pos - m_mean) / (2 * Math::sq(m_sigma)); for (int32_t i=0; i0 && m_pwm.num_cols>0) - int32_t m_w_rows = CMath::pow(m_pwm.num_rows, m_pwm.num_cols); + int32_t m_w_rows = Math::pow(m_pwm.num_rows, m_pwm.num_cols); int32_t m_w_cols = num_pos; m_w = SGMatrix(m_w_cols,m_w_rows); @@ -122,7 +122,7 @@ void CPositionalPWM::compute_w(int32_t num_pos) } } -void CPositionalPWM::register_params() +void PositionalPWM::register_params() { SG_ADD(&m_poim, "poim", "POIM Scoring Matrix"); SG_ADD(&m_w, "w", "Scoring Matrix"); @@ -131,32 +131,32 @@ void CPositionalPWM::register_params() SG_ADD(&m_mean, "mean", "Mean."); } -void CPositionalPWM::compute_scoring(int32_t max_degree) +void PositionalPWM::compute_scoring(int32_t max_degree) { int32_t num_feat=m_w.num_cols; int32_t num_sym=0; int32_t order=m_pwm.num_rows; int32_t num_words=m_pwm.num_cols; - CAlphabet* alpha=new CAlphabet(DNA); - CStringFeatures* str= new CStringFeatures(alpha); + auto alpha=std::make_shared(DNA); + auto str= std::make_shared>(alpha); int32_t num_bits=alpha->get_num_bits(); str->compute_symbol_mask_table(num_bits); for (int32_t i=0; i(num_feat*num_sym); memset(m_poim.vector,0, size_t(num_feat)*size_t(num_sym)); uint32_t kmer_mask=0; - uint32_t words=CMath::pow((int32_t) num_words,(int32_t) order); + uint32_t words=Math::pow((int32_t) num_words,(int32_t) order); int32_t offset=0; for (int32_t o=0; oget_masked_symbols(0xffff, 1); @@ -188,7 +188,7 @@ void CPositionalPWM::compute_scoring(int32_t max_degree) } float64_t marginalizer= - 1.0/CMath::pow((int32_t) num_words,(int32_t) m_sym); + 1.0/Math::pow((int32_t) num_words,(int32_t) m_sym); for (uint32_t i=0; i CPositionalPWM::get_scoring(int32_t d) +SGMatrix PositionalPWM::get_scoring(int32_t d) { int32_t offs=0; for (int32_t i=0; i data=NULL); /** get number of parameters in model * diff --git a/src/shogun/distributions/classical/GaussianDistribution.cpp b/src/shogun/distributions/classical/GaussianDistribution.cpp index 915eaf29b64..dce639b0a24 100644 --- a/src/shogun/distributions/classical/GaussianDistribution.cpp +++ b/src/shogun/distributions/classical/GaussianDistribution.cpp @@ -15,14 +15,14 @@ using namespace shogun; using namespace Eigen; -CGaussianDistribution::CGaussianDistribution() : RandomMixin() +GaussianDistribution::GaussianDistribution() : RandomMixin() { init(); } -CGaussianDistribution::CGaussianDistribution(SGVector mean, +GaussianDistribution::GaussianDistribution(SGVector mean, SGMatrix cov, bool cov_is_factor) : - RandomMixin(mean.vlen) + RandomMixin(mean.vlen) { require(cov.num_rows==cov.num_cols, "Covariance must be square but is " "{}x{}", cov.num_rows, cov.num_cols); @@ -60,12 +60,12 @@ CGaussianDistribution::CGaussianDistribution(SGVector mean, m_L=cov; } -CGaussianDistribution::~CGaussianDistribution() +GaussianDistribution::~GaussianDistribution() { } -SGMatrix CGaussianDistribution::sample(int32_t num_samples, +SGMatrix GaussianDistribution::sample(int32_t num_samples, SGMatrix pre_samples) const { require(num_samples>0, "Number of samples ({}) must be positive", @@ -105,7 +105,7 @@ SGMatrix CGaussianDistribution::sample(int32_t num_samples, return samples; } -SGVector CGaussianDistribution::log_pdf_multiple(SGMatrix samples) const +SGVector GaussianDistribution::log_pdf_multiple(SGMatrix samples) const { require(samples.num_cols>0, "Number of samples must be positive, but is {}", samples.num_cols); @@ -115,7 +115,7 @@ SGVector CGaussianDistribution::log_pdf_multiple(SGMatrix /* for easier to read code */ index_t num_samples=samples.num_cols; - float64_t const_part = -0.5 * m_dimension * std::log(2 * CMath::PI); + float64_t const_part = -0.5 * m_dimension * std::log(2 * Math::PI); /* determinant is product of diagonal elements of triangular matrix */ float64_t log_det_part=0; @@ -163,7 +163,7 @@ SGVector CGaussianDistribution::log_pdf_multiple(SGMatrix } -void CGaussianDistribution::init() +void GaussianDistribution::init() { SG_ADD(&m_mean, "mean", "Mean of the Gaussian."); SG_ADD(&m_L, "L", "Lower factor of covariance matrix, " diff --git a/src/shogun/distributions/classical/GaussianDistribution.h b/src/shogun/distributions/classical/GaussianDistribution.h index 8e508e8b9ff..b02c83dbc44 100644 --- a/src/shogun/distributions/classical/GaussianDistribution.h +++ b/src/shogun/distributions/classical/GaussianDistribution.h @@ -58,11 +58,11 @@ namespace shogun * \f$\Sigma=LL^T\f$. */ -class CGaussianDistribution: public RandomMixin +class GaussianDistribution: public RandomMixin { public: /** Default constructor */ - CGaussianDistribution(); + GaussianDistribution(); /** Constructor for which takes Gaussian mean and its covariance matrix. * It is also possible to pass a precomputed matrix factor of the specified @@ -73,11 +73,11 @@ class CGaussianDistribution: public RandomMixin * @param cov_is_factor whether cov is a factor of the covariance or not * (default is false). If false, the factorization is explicitly computed */ - CGaussianDistribution(SGVector mean, SGMatrix cov, + GaussianDistribution(SGVector mean, SGMatrix cov, bool cov_is_factor=false); /** Destructor */ - virtual ~CGaussianDistribution(); + virtual ~GaussianDistribution(); /** Samples from the distribution multiple times * @@ -125,8 +125,8 @@ class CGaussianDistribution: public RandomMixin static float64_t univariate_log_pdf(float64_t sample, float64_t mu = 0.0, float64_t sigma2 = 1.0) { require(sigma2 > 0, "Variance should be positive"); - return -0.5 * (CMath::pow(sample - mu, 2) / sigma2 + - std::log(2.0 * CMath::PI) + std::log(sigma2)); + return -0.5 * (Math::pow(sample - mu, 2) / sigma2 + + std::log(2.0 * Math::PI) + std::log(sigma2)); } private: diff --git a/src/shogun/distributions/classical/ProbabilityDistribution.cpp b/src/shogun/distributions/classical/ProbabilityDistribution.cpp index e0c05575481..9630c31cd5d 100644 --- a/src/shogun/distributions/classical/ProbabilityDistribution.cpp +++ b/src/shogun/distributions/classical/ProbabilityDistribution.cpp @@ -11,13 +11,13 @@ using namespace shogun; -CProbabilityDistribution::CProbabilityDistribution() : CSGObject() +ProbabilityDistribution::ProbabilityDistribution() : SGObject() { init(); } -CProbabilityDistribution::CProbabilityDistribution(int32_t dimension) : - CSGObject() +ProbabilityDistribution::ProbabilityDistribution(int32_t dimension) : + SGObject() { init(); @@ -28,19 +28,19 @@ CProbabilityDistribution::CProbabilityDistribution(int32_t dimension) : } -CProbabilityDistribution::~CProbabilityDistribution() +ProbabilityDistribution::~ProbabilityDistribution() { } -SGMatrix CProbabilityDistribution::sample(int32_t num_samples, +SGMatrix ProbabilityDistribution::sample(int32_t num_samples, SGMatrix pre_samples) const { error("Not implemented in sub-class"); return SGMatrix(); } -SGVector CProbabilityDistribution::sample() const +SGVector ProbabilityDistribution::sample() const { SGMatrix s=sample(1); SGVector result(m_dimension); @@ -48,14 +48,14 @@ SGVector CProbabilityDistribution::sample() const return result; } -SGVector CProbabilityDistribution::log_pdf_multiple( +SGVector ProbabilityDistribution::log_pdf_multiple( SGMatrix samples) const { error("Not implemented in sub-class"); return SGVector(); } -float64_t CProbabilityDistribution::log_pdf(SGVector sample_vec) const +float64_t ProbabilityDistribution::log_pdf(SGVector sample_vec) const { require(sample_vec.vlen==m_dimension, "Sample dimension ({}) does not " "match dimension of distribution ({})", sample_vec.vlen, @@ -66,7 +66,7 @@ float64_t CProbabilityDistribution::log_pdf(SGVector sample_vec) cons return log_pdf_multiple(s)[0]; } -void CProbabilityDistribution::init() +void ProbabilityDistribution::init() { m_dimension=0; diff --git a/src/shogun/distributions/classical/ProbabilityDistribution.h b/src/shogun/distributions/classical/ProbabilityDistribution.h index 114b09dac93..8d86231cdc5 100644 --- a/src/shogun/distributions/classical/ProbabilityDistribution.h +++ b/src/shogun/distributions/classical/ProbabilityDistribution.h @@ -22,24 +22,24 @@ template class SGVector; * over the real numbers (64bit) for which various statistics can be computed * and which can be sampled. */ -class CProbabilityDistribution: public CSGObject +class ProbabilityDistribution: public SGObject { public: /** Default constructor */ - CProbabilityDistribution(); + ProbabilityDistribution(); /** Constructur that sets the distribution's dimension */ - CProbabilityDistribution(int32_t dimension); + ProbabilityDistribution(int32_t dimension); /** Destructor */ - virtual ~CProbabilityDistribution(); + virtual ~ProbabilityDistribution(); /** Samples from the distribution multiple times * * @param num_samples number of samples to generate * @param pre_samples a matrix of pre-samples that might be used for * sampling. For example, a matrix with standard normal samples for the - * CGaussianDistribution. For reproducible results. Ignored by default. + * GaussianDistribution. For reproducible results. Ignored by default. * @return matrix with samples (column vectors) */ virtual SGMatrix sample(int32_t num_samples, diff --git a/src/shogun/ensemble/CombinationRule.cpp b/src/shogun/ensemble/CombinationRule.cpp index 0e36b96c49d..febf254daf2 100644 --- a/src/shogun/ensemble/CombinationRule.cpp +++ b/src/shogun/ensemble/CombinationRule.cpp @@ -8,13 +8,13 @@ using namespace shogun; -CCombinationRule::CCombinationRule() - : CSGObject() +CombinationRule::CombinationRule() + : SGObject() { } -CCombinationRule::~CCombinationRule() +CombinationRule::~CombinationRule() { } diff --git a/src/shogun/ensemble/CombinationRule.h b/src/shogun/ensemble/CombinationRule.h index 08d64d4e253..b5b4ce0b1cb 100644 --- a/src/shogun/ensemble/CombinationRule.h +++ b/src/shogun/ensemble/CombinationRule.h @@ -18,13 +18,13 @@ namespace shogun * The CombinationRule defines an interface to how to combine the * classification or regression outputs of an ensemble of Machines. */ - class CCombinationRule : public CSGObject + class CombinationRule : public SGObject { public: /** default ctor */ - CCombinationRule(); + CombinationRule(); - virtual ~CCombinationRule(); + virtual ~CombinationRule(); /** * Combines a matrix of an ensemble of Machines output, where each diff --git a/src/shogun/ensemble/MajorityVote.cpp b/src/shogun/ensemble/MajorityVote.cpp index 8a4b83299e7..bd1b52671a4 100644 --- a/src/shogun/ensemble/MajorityVote.cpp +++ b/src/shogun/ensemble/MajorityVote.cpp @@ -9,28 +9,28 @@ using namespace shogun; -CMajorityVote::CMajorityVote() - : CWeightedMajorityVote() +MajorityVote::MajorityVote() + : WeightedMajorityVote() { } -CMajorityVote::~CMajorityVote() +MajorityVote::~MajorityVote() { } -SGVector CMajorityVote::combine(const SGMatrix& ensemble_result) const +SGVector MajorityVote::combine(const SGMatrix& ensemble_result) const { m_weights.resize_vector(ensemble_result.num_cols); m_weights.set_const(1.0); - SGVector combined_result = CWeightedMajorityVote::combine(ensemble_result); + SGVector combined_result = WeightedMajorityVote::combine(ensemble_result); return combined_result; } -float64_t CMajorityVote::combine(const SGVector& ensemble_result) const +float64_t MajorityVote::combine(const SGVector& ensemble_result) const { m_weights.resize_vector(ensemble_result.vlen); m_weights.set_const(1.0); diff --git a/src/shogun/ensemble/MajorityVote.h b/src/shogun/ensemble/MajorityVote.h index 64938609c4f..d4c5cb0c7e4 100644 --- a/src/shogun/ensemble/MajorityVote.h +++ b/src/shogun/ensemble/MajorityVote.h @@ -14,15 +14,15 @@ namespace shogun { /** - * @brief CMajorityVote is a CWeightedMajorityVote combiner, where each + * @brief MajorityVote is a CWeightedMajorityVote combiner, where each * Machine's weight in the ensemble is 1.0 */ - class CMajorityVote : public CWeightedMajorityVote + class MajorityVote : public WeightedMajorityVote { public: - CMajorityVote(); + MajorityVote(); - virtual ~CMajorityVote(); + virtual ~MajorityVote(); /** * Combines a matrix of an ensemble of Machines output, where each diff --git a/src/shogun/ensemble/MeanRule.cpp b/src/shogun/ensemble/MeanRule.cpp index c8b13101ede..a02268370ba 100644 --- a/src/shogun/ensemble/MeanRule.cpp +++ b/src/shogun/ensemble/MeanRule.cpp @@ -10,18 +10,18 @@ using namespace shogun; -CMeanRule::CMeanRule() - : CCombinationRule() +MeanRule::MeanRule() + : CombinationRule() { } -CMeanRule::~CMeanRule() +MeanRule::~MeanRule() { } -SGVector CMeanRule::combine(const SGMatrix& ensemble_result) const +SGVector MeanRule::combine(const SGMatrix& ensemble_result) const { float64_t* row_sum = SGMatrix::get_column_sum(ensemble_result.matrix, @@ -36,7 +36,7 @@ SGVector CMeanRule::combine(const SGMatrix& ensemble_resul return mean_labels; } -float64_t CMeanRule::combine(const SGVector& ensemble_result) const +float64_t MeanRule::combine(const SGVector& ensemble_result) const { float64_t combined = SGVector::sum(ensemble_result); combined /= (float64_t)ensemble_result.vlen; diff --git a/src/shogun/ensemble/MeanRule.h b/src/shogun/ensemble/MeanRule.h index 0eec370934e..9689ba90821 100644 --- a/src/shogun/ensemble/MeanRule.h +++ b/src/shogun/ensemble/MeanRule.h @@ -14,14 +14,14 @@ namespace shogun { /** - * @brief CMeanRule simply averages the outputs of the Machines in the ensemble. + * @brief MeanRule simply averages the outputs of the Machines in the ensemble. */ - class CMeanRule : public CCombinationRule + class MeanRule : public CombinationRule { public: - CMeanRule(); + MeanRule(); - virtual ~CMeanRule(); + virtual ~MeanRule(); /** * Combines a matrix of an ensemble of Machines output, where each diff --git a/src/shogun/ensemble/WeightedMajorityVote.cpp b/src/shogun/ensemble/WeightedMajorityVote.cpp index 11d49a353a6..bb088cef0b1 100644 --- a/src/shogun/ensemble/WeightedMajorityVote.cpp +++ b/src/shogun/ensemble/WeightedMajorityVote.cpp @@ -11,27 +11,27 @@ using namespace shogun; -CWeightedMajorityVote::CWeightedMajorityVote() - : CCombinationRule() +WeightedMajorityVote::WeightedMajorityVote() + : CombinationRule() { init(); register_parameters(); } -CWeightedMajorityVote::CWeightedMajorityVote(SGVector& weights) - : CCombinationRule() +WeightedMajorityVote::WeightedMajorityVote(SGVector& weights) + : CombinationRule() { init(); register_parameters(); m_weights = weights; } -CWeightedMajorityVote::~CWeightedMajorityVote() +WeightedMajorityVote::~WeightedMajorityVote() { } -SGVector CWeightedMajorityVote::combine(const SGMatrix& ensemble_result) const +SGVector WeightedMajorityVote::combine(const SGMatrix& ensemble_result) const { require(m_weights.vlen == ensemble_result.num_cols, "The number of results and weights does not match!"); SGVector mv(ensemble_result.num_rows); @@ -44,22 +44,22 @@ SGVector CWeightedMajorityVote::combine(const SGMatrix& en return mv; } -float64_t CWeightedMajorityVote::combine(const SGVector& ensemble_result) const +float64_t WeightedMajorityVote::combine(const SGVector& ensemble_result) const { return weighted_combine(ensemble_result); } -float64_t CWeightedMajorityVote::weighted_combine(const SGVector& ensemble_result) const +float64_t WeightedMajorityVote::weighted_combine(const SGVector& ensemble_result) const { require(m_weights.vlen == ensemble_result.vlen, "The number of results and weights does not match!"); std::map freq; std::map::iterator it; index_t max_label = -100; - float64_t max = CMath::ALMOST_NEG_INFTY; + float64_t max = Math::ALMOST_NEG_INFTY; for (index_t i = 0; i < ensemble_result.vlen; ++i) { - if (CMath::is_nan(ensemble_result[i])) + if (Math::is_nan(ensemble_result[i])) continue; it = freq.find(ensemble_result[i]); @@ -86,22 +86,22 @@ float64_t CWeightedMajorityVote::weighted_combine(const SGVector& ens return max_label; } -void CWeightedMajorityVote::set_weights(SGVector& w) +void WeightedMajorityVote::set_weights(SGVector& w) { m_weights = w; } -SGVector CWeightedMajorityVote::get_weights() const +SGVector WeightedMajorityVote::get_weights() const { return m_weights; } -void CWeightedMajorityVote::init() +void WeightedMajorityVote::init() { m_weights = SGVector(); } -void CWeightedMajorityVote::register_parameters() +void WeightedMajorityVote::register_parameters() { SG_ADD(&m_weights, "weights", "Weights for the majority vote", ParameterProperties::HYPER); } diff --git a/src/shogun/ensemble/WeightedMajorityVote.h b/src/shogun/ensemble/WeightedMajorityVote.h index eb2a833d4ca..c7d55d319f3 100644 --- a/src/shogun/ensemble/WeightedMajorityVote.h +++ b/src/shogun/ensemble/WeightedMajorityVote.h @@ -27,13 +27,13 @@ namespace shogun * \f$C\f$ is the number of classes * */ - class CWeightedMajorityVote : public CCombinationRule + class WeightedMajorityVote : public CombinationRule { public: /** * Default ctor */ - CWeightedMajorityVote(); + WeightedMajorityVote(); /** * CWeightedMajorityVote constructor @@ -41,9 +41,9 @@ namespace shogun * @param weights a vector of weights, where the nth element is the * weight of the nth Machine in ensemble. */ - CWeightedMajorityVote(SGVector& weights); + WeightedMajorityVote(SGVector& weights); - virtual ~CWeightedMajorityVote(); + virtual ~WeightedMajorityVote(); /** * Combines a matrix of an ensemble of Machines output, where each diff --git a/src/shogun/evaluation/BinaryClassEvaluation.h b/src/shogun/evaluation/BinaryClassEvaluation.h index 7e835e91d37..0dd0dec724e 100644 --- a/src/shogun/evaluation/BinaryClassEvaluation.h +++ b/src/shogun/evaluation/BinaryClassEvaluation.h @@ -15,30 +15,30 @@ namespace shogun { -class CLabels; +class Labels; /** @brief The class TwoClassEvaluation, * a base class used to evaluate binary classification * labels. * */ -class CBinaryClassEvaluation: public CEvaluation +class BinaryClassEvaluation: public Evaluation { public: /** constructor */ - CBinaryClassEvaluation() : CEvaluation() {}; + BinaryClassEvaluation() : Evaluation() {}; /** destructor */ - virtual ~CBinaryClassEvaluation() {}; + virtual ~BinaryClassEvaluation() {}; /** evaluate labels * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct * @return evaluation result */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth) = 0; + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) = 0; }; } diff --git a/src/shogun/evaluation/Calibration.h b/src/shogun/evaluation/Calibration.h index e446b8d396b..100dcf96e6a 100644 --- a/src/shogun/evaluation/Calibration.h +++ b/src/shogun/evaluation/Calibration.h @@ -17,15 +17,15 @@ namespace shogun fit_multiclass() to fit the parameters on the predictions and the true labels. Call calibrate to calibrate predictions. **/ - class CCalibration : public CSGObject + class Calibration : public SGObject { public: /** Constructor. */ - CCalibration() + Calibration() { } /** Destructor. */ - virtual ~CCalibration() + virtual ~Calibration() { } @@ -40,14 +40,14 @@ namespace shogun * @return Indicates whether the calibration was succesful **/ virtual bool - fit_binary(CBinaryLabels* predictions, CBinaryLabels* targets) = 0; + fit_binary(std::shared_ptr predictions, std::shared_ptr targets) = 0; /** Calibrate binary predictions based on parameters learned by calling *fit. * @param predictions The predictions outputted by the machine * @return Calibrated binary labels **/ - virtual CBinaryLabels* calibrate_binary(CBinaryLabels* predictions) = 0; + virtual std::shared_ptr calibrate_binary(std::shared_ptr predictions) = 0; /** Fit calibration parameters for multiclass labels. * @param predictions The predictions outputted by the machine @@ -55,15 +55,15 @@ namespace shogun * @return Indicates whether the calibration was succesful **/ virtual bool fit_multiclass( - CMulticlassLabels* predictions, CMulticlassLabels* targets) = 0; + std::shared_ptr predictions, std::shared_ptr targets) = 0; /** Calibrate multiclass predictions based on parameters learned by *calling fit. * @param predictions The predictions outputted by the machine * @return Calibrated binary labels **/ - virtual CMulticlassLabels* - calibrate_multiclass(CMulticlassLabels* predictions) = 0; + virtual std::shared_ptr + calibrate_multiclass(std::shared_ptr predictions) = 0; }; } #endif \ No newline at end of file diff --git a/src/shogun/evaluation/ClusteringAccuracy.cpp b/src/shogun/evaluation/ClusteringAccuracy.cpp index cbeec53adeb..b9dd54abf9f 100644 --- a/src/shogun/evaluation/ClusteringAccuracy.cpp +++ b/src/shogun/evaluation/ClusteringAccuracy.cpp @@ -9,13 +9,13 @@ using namespace shogun; -float64_t CClusteringAccuracy::evaluate_impl(CLabels* predicted, CLabels* ground_truth) +float64_t ClusteringAccuracy::evaluate_impl(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && ground_truth) ASSERT(ground_truth->get_label_type() == LT_MULTICLASS) ASSERT(predicted->get_label_type() == LT_MULTICLASS) - SGVector predicted_ilabels=((CMulticlassLabels*) predicted)->get_int_labels(); - SGVector groundtruth_ilabels=((CMulticlassLabels*) ground_truth)->get_int_labels(); + SGVector predicted_ilabels=multiclass_labels(predicted)->get_int_labels(); + SGVector groundtruth_ilabels=multiclass_labels(ground_truth)->get_int_labels(); int32_t correct=0; for (int32_t i=0; i < predicted_ilabels.vlen; ++i) { diff --git a/src/shogun/evaluation/ClusteringAccuracy.h b/src/shogun/evaluation/ClusteringAccuracy.h index 9493d132605..3a38e465c1c 100644 --- a/src/shogun/evaluation/ClusteringAccuracy.h +++ b/src/shogun/evaluation/ClusteringAccuracy.h @@ -16,14 +16,14 @@ namespace shogun /** @brief clustering accuracy */ -class CClusteringAccuracy: public CClusteringEvaluation +class ClusteringAccuracy: public ClusteringEvaluation { public: /** constructor */ - CClusteringAccuracy(): CClusteringEvaluation() {} + ClusteringAccuracy(): ClusteringEvaluation() {} /** destructor */ - virtual ~CClusteringAccuracy() {} + virtual ~ClusteringAccuracy() {} /** @return whether criterium has to be maximized or minimized */ virtual EEvaluationDirection get_evaluation_direction() const @@ -49,7 +49,7 @@ class CClusteringAccuracy: public CClusteringEvaluation * @param ground_truth labels assumed to be correct * @return evaluation result */ - virtual float64_t evaluate_impl(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate_impl(std::shared_ptr predicted, std::shared_ptr ground_truth); }; } // namespace shogun diff --git a/src/shogun/evaluation/ClusteringEvaluation.cpp b/src/shogun/evaluation/ClusteringEvaluation.cpp index b9c0116e462..7668275371f 100644 --- a/src/shogun/evaluation/ClusteringEvaluation.cpp +++ b/src/shogun/evaluation/ClusteringEvaluation.cpp @@ -16,26 +16,23 @@ using namespace shogun; using namespace std; -CClusteringEvaluation::CClusteringEvaluation() : CEvaluation() +ClusteringEvaluation::ClusteringEvaluation() : Evaluation() { m_use_best_map = true; SG_ADD(&m_use_best_map, "use_best_map", "Find best match between predicted labels and the ground truth"); } -float64_t CClusteringEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t ClusteringEvaluation::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { if(m_use_best_map) predicted = best_map(predicted, ground_truth); - else - SG_REF(predicted); float64_t result = evaluate_impl(predicted,ground_truth); - SG_UNREF(predicted); return result; } -int32_t CClusteringEvaluation::find_match_count(SGVector l1, int32_t m1, SGVector l2, int32_t m2) +int32_t ClusteringEvaluation::find_match_count(SGVector l1, int32_t m1, SGVector l2, int32_t m2) { int32_t match_count=0; for (int32_t i=l1.vlen-1; i >= 0; --i) @@ -47,22 +44,22 @@ int32_t CClusteringEvaluation::find_match_count(SGVector l1, int32_t m1 return match_count; } -int32_t CClusteringEvaluation::find_mismatch_count(SGVector l1, int32_t m1, SGVector l2, int32_t m2) +int32_t ClusteringEvaluation::find_mismatch_count(SGVector l1, int32_t m1, SGVector l2, int32_t m2) { return l1.vlen - find_match_count(l1, m1, l2, m2); } -CLabels* CClusteringEvaluation::best_map(CLabels* predicted, CLabels* ground_truth) +std::shared_ptr ClusteringEvaluation::best_map(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels()) ASSERT(predicted->get_label_type() == LT_MULTICLASS) ASSERT(ground_truth->get_label_type() == LT_MULTICLASS) - SGVector label_p=((CMulticlassLabels*) predicted)->get_unique_labels(); - SGVector label_g=((CMulticlassLabels*) ground_truth)->get_unique_labels(); + SGVector label_p=multiclass_labels(predicted)->get_unique_labels(); + SGVector label_g=multiclass_labels(ground_truth)->get_unique_labels(); - SGVector predicted_ilabels=((CMulticlassLabels*) predicted)->get_int_labels(); - SGVector groundtruth_ilabels=((CMulticlassLabels*) ground_truth)->get_int_labels(); + SGVector predicted_ilabels=multiclass_labels(predicted)->get_int_labels(); + SGVector groundtruth_ilabels=multiclass_labels(ground_truth)->get_int_labels(); int32_t n_class=max(label_p.vlen, label_g.vlen); SGMatrix G(n_class, n_class); @@ -94,8 +91,7 @@ CLabels* CClusteringEvaluation::best_map(CLabels* predicted, CLabels* ground_tru } } - auto result = new CMulticlassLabels(predicted->get_num_labels()); - SG_REF(result); + auto result = std::make_shared(predicted->get_num_labels()); for (int32_t i= 0; i < predicted_ilabels.vlen; ++i) result->set_int_label(i, label_map[predicted_ilabels[i]]); diff --git a/src/shogun/evaluation/ClusteringEvaluation.h b/src/shogun/evaluation/ClusteringEvaluation.h index 2515a20e2cb..a910176ab32 100644 --- a/src/shogun/evaluation/ClusteringEvaluation.h +++ b/src/shogun/evaluation/ClusteringEvaluation.h @@ -17,14 +17,14 @@ namespace shogun /** @brief The base class used to evaluate clustering */ -class CClusteringEvaluation: public CEvaluation +class ClusteringEvaluation: public Evaluation { public: /** constructor */ - CClusteringEvaluation(); + ClusteringEvaluation(); /** destructor */ - virtual ~CClusteringEvaluation() {} + virtual ~ClusteringEvaluation() {} /** permute the order of the predicted labels to match the ground_truth as good as possible. * @@ -33,21 +33,21 @@ class CClusteringEvaluation: public CEvaluation * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct */ - CLabels* best_map(CLabels* predicted, CLabels* ground_truth); + std::shared_ptr best_map(std::shared_ptr predicted, std::shared_ptr ground_truth); /** evaluate labels * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct * @return evaluation result */ - float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); protected: /** implementation of label evaluation * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct * @return evaluation result */ - virtual float64_t evaluate_impl(CLabels* predicted, CLabels* ground_truth) = 0; + virtual float64_t evaluate_impl(std::shared_ptr predicted, std::shared_ptr ground_truth) = 0; /** find number of matches in the two labels sequence. * diff --git a/src/shogun/evaluation/ClusteringMutualInformation.cpp b/src/shogun/evaluation/ClusteringMutualInformation.cpp index 7ea90c346e2..2481fd72bb1 100644 --- a/src/shogun/evaluation/ClusteringMutualInformation.cpp +++ b/src/shogun/evaluation/ClusteringMutualInformation.cpp @@ -10,21 +10,21 @@ using namespace shogun; -float64_t CClusteringMutualInformation::evaluate_impl(CLabels* predicted, CLabels* ground_truth) +float64_t ClusteringMutualInformation::evaluate_impl(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && ground_truth) ASSERT(predicted->get_label_type() == LT_MULTICLASS) ASSERT(ground_truth->get_label_type() == LT_MULTICLASS) - SGVector label_p=((CMulticlassLabels*) predicted)->get_unique_labels(); - SGVector label_g=((CMulticlassLabels*) ground_truth)->get_unique_labels(); + SGVector label_p=multiclass_labels(predicted)->get_unique_labels(); + SGVector label_g=multiclass_labels(ground_truth)->get_unique_labels(); if (label_p.vlen != label_g.vlen) error("Number of classes are different"); index_t n_class=label_p.vlen; float64_t n_label=predicted->get_num_labels(); - SGVector ilabels_p=((CMulticlassLabels*) predicted)->get_int_labels(); - SGVector ilabels_g=((CMulticlassLabels*) ground_truth)->get_int_labels(); + SGVector ilabels_p=multiclass_labels(predicted)->get_int_labels(); + SGVector ilabels_g=multiclass_labels(ground_truth)->get_int_labels(); SGMatrix G(n_class, n_class); for (index_t i=0; i < n_class; ++i) @@ -66,5 +66,5 @@ float64_t CClusteringMutualInformation::evaluate_impl(CLabels* predicted, CLabel entropy_p += -G_colsum[i] * log(G_colsum[i])/log(2.); } - return mutual_info / CMath::max(entropy_g, entropy_p); + return mutual_info / Math::max(entropy_g, entropy_p); } diff --git a/src/shogun/evaluation/ClusteringMutualInformation.h b/src/shogun/evaluation/ClusteringMutualInformation.h index c3ff5b131e7..56e9be9913f 100644 --- a/src/shogun/evaluation/ClusteringMutualInformation.h +++ b/src/shogun/evaluation/ClusteringMutualInformation.h @@ -16,14 +16,14 @@ namespace shogun /** @brief clustering (normalized) mutual information */ -class CClusteringMutualInformation: public CClusteringEvaluation +class ClusteringMutualInformation: public ClusteringEvaluation { public: /** constructor */ - CClusteringMutualInformation(): CClusteringEvaluation() {} + ClusteringMutualInformation(): ClusteringEvaluation() {} /** destructor */ - virtual ~CClusteringMutualInformation() {} + virtual ~ClusteringMutualInformation() {} /** @return whether criterium has to be maximized or minimized */ virtual EEvaluationDirection get_evaluation_direction() const @@ -49,7 +49,7 @@ class CClusteringMutualInformation: public CClusteringEvaluation * @param ground_truth labels assumed to be correct * @return evaluation result */ - virtual float64_t evaluate_impl(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate_impl(std::shared_ptr predicted, std::shared_ptr ground_truth); }; } diff --git a/src/shogun/evaluation/ContingencyTableEvaluation.cpp b/src/shogun/evaluation/ContingencyTableEvaluation.cpp index 5a42a76c5ac..7340d4cd7ab 100644 --- a/src/shogun/evaluation/ContingencyTableEvaluation.cpp +++ b/src/shogun/evaluation/ContingencyTableEvaluation.cpp @@ -10,14 +10,14 @@ using namespace shogun; -CContingencyTableEvaluation::CContingencyTableEvaluation() - : CContingencyTableEvaluation(ACCURACY) +ContingencyTableEvaluation::ContingencyTableEvaluation() + : ContingencyTableEvaluation(ACCURACY) { } -CContingencyTableEvaluation::CContingencyTableEvaluation( +ContingencyTableEvaluation::ContingencyTableEvaluation( EContingencyTableMeasureType type) - : CBinaryClassEvaluation(), m_type(type), m_computed(false) + : BinaryClassEvaluation(), m_type(type), m_computed(false) { SG_ADD_OPTIONS( (machine_int_t*)&m_type, "type", "type of measure to evaluate", @@ -28,7 +28,7 @@ CContingencyTableEvaluation::CContingencyTableEvaluation( } float64_t -CContingencyTableEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) +ContingencyTableEvaluation::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { require( predicted->get_num_labels() == ground_truth->get_num_labels(), @@ -41,7 +41,7 @@ CContingencyTableEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) auto ground_truth_binary = binary_labels(ground_truth); ground_truth->ensure_valid(); - compute_scores(predicted_binary.get(), ground_truth_binary.get()); + compute_scores(predicted_binary, ground_truth_binary); switch (m_type) { case ACCURACY: @@ -71,7 +71,7 @@ CContingencyTableEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) } EEvaluationDirection -CContingencyTableEvaluation::get_evaluation_direction() const +ContingencyTableEvaluation::get_evaluation_direction() const { switch (m_type) { @@ -102,8 +102,7 @@ CContingencyTableEvaluation::get_evaluation_direction() const return ED_MINIMIZE; } -void CContingencyTableEvaluation::compute_scores( - CBinaryLabels* predicted, CBinaryLabels* ground_truth) +void ContingencyTableEvaluation::compute_scores(std::shared_ptr predicted, std::shared_ptr ground_truth) { m_TP = 0.0; m_FP = 0.0; diff --git a/src/shogun/evaluation/ContingencyTableEvaluation.h b/src/shogun/evaluation/ContingencyTableEvaluation.h index d63f6d3164f..fc8e4f7f77a 100644 --- a/src/shogun/evaluation/ContingencyTableEvaluation.h +++ b/src/shogun/evaluation/ContingencyTableEvaluation.h @@ -17,7 +17,7 @@ namespace shogun { -class CLabels; +class Labels; /** type of measure */ enum EContingencyTableMeasureType @@ -65,28 +65,28 @@ enum EContingencyTableMeasureType * convenient. * */ -class CContingencyTableEvaluation: public CBinaryClassEvaluation +class ContingencyTableEvaluation: public BinaryClassEvaluation { public: /** constructor */ - CContingencyTableEvaluation(); + ContingencyTableEvaluation(); /** constructor * @param type type of measure (e.g ACCURACY) */ - CContingencyTableEvaluation(EContingencyTableMeasureType type); + ContingencyTableEvaluation(EContingencyTableMeasureType type); /** destructor */ - virtual ~CContingencyTableEvaluation() {}; + virtual ~ContingencyTableEvaluation() {}; /** evaluate labels * @param predicted labels * @param ground_truth labels assumed to be correct * @return evaluation result */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); virtual EEvaluationDirection get_evaluation_direction() const; @@ -247,7 +247,7 @@ class CContingencyTableEvaluation: public CBinaryClassEvaluation protected: /** get scores for TP, FP, TN, FN */ - void compute_scores(CBinaryLabels* predicted, CBinaryLabels* ground_truth); + void compute_scores(std::shared_ptr predicted, std::shared_ptr ground_truth); /** type of measure to evaluate */ EContingencyTableMeasureType m_type; @@ -276,17 +276,17 @@ class CContingencyTableEvaluation: public CBinaryClassEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CAccuracyMeasure: public CContingencyTableEvaluation +class AccuracyMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CAccuracyMeasure() : CContingencyTableEvaluation(ACCURACY) {}; + AccuracyMeasure() : ContingencyTableEvaluation(ACCURACY) {}; /* virtual destructor */ - virtual ~CAccuracyMeasure() {}; + virtual ~AccuracyMeasure() {}; /* name */ virtual const char* get_name() const { return "AccuracyMeasure"; }; }; @@ -296,17 +296,17 @@ class CAccuracyMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CErrorRateMeasure: public CContingencyTableEvaluation +class ErrorRateMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CErrorRateMeasure() : CContingencyTableEvaluation(ERROR_RATE) {}; + ErrorRateMeasure() : ContingencyTableEvaluation(ERROR_RATE) {}; /* virtual destructor */ - virtual ~CErrorRateMeasure() {}; + virtual ~ErrorRateMeasure() {}; /* name */ virtual const char* get_name() const { return "ErrorRateMeasure"; }; }; @@ -316,17 +316,17 @@ class CErrorRateMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CBALMeasure: public CContingencyTableEvaluation +class BALMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CBALMeasure() : CContingencyTableEvaluation(BAL) {}; + BALMeasure() : ContingencyTableEvaluation(BAL) {}; /* virtual destructor */ - virtual ~CBALMeasure() {}; + virtual ~BALMeasure() {}; /* name */ virtual const char* get_name() const { return "BALMeasure"; }; }; @@ -336,17 +336,17 @@ class CBALMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CWRACCMeasure: public CContingencyTableEvaluation +class WRACCMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CWRACCMeasure() : CContingencyTableEvaluation(WRACC) {}; + WRACCMeasure() : ContingencyTableEvaluation(WRACC) {}; /* virtual destructor */ - virtual ~CWRACCMeasure() {}; + virtual ~WRACCMeasure() {}; /* name */ virtual const char* get_name() const { return "WRACCMeasure"; }; }; @@ -356,17 +356,17 @@ class CWRACCMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CF1Measure: public CContingencyTableEvaluation +class F1Measure: public ContingencyTableEvaluation { public: /* constructor */ - CF1Measure() : CContingencyTableEvaluation(F1) {}; + F1Measure() : ContingencyTableEvaluation(F1) {}; /* virtual destructor */ - virtual ~CF1Measure() {}; + virtual ~F1Measure() {}; /* name */ virtual const char* get_name() const { return "F1Measure"; }; }; @@ -376,17 +376,17 @@ class CF1Measure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CCrossCorrelationMeasure: public CContingencyTableEvaluation +class CrossCorrelationMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CCrossCorrelationMeasure() : CContingencyTableEvaluation(CROSS_CORRELATION) {}; + CrossCorrelationMeasure() : ContingencyTableEvaluation(CROSS_CORRELATION) {}; /* virtual destructor */ - virtual ~CCrossCorrelationMeasure() {}; + virtual ~CrossCorrelationMeasure() {}; /* name */ virtual const char* get_name() const { return "CrossCorrelationMeasure"; }; }; @@ -396,17 +396,17 @@ class CCrossCorrelationMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CRecallMeasure: public CContingencyTableEvaluation +class RecallMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CRecallMeasure() : CContingencyTableEvaluation(RECALL) {}; + RecallMeasure() : ContingencyTableEvaluation(RECALL) {}; /* virtual destructor */ - virtual ~CRecallMeasure() {}; + virtual ~RecallMeasure() {}; /* name */ virtual const char* get_name() const { return "RecallMeasure"; }; }; @@ -416,17 +416,17 @@ class CRecallMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CPrecisionMeasure: public CContingencyTableEvaluation +class PrecisionMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CPrecisionMeasure() : CContingencyTableEvaluation(PRECISION) {}; + PrecisionMeasure() : ContingencyTableEvaluation(PRECISION) {}; /* virtual destructor */ - virtual ~CPrecisionMeasure() {}; + virtual ~PrecisionMeasure() {}; /* name */ virtual const char* get_name() const { return "PrecisionMeasure"; }; }; @@ -436,17 +436,17 @@ class CPrecisionMeasure: public CContingencyTableEvaluation * * This class is also capable of measuring * any other rate using get_[measure name] methods - * of CContingencyTableEvaluation class. + * of ContingencyTableEvaluation class. * * Note that evaluate() should be called first. */ -class CSpecificityMeasure: public CContingencyTableEvaluation +class SpecificityMeasure: public ContingencyTableEvaluation { public: /* constructor */ - CSpecificityMeasure() : CContingencyTableEvaluation(SPECIFICITY) {}; + SpecificityMeasure() : ContingencyTableEvaluation(SPECIFICITY) {}; /* virtual destructor */ - virtual ~CSpecificityMeasure() {}; + virtual ~SpecificityMeasure() {}; /* name */ virtual const char* get_name() const { return "SpecificityMeasure"; }; }; diff --git a/src/shogun/evaluation/CrossValidation.cpp b/src/shogun/evaluation/CrossValidation.cpp index ca63647b8cf..8e0f4ebab0c 100644 --- a/src/shogun/evaluation/CrossValidation.cpp +++ b/src/shogun/evaluation/CrossValidation.cpp @@ -20,41 +20,41 @@ using namespace shogun; -CCrossValidation::CCrossValidation() : Seedable() +CrossValidation::CrossValidation() : Seedable() { init(); } -CCrossValidation::CCrossValidation( - CMachine* machine, CFeatures* features, CLabels* labels, - CSplittingStrategy* splitting_strategy, CEvaluation* evaluation_criterion) - : Seedable( +CrossValidation::CrossValidation( + std::shared_ptr machine, std::shared_ptr features, std::shared_ptr labels, + std::shared_ptr splitting_strategy, std::shared_ptr evaluation_criterion) + : Seedable( machine, features, labels, splitting_strategy, evaluation_criterion) { init(); } -CCrossValidation::CCrossValidation( - CMachine* machine, CLabels* labels, CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion) - : Seedable( +CrossValidation::CrossValidation( + std::shared_ptr machine, std::shared_ptr labels, std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion) + : Seedable( machine, labels, splitting_strategy, evaluation_criterion) { init(); } -CCrossValidation::~CCrossValidation() +CrossValidation::~CrossValidation() { } -void CCrossValidation::init() +void CrossValidation::init() { m_num_runs = 1; SG_ADD(&m_num_runs, "num_runs", "Number of repetitions"); } -CEvaluationResult* CCrossValidation::evaluate_impl() const +std::shared_ptr CrossValidation::evaluate_impl() const { SGVector results(m_num_runs); @@ -67,18 +67,17 @@ CEvaluationResult* CCrossValidation::evaluate_impl() const } /* construct evaluation result */ - CCrossValidationResult* result = new CCrossValidationResult(); - result->set_mean(CStatistics::mean(results)); + auto result = std::make_shared(); + result->set_mean(Statistics::mean(results)); if (m_num_runs > 1) - result->set_std_dev(CStatistics::std_deviation(results)); + result->set_std_dev(Statistics::std_deviation(results)); else result->set_std_dev(0); - SG_REF(result); return result; } -void CCrossValidation::set_num_runs(int32_t num_runs) +void CrossValidation::set_num_runs(int32_t num_runs) { if (num_runs < 1) error("{} is an illegal number of repetitions", num_runs); @@ -86,7 +85,7 @@ void CCrossValidation::set_num_runs(int32_t num_runs) m_num_runs = num_runs; } -float64_t CCrossValidation::evaluate_one_run(int64_t index) const +float64_t CrossValidation::evaluate_one_run(int64_t index) const { SG_TRACE("entering {}::evaluate_one_run()", get_name()); index_t num_subsets = m_splitting_strategy->get_num_subsets(); @@ -114,10 +113,6 @@ float64_t CCrossValidation::evaluate_one_run(int64_t index) const auto labels_train = view(m_labels, idx_train); auto features_test = view(m_features, idx_test); auto labels_test = view(m_labels, idx_test); - SG_REF(features_train); - SG_REF(labels_train); - SG_REF(features_test); - SG_REF(labels_test); auto evaluation_criterion = make_clone(m_evaluation_criterion); @@ -125,22 +120,13 @@ float64_t CCrossValidation::evaluate_one_run(int64_t index) const machine->train(features_train); auto result_labels = machine->apply(features_test); - SG_REF(result_labels); results[i] = evaluation_criterion->evaluate(result_labels, labels_test); io::info("Result of cross-validation fold {}/{} is {}", i+1, num_subsets, results[i]); - - SG_UNREF(machine); - SG_UNREF(features_train); - SG_UNREF(labels_train); - SG_UNREF(features_test); - SG_UNREF(labels_test); - SG_UNREF(evaluation_criterion); - SG_UNREF(result_labels); } /* build arithmetic mean of results */ - float64_t mean = CStatistics::mean(results); + float64_t mean = Statistics::mean(results); SG_TRACE("leaving {}::evaluate_one_run()", get_name()); return mean; diff --git a/src/shogun/evaluation/CrossValidation.h b/src/shogun/evaluation/CrossValidation.h index d9180a035dc..df61b42565f 100644 --- a/src/shogun/evaluation/CrossValidation.h +++ b/src/shogun/evaluation/CrossValidation.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn, - * Giovanni De Toni, Jacob Walker, Saurabh Mahindre, Yuyu Zhang, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn, + * Giovanni De Toni, Jacob Walker, Saurabh Mahindre, Yuyu Zhang, * Roman Votyakov */ @@ -18,17 +18,17 @@ namespace shogun { - class CMachineEvaluation; - class CCrossValidationOutput; + class MachineEvaluation; + class CrossValidationOutput; class CrossValidationStorage; - class CList; + class List; /** @brief type to encapsulate the results of an evaluation run. */ - class CCrossValidationResult : public CEvaluationResult + class CrossValidationResult : public EvaluationResult { public: - CCrossValidationResult() + CrossValidationResult() { SG_ADD(&mean, "mean", "Mean of results"); SG_ADD( @@ -123,11 +123,11 @@ namespace shogun * HP Laboratories.] for details on this subject. * */ - class CCrossValidation : public Seedable + class CrossValidation : public Seedable { public: /** constructor */ - CCrossValidation(); + CrossValidation(); /** constructor * @param machine learning machine to use @@ -137,10 +137,10 @@ namespace shogun * @param evaluation_criterion evaluation criterion to use * evaluation */ - CCrossValidation( - CMachine* machine, CFeatures* features, CLabels* labels, - CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion); + CrossValidation( + std::shared_ptr machine, std::shared_ptr features, + std::shared_ptr labels, std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion); /** constructor, for use with custom kernels (no features) * @param machine learning machine to use @@ -148,13 +148,13 @@ namespace shogun * @param splitting_strategy splitting strategy to use * @param evaluation_criterion evaluation criterion to use */ - CCrossValidation( - CMachine* machine, CLabels* labels, - CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion); + CrossValidation( + std::shared_ptr machine, std::shared_ptr labels, + std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion); /** destructor */ - virtual ~CCrossValidation(); + virtual ~CrossValidation(); /** setter for the number of runs to use for evaluation */ void set_num_runs(int32_t num_runs); @@ -173,7 +173,7 @@ namespace shogun * Does the actual evaluation. * @return the cross-validation result */ - virtual CEvaluationResult* evaluate_impl() const override; + virtual std::shared_ptr evaluate_impl() const override; /** Evaluates one single cross-validation run. * Current implementation evaluates each fold separately and then diff --git a/src/shogun/evaluation/CrossValidationSplitting.cpp b/src/shogun/evaluation/CrossValidationSplitting.cpp index 2ea7f2d2688..bb74c2fd915 100644 --- a/src/shogun/evaluation/CrossValidationSplitting.cpp +++ b/src/shogun/evaluation/CrossValidationSplitting.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Thoralf Klein, Soeren Sonnenburg, + * Authors: Heiko Strathmann, Thoralf Klein, Soeren Sonnenburg, * Fernando Iglesias, Viktor Gal */ @@ -11,18 +11,18 @@ using namespace shogun; -CCrossValidationSplitting::CCrossValidationSplitting() : - RandomMixin() +CrossValidationSplitting::CrossValidationSplitting() : + RandomMixin() { } -CCrossValidationSplitting::CCrossValidationSplitting( - CLabels* labels, index_t num_subsets) : - RandomMixin(labels, num_subsets) +CrossValidationSplitting::CrossValidationSplitting( + std::shared_ptr labels, index_t num_subsets) : + RandomMixin(labels, num_subsets) { } -void CCrossValidationSplitting::build_subsets() +void CrossValidationSplitting::build_subsets() { require(m_labels, "No labels provided."); /* ensure that subsets are empty and set flag to filled */ @@ -41,15 +41,11 @@ void CCrossValidationSplitting::build_subsets() for (index_t i=0; i* current=(CDynamicArray*) - m_subset_indices->get_element(current_subset); + auto current=m_subset_indices->get_element>(current_subset); /* add element of current index */ current->append_element(indices.vector[i]); - /* unref */ - SG_UNREF(current); - /* iterate over subsets */ current_subset=(current_subset+1) % num_subsets; } diff --git a/src/shogun/evaluation/CrossValidationSplitting.h b/src/shogun/evaluation/CrossValidationSplitting.h index e37b48adb01..02c581f30d8 100644 --- a/src/shogun/evaluation/CrossValidationSplitting.h +++ b/src/shogun/evaluation/CrossValidationSplitting.h @@ -15,24 +15,24 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Implementation of normal cross-validation on the base of - * CSplittingStrategy. Produces subset index sets of equal size (at most one + * SplittingStrategy. Produces subset index sets of equal size (at most one * difference) */ -class CCrossValidationSplitting: public RandomMixin +class CrossValidationSplitting: public RandomMixin { public: /** constructor */ - CCrossValidationSplitting(); + CrossValidationSplitting(); /** constructor * * @param labels labels to be (possibly) used for splitting * @param num_subsets desired number of subsets, the labels are split into */ - CCrossValidationSplitting(CLabels* labels, index_t num_subsets); + CrossValidationSplitting(std::shared_ptr labels, index_t num_subsets); /** @return name of the SGSerializable */ virtual const char* get_name() const diff --git a/src/shogun/evaluation/CrossValidationStorage.cpp b/src/shogun/evaluation/CrossValidationStorage.cpp index f8bf93916b4..b7b29b1eabf 100644 --- a/src/shogun/evaluation/CrossValidationStorage.cpp +++ b/src/shogun/evaluation/CrossValidationStorage.cpp @@ -41,7 +41,7 @@ using namespace shogun; -CrossValidationFoldStorage::CrossValidationFoldStorage() : CEvaluationResult() +CrossValidationFoldStorage::CrossValidationFoldStorage() : EvaluationResult() { m_current_run_index = 0; m_current_fold_index = 0; @@ -59,8 +59,8 @@ CrossValidationFoldStorage::CrossValidationFoldStorage() : CEvaluationResult() &m_trained_machine, "trained_machine", "The machine trained by this fold", ParameterProperties::HYPER); SG_ADD( - &m_test_result, "predicted_labels", "The test result of this fold", - ParameterProperties::HYPER); + &m_test_result, "predicted_labels", + "The test result of this fold", ParameterProperties::HYPER); SG_ADD( &m_test_true_result, "ground_truth_labels", "The true test result for this fold", ParameterProperties::HYPER); @@ -77,9 +77,6 @@ CrossValidationFoldStorage::CrossValidationFoldStorage() : CEvaluationResult() CrossValidationFoldStorage::~CrossValidationFoldStorage() { - SG_UNREF(m_test_result); - SG_UNREF(m_test_true_result); - SG_UNREF(m_trained_machine); } void CrossValidationFoldStorage::post_update_results() @@ -90,16 +87,14 @@ void CrossValidationFoldStorage::print_result() { } -CSGObject* CrossValidationFoldStorage::create_empty() const +std::shared_ptr CrossValidationFoldStorage::create_empty() const { - auto clone = new CrossValidationFoldStorage(); - SG_REF(clone) - return clone; + return std::make_shared(); } /** CrossValidationStorage **/ -CrossValidationStorage::CrossValidationStorage() : CEvaluationResult() +CrossValidationStorage::CrossValidationStorage() : EvaluationResult() { m_num_runs = 0; m_num_folds = 0; @@ -121,9 +116,6 @@ CrossValidationStorage::CrossValidationStorage() : CEvaluationResult() CrossValidationStorage::~CrossValidationStorage() { - SG_UNREF(m_original_labels); - for (auto i : m_folds_results) - SG_UNREF(i) } void CrossValidationStorage::post_init() @@ -131,10 +123,9 @@ void CrossValidationStorage::post_init() } void CrossValidationStorage::append_fold_result( - CrossValidationFoldStorage* result) + std::shared_ptr result) { - auto cloned = dynamic_cast(result->clone()); - SG_REF(cloned) + auto cloned = result->clone()->as(); m_folds_results.push_back(cloned); } @@ -142,9 +133,7 @@ void CrossValidationStorage::print_result() { } -CSGObject* CrossValidationStorage::create_empty() const +std::shared_ptr CrossValidationStorage::create_empty() const { - auto clone = new CrossValidationStorage(); - SG_REF(clone) - return clone; -} \ No newline at end of file + return std::make_shared(); +} diff --git a/src/shogun/evaluation/CrossValidationStorage.h b/src/shogun/evaluation/CrossValidationStorage.h index e3d24fd58f7..ca0a81b531b 100644 --- a/src/shogun/evaluation/CrossValidationStorage.h +++ b/src/shogun/evaluation/CrossValidationStorage.h @@ -44,14 +44,14 @@ namespace shogun { - class CMachine; - class CLabels; + class Machine; + class Labels; class CEvaluation; /** * Store information about a single fold run. */ - class CrossValidationFoldStorage : public CEvaluationResult + class CrossValidationFoldStorage : public EvaluationResult { public: CrossValidationFoldStorage(); @@ -78,7 +78,7 @@ namespace shogun * has no create() method inside class_list.h * @return an empty CrossValidationFoldStorage object SG_REF'ed */ - virtual CSGObject* create_empty() const; + virtual std::shared_ptr create_empty() const; /** Current run index is written here */ index_t m_current_run_index; @@ -93,13 +93,13 @@ namespace shogun SGVector m_test_indices; /** Trained machine */ - CMachine* m_trained_machine; + std::shared_ptr m_trained_machine; /** Test results */ - CLabels* m_test_result; + std::shared_ptr m_test_result; /** Ground truth */ - CLabels* m_test_true_result; + std::shared_ptr m_test_true_result; /** Evaluation result for this fold */ float64_t m_evaluation_result; @@ -108,7 +108,7 @@ namespace shogun /** * This class store some information about CrossValidation runs. */ - class CrossValidationStorage : public CEvaluationResult + class CrossValidationStorage : public EvaluationResult { public: /** Constructor */ @@ -135,7 +135,7 @@ namespace shogun * Append a fold result to this storage * @param result the result of a fold */ - virtual void append_fold_result(CrossValidationFoldStorage* result); + virtual void append_fold_result(std::shared_ptr result); protected: /** @@ -143,7 +143,7 @@ namespace shogun * has no create() method inside class_list.h * @return an empty CrossValidationStorage object SG_REF'ed */ - virtual CSGObject* create_empty() const; + virtual std::shared_ptr create_empty() const; /** number of runs is initialised here */ index_t m_num_runs; @@ -152,10 +152,10 @@ namespace shogun index_t m_num_folds; /** Original labels */ - CLabels* m_original_labels; + std::shared_ptr m_original_labels; /** Vector with all the folds results */ - std::vector m_folds_results; + std::vector> m_folds_results; }; } diff --git a/src/shogun/evaluation/DifferentiableFunction.cpp b/src/shogun/evaluation/DifferentiableFunction.cpp index 68a1fe9a1f5..28ff72bda2d 100644 --- a/src/shogun/evaluation/DifferentiableFunction.cpp +++ b/src/shogun/evaluation/DifferentiableFunction.cpp @@ -8,10 +8,10 @@ using namespace shogun; -CDifferentiableFunction::CDifferentiableFunction() : CSGObject() +DifferentiableFunction::DifferentiableFunction() : SGObject() { } -CDifferentiableFunction::~CDifferentiableFunction() +DifferentiableFunction::~DifferentiableFunction() { } diff --git a/src/shogun/evaluation/DifferentiableFunction.h b/src/shogun/evaluation/DifferentiableFunction.h index eb672742774..2e00623157c 100644 --- a/src/shogun/evaluation/DifferentiableFunction.h +++ b/src/shogun/evaluation/DifferentiableFunction.h @@ -19,13 +19,13 @@ namespace shogun /** @brief An abstract class that describes a differentiable function used for * GradientEvaluation. */ -class CDifferentiableFunction : public CSGObject +class DifferentiableFunction : public SGObject { public: /** default constructor */ - CDifferentiableFunction(); + DifferentiableFunction(); - virtual ~CDifferentiableFunction(); + virtual ~DifferentiableFunction(); /** get the gradient * @@ -34,8 +34,8 @@ class CDifferentiableFunction : public CSGObject * @return map of gradient. Keys are names of parameters, values are values * of derivative with respect to that parameter. */ - virtual CMap >* get_gradient( - CMap* parameters)=0; + virtual std::shared_ptr >> get_gradient( + std::shared_ptr> parameters)=0; /** get the function value * diff --git a/src/shogun/evaluation/DirectorContingencyTableEvaluation.h b/src/shogun/evaluation/DirectorContingencyTableEvaluation.h index 38dff02ee06..2a0005b98ee 100644 --- a/src/shogun/evaluation/DirectorContingencyTableEvaluation.h +++ b/src/shogun/evaluation/DirectorContingencyTableEvaluation.h @@ -19,26 +19,26 @@ namespace shogun * a base class used to evaluate 2-class classification * using SWIG directors. */ -IGNORE_IN_CLASSLIST class CDirectorContingencyTableEvaluation: public CContingencyTableEvaluation +IGNORE_IN_CLASSLIST class DirectorContingencyTableEvaluation: public ContingencyTableEvaluation { public: /** constructor */ - CDirectorContingencyTableEvaluation() : - CContingencyTableEvaluation(CUSTOM) + DirectorContingencyTableEvaluation() : + ContingencyTableEvaluation(CUSTOM) { } /** destructor */ - virtual ~CDirectorContingencyTableEvaluation() + virtual ~DirectorContingencyTableEvaluation() { } /** Evaluate */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth) + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { - return CContingencyTableEvaluation::evaluate(predicted, ground_truth); + return ContingencyTableEvaluation::evaluate(predicted, ground_truth); } /** Computes custom score, not implemented diff --git a/src/shogun/evaluation/Evaluation.h b/src/shogun/evaluation/Evaluation.h index f3c1b7584bf..f10f09046ee 100644 --- a/src/shogun/evaluation/Evaluation.h +++ b/src/shogun/evaluation/Evaluation.h @@ -18,7 +18,7 @@ namespace shogun { -class CLabels; +class Labels; /** enum which used to define whether an evaluation measure has to be minimized * or maximized @@ -34,14 +34,14 @@ enum EEvaluationDirection * * This class provides only interface for evaluation measures. */ -class CEvaluation : public CSGObject +class Evaluation : public SGObject { public: /** default constructor */ - CEvaluation() : CSGObject() { }; + Evaluation() : SGObject() { }; /** destructor */ - virtual ~CEvaluation() { }; + virtual ~Evaluation() { }; /** evaluate labels * @@ -50,7 +50,7 @@ class CEvaluation : public CSGObject * * @return evaluation result */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth)=0; + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth)=0; /** set absolute indices of labels to be evaluated next used by multitask * evaluations diff --git a/src/shogun/evaluation/EvaluationResult.h b/src/shogun/evaluation/EvaluationResult.h index 3d47c565210..cf599975e14 100644 --- a/src/shogun/evaluation/EvaluationResult.h +++ b/src/shogun/evaluation/EvaluationResult.h @@ -17,13 +17,13 @@ namespace shogun /** @brief Abstract class that contains the result generated by the * MachineEvaluation class. */ -class CEvaluationResult : public CSGObject +class EvaluationResult : public SGObject { public: /** default constructor */ - CEvaluationResult() : CSGObject() { } + EvaluationResult() : SGObject() { } - virtual ~CEvaluationResult() { } + virtual ~EvaluationResult() { } /** print result */ virtual void print_result()=0; diff --git a/src/shogun/evaluation/GradientCriterion.h b/src/shogun/evaluation/GradientCriterion.h index a2d9cf89a66..a54af8d02cc 100644 --- a/src/shogun/evaluation/GradientCriterion.h +++ b/src/shogun/evaluation/GradientCriterion.h @@ -19,13 +19,13 @@ namespace shogun * * Does not provide any label evaluation measure, however. */ -class CGradientCriterion : public CEvaluation +class GradientCriterion : public Evaluation { public: /** default constructor */ - CGradientCriterion() : CEvaluation() { m_direction=ED_MINIMIZE; } + GradientCriterion() : Evaluation() { m_direction=ED_MINIMIZE; } - virtual ~CGradientCriterion() { } + virtual ~GradientCriterion() { } /** evaluate labels (not really used in this class). * @@ -34,7 +34,7 @@ class CGradientCriterion : public CEvaluation * * @return evaluation result */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth) + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { return 0.0; } diff --git a/src/shogun/evaluation/GradientEvaluation.cpp b/src/shogun/evaluation/GradientEvaluation.cpp index ca6a4669cf5..bffa50a6fd0 100644 --- a/src/shogun/evaluation/GradientEvaluation.cpp +++ b/src/shogun/evaluation/GradientEvaluation.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Roman Votyakov, Heiko Strathmann, Giovanni De Toni, + * Authors: Jacob Walker, Roman Votyakov, Heiko Strathmann, Giovanni De Toni, * Sergey Lisitsyn */ @@ -10,19 +10,19 @@ using namespace shogun; -CGradientEvaluation::CGradientEvaluation() : CMachineEvaluation() +GradientEvaluation::GradientEvaluation() : MachineEvaluation() { init(); } -CGradientEvaluation::CGradientEvaluation(CMachine* machine, CFeatures* features, - CLabels* labels, CEvaluation* evaluation_crit, bool autolock) : - CMachineEvaluation(machine, features, labels, NULL, evaluation_crit, autolock) +GradientEvaluation::GradientEvaluation(std::shared_ptr machine, std::shared_ptr features, + std::shared_ptr labels, std::shared_ptr evaluation_crit, bool autolock) : + MachineEvaluation(machine, features, labels, NULL, evaluation_crit, autolock) { init(); } -void CGradientEvaluation::init() +void GradientEvaluation::init() { m_diff=NULL; m_parameter_dictionary=NULL; @@ -33,41 +33,37 @@ void CGradientEvaluation::init() ParameterProperties::HYPER); } -CGradientEvaluation::~CGradientEvaluation() +GradientEvaluation::~GradientEvaluation() { - SG_UNREF(m_diff); - SG_UNREF(m_parameter_dictionary); + + } -void CGradientEvaluation::update_parameter_dictionary() const +void GradientEvaluation::update_parameter_dictionary() const { - SG_UNREF(m_parameter_dictionary); - - m_parameter_dictionary=new CMap(); + m_parameter_dictionary=std::make_shared>(); m_diff->build_gradient_parameter_dictionary(m_parameter_dictionary); - SG_REF(m_parameter_dictionary); } -CEvaluationResult* CGradientEvaluation::evaluate_impl() const +std::shared_ptr GradientEvaluation::evaluate_impl() const { if (parameter_hash_changed()) update_parameter_dictionary(); // create gradient result object - CGradientResult* result=new CGradientResult(); - SG_REF(result); + auto result=std::make_shared(); + // set function value result->set_value(m_diff->get_value()); - CMap >* gradient=m_diff->get_gradient( - m_parameter_dictionary); + auto gradient=m_diff->get_gradient(m_parameter_dictionary); // set gradient and parameter dictionary result->set_gradient(gradient); result->set_paramter_dictionary(m_parameter_dictionary); - SG_UNREF(gradient); + update_parameter_hash(); diff --git a/src/shogun/evaluation/GradientEvaluation.h b/src/shogun/evaluation/GradientEvaluation.h index 166ec7a2533..8efdfa2e794 100644 --- a/src/shogun/evaluation/GradientEvaluation.h +++ b/src/shogun/evaluation/GradientEvaluation.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Roman Votyakov, + * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Roman Votyakov, * Yuyu Zhang, Giovanni De Toni */ @@ -20,11 +20,11 @@ namespace shogun /** @brief Class evaluates a machine using its associated differentiable * function for the function value and its gradient with respect to parameters. */ -class CGradientEvaluation: public CMachineEvaluation +class GradientEvaluation: public MachineEvaluation { public: /** default constructor */ - CGradientEvaluation(); + GradientEvaluation(); /** constructor * @@ -34,10 +34,11 @@ class CGradientEvaluation: public CMachineEvaluation * @param evaluation_criterion evaluation criterion to use * @param autolock whether machine should be auto-locked before evaluation */ - CGradientEvaluation(CMachine* machine, CFeatures* features, CLabels* labels, - CEvaluation* evaluation_criterion, bool autolock=true); + GradientEvaluation(std::shared_ptr machine, + std::shared_ptr features, std::shared_ptr labels, + std::shared_ptr evaluation_criterion, bool autolock=true); - virtual ~CGradientEvaluation(); + virtual ~GradientEvaluation(); /** returns the name of the machine evaluation * @@ -49,10 +50,10 @@ class CGradientEvaluation: public CMachineEvaluation * * @param diff differentiable function */ - inline void set_function(CDifferentiableFunction* diff) + inline void set_function(std::shared_ptr diff) { - SG_REF(diff); - SG_UNREF(m_diff); + + m_diff=diff; } @@ -60,9 +61,9 @@ class CGradientEvaluation: public CMachineEvaluation * * @return differentiable function */ - inline CDifferentiableFunction* get_function() const + inline std::shared_ptr get_function() const { - SG_REF(m_diff); + return m_diff; } @@ -74,17 +75,17 @@ class CGradientEvaluation: public CMachineEvaluation * * @return GradientResult containing value and gradient */ - virtual CEvaluationResult* evaluate_impl() const; + virtual std::shared_ptr evaluate_impl() const; /** updates parameter dictionary of differentiable function */ void update_parameter_dictionary() const; private: /** differentiable function */ - CDifferentiableFunction* m_diff; + std::shared_ptr m_diff; /** parameter dictionary of differentiable function */ - mutable CMap* m_parameter_dictionary; + mutable std::shared_ptr> m_parameter_dictionary; }; } #endif /* CGRADIENTEVALUATION_H_ */ diff --git a/src/shogun/evaluation/GradientResult.h b/src/shogun/evaluation/GradientResult.h index ebfeb7c3436..fdbcf349443 100644 --- a/src/shogun/evaluation/GradientResult.h +++ b/src/shogun/evaluation/GradientResult.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Roman Votyakov, Soeren Sonnenburg, Heiko Strathmann, + * Authors: Jacob Walker, Roman Votyakov, Soeren Sonnenburg, Heiko Strathmann, * Yuyu Zhang, Ariane Paola Gomes */ @@ -20,21 +20,21 @@ namespace shogun /** @brief Container class that returns results from GradientEvaluation. It * contains the function value as well as its gradient. */ -class CGradientResult : public CEvaluationResult +class GradientResult : public EvaluationResult { public: /** default constructor */ - CGradientResult() : CEvaluationResult() + GradientResult() : EvaluationResult() { m_total_variables=0; m_gradient=NULL; m_parameter_dictionary=NULL; } - virtual ~CGradientResult() + virtual ~GradientResult() { - SG_UNREF(m_gradient); - SG_UNREF(m_parameter_dictionary); + + } /** returns the name of the evaluation result @@ -66,7 +66,7 @@ class CGradientResult : public CEvaluationResult for (index_t i=0; iget_num_elements(); i++) { - CMapNode >* param_node= + auto param_node= m_gradient->get_node_ptr(i); // get parameter name @@ -130,12 +130,12 @@ class CGradientResult : public CEvaluationResult * * @param gradient gradient map to set */ - virtual void set_gradient(CMap >* gradient) + virtual void set_gradient(std::shared_ptr >> gradient) { require(gradient, "Gradient map should not be NULL"); - SG_REF(gradient); - SG_UNREF(m_gradient); + + m_gradient=gradient; m_total_variables=0; @@ -152,9 +152,9 @@ class CGradientResult : public CEvaluationResult * * @return gradient map */ - virtual CMap >* get_gradient() + virtual std::shared_ptr >> get_gradient() { - SG_REF(m_gradient); + return m_gradient; } @@ -163,10 +163,10 @@ class CGradientResult : public CEvaluationResult * @param parameter_dictionary parameter dictionary */ virtual void set_paramter_dictionary( - CMap* parameter_dictionary) + std::shared_ptr> parameter_dictionary) { - SG_REF(parameter_dictionary); - SG_UNREF(m_parameter_dictionary); + + m_parameter_dictionary=parameter_dictionary; } @@ -174,9 +174,9 @@ class CGradientResult : public CEvaluationResult * * @return parameter dictionary */ - virtual CMap* get_paramter_dictionary() + virtual std::shared_ptr> get_paramter_dictionary() { - SG_REF(m_parameter_dictionary); + return m_parameter_dictionary; } @@ -185,10 +185,10 @@ class CGradientResult : public CEvaluationResult SGVector m_value; /** function gradient */ - CMap >* m_gradient; + std::shared_ptr >> m_gradient; /** which objects do the gradient parameters belong to? */ - CMap* m_parameter_dictionary; + std::shared_ptr> m_parameter_dictionary; /** total number of variables represented by the gradient */ uint32_t m_total_variables; diff --git a/src/shogun/evaluation/LOOCrossValidationSplitting.cpp b/src/shogun/evaluation/LOOCrossValidationSplitting.cpp index c86575f9a63..ddff7d67d18 100644 --- a/src/shogun/evaluation/LOOCrossValidationSplitting.cpp +++ b/src/shogun/evaluation/LOOCrossValidationSplitting.cpp @@ -9,11 +9,11 @@ using namespace shogun; -CLOOCrossValidationSplitting::CLOOCrossValidationSplitting() : - CCrossValidationSplitting() +LOOCrossValidationSplitting::LOOCrossValidationSplitting() : + CrossValidationSplitting() {} -CLOOCrossValidationSplitting::CLOOCrossValidationSplitting( - CLabels* labels) : - CCrossValidationSplitting(labels, labels->get_num_labels()) +LOOCrossValidationSplitting::LOOCrossValidationSplitting( + std::shared_ptr labels) : + CrossValidationSplitting(labels, labels->get_num_labels()) {} diff --git a/src/shogun/evaluation/LOOCrossValidationSplitting.h b/src/shogun/evaluation/LOOCrossValidationSplitting.h index e33594c6aae..1ef93efa071 100644 --- a/src/shogun/evaluation/LOOCrossValidationSplitting.h +++ b/src/shogun/evaluation/LOOCrossValidationSplitting.h @@ -14,19 +14,19 @@ namespace shogun { /** @brief Implementation of Leave one out cross-validation on the base of - * CCrossValidationSplitting. Produces subset index sets consisting of one element,for each label. + * CrossValidationSplitting. Produces subset index sets consisting of one element,for each label. */ -class CLOOCrossValidationSplitting: public CCrossValidationSplitting +class LOOCrossValidationSplitting: public CrossValidationSplitting { public: /** constructor */ - CLOOCrossValidationSplitting(); + LOOCrossValidationSplitting(); /** constructor * * @param labels labels to be (possibly) used for splitting */ - CLOOCrossValidationSplitting(CLabels* labels); + LOOCrossValidationSplitting(std::shared_ptr labels); /** @return name of the SGSerializable */ virtual const char* get_name() const diff --git a/src/shogun/evaluation/MachineEvaluation.cpp b/src/shogun/evaluation/MachineEvaluation.cpp index 7605bd15b4f..dee3bc354e0 100644 --- a/src/shogun/evaluation/MachineEvaluation.cpp +++ b/src/shogun/evaluation/MachineEvaluation.cpp @@ -17,14 +17,14 @@ using namespace shogun; -CMachineEvaluation::CMachineEvaluation() +MachineEvaluation::MachineEvaluation() { init(); } -CMachineEvaluation::CMachineEvaluation(CMachine* machine, CFeatures* features, - CLabels* labels, CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion, bool autolock) +MachineEvaluation::MachineEvaluation(std::shared_ptr machine, std::shared_ptr features, + std::shared_ptr labels, std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion, bool autolock) { init(); @@ -34,16 +34,16 @@ CMachineEvaluation::CMachineEvaluation(CMachine* machine, CFeatures* features, m_splitting_strategy = splitting_strategy; m_evaluation_criterion = evaluation_criterion; - SG_REF(m_machine); - SG_REF(m_features); - SG_REF(m_labels); - SG_REF(m_splitting_strategy); - SG_REF(m_evaluation_criterion); + + + + + } -CMachineEvaluation::CMachineEvaluation(CMachine* machine, CLabels* labels, - CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion, bool autolock) +MachineEvaluation::MachineEvaluation(std::shared_ptr machine, std::shared_ptr labels, + std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion, bool autolock) { init(); @@ -52,22 +52,22 @@ CMachineEvaluation::CMachineEvaluation(CMachine* machine, CLabels* labels, m_splitting_strategy = splitting_strategy; m_evaluation_criterion = evaluation_criterion; - SG_REF(m_machine); - SG_REF(m_labels); - SG_REF(m_splitting_strategy); - SG_REF(m_evaluation_criterion); + + + + } -CMachineEvaluation::~CMachineEvaluation() +MachineEvaluation::~MachineEvaluation() { - SG_UNREF(m_machine); - SG_UNREF(m_features); - SG_UNREF(m_labels); - SG_UNREF(m_splitting_strategy); - SG_UNREF(m_evaluation_criterion); + + + + + } -void CMachineEvaluation::init() +void MachineEvaluation::init() { m_machine = NULL; m_features = NULL; @@ -86,7 +86,7 @@ void CMachineEvaluation::init() "Used evaluation criterion"); } -CEvaluationResult* CMachineEvaluation::evaluate() const +std::shared_ptr MachineEvaluation::evaluate() const { SG_TRACE("entering {}::evaluate()", get_name()); @@ -105,19 +105,19 @@ CEvaluationResult* CMachineEvaluation::evaluate() const "attached", get_name()); - CEvaluationResult* result = evaluate_impl(); + auto result = evaluate_impl(); SG_TRACE("leaving {}::evaluate()", get_name()); return result; }; -CMachine* CMachineEvaluation::get_machine() const +std::shared_ptr MachineEvaluation::get_machine() const { - SG_REF(m_machine); + return m_machine; } -EEvaluationDirection CMachineEvaluation::get_evaluation_direction() const +EEvaluationDirection MachineEvaluation::get_evaluation_direction() const { return m_evaluation_criterion->get_evaluation_direction(); } diff --git a/src/shogun/evaluation/MachineEvaluation.h b/src/shogun/evaluation/MachineEvaluation.h index 22b2405d04f..a9f4cc3658a 100644 --- a/src/shogun/evaluation/MachineEvaluation.h +++ b/src/shogun/evaluation/MachineEvaluation.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Yuyu Zhang, + * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Yuyu Zhang, * Soeren Sonnenburg, Giovanni De Toni */ @@ -16,21 +16,21 @@ namespace shogun { - class CMachine; - class CFeatures; - class CLabels; - class CSplittingStrategy; - class CEvaluation; + class Machine; + class Features; + class Labels; + class SplittingStrategy; + class Evaluation; /** @brief Machine Evaluation is an abstract class * that evaluates a machine according to some criterion. * */ - class CMachineEvaluation : public CStoppableSGObject + class MachineEvaluation : public StoppableSGObject { public: - CMachineEvaluation(); + MachineEvaluation(); /** constructor * @param machine learning machine to use @@ -41,10 +41,10 @@ namespace shogun * @param autolock whether machine should be auto-locked before * evaluation */ - CMachineEvaluation( - CMachine* machine, CFeatures* features, CLabels* labels, - CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion, bool autolock = true); + MachineEvaluation( + std::shared_ptr machine, std::shared_ptr features, std::shared_ptr labels, + std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion, bool autolock = true); /** constructor, for use with custom kernels (no features) * @param machine learning machine to use @@ -53,12 +53,12 @@ namespace shogun * @param evaluation_criterion evaluation criterion to use * @param autolock autolock */ - CMachineEvaluation( - CMachine* machine, CLabels* labels, - CSplittingStrategy* splitting_strategy, - CEvaluation* evaluation_criterion, bool autolock = true); + MachineEvaluation( + std::shared_ptr machine, std::shared_ptr labels, + std::shared_ptr splitting_strategy, + std::shared_ptr evaluation_criterion, bool autolock = true); - virtual ~CMachineEvaluation(); + virtual ~MachineEvaluation(); /** @return in which direction is the best evaluation value? */ EEvaluationDirection get_evaluation_direction() const; @@ -71,10 +71,10 @@ namespace shogun * * @return result of evaluation (already SG_REF'ed) */ - virtual CEvaluationResult* evaluate() const; + virtual std::shared_ptr evaluate() const; /** @return underlying learning machine */ - CMachine* get_machine() const; + std::shared_ptr get_machine() const; protected: /** Initialize Object */ @@ -86,24 +86,24 @@ namespace shogun * before returning it. * @return the evaluation result */ - virtual CEvaluationResult* evaluate_impl() const = 0; + virtual std::shared_ptr evaluate_impl() const = 0; /** connect the machine instance to the signal handler */ protected: /** Machine to be Evaluated */ - CMachine* m_machine; + std::shared_ptr m_machine; /** Features to be used*/ - CFeatures* m_features; + std::shared_ptr m_features; /** Labels for the features */ - CLabels* m_labels; + std::shared_ptr m_labels; /** Splitting Strategy to be used */ - CSplittingStrategy* m_splitting_strategy; + std::shared_ptr m_splitting_strategy; /** Criterion for evaluation */ - CEvaluation* m_evaluation_criterion; + std::shared_ptr m_evaluation_criterion; }; } /* namespace shogun */ diff --git a/src/shogun/evaluation/MeanAbsoluteError.cpp b/src/shogun/evaluation/MeanAbsoluteError.cpp index 28aacd03cd4..6a188eee52f 100644 --- a/src/shogun/evaluation/MeanAbsoluteError.cpp +++ b/src/shogun/evaluation/MeanAbsoluteError.cpp @@ -11,7 +11,7 @@ using namespace shogun; -float64_t CMeanAbsoluteError::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t MeanAbsoluteError::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && predicted->get_label_type() == LT_REGRESSION) ASSERT(ground_truth && ground_truth->get_label_type() == LT_REGRESSION) @@ -19,8 +19,11 @@ float64_t CMeanAbsoluteError::evaluate(CLabels* predicted, CLabels* ground_truth ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels()) int32_t length = predicted->get_num_labels(); float64_t mae = 0.0; + + auto rp = regression_labels(predicted); + auto rgt = regression_labels(ground_truth); for (int32_t i=0; iget_label(i) - ((CRegressionLabels*) ground_truth)->get_label(i)); + mae += Math::abs(rp->get_label(i) - rgt->get_label(i)); mae /= length; return mae; } diff --git a/src/shogun/evaluation/MeanAbsoluteError.h b/src/shogun/evaluation/MeanAbsoluteError.h index 70da8d5f4eb..78092d8a754 100644 --- a/src/shogun/evaluation/MeanAbsoluteError.h +++ b/src/shogun/evaluation/MeanAbsoluteError.h @@ -16,7 +16,7 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Class MeanAbsoluteError * used to compute an error of regression model. @@ -29,21 +29,21 @@ class CLabels; * \f] * */ -class CMeanAbsoluteError: public CEvaluation +class MeanAbsoluteError: public Evaluation { public: /** constructor */ - CMeanAbsoluteError() : CEvaluation() {}; + MeanAbsoluteError() : Evaluation() {}; /** destructor */ - virtual ~CMeanAbsoluteError() {}; + virtual ~MeanAbsoluteError() {}; /** evaluate mean absolute error * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct * @return mean absolute error */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); inline EEvaluationDirection get_evaluation_direction() const { diff --git a/src/shogun/evaluation/MeanSquaredError.cpp b/src/shogun/evaluation/MeanSquaredError.cpp index 37f6cf9448e..829f33b13a0 100644 --- a/src/shogun/evaluation/MeanSquaredError.cpp +++ b/src/shogun/evaluation/MeanSquaredError.cpp @@ -11,7 +11,7 @@ using namespace shogun; -float64_t CMeanSquaredError::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t MeanSquaredError::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { require(predicted, "Predicted labels must be not null."); require(ground_truth, "Ground truth labels must be not null."); @@ -22,7 +22,7 @@ float64_t CMeanSquaredError::evaluate(CLabels* predicted, CLabels* ground_truth) auto ground_truth_regression = regression_labels(ground_truth); for (int32_t i=0; iget_label(i) - ground_truth_regression->get_label(i)); mse /= length; diff --git a/src/shogun/evaluation/MeanSquaredError.h b/src/shogun/evaluation/MeanSquaredError.h index 9e1cc0eff64..16c8ebb6d9f 100644 --- a/src/shogun/evaluation/MeanSquaredError.h +++ b/src/shogun/evaluation/MeanSquaredError.h @@ -16,7 +16,7 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Class MeanSquaredError * used to compute an error of regression model. @@ -29,21 +29,21 @@ class CLabels; * \f] * */ -class CMeanSquaredError: public CEvaluation +class MeanSquaredError: public Evaluation { public: /** constructor */ - CMeanSquaredError() : CEvaluation() {}; + MeanSquaredError() : Evaluation() {}; /** destructor */ - virtual ~CMeanSquaredError() {}; + virtual ~MeanSquaredError() {}; /** evaluate mean squared error * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct * @return mean squared error */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); inline EEvaluationDirection get_evaluation_direction() const { diff --git a/src/shogun/evaluation/MeanSquaredLogError.cpp b/src/shogun/evaluation/MeanSquaredLogError.cpp index e173526488c..1830267687c 100644 --- a/src/shogun/evaluation/MeanSquaredLogError.cpp +++ b/src/shogun/evaluation/MeanSquaredLogError.cpp @@ -11,7 +11,7 @@ using namespace shogun; -float64_t CMeanSquaredLogError::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t MeanSquaredLogError::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && ground_truth) ASSERT(predicted->get_num_labels()==ground_truth->get_num_labels()) @@ -20,10 +20,12 @@ float64_t CMeanSquaredLogError::evaluate(CLabels* predicted, CLabels* ground_tru int32_t length=predicted->get_num_labels(); float64_t msle=0.0; + auto predicted_regression = regression_labels(predicted); + auto ground_truth_regression = regression_labels(ground_truth); for (int32_t i=0; iget_label(i); - float64_t truth=((CRegressionLabels*) ground_truth)->get_label(i); + float64_t prediction=predicted_regression->get_label(i); + float64_t truth=ground_truth_regression->get_label(i); if (prediction<=-1.0 || truth<=-1.0) { @@ -34,7 +36,7 @@ float64_t CMeanSquaredLogError::evaluate(CLabels* predicted, CLabels* ground_tru float64_t a = std::log(prediction + 1); float64_t b = std::log(truth + 1); - msle+=CMath::sq(a-b); + msle+=Math::sq(a-b); } msle /= length; return std::sqrt(msle); diff --git a/src/shogun/evaluation/MeanSquaredLogError.h b/src/shogun/evaluation/MeanSquaredLogError.h index cd8ac7ac594..82122dd8142 100644 --- a/src/shogun/evaluation/MeanSquaredLogError.h +++ b/src/shogun/evaluation/MeanSquaredLogError.h @@ -16,7 +16,7 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Class CMeanSquaredLogError * used to compute an error of regression model. @@ -29,21 +29,21 @@ class CLabels; * \f] * */ -class CMeanSquaredLogError: public CEvaluation +class MeanSquaredLogError: public Evaluation { public: /** constructor */ - CMeanSquaredLogError() : CEvaluation() {}; + MeanSquaredLogError() : Evaluation() {}; /** destructor */ - virtual ~CMeanSquaredLogError() {}; + virtual ~MeanSquaredLogError() {}; /** evaluate mean squared log error * @param predicted labels for evaluating * @param ground_truth labels assumed to be correct * @return mean squared error */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); inline EEvaluationDirection get_evaluation_direction() const { diff --git a/src/shogun/evaluation/MulticlassAccuracy.cpp b/src/shogun/evaluation/MulticlassAccuracy.cpp index 0597369d50b..737130d769b 100644 --- a/src/shogun/evaluation/MulticlassAccuracy.cpp +++ b/src/shogun/evaluation/MulticlassAccuracy.cpp @@ -11,7 +11,7 @@ using namespace shogun; -float64_t CMulticlassAccuracy::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t MulticlassAccuracy::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && ground_truth) ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels()) @@ -48,19 +48,22 @@ float64_t CMulticlassAccuracy::evaluate(CLabels* predicted, CLabels* ground_trut return 0.0; } -SGMatrix CMulticlassAccuracy::get_confusion_matrix(CLabels* predicted, CLabels* ground_truth) +SGMatrix MulticlassAccuracy::get_confusion_matrix(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels()) int32_t length = ground_truth->get_num_labels(); - int32_t num_classes = ((CMulticlassLabels*) ground_truth)->get_num_classes(); + auto predicted_mc = multiclass_labels(predicted); + auto ground_truth_mc = multiclass_labels(ground_truth); + int32_t num_classes = ground_truth_mc->get_num_classes(); SGMatrix confusion_matrix(num_classes, num_classes); memset(confusion_matrix.matrix,0,sizeof(int32_t)*num_classes*num_classes); + for (int32_t i=0; iget_int_label(i); - int32_t ground_truth_label = ((CMulticlassLabels*) ground_truth)->get_int_label(i); + int32_t predicted_label = predicted_mc->get_int_label(i); + int32_t ground_truth_label = ground_truth_mc->get_int_label(i); - if (predicted_label==((CMulticlassLabels*) predicted)->REJECTION_LABEL) + if (predicted_label==predicted_mc->REJECTION_LABEL) continue; confusion_matrix[predicted_label*num_classes+ground_truth_label]++; diff --git a/src/shogun/evaluation/MulticlassAccuracy.h b/src/shogun/evaluation/MulticlassAccuracy.h index c9fd107c961..02faa130851 100644 --- a/src/shogun/evaluation/MulticlassAccuracy.h +++ b/src/shogun/evaluation/MulticlassAccuracy.h @@ -16,7 +16,7 @@ namespace shogun { -class CLabels; +class Labels; /** @brief The class MulticlassAccuracy * used to compute accuracy of multiclass classification. @@ -30,33 +30,33 @@ class CLabels; * * */ -class CMulticlassAccuracy: public CEvaluation +class MulticlassAccuracy: public Evaluation { public: /** constructor */ - CMulticlassAccuracy() : - CEvaluation(), m_ignore_rejects(false), m_rejects_num(0) {}; + MulticlassAccuracy() : + Evaluation(), m_ignore_rejects(false), m_rejects_num(0) {}; /** constructor */ - CMulticlassAccuracy(bool ignore_rejects) : - CEvaluation(), m_ignore_rejects(ignore_rejects), m_rejects_num(0) {}; + MulticlassAccuracy(bool ignore_rejects) : + Evaluation(), m_ignore_rejects(ignore_rejects), m_rejects_num(0) {}; /** destructor */ - virtual ~CMulticlassAccuracy() {}; + virtual ~MulticlassAccuracy() {}; /** evaluate accuracy * @param predicted labels to be evaluated * @param ground_truth labels assumed to be correct * @return accuracy */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); /** constructs confusion matrix for multiclass classification * @param predicted labels to be evaluated * @param ground_truth labels assumed to be correct * @return confusion matrix */ - static SGMatrix get_confusion_matrix(CLabels* predicted, CLabels* ground_truth); + static SGMatrix get_confusion_matrix(std::shared_ptr predicted, std::shared_ptr ground_truth); inline EEvaluationDirection get_evaluation_direction() const { diff --git a/src/shogun/evaluation/MulticlassOVREvaluation.cpp b/src/shogun/evaluation/MulticlassOVREvaluation.cpp index ac118782546..c3c46ccd3a4 100644 --- a/src/shogun/evaluation/MulticlassOVREvaluation.cpp +++ b/src/shogun/evaluation/MulticlassOVREvaluation.cpp @@ -12,39 +12,34 @@ using namespace shogun; -CMulticlassOVREvaluation::CMulticlassOVREvaluation() : - CMulticlassOVREvaluation(nullptr) +MulticlassOVREvaluation::MulticlassOVREvaluation() : + MulticlassOVREvaluation(nullptr) { } -CMulticlassOVREvaluation::CMulticlassOVREvaluation(CBinaryClassEvaluation* binary_evaluation) : - CEvaluation(), m_binary_evaluation(nullptr), m_graph_results(nullptr), m_num_graph_results(0) +MulticlassOVREvaluation::MulticlassOVREvaluation(std::shared_ptr binary_evaluation) : + Evaluation(), m_binary_evaluation(nullptr), m_graph_results(nullptr), m_num_graph_results(0) { - SG_ADD((CEvaluation**)&m_binary_evaluation, "binary_evaluation", "binary evaluator") + SG_ADD((std::shared_ptr*)&m_binary_evaluation, "binary_evaluation", "binary evaluator") } -CMulticlassOVREvaluation::~CMulticlassOVREvaluation() +MulticlassOVREvaluation::~MulticlassOVREvaluation() { if (m_graph_results) { SG_FREE(m_graph_results); } - - if (m_binary_evaluation) - { - SG_UNREF(m_binary_evaluation); - } } -float64_t CMulticlassOVREvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t MulticlassOVREvaluation::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(m_binary_evaluation) ASSERT(predicted) ASSERT(ground_truth) int32_t n_labels = predicted->get_num_labels(); ASSERT(n_labels) - CMulticlassLabels* predicted_mc = (CMulticlassLabels*)predicted; - CMulticlassLabels* ground_truth_mc = (CMulticlassLabels*)ground_truth; + auto predicted_mc = multiclass_labels(predicted); + auto ground_truth_mc = multiclass_labels(ground_truth); int32_t n_classes = predicted_mc->get_multiclass_confidences(0).size(); ASSERT(n_classes>0) m_last_results = SGVector(n_classes); @@ -58,7 +53,7 @@ float64_t CMulticlassOVREvaluation::evaluate(CLabels* predicted, CLabels* ground all(i,j) = confs[j]; } } - if (dynamic_cast(m_binary_evaluation) || dynamic_cast(m_binary_evaluation)) + if (std::dynamic_pointer_cast(m_binary_evaluation) || std::dynamic_pointer_cast(m_binary_evaluation)) { for (int32_t i=0; i(); @@ -68,7 +63,7 @@ float64_t CMulticlassOVREvaluation::evaluate(CLabels* predicted, CLabels* ground } for (int32_t c=0; c(all.get_column_vector(c),n_labels,false)); + auto pred = std::make_shared(SGVector(all.get_column_vector(c),n_labels,false)); SGVector gt_vec(n_labels); for (int32_t i=0; i(gt_vec); m_last_results[c] = m_binary_evaluation->evaluate(pred, gt); - if (dynamic_cast(m_binary_evaluation)) + if (std::dynamic_pointer_cast(m_binary_evaluation)) { new (&m_graph_results[c]) SGMatrix(); - m_graph_results[c] = ((CROCEvaluation*)m_binary_evaluation)->get_ROC(); + m_graph_results[c] = (std::static_pointer_cast(m_binary_evaluation))->get_ROC(); } - if (dynamic_cast(m_binary_evaluation)) + if (std::dynamic_pointer_cast(m_binary_evaluation)) { new (&m_graph_results[c]) SGMatrix(); - m_graph_results[c] = ((CPRCEvaluation*)m_binary_evaluation)->get_PRC(); + m_graph_results[c] = (std::static_pointer_cast(m_binary_evaluation))->get_PRC(); } } - return CStatistics::mean(m_last_results); + return Statistics::mean(m_last_results); } diff --git a/src/shogun/evaluation/MulticlassOVREvaluation.h b/src/shogun/evaluation/MulticlassOVREvaluation.h index 1975150b52d..8f9418f6169 100644 --- a/src/shogun/evaluation/MulticlassOVREvaluation.h +++ b/src/shogun/evaluation/MulticlassOVREvaluation.h @@ -16,7 +16,7 @@ namespace shogun { -class CLabels; +class Labels; /** @brief The class MulticlassOVREvaluation * used to compute evaluation parameters @@ -24,24 +24,24 @@ class CLabels; * binary OvR decomposition and given binary * evaluation technique. */ -class CMulticlassOVREvaluation: public CEvaluation +class MulticlassOVREvaluation: public Evaluation { public: /** constructor */ - CMulticlassOVREvaluation(); + MulticlassOVREvaluation(); /** constructor */ - CMulticlassOVREvaluation(CBinaryClassEvaluation* binary_evaluation); + MulticlassOVREvaluation(std::shared_ptr binary_evaluation); /** destructor */ - virtual ~CMulticlassOVREvaluation(); + virtual ~MulticlassOVREvaluation(); /** evaluate accuracy * @param predicted labels to be evaluated * @param ground_truth labels assumed to be correct * @return mean of OvR binary evaluations */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); /** returns last results per class */ SGVector get_last_results() @@ -70,7 +70,7 @@ class CMulticlassOVREvaluation: public CEvaluation protected: /** binary evaluation to be used */ - CBinaryClassEvaluation* m_binary_evaluation; + std::shared_ptr m_binary_evaluation; /** last per class results */ SGVector m_last_results; diff --git a/src/shogun/evaluation/MultilabelAccuracy.cpp b/src/shogun/evaluation/MultilabelAccuracy.cpp index 93d8ae2fe5d..ed218ce589b 100644 --- a/src/shogun/evaluation/MultilabelAccuracy.cpp +++ b/src/shogun/evaluation/MultilabelAccuracy.cpp @@ -10,17 +10,17 @@ using namespace shogun; -CMultilabelAccuracy::CMultilabelAccuracy() - : CEvaluation() +MultilabelAccuracy::MultilabelAccuracy() + : Evaluation() { } -CMultilabelAccuracy::~CMultilabelAccuracy() +MultilabelAccuracy::~MultilabelAccuracy() { } -float64_t CMultilabelAccuracy::evaluate(CLabels* predicted, - CLabels* ground_truth) +float64_t MultilabelAccuracy::evaluate(std::shared_ptr predicted, + std::shared_ptr ground_truth) { require(predicted->get_label_type() == LT_SPARSE_MULTILABEL, "predicted label should be of multilabels type"); @@ -29,8 +29,8 @@ float64_t CMultilabelAccuracy::evaluate(CLabels* predicted, require(ground_truth->get_label_type() == predicted->get_label_type(), "predicted labels and actual labels should be of same type"); - CMultilabelLabels* m_predicted = (CMultilabelLabels*) predicted; - CMultilabelLabels* m_ground_truth = (CMultilabelLabels*) ground_truth; + auto m_predicted = std::static_pointer_cast(predicted); + auto m_ground_truth = std::static_pointer_cast(ground_truth); require(m_predicted->get_num_labels() == m_ground_truth->get_num_labels(), "predicted labels and actual labels should have same number of labels"); diff --git a/src/shogun/evaluation/MultilabelAccuracy.h b/src/shogun/evaluation/MultilabelAccuracy.h index 0eac73b4ec3..219967e2648 100644 --- a/src/shogun/evaluation/MultilabelAccuracy.h +++ b/src/shogun/evaluation/MultilabelAccuracy.h @@ -26,14 +26,14 @@ namespace shogun * h(x_i)|}$ * \f] */ -class CMultilabelAccuracy : public CEvaluation +class MultilabelAccuracy : public Evaluation { public: /** default constructor */ - CMultilabelAccuracy(); + MultilabelAccuracy(); /** destructor */ - virtual ~CMultilabelAccuracy(); + virtual ~MultilabelAccuracy(); /** evaluate accuracy * @@ -42,7 +42,7 @@ class CMultilabelAccuracy : public CEvaluation * * @return accuracy */ - virtual float64_t evaluate(CLabels * predicted, CLabels * ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); inline EEvaluationDirection get_evaluation_direction() const { diff --git a/src/shogun/evaluation/PRCEvaluation.cpp b/src/shogun/evaluation/PRCEvaluation.cpp index eb50572714b..cc3547182c2 100644 --- a/src/shogun/evaluation/PRCEvaluation.cpp +++ b/src/shogun/evaluation/PRCEvaluation.cpp @@ -12,21 +12,21 @@ using namespace shogun; -CPRCEvaluation::CPRCEvaluation() : CBinaryClassEvaluation(), m_computed(false) +PRCEvaluation::PRCEvaluation() : BinaryClassEvaluation(), m_computed(false) { m_PRC_graph = SGMatrix(); m_thresholds = SGVector(); m_auPRC = 0.0; - watch_method("PRC", &CPRCEvaluation::get_PRC); - watch_method("thresholds", &CPRCEvaluation::get_thresholds); - watch_method("auPRC", &CPRCEvaluation::get_auPRC); + watch_method("PRC", &PRCEvaluation::get_PRC); + watch_method("thresholds", &PRCEvaluation::get_thresholds); + watch_method("auPRC", &PRCEvaluation::get_auPRC); }; -CPRCEvaluation::~CPRCEvaluation() +PRCEvaluation::~PRCEvaluation() { } -float64_t CPRCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t PRCEvaluation::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && ground_truth) ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels()) @@ -53,7 +53,7 @@ float64_t CPRCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) idxs[i] = i; // sort indexes by labels ascending - CMath::qsort_backward_index(labels, idxs, length); + Math::qsort_backward_index(labels, idxs, length); // clean and initialize graph and auPRC SG_FREE(labels); @@ -87,7 +87,7 @@ float64_t CPRCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) } // calc auRPC using area under curve - m_auPRC = CMath::area_under_curve(m_PRC_graph.matrix, length, true); + m_auPRC = Math::area_under_curve(m_PRC_graph.matrix, length, true); // set computed indicator m_computed = true; @@ -96,7 +96,7 @@ float64_t CPRCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) return m_auPRC; } -SGMatrix CPRCEvaluation::get_PRC() const +SGMatrix PRCEvaluation::get_PRC() const { if (!m_computed) error("Uninitialized, please call evaluate first"); @@ -104,7 +104,7 @@ SGMatrix CPRCEvaluation::get_PRC() const return m_PRC_graph; } -SGVector CPRCEvaluation::get_thresholds() const +SGVector PRCEvaluation::get_thresholds() const { if (!m_computed) error("Uninitialized, please call evaluate first"); @@ -112,7 +112,7 @@ SGVector CPRCEvaluation::get_thresholds() const return m_thresholds; } -float64_t CPRCEvaluation::get_auPRC() const +float64_t PRCEvaluation::get_auPRC() const { if (!m_computed) error("Uninitialized, please call evaluate first"); diff --git a/src/shogun/evaluation/PRCEvaluation.h b/src/shogun/evaluation/PRCEvaluation.h index 49115b1fb55..ba0180fb4ae 100644 --- a/src/shogun/evaluation/PRCEvaluation.h +++ b/src/shogun/evaluation/PRCEvaluation.h @@ -15,20 +15,20 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Class PRCEvaluation used to evaluate PRC * (Precision Recall Curve) and an area under PRC curve (auPRC). * */ -class CPRCEvaluation: public CBinaryClassEvaluation +class PRCEvaluation: public BinaryClassEvaluation { public: /** constructor */ - CPRCEvaluation(); + PRCEvaluation(); /** destructor */ - virtual ~CPRCEvaluation(); + virtual ~PRCEvaluation(); /** get name */ virtual const char* get_name() const { return "PRCEvaluation"; }; @@ -38,7 +38,7 @@ class CPRCEvaluation: public CBinaryClassEvaluation * @param ground_truth labels assumed to be correct * @return auPRC */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); inline EEvaluationDirection get_evaluation_direction() const { diff --git a/src/shogun/evaluation/ROCEvaluation.cpp b/src/shogun/evaluation/ROCEvaluation.cpp index 39f029203a4..f1d45f51a1f 100644 --- a/src/shogun/evaluation/ROCEvaluation.cpp +++ b/src/shogun/evaluation/ROCEvaluation.cpp @@ -10,20 +10,20 @@ using namespace shogun; -CROCEvaluation::CROCEvaluation() : CBinaryClassEvaluation(), m_computed(false) +ROCEvaluation::ROCEvaluation() : BinaryClassEvaluation(), m_computed(false) { m_ROC_graph = SGMatrix(); m_thresholds = SGVector(); - watch_method("auROC", &CROCEvaluation::get_auROC); - watch_method("ROC", &CROCEvaluation::get_ROC); - watch_method("thresholds", &CROCEvaluation::get_thresholds); + watch_method("auROC", &ROCEvaluation::get_auROC); + watch_method("ROC", &ROCEvaluation::get_ROC); + watch_method("thresholds", &ROCEvaluation::get_thresholds); } -CROCEvaluation::~CROCEvaluation() +ROCEvaluation::~ROCEvaluation() { } -float64_t CROCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) +float64_t ROCEvaluation::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { require(predicted, "No predicted labels provided."); require(ground_truth, "No ground truth labels provided."); @@ -36,19 +36,17 @@ float64_t CROCEvaluation::evaluate(CLabels* predicted, CLabels* ground_truth) "Given ground truth labels ({}) must be binary ({}).", ground_truth->get_label_type(), LT_BINARY); - return evaluate_roc( - (CBinaryLabels*)predicted, (CBinaryLabels*)ground_truth); + return evaluate_roc(binary_labels(predicted),binary_labels(ground_truth)); } -float64_t CROCEvaluation::evaluate_roc( - CBinaryLabels* predicted, CBinaryLabels* ground_truth) +float64_t ROCEvaluation::evaluate_roc(std::shared_ptr predicted, std::shared_ptr ground_truth) { ASSERT(predicted && ground_truth) ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels()) ground_truth->ensure_valid(); // assume threshold as negative infinity - float64_t threshold = CMath::ALMOST_NEG_INFTY; + float64_t threshold = Math::ALMOST_NEG_INFTY; // false positive rate float64_t fp = 0.0; // true positive rate @@ -72,7 +70,7 @@ float64_t CROCEvaluation::evaluate_roc( for (i = 0; i < length; i++) idxs[i] = i; - CMath::qsort_backward_index(labels, idxs.vector, idxs.vlen); + Math::qsort_backward_index(labels, idxs.vector, idxs.vlen); // number of different predicted labels int32_t diff_count = 1; @@ -142,14 +140,14 @@ float64_t CROCEvaluation::evaluate_roc( // calc auROC using area under curve m_auROC = - CMath::area_under_curve(m_ROC_graph.matrix, diff_count + 1, false); + Math::area_under_curve(m_ROC_graph.matrix, diff_count + 1, false); m_computed = true; return m_auROC; } -SGMatrix CROCEvaluation::get_ROC() const +SGMatrix ROCEvaluation::get_ROC() const { if (!m_computed) error("Uninitialized, please call evaluate first"); @@ -157,7 +155,7 @@ SGMatrix CROCEvaluation::get_ROC() const return m_ROC_graph; } -SGVector CROCEvaluation::get_thresholds() const +SGVector ROCEvaluation::get_thresholds() const { if (!m_computed) error("Uninitialized, please call evaluate first"); @@ -165,7 +163,7 @@ SGVector CROCEvaluation::get_thresholds() const return m_thresholds; } -float64_t CROCEvaluation::get_auROC() const +float64_t ROCEvaluation::get_auROC() const { if (!m_computed) error("Uninitialized, please call evaluate first"); diff --git a/src/shogun/evaluation/ROCEvaluation.h b/src/shogun/evaluation/ROCEvaluation.h index ab7b207bdba..79646199666 100644 --- a/src/shogun/evaluation/ROCEvaluation.h +++ b/src/shogun/evaluation/ROCEvaluation.h @@ -15,7 +15,7 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Class ROCEvalution used to evaluate ROC * (Receiver Operating Characteristic) and an area @@ -26,14 +26,14 @@ class CLabels; * Fawcett, Tom (2004) ROC Graphs: * Notes and Practical Considerations for Researchers; Machine Learning, 2004 */ -class CROCEvaluation: public CBinaryClassEvaluation +class ROCEvaluation: public BinaryClassEvaluation { public: /** constructor */ - CROCEvaluation(); + ROCEvaluation(); /** destructor */ - virtual ~CROCEvaluation(); + virtual ~ROCEvaluation(); /** get name */ virtual const char* get_name() const { return "ROCEvaluation"; }; @@ -43,7 +43,7 @@ class CROCEvaluation: public CBinaryClassEvaluation * @param ground_truth labels assumed to be correct * @return auROC */ - virtual float64_t evaluate(CLabels* predicted, CLabels* ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); virtual EEvaluationDirection get_evaluation_direction() const { @@ -72,7 +72,7 @@ class CROCEvaluation: public CBinaryClassEvaluation * @param ground_truth labels assumed to be correct * @return auROC */ - float64_t evaluate_roc(CBinaryLabels* predicted, CBinaryLabels* ground_truth); + float64_t evaluate_roc(std::shared_ptr predicted, std::shared_ptr ground_truth); protected: diff --git a/src/shogun/evaluation/SigmoidCalibration.cpp b/src/shogun/evaluation/SigmoidCalibration.cpp index 22a2a2fae4e..4b368434463 100644 --- a/src/shogun/evaluation/SigmoidCalibration.cpp +++ b/src/shogun/evaluation/SigmoidCalibration.cpp @@ -11,16 +11,16 @@ using namespace shogun; -CSigmoidCalibration::CSigmoidCalibration() : CCalibration() +SigmoidCalibration::SigmoidCalibration() : Calibration() { init(); } -CSigmoidCalibration::~CSigmoidCalibration() +SigmoidCalibration::~SigmoidCalibration() { } -void CSigmoidCalibration::init() +void SigmoidCalibration::init() { m_maxiter = 100; m_minstep = 1E-10; @@ -43,52 +43,52 @@ void CSigmoidCalibration::init() SG_ADD(&m_epsilon, "m_epsilon", "Stopping criteria."); } -void CSigmoidCalibration::set_maxiter(index_t maxiter) +void SigmoidCalibration::set_maxiter(index_t maxiter) { m_maxiter = maxiter; } -index_t CSigmoidCalibration::get_maxiter() +index_t SigmoidCalibration::get_maxiter() { return m_maxiter; } -void CSigmoidCalibration::set_minstep(float64_t minstep) +void SigmoidCalibration::set_minstep(float64_t minstep) { m_minstep = minstep; } -float64_t CSigmoidCalibration::get_minstep() +float64_t SigmoidCalibration::get_minstep() { return m_minstep; } -void CSigmoidCalibration::set_sigma(float64_t sigma) +void SigmoidCalibration::set_sigma(float64_t sigma) { m_sigma = sigma; } -float64_t CSigmoidCalibration::get_sigma() +float64_t SigmoidCalibration::get_sigma() { return m_sigma; } -void CSigmoidCalibration::set_epsilon(float64_t epsilon) +void SigmoidCalibration::set_epsilon(float64_t epsilon) { m_epsilon = epsilon; } -float64_t CSigmoidCalibration::get_epsilon() +float64_t SigmoidCalibration::get_epsilon() { return m_epsilon; } -bool CSigmoidCalibration::fit_binary( - CBinaryLabels* predictions, CBinaryLabels* targets) +bool SigmoidCalibration::fit_binary( + std::shared_ptr predictions, std::shared_ptr targets) { m_sigmoid_as.resize_vector(1); m_sigmoid_bs.resize_vector(1); - auto sigmoid_params = CStatistics::fit_sigmoid( + auto sigmoid_params = Statistics::fit_sigmoid( predictions->get_values(), targets->get_labels(), m_maxiter, m_minstep, m_sigma, m_epsilon); @@ -98,24 +98,22 @@ bool CSigmoidCalibration::fit_binary( return true; } -CBinaryLabels* CSigmoidCalibration::calibrate_binary(CBinaryLabels* predictions) +std::shared_ptr SigmoidCalibration::calibrate_binary(std::shared_ptr predictions) { require( m_sigmoid_as.size() == 1, "Parameters not fitted, which need to be fitted before calibrating."); - CStatistics::SigmoidParamters params; + Statistics::SigmoidParamters params; params.a = m_sigmoid_as[0]; params.b = m_sigmoid_bs[0]; auto probabilities = calibrate_values(predictions->get_values(), params); - CBinaryLabels* calibrated_predictions = new CBinaryLabels(probabilities); - - return calibrated_predictions; + return std::make_shared(probabilities); } -bool CSigmoidCalibration::fit_multiclass( - CMulticlassLabels* predictions, CMulticlassLabels* targets) +bool SigmoidCalibration::fit_multiclass( + std::shared_ptr predictions, std::shared_ptr targets) { index_t num_classes = predictions->get_num_classes(); @@ -128,20 +126,20 @@ bool CSigmoidCalibration::fit_multiclass( auto pred_values = predictions->get_confidences_for_class(i); auto target_labels = class_targets->get_labels(); - auto sigmoid_params = CStatistics::fit_sigmoid( + auto sigmoid_params = Statistics::fit_sigmoid( pred_values, target_labels, m_maxiter, m_minstep, m_sigma, m_epsilon); m_sigmoid_as[i] = sigmoid_params.a; m_sigmoid_bs[i] = sigmoid_params.b; - SG_UNREF(class_targets); + } return true; } -CMulticlassLabels* -CSigmoidCalibration::calibrate_multiclass(CMulticlassLabels* predictions) +std::shared_ptr +SigmoidCalibration::calibrate_multiclass(std::shared_ptr predictions) { index_t num_classes = predictions->get_num_classes(); index_t num_samples = predictions->get_num_labels(); @@ -156,7 +154,7 @@ CSigmoidCalibration::calibrate_multiclass(CMulticlassLabels* predictions) { auto class_values = predictions->get_confidences_for_class(i); - CStatistics::SigmoidParamters sigmoid_params; + Statistics::SigmoidParamters sigmoid_params; sigmoid_params.a = m_sigmoid_as[i]; sigmoid_params.b = m_sigmoid_bs[i]; @@ -166,7 +164,7 @@ CSigmoidCalibration::calibrate_multiclass(CMulticlassLabels* predictions) confidence_values.set_column(i, calibrated_values); } - auto result_labels = new CMulticlassLabels(num_samples); + auto result_labels = std::make_shared(num_samples); result_labels->allocate_confidences_for(num_classes); /** Normalize the probabilities. */ @@ -185,8 +183,8 @@ CSigmoidCalibration::calibrate_multiclass(CMulticlassLabels* predictions) return result_labels; } -SGVector CSigmoidCalibration::calibrate_values( - SGVector values, CStatistics::SigmoidParamters params) +SGVector SigmoidCalibration::calibrate_values( + SGVector values, Statistics::SigmoidParamters params) { /** Calibrate values by passing them to a sigmoid function. */ for (index_t i = 0; i < values.vlen; ++i) @@ -196,4 +194,4 @@ SGVector CSigmoidCalibration::calibrate_values( : 1.0 / (1 + std::exp(fApB)); } return values; -} \ No newline at end of file +} diff --git a/src/shogun/evaluation/SigmoidCalibration.h b/src/shogun/evaluation/SigmoidCalibration.h index 899f8df1514..c4abc39fb04 100644 --- a/src/shogun/evaluation/SigmoidCalibration.h +++ b/src/shogun/evaluation/SigmoidCalibration.h @@ -24,14 +24,14 @@ namespace shogun * comparisons to regularized likelihood methods. * Advances in large margin classifiers. 1999 */ - class CSigmoidCalibration : public CCalibration + class SigmoidCalibration : public Calibration { public: /** Constructor. */ - CSigmoidCalibration(); + SigmoidCalibration(); /** Destructor. */ - virtual ~CSigmoidCalibration(); + virtual ~SigmoidCalibration(); /** Get name. */ virtual const char* get_name() const @@ -45,14 +45,14 @@ namespace shogun * @return Indicates whether the calibration was succesful **/ virtual bool - fit_binary(CBinaryLabels* predictions, CBinaryLabels* targets); + fit_binary(std::shared_ptr predictions, std::shared_ptr targets); /** Calibrate binary predictions based on parameters learned by calling *fit. * @param predictions The predictions outputted by the machine * @return Calibrated binary labels **/ - virtual CBinaryLabels* calibrate_binary(CBinaryLabels* predictions); + virtual std::shared_ptr calibrate_binary(std::shared_ptr predictions); /** Fit calibration parameters for multiclass labels. Fits sigmoid * parameters for each class seperately. @@ -61,7 +61,7 @@ namespace shogun * @return Indicates whether the calibration was succesful **/ virtual bool fit_multiclass( - CMulticlassLabels* predictions, CMulticlassLabels* targets); + std::shared_ptr predictions, std::shared_ptr targets); /** Calibrate multiclass predictions based on parameters learned by *calling fit. @@ -69,8 +69,8 @@ namespace shogun * @param predictions The predictions outputted by the machine * @return Calibrated binary labels **/ - virtual CMulticlassLabels* - calibrate_multiclass(CMulticlassLabels* predictions); + virtual std::shared_ptr + calibrate_multiclass(std::shared_ptr predictions); /** Set maximum number of iterations * @param maxiter maximum number of iterations @@ -123,7 +123,7 @@ namespace shogun * @param params The sigmoid paramters to be used for calibration */ SGVector calibrate_values( - SGVector values, CStatistics::SigmoidParamters params); + SGVector values, Statistics::SigmoidParamters params); private: /** Stores parameter A of sigmoid for each class. In case of binary diff --git a/src/shogun/evaluation/SplittingStrategy.cpp b/src/shogun/evaluation/SplittingStrategy.cpp index f74ac6616ce..e2f77051e65 100644 --- a/src/shogun/evaluation/SplittingStrategy.cpp +++ b/src/shogun/evaluation/SplittingStrategy.cpp @@ -9,12 +9,12 @@ using namespace shogun; -CSplittingStrategy::CSplittingStrategy() +SplittingStrategy::SplittingStrategy() { init(); } -CSplittingStrategy::CSplittingStrategy(CLabels* labels, int32_t num_subsets) +SplittingStrategy::SplittingStrategy(std::shared_ptr labels, int32_t num_subsets) { init(); @@ -28,31 +28,27 @@ CSplittingStrategy::CSplittingStrategy(CLabels* labels, int32_t num_subsets) } m_labels=labels; - SG_REF(m_labels); + reset_subsets(); } -void CSplittingStrategy::reset_subsets() +void SplittingStrategy::reset_subsets() { - if (m_subset_indices) - SG_UNREF(m_subset_indices); - - m_subset_indices=new CDynamicObjectArray(); - SG_REF(m_subset_indices); + m_subset_indices=std::make_shared(m_num_subsets); /* construct all arrays */ for (index_t i=0; iappend_element(new CDynamicArray ()); + m_subset_indices->append_element(std::make_shared>()); m_is_filled=false; } -void CSplittingStrategy::init() +void SplittingStrategy::init() { m_labels=NULL; m_subset_indices=NULL; - SG_REF(m_subset_indices); + m_is_filled=false; m_num_subsets=0; @@ -65,13 +61,13 @@ void CSplittingStrategy::init() &m_num_subsets, "num_subsets", "Number of index sets"); } -CSplittingStrategy::~CSplittingStrategy() +SplittingStrategy::~SplittingStrategy() { - SG_UNREF(m_labels); - SG_UNREF(m_subset_indices); + + } -SGVector CSplittingStrategy::generate_subset_indices(index_t subset_idx) const +SGVector SplittingStrategy::generate_subset_indices(index_t subset_idx) const { if (!m_is_filled) { @@ -81,8 +77,7 @@ SGVector CSplittingStrategy::generate_subset_indices(index_t subset_idx } /* construct SGVector copy from index vector */ - CDynamicArray* to_copy=(CDynamicArray*) - m_subset_indices->get_element_safe(subset_idx); + auto to_copy=m_subset_indices->get_element_safe>(subset_idx); index_t num_elements=to_copy->get_num_elements(); SGVector result(num_elements, true); @@ -90,12 +85,12 @@ SGVector CSplittingStrategy::generate_subset_indices(index_t subset_idx /* copy data */ sg_memcpy(result.vector, to_copy->get_array(), sizeof(index_t)*num_elements); - SG_UNREF(to_copy); + return result; } -SGVector CSplittingStrategy::generate_subset_inverse(index_t subset_idx) const +SGVector SplittingStrategy::generate_subset_inverse(index_t subset_idx) const { if (!m_is_filled) { @@ -104,8 +99,7 @@ SGVector CSplittingStrategy::generate_subset_inverse(index_t subset_idx get_name(), get_name()); } - CDynamicArray* to_invert=(CDynamicArray*) - m_subset_indices->get_element_safe(subset_idx); + auto to_invert=m_subset_indices->get_element_safe>(subset_idx); SGVector result( m_labels->get_num_labels()-to_invert->get_num_elements(), true); @@ -118,12 +112,12 @@ SGVector CSplittingStrategy::generate_subset_inverse(index_t subset_idx result.vector[index++]=i; } - SG_UNREF(to_invert); + return result; } -index_t CSplittingStrategy::get_num_subsets() const +index_t SplittingStrategy::get_num_subsets() const { return m_num_subsets; } diff --git a/src/shogun/evaluation/SplittingStrategy.h b/src/shogun/evaluation/SplittingStrategy.h index 51014e03009..ad5937df1b0 100644 --- a/src/shogun/evaluation/SplittingStrategy.h +++ b/src/shogun/evaluation/SplittingStrategy.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Fernando Iglesias, Yuyu Zhang, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Fernando Iglesias, Yuyu Zhang, * Sergey Lisitsyn */ @@ -17,10 +17,10 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Abstract base class for all splitting types. - * Takes a CLabels instance and generates a desired number of subsets which are + * Takes a Labels instance and generates a desired number of subsets which are * being accessed by their indices via the method generate_subset_indices(...). * * When being extended, the abstract method build_subsets() has to be @@ -29,28 +29,28 @@ class CLabels; * to allow calling them from the outside. Also they HAVE to set the m_is_filled * flag to true (otherwise there will be an error when accessing the index sets) * - * Implementations have to (re)fill the CDynamicArray elements in the + * Implementations have to (re)fill the DynamicArray elements in the * (inherited) m_subset_indices variable. Note that these elements are already * created by the constructor of this class - they just have to be filled. Every * element represents one index subset. * * Calling the method agains means that the indices are rebuilt. */ -class CSplittingStrategy: public CSGObject +class SplittingStrategy: public SGObject { public: /** constructor */ - CSplittingStrategy(); + SplittingStrategy(); /** constructor * * @param labels labels to be (possibly) used for splitting * @param num_subsets desired number of subsets, the labels are split into */ - CSplittingStrategy(CLabels* labels, index_t num_subsets); + SplittingStrategy(std::shared_ptr labels, index_t num_subsets); /** destructor */ - virtual ~CSplittingStrategy(); + virtual ~SplittingStrategy(); /** generates a newly created SGVector with indices of the subset * with the desired index @@ -79,7 +79,7 @@ class CSplittingStrategy: public CSGObject /** Abstract method. * Has to refill the elements of the m_subset_indices variable with concrete - * indices. Note that CDynamicArray instances for every subset are + * indices. Note that DynamicArray instances for every subset are * created in the constructor of this class - they just have to be filled. */ virtual void build_subsets()=0; @@ -96,10 +96,10 @@ class CSplittingStrategy: public CSGObject protected: /** labels */ - CLabels* m_labels; + std::shared_ptr m_labels; /** subset indices */ - CDynamicObjectArray* m_subset_indices; + std::shared_ptr m_subset_indices; /** additional variable to store number of index subsets */ index_t m_num_subsets; diff --git a/src/shogun/evaluation/StratifiedCrossValidationSplitting.cpp b/src/shogun/evaluation/StratifiedCrossValidationSplitting.cpp index 95470190e09..3906dbbda69 100644 --- a/src/shogun/evaluation/StratifiedCrossValidationSplitting.cpp +++ b/src/shogun/evaluation/StratifiedCrossValidationSplitting.cpp @@ -13,20 +13,20 @@ using namespace shogun; -CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting() : - RandomMixin() +StratifiedCrossValidationSplitting::StratifiedCrossValidationSplitting() : + RandomMixin() { } -CStratifiedCrossValidationSplitting::CStratifiedCrossValidationSplitting( - CLabels* labels, index_t num_subsets) : - RandomMixin(labels, num_subsets) +StratifiedCrossValidationSplitting::StratifiedCrossValidationSplitting( + std::shared_ptr labels, index_t num_subsets) : + RandomMixin(labels, num_subsets) { } -void CStratifiedCrossValidationSplitting::check_labels() const +void StratifiedCrossValidationSplitting::check_labels() const { - auto dense_labels = m_labels->as(); + auto dense_labels = std::static_pointer_cast(m_labels); auto classes = dense_labels->get_labels().unique(); SGVector labels_per_class(classes.size()); @@ -54,7 +54,7 @@ void CStratifiedCrossValidationSplitting::check_labels() const } } -void CStratifiedCrossValidationSplitting::build_subsets() +void StratifiedCrossValidationSplitting::build_subsets() { check_labels(); @@ -62,13 +62,13 @@ void CStratifiedCrossValidationSplitting::build_subsets() reset_subsets(); m_is_filled=true; - auto dense_labels = m_labels->as(); + auto dense_labels = m_labels->as(); auto classes = dense_labels->get_labels().unique(); /* for every label, build set for indices */ - CDynamicObjectArray label_indices; + DynamicObjectArray label_indices; for (auto i : range(classes.size())) - label_indices.append_element(new CDynamicArray ()); + label_indices.append_element(std::make_shared>()); /* fill set with indices, for each label type ... */ for (auto i : range(classes.size())) @@ -78,10 +78,9 @@ void CStratifiedCrossValidationSplitting::build_subsets() { if (dense_labels->get_label(j) == classes[i]) { - CDynamicArray* current=(CDynamicArray*) - label_indices.get_element(i); + auto current=label_indices.get_element>(i); current->append_element(j); - SG_UNREF(current); + } } } @@ -89,13 +88,13 @@ void CStratifiedCrossValidationSplitting::build_subsets() /* shuffle created label sets */ for (index_t i=0; i* current=(CDynamicArray*) - label_indices.get_element(i); + auto current= + label_indices.get_element>(i); // external random state important for threads random::shuffle(current->begin(), current->end(), m_prng); - SG_UNREF(current); + } /* distribute labels to subsets for all label types */ @@ -103,19 +102,19 @@ void CStratifiedCrossValidationSplitting::build_subsets() for (auto i : range(classes.size())) { /* current index set for current label */ - CDynamicArray* current=(CDynamicArray*) - label_indices.get_element(i); + auto current= + label_indices.get_element>(i); for (index_t j=0; jget_num_elements(); ++j) { - CDynamicArray* next=(CDynamicArray*) - m_subset_indices->get_element(target_set++); + auto next= + m_subset_indices->get_element>(target_set++); next->append_element(current->get_element(j)); target_set%=m_subset_indices->get_num_elements(); - SG_UNREF(next); + } - SG_UNREF(current); + } /* finally shuffle to avoid that subsets with low indices have more diff --git a/src/shogun/evaluation/StratifiedCrossValidationSplitting.h b/src/shogun/evaluation/StratifiedCrossValidationSplitting.h index 349e5277d9c..011c53bb0a1 100644 --- a/src/shogun/evaluation/StratifiedCrossValidationSplitting.h +++ b/src/shogun/evaluation/StratifiedCrossValidationSplitting.h @@ -15,26 +15,26 @@ namespace shogun { -class CLabels; +class Labels; /** @brief Implementation of stratified cross-validation on the base of - * CSplittingStrategy. Produces subset index sets of equal size (at most one + * SplittingStrategy. Produces subset index sets of equal size (at most one * difference) in which the label ratio is equal (at most one difference) to * the label ratio of the specified labels. Do not use for regression since it * may be impossible to distribute nice in that case */ -class CStratifiedCrossValidationSplitting: public RandomMixin +class StratifiedCrossValidationSplitting: public RandomMixin { public: /** constructor */ - CStratifiedCrossValidationSplitting(); + StratifiedCrossValidationSplitting(); /** constructor * * @param labels labels to be (possibly) used for splitting * @param num_subsets desired number of subsets, the labels are split into */ - CStratifiedCrossValidationSplitting(CLabels* labels, index_t num_subsets); + StratifiedCrossValidationSplitting(std::shared_ptr labels, index_t num_subsets); /** @return name of the SGSerializable */ virtual const char* get_name() const diff --git a/src/shogun/evaluation/StructuredAccuracy.cpp b/src/shogun/evaluation/StructuredAccuracy.cpp index 62948a4e724..24faa4e1549 100644 --- a/src/shogun/evaluation/StructuredAccuracy.cpp +++ b/src/shogun/evaluation/StructuredAccuracy.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Evgeniy Andreev, Sergey Lisitsyn, + * Authors: Fernando Iglesias, Evgeniy Andreev, Sergey Lisitsyn, * Soeren Sonnenburg, Sanuj Sharma, Abinash Panda */ @@ -14,28 +14,28 @@ using namespace shogun; -CStructuredAccuracy::CStructuredAccuracy() : CEvaluation() +StructuredAccuracy::StructuredAccuracy() : Evaluation() { } -CStructuredAccuracy::~CStructuredAccuracy() +StructuredAccuracy::~StructuredAccuracy() { } -float64_t CStructuredAccuracy::evaluate(CLabels * predicted, CLabels * ground_truth) +float64_t StructuredAccuracy::evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth) { - require(predicted && ground_truth, "CLabels objects passed to evaluate " + require(predicted && ground_truth, "Labels objects passed to evaluate " "cannot be null"); require(predicted->get_num_labels() == ground_truth->get_num_labels(), "The number of predicted and ground truth labels must " "be the same"); require(predicted->get_label_type() == LT_STRUCTURED, "The predicted " - "labels must be of type CStructuredLabels"); + "labels must be of type StructuredLabels"); require(ground_truth->get_label_type() == LT_STRUCTURED, "The ground truth " - "labels must be of type CStructuredLabels"); + "labels must be of type StructuredLabels"); - CStructuredLabels* pred_labs = predicted->as(); - CStructuredLabels* true_labs = ground_truth->as(); + auto pred_labs = std::dynamic_pointer_cast(predicted); + auto true_labs = std::dynamic_pointer_cast(ground_truth); require(pred_labs->get_structured_data_type() == true_labs->get_structured_data_type(), "Predicted and ground truth " @@ -59,35 +59,35 @@ float64_t CStructuredAccuracy::evaluate(CLabels * predicted, CLabels * ground_tr return 0.0; } -SGMatrix CStructuredAccuracy::get_confusion_matrix( - CLabels * predicted, CLabels * ground_truth) +SGMatrix StructuredAccuracy::get_confusion_matrix( + std::shared_ptr predicted, std::shared_ptr ground_truth) { error("Not implemented"); return SGMatrix(); } -float64_t CStructuredAccuracy::evaluate_real(CStructuredLabels * predicted, - CStructuredLabels * ground_truth) +float64_t StructuredAccuracy::evaluate_real(std::shared_ptr predicted, + std::shared_ptr ground_truth) { int32_t length = predicted->get_num_labels(); int32_t num_equal = 0; for (int32_t i = 0 ; i < length ; ++i) { - CRealNumber * truth = ground_truth->get_label(i)->as(); - CRealNumber * pred = predicted->get_label(i)->as(); + auto truth = std::dynamic_pointer_cast(ground_truth->get_label(i)); + auto pred = std::dynamic_pointer_cast(predicted->get_label(i)); num_equal += truth->value == pred->value; - SG_UNREF(truth); - SG_UNREF(pred); + + } return (1.0 * num_equal) / length; } -float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels * predicted, - CStructuredLabels * ground_truth) +float64_t StructuredAccuracy::evaluate_sequence(std::shared_ptr predicted, + std::shared_ptr ground_truth) { int32_t length = predicted->get_num_labels(); // Accuracy of each each label @@ -96,8 +96,8 @@ float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels * predicted, for (int32_t i = 0 ; i < length ; ++i) { - CSequence * true_seq = ground_truth->get_label(i)->as(); - CSequence * pred_seq = predicted->get_label(i)->as(); + auto true_seq = std::dynamic_pointer_cast(ground_truth->get_label(i)); + auto pred_seq = std::dynamic_pointer_cast(predicted->get_label(i)); SGVector true_seq_data = true_seq->get_data(); SGVector pred_seq_data = pred_seq->get_data(); @@ -115,26 +115,26 @@ float64_t CStructuredAccuracy::evaluate_sequence(CStructuredLabels * predicted, accuracies[i] = (1.0 * num_equal) / true_seq_data.size(); - SG_UNREF(true_seq); - SG_UNREF(pred_seq); + + } - return CStatistics::mean(accuracies); + return Statistics::mean(accuracies); } -float64_t CStructuredAccuracy::evaluate_sparse_multilabel(CStructuredLabels * predicted, - CStructuredLabels * ground_truth) +float64_t StructuredAccuracy::evaluate_sparse_multilabel(std::shared_ptr predicted, + std::shared_ptr ground_truth) { - CMultilabelSOLabels * multi_pred = (CMultilabelSOLabels *) predicted; - CMultilabelSOLabels * multi_truth = (CMultilabelSOLabels *) ground_truth; + auto multi_pred = std::static_pointer_cast(predicted); + auto multi_truth = std::static_pointer_cast(ground_truth); + + auto evaluator = std::make_shared(); - CMultilabelAccuracy * evaluator = new CMultilabelAccuracy(); - SG_REF(evaluator); float64_t accuracy = evaluator->evaluate(multi_pred->get_multilabel_labels(), multi_truth->get_multilabel_labels()); - SG_UNREF(evaluator); + return accuracy; } diff --git a/src/shogun/evaluation/StructuredAccuracy.h b/src/shogun/evaluation/StructuredAccuracy.h index e4f65c3db55..845a248df5e 100644 --- a/src/shogun/evaluation/StructuredAccuracy.h +++ b/src/shogun/evaluation/StructuredAccuracy.h @@ -16,16 +16,16 @@ namespace shogun { /** - * @brief class CStructuredAccuracy used to compute accuracy of structured classification + * @brief class StructuredAccuracy used to compute accuracy of structured classification */ -class CStructuredAccuracy : public CEvaluation +class StructuredAccuracy : public Evaluation { public: /** default constructor */ - CStructuredAccuracy(); + StructuredAccuracy(); /** destructor */ - virtual ~CStructuredAccuracy(); + virtual ~StructuredAccuracy(); /** evaluate accuracy * @@ -34,7 +34,7 @@ class CStructuredAccuracy : public CEvaluation * * @return accuracy */ - virtual float64_t evaluate(CLabels * predicted, CLabels * ground_truth); + virtual float64_t evaluate(std::shared_ptr predicted, std::shared_ptr ground_truth); /** NOT IMPLEMENTED * constructs confusion matrix for multiclass classification @@ -44,7 +44,7 @@ class CStructuredAccuracy : public CEvaluation * * @return confusion matrix */ - static SGMatrix get_confusion_matrix(CLabels * predicted, CLabels * ground_truth); + static SGMatrix get_confusion_matrix(std::shared_ptr predicted, std::shared_ptr ground_truth); /** whether the evaluation criterion has to be maximimed or minimized * @@ -69,7 +69,7 @@ class CStructuredAccuracy : public CEvaluation * * @return accuracy */ - float64_t evaluate_real(CStructuredLabels * predicted, CStructuredLabels * ground_truth); + float64_t evaluate_real(std::shared_ptr predicted, std::shared_ptr ground_truth); /** evaluate accuracy for structured labels composed of sequences * @@ -78,7 +78,7 @@ class CStructuredAccuracy : public CEvaluation * * @return accuracy */ - float64_t evaluate_sequence(CStructuredLabels * predicted, CStructuredLabels * ground_truth); + float64_t evaluate_sequence(std::shared_ptr predicted, std::shared_ptr ground_truth); /** evaluate accuracy for structured labels composed of sparse multi * labels. Formally the accuracy is defined as @@ -93,10 +93,10 @@ class CStructuredAccuracy : public CEvaluation * * @return accuracy */ - float64_t evaluate_sparse_multilabel(CStructuredLabels * predicted, - CStructuredLabels * ground_truth); + float64_t evaluate_sparse_multilabel(std::shared_ptr predicted, + std::shared_ptr ground_truth); -}; /* class CStructuredAccuracy*/ +}; /* class StructuredAccuracy*/ } /* namespace shogun */ diff --git a/src/shogun/evaluation/TimeSeriesSplitting.cpp b/src/shogun/evaluation/TimeSeriesSplitting.cpp index f7dff6ded2a..5a00afd3418 100644 --- a/src/shogun/evaluation/TimeSeriesSplitting.cpp +++ b/src/shogun/evaluation/TimeSeriesSplitting.cpp @@ -37,25 +37,25 @@ using namespace shogun; -CTimeSeriesSplitting::CTimeSeriesSplitting() : RandomMixin() +TimeSeriesSplitting::TimeSeriesSplitting() : RandomMixin() { init(); } -CTimeSeriesSplitting::CTimeSeriesSplitting(CLabels* labels, index_t num_subsets) - : RandomMixin(labels, num_subsets) +TimeSeriesSplitting::TimeSeriesSplitting(std::shared_ptr labels, index_t num_subsets) + : RandomMixin(labels, num_subsets) { init(); } -void CTimeSeriesSplitting::init() +void TimeSeriesSplitting::init() { m_min_subset_size = 1; - SG_ADD(&m_min_subset_size, "min_subset_size", - "The minimum subset size for test set") + SG_ADD(&m_min_subset_size, "min_subset_size", + "The minimum subset size for test set"); } -void CTimeSeriesSplitting::build_subsets() +void TimeSeriesSplitting::build_subsets() { reset_subsets(); m_is_filled = true; @@ -67,8 +67,7 @@ void CTimeSeriesSplitting::build_subsets() for (auto i = 0; i < num_subsets; ++i) { - CDynamicArray* current = - (CDynamicArray*)m_subset_indices->get_element(i); + auto current =m_subset_indices->get_element>(i); if (i == num_subsets - 1) split_index = indices.vlen - m_min_subset_size; @@ -81,13 +80,13 @@ void CTimeSeriesSplitting::build_subsets() current->append_element(indices.vector[k]); } - SG_UNREF(current); + } random::shuffle(m_subset_indices->begin(), m_subset_indices->end(), m_prng); } -void CTimeSeriesSplitting::set_min_subset_size(index_t min_size) +void TimeSeriesSplitting::set_min_subset_size(index_t min_size) { index_t num_subsets = m_subset_indices->get_num_elements(); index_t num_labels = m_labels->get_num_labels(); @@ -103,7 +102,7 @@ void CTimeSeriesSplitting::set_min_subset_size(index_t min_size) m_min_subset_size = min_size; } -index_t CTimeSeriesSplitting::get_min_subset_size() +index_t TimeSeriesSplitting::get_min_subset_size() { return m_min_subset_size; } diff --git a/src/shogun/evaluation/TimeSeriesSplitting.h b/src/shogun/evaluation/TimeSeriesSplitting.h index e00601fe461..a34b28073f2 100644 --- a/src/shogun/evaluation/TimeSeriesSplitting.h +++ b/src/shogun/evaluation/TimeSeriesSplitting.h @@ -40,7 +40,7 @@ namespace shogun { - class CLabels; + class Labels; /** @brief Implements a timeseries splitting strategy for cross-validation. * The strategy builds a given number of subsets each filled with labels @@ -54,11 +54,11 @@ namespace shogun * The two splits are \f$ S1 = [5,6,7,8,9] \f$ and \f$ S2 = [8,9]\f$ */ - class CTimeSeriesSplitting : public RandomMixin + class TimeSeriesSplitting : public RandomMixin { public: /** constructor */ - CTimeSeriesSplitting(); + TimeSeriesSplitting(); /** constructor * @@ -66,7 +66,7 @@ namespace shogun * @param num_subsets desired number of subsets, the labels are split * into */ - CTimeSeriesSplitting(CLabels* labels, index_t num_subsets); + TimeSeriesSplitting(std::shared_ptr labels, index_t num_subsets); /** Sets the minimum subset size for subsets. If forecasting h-step * ahead, set min_size to h. @@ -93,4 +93,4 @@ namespace shogun }; } -#endif /* __TIMESERIESSPLITTING_H_ */ \ No newline at end of file +#endif /* __TIMESERIESSPLITTING_H_ */ diff --git a/src/shogun/evaluation/ica/PermutationMatrix.cpp b/src/shogun/evaluation/ica/PermutationMatrix.cpp index 3b53309e508..83df3bcc069 100644 --- a/src/shogun/evaluation/ica/PermutationMatrix.cpp +++ b/src/shogun/evaluation/ica/PermutationMatrix.cpp @@ -20,7 +20,7 @@ bool is_permutation_matrix(SGMatrix m) { for (int j = 0; j < mat.cols(); j++) { - if (CMath::abs(CMath::round(mat(i,j))) >= 1.0) + if (Math::abs(Math::round(mat(i,j))) >= 1.0) mat(i,j) = 1.0; else mat(i,j) = 0.0; diff --git a/src/shogun/features/Alphabet.cpp b/src/shogun/features/Alphabet.cpp index c785d21f9f7..2b184efe111 100644 --- a/src/shogun/features/Alphabet.cpp +++ b/src/shogun/features/Alphabet.cpp @@ -16,27 +16,27 @@ using namespace shogun; //define numbers for the bases -const uint8_t CAlphabet::B_A=0; -const uint8_t CAlphabet::B_C=1; -const uint8_t CAlphabet::B_G=2; -const uint8_t CAlphabet::B_T=3; -const uint8_t CAlphabet::B_0=4; -const uint8_t CAlphabet::MAPTABLE_UNDEF=0xff; -const char* CAlphabet::alphabet_names[18]={ +const uint8_t Alphabet::B_A=0; +const uint8_t Alphabet::B_C=1; +const uint8_t Alphabet::B_G=2; +const uint8_t Alphabet::B_T=3; +const uint8_t Alphabet::B_0=4; +const uint8_t Alphabet::MAPTABLE_UNDEF=0xff; +const char* Alphabet::alphabet_names[18]={ "DNA","RAWDNA", "RNA", "PROTEIN", "BINARY", "ALPHANUM", "CUBE", "RAW", "IUPAC_NUCLEIC_ACID", "IUPAC_AMINO_ACID", "NONE", "DIGIT", "DIGIT2", "RAWDIGIT", "RAWDIGIT2", "UNKNOWN", "SNP", "RAWSNP"}; -CAlphabet::CAlphabet() - : CSGObject() +Alphabet::Alphabet() + : SGObject() { init(); } -CAlphabet::CAlphabet(char* al, int32_t len) -: CSGObject() +Alphabet::Alphabet(char* al, int32_t len) +: SGObject() { init(); @@ -82,27 +82,27 @@ CAlphabet::CAlphabet(char* al, int32_t len) set_alphabet(alpha); } -CAlphabet::CAlphabet(EAlphabet alpha) -: CSGObject() +Alphabet::Alphabet(EAlphabet alpha) +: SGObject() { init(); set_alphabet(alpha); } -CAlphabet::CAlphabet(CAlphabet* a) -: CSGObject() +Alphabet::Alphabet(std::shared_ptr a) +: SGObject() { init(); require(a, "No Alphabet specified!"); set_alphabet(a->get_alphabet()); - copy_histogram(a); + copy_histogram(a.get()); } -CAlphabet::~CAlphabet() +Alphabet::~Alphabet() { } -bool CAlphabet::set_alphabet(EAlphabet alpha) +bool Alphabet::set_alphabet(EAlphabet alpha) { bool result=true; alphabet=alpha; @@ -173,7 +173,7 @@ bool CAlphabet::set_alphabet(EAlphabet alpha) return result; } -void CAlphabet::init_map_table() +void Alphabet::init_map_table() { for (int32_t i=0; i<(1<<(8*sizeof(uint8_t))); i++) { @@ -538,13 +538,13 @@ void CAlphabet::init_map_table() }; } -void CAlphabet::clear_histogram() +void Alphabet::clear_histogram() { memset(histogram, 0, sizeof(histogram)); print_histogram(); } -int32_t CAlphabet::get_max_value_in_histogram() +int32_t Alphabet::get_max_value_in_histogram() { int32_t max_sym=-1; for (int32_t i=(int32_t) (1 <<(sizeof(uint8_t)*8))-1;i>=0; i--) @@ -559,7 +559,7 @@ int32_t CAlphabet::get_max_value_in_histogram() return max_sym; } -int32_t CAlphabet::get_num_symbols_in_histogram() +int32_t Alphabet::get_num_symbols_in_histogram() { int32_t num_sym=0; for (int32_t i=0; i<(int32_t) (1 <<(sizeof(uint8_t)*8)); i++) @@ -571,7 +571,7 @@ int32_t CAlphabet::get_num_symbols_in_histogram() return num_sym; } -int32_t CAlphabet::get_num_bits_in_histogram() +int32_t Alphabet::get_num_bits_in_histogram() { int32_t num_sym=get_num_symbols_in_histogram(); if (num_sym>0) @@ -581,7 +581,7 @@ int32_t CAlphabet::get_num_bits_in_histogram() } -void CAlphabet::print_histogram() +void Alphabet::print_histogram() { for (int32_t i=0; i<(int32_t) (1 <<(sizeof(uint8_t)*8)); i++) { @@ -606,12 +606,12 @@ void CAlphabet::print_histogram() } } -SGVector CAlphabet::get_histogram() const +SGVector Alphabet::get_histogram() const { return SGVector(const_cast(&histogram[0]), 1 << (sizeof(uint8_t)*8), false); } -bool CAlphabet::check_alphabet(bool print_error) +bool Alphabet::check_alphabet(bool print_error) { bool result = true; @@ -633,7 +633,7 @@ bool CAlphabet::check_alphabet(bool print_error) return result; } -bool CAlphabet::check_alphabet_size(bool print_error) +bool Alphabet::check_alphabet_size(bool print_error) { if (get_num_bits_in_histogram() > get_num_bits()) { @@ -650,7 +650,7 @@ bool CAlphabet::check_alphabet_size(bool print_error) } -void CAlphabet::copy_histogram(const CAlphabet* a) +void Alphabet::copy_histogram(const Alphabet* a) { SGVector h=a->get_histogram(); @@ -663,7 +663,7 @@ void CAlphabet::copy_histogram(const CAlphabet* a) sg_memcpy(histogram, h.vector, sizeof(histogram)); } -const char* CAlphabet::get_alphabet_name(EAlphabet alphabet) +const char* Alphabet::get_alphabet_name(EAlphabet alphabet) { int32_t idx; @@ -715,7 +715,7 @@ const char* CAlphabet::get_alphabet_name(EAlphabet alphabet) return alphabet_names[idx]; } -void CAlphabet::init() +void Alphabet::init() { alphabet = NONE; num_symbols = 0; @@ -740,9 +740,9 @@ void CAlphabet::init() "Histogram."); */ } -void CAlphabet::load_serializable_post() noexcept(false) +void Alphabet::load_serializable_post() noexcept(false) { - CSGObject::load_serializable_post(); + SGObject::load_serializable_post(); init_map_table(); } @@ -751,7 +751,7 @@ void CAlphabet::load_serializable_post() noexcept(false) namespace shogun { template -void CAlphabet::translate_from_single_order(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val) +void Alphabet::translate_from_single_order(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val) { int32_t i,j; ST value=0; @@ -789,7 +789,7 @@ void CAlphabet::translate_from_single_order(ST* obs, int32_t sequence_length, in } template -void CAlphabet::translate_from_single_order_reversed(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val) +void Alphabet::translate_from_single_order_reversed(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val) { int32_t i,j; ST value=0; @@ -826,16 +826,16 @@ void CAlphabet::translate_from_single_order_reversed(ST* obs, int32_t sequence_l } } -CSGObject * CAlphabet::clone(ParameterProperties pp) const +std::shared_ptr Alphabet::clone(ParameterProperties pp) const { - CAlphabet * alph_clone = (CAlphabet *) CSGObject::clone(pp); + auto alph_clone = std::dynamic_pointer_cast(SGObject::clone(pp)); alph_clone->init_map_table(); alph_clone->copy_histogram(this); return alph_clone; } template -void CAlphabet::translate_from_single_order(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +void Alphabet::translate_from_single_order(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { ASSERT(gap>=0) @@ -897,7 +897,7 @@ void CAlphabet::translate_from_single_order(ST* obs, int32_t sequence_length, in } template -void CAlphabet::translate_from_single_order_reversed(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +void Alphabet::translate_from_single_order_reversed(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { ASSERT(gap>=0) @@ -954,74 +954,74 @@ void CAlphabet::translate_from_single_order_reversed(ST* obs, int32_t sequence_l } } -template<> void CAlphabet::translate_from_single_order(float32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +template<> void Alphabet::translate_from_single_order(float32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { } -template<> void CAlphabet::translate_from_single_order(float64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +template<> void Alphabet::translate_from_single_order(float64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { } -template<> void CAlphabet::translate_from_single_order(floatmax_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +template<> void Alphabet::translate_from_single_order(floatmax_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { } -template<> void CAlphabet::translate_from_single_order_reversed(float32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +template<> void Alphabet::translate_from_single_order_reversed(float32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { } -template<> void CAlphabet::translate_from_single_order_reversed(float64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +template<> void Alphabet::translate_from_single_order_reversed(float64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { } -template<> void CAlphabet::translate_from_single_order_reversed(floatmax_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) +template<> void Alphabet::translate_from_single_order_reversed(floatmax_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap) { } -template void CAlphabet::translate_from_single_order(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); - -template void CAlphabet::translate_from_single_order(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); - -template void CAlphabet::translate_from_single_order_reversed(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); -template void CAlphabet::translate_from_single_order_reversed(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); - -template void CAlphabet::translate_from_single_order_reversed(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(float32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(float64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); -template void CAlphabet::translate_from_single_order_reversed(floatmax_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); + +template void Alphabet::translate_from_single_order(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); + +template void Alphabet::translate_from_single_order_reversed(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); +template void Alphabet::translate_from_single_order_reversed(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val); + +template void Alphabet::translate_from_single_order_reversed(bool* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(char* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(int8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(uint8_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(int16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(uint16_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(int32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(uint32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(int64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(uint64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(float32_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(float64_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); +template void Alphabet::translate_from_single_order_reversed(floatmax_t* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); } diff --git a/src/shogun/features/Alphabet.h b/src/shogun/features/Alphabet.h index 59113e15904..89f292399f9 100644 --- a/src/shogun/features/Alphabet.h +++ b/src/shogun/features/Alphabet.h @@ -84,34 +84,34 @@ enum EAlphabet * BINARY, ALPHANUM, CUBE, RAW, IUPAC_NUCLEIC_ACID and IUPAC_AMINO_ACID. * */ -class CAlphabet : public CSGObject +class Alphabet : public SGObject { public: /** default constructor * */ - CAlphabet(); + Alphabet(); /** constructor * * @param alpha alphabet to use * @param len len */ - CAlphabet(char* alpha, int32_t len); + Alphabet(char* alpha, int32_t len); /** constructor * * @param alpha alphabet (type) to use */ - CAlphabet(EAlphabet alpha); + Alphabet(EAlphabet alpha); /** constructor * * @param alpha alphabet to use */ - CAlphabet(CAlphabet* alpha); - virtual ~CAlphabet(); + Alphabet(std::shared_ptr alpha); + virtual ~Alphabet(); /** set alphabet and initialize mapping table (for remap) * @@ -302,7 +302,7 @@ class CAlphabet : public CSGObject template static void translate_from_single_order_reversed(ST* obs, int32_t sequence_length, int32_t start, int32_t p_order, int32_t max_val, int32_t gap); - virtual CSGObject* clone(ParameterProperties pp = ParameterProperties::ALL) const override; + virtual std::shared_ptr clone(ParameterProperties pp = ParameterProperties::ALL) const; private: /** Do basic initialisations like default settings @@ -317,7 +317,7 @@ class CAlphabet : public CSGObject * * @param src alphabet to copy histogram from */ - void copy_histogram(const CAlphabet* src); + void copy_histogram(const Alphabet* src); public: /** B_A */ diff --git a/src/shogun/features/AttributeFeatures.cpp b/src/shogun/features/AttributeFeatures.cpp index 0f6ab4128ee..c68baf1547a 100644 --- a/src/shogun/features/AttributeFeatures.cpp +++ b/src/shogun/features/AttributeFeatures.cpp @@ -10,33 +10,30 @@ using namespace shogun; -CAttributeFeatures::CAttributeFeatures() -: CFeatures(0) +AttributeFeatures::AttributeFeatures() +: Features(0) { } -CFeatures* CAttributeFeatures::get_attribute(char* attr_name) +std::shared_ptr AttributeFeatures::get_attribute(char* attr_name) { int32_t idx=find_attr_index(attr_name); if (idx>=0) { - CFeatures* f=features[idx].attr_obj; - SG_REF(f); - return f; + return features[idx].attr_obj; } return NULL; } -void CAttributeFeatures::get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj) +void AttributeFeatures::get_attribute_by_index(int idx, const char* &attr_name, std::shared_ptr &attr_obj) { T_ATTRIBUTE a= features.get_element_safe(idx); attr_name= a.attr_name; attr_obj= a.attr_obj; - SG_REF(a.attr_obj); } -bool CAttributeFeatures::set_attribute(char* attr_name, CFeatures* attr_obj) +bool AttributeFeatures::set_attribute(char* attr_name, std::shared_ptr attr_obj) { int32_t idx=find_attr_index(attr_name); if (idx==-1) @@ -46,12 +43,10 @@ bool CAttributeFeatures::set_attribute(char* attr_name, CFeatures* attr_obj) a.attr_name=get_strdup(attr_name); a.attr_obj=attr_obj; - SG_REF(attr_obj); - return features.set_element(a, idx); } -bool CAttributeFeatures::del_attribute(char* attr_name) +bool AttributeFeatures::del_attribute(char* attr_name) { int32_t idx=find_attr_index(attr_name); @@ -59,18 +54,18 @@ bool CAttributeFeatures::del_attribute(char* attr_name) { T_ATTRIBUTE a= features[idx]; SG_FREE(a.attr_name); - SG_UNREF(a.attr_obj); + a.attr_obj.reset(); return true; } return false; } -int32_t CAttributeFeatures::get_num_attributes() +int32_t AttributeFeatures::get_num_attributes() { return features.get_num_elements(); } -int32_t CAttributeFeatures::find_attr_index(char* attr_name) +int32_t AttributeFeatures::find_attr_index(char* attr_name) { int32_t n=features.get_num_elements(); for (int32_t i=0; i attr_obj; }; #endif // DOXYGEN_SHOULD_SKIP_THIS @@ -39,22 +39,22 @@ struct T_ATTRIBUTE * This might be used to represent * (attr, value) pairs, simple structures, trees ... */ -class CAttributeFeatures : public CFeatures +class AttributeFeatures : public Features { public: /** default constructor */ - CAttributeFeatures(); + AttributeFeatures(); /** destructor */ - virtual ~CAttributeFeatures(); + virtual ~AttributeFeatures(); /** return the feature object matching attribute name * * @param attr_name attribute name * @return feature object */ - CFeatures* get_attribute(char* attr_name); + std::shared_ptr get_attribute(char* attr_name); /** return the feature object at index * @@ -62,7 +62,7 @@ class CAttributeFeatures : public CFeatures * @param attr_name attribute name (returned by reference) * @param attr_obj attribute object (returned by reference) */ - void get_attribute_by_index(int idx, const char* &attr_name, CFeatures* &attr_obj); + void get_attribute_by_index(int idx, const char* &attr_name, std::shared_ptr& attr_obj); /** set the feature object for attribute name * @@ -70,7 +70,7 @@ class CAttributeFeatures : public CFeatures * @param attr_obj feature object to set * @return true on success */ - bool set_attribute(char* attr_name, CFeatures* attr_obj); + bool set_attribute(char* attr_name, std::shared_ptr attr_obj); /** delete the attribute matching attribute name * @@ -94,7 +94,7 @@ class CAttributeFeatures : public CFeatures * * @return feature object */ - virtual CFeatures* duplicate() const=0; + virtual std::shared_ptr duplicate() const=0; /** get feature type * diff --git a/src/shogun/features/BinnedDotFeatures.cpp b/src/shogun/features/BinnedDotFeatures.cpp index 1f4ca5a5fbe..ba668301321 100644 --- a/src/shogun/features/BinnedDotFeatures.cpp +++ b/src/shogun/features/BinnedDotFeatures.cpp @@ -10,39 +10,39 @@ using namespace shogun; -CBinnedDotFeatures::CBinnedDotFeatures(int32_t size) - : CDotFeatures(size) +BinnedDotFeatures::BinnedDotFeatures(int32_t size) + : DotFeatures(size) { init(); } -CBinnedDotFeatures::CBinnedDotFeatures(const CBinnedDotFeatures & orig) - : CDotFeatures(orig), m_bins(orig.m_bins), m_fill(orig.m_fill), +BinnedDotFeatures::BinnedDotFeatures(const BinnedDotFeatures & orig) + : DotFeatures(orig), m_bins(orig.m_bins), m_fill(orig.m_fill), m_norm_one(orig.m_norm_one) { init(); } -CBinnedDotFeatures::CBinnedDotFeatures(CFeatures* sf, SGMatrix bins) +BinnedDotFeatures::BinnedDotFeatures(std::shared_ptr sf, SGMatrix bins) { init(); - set_simple_features(sf->as>()); + set_simple_features(std::static_pointer_cast>(sf)); set_bins(bins); } -CBinnedDotFeatures::~CBinnedDotFeatures() +BinnedDotFeatures::~BinnedDotFeatures() { - SG_UNREF(m_features); + } -int32_t CBinnedDotFeatures::get_dim_feature_space() const +int32_t BinnedDotFeatures::get_dim_feature_space() const { return m_bins.num_rows*m_bins.num_cols; } -float64_t CBinnedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t BinnedDotFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) @@ -52,8 +52,9 @@ float64_t CBinnedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t ve double sum1=0; double sum2=0; + auto bin_dots = std::static_pointer_cast(df); SGVector vec1=m_features->get_feature_vector(vec_idx1); - SGVector vec2=((CBinnedDotFeatures*) df)->m_features->get_feature_vector(vec_idx2); + SGVector vec2=bin_dots->m_features->get_feature_vector(vec_idx2); for (int32_t i=0; ifree_feature_vector(vec1, vec_idx1); - ((CBinnedDotFeatures*) df)->m_features->free_feature_vector(vec2, vec_idx2); + bin_dots->m_features->free_feature_vector(vec2, vec_idx2); if (m_fill && m_norm_one && sum1!=0 && sum2!=0) result /= std::sqrt(sum1 * sum2); @@ -116,7 +117,7 @@ float64_t CBinnedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t ve } float64_t -CBinnedDotFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +BinnedDotFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { assert_shape(vec2.size()); @@ -160,7 +161,7 @@ CBinnedDotFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return result; } -void CBinnedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void BinnedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { assert_shape(vec2_len); SGVector vec1=m_features->get_feature_vector(vec_idx1); @@ -212,7 +213,7 @@ void CBinnedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, flo m_features->free_feature_vector(vec1, vec_idx1); } -void CBinnedDotFeatures::assert_shape(int32_t vec2_len) const +void BinnedDotFeatures::assert_shape(int32_t vec2_len) const { if (m_bins.num_cols*m_bins.num_rows != vec2_len) { @@ -229,7 +230,7 @@ void CBinnedDotFeatures::assert_shape(int32_t vec2_len) const } -int32_t CBinnedDotFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t BinnedDotFeatures::get_nnz_features_for_vector(int32_t num) const { if (m_fill) return m_bins.num_rows; @@ -237,94 +238,92 @@ int32_t CBinnedDotFeatures::get_nnz_features_for_vector(int32_t num) const return 1; } -void* CBinnedDotFeatures::get_feature_iterator(int32_t vector_index) +void* BinnedDotFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CBinnedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool BinnedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CBinnedDotFeatures::free_feature_iterator(void* iterator) +void BinnedDotFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } -bool CBinnedDotFeatures::get_fill() +bool BinnedDotFeatures::get_fill() { return m_fill; } -void CBinnedDotFeatures::set_fill(bool fill) +void BinnedDotFeatures::set_fill(bool fill) { m_fill=fill; } -bool CBinnedDotFeatures::get_norm_one() +bool BinnedDotFeatures::get_norm_one() { return m_fill; } -void CBinnedDotFeatures::set_norm_one(bool norm_one) +void BinnedDotFeatures::set_norm_one(bool norm_one) { m_norm_one=norm_one; } -void CBinnedDotFeatures::set_bins(SGMatrix bins) +void BinnedDotFeatures::set_bins(SGMatrix bins) { m_bins=bins; } -SGMatrix CBinnedDotFeatures::get_bins() +SGMatrix BinnedDotFeatures::get_bins() { return m_bins; } -void CBinnedDotFeatures::set_simple_features(CDenseFeatures* features) +void BinnedDotFeatures::set_simple_features(std::shared_ptr> features) { - SG_REF(features); m_features=features; } -CDenseFeatures* CBinnedDotFeatures::get_simple_features() +std::shared_ptr> BinnedDotFeatures::get_simple_features() { - SG_REF(m_features); return m_features; } -void CBinnedDotFeatures::init() +void BinnedDotFeatures::init() { m_features=NULL; m_fill=true; m_norm_one=false; } -const char* CBinnedDotFeatures::get_name() const +const char* BinnedDotFeatures::get_name() const { return "BinnedDotFeatures"; } -CFeatures* CBinnedDotFeatures::duplicate() const +std::shared_ptr BinnedDotFeatures::duplicate() const { - return new CBinnedDotFeatures(*this); + return std::make_shared(*this); } -EFeatureType CBinnedDotFeatures::get_feature_type() const +EFeatureType BinnedDotFeatures::get_feature_type() const { return F_DREAL; } -EFeatureClass CBinnedDotFeatures::get_feature_class() const +EFeatureClass BinnedDotFeatures::get_feature_class() const { return C_BINNED_DOT; } -int32_t CBinnedDotFeatures::get_num_vectors() const +int32_t BinnedDotFeatures::get_num_vectors() const { ASSERT(m_features) return m_features->get_num_vectors(); diff --git a/src/shogun/features/BinnedDotFeatures.h b/src/shogun/features/BinnedDotFeatures.h index be97017113a..92157e4eff9 100644 --- a/src/shogun/features/BinnedDotFeatures.h +++ b/src/shogun/features/BinnedDotFeatures.h @@ -16,7 +16,7 @@ namespace shogun { - template class CDenseFeatures; + template class DenseFeatures; /** @brief The class BinnedDotFeatures contains a 0-1 conversion of features into bins. * @@ -36,19 +36,19 @@ namespace shogun * * Note that BinnedDotFeatures never *explicitly* compute the binned feature * representation but only overload the abstract dot/add methods in - * CDotFeatures making them highly memory and computationally efficient. + * DotFeatures making them highly memory and computationally efficient. */ -class CBinnedDotFeatures : public CDotFeatures +class BinnedDotFeatures : public DotFeatures { public: /** constructor * * @param size cache size */ - CBinnedDotFeatures(int32_t size=0); + BinnedDotFeatures(int32_t size=0); /** copy constructor */ - CBinnedDotFeatures(const CBinnedDotFeatures & orig); + BinnedDotFeatures(const BinnedDotFeatures & orig); /** constructor * @@ -56,9 +56,9 @@ class CBinnedDotFeatures : public CDotFeatures * binned features * @param bins a matrix with bins to compute binned features from */ - CBinnedDotFeatures(CFeatures* sf, SGMatrix bins); + BinnedDotFeatures(std::shared_ptr sf, SGMatrix bins); - virtual ~CBinnedDotFeatures(); + virtual ~BinnedDotFeatures(); /** obtain the dimensionality of the feature space * @@ -76,7 +76,7 @@ class CBinnedDotFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -164,13 +164,13 @@ class CBinnedDotFeatures : public CDotFeatures * * @param features - features to convert to binned features */ - void set_simple_features(CDenseFeatures* features); + void set_simple_features(std::shared_ptr> features); /** get features that are convert to binned features * * @return features - simple features object */ - CDenseFeatures* get_simple_features(); + std::shared_ptr> get_simple_features(); /** set bins * @@ -198,7 +198,7 @@ class CBinnedDotFeatures : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -229,7 +229,7 @@ class CBinnedDotFeatures : public CDotFeatures protected: /// underlying features - CDenseFeatures* m_features; + std::shared_ptr> m_features; /// bins with limits SGMatrix m_bins; diff --git a/src/shogun/features/CombinedDotFeatures.cpp b/src/shogun/features/CombinedDotFeatures.cpp index 413e8bf07cb..8b8352871a8 100644 --- a/src/shogun/features/CombinedDotFeatures.cpp +++ b/src/shogun/features/CombinedDotFeatures.cpp @@ -13,18 +13,16 @@ using namespace shogun; -constexpr float64_t CCombinedDotFeatures::initial_weight = 1.0; +constexpr float64_t CombinedDotFeatures::initial_weight = 1.0; -CCombinedDotFeatures::CCombinedDotFeatures() : CDotFeatures() +CombinedDotFeatures::CombinedDotFeatures() : DotFeatures() { init(); - - update_dim_feature_space_and_num_vec(); } -CCombinedDotFeatures::CCombinedDotFeatures(const CCombinedDotFeatures& orig) - : CDotFeatures(orig), feature_array(orig.feature_array), +CombinedDotFeatures::CombinedDotFeatures(const CombinedDotFeatures& orig) + : DotFeatures(orig), feature_array(orig.feature_array), feature_weights(orig.feature_weights), num_vectors(orig.num_vectors), num_dimensions(orig.num_dimensions) { @@ -32,24 +30,24 @@ CCombinedDotFeatures::CCombinedDotFeatures(const CCombinedDotFeatures& orig) update_dim_feature_space_and_num_vec(); } -CFeatures* CCombinedDotFeatures::duplicate() const +std::shared_ptr CombinedDotFeatures::duplicate() const { - return new CCombinedDotFeatures(*this); + return std::make_shared(*this); } -CCombinedDotFeatures::~CCombinedDotFeatures() +CombinedDotFeatures::~CombinedDotFeatures() { - SG_UNREF(feature_array); + } -void CCombinedDotFeatures::update_dim_feature_space_and_num_vec() +void CombinedDotFeatures::update_dim_feature_space_and_num_vec() { int32_t dim=0; int32_t vec=-1; for (index_t f_idx=0; f_idxget_dim_feature_space(); if (vec==-1) vec=f->get_num_vectors(); @@ -59,7 +57,6 @@ void CCombinedDotFeatures::update_dim_feature_space_and_num_vec() error("Number of vectors ({}) mismatches in above feature obj ({})", vec, f->get_num_vectors()); } - SG_UNREF(f); } num_dimensions=dim; @@ -67,22 +64,22 @@ void CCombinedDotFeatures::update_dim_feature_space_and_num_vec() SG_DEBUG("vecs={}, dims={}", num_vectors, num_dimensions) } -float64_t CCombinedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t CombinedDotFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { float64_t result=0; ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CCombinedDotFeatures* cf = (CCombinedDotFeatures*) df; + auto cf = std::static_pointer_cast(df); // check that both have same number of feature objects inside ASSERT(get_num_feature_obj()==cf->get_num_feature_obj()) for (index_t f_idx=0; f_idxget_feature_obj(f_idx); + auto f1 = get_feature_obj(f_idx); + auto f2 = cf->get_feature_obj(f_idx); ASSERT(f1) ASSERT(f2) @@ -90,15 +87,12 @@ float64_t CCombinedDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t result += f1->dot(vec_idx1, f2,vec_idx2) * get_subfeature_weight(f_idx) * cf->get_subfeature_weight(f_idx); - - SG_UNREF(f1); - SG_UNREF(f2); } return result; } -float64_t CCombinedDotFeatures::dot( +float64_t CombinedDotFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { float64_t result=0; @@ -107,19 +101,18 @@ float64_t CCombinedDotFeatures::dot( for (index_t f_idx=0; f_idxget_dim_feature_space(); result += f->dot(vec_idx1, vec2.slice(offs, offs+dim)) * get_subfeature_weight(f_idx); offs += dim; - SG_UNREF(f); } return result; } -void CCombinedDotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const +void CombinedDotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const { ASSERT(stop > start) ASSERT(dim==num_dimensions) @@ -131,7 +124,7 @@ void CCombinedDotFeatures::dense_dot_range(float64_t* output, int32_t start, int for (index_t f_idx=0; f_idxget_dim_feature_space(); f->dense_dot_range( @@ -141,11 +134,10 @@ void CCombinedDotFeatures::dense_dot_range(float64_t* output, int32_t start, int offs += f_dim; - SG_UNREF(f); } } -void CCombinedDotFeatures::dense_dot_range_subset(int32_t* sub_index, int32_t num, float64_t* output, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const +void CombinedDotFeatures::dense_dot_range_subset(int32_t* sub_index, int32_t num, float64_t* output, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const { ASSERT(num > 0) ASSERT(dim==num_dimensions) @@ -157,7 +149,7 @@ void CCombinedDotFeatures::dense_dot_range_subset(int32_t* sub_index, int32_t nu for (index_t f_idx=0; f_idxget_dim_feature_space(); f->dense_dot_range_subset( @@ -167,26 +159,24 @@ void CCombinedDotFeatures::dense_dot_range_subset(int32_t* sub_index, int32_t nu offs += f_dim; - SG_UNREF(f); } } -void CCombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void CombinedDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { uint32_t offs=0; for (index_t f_idx=0; f_idxget_dim_feature_space(); f->add_to_dense_vec(alpha*get_subfeature_weight(f_idx), vec_idx1, vec2+offs, dim, abs_val); offs += dim; - SG_UNREF(f); } } -void* CCombinedDotFeatures::get_feature_iterator(int32_t vector_index) +void* CombinedDotFeatures::get_feature_iterator(int32_t vector_index) { combined_feature_iterator* it=SG_MALLOC(combined_feature_iterator, 1); @@ -197,7 +187,7 @@ void* CCombinedDotFeatures::get_feature_iterator(int32_t vector_index) return it; } -bool CCombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool CombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { ASSERT(iterator) combined_feature_iterator* it = (combined_feature_iterator*) iterator; @@ -217,7 +207,6 @@ bool CCombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, vo } it->f->free_feature_iterator(it->iterator); - SG_UNREF(it->f); it->f = get_feature_obj(it->iterator_idx); if (it->f) it->iterator=it->f->get_feature_iterator(it->vector_index); @@ -227,24 +216,24 @@ bool CCombinedDotFeatures::get_next_feature(int32_t& index, float64_t& value, vo return false; } -void CCombinedDotFeatures::free_feature_iterator(void* iterator) +void CombinedDotFeatures::free_feature_iterator(void* iterator) { if (iterator) { combined_feature_iterator* it = (combined_feature_iterator*) iterator; if (it->iterator && it->f) it->f->free_feature_iterator(it->iterator); - SG_UNREF(it->f); + SG_FREE(it); } } -CDotFeatures* CCombinedDotFeatures::get_feature_obj(int32_t idx) const +std::shared_ptr CombinedDotFeatures::get_feature_obj(int32_t idx) const { - return (CDotFeatures*) feature_array->get_element(idx); + return feature_array->get_element(idx); } -bool CCombinedDotFeatures::insert_feature_obj(CDotFeatures* obj, int32_t idx) +bool CombinedDotFeatures::insert_feature_obj(std::shared_ptr obj, int32_t idx) { ASSERT(obj) bool result=feature_array->insert_element(obj, idx); @@ -254,7 +243,7 @@ bool CCombinedDotFeatures::insert_feature_obj(CDotFeatures* obj, int32_t idx) return result; } -bool CCombinedDotFeatures::append_feature_obj(CDotFeatures* obj) +bool CombinedDotFeatures::append_feature_obj(std::shared_ptr obj) { ASSERT(obj) int n = get_num_feature_obj(); @@ -264,7 +253,7 @@ bool CCombinedDotFeatures::append_feature_obj(CDotFeatures* obj) return n+1==get_num_feature_obj(); } -bool CCombinedDotFeatures::delete_feature_obj(int32_t idx) +bool CombinedDotFeatures::delete_feature_obj(int32_t idx) { bool succesful_deletion = feature_array->delete_element(idx); if (succesful_deletion) @@ -274,26 +263,25 @@ bool CCombinedDotFeatures::delete_feature_obj(int32_t idx) return succesful_deletion; } -int32_t CCombinedDotFeatures::get_num_feature_obj() const +int32_t CombinedDotFeatures::get_num_feature_obj() const { return feature_array->get_num_elements(); } -int32_t CCombinedDotFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t CombinedDotFeatures::get_nnz_features_for_vector(int32_t num) const { int32_t result=0; for (index_t f_idx=0; f_idxget_nnz_features_for_vector(num); - SG_UNREF(f); } return result; } -SGVector CCombinedDotFeatures::get_subfeature_weights() const +SGVector CombinedDotFeatures::get_subfeature_weights() const { int32_t num_weights = get_num_feature_obj(); ASSERT(num_weights > 0) @@ -304,7 +292,7 @@ SGVector CCombinedDotFeatures::get_subfeature_weights() const return weights; } -void CCombinedDotFeatures::set_subfeature_weights(const SGVector& weights) +void CombinedDotFeatures::set_subfeature_weights(const SGVector& weights) { ASSERT(weights.vlen==get_num_feature_obj()) @@ -312,13 +300,13 @@ void CCombinedDotFeatures::set_subfeature_weights(const SGVector& wei weights.vector, weights.vector + weights.vlen, feature_weights.begin()); } -float64_t CCombinedDotFeatures::get_subfeature_weight(index_t idx) const +float64_t CombinedDotFeatures::get_subfeature_weight(index_t idx) const { ASSERT(idx >= 0 && (size_t)idx < feature_weights.size()) return feature_weights[idx]; } -void CCombinedDotFeatures::set_subfeature_weight(index_t idx, float64_t weight) +void CombinedDotFeatures::set_subfeature_weight(index_t idx, float64_t weight) { require( idx >= 0 && (size_t)idx < feature_weights.size(), @@ -327,14 +315,13 @@ void CCombinedDotFeatures::set_subfeature_weight(index_t idx, float64_t weight) feature_weights[idx] = weight; } -void CCombinedDotFeatures::init() +void CombinedDotFeatures::init() { - feature_array=new CDynamicObjectArray(); - SG_REF(feature_array); + feature_array=std::make_shared(); register_params(); } -void CCombinedDotFeatures::register_params() +void CombinedDotFeatures::register_params() { SG_ADD( &num_dimensions, "num_dimensions", "Total number of dimensions."); diff --git a/src/shogun/features/CombinedDotFeatures.h b/src/shogun/features/CombinedDotFeatures.h index 8eb05daddb7..8ff294cee58 100644 --- a/src/shogun/features/CombinedDotFeatures.h +++ b/src/shogun/features/CombinedDotFeatures.h @@ -22,8 +22,8 @@ namespace std namespace shogun { -class CFeatures; -class CDynamicObjectArray; +class Features; +class DynamicObjectArray; /** @brief Features that allow stacking of a number of DotFeatures. * * They transparently provide all the operations of DotFeatures, i.e. @@ -43,17 +43,17 @@ class CDynamicObjectArray; * \f[{\bf z'} = \alpha {\bf x} + {\bf z}\f] * */ -class CCombinedDotFeatures : public CDotFeatures +class CombinedDotFeatures : public DotFeatures { public: /** constructor */ - CCombinedDotFeatures(); + CombinedDotFeatures(); /** copy constructor */ - CCombinedDotFeatures(const CCombinedDotFeatures & orig); + CombinedDotFeatures(const CombinedDotFeatures & orig); /** destructor */ - virtual ~CCombinedDotFeatures(); + virtual ~CombinedDotFeatures(); /** get the number of vectors * @@ -80,7 +80,7 @@ class CCombinedDotFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -161,7 +161,7 @@ class CCombinedDotFeatures : public CDotFeatures struct combined_feature_iterator { /** pointer to current feature object */ - CDotFeatures* f; + std::shared_ptr f; /** pointer to combined feature iterator */ void* iterator; /// idx for iterator @@ -205,14 +205,14 @@ class CCombinedDotFeatures : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature object at position idx * * @param idx the index of the feature to return * @return next feature object */ - CDotFeatures* get_feature_obj(int32_t idx) const; + std::shared_ptr get_feature_obj(int32_t idx) const; /** insert feature object at position idx * idx must be < get_num_feature_obj() @@ -221,14 +221,14 @@ class CCombinedDotFeatures : public CDotFeatures * @param idx position where to insert the feature obj * @return if inserting was successful */ - bool insert_feature_obj(CDotFeatures* obj, int32_t idx); + bool insert_feature_obj(std::shared_ptr obj, int32_t idx); /** append feature object * * @param obj feature object to append * @return if appending was successful */ - bool append_feature_obj(CDotFeatures* obj); + bool append_feature_obj(std::shared_ptr obj); /** delete feature object at position idx * @@ -280,7 +280,7 @@ class CCombinedDotFeatures : public CDotFeatures protected: /** feature array */ - CDynamicObjectArray* feature_array; + std::shared_ptr feature_array; std::vector feature_weights; static const float64_t initial_weight; /// total number of vectors diff --git a/src/shogun/features/CombinedFeatures.cpp b/src/shogun/features/CombinedFeatures.cpp index 4b6bcc1cce8..67a363f302e 100644 --- a/src/shogun/features/CombinedFeatures.cpp +++ b/src/shogun/features/CombinedFeatures.cpp @@ -7,20 +7,21 @@ #include #include -#include -#include + +#include +#include using namespace shogun; -CCombinedFeatures::CCombinedFeatures() -: CFeatures(0) +CombinedFeatures::CombinedFeatures() +: Features(0) { init(); num_vec=0; } -CCombinedFeatures::CCombinedFeatures(const CCombinedFeatures& orig) -: CFeatures(orig) +CombinedFeatures::CombinedFeatures(const CombinedFeatures& orig) +: Features(orig) { init(); @@ -31,37 +32,33 @@ CCombinedFeatures::CCombinedFeatures(const CCombinedFeatures& orig) { auto f = orig.get_feature_obj(i); append_feature_obj(f->duplicate()); - SG_UNREF(f); } if (orig.m_subset_stack) { - auto old = m_subset_stack; - m_subset_stack=new CSubsetStack(*orig.m_subset_stack); - SG_REF(m_subset_stack); - SG_UNREF(old); + m_subset_stack=std::make_shared(*orig.m_subset_stack); } } -CFeatures* CCombinedFeatures::duplicate() const +std::shared_ptr CombinedFeatures::duplicate() const { - return new CCombinedFeatures(*this); + return std::make_shared(*this); } -CCombinedFeatures::~CCombinedFeatures() +CombinedFeatures::~CombinedFeatures() { - SG_UNREF(feature_array); + } -CFeatures* CCombinedFeatures::get_feature_obj(int32_t idx) const +std::shared_ptr CombinedFeatures::get_feature_obj(int32_t idx) const { require( idx < get_num_feature_obj() && idx>=0, "Feature index ({}) must be within [{}, {}]", idx, 0, get_num_feature_obj()-1); - return (CFeatures*) feature_array->get_element(idx); + return feature_array[idx]; } -bool CCombinedFeatures::check_feature_obj_compatibility(CCombinedFeatures* comb_feat) +bool CombinedFeatures::check_feature_obj_compatibility(std::shared_ptr comb_feat) { bool result=false; @@ -69,13 +66,11 @@ bool CCombinedFeatures::check_feature_obj_compatibility(CCombinedFeatures* comb_ { for (index_t f_idx=0; f_idxget_feature_obj(f_idx); - CFeatures* f2=comb_feat->get_feature_obj(f_idx); + auto f1=this->get_feature_obj(f_idx); + auto f2=comb_feat->get_feature_obj(f_idx); if ( ! (f1 && f2 && f1->check_feature_compatibility(f2)) ) { - SG_UNREF(f1); - SG_UNREF(f2); io::info("not compatible, combfeat"); io::info("{}", comb_feat->to_string().c_str()); io::info("vs this"); @@ -83,8 +78,8 @@ bool CCombinedFeatures::check_feature_obj_compatibility(CCombinedFeatures* comb_ return false; } - SG_UNREF(f1); - SG_UNREF(f2); + + } SG_DEBUG("features are compatible") result=true; @@ -108,17 +103,17 @@ bool CCombinedFeatures::check_feature_obj_compatibility(CCombinedFeatures* comb_ return result; } -CFeatures* CCombinedFeatures::get_first_feature_obj() const +std::shared_ptr CombinedFeatures::get_first_feature_obj() const { return get_feature_obj(0); } -CFeatures* CCombinedFeatures::get_last_feature_obj() const +std::shared_ptr CombinedFeatures::get_last_feature_obj() const { return get_feature_obj(get_num_feature_obj()-1); } -bool CCombinedFeatures::insert_feature_obj(CFeatures* obj, int32_t idx) +bool CombinedFeatures::insert_feature_obj(std::shared_ptr obj, int32_t idx) { ASSERT(obj) int32_t n=obj->get_num_vectors(); @@ -130,10 +125,11 @@ bool CCombinedFeatures::insert_feature_obj(CFeatures* obj, int32_t idx) } num_vec=n; - return feature_array->insert_element(obj, idx); + feature_array.insert(feature_array.begin()+idx, obj); + return true; } -bool CCombinedFeatures::append_feature_obj(CFeatures* obj) +bool CombinedFeatures::append_feature_obj(std::shared_ptr obj) { ASSERT(obj) int32_t n=obj->get_num_vectors(); @@ -147,30 +143,28 @@ bool CCombinedFeatures::append_feature_obj(CFeatures* obj) num_vec=n; int num_feature_obj = get_num_feature_obj(); - feature_array->push_back(obj); - return num_feature_obj+1 == feature_array->get_num_elements(); + feature_array.push_back(obj); + return num_feature_obj+1 == feature_array.size(); } -bool CCombinedFeatures::delete_feature_obj(int32_t idx) +bool CombinedFeatures::delete_feature_obj(int32_t idx) { - return feature_array->delete_element(idx); + feature_array.erase(feature_array.cbegin()+idx); + return true; } -int32_t CCombinedFeatures::get_num_feature_obj() const +int32_t CombinedFeatures::get_num_feature_obj() const { - return feature_array->get_num_elements(); + return feature_array.size(); } -void CCombinedFeatures::init() +void CombinedFeatures::init() { - feature_array = new CDynamicObjectArray(); - SG_REF(feature_array); - SG_ADD(&num_vec, "num_vec", "Number of vectors."); SG_ADD(&feature_array, "feature_array", "Feature array."); } -CFeatures* CCombinedFeatures::create_merged_copy(CFeatures* other) const +std::shared_ptr CombinedFeatures::create_merged_copy(std::shared_ptr other) const { /* TODO, if all features are the same, only one copy should be created * in memory */ @@ -183,7 +177,7 @@ CFeatures* CCombinedFeatures::create_merged_copy(CFeatures* other) const get_name()); } - auto casted = dynamic_cast(other); + auto casted = std::dynamic_pointer_cast(other); if (!casted) { @@ -197,142 +191,127 @@ CFeatures* CCombinedFeatures::create_merged_copy(CFeatures* other) const "have the same number of sub-feature-objects\n", get_name()); } - CCombinedFeatures* result=new CCombinedFeatures(); + auto result=std::make_shared(); for (index_t f_idx=0; f_idxget_feature_obj(f_idx); + auto current_this=get_feature_obj(f_idx); + auto current_other=casted->get_feature_obj(f_idx); result->append_feature_obj( current_this->create_merged_copy(current_other)); - SG_UNREF(current_this); - SG_UNREF(current_other); + + } SG_TRACE("leaving {}::create_merged_copy()", get_name()); return result; } -void CCombinedFeatures::add_subset(SGVector subset) +void CombinedFeatures::add_subset(SGVector subset) { SG_TRACE("entering {}::add_subset()", get_name()); - CSet* processed=new CSet(); + std::unordered_set> processed; for (index_t f_idx=0; f_idxcontains(current)) + if (processed.find(current) == processed.end()) { /* remember that subset was added here */ current->add_subset(subset); - processed->add(current); + processed.insert(current); SG_DEBUG("adding subset to {} at {}", - current->get_name(), fmt::ptr(current)); + current->get_name(), fmt::ptr(current.get())); } - SG_UNREF(current); + } /* also add subset to local stack to have it for easy access */ m_subset_stack->add_subset(subset); subset_changed_post(); - SG_UNREF(processed); SG_TRACE("leaving {}::add_subset()", get_name()); } -void CCombinedFeatures::remove_subset() +void CombinedFeatures::remove_subset() { SG_TRACE("entering {}::remove_subset()", get_name()); - CSet* processed=new CSet(); + std::unordered_set> processed; for (index_t f_idx=0; f_idxcontains(current)) + auto current=get_feature_obj(f_idx); + if (processed.find(current) == processed.end()) { /* remember that subset was added here */ current->remove_subset(); - processed->add(current); + processed.insert(current); SG_DEBUG("removing subset from {} at {}", - current->get_name(), fmt::ptr(current)); + current->get_name(), fmt::ptr(current.get())); } - SG_UNREF(current); + } /* also remove subset from local stack to have it for easy access */ m_subset_stack->remove_subset(); subset_changed_post(); - SG_UNREF(processed); SG_TRACE("leaving {}::remove_subset()", get_name()); } -void CCombinedFeatures::remove_all_subsets() +void CombinedFeatures::remove_all_subsets() { SG_TRACE("entering {}::remove_all_subsets()", get_name()); - CSet* processed=new CSet(); + std::unordered_set> processed; for (index_t f_idx=0; f_idxcontains(current)) + auto current=get_feature_obj(f_idx); + if (processed.find(current) == processed.end()) { /* remember that subset was added here */ current->remove_all_subsets(); - processed->add(current); + processed.insert(current); SG_DEBUG("removing all subsets from {} at {}", - current->get_name(), fmt::ptr(current)); + current->get_name(), fmt::ptr(current.get())); } - SG_UNREF(current); + } /* also remove subsets from local stack to have it for easy access */ m_subset_stack->remove_all_subsets(); subset_changed_post(); - SG_UNREF(processed); SG_TRACE("leaving {}::remove_all_subsets()", get_name()); } -CFeatures* CCombinedFeatures::copy_subset(SGVector indices) const +std::shared_ptr CombinedFeatures::copy_subset(SGVector indices) const { /* this is returned with the results of copy_subset of sub-features */ - CCombinedFeatures* result=new CCombinedFeatures(); + auto result=std::make_shared(); /* map to only copy same feature objects once */ - CMap* processed=new CMap(); + std::unordered_map, std::shared_ptr> processed; for (index_t f_idx=0; f_idx new_element=NULL; /* only copy if not done yet, otherwise, use old copy */ - if (!processed->contains(current)) + if (processed.find(current) == processed.end()) { new_element=current->copy_subset(indices); - processed->add(current, new_element); + processed.emplace(current, new_element); } else { - new_element=processed->get_element(current); - - /* has to be SG_REF'ed since it will be unrefed afterwards */ - SG_REF(new_element); + new_element=processed[current]; } /* add to result */ result->append_feature_obj(new_element); - - /* clean up: copy_subset of SG_REF has to be undone */ - SG_UNREF(new_element); - - SG_UNREF(current); } - - SG_UNREF(processed); - - SG_REF(result); return result; } diff --git a/src/shogun/features/CombinedFeatures.h b/src/shogun/features/CombinedFeatures.h index ae7ea15a3ca..cc9a1ea3b63 100644 --- a/src/shogun/features/CombinedFeatures.h +++ b/src/shogun/features/CombinedFeatures.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Evangelos Anagnostopoulos, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Evangelos Anagnostopoulos, * Vladislav Horbatiuk, Yuyu Zhang, Evgeniy Andreev, Bjoern Esser */ @@ -15,14 +15,14 @@ namespace shogun { -class CFeatures; -class CDynamicObjectArray; +class Features; +class DynamicObjectArray; /** @brief The class CombinedFeatures is used to combine a number of of feature objects * into a single CombinedFeatures object. * * It keeps pointers to the added sub-features and is especially useful to - * combine kernels working on different domains (c.f. CCombinedKernel) and to + * combine kernels working on different domains (c.f. CombinedKernel) and to * combine kernels looking at independent features. * * Subsets are supported: All actions will just be given through to all @@ -30,22 +30,22 @@ class CDynamicObjectArray; * sub-features that are the same instance, the subset action will only be * performed once. */ -class CCombinedFeatures : public CFeatures +class CombinedFeatures : public Features { public: /** default constructor */ - CCombinedFeatures(); + CombinedFeatures(); /** copy constructor */ - CCombinedFeatures(const CCombinedFeatures& orig); + CombinedFeatures(const CombinedFeatures& orig); /** duplicate feature object * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** destructor */ - virtual ~CCombinedFeatures(); + virtual ~CombinedFeatures(); /** get feature type * @@ -80,26 +80,26 @@ class CCombinedFeatures : public CFeatures * @param comb_feat feature to check for compatibility * @return if feature is compatible */ - bool check_feature_obj_compatibility(CCombinedFeatures* comb_feat); + bool check_feature_obj_compatibility(std::shared_ptr comb_feat); /** get first feature object * * @return first feature object */ - CFeatures* get_first_feature_obj() const; + std::shared_ptr get_first_feature_obj() const; /** get feature object at index idx * * @param idx index of feature object * @return the feature object at index idx */ - CFeatures* get_feature_obj(int32_t idx) const; + std::shared_ptr get_feature_obj(int32_t idx) const; /** get last feature object * * @return last feature object */ - CFeatures* get_last_feature_obj() const; + std::shared_ptr get_last_feature_obj() const; /** insert feature object at index idx * Important, idx must be < num_feature_obj @@ -108,14 +108,14 @@ class CCombinedFeatures : public CFeatures * @param idx the index where to insert the feature object * @return if inserting was successful */ - bool insert_feature_obj(CFeatures* obj, int32_t idx); + bool insert_feature_obj(std::shared_ptr obj, int32_t idx); /** append feature object to the end of this CombinedFeatures object array * * @param obj feature object to append * @return if appending was successful */ - bool append_feature_obj(CFeatures* obj); + bool append_feature_obj(std::shared_ptr obj); /** delete feature object at position idx * @@ -140,7 +140,7 @@ class CCombinedFeatures : public CFeatures * @return new feature object which contains copy of data of this * instance and of given one */ - CFeatures* create_merged_copy(CFeatures* other) const; + std::shared_ptr create_merged_copy(std::shared_ptr other) const; /** adds a subset of indices on top of the current subsets (possibly * subset o subset. Calls subset_changed_post() afterwards. @@ -164,15 +164,15 @@ class CCombinedFeatures : public CFeatures * */ virtual void remove_all_subsets(); - /** Creates a new CFeatures instance containing copies of the elements + /** Creates a new Features instance containing copies of the elements * which are specified by the provided indices. * Simply creates a combined features instance where all sub-features * are the results of their copy_subset calls * * @param indices indices of feature elements to copy - * @return new CFeatures instance with copies of feature data + * @return new Features instance with copies of feature data */ - virtual CFeatures* copy_subset(SGVector indices) const; + virtual std::shared_ptr copy_subset(SGVector indices) const; /** @return object name */ virtual const char* get_name() const { return "CombinedFeatures"; } @@ -182,7 +182,7 @@ class CCombinedFeatures : public CFeatures protected: /** feature array */ - CDynamicObjectArray* feature_array; + std::vector> feature_array; /** number of vectors * must match between sub features diff --git a/src/shogun/features/DataGenerator.cpp b/src/shogun/features/DataGenerator.cpp index 55bdf474402..b00b2ac93e1 100644 --- a/src/shogun/features/DataGenerator.cpp +++ b/src/shogun/features/DataGenerator.cpp @@ -19,22 +19,22 @@ using namespace shogun; -CDataGenerator::CDataGenerator() : CSGObject() +DataGenerator::DataGenerator() : SGObject() { init(); } -CDataGenerator::~CDataGenerator() +DataGenerator::~DataGenerator() { } -void CDataGenerator::init() +void DataGenerator::init() { } template -SGMatrix CDataGenerator::generate_checkboard_data(int32_t num_classes, +SGMatrix DataGenerator::generate_checkboard_data(int32_t num_classes, int32_t dim, int32_t num_points, float64_t overlap, PRNG& prng) { int32_t points_per_class = num_points / num_classes; @@ -86,12 +86,12 @@ SGMatrix CDataGenerator::generate_checkboard_data(int32_t num_classes return points; } -template SGMatrix CDataGenerator::generate_checkboard_data(int32_t num_classes, +template SGMatrix DataGenerator::generate_checkboard_data(int32_t num_classes, int32_t dim, int32_t num_points, float64_t overlap, std::mt19937_64& prng); template -SGMatrix CDataGenerator::generate_mean_data(index_t m, +SGMatrix DataGenerator::generate_mean_data(index_t m, index_t dim, float64_t mean_shift, PRNG& prng, SGMatrix target) { @@ -110,13 +110,13 @@ SGMatrix CDataGenerator::generate_mean_data(index_t m, return result; } -template SGMatrix CDataGenerator::generate_mean_data(index_t m, +template SGMatrix DataGenerator::generate_mean_data(index_t m, index_t dim, float64_t mean_shift, std::mt19937_64& prng, SGMatrix target); template -SGMatrix CDataGenerator::generate_sym_mix_gauss(index_t m, +SGMatrix DataGenerator::generate_sym_mix_gauss(index_t m, float64_t d, float64_t angle, PRNG& prng, SGMatrix target) { /* evtl. allocate space */ @@ -147,12 +147,12 @@ SGMatrix CDataGenerator::generate_sym_mix_gauss(index_t m, return result; } -template SGMatrix CDataGenerator::generate_sym_mix_gauss(index_t m, +template SGMatrix DataGenerator::generate_sym_mix_gauss(index_t m, float64_t d, float64_t angle, std::mt19937_64& prng, SGMatrix target); template -SGMatrix CDataGenerator::generate_gaussians(index_t m, index_t n, index_t dim, PRNG& prng) +SGMatrix DataGenerator::generate_gaussians(index_t m, index_t n, index_t dim, PRNG& prng) { /* evtl. allocate space */ SGMatrix result = @@ -171,7 +171,7 @@ SGMatrix CDataGenerator::generate_gaussians(index_t m, index_t n, ind if (k % (i+1) == 0) mean[k] *= -1; } - CGaussian* g = new CGaussian(mean, cov, DIAG); + auto g = std::make_shared(mean, cov, DIAG); random::seed(g, prng); for (index_t j = 0; j < m; ++j) { @@ -179,12 +179,12 @@ SGMatrix CDataGenerator::generate_gaussians(index_t m, index_t n, ind sg_memcpy((result.matrix+j*result.num_rows+i*m*dim), v.vector, dim*sizeof(float64_t)); } - SG_UNREF(g); + } return result; } -template SGMatrix CDataGenerator::generate_gaussians( +template SGMatrix DataGenerator::generate_gaussians( index_t m, index_t n, index_t dim, std::mt19937_64& prng); diff --git a/src/shogun/features/DataGenerator.h b/src/shogun/features/DataGenerator.h index ef021651dc8..19c68065447 100644 --- a/src/shogun/features/DataGenerator.h +++ b/src/shogun/features/DataGenerator.h @@ -21,12 +21,12 @@ namespace shogun /** @brief Class that is able to generate various data samples, which may be * used for examples in SHOGUN. */ -class CDataGenerator: public CSGObject +class DataGenerator: public SGObject { public: - CDataGenerator(); + DataGenerator(); - virtual ~CDataGenerator(); + virtual ~DataGenerator(); /** Generate points for classification tasks. Every dimension is in the range [0,1]. You can * scale or translate the features afterwards. It works be creating a grid in the n-dimensional space diff --git a/src/shogun/features/DenseFeatures.cpp b/src/shogun/features/DenseFeatures.cpp index 8b0877c4087..2010ee20fac 100644 --- a/src/shogun/features/DenseFeatures.cpp +++ b/src/shogun/features/DenseFeatures.cpp @@ -8,7 +8,6 @@ * Christopher Goldsworthy */ -#include #include #include #include @@ -34,13 +33,13 @@ namespace shogun { -template CDenseFeatures::CDenseFeatures(int32_t size) : CDotFeatures(size) +template DenseFeatures::DenseFeatures(int32_t size) : DotFeatures(size) { init(); } -template CDenseFeatures::CDenseFeatures(const CDenseFeatures & orig) : - CDotFeatures(orig) +template DenseFeatures::DenseFeatures(const DenseFeatures & orig) : + DotFeatures(orig) { init(); set_feature_matrix(orig.feature_matrix); @@ -48,34 +47,34 @@ template CDenseFeatures::CDenseFeatures(const CDenseFeatures & ori if (orig.m_subset_stack != NULL) { - SG_UNREF(m_subset_stack); - m_subset_stack=new CSubsetStack(*orig.m_subset_stack); - SG_REF(m_subset_stack); + + m_subset_stack=std::make_shared(*orig.m_subset_stack); + } } -template CDenseFeatures::CDenseFeatures(SGMatrix matrix) : - CDotFeatures() +template DenseFeatures::DenseFeatures(SGMatrix matrix) : + DotFeatures() { init(); set_feature_matrix(matrix); } -template CDenseFeatures::CDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec) : - CDotFeatures() +template DenseFeatures::DenseFeatures(ST* src, int32_t num_feat, int32_t num_vec) : + DotFeatures() { init(); set_feature_matrix(SGMatrix(src, num_feat, num_vec)); } -template CDenseFeatures::CDenseFeatures(CFile* loader) : - CDotFeatures() +template DenseFeatures::DenseFeatures(std::shared_ptr loader) : + DotFeatures() { init(); load(loader); } -template CDenseFeatures::CDenseFeatures(CDotFeatures* features) : - CDotFeatures() +template DenseFeatures::DenseFeatures(std::shared_ptr features) : + DotFeatures() { init(); @@ -96,23 +95,23 @@ template CDenseFeatures::CDenseFeatures(CDotFeatures* features) : num_vectors = num_vec; } -template CFeatures* CDenseFeatures::duplicate() const +template std::shared_ptr DenseFeatures::duplicate() const { - return new CDenseFeatures(*this); + return std::make_shared(*this); } -template CDenseFeatures::~CDenseFeatures() +template DenseFeatures::~DenseFeatures() { free_features(); } -template void CDenseFeatures::free_features() +template void DenseFeatures::free_features() { free_feature_matrix(); - SG_UNREF(feature_cache); + } -template void CDenseFeatures::free_feature_matrix() +template void DenseFeatures::free_feature_matrix() { m_subset_stack->remove_all_subsets(); feature_matrix=SGMatrix(); @@ -120,7 +119,7 @@ template void CDenseFeatures::free_feature_matrix() num_features = 0; } -template ST* CDenseFeatures::get_feature_vector(int32_t num, int32_t& len, bool& dofree) const +template ST* DenseFeatures::get_feature_vector(int32_t num, int32_t& len, bool& dofree) const { /* index conversion for subset, only for array access */ int32_t real_num=m_subset_stack->subset_idx_conversion(num); @@ -158,7 +157,7 @@ template ST* CDenseFeatures::get_feature_vector(int32_t num, int32 for (auto i = 0; i < get_num_preprocessors(); i++) { auto preprocessor = - get_preprocessor(i)->template as>(); + get_preprocessor(i)->template as>(); // temporary hack SGVector applied = preprocessor->apply_to_feature_vector(feat_vec); @@ -176,7 +175,7 @@ template ST* CDenseFeatures::get_feature_vector(int32_t num, int32 return feat; } -template SGVector CDenseFeatures::get_feature_vector(int32_t num) const +template SGVector DenseFeatures::get_feature_vector(int32_t num) const { /* index conversion for subset, only for array access */ int32_t real_num=m_subset_stack->subset_idx_conversion(num); @@ -193,7 +192,7 @@ template SGVector CDenseFeatures::get_feature_vector(int32_t n return SGVector(vector, vlen, do_free); } -template void CDenseFeatures::free_feature_vector(ST* feat_vec, int32_t num, bool dofree) const +template void DenseFeatures::free_feature_vector(ST* feat_vec, int32_t num, bool dofree) const { if (feature_cache) feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num)); @@ -202,13 +201,13 @@ template void CDenseFeatures::free_feature_vector(ST* feat_vec, in SG_FREE(feat_vec); } -template void CDenseFeatures::free_feature_vector(SGVector vec, int32_t num) const +template void DenseFeatures::free_feature_vector(SGVector vec, int32_t num) const { free_feature_vector(vec.vector, num, false); vec=SGVector(); } -template void CDenseFeatures::vector_subset(int32_t* idx, int32_t idx_len) +template void DenseFeatures::vector_subset(int32_t* idx, int32_t idx_len) { if (m_subset_stack->has_subsets()) error("A subset is set, cannot call vector_subset"); @@ -239,7 +238,7 @@ template void CDenseFeatures::vector_subset(int32_t* idx, int32_t } } -template void CDenseFeatures::feature_subset(int32_t* idx, int32_t idx_len) +template void DenseFeatures::feature_subset(int32_t* idx, int32_t idx_len) { if (m_subset_stack->has_subsets()) error("A subset is set, cannot call feature_subset"); @@ -269,7 +268,7 @@ template void CDenseFeatures::feature_subset(int32_t* idx, int32_t } template -SGMatrix CDenseFeatures::get_feature_matrix() const +SGMatrix DenseFeatures::get_feature_matrix() const { if (!m_subset_stack->has_subsets()) return feature_matrix; @@ -280,7 +279,7 @@ SGMatrix CDenseFeatures::get_feature_matrix() const } template -void CDenseFeatures::copy_feature_matrix(SGMatrix target, index_t column_offset) const +void DenseFeatures::copy_feature_matrix(SGMatrix target, index_t column_offset) const { require(column_offset>=0, "Column offset ({}) cannot be negative!", column_offset); require(!target.equals(feature_matrix), "Source and target feature matrices cannot be the same"); @@ -314,7 +313,7 @@ void CDenseFeatures::copy_feature_matrix(SGMatrix target, index_t column } } -template void CDenseFeatures::set_feature_matrix(SGMatrix matrix) +template void DenseFeatures::set_feature_matrix(SGMatrix matrix) { m_subset_stack->remove_all_subsets(); free_feature_matrix(); @@ -324,7 +323,7 @@ template void CDenseFeatures::set_feature_matrix(SGMatrix matr } template -ST* CDenseFeatures::get_feature_matrix( +ST* DenseFeatures::get_feature_matrix( int32_t& num_feat, int32_t& num_vec) const { num_feat = num_features; @@ -332,16 +331,16 @@ ST* CDenseFeatures::get_feature_matrix( return feature_matrix.matrix; } -template CDenseFeatures* CDenseFeatures::get_transposed() +template std::shared_ptr> DenseFeatures::get_transposed() { int32_t num_feat; int32_t num_vec; - ST* fm = get_transposed(num_feat, num_vec); + auto fm = get_transposed(num_feat, num_vec); - return new CDenseFeatures(fm, num_feat, num_vec); + return std::make_shared(fm, num_feat, num_vec); } -template ST* CDenseFeatures::get_transposed(int32_t &num_feat, int32_t &num_vec) +template ST* DenseFeatures::get_transposed(int32_t &num_feat, int32_t &num_vec) { num_feat = get_num_vectors(); num_vec = num_features; @@ -363,20 +362,20 @@ template ST* CDenseFeatures::get_transposed(int32_t &num_feat, int return fm; } -template int32_t CDenseFeatures::get_num_vectors() const +template int32_t DenseFeatures::get_num_vectors() const { return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : num_vectors; } -template int32_t CDenseFeatures::get_num_features() const { return num_features; } +template int32_t DenseFeatures::get_num_features() const { return num_features; } -template void CDenseFeatures::set_num_features(int32_t num) +template void DenseFeatures::set_num_features(int32_t num) { num_features = num; initialize_cache(); } -template void CDenseFeatures::set_num_vectors(int32_t num) +template void DenseFeatures::set_num_vectors(int32_t num) { if (m_subset_stack->has_subsets()) error("A subset is set, cannot call set_num_vectors"); @@ -385,31 +384,31 @@ template void CDenseFeatures::set_num_vectors(int32_t num) initialize_cache(); } -template void CDenseFeatures::initialize_cache() +template void DenseFeatures::initialize_cache() { if (m_subset_stack->has_subsets()) error("A subset is set, cannot call initialize_cache"); if (num_features && num_vectors) { - SG_UNREF(feature_cache); - feature_cache = new CCache(get_cache_size(), num_features, + + feature_cache = std::make_shared>(get_cache_size(), num_features, num_vectors); - SG_REF(feature_cache); + } } -template EFeatureClass CDenseFeatures::get_feature_class() const { return C_DENSE; } +template EFeatureClass DenseFeatures::get_feature_class() const { return C_DENSE; } -template int32_t CDenseFeatures::get_dim_feature_space() const { return num_features; } +template int32_t DenseFeatures::get_dim_feature_space() const { return num_features; } -template float64_t CDenseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, +template float64_t DenseFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CDenseFeatures* sf = (CDenseFeatures*) df; + auto sf = std::static_pointer_cast>(df); int32_t len1, len2; bool free1, free2; @@ -427,7 +426,7 @@ template float64_t CDenseFeatures::dot(int32_t vec_idx1, CDotFeatu return result; } -template void CDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, +template void DenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { ASSERT(vec2_len == num_features) @@ -441,7 +440,7 @@ template void CDenseFeatures::add_to_dense_vec(float64_t alpha, in if (abs_val) { for (int32_t i = 0; i < num_features; i++) - vec2[i] += alpha * CMath::abs(vec1[i]); + vec2[i] += alpha * Math::abs(vec1[i]); } else { @@ -453,7 +452,7 @@ template void CDenseFeatures::add_to_dense_vec(float64_t alpha, in } template<> -void CDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, +void DenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { ASSERT(vec2_len == num_features) @@ -467,7 +466,7 @@ void CDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_id if (abs_val) { for (int32_t i = 0; i < num_features; i++) - vec2[i] += alpha * CMath::abs(vec1[i]); + vec2[i] += alpha * Math::abs(vec1[i]); } else { @@ -477,12 +476,12 @@ void CDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_id free_feature_vector(vec1, vec_idx1, vfree); } -template int32_t CDenseFeatures::get_nnz_features_for_vector(int32_t num) const +template int32_t DenseFeatures::get_nnz_features_for_vector(int32_t num) const { return num_features; } -template void* CDenseFeatures::get_feature_iterator(int32_t vector_index) +template void* DenseFeatures::get_feature_iterator(int32_t vector_index) { if (vector_index>=get_num_vectors()) { @@ -498,7 +497,7 @@ template void* CDenseFeatures::get_feature_iterator(int32_t vector return iterator; } -template bool CDenseFeatures::get_next_feature(int32_t& index, float64_t& value, +template bool DenseFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { dense_feature_iterator* it = (dense_feature_iterator*) iterator; @@ -511,7 +510,7 @@ template bool CDenseFeatures::get_next_feature(int32_t& index, flo return true; } -template void CDenseFeatures::free_feature_iterator(void* iterator) +template void DenseFeatures::free_feature_iterator(void* iterator) { if (!iterator) return; @@ -521,7 +520,7 @@ template void CDenseFeatures::free_feature_iterator(void* iterator SG_FREE(it); } -template CFeatures* CDenseFeatures::copy_subset(SGVector indices) const +template std::shared_ptr DenseFeatures::copy_subset(SGVector indices) const { SGMatrix feature_matrix_copy(num_features, indices.vlen); @@ -533,19 +532,17 @@ template CFeatures* CDenseFeatures::copy_subset(SGVector num_features*sizeof(ST)); } - CFeatures* result=new CDenseFeatures(feature_matrix_copy); - SG_REF(result); - return result; + return std::make_shared(feature_matrix_copy); } template -CFeatures* CDenseFeatures::copy_dimension_subset(SGVector dims) const +std::shared_ptr DenseFeatures::copy_dimension_subset(SGVector dims) const { SG_TRACE("Entering!"); // sanity checks - index_t max=CMath::max(dims.vector, dims.vlen); - index_t min=CMath::min(dims.vector, dims.vlen); + index_t max=Math::max(dims.vector, dims.vlen); + index_t min=Math::min(dims.vector, dims.vlen); require(max=0, "Provided dimensions is in the range [{}, {}] but they " "have to be within [0, {}]! But it ", min, max, num_features); @@ -561,29 +558,26 @@ CFeatures* CDenseFeatures::copy_dimension_subset(SGVector dims) con } } - CFeatures* result=new CDenseFeatures(feature_matrix_copy); - SG_REF(result); - SG_TRACE("Leaving!"); - return result; + return std::make_shared(feature_matrix_copy); } template -CFeatures* CDenseFeatures::shallow_subset_copy() +std::shared_ptr DenseFeatures::shallow_subset_copy() { - CFeatures* shallow_copy_features=NULL; + std::shared_ptr shallow_copy_features=NULL; SG_DEBUG("Using underlying feature matrix with {} dimensions and {} feature vectors!", num_features, num_vectors); SGMatrix shallow_copy_matrix(feature_matrix); - shallow_copy_features=new CDenseFeatures(shallow_copy_matrix); - SG_REF(shallow_copy_features); + shallow_copy_features=std::make_shared(shallow_copy_matrix); + if (m_subset_stack->has_subsets()) shallow_copy_features->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); return shallow_copy_features; } -template ST* CDenseFeatures::compute_feature_vector(int32_t num, int32_t& len, +template ST* DenseFeatures::compute_feature_vector(int32_t num, int32_t& len, ST* target) const { not_implemented(SOURCE_LOCATION); @@ -591,7 +585,7 @@ template ST* CDenseFeatures::compute_feature_vector(int32_t num, i return NULL; } -template void CDenseFeatures::init() +template void DenseFeatures::init() { num_vectors = 0; num_features = 0; @@ -609,7 +603,7 @@ template void CDenseFeatures::init() } #define GET_FEATURE_TYPE(f_type, sg_type) \ -template<> EFeatureType CDenseFeatures::get_feature_type() const \ +template<> EFeatureType DenseFeatures::get_feature_type() const \ { \ return f_type; \ } @@ -631,7 +625,7 @@ GET_FEATURE_TYPE(F_LONGREAL, floatmax_t) template float64_t -CDenseFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +DenseFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { SGVector vec1 = get_feature_vector(vec_idx1); float64_t result = linalg::dot(vec2, vec1, linalg::allow_cast{}); @@ -639,7 +633,7 @@ CDenseFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return result; } -template bool CDenseFeatures::is_equal(CDenseFeatures* rhs) +template bool DenseFeatures::is_equal(std::shared_ptr rhs) { if ( num_features != rhs->num_features || num_vectors != rhs->num_vectors ) return false; @@ -674,19 +668,16 @@ template bool CDenseFeatures::is_equal(CDenseFeatures* rhs) } template -CFeatures* CDenseFeatures::create_merged_copy(CList* others) const +std::shared_ptr DenseFeatures::create_merged_copy(const std::vector>& others) const { SG_TRACE("Entering."); - require(others!=nullptr, "The list of other feature instances is not initialized!"); - - auto current=others->get_first_element(); + require(others.size() > 0, "The list of other feature instances is not initialized!"); auto total_num_vectors=get_num_vectors(); - auto unref_required=others->get_delete_data(); - while (current!=nullptr) + for (auto current: others) { - auto casted = dynamic_cast*>(current); + auto casted = current->as>(); require(casted!=nullptr, "Provided object's type ({}) must match own type ({})!", current->get_name(), get_name()); @@ -695,11 +686,6 @@ CFeatures* CDenseFeatures::create_merged_copy(CList* others) const casted->num_features, num_features); total_num_vectors+=casted->get_num_vectors(); - - if (unref_required) - SG_UNREF(current); - - current=others->get_next_element(); } SGMatrix data(num_features, total_num_vectors); @@ -707,36 +693,26 @@ CFeatures* CDenseFeatures::create_merged_copy(CList* others) const copy_feature_matrix(data, num_copied); num_copied+=get_num_vectors(); - current=others->get_first_element(); - - while (current!=nullptr) + for (auto current: others) { - auto casted = static_cast*>(current); + auto casted = current->as>(); casted->copy_feature_matrix(data, num_copied); num_copied+=casted->get_num_vectors(); - - if (unref_required) - SG_UNREF(current); - - current=others->get_next_element(); } - auto result=new CDenseFeatures(data); - SG_TRACE("Leaving."); - return result; + return std::make_shared(data); } template -CFeatures* CDenseFeatures::create_merged_copy(CFeatures* other) const +std::shared_ptr DenseFeatures::create_merged_copy(std::shared_ptr other) const { - auto list=some(); - list->append_element(other); - return create_merged_copy(list); + std::vector> v {other}; + return create_merged_copy(v); } template -void CDenseFeatures::load(CFile* loader) +void DenseFeatures::load(std::shared_ptr loader) { SGMatrix matrix; matrix.load(loader); @@ -744,21 +720,21 @@ void CDenseFeatures::load(CFile* loader) } template -void CDenseFeatures::save(CFile* writer) +void DenseFeatures::save(std::shared_ptr writer) { feature_matrix.save(writer); } -template< class ST > CDenseFeatures< ST >* CDenseFeatures< ST >::obtain_from_generic(CFeatures* const base_features) +template< class ST > std::shared_ptr> DenseFeatures< ST >::obtain_from_generic(std::shared_ptr base_features) { require(base_features->get_feature_class() == C_DENSE, - "base_features must be of dynamic type CDenseFeatures"); + "base_features must be of dynamic type DenseFeatures"); - return (CDenseFeatures< ST >*) base_features; + return std::static_pointer_cast>(base_features); } template -SGVector CDenseFeatures::sum() const +SGVector DenseFeatures::sum() const { // TODO optimize non batch mode, but get_feature_vector is non const :( SGVector result = linalg::rowwise_sum(get_feature_matrix()); @@ -766,7 +742,7 @@ SGVector CDenseFeatures::sum() const } template -SGVector CDenseFeatures::mean() const +SGVector DenseFeatures::mean() const { ASSERT_FLOATING_POINT @@ -777,7 +753,7 @@ SGVector CDenseFeatures::mean() const } template -SGVector CDenseFeatures::std(bool colwise) const +SGVector DenseFeatures::std(bool colwise) const { ASSERT_FLOATING_POINT @@ -786,7 +762,7 @@ SGVector CDenseFeatures::std(bool colwise) const } template -SGMatrix CDenseFeatures::cov() const +SGMatrix DenseFeatures::cov() const { // TODO optimize non batch mode, but get_feature_vector is non const :( auto mat = get_feature_matrix(); @@ -794,7 +770,7 @@ SGMatrix CDenseFeatures::cov() const } template -SGMatrix CDenseFeatures::gram() const +SGMatrix DenseFeatures::gram() const { // TODO optimize non batch mode, but get_feature_vector is non const :( auto mat = get_feature_matrix(); @@ -802,7 +778,7 @@ SGMatrix CDenseFeatures::gram() const } template -SGVector CDenseFeatures::dot(const SGVector& other) const +SGVector DenseFeatures::dot(const SGVector& other) const { require( get_num_vectors() == other.size(), "Number of feature vectors ({}) " @@ -813,17 +789,17 @@ SGVector CDenseFeatures::dot(const SGVector& other) const return linalg::matrix_prod(get_feature_matrix(), other, false); } -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; -template class CDenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; +template class DenseFeatures; } diff --git a/src/shogun/features/DenseFeatures.h b/src/shogun/features/DenseFeatures.h index baa700a417f..5ee3bc24a4d 100644 --- a/src/shogun/features/DenseFeatures.h +++ b/src/shogun/features/DenseFeatures.h @@ -20,10 +20,10 @@ #include namespace shogun { -template class CStringFeatures; -template class CDenseFeatures; +template class StringFeatures; +template class DenseFeatures; template class SGMatrix; -class CDotFeatures; +class DotFeatures; /** @brief The class DenseFeatures implements dense feature matrices. * @@ -40,43 +40,43 @@ class CDotFeatures; * From this template class a number the following dense feature matrix types * are used and supported: * - * \li bool matrix - CDenseFeatures - * \li 8bit char matrix - CDenseFeatures - * \li 8bit Byte matrix - CDenseFeatures - * \li 16bit Integer matrix - CDenseFeatures - * \li 16bit Word matrix - CDenseFeatures - * \li 32bit Integer matrix - CDenseFeatures - * \li 32bit Unsigned Integer matrix - CDenseFeatures - * \li 32bit Float matrix - CDenseFeatures - * \li 64bit Float matrix - CDenseFeatures + * \li bool matrix - DenseFeatures + * \li 8bit char matrix - DenseFeatures + * \li 8bit Byte matrix - DenseFeatures + * \li 16bit Integer matrix - DenseFeatures + * \li 16bit Word matrix - DenseFeatures + * \li 32bit Integer matrix - DenseFeatures + * \li 32bit Unsigned Integer matrix - DenseFeatures + * \li 32bit Float matrix - DenseFeatures + * \li 64bit Float matrix - DenseFeatures * \li 64bit Float matrix in a file - CRealFileFeatures * \li 64bit Tangent of posterior log-odds (TOP) features from HMM - CTOPFeatures * \li 64bit Fisher Kernel (FK) features from HMM - CTOPFeatures - * \li 96bit Float matrix - CDenseFeatures + * \li 96bit Float matrix - DenseFeatures * * Partly) subset access is supported for this feature type. * Dense use the (inherited) add_subset(), remove_subset() functions. * If done, all calls that work with features are translated to the subset. * See comments to find out whether it is supported for that method. - * See also CFeatures class documentation + * See also Features class documentation */ -template class CDenseFeatures: public CDotFeatures +template class DenseFeatures: public DotFeatures { public: /** constructor * * @param size cache size */ - CDenseFeatures(int32_t size = 0); + DenseFeatures(int32_t size = 0); /** copy constructor */ - CDenseFeatures(const CDenseFeatures & orig); + DenseFeatures(const DenseFeatures & orig); /** constructor * * @param matrix feature matrix */ - CDenseFeatures(SGMatrix matrix); + DenseFeatures(SGMatrix matrix); /** constructor * @@ -84,27 +84,27 @@ template class CDenseFeatures: public CDotFeatures * @param num_feat number of features in matrix * @param num_vec number of vectors in matrix */ - CDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec); + DenseFeatures(ST* src, int32_t num_feat, int32_t num_vec); /** constructor from DotFeatures * * @param features DotFeatures object */ - CDenseFeatures(CDotFeatures* features); + DenseFeatures(std::shared_ptr features); /** constructor loading features from file * * @param loader File object via which to load data */ - CDenseFeatures(CFile* loader); + DenseFeatures(std::shared_ptr loader); /** duplicate feature object * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; - virtual ~CDenseFeatures(); + virtual ~DenseFeatures(); /** free feature matrix * @@ -209,7 +209,7 @@ template class CDenseFeatures: public CDotFeatures * * @return transposed copy */ - CDenseFeatures* get_transposed(); + std::shared_ptr> get_transposed(); /** compute and return the transpose of the feature matrix * which will be prepocessed. @@ -286,7 +286,7 @@ template class CDenseFeatures: public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** Computes the sum of all feature vectors @@ -344,7 +344,7 @@ template class CDenseFeatures: public CDotFeatures * @param vec2 dense vector */ virtual float64_t - dot(int32_t vec_idx1, const SGVector& vec2) const override; + dot(int32_t vec_idx1, const SGVector& vec2) const; /** add vector 1 multiplied with alpha to dense vector2 * @@ -370,13 +370,13 @@ template class CDenseFeatures: public CDotFeatures * * @param loader File object via which to load data */ - virtual void load(CFile* loader); + virtual void load(std::shared_ptr loader); /** save features to file * * @param saver File object via which to save data */ - virtual void save(CFile* saver); + virtual void save(std::shared_ptr saver); #ifndef DOXYGEN_SHOULD_SKIP_THIS /** iterator for dense features */ @@ -431,17 +431,17 @@ template class CDenseFeatures: public CDotFeatures */ virtual void free_feature_iterator(void* iterator); - /** Creates a new CFeatures instance containing copies of the elements + /** Creates a new Features instance containing copies of the elements * which are specified by the provided indices. * * possible with subset * * @param indices indices of feature elements to copy - * @return new CFeatures instance with copies of feature data + * @return new Features instance with copies of feature data */ - virtual CFeatures* copy_subset(SGVector indices) const; + virtual std::shared_ptr copy_subset(SGVector indices) const; - /** Creates a new CFeatures instance containing only the dimensions + /** Creates a new Features instance containing only the dimensions * of the feature vector which are specified by the provided indices. * * This method is needed for feature selection tasks @@ -449,17 +449,17 @@ template class CDenseFeatures: public CDotFeatures * possible with subset * * @param dims indices of feature dimensions to copy - * @return new CFeatures instance with copies of specified features + * @return new Features instance with copies of specified features */ - virtual CFeatures* copy_dimension_subset(SGVector dims) const; + virtual std::shared_ptr copy_dimension_subset(SGVector dims) const; - /** checks if the contents of this CDenseFeatures object are the same to + /** checks if the contents of this DenseFeatures object are the same to * the contents of rhs * - * @param rhs other CDenseFeatures object to compare to this one + * @param rhs other DenseFeatures object to compare to this one * @return whether they represent the same information */ - virtual bool is_equal(CDenseFeatures* rhs); + virtual bool is_equal(std::shared_ptr rhs); /** Takes a list of feature instances and returns a new instance which is * a concatenation of a copy if this instace's data and the given @@ -470,7 +470,7 @@ template class CDenseFeatures: public CDotFeatures * @return new feature object which contains copy of data of this * instance and of given one */ - CFeatures* create_merged_copy(CList* other) const; + std::shared_ptr create_merged_copy(const std::vector>& other) const; /** Convenience method for method with same name and list as parameter. * @@ -478,7 +478,7 @@ template class CDenseFeatures: public CDotFeatures * @return new feature object which contains copy of data of this * instance and of given one */ - CFeatures* create_merged_copy(CFeatures* other) const; + std::shared_ptr create_merged_copy(std::shared_ptr other) const; /** helper method used to specialize a base class instance * @@ -486,10 +486,10 @@ template class CDenseFeatures: public CDotFeatures #ifndef SWIG [[deprecated("use .as template function")]] #endif - static CDenseFeatures* obtain_from_generic(CFeatures* const base_features); + static std::shared_ptr obtain_from_generic(std::shared_ptr base_features); #ifndef SWIG // SWIG should skip this part - virtual CFeatures* shallow_subset_copy(); + virtual std::shared_ptr shallow_subset_copy(); #endif /** @return object name */ @@ -555,7 +555,7 @@ template class CDenseFeatures: public CDotFeatures SGMatrix feature_matrix; /** feature cache */ - CCache* feature_cache; + std::shared_ptr> feature_cache; }; } #endif // _DENSEFEATURES__H__ diff --git a/src/shogun/features/DenseSubSamplesFeatures.cpp b/src/shogun/features/DenseSubSamplesFeatures.cpp index db33081199e..51c681ceed0 100644 --- a/src/shogun/features/DenseSubSamplesFeatures.cpp +++ b/src/shogun/features/DenseSubSamplesFeatures.cpp @@ -33,79 +33,77 @@ namespace shogun { -template CDenseSubSamplesFeatures::CDenseSubSamplesFeatures() - :CDotFeatures() +template DenseSubSamplesFeatures::DenseSubSamplesFeatures() + :DotFeatures() { init(); } -template CDenseSubSamplesFeatures::CDenseSubSamplesFeatures( - CDenseFeatures *fea, SGVector idx) - :CDotFeatures() +template DenseSubSamplesFeatures::DenseSubSamplesFeatures( + std::shared_ptr> fea, SGVector idx) + :DotFeatures() { init(); set_features(fea); set_subset_idx(idx); } -template CDenseSubSamplesFeatures::~CDenseSubSamplesFeatures() +template DenseSubSamplesFeatures::~DenseSubSamplesFeatures() { - SG_UNREF(m_fea); + } -template void CDenseSubSamplesFeatures::set_features(CDenseFeatures *fea) +template void DenseSubSamplesFeatures::set_features(std::shared_ptr> fea) { - if (m_fea!=fea) + if (m_fea.get()!=fea.get()) { - SG_REF(fea); - SG_UNREF(m_fea); m_fea = fea; } } -template void CDenseSubSamplesFeatures::init() +template void DenseSubSamplesFeatures::init() { set_generic(); m_fea=NULL; m_idx=SGVector(); SG_ADD(&m_idx, "idx", "idx"); - SG_ADD((CSGObject **)&m_fea, "fea", "fea"); + SG_ADD((std::shared_ptr*)&m_fea, "fea", "fea"); } -template CFeatures* CDenseSubSamplesFeatures::duplicate() const +template std::shared_ptr DenseSubSamplesFeatures::duplicate() const { - return new CDenseSubSamplesFeatures(m_fea, m_idx); + return std::make_shared(m_fea, m_idx); } -template bool CDenseSubSamplesFeatures::get_feature_class_compatibility (EFeatureClass rhs) const +template bool DenseSubSamplesFeatures::get_feature_class_compatibility (EFeatureClass rhs) const { if (this->get_feature_class()==rhs || m_fea->get_feature_class()==rhs) return true; return false; } -template EFeatureType CDenseSubSamplesFeatures::get_feature_type() const +template EFeatureType DenseSubSamplesFeatures::get_feature_type() const { return m_fea->get_feature_type(); } -template EFeatureClass CDenseSubSamplesFeatures::get_feature_class() const +template EFeatureClass DenseSubSamplesFeatures::get_feature_class() const { return C_SUB_SAMPLES_DENSE; } -template int32_t CDenseSubSamplesFeatures::get_dim_feature_space() const +template int32_t DenseSubSamplesFeatures::get_dim_feature_space() const { return m_fea->get_dim_feature_space(); } -template int32_t CDenseSubSamplesFeatures::get_num_vectors() const +template int32_t DenseSubSamplesFeatures::get_num_vectors() const { return m_idx.vlen; } -template void CDenseSubSamplesFeatures::set_subset_idx(SGVector idx) +template void DenseSubSamplesFeatures::set_subset_idx(SGVector idx) { require(m_fea, "Please set the features first"); int32_t total_vlen=m_fea->get_num_vectors(); @@ -119,11 +117,11 @@ template void CDenseSubSamplesFeatures::set_subset_idx(SGVector float64_t CDenseSubSamplesFeatures::dot( - int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +template float64_t DenseSubSamplesFeatures::dot( + int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { check_bound(vec_idx1); - CDenseSubSamplesFeatures* df_f= dynamic_cast* >(df); + auto df_f= std::dynamic_pointer_cast>(df); float64_t res=0.0; if (df_f) { @@ -138,64 +136,64 @@ template float64_t CDenseSubSamplesFeatures::dot( } template -float64_t CDenseSubSamplesFeatures::dot( +float64_t DenseSubSamplesFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { check_bound(vec_idx1); return m_fea->dot(m_idx[vec_idx1], vec2); } -template void CDenseSubSamplesFeatures::check_bound(int32_t index) const +template void DenseSubSamplesFeatures::check_bound(int32_t index) const { require(index=0, "Index ({}) is out of bound ({})", index, m_idx.vlen); } -template void CDenseSubSamplesFeatures::add_to_dense_vec( +template void DenseSubSamplesFeatures::add_to_dense_vec( float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { check_bound(vec_idx1); m_fea->add_to_dense_vec(alpha, m_idx[vec_idx1], vec2, vec2_len, abs_val); } -template int32_t CDenseSubSamplesFeatures::get_nnz_features_for_vector( +template int32_t DenseSubSamplesFeatures::get_nnz_features_for_vector( int32_t num) const { return m_fea->get_nnz_features_for_vector(num); } -template void* CDenseSubSamplesFeatures::get_feature_iterator( +template void* DenseSubSamplesFeatures::get_feature_iterator( int32_t vector_index) { not_implemented(SOURCE_LOCATION);; return NULL; } -template bool CDenseSubSamplesFeatures::get_next_feature( +template bool DenseSubSamplesFeatures::get_next_feature( int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION);; return false; } -template void CDenseSubSamplesFeatures::free_feature_iterator( +template void DenseSubSamplesFeatures::free_feature_iterator( void* iterator) { not_implemented(SOURCE_LOCATION); } -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; -template class CDenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; +template class DenseSubSamplesFeatures; } diff --git a/src/shogun/features/DenseSubSamplesFeatures.h b/src/shogun/features/DenseSubSamplesFeatures.h index c27285eec1d..cc53ff06a20 100644 --- a/src/shogun/features/DenseSubSamplesFeatures.h +++ b/src/shogun/features/DenseSubSamplesFeatures.h @@ -40,28 +40,28 @@ namespace shogun { -template class CDenseFeatures; +template class DenseFeatures; template class SGVector; -class CDotFeatures; +class DotFeatures; -/** SubSamples wrap CDotFeatures but only uses a subset of samples */ -template class CDenseSubSamplesFeatures: public CDotFeatures +/** SubSamples wrap DotFeatures but only uses a subset of samples */ +template class DenseSubSamplesFeatures: public DotFeatures { public: /** default constructor */ - CDenseSubSamplesFeatures(); + DenseSubSamplesFeatures(); /** constructor */ - CDenseSubSamplesFeatures(CDenseFeatures *fea, SGVector idx); + DenseSubSamplesFeatures(std::shared_ptr> fea, SGVector idx); /** destructor */ - virtual ~CDenseSubSamplesFeatures(); + virtual ~DenseSubSamplesFeatures(); /** get name */ virtual const char* get_name() const { return "DenseSubSamplesFeatures"; } /** set the underlying features */ - void set_features(CDenseFeatures *fea); + void set_features(std::shared_ptr> fea); /** set the index into the subset of samples */ void set_subset_idx(SGVector idx); @@ -72,7 +72,7 @@ template class CDenseSubSamplesFeatures: public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -114,7 +114,7 @@ template class CDenseSubSamplesFeatures: public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -202,7 +202,7 @@ template class CDenseSubSamplesFeatures: public CDotFeatures void check_bound(int32_t index) const; /* full samples */ - CDenseFeatures *m_fea; + std::shared_ptr> m_fea; /* the indices of subsamples of m_fea */ SGVector m_idx; diff --git a/src/shogun/features/DenseSubsetFeatures.h b/src/shogun/features/DenseSubsetFeatures.h index 18b9185cbce..0c368d80472 100644 --- a/src/shogun/features/DenseSubsetFeatures.h +++ b/src/shogun/features/DenseSubsetFeatures.h @@ -16,32 +16,30 @@ namespace shogun { -template class CDenseFeatures; +template class DenseFeatures; template class SGVector; -class CDotFeatures; +class DotFeatures; /** SubsetFeatures wrap features but only uses a subset of the variables */ -template class CDenseSubsetFeatures: public CDotFeatures +template class DenseSubsetFeatures: public DotFeatures { public: /** default constructor */ - CDenseSubsetFeatures():m_fea(NULL) { set_generic(); } + DenseSubsetFeatures():m_fea(NULL) { set_generic(); } /** constructor */ - CDenseSubsetFeatures(CDenseFeatures *fea, SGVector idx) - :m_fea(fea), m_idx(idx) { SG_REF(m_fea); set_generic(); } + DenseSubsetFeatures(std::shared_ptr> fea, SGVector idx) + :m_fea(fea), m_idx(idx) { set_generic(); } /** destructor */ - virtual ~CDenseSubsetFeatures() { SG_UNREF(m_fea); } + virtual ~DenseSubsetFeatures() { } /** get name */ virtual const char* get_name() const { return "DenseSubsetFeatures"; } /** set the underlying features */ - void set_features(CDenseFeatures *fea) + void set_features(std::shared_ptr> fea) { - SG_REF(fea); - SG_UNREF(m_fea); m_fea = fea; } @@ -57,9 +55,9 @@ template class CDenseSubsetFeatures: public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const + virtual std::shared_ptr duplicate() const { - return new CDenseSubsetFeatures(m_fea, m_idx); + return std::make_shared(m_fea, m_idx); } /** get feature type @@ -114,9 +112,9 @@ template class CDenseSubsetFeatures: public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { - CDenseSubsetFeatures *dsf = dynamic_cast *>(df); + auto dsf = std::dynamic_pointer_cast>(df); if (dsf == NULL) error("Require DenseSubsetFeatures of the same kind to perform dot"); @@ -170,7 +168,7 @@ template class CDenseSubsetFeatures: public CDotFeatures if (abs_val) { for (int32_t i=0; i < vec2_len; ++i) - vec2[i] += alpha * CMath::abs(vec1[m_idx[i]]); + vec2[i] += alpha * Math::abs(vec1[m_idx[i]]); } else { @@ -232,7 +230,7 @@ template class CDenseSubsetFeatures: public CDotFeatures not_implemented(SOURCE_LOCATION); } private: - CDenseFeatures *m_fea; + std::shared_ptr> m_fea; SGVector m_idx; }; } /* shogun */ diff --git a/src/shogun/features/DirectorDotFeatures.h b/src/shogun/features/DirectorDotFeatures.h index e42a9c0c61d..f04a2ed4a21 100644 --- a/src/shogun/features/DirectorDotFeatures.h +++ b/src/shogun/features/DirectorDotFeatures.h @@ -20,7 +20,7 @@ namespace shogun /** @brief DirectorDotFeatures that support dot products among other operations and can be overloaded in modular interfaces. */ #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures +IGNORE_IN_CLASSLIST class DirectorDotFeatures : public DotFeatures { public: @@ -28,11 +28,11 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures * * @param size cache size */ - CDirectorDotFeatures(int32_t size=0) : CDotFeatures(size) + DirectorDotFeatures(int32_t size=0) : DotFeatures(size) { } - virtual ~CDirectorDotFeatures() { } + virtual ~DirectorDotFeatures() { } /** get number of examples/vectors, possibly corresponding to the current subset * @@ -67,7 +67,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) { not_implemented(SOURCE_LOCATION); return 0; @@ -125,7 +125,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures */ virtual void dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) { - CDotFeatures::dense_dot_range(output, start, stop, alphas, vec, dim, b); + DotFeatures::dense_dot_range(output, start, stop, alphas, vec, dim, b); } /** Compute the dot product for a subset of vectors. This function makes use of dense_dot @@ -142,7 +142,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures virtual void dense_dot_range_subset(int32_t* sub_index, int32_t num, float64_t* output, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) { - CDotFeatures::dense_dot_range_subset(sub_index, num, output, alphas, vec, dim, b); + DotFeatures::dense_dot_range_subset(sub_index, num, output, alphas, vec, dim, b); } /** get number of non-zero features in vector @@ -205,7 +205,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures */ virtual SGVector get_mean() { - return CDotFeatures::get_mean(); + return DotFeatures::get_mean(); } /** get covariance @@ -214,7 +214,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures */ virtual SGMatrix get_cov() { - return CDotFeatures::get_cov(); + return DotFeatures::get_cov(); } /** get feature type @@ -234,7 +234,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const + virtual ::std::shared_ptr duplicate() const { not_implemented(SOURCE_LOCATION); return NULL; @@ -255,9 +255,9 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures * * @param p preprocessor to set */ - virtual void add_preprocessor(CPreprocessor* p) + virtual void add_preprocessor(std::shared_ptr p) { - CFeatures::add_preprocessor(p); + Features::add_preprocessor(p); } /** delete preprocessor from list @@ -267,7 +267,7 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures */ virtual void del_preprocessor(int32_t num) { - CFeatures::del_preprocessor(num); + Features::del_preprocessor(num); } /** in case there is a feature matrix allow for reshaping @@ -288,18 +288,18 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures * * @param loader File object via which data shall be loaded */ - virtual void load(CFile* loader) + virtual void load(std::shared_ptr loader) { - CFeatures::load(loader); + Features::load(loader); } /** save features to file * * @param writer File object via which data shall be saved */ - virtual void save(CFile* writer) + virtual void save(std::shared_ptr writer) { - CFeatures::save(writer); + Features::save(writer); } /** adds a subset of indices on top of the current subsets (possibly @@ -309,41 +309,41 @@ IGNORE_IN_CLASSLIST class CDirectorDotFeatures : public CDotFeatures * */ virtual void add_subset(SGVector subset) { - CFeatures::add_subset(subset); + Features::add_subset(subset); } /** removes that last added subset from subset stack, if existing * Calls subset_changed_post() afterwards */ virtual void remove_subset() { - CFeatures::remove_subset(); + Features::remove_subset(); } /** removes all subsets * Calls subset_changed_post() afterwards */ virtual void remove_all_subsets() { - CFeatures::remove_all_subsets(); + Features::remove_all_subsets(); } /** method may be overwritten to update things that depend on subset */ virtual void subset_changed_post() { - CFeatures::subset_changed_post(); + Features::subset_changed_post(); } - /** Creates a new CFeatures instance containing copies of the elements + /** Creates a new Features instance containing copies of the elements * which are specified by the provided indices. * * This method is needed for a KernelMachine to store its model data. * NOT IMPLEMENTED! * * @param indices indices of feature elements to copy - * @return new CFeatures instance with copies of feature data + * @return new Features instance with copies of feature data */ - virtual CFeatures* copy_subset(SGVector indices) const + virtual std::shared_ptr copy_subset(SGVector indices) const { - return CFeatures::copy_subset(indices); + return Features::copy_subset(indices); } /** @return object name */ diff --git a/src/shogun/features/DotFeatures.cpp b/src/shogun/features/DotFeatures.cpp index de9626a3cba..384713a91f9 100644 --- a/src/shogun/features/DotFeatures.cpp +++ b/src/shogun/features/DotFeatures.cpp @@ -23,27 +23,27 @@ using namespace shogun; -CDotFeatures::CDotFeatures(int32_t size) - :CFeatures(size) +DotFeatures::DotFeatures(int32_t size) + :Features(size) { init(); } -CDotFeatures::CDotFeatures(const CDotFeatures & orig) - :CFeatures(orig) +DotFeatures::DotFeatures(const DotFeatures & orig) + :Features(orig) { init(); } -CDotFeatures::CDotFeatures(CFile* loader) - :CFeatures(loader) +DotFeatures::DotFeatures(std::shared_ptr loader) + :Features(loader) { init(); } -void CDotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const +void DotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const { ASSERT(output) ASSERT(start>=0) @@ -81,7 +81,7 @@ void CDotFeatures::dense_dot_range(float64_t* output, int32_t start, int32_t sto #else // TODO: replace with the new signal // for (int32_t i=t_start; i CDotFeatures::get_computed_dot_feature_matrix() const +SGMatrix DotFeatures::get_computed_dot_feature_matrix() const { int64_t offs=0; @@ -163,7 +163,7 @@ SGMatrix CDotFeatures::get_computed_dot_feature_matrix() const return m; } -SGVector CDotFeatures::get_computed_dot_feature_vector(int32_t num) const +SGVector DotFeatures::get_computed_dot_feature_vector(int32_t num) const { int32_t dim=get_dim_feature_space(); @@ -176,7 +176,7 @@ SGVector CDotFeatures::get_computed_dot_feature_vector(int32_t num) c return v; } -SGVector CDotFeatures::get_mean() const +SGVector DotFeatures::get_mean() const { int32_t num=get_num_vectors(); int32_t dim=get_dim_feature_space(); @@ -194,7 +194,7 @@ SGVector CDotFeatures::get_mean() const return mean; } -SGVector CDotFeatures::get_std(bool colwise) const +SGVector DotFeatures::get_std(bool colwise) const { auto num=get_num_vectors(); auto dim=get_dim_feature_space(); @@ -233,7 +233,7 @@ SGVector CDotFeatures::get_std(bool colwise) const } SGVector -CDotFeatures::compute_mean(CDotFeatures* lhs, CDotFeatures* rhs) +DotFeatures::compute_mean(std::shared_ptr lhs, std::shared_ptr rhs) { ASSERT(lhs && rhs) ASSERT(lhs->get_dim_feature_space() == rhs->get_dim_feature_space()) @@ -259,7 +259,7 @@ CDotFeatures::compute_mean(CDotFeatures* lhs, CDotFeatures* rhs) return mean; } -SGMatrix CDotFeatures::get_cov(bool copy_data_for_speed) const +SGMatrix DotFeatures::get_cov(bool copy_data_for_speed) const { int32_t num=get_num_vectors(); int32_t dim=get_dim_feature_space(); @@ -303,10 +303,10 @@ SGMatrix CDotFeatures::get_cov(bool copy_data_for_speed) const return cov; } -SGMatrix CDotFeatures::compute_cov( - CDotFeatures* lhs, CDotFeatures* rhs, bool copy_data_for_speed) +SGMatrix DotFeatures::compute_cov( + std::shared_ptr lhs, std::shared_ptr rhs, bool copy_data_for_speed) { - CDotFeatures* feats[2]; + std::shared_ptr feats[2]; feats[0]=lhs; feats[1]=rhs; @@ -369,7 +369,7 @@ SGMatrix CDotFeatures::compute_cov( return cov; } -void CDotFeatures::init() +void DotFeatures::init() { set_property(FP_DOT); } diff --git a/src/shogun/features/DotFeatures.h b/src/shogun/features/DotFeatures.h index fd77373223d..797d687717e 100644 --- a/src/shogun/features/DotFeatures.h +++ b/src/shogun/features/DotFeatures.h @@ -38,7 +38,7 @@ namespace shogun * - iteration over all (potentially) non-zero features of \f${\bf x}\f$ * */ -class CDotFeatures : public CFeatures +class DotFeatures : public Features { public: @@ -46,18 +46,18 @@ class CDotFeatures : public CFeatures * * @param size cache size */ - CDotFeatures(int32_t size=0); + DotFeatures(int32_t size=0); /** copy constructor */ - CDotFeatures(const CDotFeatures & orig); + DotFeatures(const DotFeatures & orig); /** constructor * * @param loader File object via which to load data */ - CDotFeatures(CFile* loader); + DotFeatures(std::shared_ptr loader); - virtual ~CDotFeatures() { } + virtual ~DotFeatures() { } /** obtain the dimensionality of the feature space * @@ -75,7 +75,7 @@ class CDotFeatures : public CFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const = 0; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const = 0; /** compute dot product between vector1 and a dense vector * @@ -194,7 +194,7 @@ class CDotFeatures : public CFeatures * @return mean returned */ static SGVector - compute_mean(CDotFeatures* lhs, CDotFeatures* rhs); + compute_mean(std::shared_ptr lhs, std::shared_ptr rhs); /** get covariance * @@ -210,13 +210,13 @@ class CDotFeatures : public CFeatures */ virtual SGMatrix get_cov(bool copy_data_for_speed = true) const; - /** compute the covariance of two CDotFeatures together + /** compute the covariance of two DotFeatures together * - * @param copy_data_for_speed @see CDotFeatures::get_cov + * @param copy_data_for_speed @see DotFeatures::get_cov * @return covariance */ static SGMatrix compute_cov( - CDotFeatures* lhs, CDotFeatures* rhs, + std::shared_ptr lhs, std::shared_ptr rhs, bool copy_data_for_speed = true); private: diff --git a/src/shogun/features/DummyFeatures.cpp b/src/shogun/features/DummyFeatures.cpp index cbcb467b71b..d995fb38203 100644 --- a/src/shogun/features/DummyFeatures.cpp +++ b/src/shogun/features/DummyFeatures.cpp @@ -3,48 +3,48 @@ using namespace shogun; -CDummyFeatures::CDummyFeatures() +DummyFeatures::DummyFeatures() { init(); num_vectors = 0; } -CDummyFeatures::CDummyFeatures(int32_t num) : CFeatures(0), num_vectors(num) +DummyFeatures::DummyFeatures(int32_t num) : Features(0), num_vectors(num) { init(); } -CDummyFeatures::CDummyFeatures(const CDummyFeatures &orig) : CFeatures(0), +DummyFeatures::DummyFeatures(const DummyFeatures &orig) : Features(0), num_vectors(orig.num_vectors) { init(); } -CDummyFeatures::~CDummyFeatures() +DummyFeatures::~DummyFeatures() { } -int32_t CDummyFeatures::get_num_vectors() const +int32_t DummyFeatures::get_num_vectors() const { return num_vectors; } -CFeatures* CDummyFeatures::duplicate() const +std::shared_ptr DummyFeatures::duplicate() const { - return new CDummyFeatures(*this); + return std::make_shared(*this); } -inline EFeatureType CDummyFeatures::get_feature_type() const +inline EFeatureType DummyFeatures::get_feature_type() const { return F_ANY; } -EFeatureClass CDummyFeatures::get_feature_class() const +EFeatureClass DummyFeatures::get_feature_class() const { return C_ANY; } -void CDummyFeatures::init() +void DummyFeatures::init() { SG_ADD( &num_vectors, "num_vectors", "Number of feature vectors."); diff --git a/src/shogun/features/DummyFeatures.h b/src/shogun/features/DummyFeatures.h index f6021d28455..34ca215451c 100644 --- a/src/shogun/features/DummyFeatures.h +++ b/src/shogun/features/DummyFeatures.h @@ -18,30 +18,30 @@ namespace shogun /** @brief The class DummyFeatures implements features that only know the * number of feature objects (but don't actually contain any). * - * This is used in the CCustomKernel.*/ -class CDummyFeatures : public CFeatures + * This is used in the CustomKernel.*/ +class DummyFeatures : public Features { public: /** default constructor */ - CDummyFeatures(); + DummyFeatures(); /** constructor * * @param num number of feature vectors */ - CDummyFeatures(int32_t num); + DummyFeatures(int32_t num); /** copy constructor */ - CDummyFeatures(const CDummyFeatures &orig); + DummyFeatures(const DummyFeatures &orig); /** destructor */ - virtual ~CDummyFeatures(); + virtual ~DummyFeatures(); /** get number of feature vectors */ virtual int32_t get_num_vectors() const; /** duplicate features */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type (ANY) */ inline EFeatureType get_feature_type() const; diff --git a/src/shogun/features/ExplicitSpecFeatures.cpp b/src/shogun/features/ExplicitSpecFeatures.cpp index e35dda05a65..6e8c88494d0 100644 --- a/src/shogun/features/ExplicitSpecFeatures.cpp +++ b/src/shogun/features/ExplicitSpecFeatures.cpp @@ -12,7 +12,7 @@ using namespace shogun; -CExplicitSpecFeatures::CExplicitSpecFeatures() :CDotFeatures() +ExplicitSpecFeatures::ExplicitSpecFeatures() :DotFeatures() { unstable(SOURCE_LOCATION); @@ -25,7 +25,7 @@ CExplicitSpecFeatures::CExplicitSpecFeatures() :CDotFeatures() } -CExplicitSpecFeatures::CExplicitSpecFeatures(CStringFeatures* str, bool normalize) : CDotFeatures() +ExplicitSpecFeatures::ExplicitSpecFeatures(std::shared_ptr> str, bool normalize) : DotFeatures() { ASSERT(str) @@ -38,7 +38,7 @@ CExplicitSpecFeatures::CExplicitSpecFeatures(CStringFeatures* str, boo SG_DEBUG("SPEC size={}, num_str={}", spec_size, num_strings) } -CExplicitSpecFeatures::CExplicitSpecFeatures(const CExplicitSpecFeatures& orig) : CDotFeatures(orig), +ExplicitSpecFeatures::ExplicitSpecFeatures(const ExplicitSpecFeatures& orig) : DotFeatures(orig), num_strings(orig.num_strings), alphabet_size(orig.alphabet_size), spec_size(orig.spec_size) { k_spectrum= SG_MALLOC(float64_t*, num_strings); @@ -46,22 +46,22 @@ CExplicitSpecFeatures::CExplicitSpecFeatures(const CExplicitSpecFeatures& orig) k_spectrum[i]=SGVector::clone_vector(k_spectrum[i], spec_size); } -CExplicitSpecFeatures::~CExplicitSpecFeatures() +ExplicitSpecFeatures::~ExplicitSpecFeatures() { delete_kmer_spectrum(); } -int32_t CExplicitSpecFeatures::get_dim_feature_space() const +int32_t ExplicitSpecFeatures::get_dim_feature_space() const { return spec_size; } -float64_t CExplicitSpecFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t ExplicitSpecFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CExplicitSpecFeatures* sf = (CExplicitSpecFeatures*) df; + auto sf = std::static_pointer_cast(df); ASSERT(vec_idx1 < num_strings) ASSERT(vec_idx2 < sf->num_strings) @@ -71,7 +71,7 @@ float64_t CExplicitSpecFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t return linalg::dot(vec1, vec2); } -float64_t CExplicitSpecFeatures::dot( +float64_t ExplicitSpecFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { ASSERT(vec2.size() == spec_size) @@ -81,7 +81,7 @@ float64_t CExplicitSpecFeatures::dot( return linalg::dot(vec1, vec2); } -void CExplicitSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void ExplicitSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { ASSERT(vec2_len == spec_size) ASSERT(vec_idx1 < num_strings) @@ -90,7 +90,7 @@ void CExplicitSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, if (abs_val) { for (int32_t i=0; i* str) +void ExplicitSpecFeatures::obtain_kmer_spectrum(std::shared_ptr> str) { k_spectrum= SG_MALLOC(float64_t*, num_strings); @@ -121,7 +121,7 @@ void CExplicitSpecFeatures::obtain_kmer_spectrum(CStringFeatures* str) { float64_t n=0; for (int32_t j=0; j* str) } } -void CExplicitSpecFeatures::delete_kmer_spectrum() +void ExplicitSpecFeatures::delete_kmer_spectrum() { for (int32_t i=0; i ExplicitSpecFeatures::duplicate() const { - return new CExplicitSpecFeatures(*this); + return std::make_shared(*this); } -void* CExplicitSpecFeatures::get_feature_iterator(int32_t vector_index) +void* ExplicitSpecFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CExplicitSpecFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool ExplicitSpecFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CExplicitSpecFeatures::free_feature_iterator(void* iterator) +void ExplicitSpecFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } -int32_t CExplicitSpecFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t ExplicitSpecFeatures::get_nnz_features_for_vector(int32_t num) const { not_implemented(SOURCE_LOCATION); return 0; } -EFeatureType CExplicitSpecFeatures::get_feature_type() const +EFeatureType ExplicitSpecFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CExplicitSpecFeatures::get_feature_class() const +EFeatureClass ExplicitSpecFeatures::get_feature_class() const { return C_SPEC; } -int32_t CExplicitSpecFeatures::get_num_vectors() const +int32_t ExplicitSpecFeatures::get_num_vectors() const { return num_strings; } diff --git a/src/shogun/features/ExplicitSpecFeatures.h b/src/shogun/features/ExplicitSpecFeatures.h index 23ec0981209..cff5d6fa706 100644 --- a/src/shogun/features/ExplicitSpecFeatures.h +++ b/src/shogun/features/ExplicitSpecFeatures.h @@ -16,36 +16,36 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /**@brief Features that compute the Spectrum Kernel feature space explicitly. * * \sa CCommWordStringKernel */ -class CExplicitSpecFeatures : public CDotFeatures +class ExplicitSpecFeatures : public DotFeatures { public: /** default constructor */ - CExplicitSpecFeatures(); + ExplicitSpecFeatures(); /** constructor * * @param str stringfeatures (of words) * @param normalize whether to use sqrtdiag normalization */ - CExplicitSpecFeatures(CStringFeatures* str, bool normalize=true); + ExplicitSpecFeatures(std::shared_ptr> str, bool normalize=true); /** copy constructor */ - CExplicitSpecFeatures(const CExplicitSpecFeatures & orig); + ExplicitSpecFeatures(const ExplicitSpecFeatures & orig); /** destructor */ - virtual ~CExplicitSpecFeatures(); + virtual ~ExplicitSpecFeatures(); /** duplicate feature object * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** obtain the dimensionality of the feature space * @@ -63,7 +63,7 @@ class CExplicitSpecFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -166,7 +166,7 @@ class CExplicitSpecFeatures : public CDotFeatures * * @param str the string feature object already in k-mer format */ - void obtain_kmer_spectrum(CStringFeatures* str); + void obtain_kmer_spectrum(std::shared_ptr> str); /** free kmer spectrum */ void delete_kmer_spectrum(); diff --git a/src/shogun/features/FKFeatures.cpp b/src/shogun/features/FKFeatures.cpp index c5065d76045..862df9d8d05 100644 --- a/src/shogun/features/FKFeatures.cpp +++ b/src/shogun/features/FKFeatures.cpp @@ -11,33 +11,33 @@ using namespace shogun; -CFKFeatures::CFKFeatures() : CDenseFeatures() +FKFeatures::FKFeatures() : DenseFeatures() { init(); } -CFKFeatures::CFKFeatures(int32_t size, CHMM* p, CHMM* n) -: CDenseFeatures(size) +FKFeatures::FKFeatures(int32_t size, std::shared_ptr p, std::shared_ptr n) +: DenseFeatures(size) { init(); weight_a=-1; set_models(p,n); } -CFKFeatures::CFKFeatures(const CFKFeatures &orig) -: CDenseFeatures(orig), pos(orig.pos), neg(orig.neg), weight_a(orig.weight_a) +FKFeatures::FKFeatures(const FKFeatures &orig) +: DenseFeatures(orig), pos(orig.pos), neg(orig.neg), weight_a(orig.weight_a) { } -CFKFeatures::~CFKFeatures() +FKFeatures::~FKFeatures() { - SG_UNREF(pos); - SG_UNREF(neg); + + } -float64_t CFKFeatures::deriv_a(float64_t a, int32_t dimension) const +float64_t FKFeatures::deriv_a(float64_t a, int32_t dimension) const { - CStringFeatures *Obs=pos->get_observations() ; + auto Obs=pos->get_observations() ; float64_t deriv=0.0 ; int32_t i=dimension ; @@ -83,7 +83,7 @@ float64_t CFKFeatures::deriv_a(float64_t a, int32_t dimension) const } -float64_t CFKFeatures::set_opt_a(float64_t a) +float64_t FKFeatures::set_opt_a(float64_t a) { if (a==-1) { @@ -99,7 +99,7 @@ float64_t CFKFeatures::set_opt_a(float64_t a) float64_t la=0; float64_t ua=1; a=(la+ua)/2; - while (CMath::abs(ua-la)>1e-6) + while (Math::abs(ua-la)>1e-6) { float64_t da=deriv_a(a); if (da>0) @@ -120,11 +120,11 @@ float64_t CFKFeatures::set_opt_a(float64_t a) return a; } -void CFKFeatures::set_models(CHMM* p, CHMM* n) +void FKFeatures::set_models(std::shared_ptr p, std::shared_ptr n) { ASSERT(p && n) - SG_REF(p); - SG_REF(n); + + pos=p; neg=n; @@ -140,7 +140,7 @@ void CFKFeatures::set_models(CHMM* p, CHMM* n) num_features=1+pos->get_N()*(1+pos->get_N()+1+pos->get_M()) + neg->get_N()*(1+neg->get_N()+1+neg->get_M()) ; } -float64_t* CFKFeatures::compute_feature_vector( +float64_t* FKFeatures::compute_feature_vector( int32_t num, int32_t &len, float64_t* target) const { float64_t* featurevector=target; @@ -160,7 +160,7 @@ float64_t* CFKFeatures::compute_feature_vector( return featurevector; } -void CFKFeatures::compute_feature_vector( +void FKFeatures::compute_feature_vector( float64_t* featurevector, int32_t num, int32_t& len) const { int32_t i,j,p=0,x=num; @@ -171,7 +171,7 @@ void CFKFeatures::compute_feature_vector( len=1+pos->get_N()*(1+pos->get_N()+1+pos->get_M()) + neg->get_N()*(1+neg->get_N()+1+neg->get_M()); featurevector[p++] = deriv_a(weight_a, x); - float64_t px=CMath::logarithmic_sum( + float64_t px=Math::logarithmic_sum( posx+log(weight_a),negx+log(1-weight_a)); //first do positive model @@ -206,7 +206,7 @@ void CFKFeatures::compute_feature_vector( } } -float64_t* CFKFeatures::set_feature_matrix() +float64_t* FKFeatures::set_feature_matrix() { ASSERT(pos) ASSERT(pos->get_observations()) @@ -243,7 +243,7 @@ float64_t* CFKFeatures::set_feature_matrix() return feature_matrix.matrix; } -void CFKFeatures::init() +void FKFeatures::init() { pos = NULL; neg = NULL; @@ -253,7 +253,7 @@ void CFKFeatures::init() unset_generic(); //TODO serialize HMMs - //m_parameters->add((CSGObject**) &pos, "pos", "HMM for positive class."); - //m_parameters->add((CSGObject**) &neg, "neg", "HMM for negative class."); + //m_parameters->add((std::shared_ptr*) &pos, "pos", "HMM for positive class."); + //m_parameters->add((std::shared_ptr*) &neg, "neg", "HMM for negative class."); SG_ADD(&weight_a, "weight_a", "Class prior."); } diff --git a/src/shogun/features/FKFeatures.h b/src/shogun/features/FKFeatures.h index 0fc75823247..242a95c06fd 100644 --- a/src/shogun/features/FKFeatures.h +++ b/src/shogun/features/FKFeatures.h @@ -15,8 +15,8 @@ namespace shogun { -template class CDenseFeatures; -class CHMM; +template class DenseFeatures; +class HMM; /** @brief The class FKFeatures implements Fischer kernel features obtained from * two Hidden Markov models. @@ -32,14 +32,14 @@ class CHMM; * Note that FK-features are computed on the fly, so to be effective feature * caching should be enabled. * - * It inherits its functionality from CDenseFeatures, which should be + * It inherits its functionality from DenseFeatures, which should be * consulted for further reference. */ -class CFKFeatures: public CDenseFeatures +class FKFeatures: public DenseFeatures { public: /** default constructor */ - CFKFeatures(); + FKFeatures(); /** constructor * @@ -47,19 +47,19 @@ class CFKFeatures: public CDenseFeatures * @param p positive HMM * @param n negative HMM */ - CFKFeatures(int32_t size, CHMM* p, CHMM* n); + FKFeatures(int32_t size, std::shared_ptr p, std::shared_ptr n); /** copy constructor */ - CFKFeatures(const CFKFeatures &orig); + FKFeatures(const FKFeatures &orig); - virtual ~CFKFeatures(); + virtual ~FKFeatures(); /** set HMMs * * @param p positive HMM * @param n negative HMM */ - void set_models(CHMM* p, CHMM* n); + void set_models(std::shared_ptr p, std::shared_ptr n); /** set weight a * @@ -132,9 +132,9 @@ class CFKFeatures: public CDenseFeatures protected: /** positive HMM */ - CHMM* pos; + std::shared_ptr pos; /** negative HMM */ - CHMM* neg; + std::shared_ptr neg; /** positive prob */ float64_t* pos_prob; /** negative prob */ diff --git a/src/shogun/features/FactorGraphFeatures.cpp b/src/shogun/features/FactorGraphFeatures.cpp index d949d043183..e352a419819 100644 --- a/src/shogun/features/FactorGraphFeatures.cpp +++ b/src/shogun/features/FactorGraphFeatures.cpp @@ -8,42 +8,39 @@ using namespace shogun; -CFactorGraphFeatures::CFactorGraphFeatures() +FactorGraphFeatures::FactorGraphFeatures(): FactorGraphFeatures(0) { - init(); - m_samples = new CDynamicObjectArray(); - SG_REF(m_samples); } -CFactorGraphFeatures::CFactorGraphFeatures(int32_t num_samples) +FactorGraphFeatures::FactorGraphFeatures(int32_t num_samples) { init(); - m_samples = new CDynamicObjectArray(num_samples); - SG_REF(m_samples); + m_samples = std::make_shared(num_samples); + } -CFactorGraphFeatures::~CFactorGraphFeatures() +FactorGraphFeatures::~FactorGraphFeatures() { - SG_UNREF(m_samples); + } -CFeatures* CFactorGraphFeatures::duplicate() const +std::shared_ptr FactorGraphFeatures::duplicate() const { - return new CFactorGraphFeatures(*this); + return std::make_shared(*this); } -EFeatureType CFactorGraphFeatures::get_feature_type() const +EFeatureType FactorGraphFeatures::get_feature_type() const { return F_ANY; } -EFeatureClass CFactorGraphFeatures::get_feature_class() const +EFeatureClass FactorGraphFeatures::get_feature_class() const { return C_FACTOR_GRAPH; } -int32_t CFactorGraphFeatures::get_num_vectors() const +int32_t FactorGraphFeatures::get_num_vectors() const { if (m_samples == NULL) return 0; @@ -51,7 +48,7 @@ int32_t CFactorGraphFeatures::get_num_vectors() const return m_samples->get_array_size(); } -bool CFactorGraphFeatures::add_sample(CFactorGraph* example) +bool FactorGraphFeatures::add_sample(std::shared_ptr example) { if (m_samples != NULL) { @@ -62,27 +59,27 @@ bool CFactorGraphFeatures::add_sample(CFactorGraph* example) return false; } -CFactorGraph* CFactorGraphFeatures::get_sample(index_t idx) +std::shared_ptr FactorGraphFeatures::get_sample(index_t idx) { require(m_samples != NULL, "{}::get_sample(): m_samples is NULL!", get_name()); require(idx >= 0 && idx < get_num_vectors(), "{}::get_sample(): out of index!", get_name()); - return dynamic_cast(m_samples->get_element(idx)); + return m_samples->get_element(idx); } -void CFactorGraphFeatures::init() +void FactorGraphFeatures::init() { - SG_ADD((CSGObject**) &m_samples, "samples", "Array of examples"); + SG_ADD((std::shared_ptr*) &m_samples, "samples", "Array of examples"); } -CFactorGraphFeatures* CFactorGraphFeatures::obtain_from_generic(CFeatures* base_feats) +std::shared_ptr FactorGraphFeatures::obtain_from_generic(std::shared_ptr base_feats) { - require(base_feats != NULL, "CFactorGraphFeatures::obtain_from_generic(): base_feats is NULL!"); + require(base_feats != NULL, "FactorGraphFeatures::obtain_from_generic(): base_feats is NULL!"); if (base_feats->get_feature_class() == C_FACTOR_GRAPH) - return dynamic_cast(base_feats); + return std::dynamic_pointer_cast(base_feats); else - error("base_labels must be of dynamic type CFactorGraph!"); + error("base_labels must be of dynamic type FactorGraph!"); return NULL; } diff --git a/src/shogun/features/FactorGraphFeatures.h b/src/shogun/features/FactorGraphFeatures.h index 1f06bb354e1..cddfe23d8b7 100644 --- a/src/shogun/features/FactorGraphFeatures.h +++ b/src/shogun/features/FactorGraphFeatures.h @@ -15,28 +15,28 @@ namespace shogun { -/** @brief CFactorGraphFeatures maintains an array of factor graphs, +/** @brief FactorGraphFeatures maintains an array of factor graphs, * each graph is a sample, i.e. an instance of structured input. */ -class CFactorGraphFeatures : public CFeatures +class FactorGraphFeatures : public Features { public: /** default constructor */ - CFactorGraphFeatures(); + FactorGraphFeatures(); /** constructor * * @param num_samples the number of examples the object will contain */ - CFactorGraphFeatures(int32_t num_samples); + FactorGraphFeatures(int32_t num_samples); - virtual ~CFactorGraphFeatures(); + virtual ~FactorGraphFeatures(); /** Copy-constructor * * @return the copy of the given object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -67,25 +67,25 @@ class CFactorGraphFeatures : public CFeatures * @param fg a factor graph instance * @return whether the sample has been added successfully */ - bool add_sample(CFactorGraph* fg); + bool add_sample(std::shared_ptr fg); /** get a graph instance * * @param idx index of the required example - * @return pointer of CFactorGraph + * @return pointer of FactorGraph */ - CFactorGraph* get_sample(index_t idx); + std::shared_ptr get_sample(index_t idx); /** helper method used to specialize a base class instance * - * @param base_feats its dynamic type must be CFactorGraphFeatures - * @return pointer to CFactorGraphFeatures + * @param base_feats its dynamic type must be FactorGraphFeatures + * @return pointer to FactorGraphFeatures */ - static CFactorGraphFeatures* obtain_from_generic(CFeatures* base_feats); + static std::shared_ptr obtain_from_generic(std::shared_ptr base_feats); protected: - /** array of CFactorGraph */ - CDynamicObjectArray* m_samples; + /** array of FactorGraph */ + std::shared_ptr m_samples; private: /** init function for the object */ diff --git a/src/shogun/features/Features.cpp b/src/shogun/features/Features.cpp index 74d56ee9d48..8937f1bca00 100644 --- a/src/shogun/features/Features.cpp +++ b/src/shogun/features/Features.cpp @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Evgeniy Andreev, - * Sergey Lisitsyn, Soumyajit De, Shashwat Lal Das, Fernando Iglesias, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Evgeniy Andreev, + * Sergey Lisitsyn, Soumyajit De, Shashwat Lal Das, Fernando Iglesias, * Bjoern Esser, Wu Lin */ @@ -16,26 +16,25 @@ using namespace shogun; -CFeatures::CFeatures(int32_t size) -: CSGObject() +Features::Features(int32_t size) +: SGObject() { init(); cache_size = size; } -CFeatures::CFeatures(const CFeatures& orig) -: CSGObject(orig) +Features::Features(const Features& orig) +: SGObject(orig) { init(); - // TODO this should be a shallow copy - auto old_preproc = preproc; - preproc = make_clone(preproc); - SG_UNREF(old_preproc); + // Call to init creates new preproc arrays. + // FIXME: make_clone? + preproc = orig.preproc; } -CFeatures::CFeatures(CFile* loader) -: CSGObject() +Features::Features(std::shared_ptr loader) +: SGObject() { init(); @@ -43,104 +42,99 @@ CFeatures::CFeatures(CFile* loader) io::info("Feature object loaded ({})",fmt::ptr(this)); } -CFeatures::~CFeatures() +Features::~Features() { clean_preprocessors(); - SG_UNREF(m_subset_stack); - SG_UNREF(preproc); } -void CFeatures::init() +void Features::init() { set_default_mask(ParameterProperties::READONLY); SG_ADD(&properties, "properties", "Feature properties"); SG_ADD(&cache_size, "cache_size", "Size of cache in MB"); - SG_ADD((CSGObject**) &preproc, "preproc", "Array of preprocessors."); + SG_ADD(&preproc, "preproc", "Array of preprocessors."); - SG_ADD((CSGObject**)&m_subset_stack, "subset_stack", "Stack of subsets"); + SG_ADD((std::shared_ptr*)&m_subset_stack, "subset_stack", "Stack of subsets"); + + m_subset_stack=std::make_shared(); - m_subset_stack=new CSubsetStack(); - SG_REF(m_subset_stack); properties = FP_NONE; cache_size = 0; - preproc = new CDynamicObjectArray(); - SG_REF(preproc); } -void CFeatures::add_preprocessor(CPreprocessor* p) +void Features::add_preprocessor(std::shared_ptr p) { ASSERT(p) - preproc->push_back(p); + preproc.push_back(p); } -CPreprocessor* CFeatures::get_preprocessor(int32_t num) const +std::shared_ptr Features::get_preprocessor(int32_t num) const { - if (numget_num_elements() && num>=0) + if (num=0) { - return (CPreprocessor*) preproc->get_element(num); + return preproc[num]; } else return NULL; } -void CFeatures::clean_preprocessors() +void Features::clean_preprocessors() { - preproc->reset_array(); + preproc.clear(); } -void CFeatures::del_preprocessor(int32_t num) +void Features::del_preprocessor(int32_t num) { - if (numget_num_elements() && num>=0) + if (num=0) { - preproc->delete_element(num); + preproc.erase(preproc.cbegin()+num); } } -void CFeatures::list_preprocessors() +void Features::list_preprocessors() { - int32_t num_preproc = preproc->get_num_elements(); - - for (int32_t i=0; iget_element(i)->get_name()); + io::info("preproc[{}]={}\n", i++, v->get_name()); } } -int32_t CFeatures::get_num_preprocessors() const +int32_t Features::get_num_preprocessors() const { - return preproc->get_num_elements(); + return preproc.size(); } -int32_t CFeatures::get_cache_size() const +int32_t Features::get_cache_size() const { return cache_size; } -bool CFeatures::reshape(int32_t num_features, int32_t num_vectors) +bool Features::reshape(int32_t num_features, int32_t num_vectors) { not_implemented(SOURCE_LOCATION); return false; } -void CFeatures::load(CFile* loader) +void Features::load(std::shared_ptr loader) { SG_SET_LOCALE_C; not_implemented(SOURCE_LOCATION); SG_RESET_LOCALE; } -void CFeatures::save(CFile* writer) +void Features::save(std::shared_ptr writer) { SG_SET_LOCALE_C; not_implemented(SOURCE_LOCATION); SG_RESET_LOCALE; } -bool CFeatures::check_feature_compatibility(CFeatures* f) const +bool Features::check_feature_compatibility(std::shared_ptr f) const { bool result=false; @@ -152,67 +146,67 @@ bool CFeatures::check_feature_compatibility(CFeatures* f) const return result; } -bool CFeatures::has_property(EFeatureProperty p) const +bool Features::has_property(EFeatureProperty p) const { return (properties & p) != 0; } -void CFeatures::set_property(EFeatureProperty p) +void Features::set_property(EFeatureProperty p) { properties |= p; } -void CFeatures::unset_property(EFeatureProperty p) +void Features::unset_property(EFeatureProperty p) { properties &= (properties | p) ^ p; } -void CFeatures::add_subset(SGVector subset) +void Features::add_subset(SGVector subset) { m_subset_stack->add_subset(subset); subset_changed_post(); } -void CFeatures::add_subset_in_place(SGVector subset) +void Features::add_subset_in_place(SGVector subset) { m_subset_stack->add_subset_in_place(subset); subset_changed_post(); } -void CFeatures::remove_subset() +void Features::remove_subset() { m_subset_stack->remove_subset(); subset_changed_post(); } -void CFeatures::remove_all_subsets() +void Features::remove_all_subsets() { m_subset_stack->remove_all_subsets(); subset_changed_post(); } -CSubsetStack* CFeatures::get_subset_stack() +std::shared_ptr Features::get_subset_stack() { - SG_REF(m_subset_stack); + return m_subset_stack; } -CFeatures* CFeatures::copy_subset(SGVector indices) const +std::shared_ptr Features::copy_subset(SGVector indices) const { error("{}::copy_subset(): copy_subset and therefore model storage of " - "CMachine (required for cross-validation and model-selection is " + "Machine (required for cross-validation and model-selection is " "not yet implemented yet. Ask developers!", get_name()); return NULL; } -CFeatures* CFeatures::copy_dimension_subset(SGVector dims) const +std::shared_ptr Features::copy_dimension_subset(SGVector dims) const { io::warn("{}::copy_dimension_subset():: Is not yet implemented!", get_name()); return NULL; } -bool CFeatures::get_feature_class_compatibility(EFeatureClass rhs) const +bool Features::get_feature_class_compatibility(EFeatureClass rhs) const { if (this->get_feature_class()==rhs) return true; diff --git a/src/shogun/features/Features.h b/src/shogun/features/Features.h index 81cc7d10564..976e6fd807a 100644 --- a/src/shogun/features/Features.h +++ b/src/shogun/features/Features.h @@ -24,9 +24,9 @@ namespace shogun { - class CFile; - class CPreprocessor; - class CKernel; + class File; + class Preprocessor; + class Kernel; } namespace shogun @@ -46,9 +46,9 @@ namespace shogun * * In addition it provides helpers to check e.g. for compatibility of feature objects. * - * Currently there are 3 general feature classes, which are CDenseFeatures - * (dense matrices), CSparseFeatures (sparse matrices), CStringFeatures (a - * set of strings) from which all the specific features like CDenseFeatures + * Currently there are 3 general feature classes, which are DenseFeatures + * (dense matrices), SparseFeatures (sparse matrices), CStringFeatures (a + * set of strings) from which all the specific features like DenseFeatures * (dense real valued feature matrices) are derived. * * @@ -62,23 +62,23 @@ namespace shogun * to many when add_subset() is called many times) with add_subset_in_place(). * The latter does not allow to remove such modifications one-by-one. */ -class CFeatures : public CSGObject +class Features : public SGObject { public: /** constructor * * @param size cache size */ - CFeatures(int32_t size=0); + Features(int32_t size=0); /** copy constructor */ - CFeatures(const CFeatures& orig); + Features(const Features& orig); /** constructor * * @param loader File object via which data shall be loaded */ - CFeatures(CFile* loader); + Features(std::shared_ptr loader); /** duplicate feature object * @@ -86,9 +86,9 @@ class CFeatures : public CSGObject * * @return feature object */ - virtual CFeatures* duplicate() const=0; + virtual std::shared_ptr duplicate() const=0; - virtual ~CFeatures(); + virtual ~Features(); /** get feature type * @@ -108,7 +108,7 @@ class CFeatures : public CSGObject #ifndef SWIG /** returns an iterator of indices - * from 0 to @ref CFeatures::get_num_vectors + * from 0 to @ref Features::get_num_vectors * * Should be used in algorithms in the following way: * @code @@ -126,7 +126,7 @@ class CFeatures : public CSGObject * * @param p preprocessor to set */ - virtual void add_preprocessor(CPreprocessor* p); + virtual void add_preprocessor(std::shared_ptr p); /** delete preprocessor from list * @@ -138,7 +138,7 @@ class CFeatures : public CSGObject * * @param num index of preprocessor in list */ - CPreprocessor* get_preprocessor(int32_t num) const; + std::shared_ptr get_preprocessor(int32_t num) const; /** get number of preprocessors * @@ -180,20 +180,20 @@ class CFeatures : public CSGObject * * @param loader File object via which data shall be loaded */ - virtual void load(CFile* loader); + virtual void load(std::shared_ptr loader); /** save features to file * * @param writer File object via which data shall be saved */ - virtual void save(CFile* writer); + virtual void save(std::shared_ptr writer); /** check feature compatibility * * @param f features to check for compatibility * @return if features are compatible */ - bool check_feature_compatibility(CFeatures* f) const; + bool check_feature_compatibility(std::shared_ptr f) const; /** check if features have given property * @@ -224,7 +224,7 @@ class CFeatures : public CSGObject * @return new feature object which contains copy of data of this * instance and given ones */ - virtual CFeatures* create_merged_copy(CList* others) const + virtual std::shared_ptr create_merged_copy(const std::vector>& others) const { error("{}::create_merged_copy() is not yet implemented!"); return NULL; @@ -238,7 +238,7 @@ class CFeatures : public CSGObject * @return new feature object which contains copy of data of this * instance and of given one */ - virtual CFeatures* create_merged_copy(CFeatures* other) const + virtual std::shared_ptr create_merged_copy(std::shared_ptr other) const { error("{}::create_merged_copy() is not yet implemented!"); return NULL; @@ -279,32 +279,32 @@ class CFeatures : public CSGObject * * @return subset stack */ - virtual CSubsetStack* get_subset_stack(); + virtual std::shared_ptr get_subset_stack(); /** method may be overwritten to update things that depend on subset */ virtual void subset_changed_post() {} - /** Creates a new CFeatures instance containing copies of the elements + /** Creates a new Features instance containing copies of the elements * which are specified by the provided indices. * * This method is needed for a KernelMachine to store its model data. * NOT IMPLEMENTED! * * @param indices indices of feature elements to copy - * @return new CFeatures instance with copies of feature data + * @return new Features instance with copies of feature data */ - virtual CFeatures* copy_subset(SGVector indices) const; + virtual std::shared_ptr copy_subset(SGVector indices) const; - /** Creates a new CFeatures instance containing only the dimensions + /** Creates a new Features instance containing only the dimensions * of the feature vector which are specified by the provided indices. * * This method is needed for feature selection tasks * NOT IMPLEMENTED! * * @param dims indices of feature dimensions to copy - * @return new CFeatures instance with copies of specified features + * @return new Features instance with copies of specified features */ - virtual CFeatures* copy_dimension_subset(SGVector dims) const; + virtual std::shared_ptr copy_dimension_subset(SGVector dims) const; /** does this class support compatible computation bewteen difference classes? * for example, this->dot(rhs_prt), @@ -325,7 +325,7 @@ class CFeatures : public CSGObject virtual bool get_feature_class_compatibility (EFeatureClass rhs) const; #ifndef SWIG // SWIG should skip this part - virtual CFeatures* shallow_subset_copy() + virtual std::shared_ptr shallow_subset_copy() { not_implemented(SOURCE_LOCATION);; return NULL; @@ -343,11 +343,11 @@ class CFeatures : public CSGObject int32_t cache_size; /** list of preprocessors */ - CDynamicObjectArray* preproc; + std::vector> preproc; protected: /** subset used for index transformations */ - CSubsetStack* m_subset_stack; + std::shared_ptr m_subset_stack; }; } #endif diff --git a/src/shogun/features/ImplicitWeightedSpecFeatures.cpp b/src/shogun/features/ImplicitWeightedSpecFeatures.cpp index 476720db69e..7d05cbdb5c2 100644 --- a/src/shogun/features/ImplicitWeightedSpecFeatures.cpp +++ b/src/shogun/features/ImplicitWeightedSpecFeatures.cpp @@ -10,8 +10,8 @@ using namespace shogun; -CImplicitWeightedSpecFeatures::CImplicitWeightedSpecFeatures() - :CDotFeatures() +ImplicitWeightedSpecFeatures::ImplicitWeightedSpecFeatures() + :DotFeatures() { unstable(SOURCE_LOCATION); @@ -25,11 +25,11 @@ CImplicitWeightedSpecFeatures::CImplicitWeightedSpecFeatures() spec_weights = 0; } -CImplicitWeightedSpecFeatures::CImplicitWeightedSpecFeatures(CStringFeatures* str, bool normalize) : CDotFeatures() +ImplicitWeightedSpecFeatures::ImplicitWeightedSpecFeatures(std::shared_ptr> str, bool normalize) : DotFeatures() { ASSERT(str) strings=str; - SG_REF(strings) + normalization_factors=NULL; spec_weights=NULL; num_strings = str->get_num_vectors(); @@ -44,18 +44,19 @@ CImplicitWeightedSpecFeatures::CImplicitWeightedSpecFeatures(CStringFeatures>(shared_from_this()), i)); normalization_factors=factors; - //CMath::display_vector(normalization_factors, num_strings, "n"); + //Math::display_vector(normalization_factors, num_strings, "n"); } -bool CImplicitWeightedSpecFeatures::set_wd_weights() +bool ImplicitWeightedSpecFeatures::set_wd_weights() { SG_FREE(spec_weights); spec_weights=SG_MALLOC(float64_t, degree); @@ -66,7 +67,7 @@ bool CImplicitWeightedSpecFeatures::set_wd_weights() for (i=0; i df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CImplicitWeightedSpecFeatures* sf = (CImplicitWeightedSpecFeatures*) df; + auto sf = std::static_pointer_cast(df); ASSERT(vec_idx1 < num_strings) ASSERT(vec_idx2 < sf->get_num_vectors()) @@ -165,7 +165,7 @@ float64_t CImplicitWeightedSpecFeatures::dot(int32_t vec_idx1, CDotFeatures* df, return result; } -float64_t CImplicitWeightedSpecFeatures::dot( +float64_t ImplicitWeightedSpecFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { ASSERT(vec2.size() == spec_size) @@ -205,7 +205,7 @@ float64_t CImplicitWeightedSpecFeatures::dot( return result; } -void CImplicitWeightedSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void ImplicitWeightedSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { int32_t len1=-1; bool free_vec1; @@ -226,7 +226,7 @@ void CImplicitWeightedSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t ve int32_t idx=strings->get_masked_symbols(vec[j], mask); idx=strings->shift_symbol(idx, degree-d-1); if (abs_val) - vec2[offs + idx] += CMath::abs(alpha*spec_weights[d]); + vec2[offs + idx] += Math::abs(alpha*spec_weights[d]); else vec2[offs + idx] += alpha*spec_weights[d]; offs+=strings->shift_offset(1,d+1); @@ -237,17 +237,17 @@ void CImplicitWeightedSpecFeatures::add_to_dense_vec(float64_t alpha, int32_t ve strings->free_feature_vector(vec, vec_idx1, free_vec1); } -CFeatures* CImplicitWeightedSpecFeatures::duplicate() const +std::shared_ptr ImplicitWeightedSpecFeatures::duplicate() const { - return new CImplicitWeightedSpecFeatures(*this); + return std::make_shared(*this); } -int32_t CImplicitWeightedSpecFeatures::get_dim_feature_space() const +int32_t ImplicitWeightedSpecFeatures::get_dim_feature_space() const { return spec_size; } -void* CImplicitWeightedSpecFeatures::get_feature_iterator(int32_t vector_index) +void* ImplicitWeightedSpecFeatures::get_feature_iterator(int32_t vector_index) { if (vector_index>=num_strings) { @@ -268,7 +268,7 @@ void* CImplicitWeightedSpecFeatures::get_feature_iterator(int32_t vector_index) return it; } -bool CImplicitWeightedSpecFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool ImplicitWeightedSpecFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { wspec_feature_iterator* it=(wspec_feature_iterator*) iterator; @@ -298,7 +298,7 @@ bool CImplicitWeightedSpecFeatures::get_next_feature(int32_t& index, float64_t& return true; } -void CImplicitWeightedSpecFeatures::free_feature_iterator(void* iterator) +void ImplicitWeightedSpecFeatures::free_feature_iterator(void* iterator) { ASSERT(iterator) wspec_feature_iterator* it=(wspec_feature_iterator*) iterator; @@ -307,7 +307,7 @@ void CImplicitWeightedSpecFeatures::free_feature_iterator(void* iterator) } -int32_t CImplicitWeightedSpecFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t ImplicitWeightedSpecFeatures::get_nnz_features_for_vector(int32_t num) const { int32_t vlen=-1; bool free_vec; @@ -315,21 +315,21 @@ int32_t CImplicitWeightedSpecFeatures::get_nnz_features_for_vector(int32_t num) strings->free_feature_vector(vec1, num, free_vec); int32_t nnz=0; for (int32_t i=1; i<=degree; i++) - nnz+=CMath::min(CMath::pow(alphabet_size,i), vlen); + nnz+=Math::min(Math::pow(alphabet_size,i), vlen); return nnz; } -EFeatureType CImplicitWeightedSpecFeatures::get_feature_type() const +EFeatureType ImplicitWeightedSpecFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CImplicitWeightedSpecFeatures::get_feature_class() const +EFeatureClass ImplicitWeightedSpecFeatures::get_feature_class() const { return C_WEIGHTEDSPEC; } -int32_t CImplicitWeightedSpecFeatures::get_num_vectors() const +int32_t ImplicitWeightedSpecFeatures::get_num_vectors() const { return num_strings; } diff --git a/src/shogun/features/ImplicitWeightedSpecFeatures.h b/src/shogun/features/ImplicitWeightedSpecFeatures.h index e0a19a18a84..00f458ab828 100644 --- a/src/shogun/features/ImplicitWeightedSpecFeatures.h +++ b/src/shogun/features/ImplicitWeightedSpecFeatures.h @@ -19,36 +19,36 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /** @brief Features that compute the Weighted Spectrum Kernel feature space * explicitly. * * \sa CWeightedCommWordStringKernel */ -class CImplicitWeightedSpecFeatures : public CDotFeatures +class ImplicitWeightedSpecFeatures : public DotFeatures { public: /** default constructor */ - CImplicitWeightedSpecFeatures(); + ImplicitWeightedSpecFeatures(); /** constructor * * @param str stringfeatures (of words) * @param normalize whether to use sqrtdiag normalization */ - CImplicitWeightedSpecFeatures(CStringFeatures* str, bool normalize=true); + ImplicitWeightedSpecFeatures(std::shared_ptr> str, bool normalize=true); /** copy constructor */ - CImplicitWeightedSpecFeatures(const CImplicitWeightedSpecFeatures & orig); + ImplicitWeightedSpecFeatures(const ImplicitWeightedSpecFeatures & orig); - virtual ~CImplicitWeightedSpecFeatures(); + virtual ~ImplicitWeightedSpecFeatures(); /** duplicate feature object * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** obtain the dimensionality of the feature space * @@ -66,7 +66,7 @@ class CImplicitWeightedSpecFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -194,7 +194,7 @@ class CImplicitWeightedSpecFeatures : public CDotFeatures protected: /** reference to strings */ - CStringFeatures* strings; + std::shared_ptr> strings; /** use sqrtdiag normalization */ float64_t* normalization_factors; diff --git a/src/shogun/features/IndexFeatures.cpp b/src/shogun/features/IndexFeatures.cpp index 4f0da59271d..1febbec7767 100644 --- a/src/shogun/features/IndexFeatures.cpp +++ b/src/shogun/features/IndexFeatures.cpp @@ -3,50 +3,50 @@ using namespace shogun; -CIndexFeatures::CIndexFeatures() +IndexFeatures::IndexFeatures() { init(); } -CIndexFeatures::CIndexFeatures(SGVector feature_index) +IndexFeatures::IndexFeatures(SGVector feature_index) { init(); set_feature_index(feature_index); } -CIndexFeatures::~CIndexFeatures() +IndexFeatures::~IndexFeatures() { } -int32_t CIndexFeatures::get_num_vectors() const +int32_t IndexFeatures::get_num_vectors() const { return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : num_vectors; } -CFeatures* CIndexFeatures::duplicate() const +std::shared_ptr IndexFeatures::duplicate() const { - CIndexFeatures* indexfeature_dup = - new CIndexFeatures(m_feature_index.clone()); + auto indexfeature_dup = + std::make_shared(m_feature_index.clone()); if (m_subset_stack != NULL) { - SG_UNREF(indexfeature_dup->m_subset_stack); - indexfeature_dup->m_subset_stack=new CSubsetStack(*m_subset_stack); - SG_REF(indexfeature_dup->m_subset_stack); + + indexfeature_dup->m_subset_stack=std::make_shared(*m_subset_stack); + } return indexfeature_dup; } -EFeatureType CIndexFeatures::get_feature_type() const +EFeatureType IndexFeatures::get_feature_type() const { return F_ANY; } -EFeatureClass CIndexFeatures::get_feature_class() const +EFeatureClass IndexFeatures::get_feature_class() const { return C_INDEX; } -SGVector CIndexFeatures::get_feature_index() +SGVector IndexFeatures::get_feature_index() { if (!m_subset_stack->has_subsets()) return m_feature_index; @@ -63,7 +63,7 @@ SGVector CIndexFeatures::get_feature_index() return sub_feature_index; } -void CIndexFeatures::set_feature_index(SGVector feature_index) +void IndexFeatures::set_feature_index(SGVector feature_index) { m_subset_stack->remove_all_subsets(); free_feature_index(); @@ -71,14 +71,14 @@ void CIndexFeatures::set_feature_index(SGVector feature_index) num_vectors = feature_index.vlen; } -void CIndexFeatures::free_feature_index() +void IndexFeatures::free_feature_index() { m_subset_stack->remove_all_subsets(); m_feature_index=SGVector(); num_vectors = 0; } -void CIndexFeatures::init() +void IndexFeatures::init() { num_vectors = 0; SG_ADD(&m_feature_index, "m_feature_index", diff --git a/src/shogun/features/IndexFeatures.h b/src/shogun/features/IndexFeatures.h index 99c95b7617e..407130546ca 100644 --- a/src/shogun/features/IndexFeatures.h +++ b/src/shogun/features/IndexFeatures.h @@ -42,19 +42,19 @@ namespace shogun { /** @brief The class IndexFeatures implements features that * contain the index of the features. This features used in - * the CCustomKernel::init to make the subset of the kernel + * the CustomKernel::init to make the subset of the kernel * matrix. * Initial CIndexFeature of row_idx and col_idx, pass them - * to the CCustomKernel::init(row_idx, col_idx), then use - * CCustomKernel::get_kernel_matrix() will get the sub kernel + * to the CustomKernel::init(row_idx, col_idx), then use + * CustomKernel::get_kernel_matrix() will get the sub kernel * matrix specified by the row_idx and col_idx. * - * This is used in the CCustomKernel.*/ -class CIndexFeatures : public CDummyFeatures + * This is used in the CustomKernel.*/ +class IndexFeatures : public DummyFeatures { public: /** default constructor */ - CIndexFeatures(); + IndexFeatures(); /** constructor * @@ -63,10 +63,10 @@ class CIndexFeatures : public CDummyFeatures * * @param feature_index feature index vector */ - CIndexFeatures(SGVector feature_index); + IndexFeatures(SGVector feature_index); /** destructor */ - virtual ~CIndexFeatures(); + virtual ~IndexFeatures(); /** get number of feature vectors * @@ -81,9 +81,9 @@ class CIndexFeatures : public CDummyFeatures * * return the copy of this instance * - * @return the copy of this CIndexFeatures + * @return the copy of this IndexFeatures */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type (ANY) * @@ -135,7 +135,7 @@ class CIndexFeatures : public CDummyFeatures void free_feature_index(); private: - /** Initialize CIndexFeatures + /** Initialize IndexFeatures * * set num_vectors to 0 * add m_feature_index to m_parameters diff --git a/src/shogun/features/LBPPyrDotFeatures.cpp b/src/shogun/features/LBPPyrDotFeatures.cpp index 004170aba42..66035ce27f7 100644 --- a/src/shogun/features/LBPPyrDotFeatures.cpp +++ b/src/shogun/features/LBPPyrDotFeatures.cpp @@ -13,90 +13,90 @@ using namespace shogun; #define LIBLBP_INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW)) -CLBPPyrDotFeatures::CLBPPyrDotFeatures() : CDotFeatures() +LBPPyrDotFeatures::LBPPyrDotFeatures() : DotFeatures() { init(NULL, 0, 0); vec_nDim = 0; } -CLBPPyrDotFeatures::CLBPPyrDotFeatures(CDenseFeatures* image_set, int32_t image_w, - int32_t image_h, uint16_t num_pyramids) : CDotFeatures() +LBPPyrDotFeatures::LBPPyrDotFeatures(std::shared_ptr> image_set, int32_t image_w, + int32_t image_h, uint16_t num_pyramids) : DotFeatures() { ASSERT(image_set) init(image_set, image_w, image_h); vec_nDim = liblbp_pyr_get_dim(num_pyramids); } -void CLBPPyrDotFeatures::init(CDenseFeatures* image_set, int32_t image_w, int32_t image_h) +void LBPPyrDotFeatures::init(std::shared_ptr> image_set, int32_t image_w, int32_t image_h) { images = image_set; - SG_REF(images); + image_width = image_w; image_height = image_h; - SG_ADD((CSGObject**) &images, "images", "Set of images"); + SG_ADD((std::shared_ptr*) &images, "images", "Set of images"); SG_ADD(&image_width, "image_width", "The image width"); SG_ADD(&image_height, "image_height", "The image height"); SG_ADD(&vec_nDim, "vec_nDim", "The dimension of the pyr"); } -CLBPPyrDotFeatures::~CLBPPyrDotFeatures() +LBPPyrDotFeatures::~LBPPyrDotFeatures() { - SG_UNREF(images); + } -CLBPPyrDotFeatures::CLBPPyrDotFeatures(const CLBPPyrDotFeatures& orig) +LBPPyrDotFeatures::LBPPyrDotFeatures(const LBPPyrDotFeatures& orig) { init(orig.images, orig.image_width, orig.image_height); vec_nDim = orig.vec_nDim; } -int32_t CLBPPyrDotFeatures::get_dim_feature_space() const +int32_t LBPPyrDotFeatures::get_dim_feature_space() const { return vec_nDim; } -int32_t CLBPPyrDotFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t LBPPyrDotFeatures::get_nnz_features_for_vector(int32_t num) const { return vec_nDim; } -EFeatureType CLBPPyrDotFeatures::get_feature_type() const +EFeatureType LBPPyrDotFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CLBPPyrDotFeatures::get_feature_class() const +EFeatureClass LBPPyrDotFeatures::get_feature_class() const { return C_POLY; } -int32_t CLBPPyrDotFeatures::get_num_vectors() const +int32_t LBPPyrDotFeatures::get_num_vectors() const { return images->get_num_vectors(); } -void* CLBPPyrDotFeatures::get_feature_iterator(int32_t vector_index) +void* LBPPyrDotFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CLBPPyrDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool LBPPyrDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CLBPPyrDotFeatures::free_feature_iterator(void* iterator) +void LBPPyrDotFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } -float64_t CLBPPyrDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t LBPPyrDotFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(strcmp(df->get_name(),get_name())==0) - CLBPPyrDotFeatures* lbp_feat = (CLBPPyrDotFeatures* ) df; + auto lbp_feat = std::static_pointer_cast(df); ASSERT(get_dim_feature_space() == lbp_feat->get_dim_feature_space()); SGVector vec1 = get_transformed_image(vec_idx1); @@ -105,7 +105,7 @@ float64_t CLBPPyrDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t ve return linalg::dot(vec1, vec2); } -SGVector CLBPPyrDotFeatures::get_transformed_image(int32_t index) const +SGVector LBPPyrDotFeatures::get_transformed_image(int32_t index) const { SGVector vec(vec_nDim); SGVector::fill_vector(vec, vec_nDim, 0); @@ -152,7 +152,7 @@ SGVector CLBPPyrDotFeatures::get_transformed_image(int32_t index) const return vec; } -uint32_t* CLBPPyrDotFeatures::get_image(int32_t index, int32_t& width, int32_t& height) const +uint32_t* LBPPyrDotFeatures::get_image(int32_t index, int32_t& width, int32_t& height) const { int32_t len; bool do_free; @@ -167,7 +167,7 @@ uint32_t* CLBPPyrDotFeatures::get_image(int32_t index, int32_t& width, int32_t& } float64_t -CLBPPyrDotFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +LBPPyrDotFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.size() == vec_nDim, @@ -217,7 +217,7 @@ CLBPPyrDotFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return dot_prod; } -void CLBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void LBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len != vec_nDim) error("Dimensions don't match, vec2_dim={}, vec_nDim={}", vec2_len, vec_nDim); @@ -227,7 +227,7 @@ void CLBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, flo uint32_t* img = get_image(vec_idx1, ww, hh); if (abs_val) - alpha = CMath::abs(alpha); + alpha = Math::abs(alpha); int32_t offset = 0; @@ -266,7 +266,7 @@ void CLBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, flo SG_FREE(img); } -uint8_t CLBPPyrDotFeatures::create_lbp_pattern(uint32_t* img, int32_t x, int32_t y) const +uint8_t LBPPyrDotFeatures::create_lbp_pattern(uint32_t* img, int32_t x, int32_t y) const { uint8_t pattern = 0; uint32_t center = img[LIBLBP_INDEX(y,x,image_height)]; @@ -291,18 +291,18 @@ uint8_t CLBPPyrDotFeatures::create_lbp_pattern(uint32_t* img, int32_t x, int32_t return pattern; } -CFeatures* CLBPPyrDotFeatures::duplicate() const +std::shared_ptr LBPPyrDotFeatures::duplicate() const { - return new CLBPPyrDotFeatures(*this); + return std::make_shared(*this); } -uint32_t CLBPPyrDotFeatures::liblbp_pyr_get_dim(uint16_t nPyramids) +uint32_t LBPPyrDotFeatures::liblbp_pyr_get_dim(uint16_t nPyramids) { uint32_t N = 0; uint32_t w = image_width; uint32_t h = image_height; - for (uint32_t i=0; (i=3); i++) + for (uint32_t i=0; (i=3); i++) { N += (w-2)*(h-2); diff --git a/src/shogun/features/LBPPyrDotFeatures.h b/src/shogun/features/LBPPyrDotFeatures.h index affd01524e2..07e4616de19 100644 --- a/src/shogun/features/LBPPyrDotFeatures.h +++ b/src/shogun/features/LBPPyrDotFeatures.h @@ -17,16 +17,16 @@ namespace shogun { /** @brief Implements Local Binary Patterns with Scale Pyramids as dot features for a set - * of images. Expects the images to be loaded in a CDenseFeatures object. + * of images. Expects the images to be loaded in a DenseFeatures object. * * Original code can be found here : https://github.com/grenaud/freeIbis/tree/master/libocas_v093 * */ -class CLBPPyrDotFeatures : public CDotFeatures +class LBPPyrDotFeatures : public DotFeatures { public: /** default constructor */ - CLBPPyrDotFeatures(); + LBPPyrDotFeatures(); /** constructor that initializes this Features object with the images to work on and * the number of pyramids to consider. @@ -36,11 +36,11 @@ class CLBPPyrDotFeatures : public CDotFeatures * @param image_h image height * @param num_pyramids the number of pyramids to consider */ - CLBPPyrDotFeatures(CDenseFeatures* image_set, int32_t image_w, int32_t image_h, + LBPPyrDotFeatures(std::shared_ptr> image_set, int32_t image_w, int32_t image_h, uint16_t num_pyramids); /** Destructor */ - virtual ~CLBPPyrDotFeatures(); + virtual ~LBPPyrDotFeatures(); /** copy constructor * @@ -48,7 +48,7 @@ class CLBPPyrDotFeatures : public CDotFeatures * * @param orig original PolyFeature */ - CLBPPyrDotFeatures(const CLBPPyrDotFeatures & orig); + LBPPyrDotFeatures(const LBPPyrDotFeatures & orig); /** get dimensions of feature space * @@ -88,7 +88,7 @@ class CLBPPyrDotFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** iterate over the non-zero features * @@ -124,7 +124,7 @@ class CLBPPyrDotFeatures : public CDotFeatures * * @return feature object */ - CFeatures* duplicate() const; + std::shared_ptr duplicate() const; /** * @@ -183,12 +183,12 @@ class CLBPPyrDotFeatures : public CDotFeatures private: /** init */ - void init(CDenseFeatures* image_set, int32_t image_w, + void init(std::shared_ptr> image_set, int32_t image_w, int32_t image_h); protected: /** features in original space */ - CDenseFeatures* images; + std::shared_ptr> images; /** image width */ int32_t image_width; diff --git a/src/shogun/features/LatentFeatures.cpp b/src/shogun/features/LatentFeatures.cpp index f2027bb6ce3..14927bd1820 100644 --- a/src/shogun/features/LatentFeatures.cpp +++ b/src/shogun/features/LatentFeatures.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Viktor Gal, Heiko Strathmann, Vladislav Horbatiuk, + * Authors: Viktor Gal, Heiko Strathmann, Vladislav Horbatiuk, * Soeren Sonnenburg */ @@ -9,42 +9,39 @@ using namespace shogun; -CLatentFeatures::CLatentFeatures() +LatentFeatures::LatentFeatures():LatentFeatures(10) { - init(); - m_samples = new CDynamicObjectArray(10); - SG_REF(m_samples); } -CLatentFeatures::CLatentFeatures(int32_t num_samples) +LatentFeatures::LatentFeatures(int32_t num_samples) { init(); - m_samples = new CDynamicObjectArray(num_samples); - SG_REF(m_samples); + m_samples = std::make_shared(num_samples); + } -CLatentFeatures::~CLatentFeatures() +LatentFeatures::~LatentFeatures() { - SG_UNREF(m_samples); + } -CFeatures* CLatentFeatures::duplicate() const +std::shared_ptr LatentFeatures::duplicate() const { - return new CLatentFeatures(*this); + return std::make_shared(*this); } -EFeatureType CLatentFeatures::get_feature_type() const +EFeatureType LatentFeatures::get_feature_type() const { return F_ANY; } -EFeatureClass CLatentFeatures::get_feature_class() const +EFeatureClass LatentFeatures::get_feature_class() const { return C_LATENT; } -int32_t CLatentFeatures::get_num_vectors() const +int32_t LatentFeatures::get_num_vectors() const { if (m_samples == NULL) return 0; @@ -52,7 +49,7 @@ int32_t CLatentFeatures::get_num_vectors() const return m_samples->get_array_size(); } -bool CLatentFeatures::add_sample(CData* example) +bool LatentFeatures::add_sample(std::shared_ptr example) { ASSERT(m_samples != NULL) if (m_samples != NULL) @@ -64,26 +61,26 @@ bool CLatentFeatures::add_sample(CData* example) return false; } -CData* CLatentFeatures::get_sample(index_t idx) +std::shared_ptr LatentFeatures::get_sample(index_t idx) { ASSERT(m_samples != NULL) if (idx < 0 || idx >= this->get_num_vectors()) error("Out of index!"); - return (CData*) m_samples->get_element(idx); + return m_samples->get_element(idx); } -void CLatentFeatures::init() +void LatentFeatures::init() { - SG_ADD((CSGObject**) &m_samples, "samples", "Array of examples"); + SG_ADD((std::shared_ptr*) &m_samples, "samples", "Array of examples"); } -CLatentFeatures* CLatentFeatures::obtain_from_generic(CFeatures* base_feats) +std::shared_ptr LatentFeatures::obtain_from_generic(std::shared_ptr base_feats) { ASSERT(base_feats != NULL) if (base_feats->get_feature_class() == C_LATENT) - return (CLatentFeatures*) base_feats; + return std::static_pointer_cast(base_feats); else error("base_labels must be of dynamic type CLatentLabels"); diff --git a/src/shogun/features/LatentFeatures.h b/src/shogun/features/LatentFeatures.h index 7f67b8a95b4..ff5f832e867 100644 --- a/src/shogun/features/LatentFeatures.h +++ b/src/shogun/features/LatentFeatures.h @@ -17,27 +17,27 @@ namespace shogun /** @brief Latent Features class * The class if for representing features for latent learning, e.g. LatentSVM. * It's basically a very generic way of storing features of any (user-defined) form - * based on CData. + * based on Data. */ - class CLatentFeatures : public CFeatures + class LatentFeatures : public Features { public: /** default constructor */ - CLatentFeatures(); + LatentFeatures(); /** constructor * * @param num_samples the number of examples the object will contain */ - CLatentFeatures(int32_t num_samples); + LatentFeatures(int32_t num_samples); - virtual ~CLatentFeatures(); + virtual ~LatentFeatures(); /** Copy-constructor * * @return the copy of the given object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -65,25 +65,25 @@ namespace shogun /** add latent example * - * @param example the user defined CData + * @param example the user defined Data */ - bool add_sample(CData* example); + bool add_sample(std::shared_ptr example); /** get latent example * * @param idx index of the required example - * @return the user defined CData at the given index + * @return the user defined Data at the given index */ - CData* get_sample(index_t idx); + std::shared_ptr get_sample(index_t idx); /** helper method used to specialize a base class instance * - * @param base_feats its dynamic type must be CLatentFeatures + * @param base_feats its dynamic type must be LatentFeatures */ - static CLatentFeatures* obtain_from_generic(CFeatures* base_feats); + static std::shared_ptr obtain_from_generic(std::shared_ptr base_feats); protected: - /** array of CData */ - CDynamicObjectArray* m_samples; + /** array of Data */ + std::shared_ptr m_samples; private: /** init function for the object */ diff --git a/src/shogun/features/MatrixFeatures.cpp b/src/shogun/features/MatrixFeatures.cpp index ea3002b3cbd..e0c7f9751a3 100644 --- a/src/shogun/features/MatrixFeatures.cpp +++ b/src/shogun/features/MatrixFeatures.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Heiko Strathmann, Vladislav Horbatiuk, + * Authors: Fernando Iglesias, Heiko Strathmann, Vladislav Horbatiuk, * Bjoern Esser */ @@ -9,16 +9,16 @@ namespace shogun { -template< class ST > CMatrixFeatures< ST >::CMatrixFeatures() -: CFeatures(0) +template< class ST > MatrixFeatures< ST >::MatrixFeatures() +: Features(0) { init(); } -template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( +template< class ST > MatrixFeatures< ST >::MatrixFeatures( int32_t num_vecs, int32_t num_feats) -: CFeatures(0) +: Features(0) { init(); m_features = SGMatrixList< ST >(num_vecs); @@ -26,17 +26,17 @@ template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( m_num_features = num_feats; } -template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( +template< class ST > MatrixFeatures< ST >::MatrixFeatures( SGMatrixList< ST > feats, int32_t num_feats) -: CFeatures(0) +: Features(0) { init(); set_features(feats, num_feats); } -template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( +template< class ST > MatrixFeatures< ST >::MatrixFeatures( SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs) -: CFeatures(0) +: Features(0) { require(feats.num_cols == feat_length*num_vecs, "The number of columns of feats " "must be equal to feat_length times num_vecs"); @@ -46,28 +46,28 @@ template< class ST > CMatrixFeatures< ST >::CMatrixFeatures( } /* TODO */ -template< class ST > CFeatures* CMatrixFeatures< ST >::duplicate() const +template< class ST > std::shared_ptr MatrixFeatures< ST >::duplicate() const { return NULL; } -template< class ST > CMatrixFeatures< ST >::~CMatrixFeatures() +template< class ST > MatrixFeatures< ST >::~MatrixFeatures() { cleanup(); } /* TODO */ -template< class ST > EFeatureType CMatrixFeatures< ST >::get_feature_type() const +template< class ST > EFeatureType MatrixFeatures< ST >::get_feature_type() const { return F_UNKNOWN; } -template< class ST > EFeatureClass CMatrixFeatures< ST >::get_feature_class() const +template< class ST > EFeatureClass MatrixFeatures< ST >::get_feature_class() const { return C_MATRIX; } -template< class ST > SGMatrix< ST > CMatrixFeatures< ST >::get_feature_vector( +template< class ST > SGMatrix< ST > MatrixFeatures< ST >::get_feature_vector( int32_t num) const { if ( num < 0 || num >= get_num_vectors() ) @@ -79,7 +79,7 @@ template< class ST > SGMatrix< ST > CMatrixFeatures< ST >::get_feature_vector( return m_features[num]; } -template< class ST > void CMatrixFeatures< ST >::get_feature_vector_col( +template< class ST > void MatrixFeatures< ST >::get_feature_vector_col( SGVector< ST > out, int32_t num, int32_t col) const @@ -113,7 +113,7 @@ template< class ST > void CMatrixFeatures< ST >::get_feature_vector_col( } } -template< class ST > void CMatrixFeatures< ST >::set_feature_vector( +template< class ST > void MatrixFeatures< ST >::set_feature_vector( SGMatrix< ST > const vec, int32_t num) { @@ -133,7 +133,7 @@ template< class ST > void CMatrixFeatures< ST >::set_feature_vector( m_features.set_matrix(num, vec); } -template< class ST > void CMatrixFeatures< ST >::set_features( +template< class ST > void MatrixFeatures< ST >::set_features( SGMatrixList< ST > features, int32_t num_feats) { m_features = features; @@ -141,11 +141,11 @@ template< class ST > void CMatrixFeatures< ST >::set_features( m_num_features = num_feats; } -template< class ST > void CMatrixFeatures< ST >::init() +template< class ST > void MatrixFeatures< ST >::init() { - SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors"); - SG_ADD(&m_num_features, "m_num_features", - "Number of features per vector (optional)"); + //SG_ADD(&m_num_vectors, "m_num_vectors", "Number of feature vectors"); + //SG_ADD(&m_num_features, "m_num_features", + // "Number of features per vector (optional)"); //TODO add SG_ADD for SGMatrixList //SG_ADD(&m_features, "m_features", "Matrix features"); @@ -155,33 +155,33 @@ template< class ST > void CMatrixFeatures< ST >::init() set_generic(); } -template< class ST > void CMatrixFeatures< ST >::cleanup() +template< class ST > void MatrixFeatures< ST >::cleanup() { m_features = SGMatrixList< ST >(); m_num_vectors = 0; m_num_features = 0; } -template< class ST > CMatrixFeatures< ST >* CMatrixFeatures< ST >::obtain_from_generic(CFeatures* const base_features) +template< class ST > std::shared_ptr> MatrixFeatures< ST >::obtain_from_generic(std::shared_ptr base_features) { require(base_features->get_feature_class() == C_MATRIX, "base_features must be of dynamic type CMatrixFeatures"); - return (CMatrixFeatures< ST >*) base_features; + return std::dynamic_pointer_cast>(base_features); } -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; -template class CMatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; +template class MatrixFeatures; } /* namespace shogun */ diff --git a/src/shogun/features/MatrixFeatures.h b/src/shogun/features/MatrixFeatures.h index 49bf5bafa85..13eb3f1a1f8 100644 --- a/src/shogun/features/MatrixFeatures.h +++ b/src/shogun/features/MatrixFeatures.h @@ -24,25 +24,25 @@ namespace shogun * to use this restriction. Allow feature vectors with different number of * features by setting num_features equal to zero (default behaviour). */ -template< class ST > class CMatrixFeatures : public CFeatures +template< class ST > class MatrixFeatures : public Features { public: /** default constructor */ - CMatrixFeatures(); + MatrixFeatures(); /** standard constructor * * @param num_vecs number of vectors * @param num_feats number of features per vector */ - CMatrixFeatures(int32_t num_vecs, int32_t num_feats = 0); + MatrixFeatures(int32_t num_vecs, int32_t num_feats = 0); /** constructor * * @param feats list of feature matrices * @param num_feats number of features per vector */ - CMatrixFeatures(SGMatrixList< ST > feats, int32_t num_feats = 0); + MatrixFeatures(SGMatrixList< ST > feats, int32_t num_feats = 0); /** * constructor using the data of all the features concatenated @@ -55,16 +55,16 @@ template< class ST > class CMatrixFeatures : public CFeatures * @param feat_length length of each feature * @param num_vecs number of feature vectors */ - CMatrixFeatures(SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs); + MatrixFeatures(SGMatrix< ST > feats, int32_t feat_length, int32_t num_vecs); /** duplicate feature object * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** destructor */ - virtual ~CMatrixFeatures(); + virtual ~MatrixFeatures(); /** get feature type * @@ -131,7 +131,7 @@ template< class ST > class CMatrixFeatures : public CFeatures /** helper method used to specialize a base class instance * */ - static CMatrixFeatures* obtain_from_generic(CFeatures* const base_features); + static std::shared_ptr> obtain_from_generic(std::shared_ptr base_features); private: /** internal initialization */ diff --git a/src/shogun/features/PolyFeatures.cpp b/src/shogun/features/PolyFeatures.cpp index c68d1a04143..3b6fae35793 100644 --- a/src/shogun/features/PolyFeatures.cpp +++ b/src/shogun/features/PolyFeatures.cpp @@ -2,7 +2,7 @@ using namespace shogun; -CPolyFeatures::CPolyFeatures() :CDotFeatures() +PolyFeatures::PolyFeatures() :DotFeatures() { m_feat=NULL; m_degree=0; @@ -16,14 +16,14 @@ CPolyFeatures::CPolyFeatures() :CDotFeatures() register_parameters(); } -CPolyFeatures::CPolyFeatures(CDenseFeatures* feat, int32_t degree, bool normalize) - : CDotFeatures(), m_multi_index(NULL), m_multinomial_coefficients(NULL), +PolyFeatures::PolyFeatures(std::shared_ptr> feat, int32_t degree, bool normalize) + : DotFeatures(), m_multi_index(NULL), m_multinomial_coefficients(NULL), m_normalization_values(NULL) { ASSERT(feat) m_feat = feat; - SG_REF(m_feat); + m_degree=degree; m_normalize=normalize; m_input_dimensions=feat->get_num_features(); @@ -38,41 +38,41 @@ CPolyFeatures::CPolyFeatures(CDenseFeatures* feat, int32_t degree, bo } -CPolyFeatures::~CPolyFeatures() +PolyFeatures::~PolyFeatures() { SG_FREE(m_multi_index); SG_FREE(m_multinomial_coefficients); SG_FREE(m_normalization_values); - SG_UNREF(m_feat); + } -CPolyFeatures::CPolyFeatures(const CPolyFeatures & orig) +PolyFeatures::PolyFeatures(const PolyFeatures & orig) { io::print("CPolyFeatures:\n"); not_implemented(SOURCE_LOCATION); }; -int32_t CPolyFeatures::get_dim_feature_space() const +int32_t PolyFeatures::get_dim_feature_space() const { return m_output_dimensions; } -int32_t CPolyFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t PolyFeatures::get_nnz_features_for_vector(int32_t num) const { return m_output_dimensions; } -EFeatureType CPolyFeatures::get_feature_type() const +EFeatureType PolyFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CPolyFeatures::get_feature_class() const +EFeatureClass PolyFeatures::get_feature_class() const { return C_POLY; } -int32_t CPolyFeatures::get_num_vectors() const +int32_t PolyFeatures::get_num_vectors() const { if (m_feat) return m_feat->get_num_vectors(); @@ -81,32 +81,32 @@ int32_t CPolyFeatures::get_num_vectors() const } -void* CPolyFeatures::get_feature_iterator(int32_t vector_index) +void* PolyFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CPolyFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool PolyFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CPolyFeatures::free_feature_iterator(void* iterator) +void PolyFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } -float64_t CPolyFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t PolyFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CPolyFeatures* pf=(CPolyFeatures*) df; + auto pf=std::static_pointer_cast(df); int32_t len1; bool do_free1; @@ -137,7 +137,7 @@ float64_t CPolyFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx } float64_t -CPolyFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +PolyFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.size() == m_output_dimensions, @@ -168,7 +168,7 @@ CPolyFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return sum; } -void CPolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void PolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len != m_output_dimensions) error("Dimensions don't match, vec2_dim={}, m_output_dimensions={}", vec2_len, m_output_dimensions); @@ -192,13 +192,13 @@ void CPolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_ cnt++; } if (abs_val) - output=CMath::abs(output); + output=Math::abs(output); vec2[j]+=alpha*output; } m_feat->free_feature_vector(vec, len, do_free); } -void CPolyFeatures::store_normalization_values() +void PolyFeatures::store_normalization_values() { SG_FREE(m_normalization_values); @@ -207,7 +207,7 @@ void CPolyFeatures::store_normalization_values() m_normalization_values=SG_MALLOC(float32_t, num_vec); for (int i=0; i>(shared_from_this()), i)); if (tmp==0) // trap division by zero m_normalization_values[i]=1; @@ -217,7 +217,7 @@ void CPolyFeatures::store_normalization_values() } -void CPolyFeatures::store_multi_index() +void PolyFeatures::store_multi_index() { SG_FREE(m_multi_index); @@ -233,7 +233,7 @@ void CPolyFeatures::store_multi_index() SG_FREE(exponents); } -void CPolyFeatures::enumerate_multi_index(const int32_t feat_idx, uint16_t** index, uint16_t* exponents, const int32_t degree) +void PolyFeatures::enumerate_multi_index(const int32_t feat_idx, uint16_t** index, uint16_t* exponents, const int32_t degree) { if (feat_idx==m_input_dimensions-1 || degree==0) { @@ -261,7 +261,7 @@ void CPolyFeatures::enumerate_multi_index(const int32_t feat_idx, uint16_t** ind } -void CPolyFeatures::store_multinomial_coefficients() +void PolyFeatures::store_multinomial_coefficients() { SG_FREE(m_multinomial_coefficients); @@ -289,7 +289,7 @@ void CPolyFeatures::store_multinomial_coefficients() SG_FREE(exponents); } -int32_t CPolyFeatures::bico2(int32_t n, int32_t k) +int32_t PolyFeatures::bico2(int32_t n, int32_t k) { /* for this problem k is usually small (<=degree), @@ -314,7 +314,7 @@ int32_t CPolyFeatures::bico2(int32_t n, int32_t k) } -int32_t CPolyFeatures::calc_feature_space_dimensions(int32_t N, int32_t D) +int32_t PolyFeatures::calc_feature_space_dimensions(int32_t N, int32_t D) { if (N==1) return 1; @@ -328,7 +328,7 @@ int32_t CPolyFeatures::calc_feature_space_dimensions(int32_t N, int32_t D) return ret; } -int32_t CPolyFeatures::multinomialcoef(int32_t* exps, int32_t len) +int32_t PolyFeatures::multinomialcoef(int32_t* exps, int32_t len) { int32_t ret = 1, i; int32_t n = 0; @@ -342,7 +342,7 @@ int32_t CPolyFeatures::multinomialcoef(int32_t* exps, int32_t len) /* gammln as implemented in the * second edition of Numerical Recipes in C */ -float64_t CPolyFeatures::gammln(float64_t xx) +float64_t PolyFeatures::gammln(float64_t xx) { float64_t x,y,tmp,ser; static float64_t cof[6]={76.18009172947146, -86.50532032941677, @@ -358,7 +358,7 @@ float64_t CPolyFeatures::gammln(float64_t xx) return -tmp+log(2.5066282746310005*ser/x); } -float64_t CPolyFeatures::factln(int32_t n) +float64_t PolyFeatures::factln(int32_t n) { static float64_t a[101]; @@ -368,20 +368,20 @@ float64_t CPolyFeatures::factln(int32_t n) else return gammln(n+1.0); } -int32_t CPolyFeatures::bico(int32_t n, int32_t k) +int32_t PolyFeatures::bico(int32_t n, int32_t k) { /* use floor to clean roundoff errors*/ return (int32_t) floor(0.5+exp(factln(n)-factln(k)-factln(n-k))); } -CFeatures* CPolyFeatures::duplicate() const +std::shared_ptr PolyFeatures::duplicate() const { - return new CPolyFeatures(*this); + return std::make_shared(*this); } -void CPolyFeatures::register_parameters() +void PolyFeatures::register_parameters() { SG_ADD( - (CSGObject**)&m_feat, "features", "Features in original space."); + (std::shared_ptr*)&m_feat, "features", "Features in original space."); SG_ADD( &m_degree, "degree", "Degree of the polynomial kernel.", ParameterProperties::HYPER); SG_ADD(&m_normalize, "normalize", "Normalize?"); @@ -393,26 +393,26 @@ void CPolyFeatures::register_parameters() "Dimensions of the feature space of the polynomial kernel."); multi_index_length=m_output_dimensions*m_degree; - m_parameters->add_vector( + /*m_parameters->add_vector( &m_multi_index, &multi_index_length, "multi_index", "Flattened matrix of all multi indices that sum do the" - " degree of the polynomial kernel."); + " degree of the polynomial kernel.");*/ watch_param("multi_index", &m_multi_index, &multi_index_length); multinomial_coefficients_length=m_output_dimensions; - m_parameters->add_vector(&m_multinomial_coefficients, + /*m_parameters->add_vector(&m_multinomial_coefficients, &multinomial_coefficients_length, "multinomial_coefficients", - "Multinomial coefficients for all multi-indices."); + "Multinomial coefficients for all multi-indices.");*/ watch_param( "multinomial_coefficients", &m_multinomial_coefficients, &multinomial_coefficients_length); normalization_values_length=get_num_vectors(); - m_parameters->add_vector(&m_normalization_values, + /*m_parameters->add_vector(&m_normalization_values, &normalization_values_length, "normalization_values", - "Norm of each training example."); + "Norm of each training example.");*/ watch_param( "normalization_values", &m_normalization_values, &normalization_values_length); diff --git a/src/shogun/features/PolyFeatures.h b/src/shogun/features/PolyFeatures.h index ed698a62728..fe0b7ed519d 100644 --- a/src/shogun/features/PolyFeatures.h +++ b/src/shogun/features/PolyFeatures.h @@ -21,11 +21,11 @@ namespace shogun * see DotFeatures for further discription * */ -class CPolyFeatures : public CDotFeatures +class PolyFeatures : public DotFeatures { public: /** default constructor */ - CPolyFeatures(); + PolyFeatures(); /** constructor * @@ -33,9 +33,9 @@ class CPolyFeatures : public CDotFeatures * @param degree degree of the polynomial kernel * @param normalize normalize kernel */ - CPolyFeatures(CDenseFeatures* feat, int32_t degree, bool normalize); + PolyFeatures(std::shared_ptr> feat, int32_t degree, bool normalize); - virtual ~CPolyFeatures(); + virtual ~PolyFeatures(); /** copy constructor * @@ -43,7 +43,7 @@ class CPolyFeatures : public CDotFeatures * * @param orig original PolyFeature */ - CPolyFeatures(const CPolyFeatures & orig); + PolyFeatures(const PolyFeatures & orig); /** get dimensions of feature space * @@ -83,13 +83,13 @@ class CPolyFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** duplicate feature object * * @return feature object */ - CFeatures* duplicate() const; + std::shared_ptr duplicate() const; /** * @@ -210,7 +210,7 @@ class CPolyFeatures : public CDotFeatures protected: /** features in original space*/ - CDenseFeatures* m_feat; + std::shared_ptr> m_feat; /** degree of the polynomial kernel */ int32_t m_degree; /** normalize */ diff --git a/src/shogun/features/RandomFourierDotFeatures.cpp b/src/shogun/features/RandomFourierDotFeatures.cpp index c090c42ebcc..04bc6342ce9 100644 --- a/src/shogun/features/RandomFourierDotFeatures.cpp +++ b/src/shogun/features/RandomFourierDotFeatures.cpp @@ -14,43 +14,43 @@ namespace shogun { enum KernelName; -CRandomFourierDotFeatures::CRandomFourierDotFeatures() +RandomFourierDotFeatures::RandomFourierDotFeatures() { init(NOT_SPECIFIED, SGVector()); } -CRandomFourierDotFeatures::CRandomFourierDotFeatures(CDotFeatures* features, +RandomFourierDotFeatures::RandomFourierDotFeatures(std::shared_ptr features, int32_t D, KernelName kernel_name, SGVector params) -: CRandomKitchenSinksDotFeatures(features, D) +: RandomKitchenSinksDotFeatures(features, D) { init(kernel_name, params); random_coeff = generate_random_coefficients(); } -CRandomFourierDotFeatures::CRandomFourierDotFeatures(CDotFeatures* features, +RandomFourierDotFeatures::RandomFourierDotFeatures(std::shared_ptr features, int32_t D, KernelName kernel_name, SGVector params, SGMatrix coeff) -: CRandomKitchenSinksDotFeatures(features, D, coeff) +: RandomKitchenSinksDotFeatures(features, D, coeff) { init(kernel_name, params); } -CRandomFourierDotFeatures::CRandomFourierDotFeatures(CFile* loader) +RandomFourierDotFeatures::RandomFourierDotFeatures(std::shared_ptr loader) { not_implemented(SOURCE_LOCATION);; } -CRandomFourierDotFeatures::CRandomFourierDotFeatures(const CRandomFourierDotFeatures& orig) -: CRandomKitchenSinksDotFeatures(orig) +RandomFourierDotFeatures::RandomFourierDotFeatures(const RandomFourierDotFeatures& orig) +: RandomKitchenSinksDotFeatures(orig) { init(orig.kernel, orig.kernel_params); } -CRandomFourierDotFeatures::~CRandomFourierDotFeatures() +RandomFourierDotFeatures::~RandomFourierDotFeatures() { } - void CRandomFourierDotFeatures::init( + void RandomFourierDotFeatures::init( KernelName kernel_name, SGVector params) { kernel = kernel_name; @@ -66,26 +66,26 @@ CRandomFourierDotFeatures::~CRandomFourierDotFeatures() ParameterProperties::NONE, SG_OPTIONS(GAUSSIAN, NOT_SPECIFIED)); } -CFeatures* CRandomFourierDotFeatures::duplicate() const +std::shared_ptr RandomFourierDotFeatures::duplicate() const { - return new CRandomFourierDotFeatures(*this); + return std::make_shared(*this); } -const char* CRandomFourierDotFeatures::get_name() const +const char* RandomFourierDotFeatures::get_name() const { return "RandomFourierDotFeatures"; } -float64_t CRandomFourierDotFeatures::post_dot(float64_t dot_result, index_t par_idx) const +float64_t RandomFourierDotFeatures::post_dot(float64_t dot_result, index_t par_idx) const { dot_result += random_coeff(random_coeff.num_rows-1, par_idx); return std::cos(dot_result) * constant; } -SGVector CRandomFourierDotFeatures::generate_random_parameter_vector() +SGVector RandomFourierDotFeatures::generate_random_parameter_vector() { NormalDistribution normal_dist; - UniformRealDistribution uniform_real_dist(0.0, 2 * CMath::PI); + UniformRealDistribution uniform_real_dist(0.0, 2 * Math::PI); SGVector vec(feats->get_dim_feature_space()+1); switch (kernel) { diff --git a/src/shogun/features/RandomFourierDotFeatures.h b/src/shogun/features/RandomFourierDotFeatures.h index 444e29bea9a..cb9bd7d5c59 100644 --- a/src/shogun/features/RandomFourierDotFeatures.h +++ b/src/shogun/features/RandomFourierDotFeatures.h @@ -15,8 +15,8 @@ namespace shogun { -template class CDenseFeatures; -class CDotFeatures; +template class DenseFeatures; +class DotFeatures; /** names of kernels that can be approximated currently */ enum KernelName @@ -41,12 +41,12 @@ enum KernelName * For more detailed information you can take a look at this source: * i) Random Features for Large-Scale Kernel Machines - Ali Rahimi and Ben Recht */ -class CRandomFourierDotFeatures : public CRandomKitchenSinksDotFeatures +class RandomFourierDotFeatures : public RandomKitchenSinksDotFeatures { public: /** default constructor */ - CRandomFourierDotFeatures(); + RandomFourierDotFeatures(); /** constructor that creates new random coefficients, basedon the kernel specified and the parameters * of the kernel. @@ -56,7 +56,7 @@ class CRandomFourierDotFeatures : public CRandomKitchenSinksDotFeatures * @param kernel_name the name of the kernel to approximate * @param params kernel parameters (see kernel's description in KernelName to see what each kernel expects) */ - CRandomFourierDotFeatures(CDotFeatures* features, int32_t D, KernelName kernel_name, + RandomFourierDotFeatures(std::shared_ptr features, int32_t D, KernelName kernel_name, SGVector params); /** constructor that uses the specified random coefficients. @@ -67,23 +67,23 @@ class CRandomFourierDotFeatures : public CRandomKitchenSinksDotFeatures * @param params kernel parameters (see kernel's description in KernelName to see what each kernel expects) * @param coeff pre-computed random coefficients to use */ - CRandomFourierDotFeatures(CDotFeatures* features, int32_t D, KernelName kernel_name, + RandomFourierDotFeatures(std::shared_ptr features, int32_t D, KernelName kernel_name, SGVector params, SGMatrix coeff); /** constructor loading features from file * * @param loader File object via which to load data */ - CRandomFourierDotFeatures(CFile* loader); + RandomFourierDotFeatures(std::shared_ptr loader); /** copy constructor */ - CRandomFourierDotFeatures(const CRandomFourierDotFeatures& orig); + RandomFourierDotFeatures(const RandomFourierDotFeatures& orig); /** duplicate */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** destructor */ - virtual ~CRandomFourierDotFeatures(); + virtual ~RandomFourierDotFeatures(); /** @return object name */ virtual const char* get_name() const; diff --git a/src/shogun/features/RandomFourierDotFeatures_benchmark.cc b/src/shogun/features/RandomFourierDotFeatures_benchmark.cc index 86133dc2889..20ada5a9b08 100644 --- a/src/shogun/features/RandomFourierDotFeatures_benchmark.cc +++ b/src/shogun/features/RandomFourierDotFeatures_benchmark.cc @@ -31,7 +31,7 @@ static std::shared_ptr createRandomData(const benchma mat(j,i) = uniform_int_dist(prng) + 0.5; } } - auto dense_feats = new CDenseFeatures(mat); + auto dense_feats = new DenseFeatures(mat); SGVector params(1); params[0] = num_dim - 20; return std::make_shared(dense_feats, state.range(1), KernelName::GAUSSIAN, params); diff --git a/src/shogun/features/RandomKitchenSinksDotFeatures.cpp b/src/shogun/features/RandomKitchenSinksDotFeatures.cpp index 3c0918ede87..3c12b4876d1 100644 --- a/src/shogun/features/RandomKitchenSinksDotFeatures.cpp +++ b/src/shogun/features/RandomKitchenSinksDotFeatures.cpp @@ -13,26 +13,26 @@ namespace shogun class CRKSFunctions; -CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures() - : RandomMixin() +RandomKitchenSinksDotFeatures::RandomKitchenSinksDotFeatures() + : RandomMixin() { init(NULL, 0); } -CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures( - CDotFeatures* dataset, int32_t K) +RandomKitchenSinksDotFeatures::RandomKitchenSinksDotFeatures( + std::shared_ptr dataset, int32_t K) { init(dataset, K); } -CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures( - CDotFeatures* dataset, int32_t K, SGMatrix coeff) +RandomKitchenSinksDotFeatures::RandomKitchenSinksDotFeatures( + std::shared_ptr dataset, int32_t K, SGMatrix coeff) { init(dataset, K); random_coeff = coeff; } -SGMatrix CRandomKitchenSinksDotFeatures::generate_random_coefficients() +SGMatrix RandomKitchenSinksDotFeatures::generate_random_coefficients() { SGVector vec = generate_random_parameter_vector(); SGMatrix random_params(vec.vlen, num_samples); @@ -48,46 +48,46 @@ SGMatrix CRandomKitchenSinksDotFeatures::generate_random_coefficients return random_params; } -CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures(CFile* loader) +RandomKitchenSinksDotFeatures::RandomKitchenSinksDotFeatures(std::shared_ptr loader) { not_implemented(SOURCE_LOCATION);; } -CRandomKitchenSinksDotFeatures::CRandomKitchenSinksDotFeatures( - const CRandomKitchenSinksDotFeatures& orig) +RandomKitchenSinksDotFeatures::RandomKitchenSinksDotFeatures( + const RandomKitchenSinksDotFeatures& orig) { init(orig.feats, orig.num_samples); random_coeff = orig.random_coeff; } -CRandomKitchenSinksDotFeatures::~CRandomKitchenSinksDotFeatures() +RandomKitchenSinksDotFeatures::~RandomKitchenSinksDotFeatures() { - SG_UNREF(feats); + } -void CRandomKitchenSinksDotFeatures::init(CDotFeatures* dataset, +void RandomKitchenSinksDotFeatures::init(std::shared_ptr dataset, int32_t K) { feats = dataset; - SG_REF(feats); + num_samples = K; - SG_ADD((CSGObject** ) &feats, "feats", "Features to work on"); + SG_ADD((std::shared_ptr* ) &feats, "feats", "Features to work on"); SG_ADD( &random_coeff, "random_coeff", "Random function parameters"); } -int32_t CRandomKitchenSinksDotFeatures::get_dim_feature_space() const +int32_t RandomKitchenSinksDotFeatures::get_dim_feature_space() const { return num_samples; } -float64_t CRandomKitchenSinksDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, +float64_t RandomKitchenSinksDotFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(typeid(*this) == typeid(*df)); - CRandomKitchenSinksDotFeatures* other = (CRandomKitchenSinksDotFeatures* ) df; + auto other = std::static_pointer_cast(df); ASSERT(get_dim_feature_space()==other->get_dim_feature_space()); float64_t dot_product = 0; @@ -103,7 +103,7 @@ float64_t CRandomKitchenSinksDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df return dot_product; } -float64_t CRandomKitchenSinksDotFeatures::dot( +float64_t RandomKitchenSinksDotFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { SG_TRACE("entering dense_dot()"); @@ -120,7 +120,7 @@ float64_t CRandomKitchenSinksDotFeatures::dot( return dot_product; } -void CRandomKitchenSinksDotFeatures::add_to_dense_vec(float64_t alpha, +void RandomKitchenSinksDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { SG_TRACE("Entering add_to_dense()"); @@ -131,74 +131,74 @@ void CRandomKitchenSinksDotFeatures::add_to_dense_vec(float64_t alpha, float64_t tmp_dot = dot(vec_idx1, i); tmp_dot = post_dot(tmp_dot, i); if (abs_val) - vec2[i] += CMath::abs(alpha * tmp_dot); + vec2[i] += Math::abs(alpha * tmp_dot); else vec2[i] += alpha * tmp_dot; } SG_TRACE("Leaving add_to_dense()"); } -int32_t CRandomKitchenSinksDotFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t RandomKitchenSinksDotFeatures::get_nnz_features_for_vector(int32_t num) const { return num_samples; } -void* CRandomKitchenSinksDotFeatures::get_feature_iterator(int32_t vector_index) +void* RandomKitchenSinksDotFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION);; return NULL; } -bool CRandomKitchenSinksDotFeatures::get_next_feature(int32_t& index, +bool RandomKitchenSinksDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION);; return false; } -void CRandomKitchenSinksDotFeatures::free_feature_iterator(void* iterator) +void RandomKitchenSinksDotFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION);; } -EFeatureType CRandomKitchenSinksDotFeatures::get_feature_type() const +EFeatureType RandomKitchenSinksDotFeatures::get_feature_type() const { return F_DREAL; } -EFeatureClass CRandomKitchenSinksDotFeatures::get_feature_class() const +EFeatureClass RandomKitchenSinksDotFeatures::get_feature_class() const { return C_DENSE; } -int32_t CRandomKitchenSinksDotFeatures::get_num_vectors() const +int32_t RandomKitchenSinksDotFeatures::get_num_vectors() const { return feats->get_num_vectors(); } -const char* CRandomKitchenSinksDotFeatures::get_name() const +const char* RandomKitchenSinksDotFeatures::get_name() const { return "RandomKitchenSinksDotFeatures"; } -CFeatures* CRandomKitchenSinksDotFeatures::duplicate() const +std::shared_ptr RandomKitchenSinksDotFeatures::duplicate() const { not_implemented(SOURCE_LOCATION);; return NULL; } -SGMatrix CRandomKitchenSinksDotFeatures::get_random_coefficients() +SGMatrix RandomKitchenSinksDotFeatures::get_random_coefficients() { return random_coeff; } -float64_t CRandomKitchenSinksDotFeatures::dot(index_t vec_idx, index_t par_idx) const +float64_t RandomKitchenSinksDotFeatures::dot(index_t vec_idx, index_t par_idx) const { auto vec2 = random_coeff.get_column(par_idx).slice(0, feats->get_dim_feature_space()); return feats->dot(vec_idx, vec2); } -float64_t CRandomKitchenSinksDotFeatures::post_dot(float64_t dot_result, index_t par_idx) const +float64_t RandomKitchenSinksDotFeatures::post_dot(float64_t dot_result, index_t par_idx) const { return dot_result; } diff --git a/src/shogun/features/RandomKitchenSinksDotFeatures.h b/src/shogun/features/RandomKitchenSinksDotFeatures.h index c0f4c309c10..4c32ac2de95 100644 --- a/src/shogun/features/RandomKitchenSinksDotFeatures.h +++ b/src/shogun/features/RandomKitchenSinksDotFeatures.h @@ -44,12 +44,12 @@ namespace shogun * http://www.shloosl.com/~ali/random-features/ * https://research.microsoft.com/apps/video/dl.aspx?id=103390&l=i */ -class CRandomKitchenSinksDotFeatures : public RandomMixin +class RandomKitchenSinksDotFeatures : public RandomMixin { public: /** default constructor */ - CRandomKitchenSinksDotFeatures(); + RandomKitchenSinksDotFeatures(); /** constructor * Subclasses should call generate_random_coefficients() on their @@ -58,7 +58,7 @@ class CRandomKitchenSinksDotFeatures : public RandomMixin * @param dataset the dataset to work on * @param K the number of samples to draw */ - CRandomKitchenSinksDotFeatures(CDotFeatures* dataset, int32_t K); + RandomKitchenSinksDotFeatures(std::shared_ptr dataset, int32_t K); /** constructor * @@ -66,23 +66,23 @@ class CRandomKitchenSinksDotFeatures : public RandomMixin * @param K the number of samples to draw * @param coeff the random coefficients to use */ - CRandomKitchenSinksDotFeatures(CDotFeatures* dataset, int32_t K, + RandomKitchenSinksDotFeatures(std::shared_ptr dataset, int32_t K, SGMatrix coeff); /** constructor loading features from file * * @param loader File object via which to load data */ - CRandomKitchenSinksDotFeatures(CFile* loader); + RandomKitchenSinksDotFeatures(std::shared_ptr loader); /** copy constructor */ - CRandomKitchenSinksDotFeatures(const CRandomKitchenSinksDotFeatures& orig); + RandomKitchenSinksDotFeatures(const RandomKitchenSinksDotFeatures& orig); /** duplicate */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** destructor */ - virtual ~CRandomKitchenSinksDotFeatures(); + virtual ~RandomKitchenSinksDotFeatures(); /** obtain the dimensionality of the feature space * @@ -102,7 +102,7 @@ class CRandomKitchenSinksDotFeatures : public RandomMixin * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector @@ -226,12 +226,12 @@ class CRandomKitchenSinksDotFeatures : public RandomMixin */ virtual SGVector generate_random_parameter_vector()=0; private: - void init(CDotFeatures* dataset, int32_t K); + void init(std::shared_ptr dataset, int32_t K); protected: /** the dataset to work on */ - CDotFeatures* feats; + std::shared_ptr feats; /** the number of samples to use */ int32_t num_samples; diff --git a/src/shogun/features/RealFileFeatures.cpp b/src/shogun/features/RealFileFeatures.cpp index 209112b4c4f..b5848837efe 100644 --- a/src/shogun/features/RealFileFeatures.cpp +++ b/src/shogun/features/RealFileFeatures.cpp @@ -14,14 +14,14 @@ using namespace shogun; -CRealFileFeatures::CRealFileFeatures() +RealFileFeatures::RealFileFeatures() { unstable(SOURCE_LOCATION); init(); } -CRealFileFeatures::CRealFileFeatures(int32_t size, char* fname) -: CDenseFeatures(size) +RealFileFeatures::RealFileFeatures(int32_t size, char* fname) +: DenseFeatures(size) { init(); @@ -31,8 +31,8 @@ CRealFileFeatures::CRealFileFeatures(int32_t size, char* fname) status=load_base_data(); } -CRealFileFeatures::CRealFileFeatures(int32_t size, FILE* file) -: CDenseFeatures(size) +RealFileFeatures::RealFileFeatures(int32_t size, FILE* file) +: DenseFeatures(size) { init(); @@ -40,7 +40,7 @@ CRealFileFeatures::CRealFileFeatures(int32_t size, FILE* file) status=load_base_data(); } -void CRealFileFeatures::init() +void RealFileFeatures::init() { working_file=NULL; working_filename=get_strdup(""); @@ -55,14 +55,14 @@ void CRealFileFeatures::init() unset_generic(); } -CRealFileFeatures::~CRealFileFeatures() +RealFileFeatures::~RealFileFeatures() { SG_FREE(working_filename); SG_FREE(labels); } -CRealFileFeatures::CRealFileFeatures(const CRealFileFeatures & orig) -: CDenseFeatures(orig), working_file(orig.working_file), status(orig.status) +RealFileFeatures::RealFileFeatures(const RealFileFeatures & orig) +: DenseFeatures(orig), working_file(orig.working_file), status(orig.status) { if (orig.working_filename) working_filename=get_strdup(orig.working_filename); @@ -73,7 +73,7 @@ CRealFileFeatures::CRealFileFeatures(const CRealFileFeatures & orig) } } -float64_t* CRealFileFeatures::compute_feature_vector( +float64_t* RealFileFeatures::compute_feature_vector( int32_t num, int32_t &len, float64_t* target) const { ASSERT(numfrom a file. * - * It inherits its functionality from CDenseFeatures, which should be + * It inherits its functionality from DenseFeatures, which should be * consulted for further reference. */ -class CRealFileFeatures: public CDenseFeatures +class RealFileFeatures: public DenseFeatures { public: /** default constructor */ - CRealFileFeatures(); + RealFileFeatures(); /** constructor * * @param size cache size * @param file file to load features from */ - CRealFileFeatures(int32_t size, FILE* file); + RealFileFeatures(int32_t size, FILE* file); /** constructor * * @param size cache size * @param filename filename to load features from */ - CRealFileFeatures(int32_t size, char* filename); + RealFileFeatures(int32_t size, char* filename); /** copy constructor */ - CRealFileFeatures(const CRealFileFeatures& orig); + RealFileFeatures(const RealFileFeatures& orig); - virtual ~CRealFileFeatures(); + virtual ~RealFileFeatures(); /** load feature matrix * diff --git a/src/shogun/features/SNPFeatures.cpp b/src/shogun/features/SNPFeatures.cpp index 79b8bfee06a..c5758b782f9 100644 --- a/src/shogun/features/SNPFeatures.cpp +++ b/src/shogun/features/SNPFeatures.cpp @@ -11,7 +11,7 @@ using namespace shogun; -CSNPFeatures::CSNPFeatures() +SNPFeatures::SNPFeatures() { unstable(SOURCE_LOCATION); @@ -27,33 +27,32 @@ CSNPFeatures::CSNPFeatures() m_str_maj = NULL; } -CSNPFeatures::CSNPFeatures(CStringFeatures* str) : CDotFeatures(), +SNPFeatures::SNPFeatures(std::shared_ptr> str) : DotFeatures(), m_str_min(NULL), m_str_maj(NULL) { ASSERT(str) ASSERT(str->have_same_length()) - SG_REF(str); + strings=str; string_length=str->get_max_vector_length(); ASSERT((string_length & 1) == 0) // length divisible by 2 w_dim=3*string_length/2; num_strings=str->get_num_vectors(); - CAlphabet* alpha=str->get_alphabet(); + auto alpha=str->get_alphabet(); ASSERT(alpha->get_alphabet()==SNP) - SG_UNREF(alpha); obtain_base_strings(); set_normalization_const(); } -CSNPFeatures::CSNPFeatures(const CSNPFeatures& orig) - : CDotFeatures(orig), strings(orig.strings), +SNPFeatures::SNPFeatures(const SNPFeatures& orig) + : DotFeatures(orig), strings(orig.strings), normalization_const(orig.normalization_const), m_str_min(NULL), m_str_maj(NULL) { - SG_REF(strings); + if (strings) { @@ -72,67 +71,67 @@ CSNPFeatures::CSNPFeatures(const CSNPFeatures& orig) obtain_base_strings(); } -CSNPFeatures::~CSNPFeatures() +SNPFeatures::~SNPFeatures() { - SG_UNREF(strings); + } -int32_t CSNPFeatures::get_dim_feature_space() const +int32_t SNPFeatures::get_dim_feature_space() const { return w_dim; } -int32_t CSNPFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t SNPFeatures::get_nnz_features_for_vector(int32_t num) const { return w_dim/3; } -EFeatureType CSNPFeatures::get_feature_type() const +EFeatureType SNPFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CSNPFeatures::get_feature_class() const +EFeatureClass SNPFeatures::get_feature_class() const { return C_WD; } -int32_t CSNPFeatures::get_num_vectors() const +int32_t SNPFeatures::get_num_vectors() const { return num_strings; } -float64_t CSNPFeatures::get_normalization_const() +float64_t SNPFeatures::get_normalization_const() { return normalization_const; } -void CSNPFeatures::set_minor_base_string(const char* str) +void SNPFeatures::set_minor_base_string(const char* str) { m_str_min=(uint8_t*) get_strdup(str); } -void CSNPFeatures::set_major_base_string(const char* str) +void SNPFeatures::set_major_base_string(const char* str) { m_str_maj=(uint8_t*) get_strdup(str); } -char* CSNPFeatures::get_minor_base_string() +char* SNPFeatures::get_minor_base_string() { return (char*) m_str_min; } -char* CSNPFeatures::get_major_base_string() +char* SNPFeatures::get_major_base_string() { return (char*) m_str_maj; } -float64_t CSNPFeatures::dot(int32_t idx_a, CDotFeatures* df, int32_t idx_b) const +float64_t SNPFeatures::dot(int32_t idx_a, std::shared_ptr df, int32_t idx_b) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CSNPFeatures* sf=(CSNPFeatures*) df; + auto sf=std::static_pointer_cast(df); int32_t alen, blen; bool free_avec, free_bvec; @@ -186,7 +185,7 @@ float64_t CSNPFeatures::dot(int32_t idx_a, CDotFeatures* df, int32_t idx_b) cons } float64_t -CSNPFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +SNPFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.size() == w_dim, "Dimensions don't match, vec2_dim={}, w_dim={}", @@ -226,7 +225,7 @@ CSNPFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return sum/normalization_const; } -void CSNPFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void SNPFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len != w_dim) error("Dimensions don't match, vec2_dim={}, w_dim={}", vec2_len, w_dim); @@ -237,7 +236,7 @@ void CSNPFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t int32_t offs=0; if (abs_val) - alpha=CMath::abs(alpha); + alpha=Math::abs(alpha); for (int32_t i=0; ifree_feature_vector(vec, vec_idx1, free_vec1); } -void CSNPFeatures::find_minor_major_strings(uint8_t* minor, uint8_t* major) +void SNPFeatures::find_minor_major_strings(uint8_t* minor, uint8_t* major) { for (int32_t i=0; i*) strings)->get_feature_vector(i, len, free_vec); + uint8_t* vec = strings->get_feature_vector(i, len, free_vec); ASSERT(string_length==len) for (int32_t j=0; j*) strings)->free_feature_vector(vec, i, free_vec); + strings->free_feature_vector(vec, i, free_vec); } } -void CSNPFeatures::obtain_base_strings(CSNPFeatures* snp) +void SNPFeatures::obtain_base_strings(std::shared_ptr snp) { SG_FREE(m_str_min); SG_FREE(m_str_maj); @@ -313,11 +312,11 @@ void CSNPFeatures::obtain_base_strings(CSNPFeatures* snp) m_str_maj[j]='0'; if (m_str_min[j]>m_str_maj[j]) - CMath::swap(m_str_min[j], m_str_maj[j]); + Math::swap(m_str_min[j], m_str_maj[j]); } } -void CSNPFeatures::set_normalization_const(float64_t n) +void SNPFeatures::set_normalization_const(float64_t n) { if (n==0) { @@ -330,26 +329,26 @@ void CSNPFeatures::set_normalization_const(float64_t n) SG_DEBUG("normalization_const:{}", normalization_const) } -void* CSNPFeatures::get_feature_iterator(int32_t vector_index) +void* SNPFeatures::get_feature_iterator(int32_t vector_index) { return NULL; } -bool CSNPFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool SNPFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { return false; } -void CSNPFeatures::free_feature_iterator(void* iterator) +void SNPFeatures::free_feature_iterator(void* iterator) { } -CFeatures* CSNPFeatures::duplicate() const +std::shared_ptr SNPFeatures::duplicate() const { - return new CSNPFeatures(*this); + return std::make_shared(*this); } -SGMatrix CSNPFeatures::get_histogram(bool normalize) +SGMatrix SNPFeatures::get_histogram(bool normalize) { int32_t nsym=3; float64_t* h= SG_CALLOC(float64_t, size_t(nsym)*string_length/2); @@ -406,7 +405,7 @@ SGMatrix CSNPFeatures::get_histogram(bool normalize) return SGMatrix(h, nsym, string_length/2); } -SGMatrix CSNPFeatures::get_2x3_table(CSNPFeatures* pos, CSNPFeatures* neg) +SGMatrix SNPFeatures::get_2x3_table(std::shared_ptr pos, std::shared_ptr neg) { ASSERT(pos->strings->get_max_vector_length() == neg->strings->get_max_vector_length()) diff --git a/src/shogun/features/SNPFeatures.h b/src/shogun/features/SNPFeatures.h index cd3c1b4a0c5..196488cdd3c 100644 --- a/src/shogun/features/SNPFeatures.h +++ b/src/shogun/features/SNPFeatures.h @@ -16,30 +16,30 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /** @brief Features that compute the Weighted Degreee Kernel feature space * explicitly. * * \sa CWeightedDegreeStringKernel */ -class CSNPFeatures : public CDotFeatures +class SNPFeatures : public DotFeatures { public: /** default constructor */ - CSNPFeatures(); + SNPFeatures(); /** constructor * * @param str stringfeatures (of bytes) */ - CSNPFeatures(CStringFeatures* str); + SNPFeatures(std::shared_ptr> str); /** copy constructor */ - CSNPFeatures(const CSNPFeatures & orig); + SNPFeatures(const SNPFeatures & orig); /** destructor */ - virtual ~CSNPFeatures(); + virtual ~SNPFeatures(); /** obtain the dimensionality of the feature space * @@ -57,7 +57,7 @@ class CSNPFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -119,7 +119,7 @@ class CSNPFeatures : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -175,7 +175,7 @@ class CSNPFeatures : public CDotFeatures * * @param snp optionally compute base string for snp too */ - void obtain_base_strings(CSNPFeatures* snp=NULL); + void obtain_base_strings(std::shared_ptr snp=NULL); /** @return object name */ virtual const char* get_name() const { return "SNPFeatures"; } @@ -187,7 +187,7 @@ class CSNPFeatures : public CDotFeatures /** * compute 2x3 histogram table */ - static SGMatrix get_2x3_table(CSNPFeatures* pos, CSNPFeatures* neg); + static SGMatrix get_2x3_table(std::shared_ptr pos, std::shared_ptr neg); private: /** determine minor and major base strings from current strings @@ -200,7 +200,7 @@ class CSNPFeatures : public CDotFeatures protected: /** stringfeatures the wdfeatures are based on*/ - CStringFeatures* strings; + std::shared_ptr> strings; /** length of string in vector */ int32_t string_length; diff --git a/src/shogun/features/SparseFeatures.cpp b/src/shogun/features/SparseFeatures.cpp index ed47b1e2b93..bd877d497c0 100644 --- a/src/shogun/features/SparseFeatures.cpp +++ b/src/shogun/features/SparseFeatures.cpp @@ -11,75 +11,69 @@ namespace shogun { -template class CSparsePreprocessor; +template class SparsePreprocessor; -template CSparseFeatures::CSparseFeatures(int32_t size) -: CDotFeatures(size), feature_cache(NULL) +template SparseFeatures::SparseFeatures(int32_t size) +: DotFeatures(size), feature_cache(NULL) { init(); } -template CSparseFeatures::CSparseFeatures(SGSparseMatrix sparse) -: CDotFeatures(0), feature_cache(NULL) +template SparseFeatures::SparseFeatures(SGSparseMatrix sparse) +: SparseFeatures(0) { - init(); - set_sparse_feature_matrix(sparse); } -template CSparseFeatures::CSparseFeatures(SGMatrix dense) -: CDotFeatures(0), feature_cache(NULL) +template SparseFeatures::SparseFeatures(SGMatrix dense) +: SparseFeatures(0) { - init(); - set_full_feature_matrix(dense); } -template CSparseFeatures::CSparseFeatures(const CSparseFeatures & orig) -: CDotFeatures(orig), sparse_feature_matrix(orig.sparse_feature_matrix), +template SparseFeatures::SparseFeatures(const SparseFeatures & orig) +: DotFeatures(orig), sparse_feature_matrix(orig.sparse_feature_matrix), feature_cache(orig.feature_cache) { init(); m_subset_stack=orig.m_subset_stack; - SG_REF(m_subset_stack); + } template -CSparseFeatures::CSparseFeatures(CDenseFeatures* dense) - : CDotFeatures(0), feature_cache(NULL) +SparseFeatures::SparseFeatures(std::shared_ptr> dense) + : SparseFeatures(0) { - init(); - SGMatrix fm=dense->get_feature_matrix(); ASSERT(fm.matrix && fm.num_cols>0 && fm.num_rows>0) set_full_feature_matrix(fm); } -template<> CSparseFeatures::CSparseFeatures(CDenseFeatures* dense) +template<> SparseFeatures::SparseFeatures(std::shared_ptr> dense) { not_implemented(SOURCE_LOCATION);; } -template CSparseFeatures::CSparseFeatures(CFile* loader) -: CDotFeatures(), feature_cache(NULL) +template SparseFeatures::SparseFeatures(std::shared_ptr loader) +: DotFeatures(), feature_cache(NULL) { init(); load(loader); } -template CSparseFeatures::~CSparseFeatures() +template SparseFeatures::~SparseFeatures() { - SG_UNREF(feature_cache); + } -template CFeatures* CSparseFeatures::duplicate() const +template std::shared_ptr SparseFeatures::duplicate() const { - return new CSparseFeatures(*this); + return std::make_shared(*this); } -template ST CSparseFeatures::get_feature(int32_t num, int32_t index) const +template ST SparseFeatures::get_feature(int32_t num, int32_t index) const { require(index>=0 && index ST CSparseFeatures::get_feature(int32_t num, int32_t inde return ret; } -template SGVector CSparseFeatures::get_full_feature_vector(int32_t num) +template SGVector SparseFeatures::get_full_feature_vector(int32_t num) { SGSparseVector sv=get_sparse_feature_vector(num); SGVector dense = sv.get_dense(get_num_features()); @@ -100,7 +94,7 @@ template SGVector CSparseFeatures::get_full_feature_vector(int return dense; } -template int32_t CSparseFeatures::get_nnz_features_for_vector(int32_t num) const +template int32_t SparseFeatures::get_nnz_features_for_vector(int32_t num) const { SGSparseVector sv = get_sparse_feature_vector(num); int32_t len=sv.num_feat_entries; @@ -108,7 +102,7 @@ template int32_t CSparseFeatures::get_nnz_features_for_vector(int3 return len; } -template SGSparseVector CSparseFeatures::get_sparse_feature_vector(int32_t num) const +template SGSparseVector SparseFeatures::get_sparse_feature_vector(int32_t num) const { require(num>=0 && num SGSparseVector CSparseFeatures::get_sparse_feature_ve for (int32_t i=0; i*) get_preproc(i))->apply_to_feature_vector(tmp_feat_before, tmp_len); + //tmp_feat_after=((SparsePreprocessor*) get_preproc(i))->apply_to_feature_vector(tmp_feat_before, tmp_len); if (i!=0) // delete feature vector, except for the the first one, i.e., feat SG_FREE(tmp_feat_before); @@ -170,7 +164,7 @@ template SGSparseVector CSparseFeatures::get_sparse_feature_ve } } -template ST CSparseFeatures::dense_dot(ST alpha, int32_t num, ST* vec, int32_t dim, ST b) const +template ST SparseFeatures::dense_dot(ST alpha, int32_t num, ST* vec, int32_t dim, ST b) const { SGSparseVector sv=get_sparse_feature_vector(num); ST result = sv.dense_dot(alpha,vec,dim,b); @@ -178,7 +172,7 @@ template ST CSparseFeatures::dense_dot(ST alpha, int32_t num, ST* return result; } -template void CSparseFeatures::add_to_dense_vec(float64_t alpha, int32_t num, float64_t* vec, int32_t dim, bool abs_val) const +template void SparseFeatures::add_to_dense_vec(float64_t alpha, int32_t num, float64_t* vec, int32_t dim, bool abs_val) const { require(vec, "add_to_dense_vec(num={},dim={}): vec must not be NULL", num, dim); @@ -195,7 +189,7 @@ template void CSparseFeatures::add_to_dense_vec(float64_t alpha, i for (int32_t i=0; i void CSparseFeatures::add_to_dense_vec(float64_t alpha, i } template<> -void CSparseFeatures::add_to_dense_vec(float64_t alpha, +void SparseFeatures::add_to_dense_vec(float64_t alpha, int32_t num, float64_t* vec, int32_t dim, bool abs_val) const { not_implemented(SOURCE_LOCATION);; } -template void CSparseFeatures::free_sparse_feature_vector(int32_t num) const +template void SparseFeatures::free_sparse_feature_vector(int32_t num) const { if (feature_cache) feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num)); @@ -226,7 +220,7 @@ template void CSparseFeatures::free_sparse_feature_vector(int32_t //vec.free_vector(); } -template SGSparseMatrix CSparseFeatures::get_sparse_feature_matrix() +template SGSparseMatrix SparseFeatures::get_sparse_feature_matrix() { if (m_subset_stack->has_subsets()) error("Not allowed with subset"); @@ -234,15 +228,15 @@ template SGSparseMatrix CSparseFeatures::get_sparse_feature_ma return sparse_feature_matrix; } -template CSparseFeatures* CSparseFeatures::get_transposed() +template std::shared_ptr> SparseFeatures::get_transposed() { if (m_subset_stack->has_subsets()) error("Not allowed with subset"); - return new CSparseFeatures(sparse_feature_matrix.get_transposed()); + return std::make_shared(sparse_feature_matrix.get_transposed()); } -template void CSparseFeatures::set_sparse_feature_matrix(SGSparseMatrix sm) +template void SparseFeatures::set_sparse_feature_matrix(SGSparseMatrix sm) { if (m_subset_stack->has_subsets()) error("Not allowed with subset"); @@ -258,7 +252,7 @@ template void CSparseFeatures::set_sparse_feature_matrix(SGSparseM } } -template SGMatrix CSparseFeatures::get_full_feature_matrix() +template SGMatrix SparseFeatures::get_full_feature_matrix() { SGMatrix full(get_num_features(), get_num_vectors()); full.zero(); @@ -283,35 +277,35 @@ template SGMatrix CSparseFeatures::get_full_feature_matrix() return full; } -template void CSparseFeatures::free_sparse_features() +template void SparseFeatures::free_sparse_features() { free_sparse_feature_matrix(); - SG_UNREF(feature_cache); + } -template void CSparseFeatures::free_sparse_feature_matrix() +template void SparseFeatures::free_sparse_feature_matrix() { sparse_feature_matrix=SGSparseMatrix(); } -template void CSparseFeatures::set_full_feature_matrix(SGMatrix full) +template void SparseFeatures::set_full_feature_matrix(SGMatrix full) { remove_all_subsets(); free_sparse_feature_matrix(); sparse_feature_matrix.from_dense(full); } -template int32_t CSparseFeatures::get_num_vectors() const +template int32_t SparseFeatures::get_num_vectors() const { return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : sparse_feature_matrix.num_vectors; } -template int32_t CSparseFeatures::get_num_features() const +template int32_t SparseFeatures::get_num_features() const { return sparse_feature_matrix.num_features; } -template int32_t CSparseFeatures::set_num_features(int32_t num) +template int32_t SparseFeatures::set_num_features(int32_t num) { int32_t n=get_num_features(); ASSERT(n<=num) @@ -319,12 +313,12 @@ template int32_t CSparseFeatures::set_num_features(int32_t num) return sparse_feature_matrix.num_features; } -template EFeatureClass CSparseFeatures::get_feature_class() const +template EFeatureClass SparseFeatures::get_feature_class() const { return C_SPARSE; } -template void CSparseFeatures::free_feature_vector(int32_t num) const +template void SparseFeatures::free_feature_vector(int32_t num) const { if (feature_cache) feature_cache->unlock_entry(m_subset_stack->subset_idx_conversion(num)); @@ -332,7 +326,7 @@ template void CSparseFeatures::free_feature_vector(int32_t num) co //vec.free_vector(); } -template int64_t CSparseFeatures::get_num_nonzero_entries() +template int64_t SparseFeatures::get_num_nonzero_entries() { int64_t num=0; index_t num_vec=get_num_vectors(); @@ -342,7 +336,7 @@ template int64_t CSparseFeatures::get_num_nonzero_entries() return num; } -template float64_t* CSparseFeatures::compute_squared(float64_t* sq) +template float64_t* SparseFeatures::compute_squared(float64_t* sq) { ASSERT(sq) @@ -361,15 +355,15 @@ template float64_t* CSparseFeatures::compute_squared(float64_t* sq return sq; } -template<> float64_t* CSparseFeatures::compute_squared(float64_t* sq) +template<> float64_t* SparseFeatures::compute_squared(float64_t* sq) { not_implemented(SOURCE_LOCATION);; return sq; } -template float64_t CSparseFeatures::compute_squared_norm( - CSparseFeatures* lhs, float64_t* sq_lhs, int32_t idx_a, - CSparseFeatures* rhs, float64_t* sq_rhs, int32_t idx_b) +template float64_t SparseFeatures::compute_squared_norm( + std::shared_ptr> lhs, float64_t* sq_lhs, int32_t idx_a, + std::shared_ptr> rhs, float64_t* sq_rhs, int32_t idx_b) { int32_t i,j; ASSERT(lhs) @@ -421,24 +415,24 @@ template float64_t CSparseFeatures::compute_squared_norm( } } - ((CSparseFeatures*) lhs)->free_feature_vector(idx_a); - ((CSparseFeatures*) rhs)->free_feature_vector(idx_b); + lhs->free_feature_vector(idx_a); + rhs->free_feature_vector(idx_b); - return CMath::abs(result); + return Math::abs(result); } -template int32_t CSparseFeatures::get_dim_feature_space() const +template int32_t SparseFeatures::get_dim_feature_space() const { return get_num_features(); } -template float64_t CSparseFeatures::dot(int32_t vec_idx1, - CDotFeatures* df, int32_t vec_idx2) const +template float64_t SparseFeatures::dot(int32_t vec_idx1, + std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CSparseFeatures* sf = (CSparseFeatures*) df; + auto sf = std::dynamic_pointer_cast>(df); SGSparseVector avec=get_sparse_feature_vector(vec_idx1); SGSparseVector bvec=sf->get_sparse_feature_vector(vec_idx2); @@ -450,8 +444,8 @@ template float64_t CSparseFeatures::dot(int32_t vec_idx1, return result; } -template<> float64_t CSparseFeatures::dot(int32_t vec_idx1, - CDotFeatures* df, int32_t vec_idx2) const +template<> float64_t SparseFeatures::dot(int32_t vec_idx1, + std::shared_ptr df, int32_t vec_idx2) const { not_implemented(SOURCE_LOCATION);; return 0.0; @@ -459,7 +453,7 @@ template<> float64_t CSparseFeatures::dot(int32_t vec_idx1, template float64_t -CSparseFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +SparseFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.size() >= get_num_features(), @@ -492,14 +486,14 @@ CSparseFeatures::dot(int32_t vec_idx1, const SGVector& vec2) cons } template <> -float64_t CSparseFeatures::dot( +float64_t SparseFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { not_implemented(SOURCE_LOCATION);; return 0.0; } -template void* CSparseFeatures::get_feature_iterator(int32_t vector_index) +template void* SparseFeatures::get_feature_iterator(int32_t vector_index) { if (vector_index>=get_num_vectors()) { @@ -518,7 +512,7 @@ template void* CSparseFeatures::get_feature_iterator(int32_t vecto return it; } -template bool CSparseFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +template bool SparseFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { sparse_feature_iterator* it=(sparse_feature_iterator*) iterator; if (!it || it->index>=it->sv.num_feat_entries) @@ -532,14 +526,14 @@ template bool CSparseFeatures::get_next_feature(int32_t& index, fl return true; } -template<> bool CSparseFeatures::get_next_feature(int32_t& index, +template<> bool SparseFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION);; return false; } -template void CSparseFeatures::free_feature_iterator(void* iterator) +template void SparseFeatures::free_feature_iterator(void* iterator) { if (!iterator) return; @@ -547,7 +541,7 @@ template void CSparseFeatures::free_feature_iterator(void* iterato delete ((sparse_feature_iterator*) iterator); } -template CFeatures* CSparseFeatures::copy_subset(SGVector indices) const +template std::shared_ptr SparseFeatures::copy_subset(SGVector indices ) const { SGSparseMatrix matrix_copy=SGSparseMatrix(get_dim_feature_space(), indices.vlen); @@ -565,11 +559,10 @@ template CFeatures* CSparseFeatures::copy_subset(SGVector free_sparse_feature_vector(index); } - CFeatures* result=new CSparseFeatures(matrix_copy); - return result; + return std::make_shared(matrix_copy); } -template SGSparseVectorEntry* CSparseFeatures::compute_sparse_feature_vector(int32_t num, +template SGSparseVectorEntry* SparseFeatures::compute_sparse_feature_vector(int32_t num, int32_t& len, SGSparseVectorEntry* target) const { not_implemented(SOURCE_LOCATION); @@ -578,29 +571,29 @@ template SGSparseVectorEntry* CSparseFeatures::compute_sparse_ return NULL; } -template void CSparseFeatures::sort_features() +template void SparseFeatures::sort_features() { sparse_feature_matrix.sort_features(); } -template void CSparseFeatures::init() +template void SparseFeatures::init() { set_generic(); - m_parameters->add_vector(&sparse_feature_matrix.sparse_matrix, &sparse_feature_matrix.num_vectors, + /*m_parameters->add_vector(&sparse_feature_matrix.sparse_matrix, &sparse_feature_matrix.num_vectors, "sparse_feature_matrix", - "Array of sparse vectors."); + "Array of sparse vectors.");*/ watch_param( "sparse_feature_matrix", &sparse_feature_matrix.sparse_matrix, &sparse_feature_matrix.num_vectors); watch_param("sparse_feature_matrix.num_features", &sparse_feature_matrix.num_features); - m_parameters->add(&sparse_feature_matrix.num_features, "sparse_feature_matrix.num_features", - "Total number of features."); + /*m_parameters->add(&sparse_feature_matrix.num_features, "sparse_feature_matrix.num_features", + "Total number of features.");*/ } #define GET_FEATURE_TYPE(sg_type, f_type) \ -template<> EFeatureType CSparseFeatures::get_feature_type() const \ +template<> EFeatureType SparseFeatures::get_feature_type() const \ { \ return f_type; \ } @@ -620,7 +613,7 @@ GET_FEATURE_TYPE(floatmax_t, F_LONGREAL) GET_FEATURE_TYPE(complex128_t, F_ANY) #undef GET_FEATURE_TYPE -template void CSparseFeatures::load(CFile* loader) +template void SparseFeatures::load(std::shared_ptr loader) { remove_all_subsets(); ASSERT(loader) @@ -628,7 +621,7 @@ template void CSparseFeatures::load(CFile* loader) sparse_feature_matrix.load(loader); } -template SGVector CSparseFeatures::load_with_labels(CLibSVMFile* loader) +template SGVector SparseFeatures::load_with_labels(std::shared_ptr loader) { remove_all_subsets(); ASSERT(loader) @@ -636,7 +629,7 @@ template SGVector CSparseFeatures::load_with_labels(CLi return sparse_feature_matrix.load_with_labels(loader); } -template void CSparseFeatures::save(CFile* writer) +template void SparseFeatures::save(std::shared_ptr writer) { if (m_subset_stack->has_subsets()) error("Not allowed with subset"); @@ -644,7 +637,7 @@ template void CSparseFeatures::save(CFile* writer) sparse_feature_matrix.save(writer); } -template void CSparseFeatures::save_with_labels(CLibSVMFile* writer, SGVector labels) +template void SparseFeatures::save_with_labels(std::shared_ptr writer, SGVector labels) { if (m_subset_stack->has_subsets()) error("Not allowed with subset"); @@ -652,18 +645,18 @@ template void CSparseFeatures::save_with_labels(CLibSVMFile* write sparse_feature_matrix.save_with_labels(writer, labels); } -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; -template class CSparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; +template class SparseFeatures; } diff --git a/src/shogun/features/SparseFeatures.h b/src/shogun/features/SparseFeatures.h index bfebd0cd305..a90655f7fd9 100644 --- a/src/shogun/features/SparseFeatures.h +++ b/src/shogun/features/SparseFeatures.h @@ -22,11 +22,11 @@ namespace shogun { -class CFile; -class CLibSVMFile; -class CFeatures; -template class CDenseFeatures; -template class CCache; +class File; +class LibSVMFile; +class Features; +template class DenseFeatures; +template class Cache; /** @brief Template class SparseFeatures implements sparse matrices. * @@ -44,45 +44,45 @@ template class CCache; * Simple use the (inherited) add_subset(), remove_subset() functions. * If done, all calls that work with features are translated to the subset. * See comments to find out whether it is supported for that method. - * See also CFeatures class documentation + * See also Features class documentation */ -template class CSparseFeatures : public CDotFeatures +template class SparseFeatures : public DotFeatures { public: /** constructor * * @param size cache size */ - CSparseFeatures(int32_t size=0); + SparseFeatures(int32_t size=0); /** convenience constructor that creates sparse features from * sparse features * * @param sparse sparse matrix */ - CSparseFeatures(SGSparseMatrix sparse); + SparseFeatures(SGSparseMatrix sparse); /** convenience constructor that creates sparse features from * dense features * * @param dense dense feature matrix */ - CSparseFeatures(SGMatrix dense); + SparseFeatures(SGMatrix dense); /** copy constructor */ - CSparseFeatures(const CSparseFeatures & orig); + SparseFeatures(const SparseFeatures & orig); /** copy constructor from DenseFeatures */ - CSparseFeatures(CDenseFeatures* dense); + SparseFeatures(std::shared_ptr> dense); /** constructor loading features from file * * @param loader File object to load data from */ - CSparseFeatures(CFile* loader); + SparseFeatures(std::shared_ptr loader); /** default destructor */ - virtual ~CSparseFeatures(); + virtual ~SparseFeatures(); /** free sparse feature matrix * @@ -100,7 +100,7 @@ template class CSparseFeatures : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get a single feature * @@ -200,7 +200,7 @@ template class CSparseFeatures : public CDotFeatures * * @return transposed copy */ - CSparseFeatures* get_transposed(); + std::shared_ptr> get_transposed(); /** compute and return the transpose of the sparse feature matrix * which will be prepocessed. @@ -317,9 +317,9 @@ template class CSparseFeatures : public CDotFeatures * @param sq_rhs squared values of right-hand side * @param idx_b index of right-hand side's vector to compute */ - float64_t compute_squared_norm(CSparseFeatures* lhs, + float64_t compute_squared_norm(std::shared_ptr> lhs, float64_t* sq_lhs, int32_t idx_a, - CSparseFeatures* rhs, float64_t* sq_rhs, + std::shared_ptr> rhs, float64_t* sq_rhs, int32_t idx_b); /** load features from file @@ -328,7 +328,7 @@ template class CSparseFeatures : public CDotFeatures * * @param loader File object to load data from */ - void load(CFile* loader); + void load(std::shared_ptr loader); /** load features from file * @@ -337,7 +337,7 @@ template class CSparseFeatures : public CDotFeatures * @param loader File object to load data from * @return label vector */ - SGVector load_with_labels(CLibSVMFile* loader); + SGVector load_with_labels(std::shared_ptr loader); /** save features to file * @@ -345,7 +345,7 @@ template class CSparseFeatures : public CDotFeatures * * @param writer File object to write data to */ - void save(CFile* writer); + void save(std::shared_ptr writer); /** save features to file * @@ -354,7 +354,7 @@ template class CSparseFeatures : public CDotFeatures * @param writer File object to write data to * @param labels vector with labels to write out */ - void save_with_labels(CLibSVMFile* writer, SGVector labels); + void save_with_labels(std::shared_ptr writer, SGVector labels); /** ensure that features occur in ascending order, only call when no * preprocessors are attached @@ -381,7 +381,7 @@ template class CSparseFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -391,7 +391,7 @@ template class CSparseFeatures : public CDotFeatures * @param vec2 dense vector */ virtual float64_t - dot(int32_t vec_idx1, const SGVector& vec2) const override; + dot(int32_t vec_idx1, const SGVector& vec2) const; #ifndef DOXYGEN_SHOULD_SKIP_THIS /** iterator for sparse features */ @@ -447,13 +447,13 @@ template class CSparseFeatures : public CDotFeatures */ virtual void free_feature_iterator(void* iterator); - /** Creates a new CFeatures instance containing copies of the elements + /** Creates a new Features instance containing copies of the elements * which are specified by the provided indices. * * @param indices indices of feature elements to copy - * @return new CFeatures instance with copies of feature data + * @return new Features instance with copies of feature data */ - virtual CFeatures* copy_subset(SGVector indices) const; + virtual std::shared_ptr copy_subset(SGVector indices) const; /** @return object name */ virtual const char* get_name() const { return "SparseFeatures"; } @@ -481,7 +481,7 @@ template class CSparseFeatures : public CDotFeatures SGSparseMatrix sparse_feature_matrix; /** feature cache */ - CCache< SGSparseVectorEntry >* feature_cache; + std::shared_ptr >> feature_cache; }; } #endif /* _SPARSEFEATURES__H__ */ diff --git a/src/shogun/features/SparsePolyFeatures.cpp b/src/shogun/features/SparsePolyFeatures.cpp index f047c31bff6..bd3d88466c5 100644 --- a/src/shogun/features/SparsePolyFeatures.cpp +++ b/src/shogun/features/SparsePolyFeatures.cpp @@ -10,7 +10,7 @@ using namespace shogun; -CSparsePolyFeatures::CSparsePolyFeatures() +SparsePolyFeatures::SparsePolyFeatures(): DotFeatures() { unstable(SOURCE_LOCATION); @@ -24,13 +24,13 @@ CSparsePolyFeatures::CSparsePolyFeatures() m_hash_bits = 0; } -CSparsePolyFeatures::CSparsePolyFeatures(CSparseFeatures* feat, int32_t degree, bool normalize, int32_t hash_bits) - : CDotFeatures(), m_normalization_values(NULL) +SparsePolyFeatures::SparsePolyFeatures(std::shared_ptr> feat, int32_t degree, bool normalize, int32_t hash_bits) + : DotFeatures(), m_normalization_values(NULL) { ASSERT(feat) m_feat = feat; - SG_REF(m_feat); + m_degree=degree; m_normalize=normalize; m_hash_bits=hash_bits; @@ -42,24 +42,24 @@ CSparsePolyFeatures::CSparsePolyFeatures(CSparseFeatures* feat, int32 store_normalization_values(); } -CSparsePolyFeatures::~CSparsePolyFeatures() +SparsePolyFeatures::~SparsePolyFeatures() { SG_FREE(m_normalization_values); - SG_UNREF(m_feat); + } -CSparsePolyFeatures::CSparsePolyFeatures(const CSparsePolyFeatures & orig) +SparsePolyFeatures::SparsePolyFeatures(const SparsePolyFeatures & orig) { io::print("CSparsePolyFeatures:\n"); not_implemented(SOURCE_LOCATION); } -int32_t CSparsePolyFeatures::get_dim_feature_space() const +int32_t SparsePolyFeatures::get_dim_feature_space() const { return m_output_dimensions; } -int32_t CSparsePolyFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t SparsePolyFeatures::get_nnz_features_for_vector(int32_t num) const { int32_t vlen; SGSparseVector vec=m_feat->get_sparse_feature_vector(num); @@ -68,17 +68,17 @@ int32_t CSparsePolyFeatures::get_nnz_features_for_vector(int32_t num) const return vlen*(vlen+1)/2; } -EFeatureType CSparsePolyFeatures::get_feature_type() const +EFeatureType SparsePolyFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CSparsePolyFeatures::get_feature_class() const +EFeatureClass SparsePolyFeatures::get_feature_class() const { return C_POLY; } -int32_t CSparsePolyFeatures::get_num_vectors() const +int32_t SparsePolyFeatures::get_num_vectors() const { if (m_feat) return m_feat->get_num_vectors(); @@ -87,37 +87,37 @@ int32_t CSparsePolyFeatures::get_num_vectors() const } -void* CSparsePolyFeatures::get_feature_iterator(int32_t vector_index) +void* SparsePolyFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CSparsePolyFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool SparsePolyFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CSparsePolyFeatures::free_feature_iterator(void* iterator) +void SparsePolyFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } -float64_t CSparsePolyFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t SparsePolyFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CSparsePolyFeatures* pf=(CSparsePolyFeatures*) df; + auto pf=std::static_pointer_cast(df); SGSparseVector vec1=m_feat->get_sparse_feature_vector(vec_idx1); SGSparseVector vec2=pf->m_feat->get_sparse_feature_vector( vec_idx2); float64_t result=SGSparseVector::sparse_dot(vec1, vec2); - result=CMath::pow(result, m_degree); + result=Math::pow(result, m_degree); m_feat->free_feature_vector(vec_idx1); pf->m_feat->free_feature_vector(vec_idx2); @@ -126,7 +126,7 @@ float64_t CSparsePolyFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t v } float64_t -CSparsePolyFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +SparsePolyFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.vlen == m_output_dimensions, @@ -145,14 +145,14 @@ CSparsePolyFeatures::dot(int32_t vec_idx1, const SGVector& vec2) cons for (int32_t i=0; i& vec2) cons return result; } -void CSparsePolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void SparsePolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len!=m_output_dimensions) error("Dimensions don't match, vec2_dim={}, m_output_dimensions={}", vec2_len, m_output_dimensions); @@ -195,14 +195,14 @@ void CSparsePolyFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, fl for (int32_t i=0; ifree_feature_vector(vec_idx1); } -void CSparsePolyFeatures::store_normalization_values() +void SparsePolyFeatures::store_normalization_values() { SG_FREE(m_normalization_values); @@ -234,7 +234,8 @@ void CSparsePolyFeatures::store_normalization_values() m_normalization_values=SG_MALLOC(float64_t, m_normalization_values_len); for (int i=0; i>(shared_from_this()), i)); if (val==0) // trap division by zero m_normalization_values[i]=1.0; @@ -244,12 +245,12 @@ void CSparsePolyFeatures::store_normalization_values() } -CFeatures* CSparsePolyFeatures::duplicate() const +std::shared_ptr SparsePolyFeatures::duplicate() const { - return new CSparsePolyFeatures(*this); + return std::make_shared(*this); } -void CSparsePolyFeatures::init() +void SparsePolyFeatures::init() { SG_ADD( &m_feat, "features", "Features in original space."); @@ -264,8 +265,8 @@ void CSparsePolyFeatures::init() "Dimensions of the feature space of the polynomial kernel."); m_normalization_values_len = get_num_vectors(); - m_parameters->add_vector(&m_normalization_values, &m_normalization_values_len, - "m_normalization_values", "Norm of each training example"); + /*m_parameters->add_vector(&m_normalization_values, &m_normalization_values_len, + "m_normalization_values", "Norm of each training example");*/ watch_param( "m_normalization_values", &m_normalization_values, &m_normalization_values_len); diff --git a/src/shogun/features/SparsePolyFeatures.h b/src/shogun/features/SparsePolyFeatures.h index 4191710feca..ac97987d7c1 100644 --- a/src/shogun/features/SparsePolyFeatures.h +++ b/src/shogun/features/SparsePolyFeatures.h @@ -21,11 +21,11 @@ namespace shogun * see DotFeatures for further discription * */ -class CSparsePolyFeatures : public CDotFeatures +class SparsePolyFeatures : public DotFeatures { public: /** default constructor */ - CSparsePolyFeatures(); + SparsePolyFeatures(); /** constructor * @@ -35,9 +35,9 @@ class CSparsePolyFeatures : public CDotFeatures * @param normalize normalize kernel * @param hash_bits number of bits in hashd feature space */ - CSparsePolyFeatures(CSparseFeatures* feat, int32_t degree, bool normalize, int32_t hash_bits); + SparsePolyFeatures(std::shared_ptr> feat, int32_t degree, bool normalize, int32_t hash_bits); - virtual ~CSparsePolyFeatures(); + virtual ~SparsePolyFeatures(); /** copy constructor * @@ -45,7 +45,7 @@ class CSparsePolyFeatures : public CDotFeatures * * @param orig original PolyFeature */ - CSparsePolyFeatures(const CSparsePolyFeatures & orig); + SparsePolyFeatures(const SparsePolyFeatures & orig); /** get dimensions of feature space * @@ -85,7 +85,7 @@ class CSparsePolyFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; #ifndef DOXYGEN_SHOULD_SKIP_THIS /** iterator for weighted spectrum features */ @@ -139,7 +139,7 @@ class CSparsePolyFeatures : public CDotFeatures * * @return feature object */ - CFeatures* duplicate() const; + std::shared_ptr duplicate() const; /** * @@ -171,7 +171,7 @@ class CSparsePolyFeatures : public CDotFeatures protected: /** features in original space*/ - CSparseFeatures* m_feat; + std::shared_ptr> m_feat; /** degree of the polynomial kernel */ int32_t m_degree; /** normalize */ diff --git a/src/shogun/features/StringFeatures.cpp b/src/shogun/features/StringFeatures.cpp index 2c8a508bb6c..41fa4e1dc71 100644 --- a/src/shogun/features/StringFeatures.cpp +++ b/src/shogun/features/StringFeatures.cpp @@ -29,62 +29,58 @@ namespace shogun { -template CStringFeatures::CStringFeatures() : CFeatures(0) +template StringFeatures::StringFeatures() : Features(0) { init(); - alphabet=new CAlphabet(); - SG_REF(alphabet); + alphabet=std::make_shared(); + } -template CStringFeatures::CStringFeatures(EAlphabet alpha) : CFeatures(0) +template StringFeatures::StringFeatures(EAlphabet alpha) : Features(0) { init(); - alphabet=new CAlphabet(alpha); - SG_REF(alphabet); + alphabet=std::make_shared(alpha); + num_symbols=alphabet->get_num_symbols(); original_num_symbols=num_symbols; } -template CStringFeatures::CStringFeatures(const std::vector>& string_list, EAlphabet alpha) -: CStringFeatures(alpha) +template StringFeatures::StringFeatures(const std::vector>& string_list, EAlphabet alpha) +: StringFeatures(alpha) { set_features(string_list); } -template CStringFeatures::CStringFeatures(const std::vector>& string_list, CAlphabet* alpha) -: CStringFeatures(alpha) +template StringFeatures::StringFeatures(const std::vector>& string_list, std::shared_ptr alpha) +: StringFeatures(alpha) { set_features(string_list); } -template CStringFeatures::CStringFeatures(CAlphabet* alpha) -: CFeatures(0) +template StringFeatures::StringFeatures(std::shared_ptr alpha) +: Features(0) { ASSERT(alpha) init(); - SG_REF(alpha); - SG_UNREF(alphabet); alphabet=alpha; num_symbols=alphabet->get_num_symbols(); original_num_symbols=num_symbols; } -template CStringFeatures::CStringFeatures(CFile* loader, EAlphabet alpha) -: CStringFeatures(alpha) +template StringFeatures::StringFeatures(std::shared_ptr loader, EAlphabet alpha) +: StringFeatures(alpha) { load(loader); } -template CStringFeatures::~CStringFeatures() +template StringFeatures::~StringFeatures() { cleanup(); - - SG_UNREF(alphabet); } -template void CStringFeatures::cleanup() +template void StringFeatures::cleanup() { remove_all_subsets(); @@ -110,13 +106,13 @@ template void CStringFeatures::cleanup() * create a new object (to leave the alphabet object alone if it is used * by others) */ - CAlphabet* alpha=new CAlphabet(alphabet->get_alphabet()); - SG_UNREF(alphabet); + auto alpha=std::make_shared(alphabet->get_alphabet()); + alphabet=alpha; - SG_REF(alphabet); + } -template void CStringFeatures::cleanup_feature_vector(int32_t num) +template void StringFeatures::cleanup_feature_vector(int32_t num) { ASSERT(num void CStringFeatures::cleanup_feature_vector(int32_t num) features[real_num] = SGVector(); } -template void CStringFeatures::cleanup_feature_vectors(int32_t start, int32_t stop) +template void StringFeatures::cleanup_feature_vectors(int32_t start, int32_t stop) { if(get_num_vectors()) { @@ -139,22 +135,22 @@ template void CStringFeatures::cleanup_feature_vectors(int32_t sta } } -template EFeatureClass CStringFeatures::get_feature_class() const { return C_STRING; } +template EFeatureClass StringFeatures::get_feature_class() const { return C_STRING; } -template EFeatureType CStringFeatures::get_feature_type() const { return F_UNKNOWN; } +template EFeatureType StringFeatures::get_feature_type() const { return F_UNKNOWN; } -template CAlphabet* CStringFeatures::get_alphabet() const +template std::shared_ptr StringFeatures::get_alphabet() const { - SG_REF(alphabet); + return alphabet; } -template CFeatures* CStringFeatures::duplicate() const +template std::shared_ptr StringFeatures::duplicate() const { - return new CStringFeatures(*this); + return std::make_shared(*this); } -template SGVector CStringFeatures::get_feature_vector(int32_t num) +template SGVector StringFeatures::get_feature_vector(int32_t num) { if (num>=get_num_vectors()) { @@ -171,7 +167,7 @@ template SGVector CStringFeatures::get_feature_vector(int32_t return SGVector(dst, l, true); } -template void CStringFeatures::set_feature_vector(SGVector vector, int32_t num) +template void StringFeatures::set_feature_vector(SGVector vector, int32_t num) { if (m_subset_stack->has_subsets()) error("A subset is set, cannot set feature vector"); @@ -188,17 +184,17 @@ template void CStringFeatures::set_feature_vector(SGVector vec features[num] = vector.clone(); } -template void CStringFeatures::enable_on_the_fly_preprocessing() +template void StringFeatures::enable_on_the_fly_preprocessing() { preprocess_on_get=true; } -template void CStringFeatures::disable_on_the_fly_preprocessing() +template void StringFeatures::disable_on_the_fly_preprocessing() { preprocess_on_get=false; } -template ST* CStringFeatures::get_feature_vector(int32_t num, int32_t& len, bool& dofree) +template ST* StringFeatures::get_feature_vector(int32_t num, int32_t& len, bool& dofree) { if (num>=get_num_vectors()) error("Requested feature vector with index {} while total num is {}", num, get_num_vectors()); @@ -223,9 +219,8 @@ template ST* CStringFeatures::get_feature_vector(int32_t num, int3 for (int32_t i=0; i* p=(CStringPreprocessor*) get_preprocessor(i); + auto p=std::dynamic_pointer_cast>(get_preprocessor(i)); feat=p->apply_to_string(tmp_feat_before, len); - SG_UNREF(p); SG_FREE(tmp_feat_before); tmp_feat_before=feat; } @@ -235,12 +230,12 @@ template ST* CStringFeatures::get_feature_vector(int32_t num, int3 } } -template CStringFeatures* CStringFeatures::get_transposed() +template std::shared_ptr> StringFeatures::get_transposed() { - return new CStringFeatures(get_transposed_matrix(), alphabet); + return std::make_shared>(get_transposed_matrix(), alphabet); } -template std::vector> CStringFeatures::get_transposed_matrix() +template std::vector> StringFeatures::get_transposed_matrix() { int32_t num_feat=get_num_vectors(); int32_t num_vec=get_max_vector_length(); @@ -271,7 +266,7 @@ template std::vector> CStringFeatures::get_transposed return sf; } -template void CStringFeatures::free_feature_vector(ST* feat_vec, int32_t num, bool dofree) +template void StringFeatures::free_feature_vector(ST* feat_vec, int32_t num, bool dofree) { if (num>=get_num_vectors()) { @@ -289,7 +284,7 @@ template void CStringFeatures::free_feature_vector(ST* feat_vec, i SG_FREE(feat_vec); } -template void CStringFeatures::free_feature_vector(SGVector feat_vec, int32_t num) +template void StringFeatures::free_feature_vector(SGVector feat_vec, int32_t num) { if (num>=get_num_vectors()) { @@ -304,7 +299,7 @@ template void CStringFeatures::free_feature_vector(SGVector fe feature_cache->unlock_entry(real_num); } -template ST CStringFeatures::get_feature(int32_t vec_num, int32_t feat_num) +template ST StringFeatures::get_feature(int32_t vec_num, int32_t feat_num) { ASSERT(vec_num ST CStringFeatures::get_feature(int32_t vec_num, int32_t return result; } -template int32_t CStringFeatures::get_vector_length(int32_t vec_num) +template int32_t StringFeatures::get_vector_length(int32_t vec_num) { ASSERT(vec_num int32_t CStringFeatures::get_vector_length(int32_t vec_nu return len; } -template int32_t CStringFeatures::get_max_vector_length() const +template int32_t StringFeatures::get_max_vector_length() const { int32_t max_string_length=0; index_t num_str=get_num_vectors(); for (int32_t i=0; isubset_idx_conversion(i)].vlen); } return max_string_length; } -template int32_t CStringFeatures::get_num_vectors() const +template int32_t StringFeatures::get_num_vectors() const { return m_subset_stack->has_subsets() ? m_subset_stack->get_size() : features.size(); } -template floatmax_t CStringFeatures::get_num_symbols() { return num_symbols; } +template floatmax_t StringFeatures::get_num_symbols() { return num_symbols; } -template floatmax_t CStringFeatures::get_max_num_symbols() { return CMath::powl(2,sizeof(ST)*8); } +template floatmax_t StringFeatures::get_max_num_symbols() { return Math::powl(2,sizeof(ST)*8); } -template floatmax_t CStringFeatures::get_original_num_symbols() { return original_num_symbols; } +template floatmax_t StringFeatures::get_original_num_symbols() { return original_num_symbols; } -template int32_t CStringFeatures::get_order() { return order; } +template int32_t StringFeatures::get_order() { return order; } -template ST CStringFeatures::get_masked_symbols(ST symbol, uint8_t mask) +template ST StringFeatures::get_masked_symbols(ST symbol, uint8_t mask) { ASSERT(symbol_mask_table) return symbol_mask_table[mask] & symbol; } -template ST CStringFeatures::shift_offset(ST offset, int32_t amount) +template ST StringFeatures::shift_offset(ST offset, int32_t amount) { ASSERT(alphabet) return (offset << (amount*alphabet->get_num_bits())); } -template ST CStringFeatures::shift_symbol(ST symbol, int32_t amount) +template ST StringFeatures::shift_symbol(ST symbol, int32_t amount) { ASSERT(alphabet) return (symbol >> (amount*alphabet->get_num_bits())); } -template void CStringFeatures::load_ascii_file(char* fname, bool remap_to_bin, +template void StringFeatures::load_ascii_file(char* fname, bool remap_to_bin, EAlphabet ascii_alphabet, EAlphabet binary_alphabet) { remove_all_subsets(); @@ -386,8 +381,8 @@ template void CStringFeatures::load_ascii_file(char* fname, bool r cleanup(); - CAlphabet* alpha=new CAlphabet(ascii_alphabet); - CAlphabet* alpha_bin=new CAlphabet(binary_alphabet); + auto alpha=std::make_shared(ascii_alphabet); + auto alpha_bin=std::make_shared(binary_alphabet); FILE* f=fopen(fname, "ro"); int32_t num_vectors = 0; @@ -416,7 +411,7 @@ template void CStringFeatures::load_ascii_file(char* fname, bool r if (dummy[i]=='\n' || (i==sz-1 && sz void CStringFeatures::load_ascii_file(char* fname, bool r // clear overflow overflow_len=0; - //CMath::display_vector(features[lines].vector, len); + //Math::display_vector(features[lines].string, len); old_sz=i+1; lines++; pb2.print_progress(); @@ -500,23 +495,19 @@ template void CStringFeatures::load_ascii_file(char* fname, bool r SG_FREE(dummy); SG_FREE(overflow); - SG_UNREF(alphabet); - if (remap_to_bin) { alphabet=alpha_bin; - SG_UNREF(alpha); } else { alphabet=alpha; - SG_UNREF(alpha_bin); } - SG_REF(alphabet); + num_symbols=alphabet->get_num_symbols(); } -template bool CStringFeatures::load_fasta_file(const char* fname, bool ignore_invalid) +template bool StringFeatures::load_fasta_file(const char* fname, bool ignore_invalid) { remove_all_subsets(); @@ -525,7 +516,7 @@ template bool CStringFeatures::load_fasta_file(const char* fname, uint64_t offs=0; int32_t num=0; - CMemoryMappedFile f(fname); + MemoryMappedFile f(fname); while (true) { @@ -541,8 +532,8 @@ template bool CStringFeatures::load_fasta_file(const char* fname, error("No fasta hunks (lines starting with '>') found"); cleanup(); - SG_UNREF(alphabet); - alphabet=new CAlphabet(DNA); + + alphabet=std::make_shared(DNA); num_symbols=alphabet->get_num_symbols(); std::vector> strings; @@ -606,12 +597,12 @@ template bool CStringFeatures::load_fasta_file(const char* fname, return set_features(strings); } -template bool CStringFeatures::load_fastq_file(const char* fname, +template bool StringFeatures::load_fastq_file(const char* fname, bool ignore_invalid, bool bitremap_in_single_string) { remove_all_subsets(); - CMemoryMappedFile f(fname); + MemoryMappedFile f(fname); int32_t i=0; uint64_t len=0; @@ -625,8 +616,8 @@ template bool CStringFeatures::load_fastq_file(const char* fname, num/=4; cleanup(); - SG_UNREF(alphabet); - alphabet=new CAlphabet(DNA); + + alphabet=std::make_shared(DNA); std::vector> strings; @@ -684,7 +675,7 @@ template bool CStringFeatures::load_fastq_file(const char* fname, for (uint64_t j=0; j bool CStringFeatures::load_fastq_file(const char* fname, return true; } -template bool CStringFeatures::load_from_directory(char* dirname) +template bool StringFeatures::load_from_directory(char* dirname) { remove_all_subsets(); @@ -751,7 +742,6 @@ template bool CStringFeatures::load_from_directory(char* dirname) } if (file->read(0, filesize, &result, &(buffer[0]))) error("failed to read file"); - int64_t sg_string_len = filesize/(int64_t)sizeof(ST); strings.emplace_back(sg_string_len); sg_memcpy(const_cast(result.data()), strings[num].vector, filesize); @@ -771,19 +761,19 @@ template bool CStringFeatures::load_from_directory(char* dirname) return false; } -template bool CStringFeatures::set_features(const std::vector>& string_list) +template bool StringFeatures::set_features(const std::vector>& string_list) { return set_features(string_list.data(), string_list.size()); } -template bool CStringFeatures::set_features(const SGVector* p_features, int32_t p_num_vectors) +template bool StringFeatures::set_features(const SGVector* p_features, int32_t p_num_vectors) { if (m_subset_stack->has_subsets()) error("Cannot call set_features() with subset."); if (p_features) { - CAlphabet* alpha=new CAlphabet(alphabet->get_alphabet()); + auto alpha=std::make_shared(alphabet->get_alphabet()); //compute histogram for char/byte for (int32_t i=0; i bool CStringFeatures::set_features(const SGVector* p_ if (alpha->check_alphabet_size() && alpha->check_alphabet()) { cleanup(); - SG_UNREF(alphabet); + alphabet=alpha; - SG_REF(alphabet); + // TODO remove copying features.clear(); @@ -807,14 +797,12 @@ template bool CStringFeatures::set_features(const SGVector* p_ return true; } - else - SG_UNREF(alpha); } return false; } -template bool CStringFeatures::append_features(CStringFeatures* sf) +template bool StringFeatures::append_features(std::shared_ptr> sf) { ASSERT(sf) @@ -828,13 +816,12 @@ template bool CStringFeatures::append_features(CStringFeatures for (int32_t i=0; im_subset_stack->subset_idx_conversion(i); - int32_t length=sf->features[real_i].vlen; new_features[i] = sf->features[real_i].clone(); } return append_features(new_features); } -template bool CStringFeatures::append_features(const std::vector>& p_features) +template bool StringFeatures::append_features(const std::vector>& p_features) { if (m_subset_stack->has_subsets()) error("Cannot call set_features() with subset."); @@ -842,7 +829,7 @@ template bool CStringFeatures::append_features(const std::vectorget_alphabet()); + auto alpha=std::make_shared(alphabet->get_alphabet()); //compute histogram for char/byte for (int32_t i=0; i bool CStringFeatures::append_features(const std::vectorcheck_alphabet_size() && alpha->check_alphabet()) { - SG_UNREF(alpha); for (int32_t i=0; iadd_string_to_histogram( p_features[i].vector, p_features[i].vlen); + alphabet->add_string_to_histogram(p_features[i].vector, p_features[i].vlen); int32_t old_num_vectors=get_num_vectors(); int32_t num_vectors=old_num_vectors+p_features.size(); @@ -874,12 +860,12 @@ template bool CStringFeatures::append_features(const std::vector const std::vector>& CStringFeatures::get_string_list() const +template const std::vector>& StringFeatures::get_string_list() const { if (m_subset_stack->has_subsets()) error("get features() is not possible on subset"); @@ -887,13 +873,13 @@ template const std::vector>& CStringFeatures::get_str return features; } -template std::vector>& CStringFeatures::get_string_list() +template std::vector>& StringFeatures::get_string_list() { return const_cast>&>( std::as_const(*this).get_string_list()); } -template std::vector> CStringFeatures::copy_features() +template std::vector> StringFeatures::copy_features() { ASSERT(get_num_vectors()>0) @@ -910,12 +896,12 @@ template std::vector> CStringFeatures::copy_features( return new_feat; } -template void CStringFeatures::get_features(std::vector>* dst) +template void StringFeatures::get_features(std::vector>* dst) { *dst=copy_features(); } -template bool CStringFeatures::load_compressed(char* src, bool decompress) +template bool StringFeatures::load_compressed(char* src, bool decompress) { remove_all_subsets(); @@ -946,13 +932,13 @@ template bool CStringFeatures::load_compressed(char* src, bool dec uint8_t c; if (fread(&c, sizeof(uint8_t), 1, file)!=1) error("failed to read compression type"); - CCompressor* compressor= new CCompressor((E_COMPRESSION_TYPE) c); + auto compressor= std::make_shared((E_COMPRESSION_TYPE) c); //alphabet uint8_t a; - delete alphabet; + alphabet.reset(); if (fread(&a, sizeof(uint8_t), 1, file)!=1) error("failed to read compression alphabet"); - alphabet=new CAlphabet((EAlphabet) a); + alphabet=std::make_shared((EAlphabet) a); // number of vectors if (fread(&num_vectors, sizeof(int32_t), 1, file)!=1) error("failed to read compression number of vectors"); @@ -1003,13 +989,12 @@ template bool CStringFeatures::load_compressed(char* src, bool dec } } - delete compressor; fclose(file); return false; } -template bool CStringFeatures::save_compressed(char* dest, E_COMPRESSION_TYPE compression, int level) +template bool StringFeatures::save_compressed(char* dest, E_COMPRESSION_TYPE compression, int level) { int32_t num_vectors = get_num_vectors(); int32_t max_string_length = get_max_vector_length(); @@ -1022,7 +1007,7 @@ template bool CStringFeatures::save_compressed(char* dest, E_COMPR if (!(file=fopen(dest, "wb"))) return false; - CCompressor* compressor= new CCompressor(compression); + auto compressor= std::make_shared(compression); // header shogun v0 const char* id="SGV0"; @@ -1067,12 +1052,11 @@ template bool CStringFeatures::save_compressed(char* dest, E_COMPR free_feature_vector(vec, i, vfree); } - delete compressor; fclose(file); return true; } -template int32_t CStringFeatures::obtain_by_sliding_window(int32_t window_size, int32_t step_size, int32_t skip) +template int32_t StringFeatures::obtain_by_sliding_window(int32_t window_size, int32_t step_size, int32_t skip) { if (m_subset_stack->has_subsets()) not_implemented(SOURCE_LOCATION); @@ -1109,7 +1093,7 @@ template int32_t CStringFeatures::obtain_by_sliding_window(int32_t return num_vectors; } -template int32_t CStringFeatures::obtain_by_position_list(int32_t window_size, CDynamicArray* positions, +template int32_t StringFeatures::obtain_by_position_list(int32_t window_size, std::shared_ptr> positions, int32_t skip) { if (m_subset_stack->has_subsets()) @@ -1167,12 +1151,12 @@ template int32_t CStringFeatures::obtain_by_position_list(int32_t return num_vectors; } -template bool CStringFeatures::obtain_from_char(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev) +template bool StringFeatures::obtain_from_char(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev) { return obtain_from_char_features(sf, start, p_order, gap, rev); } -template bool CStringFeatures::have_same_length(int32_t len) +template bool StringFeatures::have_same_length(int32_t len) { int32_t max_string_length = get_max_vector_length(); if (len!=-1) @@ -1192,7 +1176,7 @@ template bool CStringFeatures::have_same_length(int32_t len) return true; } -template void CStringFeatures::embed_features(int32_t p_order) +template void StringFeatures::embed_features(int32_t p_order) { if (m_subset_stack->has_subsets()) not_implemented(SOURCE_LOCATION); @@ -1204,13 +1188,13 @@ template void CStringFeatures::embed_features(int32_t p_order) int32_t max_val=alphabet->get_num_bits(); if (p_order>1) - num_symbols=CMath::powl((floatmax_t) 2, (floatmax_t) max_val*p_order); + num_symbols=Math::powl((floatmax_t) 2, (floatmax_t) max_val*p_order); else num_symbols=original_num_symbols; io::info("max_val (bit): {} order: {} -> results in num_symbols: {:.0f}", max_val, p_order, num_symbols); - if ( ((floatmax_t) num_symbols) > CMath::powl(((floatmax_t) 2),((floatmax_t) sizeof(ST)*8)) ) + if ( ((floatmax_t) num_symbols) > Math::powl(((floatmax_t) 2),((floatmax_t) sizeof(ST)*8)) ) io::warn("symbols did not fit into datatype \"{}\" ({})", (char) max_val, (int) max_val); ST mask=0; @@ -1246,7 +1230,7 @@ template void CStringFeatures::embed_features(int32_t p_order) compute_symbol_mask_table(max_val); } -template void CStringFeatures::compute_symbol_mask_table(int64_t max_val) +template void StringFeatures::compute_symbol_mask_table(int64_t max_val) { if (m_subset_stack->has_subsets()) not_implemented(SOURCE_LOCATION); @@ -1272,7 +1256,7 @@ template void CStringFeatures::compute_symbol_mask_table(int64_t m } } -template void CStringFeatures::unembed_word(ST word, uint8_t* seq, int32_t len) +template void StringFeatures::unembed_word(ST word, uint8_t* seq, int32_t len) { uint32_t nbits= (uint32_t) alphabet->get_num_bits(); @@ -1288,7 +1272,7 @@ template void CStringFeatures::unembed_word(ST word, uint8_t* seq, } } -template ST CStringFeatures::embed_word(ST* seq, int32_t len) +template ST StringFeatures::embed_word(ST* seq, int32_t len) { ST value=(ST) 0; uint32_t nbits= (uint32_t) alphabet->get_num_bits(); @@ -1301,7 +1285,7 @@ template ST CStringFeatures::embed_word(ST* seq, int32_t len) return value; } -template ST* CStringFeatures::get_zero_terminated_string_copy(SGVector str) +template ST* StringFeatures::get_zero_terminated_string_copy(SGVector str) { int32_t l=str.vlen; ST* s=SG_MALLOC(ST, l+1); @@ -1310,7 +1294,7 @@ template ST* CStringFeatures::get_zero_terminated_string_copy(SGVe return s; } -template void CStringFeatures::set_feature_vector(int32_t num, ST* string, int32_t len) +template void StringFeatures::set_feature_vector(int32_t num, ST* string, int32_t len) { ASSERT(num void CStringFeatures::set_feature_vector(int32_t num, ST* features[real_num] = SGVector(string, len); } -template void CStringFeatures::get_histogram(float64_t** hist, int32_t* rows, int32_t* cols, bool normalize) +template void StringFeatures::get_histogram(float64_t** hist, int32_t* rows, int32_t* cols, bool normalize) { int32_t nsym=get_num_symbols(); int32_t slen=get_max_vector_length(); @@ -1362,7 +1346,7 @@ template void CStringFeatures::get_histogram(float64_t** hist, int } template -void CStringFeatures::create_random(float64_t* hist, int32_t rows, int32_t cols, int32_t num_vec, int32_t seed) +void StringFeatures::create_random(float64_t* hist, int32_t rows, int32_t cols, int32_t num_vec, int32_t seed) { std::mt19937_64 prng(seed); ASSERT(rows == get_num_symbols()) @@ -1465,7 +1449,7 @@ CStringFeatures* obtain_sssk_double_from_char(int **S, int *len, int } */ -template CFeatures* CStringFeatures::copy_subset( +template std::shared_ptr StringFeatures::copy_subset( SGVector indices) const { /* string list to create new CStringFeatures from */ @@ -1484,23 +1468,21 @@ template CFeatures* CStringFeatures::copy_subset( } /* create copy instance */ - CStringFeatures* result=new CStringFeatures(list_copy, alphabet); + auto result=std::make_shared>(); /* keep things from original features (otherwise assertions in x-val) */ result->order=order; result->compute_symbol_mask_table(result->alphabet->get_num_symbols()); - SG_REF(result); - return result; } -template void CStringFeatures::subset_changed_post() +template void StringFeatures::subset_changed_post() { /* max string length has to be updated */ } -template ST* CStringFeatures::compute_feature_vector(int32_t num, int32_t& len) +template ST* StringFeatures::compute_feature_vector(int32_t num, int32_t& len) { ASSERT(num ST* CStringFeatures::compute_feature_vector(int32_t num, return target; } -template void CStringFeatures::init() +template void StringFeatures::init() { set_generic(); @@ -1531,9 +1513,9 @@ template void CStringFeatures::init() SG_ADD(&alphabet, "alphabet", "Alphabet used."); - m_parameters->add(&single_string, + /*m_parameters->add(&single_string, "single_string", - "Created by sliding window."); + "Created by sliding window.");*/ watch_param("single_string", &single_string); SG_ADD( @@ -1550,15 +1532,15 @@ template void CStringFeatures::init() "Symbol mask table - using in higher order mapping"); watch_param("string_list", &features); - watch_method("num_vectors", &CStringFeatures::get_num_vectors); - watch_method("max_string_length", &CStringFeatures::get_max_vector_length); + watch_method("num_vectors", &StringFeatures::get_num_vectors); + watch_method("max_string_length", &StringFeatures::get_max_vector_length); } /** get feature type the char feature can deal with * * @return feature type char */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_BOOL; } @@ -1567,7 +1549,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type char */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_CHAR; } @@ -1576,7 +1558,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type BYTE */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_BYTE; } @@ -1585,7 +1567,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type SHORT */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_SHORT; } @@ -1594,7 +1576,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type WORD */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_WORD; } @@ -1603,7 +1585,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type INT */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_INT; } @@ -1612,7 +1594,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type INT */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_UINT; } @@ -1621,7 +1603,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type LONG */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_LONG; } @@ -1630,7 +1612,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type ULONG */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_ULONG; } @@ -1639,7 +1621,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type SHORTREAL */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_SHORTREAL; } @@ -1648,7 +1630,7 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type DREAL */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_DREAL; } @@ -1657,121 +1639,121 @@ template<> EFeatureType CStringFeatures::get_feature_type() const * * @return feature type LONGREAL */ -template<> EFeatureType CStringFeatures::get_feature_type() const +template<> EFeatureType StringFeatures::get_feature_type() const { return F_LONGREAL; } -template<> bool CStringFeatures::get_masked_symbols(bool symbol, uint8_t mask) +template<> bool StringFeatures::get_masked_symbols(bool symbol, uint8_t mask) { return symbol; } -template<> float32_t CStringFeatures::get_masked_symbols(float32_t symbol, uint8_t mask) +template<> float32_t StringFeatures::get_masked_symbols(float32_t symbol, uint8_t mask) { return symbol; } -template<> float64_t CStringFeatures::get_masked_symbols(float64_t symbol, uint8_t mask) +template<> float64_t StringFeatures::get_masked_symbols(float64_t symbol, uint8_t mask) { return symbol; } -template<> floatmax_t CStringFeatures::get_masked_symbols(floatmax_t symbol, uint8_t mask) +template<> floatmax_t StringFeatures::get_masked_symbols(floatmax_t symbol, uint8_t mask) { return symbol; } -template<> bool CStringFeatures::shift_offset(bool symbol, int32_t amount) +template<> bool StringFeatures::shift_offset(bool symbol, int32_t amount) { return false; } -template<> float32_t CStringFeatures::shift_offset(float32_t symbol, int32_t amount) +template<> float32_t StringFeatures::shift_offset(float32_t symbol, int32_t amount) { return 0; } -template<> float64_t CStringFeatures::shift_offset(float64_t symbol, int32_t amount) +template<> float64_t StringFeatures::shift_offset(float64_t symbol, int32_t amount) { return 0; } -template<> floatmax_t CStringFeatures::shift_offset(floatmax_t symbol, int32_t amount) +template<> floatmax_t StringFeatures::shift_offset(floatmax_t symbol, int32_t amount) { return 0; } -template<> bool CStringFeatures::shift_symbol(bool symbol, int32_t amount) +template<> bool StringFeatures::shift_symbol(bool symbol, int32_t amount) { return symbol; } -template<> float32_t CStringFeatures::shift_symbol(float32_t symbol, int32_t amount) +template<> float32_t StringFeatures::shift_symbol(float32_t symbol, int32_t amount) { return symbol; } -template<> float64_t CStringFeatures::shift_symbol(float64_t symbol, int32_t amount) +template<> float64_t StringFeatures::shift_symbol(float64_t symbol, int32_t amount) { return symbol; } -template<> floatmax_t CStringFeatures::shift_symbol(floatmax_t symbol, int32_t amount) +template<> floatmax_t StringFeatures::shift_symbol(floatmax_t symbol, int32_t amount) { return symbol; } #ifndef SUNOS -template<> template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev) +template<> template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev) { return false; } -template<> template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev) +template<> template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev) { return false; } -template<> template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev) +template<> template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev) { return false; } #endif -template<> void CStringFeatures::embed_features(int32_t p_order) +template<> void StringFeatures::embed_features(int32_t p_order) { } -template<> void CStringFeatures::embed_features(int32_t p_order) +template<> void StringFeatures::embed_features(int32_t p_order) { } -template<> void CStringFeatures::embed_features(int32_t p_order) +template<> void StringFeatures::embed_features(int32_t p_order) { } -template<> void CStringFeatures::compute_symbol_mask_table(int64_t max_val) +template<> void StringFeatures::compute_symbol_mask_table(int64_t max_val) { } -template<> void CStringFeatures::compute_symbol_mask_table(int64_t max_val) +template<> void StringFeatures::compute_symbol_mask_table(int64_t max_val) { } -template<> void CStringFeatures::compute_symbol_mask_table(int64_t max_val) +template<> void StringFeatures::compute_symbol_mask_table(int64_t max_val) { } -template<> float32_t CStringFeatures::embed_word(float32_t* seq, int32_t len) +template<> float32_t StringFeatures::embed_word(float32_t* seq, int32_t len) { return 0; } -template<> float64_t CStringFeatures::embed_word(float64_t* seq, int32_t len) +template<> float64_t StringFeatures::embed_word(float64_t* seq, int32_t len) { return 0; } -template<> floatmax_t CStringFeatures::embed_word(floatmax_t* seq, int32_t len) +template<> floatmax_t StringFeatures::embed_word(floatmax_t* seq, int32_t len) { return 0; } -template<> void CStringFeatures::unembed_word(float32_t word, uint8_t* seq, int32_t len) +template<> void StringFeatures::unembed_word(float32_t word, uint8_t* seq, int32_t len) { } -template<> void CStringFeatures::unembed_word(float64_t word, uint8_t* seq, int32_t len) +template<> void StringFeatures::unembed_word(float64_t word, uint8_t* seq, int32_t len) { } -template<> void CStringFeatures::unembed_word(floatmax_t word, uint8_t* seq, int32_t len) +template<> void StringFeatures::unembed_word(floatmax_t word, uint8_t* seq, int32_t len) { } #define LOAD(f_load, sg_type) \ -template<> void CStringFeatures::load(CFile* loader) \ +template<> void StringFeatures::load(std::shared_ptr loader) \ { \ io::info("loading..."); \ \ @@ -1801,7 +1783,7 @@ LOAD(get_string_list, floatmax_t) #undef LOAD #define SAVE(f_write, sg_type) \ -template<> void CStringFeatures::save(CFile* writer) \ +template<> void StringFeatures::save(std::shared_ptr writer) \ { \ if (m_subset_stack->has_subsets()) \ error("save() is not possible on subset"); \ @@ -1827,13 +1809,13 @@ SAVE(set_string_list, floatmax_t) #undef SAVE template template -bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, +bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev) { remove_all_subsets(); ASSERT(sf) - CAlphabet* alpha=sf->get_alphabet(); + auto alpha=sf->get_alphabet(); ASSERT(alpha->get_num_symbols_in_histogram() > 0) this->order=p_order; @@ -1862,15 +1844,13 @@ bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int original_num_symbols=alpha->get_num_symbols(); int32_t max_val=alpha->get_num_bits(); - SG_UNREF(alpha); - if (p_order>1) - num_symbols=CMath::powl((floatmax_t) 2, (floatmax_t) max_val*p_order); + num_symbols=Math::powl((floatmax_t) 2, (floatmax_t) max_val*p_order); else num_symbols=original_num_symbols; io::info("max_val (bit): {} order: {} -> results in num_symbols: {:.0f}", max_val, p_order, num_symbols); - if ( ((floatmax_t) num_symbols) > CMath::powl(((floatmax_t) 2),((floatmax_t) sizeof(ST)*8)) ) + if ( ((floatmax_t) num_symbols) > Math::powl(((floatmax_t) 2),((floatmax_t) sizeof(ST)*8)) ) { error("symbol does not fit into datatype \"{}\" ({})", (char) max_val, (int) max_val); return false; @@ -1885,9 +1865,9 @@ bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int ASSERT(!vfree) // won't work when preprocessors are attached if (rev) - CAlphabet::translate_from_single_order_reversed(fv, len, start+gap, p_order+gap, max_val, gap); + Alphabet::translate_from_single_order_reversed(fv, len, start+gap, p_order+gap, max_val, gap); else - CAlphabet::translate_from_single_order(fv, len, start+gap, p_order+gap, max_val, gap); + Alphabet::translate_from_single_order(fv, len, start+gap, p_order+gap, max_val, gap); /* fix the length of the string -- hacky */ features[line].vlen-=start+gap ; @@ -1900,25 +1880,25 @@ bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int return true; } -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; -template class CStringFeatures; - -template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev); -template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev); -template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev); - -template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev); -template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev); -template bool CStringFeatures::obtain_from_char_features(CStringFeatures* sf, int32_t start, int32_t p_order, int32_t gap, bool rev); +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; +template class StringFeatures; + +template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); +template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); +template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); + +template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); +template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); +template bool StringFeatures::obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); } diff --git a/src/shogun/features/StringFeatures.h b/src/shogun/features/StringFeatures.h index 4443655e8a8..c13a2ca4f19 100644 --- a/src/shogun/features/StringFeatures.h +++ b/src/shogun/features/StringFeatures.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, - * Evgeniy Andreev, Vladislav Horbatiuk, Evan Shelhamer, Yuyu Zhang, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, + * Evgeniy Andreev, Vladislav Horbatiuk, Evan Shelhamer, Yuyu Zhang, * Thoralf Klein, Fernando Iglesias, Bjoern Esser */ @@ -22,9 +22,9 @@ namespace shogun { -class CAlphabet; -template class CDynamicArray; -class CFile; +class Alphabet; +template class DynamicArray; +class File; template class SGVector; #ifndef DOXYGEN_SHOULD_SKIP_THIS @@ -48,7 +48,7 @@ struct SSKTripleFeature * * As this class is a template the underlying storage type is quite arbitrary and * not limited to character strings, but could also be sequences of floating - * point numbers etc. Strings differ from matrices (cf. CDenseFeatures) in a + * point numbers etc. Strings differ from matrices (cf. DenseFeatures) in a * way that the dimensionality of the feature vectors (i.e. the strings) is not * fixed; it may vary between strings. * @@ -65,49 +65,49 @@ struct SSKTripleFeature * Simple use the (inherited) add_subset(), remove_subset() functions. * If done, all calls that work with features are translated to the subset. * See comments to find out whether it is supported for that method. - * See also CFeatures class documentation + * See also Features class documentation */ -template class CStringFeatures : public CFeatures +template class StringFeatures : public Features { public: /** default constructor */ - CStringFeatures(); + StringFeatures(); /** constructor * * @param alpha alphabet (type) to use for string features */ - CStringFeatures(EAlphabet alpha); + StringFeatures(EAlphabet alpha); /** constructor * * @param string_list * @param alpha alphabet (type) to use for string features */ - CStringFeatures(const std::vector>& string_list, EAlphabet alpha); + StringFeatures(const std::vector>& string_list, EAlphabet alpha); /** constructor * * @param string_list * @param alpha an actual alphabet */ - CStringFeatures(const std::vector>& string_list, CAlphabet* alpha); + StringFeatures(const std::vector>& string_list, std::shared_ptr alpha); /** constructor * * @param alpha alphabet to use for string features */ - CStringFeatures(CAlphabet* alpha); + StringFeatures(std::shared_ptr alpha); /** constructor * * @param loader File object via which to load data * @param alpha alphabet (type) to use for string features */ - CStringFeatures(CFile* loader, EAlphabet alpha=DNA); + StringFeatures(std::shared_ptr loader, EAlphabet alpha=DNA); /** destructor */ - virtual ~CStringFeatures(); + virtual ~StringFeatures(); /** cleanup string features. * @@ -149,13 +149,13 @@ template class CStringFeatures : public CFeatures * * @return alphabet */ - CAlphabet* get_alphabet() const; + std::shared_ptr get_alphabet() const; /** duplicate feature object * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get string for selected example num * @@ -201,7 +201,7 @@ template class CStringFeatures : public CFeatures * * @return transposed copy */ - CStringFeatures* get_transposed(); + std::shared_ptr> get_transposed(); /** compute and return the transpose of string features matrix * which will be prepocessed. @@ -327,7 +327,7 @@ template class CStringFeatures : public CFeatures * * @param loader File object via which to load data */ - virtual void load(CFile* loader); + virtual void load(std::shared_ptr loader); /** load ascii line-based string features from file. * @@ -399,7 +399,7 @@ template class CStringFeatures : public CFeatures * @param sf features to append * @return if setting was successful */ - bool append_features(CStringFeatures* sf); + bool append_features(std::shared_ptr> sf); /** append features * @@ -452,7 +452,7 @@ template class CStringFeatures : public CFeatures * * @param writer File object via which to save data */ - virtual void save(CFile* writer); + virtual void save(std::shared_ptr writer); /** load compressed features from file * @@ -499,7 +499,7 @@ template class CStringFeatures : public CFeatures * @param skip skip * @return something inty */ - int32_t obtain_by_position_list(int32_t window_size, CDynamicArray* positions, + int32_t obtain_by_position_list(int32_t window_size, std::shared_ptr> positions, int32_t skip=0); /** obtain string features from char features @@ -515,7 +515,7 @@ template class CStringFeatures : public CFeatures * @param rev reverse * @return if obtaining was successful */ - bool obtain_from_char(CStringFeatures* sf, int32_t start, + bool obtain_from_char(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); /** template obtain from char features @@ -530,7 +530,7 @@ template class CStringFeatures : public CFeatures * @return if obtaining was successful */ template - bool obtain_from_char_features(CStringFeatures* sf, int32_t start, + bool obtain_from_char_features(std::shared_ptr> sf, int32_t start, int32_t p_order, int32_t gap, bool rev); /** check if length of each vector in this feature object equals the @@ -607,15 +607,15 @@ template class CStringFeatures : public CFeatures virtual void create_random(float64_t* hist, int32_t rows, int32_t cols, int32_t num_vec, int32_t seed=0); - /** Creates a new CFeatures instance containing copies of the elements + /** Creates a new Features instance containing copies of the elements * which are specified by the provided indices. * * possible with subset * * @param indices indices of feature elements to copy - * @return new CFeatures instance with copies of feature data + * @return new Features instance with copies of feature data */ - virtual CFeatures* copy_subset(SGVector indices) const; + virtual std::shared_ptr copy_subset(SGVector indices) const; /** @return object name */ virtual const char* get_name() const { return "StringFeatures"; } @@ -641,7 +641,7 @@ template class CStringFeatures : public CFeatures protected: /** alphabet */ - CAlphabet* alphabet; + std::shared_ptr alphabet; /** this contains the array of features */ std::vector> features; @@ -665,7 +665,7 @@ template class CStringFeatures : public CFeatures bool preprocess_on_get; /** feature cache */ - CCache* feature_cache; + Cache* feature_cache; }; } #endif // _CSTRINGFEATURES__H__ diff --git a/src/shogun/features/StringFileFeatures.cpp b/src/shogun/features/StringFileFeatures.cpp index 5c017bbb460..6013c2a813f 100644 --- a/src/shogun/features/StringFileFeatures.cpp +++ b/src/shogun/features/StringFileFeatures.cpp @@ -3,24 +3,23 @@ namespace shogun { -template CStringFileFeatures::CStringFileFeatures() : CStringFeatures(), file(NULL) +template StringFileFeatures::StringFileFeatures() : StringFeatures(), file(NULL) { } -template CStringFileFeatures::CStringFileFeatures(const char* fname, EAlphabet alpha) -: CStringFeatures(alpha) +template StringFileFeatures::StringFileFeatures(const char* fname, EAlphabet alpha) +: StringFeatures(alpha) { - file = new CMemoryMappedFile(fname); + file = std::make_shared>(fname); fetch_meta_info_from_file(); } -template CStringFileFeatures::~CStringFileFeatures() +template StringFileFeatures::~StringFileFeatures() { - SG_UNREF(file); - CStringFileFeatures::cleanup(); + StringFileFeatures::cleanup(); } -template ST* CStringFileFeatures::get_line(uint64_t& len, uint64_t& offs, int32_t& line_nr, uint64_t file_length) +template ST* StringFileFeatures::get_line(uint64_t& len, uint64_t& offs, int32_t& line_nr, uint64_t file_length) { ST* s = file->get_map(); for (uint64_t i=offs; i ST* CStringFileFeatures::get_line(uint64_t& len, uint64_ } else { - if (!CStringFeatures::alphabet->is_valid((uint8_t) c)) + if (!StringFeatures::alphabet->is_valid((uint8_t) c)) { - CStringFileFeatures::cleanup(); + StringFileFeatures::cleanup(); error("Invalid character ({}) in line {}", c, line_nr); } } @@ -50,37 +49,35 @@ template ST* CStringFileFeatures::get_line(uint64_t& len, uint64_ return NULL; } -template void CStringFileFeatures::cleanup() +template void StringFileFeatures::cleanup() { - CStringFeatures::features.clear(); - CStringFeatures::symbol_mask_table = SGVector(); + StringFeatures::features.clear(); + StringFeatures::symbol_mask_table = SGVector(); /* start with a fresh alphabet, but instead of emptying the histogram - * create a new object (to leave the alphabet object alone if it is used - * by others) - */ - CAlphabet* alpha=new CAlphabet(CStringFeatures::alphabet->get_alphabet()); - SG_UNREF(CStringFeatures::alphabet); - CStringFeatures::alphabet=alpha; - SG_REF(CStringFeatures::alphabet); + * create a new object (to leave the alphabet object alone if it is used + * by others) + */ + auto alpha=std::make_shared(StringFeatures::alphabet->get_alphabet()); + StringFeatures::alphabet=alpha; } -template void CStringFileFeatures::cleanup_feature_vector(int32_t num) +template void StringFileFeatures::cleanup_feature_vector(int32_t num) { error("Cleaning single feature vector not" "supported by StringFileFeatures"); } -template void CStringFileFeatures::fetch_meta_info_from_file(int32_t granularity) +template void StringFileFeatures::fetch_meta_info_from_file(int32_t granularity) { - CStringFileFeatures::cleanup(); + StringFileFeatures::cleanup(); uint64_t file_size=file->get_size(); ASSERT(granularity>=1) - ASSERT(CStringFeatures::alphabet) + ASSERT(StringFeatures::alphabet) int64_t buffer_size=granularity; - CStringFeatures::features.clear(); - CStringFeatures::features.resize(buffer_size); + StringFeatures::features.clear(); + StringFeatures::features.resize(buffer_size); uint64_t offs=0; uint64_t len=0; @@ -94,38 +91,38 @@ template void CStringFileFeatures::fetch_meta_info_from_file(int3 { if (num_vectors > buffer_size) { - CStringFeatures::features.resize(buffer_size+granularity); + StringFeatures::features.resize(buffer_size+granularity); buffer_size+=granularity; } - CStringFeatures::features[num_vectors-1]=SGVector(line, len, false); + StringFeatures::features[num_vectors-1]=SGVector(line, len, false); } else break; } - io::info("number of strings:{}", CStringFeatures::get_num_vectors()); - io::info("maximum string length:{}", CStringFeatures::get_max_vector_length()); - io::info("max_value_in_histogram:{}", CStringFeatures::alphabet->get_max_value_in_histogram()); - io::info("num_symbols_in_histogram:{}", CStringFeatures::alphabet->get_num_symbols_in_histogram()); + io::info("number of strings:{}", StringFeatures::get_num_vectors()); + io::info("maximum string length:{}", StringFeatures::get_max_vector_length()); + io::info("max_value_in_histogram:{}", StringFeatures::alphabet->get_max_value_in_histogram()); + io::info("num_symbols_in_histogram:{}", StringFeatures::alphabet->get_num_symbols_in_histogram()); - if (!CStringFeatures::alphabet->check_alphabet_size() || !CStringFeatures::alphabet->check_alphabet()) - CStringFileFeatures::cleanup(); + if (!StringFeatures::alphabet->check_alphabet_size() || !StringFeatures::alphabet->check_alphabet()) + StringFileFeatures::cleanup(); - CStringFeatures::features.resize(num_vectors); + StringFeatures::features.resize(num_vectors); } -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; -template class CStringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; +template class StringFileFeatures; } diff --git a/src/shogun/features/StringFileFeatures.h b/src/shogun/features/StringFileFeatures.h index bc5137c43e3..399c367e485 100644 --- a/src/shogun/features/StringFileFeatures.h +++ b/src/shogun/features/StringFileFeatures.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Yuyu Zhang, Viktor Gal, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Yuyu Zhang, Viktor Gal, * Evan Shelhamer, Bjoern Esser */ @@ -18,8 +18,8 @@ namespace shogun { -class CAlphabet; -template class CMemoryMappedFile; +class Alphabet; +template class MemoryMappedFile; /** @brief File based string features. * @@ -30,26 +30,26 @@ template class CMemoryMappedFile; * Supported file format contains one string per line, lines of variable length * are supported and must be separated by '\n'. */ -template class CStringFileFeatures : public CStringFeatures +template class StringFileFeatures : public StringFeatures { public: /** default constructor * */ - CStringFileFeatures(); + StringFileFeatures(); /** constructor * * @param fname filename of the file containing line based features * @param alpha alphabet (type) to use for string features */ - CStringFileFeatures(const char* fname, EAlphabet alpha); + StringFileFeatures(const char* fname, EAlphabet alpha); /** default destructor * */ - virtual ~CStringFileFeatures(); + virtual ~StringFileFeatures(); /** Returns the name of the SGSerializable instance. * @@ -88,7 +88,7 @@ template class CStringFileFeatures : public CStringFeatures protected: /** memory mapped file*/ - CMemoryMappedFile* file; + std::shared_ptr> file; }; } #endif // _CSTRINGFILEFEATURES__H__ diff --git a/src/shogun/features/Subset.cpp b/src/shogun/features/Subset.cpp index bacbaad739c..acacbcaa931 100644 --- a/src/shogun/features/Subset.cpp +++ b/src/shogun/features/Subset.cpp @@ -9,23 +9,23 @@ using namespace shogun; -CSubset::CSubset() +Subset::Subset() { init(); } -CSubset::CSubset(const SGVector& subset_idx) +Subset::Subset(const SGVector& subset_idx) { init(); m_subset_idx = subset_idx.clone(); } -CSubset::~CSubset() +Subset::~Subset() { } -void CSubset::init() +void Subset::init() { SG_ADD(&m_subset_idx, "subset", "Vector of subset indices"); } diff --git a/src/shogun/features/Subset.h b/src/shogun/features/Subset.h index c0540acfb6c..d67f4bf133b 100644 --- a/src/shogun/features/Subset.h +++ b/src/shogun/features/Subset.h @@ -18,22 +18,22 @@ namespace shogun { /** @brief Wrapper class for an index subset which is used by SubsetStack. */ -class CSubset: public CSGObject +class Subset: public SGObject { - friend class CSubsetStack; + friend class SubsetStack; public: /** default constructor, do not use */ - CSubset(); + Subset(); /** constructor * * @param subset_idx vector of subset indices. */ - CSubset(const SGVector& subset_idx); + Subset(const SGVector& subset_idx); /** destructor */ - virtual ~CSubset(); + virtual ~Subset(); /** @return size of subset index array */ index_t get_size() const { return m_subset_idx.vlen; } diff --git a/src/shogun/features/SubsetStack.cpp b/src/shogun/features/SubsetStack.cpp index 3a3d86181aa..18d2723585e 100644 --- a/src/shogun/features/SubsetStack.cpp +++ b/src/shogun/features/SubsetStack.cpp @@ -34,59 +34,43 @@ using namespace shogun; -CSubsetStack::CSubsetStack() +SubsetStack::SubsetStack() { init(); } -CSubsetStack::CSubsetStack(const CSubsetStack& other) +SubsetStack::SubsetStack(const SubsetStack& other) { init(); - for (int32_t i=0; i < other.m_active_subsets_stack->get_num_elements(); ++i) - { - auto subset = other.m_active_subsets_stack->get_element(i); - m_active_subsets_stack->append_element(subset); - SG_UNREF(subset) - } + m_active_subsets_stack = other.m_active_subsets_stack; m_active_subset = other.m_active_subset; - SG_REF(m_active_subset) -} - -CSubsetStack::~CSubsetStack() -{ - SG_UNREF(m_active_subsets_stack); - SG_UNREF(m_active_subset); } -void CSubsetStack::remove_all_subsets() +void SubsetStack::remove_all_subsets() { - for (index_t i=0; i < m_active_subsets_stack->get_num_elements(); ++i) - m_active_subsets_stack->pop_back(); - - SG_UNREF(m_active_subset); + /* delete all active subsets, backwards due to DynArray implementation */ + m_active_subsets_stack.clear(); m_active_subset = nullptr; } -void CSubsetStack::init() +void SubsetStack::init() { - SG_ADD((CSGObject**)&m_active_subset, "active_subset", + SG_ADD((std::shared_ptr*)&m_active_subset, "active_subset", "Currently active subset"); - SG_ADD((CSGObject**)&m_active_subsets_stack, "active_subsets_stack", + SG_ADD(&m_active_subsets_stack, "active_subsets_stack", "Stack of active subsets"); m_active_subset=NULL; - m_active_subsets_stack=new CDynamicObjectArray(); - SG_REF(m_active_subsets_stack); } -void CSubsetStack::add_subset(const SGVector& subset) +void SubsetStack::add_subset(const SGVector& subset) { /* if there are already subsets on stack, do some legality checks */ - if (m_active_subsets_stack->get_num_elements()) + if (!m_active_subsets_stack.empty()) { /* check that subsets may only be smaller or equal than existing */ - CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element(); + auto latest=m_active_subsets_stack.back(); if (subset.vlen>latest->m_subset_idx.vlen) { subset.display_vector("subset"); @@ -96,7 +80,7 @@ void CSubsetStack::add_subset(const SGVector& subset) } /* check for range of indices */ - index_t max_index=CMath::max(subset.vector, subset.vlen); + index_t max_index=Math::max(subset.vector, subset.vlen); if (max_index>=latest->m_subset_idx.vlen) { subset.display_vector("subset"); @@ -104,22 +88,19 @@ void CSubsetStack::add_subset(const SGVector& subset) error("{}::add_subset(): Provided index vector contains" " indices larger than possible range!", get_name()); } - - /* clean up */ - SG_UNREF(latest); } /* active subset will be changed anyway, no setting to NULL */ - SG_UNREF(m_active_subset); + /* two cases: stack is empty/stack is not empty */ - if (m_active_subsets_stack->get_num_elements()) + if (!m_active_subsets_stack.empty()) { /* if there are already subsets, we need to map given one through * existing ones */ /* get latest current subset */ - CSubset* latest=(CSubset*)m_active_subsets_stack->get_last_element(); + auto latest=m_active_subsets_stack.back(); /* create new index vector */ SGVector new_active_subset=SGVector(subset.vlen); @@ -133,49 +114,46 @@ void CSubsetStack::add_subset(const SGVector& subset) } /* replace active subset */ - m_active_subset=new CSubset(new_active_subset); - SG_REF(m_active_subset); - SG_UNREF(latest); + m_active_subset=std::make_shared(new_active_subset); + + } else { /* just use plain given subset since there is nothing to map */ - m_active_subset=new CSubset(subset); - SG_REF(m_active_subset); + m_active_subset=std::make_shared(subset); + } /* add current active subset on stack of active subsets in any case */ - m_active_subsets_stack->append_element(m_active_subset); + m_active_subsets_stack.push_back(m_active_subset); } -void CSubsetStack::add_subset_in_place(SGVector subset) +void SubsetStack::add_subset_in_place(SGVector subset) { not_implemented(SOURCE_LOCATION);; } -void CSubsetStack::remove_subset() +void SubsetStack::remove_subset() { - index_t num_subsets=m_active_subsets_stack->get_num_elements(); + index_t num_subsets=m_active_subsets_stack.size(); if (num_subsets) { /* unref current subset */ - SG_UNREF(m_active_subset); + m_active_subset=NULL; /* delete last element on stack */ if (num_subsets>=1) { - index_t last_idx=m_active_subsets_stack->get_num_elements()-1; - m_active_subsets_stack->delete_element(last_idx); + m_active_subsets_stack.pop_back(); } /* if there are subsets left on stack, set the next one as active */ if (num_subsets>1) { /* use new last element on stack as active subset */ - index_t last_idx=m_active_subsets_stack->get_num_elements()-1; - m_active_subset=(CSubset*) - m_active_subsets_stack->get_element(last_idx); + m_active_subset=m_active_subsets_stack.back(); } /* otherwise, active subset is just empty */ diff --git a/src/shogun/features/SubsetStack.h b/src/shogun/features/SubsetStack.h index 4ba229402af..3d02bd3faa4 100644 --- a/src/shogun/features/SubsetStack.h +++ b/src/shogun/features/SubsetStack.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, Soumyajit De, - * Chiyuan Zhang, Viktor Gal, Fernando Iglesias, Bjoern Esser, + * Authors: Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, Soumyajit De, + * Chiyuan Zhang, Viktor Gal, Fernando Iglesias, Bjoern Esser, * Yuyu Zhang */ @@ -20,7 +20,7 @@ namespace shogun { -/** @brief class to add subset support to another class. A CSubsetStackStack instance +/** @brief class to add subset support to another class. A SubsetStackStack instance * should be added and wrapper methods to all interfaces should be added. * * The subsets are organized as a stack. One can add arbritary many index sets @@ -33,19 +33,19 @@ namespace shogun * mapping. On removal, the last element on stack will be removed. This is done * for computational convenience. */ -class CSubsetStack: public CSGObject +class SubsetStack: public SGObject { public: /** Constructor. Creates empty subset stack */ - CSubsetStack(); + SubsetStack(); /** copy constructor */ - CSubsetStack(const CSubsetStack& other); + SubsetStack(const SubsetStack& other); /** destructor */ - virtual ~CSubsetStack(); + virtual ~SubsetStack() = default; /** @return name of the SGSerializable */ inline const char* get_name() const { return "SubsetStack"; } @@ -79,7 +79,7 @@ class CSubsetStack: public CSGObject inline index_t get_size() const { if (!has_subsets()) - io::warn("CSubsetStack::get_size(): No subset in stack!"); + io::warn("SubsetStack::get_size(): No subset in stack!"); return has_subsets() ? m_active_subset->get_size() : -1; } @@ -87,14 +87,14 @@ class CSubsetStack: public CSGObject /** @return true iff subset was added */ virtual bool has_subsets() const { - return (m_active_subsets_stack->get_num_elements() > 0); + return (m_active_subsets_stack.size() > 0); } /** returns last (active) subset of the stack * * @return active subset */ - CSubset* get_last_subset() const { return m_active_subset; } + std::shared_ptr get_last_subset() const { return m_active_subset; } /** returns the corresponding real index of a subset index * Maps through all added subsets in stack. @@ -115,10 +115,10 @@ class CSubsetStack: public CSGObject * recomputing them when subsets are removed. There is always the identity * subset as first element in here (only internal visible, has_subsets() * returns false if only this identity is present) */ - CDynamicObjectArray* m_active_subsets_stack; + std::vector> m_active_subsets_stack; /** active index subset. Last element on stack for quick access */ - CSubset* m_active_subset; + std::shared_ptr m_active_subset; }; } diff --git a/src/shogun/features/TOPFeatures.cpp b/src/shogun/features/TOPFeatures.cpp index d2ef2f1c98f..4f4a18f2a39 100644 --- a/src/shogun/features/TOPFeatures.cpp +++ b/src/shogun/features/TOPFeatures.cpp @@ -10,14 +10,14 @@ using namespace shogun; -CTOPFeatures::CTOPFeatures() +TOPFeatures::TOPFeatures() { init(); } -CTOPFeatures::CTOPFeatures( - int32_t size, CHMM* p, CHMM* n, bool neglin, bool poslin) -: CDenseFeatures(size) +TOPFeatures::TOPFeatures( + int32_t size, std::shared_ptr p, std::shared_ptr n, bool neglin, bool poslin) +: DenseFeatures(size) { init(); neglinear=neglin; @@ -26,8 +26,8 @@ CTOPFeatures::CTOPFeatures( set_models(p,n); } -CTOPFeatures::CTOPFeatures(const CTOPFeatures &orig) -: CDenseFeatures(orig) +TOPFeatures::TOPFeatures(const TOPFeatures &orig) +: DenseFeatures(orig) { init(); pos=orig.pos; @@ -36,7 +36,7 @@ CTOPFeatures::CTOPFeatures(const CTOPFeatures &orig) poslinear=orig.poslinear; } -CTOPFeatures::~CTOPFeatures() +TOPFeatures::~TOPFeatures() { SG_FREE(pos_relevant_indizes.idx_p); SG_FREE(pos_relevant_indizes.idx_q); @@ -52,15 +52,15 @@ CTOPFeatures::~CTOPFeatures() SG_FREE(neg_relevant_indizes.idx_b_cols); SG_FREE(neg_relevant_indizes.idx_b_rows); - SG_UNREF(pos); - SG_UNREF(neg); + + } -void CTOPFeatures::set_models(CHMM* p, CHMM* n) +void TOPFeatures::set_models(std::shared_ptr p, std::shared_ptr n) { ASSERT(p && n) - SG_REF(p); - SG_REF(n); + + pos=p; neg=n; @@ -78,7 +78,7 @@ void CTOPFeatures::set_models(CHMM* p, CHMM* n) SG_DEBUG("pos_feat=[{},{},{},{}],neg_feat=[{},{},{},{}] -> {} features", pos->get_N(), pos->get_N(), pos->get_N()*pos->get_N(), pos->get_N()*pos->get_M(), neg->get_N(), neg->get_N(), neg->get_N()*neg->get_N(), neg->get_N()*neg->get_M(),num_features) } -float64_t* CTOPFeatures::compute_feature_vector( +float64_t* TOPFeatures::compute_feature_vector( int32_t num, int32_t &len, float64_t* target) const { float64_t* featurevector=target; @@ -94,7 +94,7 @@ float64_t* CTOPFeatures::compute_feature_vector( return featurevector; } -void CTOPFeatures::compute_feature_vector( +void TOPFeatures::compute_feature_vector( float64_t* featurevector, int32_t num, int32_t& len) const { int32_t i,j,p=0,x=num; @@ -183,7 +183,7 @@ void CTOPFeatures::compute_feature_vector( } } -float64_t* CTOPFeatures::set_feature_matrix() +float64_t* TOPFeatures::set_feature_matrix() { int32_t len=0; @@ -221,7 +221,7 @@ float64_t* CTOPFeatures::set_feature_matrix() return feature_matrix.matrix; } -bool CTOPFeatures::compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx) +bool TOPFeatures::compute_relevant_indizes(std::shared_ptr hmm, T_HMM_INDIZES* hmm_idx) { int32_t i=0; int32_t j=0; @@ -233,21 +233,21 @@ bool CTOPFeatures::compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx) for (i=0; iget_N(); i++) { - if (hmm->get_p(i)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_p(i)>Math::ALMOST_NEG_INFTY) hmm_idx->num_p++; - if (hmm->get_q(i)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_q(i)>Math::ALMOST_NEG_INFTY) hmm_idx->num_q++; for (j=0; jget_N(); j++) { - if (hmm->get_a(i,j)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_a(i,j)>Math::ALMOST_NEG_INFTY) hmm_idx->num_a++; } for (j=0; jget_M(); j++) { - if (hmm->get_b(i,j)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_b(i,j)>Math::ALMOST_NEG_INFTY) hmm_idx->num_b++; } } @@ -288,13 +288,13 @@ bool CTOPFeatures::compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx) for (i=0; iget_N(); i++) { - if (hmm->get_p(i)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_p(i)>Math::ALMOST_NEG_INFTY) { ASSERT(idx_p < hmm_idx->num_p) hmm_idx->idx_p[idx_p++]=i; } - if (hmm->get_q(i)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_q(i)>Math::ALMOST_NEG_INFTY) { ASSERT(idx_q < hmm_idx->num_q) hmm_idx->idx_q[idx_q++]=i; @@ -302,7 +302,7 @@ bool CTOPFeatures::compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx) for (j=0; jget_N(); j++) { - if (hmm->get_a(i,j)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_a(i,j)>Math::ALMOST_NEG_INFTY) { ASSERT(idx_a < hmm_idx->num_a) hmm_idx->idx_a_rows[idx_a]=i; @@ -312,7 +312,7 @@ bool CTOPFeatures::compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx) for (j=0; jget_M(); j++) { - if (hmm->get_b(i,j)>CMath::ALMOST_NEG_INFTY) + if (hmm->get_b(i,j)>Math::ALMOST_NEG_INFTY) { ASSERT(idx_b < hmm_idx->num_b) hmm_idx->idx_b_rows[idx_b]=i; @@ -324,7 +324,7 @@ bool CTOPFeatures::compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx) return true; } -int32_t CTOPFeatures::compute_num_features() +int32_t TOPFeatures::compute_num_features() { int32_t num=0; @@ -353,7 +353,7 @@ int32_t CTOPFeatures::compute_num_features() return num; } -void CTOPFeatures::init() +void TOPFeatures::init() { pos = NULL; neg = NULL; @@ -365,8 +365,8 @@ void CTOPFeatures::init() unset_generic(); //TODO serialize HMMs - //m_parameters->add((CSGObject**) &pos, "pos", "HMM for positive class."); - //m_parameters->add((CSGObject**) &neg, "neg", "HMM for negative class."); + //m_parameters->add((std::shared_ptr*) &pos, "pos", "HMM for positive class."); + //m_parameters->add((std::shared_ptr*) &neg, "neg", "HMM for negative class."); SG_ADD( &neglinear, "neglinear", "If negative HMM is a LinearHMM"); SG_ADD( diff --git a/src/shogun/features/TOPFeatures.h b/src/shogun/features/TOPFeatures.h index e308bca5b0e..7d97967500a 100644 --- a/src/shogun/features/TOPFeatures.h +++ b/src/shogun/features/TOPFeatures.h @@ -14,8 +14,8 @@ namespace shogun { -template class CDenseFeatures; -class CHMM; +template class DenseFeatures; +class HMM; #ifndef DOXYGEN_SHOULD_SKIP_THIS /** HMM indices */ @@ -59,14 +59,14 @@ struct T_HMM_INDIZES * Note that TOP-features are computed on the fly, so to be effective feature * caching should be enabled. * - * It inherits its functionality from CDenseFeatures, which should be + * It inherits its functionality from DenseFeatures, which should be * consulted for further reference. */ -class CTOPFeatures : public CDenseFeatures +class TOPFeatures : public DenseFeatures { public: /** default constructor */ - CTOPFeatures(); + TOPFeatures(); /** constructor * @@ -76,19 +76,19 @@ class CTOPFeatures : public CDenseFeatures * @param neglin if negative HMM is of linear shape * @param poslin if positive HMM is of linear shape */ - CTOPFeatures(int32_t size, CHMM* p, CHMM* n, bool neglin, bool poslin); + TOPFeatures(int32_t size, std::shared_ptr p, std::shared_ptr n, bool neglin, bool poslin); /** copy constructor */ - CTOPFeatures(const CTOPFeatures &orig); + TOPFeatures(const TOPFeatures &orig); - virtual ~CTOPFeatures(); + virtual ~TOPFeatures(); /** set HMMs * * @param p positive HMM * @param n negative HMM */ - void set_models(CHMM* p, CHMM* n); + void set_models(std::shared_ptr p, std::shared_ptr n); /** set feature matrix * @@ -108,7 +108,7 @@ class CTOPFeatures : public CDenseFeatures * @param hmm_idx HMM index * @return if computing was successful */ - bool compute_relevant_indizes(CHMM* hmm, T_HMM_INDIZES* hmm_idx); + bool compute_relevant_indizes(std::shared_ptr hmm, T_HMM_INDIZES* hmm_idx); /** @return object name */ virtual const char* get_name() const { return "TOPFeatures"; } @@ -137,9 +137,9 @@ class CTOPFeatures : public CDenseFeatures protected: /** positive HMM */ - CHMM* pos; + std::shared_ptr pos; /** negative HMM */ - CHMM* neg; + std::shared_ptr neg; /** if negative HMM is a LinearHMM */ bool neglinear; /** if positive HMM is a LinearHMM */ diff --git a/src/shogun/features/WDFeatures.cpp b/src/shogun/features/WDFeatures.cpp index 3dfd7c17f99..86fc906106e 100644 --- a/src/shogun/features/WDFeatures.cpp +++ b/src/shogun/features/WDFeatures.cpp @@ -10,7 +10,7 @@ using namespace shogun; -CWDFeatures::CWDFeatures() :CDotFeatures() +WDFeatures::WDFeatures() :DotFeatures() { unstable(SOURCE_LOCATION); @@ -26,19 +26,18 @@ CWDFeatures::CWDFeatures() :CDotFeatures() normalization_const = 0.0; } -CWDFeatures::CWDFeatures(CStringFeatures* str, - int32_t order, int32_t from_order) : CDotFeatures() +WDFeatures::WDFeatures(std::shared_ptr> str, + int32_t order, int32_t from_order) : DotFeatures() { ASSERT(str) ASSERT(str->have_same_length()) - SG_REF(str); + strings=str; string_length=str->get_max_vector_length(); num_strings=str->get_num_vectors(); - CAlphabet* alpha=str->get_alphabet(); + auto alpha=str->get_alphabet(); alphabet_size=alpha->get_num_symbols(); - SG_UNREF(alpha); degree=order; from_degree=from_order; @@ -48,20 +47,19 @@ CWDFeatures::CWDFeatures(CStringFeatures* str, } -CWDFeatures::CWDFeatures(const CWDFeatures& orig) - : CDotFeatures(orig), strings(orig.strings), +WDFeatures::WDFeatures(const WDFeatures& orig) + : DotFeatures(orig), strings(orig.strings), degree(orig.degree), from_degree(orig.from_degree), normalization_const(orig.normalization_const) { - SG_REF(strings); + if (strings) { string_length=strings->get_max_vector_length(); num_strings=strings->get_num_vectors(); - CAlphabet* alpha=strings->get_alphabet(); + auto alpha=strings->get_alphabet(); alphabet_size=alpha->get_num_symbols(); - SG_UNREF(alpha); } else { @@ -75,18 +73,18 @@ CWDFeatures::CWDFeatures(const CWDFeatures& orig) set_wd_weights(); } -CWDFeatures::~CWDFeatures() +WDFeatures::~WDFeatures() { - SG_UNREF(strings); + SG_FREE(wd_weights); } -float64_t CWDFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t WDFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CWDFeatures* wdf = (CWDFeatures*) df; + auto wdf = std::static_pointer_cast(df); int32_t len1, len2; bool free_vec1, free_vec2; @@ -109,18 +107,18 @@ float64_t CWDFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) } strings->free_feature_vector(vec1, vec_idx1, free_vec1); wdf->strings->free_feature_vector(vec2, vec_idx2, free_vec2); - return sum/CMath::sq(normalization_const); + return sum/Math::sq(normalization_const); } float64_t -CWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +WDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.size() == w_dim, "Dimensions don't match, vec2_dim={}, w_dim={}", vec2.size(), w_dim); float64_t sum=0; - int32_t lim=CMath::min(degree, string_length); + int32_t lim=Math::min(degree, string_length); int32_t len; bool free_vec1; uint8_t* vec = strings->get_feature_vector(vec_idx1, len, free_vec1); @@ -152,12 +150,12 @@ CWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return sum/normalization_const; } -void CWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void WDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len != w_dim) error("Dimensions don't match, vec2_dim={}, w_dim={}", vec2_len, w_dim); - int32_t lim=CMath::min(degree, string_length); + int32_t lim=Math::min(degree, string_length); int32_t len; bool free_vec1; uint8_t* vec = strings->get_feature_vector(vec_idx1, len, free_vec1); @@ -173,7 +171,7 @@ void CWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* float64_t wd = alpha*wd_weights[k]/normalization_const; if (abs_val) - wd=CMath::abs(wd); + wd=Math::abs(wd); int32_t o=offs; for (int32_t i=0; i+k < len; i++) @@ -191,7 +189,7 @@ void CWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* strings->free_feature_vector(vec, vec_idx1, free_vec1); } -void CWDFeatures::set_wd_weights() +void WDFeatures::set_wd_weights() { ASSERT(degree>0 && degree<=8) SG_FREE(wd_weights); @@ -200,14 +198,14 @@ void CWDFeatures::set_wd_weights() for (int32_t i=0; i=num_strings) { @@ -233,7 +231,7 @@ void* CWDFeatures::get_feature_iterator(int32_t vector_index) wd_feature_iterator* it=SG_MALLOC(wd_feature_iterator, 1); - it->lim=CMath::min(degree, string_length); + it->lim=Math::min(degree, string_length); it->vec= strings->get_feature_vector(vector_index, it->vlen, it->vfree); it->vidx=vector_index; @@ -251,7 +249,7 @@ void* CWDFeatures::get_feature_iterator(int32_t vector_index) return it; } -bool CWDFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool WDFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { wd_feature_iterator* it=(wd_feature_iterator*) iterator; @@ -289,7 +287,7 @@ bool CWDFeatures::get_next_feature(int32_t& index, float64_t& value, void* itera return true; } -void CWDFeatures::free_feature_iterator(void* iterator) +void WDFeatures::free_feature_iterator(void* iterator) { ASSERT(iterator) wd_feature_iterator* it=(wd_feature_iterator*) iterator; @@ -298,17 +296,17 @@ void CWDFeatures::free_feature_iterator(void* iterator) SG_FREE(it); } -CFeatures* CWDFeatures::duplicate() const +std::shared_ptr WDFeatures::duplicate() const { - return new CWDFeatures(*this); + return std::make_shared(*this); } -int32_t CWDFeatures::get_dim_feature_space() const +int32_t WDFeatures::get_dim_feature_space() const { return w_dim; } -int32_t CWDFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t WDFeatures::get_nnz_features_for_vector(int32_t num) const { int32_t vlen=-1; bool free_vec; @@ -317,27 +315,27 @@ int32_t CWDFeatures::get_nnz_features_for_vector(int32_t num) const return degree*vlen; } -EFeatureType CWDFeatures::get_feature_type() const +EFeatureType WDFeatures::get_feature_type() const { return F_UNKNOWN; } -EFeatureClass CWDFeatures::get_feature_class() const +EFeatureClass WDFeatures::get_feature_class() const { return C_WD; } -int32_t CWDFeatures::get_num_vectors() const +int32_t WDFeatures::get_num_vectors() const { return num_strings; } -float64_t CWDFeatures::get_normalization_const() +float64_t WDFeatures::get_normalization_const() { return normalization_const; } -void CWDFeatures::set_wd_weights(SGVector weights) +void WDFeatures::set_wd_weights(SGVector weights) { ASSERT(weights.vlen==degree) diff --git a/src/shogun/features/WDFeatures.h b/src/shogun/features/WDFeatures.h index cdfe1980eb1..4d66ca63176 100644 --- a/src/shogun/features/WDFeatures.h +++ b/src/shogun/features/WDFeatures.h @@ -16,18 +16,18 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /** @brief Features that compute the Weighted Degreee Kernel feature space * explicitly. * * \sa CWeightedDegreeStringKernel */ -class CWDFeatures : public CDotFeatures +class WDFeatures : public DotFeatures { public: /** defualt constructor */ - CWDFeatures(); + WDFeatures(); /** constructor * @@ -35,13 +35,13 @@ class CWDFeatures : public CDotFeatures * @param order of wd kernel * @param from_order use first order weights from higher order weighting */ - CWDFeatures(CStringFeatures* str, int32_t order, int32_t from_order); + WDFeatures(std::shared_ptr> str, int32_t order, int32_t from_order); /** copy constructor */ - CWDFeatures(const CWDFeatures & orig); + WDFeatures(const WDFeatures & orig); /** destructor */ - virtual ~CWDFeatures(); + virtual ~WDFeatures(); /** obtain the dimensionality of the feature space * @@ -59,7 +59,7 @@ class CWDFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -150,7 +150,7 @@ class CWDFeatures : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -187,7 +187,7 @@ class CWDFeatures : public CDotFeatures protected: /** stringfeatures the wdfeatures are based on*/ - CStringFeatures* strings; + std::shared_ptr> strings; /** degree */ int32_t degree; diff --git a/src/shogun/features/hashed/HashedDenseFeatures.cpp b/src/shogun/features/hashed/HashedDenseFeatures.cpp index 5a88f6c212a..b4be3eefb7b 100644 --- a/src/shogun/features/hashed/HashedDenseFeatures.cpp +++ b/src/shogun/features/hashed/HashedDenseFeatures.cpp @@ -18,89 +18,89 @@ namespace shogun { template -CHashedDenseFeatures::CHashedDenseFeatures(int32_t size, bool use_quadr, bool keep_lin_terms) -: CDotFeatures(size) +HashedDenseFeatures::HashedDenseFeatures(int32_t size, bool use_quadr, bool keep_lin_terms) +: DotFeatures(size) { init(NULL, 0, use_quadr, keep_lin_terms); } template -CHashedDenseFeatures::CHashedDenseFeatures(CDenseFeatures* feats, int32_t d, - bool use_quadr, bool keep_lin_terms) : CDotFeatures() +HashedDenseFeatures::HashedDenseFeatures(std::shared_ptr> feats, int32_t d, + bool use_quadr, bool keep_lin_terms) : DotFeatures() { init(feats, d, use_quadr, keep_lin_terms); } template -CHashedDenseFeatures::CHashedDenseFeatures(SGMatrix matrix, int32_t d, bool use_quadr, - bool keep_lin_terms) : CDotFeatures() +HashedDenseFeatures::HashedDenseFeatures(SGMatrix matrix, int32_t d, bool use_quadr, + bool keep_lin_terms) : DotFeatures() { - CDenseFeatures* feats = new CDenseFeatures(matrix); + auto feats = std::make_shared>(matrix); init(feats, d, use_quadr, keep_lin_terms); } template -CHashedDenseFeatures::CHashedDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec, - int32_t d, bool use_quadr, bool keep_lin_terms) : CDotFeatures() +HashedDenseFeatures::HashedDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec, + int32_t d, bool use_quadr, bool keep_lin_terms) : DotFeatures() { - CDenseFeatures* feats = new CDenseFeatures(src, num_feat, num_vec); + auto feats = std::make_shared>(src, num_feat, num_vec); init(feats, d, use_quadr, keep_lin_terms); } template -CHashedDenseFeatures::CHashedDenseFeatures(CFile* loader, int32_t d, bool use_quadr, - bool keep_lin_terms) : CDotFeatures(loader) +HashedDenseFeatures::HashedDenseFeatures(std::shared_ptr loader, int32_t d, bool use_quadr, + bool keep_lin_terms) : DotFeatures(loader) { - CDenseFeatures* feats = new CDenseFeatures(); + auto feats = std::make_shared>(); feats->load(loader); init(feats, d, use_quadr, keep_lin_terms); } template -void CHashedDenseFeatures::init(CDenseFeatures* feats, int32_t d, bool use_quadr, +void HashedDenseFeatures::init(std::shared_ptr> feats, int32_t d, bool use_quadr, bool keep_lin_terms) { dim = d; dense_feats = feats; - SG_REF(dense_feats); + use_quadratic = use_quadr; keep_linear_terms = keep_lin_terms; SG_ADD(&use_quadratic, "use_quadratic", "Whether to use quadratic features"); SG_ADD(&keep_linear_terms, "keep_linear_terms", "Whether to keep the linear terms or not"); SG_ADD(&dim, "dim", "Dimension of new feature space"); - SG_ADD((CSGObject** ) &dense_feats, "dense_feats", "Dense features to work on"); + SG_ADD((std::shared_ptr* ) &dense_feats, "dense_feats", "Dense features to work on"); set_generic(); } template -CHashedDenseFeatures::CHashedDenseFeatures(const CHashedDenseFeatures& orig) -: CDotFeatures(orig) +HashedDenseFeatures::HashedDenseFeatures(const HashedDenseFeatures& orig) +: DotFeatures(orig) { init(orig.dense_feats, orig.dim, orig.use_quadratic, orig.keep_linear_terms); } template -CHashedDenseFeatures::~CHashedDenseFeatures() +HashedDenseFeatures::~HashedDenseFeatures() { - SG_UNREF(dense_feats); + } template -CFeatures* CHashedDenseFeatures::duplicate() const +std::shared_ptr HashedDenseFeatures::duplicate() const { - return new CHashedDenseFeatures(*this); + return std::make_shared(*this); } template -int32_t CHashedDenseFeatures::get_dim_feature_space() const +int32_t HashedDenseFeatures::get_dim_feature_space() const { return dim; } template -float64_t CHashedDenseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, +float64_t HashedDenseFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) @@ -108,12 +108,12 @@ float64_t CHashedDenseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, ASSERT(df->get_feature_class() == get_feature_class()) ASSERT(strcmp(df->get_name(), get_name())==0) - CHashedDenseFeatures* feats = (CHashedDenseFeatures* ) df; + auto feats = std::static_pointer_cast>(df); ASSERT(feats->get_dim_feature_space() == get_dim_feature_space()) SGSparseVector vec_1 = get_hashed_feature_vector(vec_idx1); - bool same_vec = (df == this) && (vec_idx1 == vec_idx2); + bool same_vec = (df.get() == this) && (vec_idx1 == vec_idx2); SGSparseVector vec_2 = same_vec ? vec_1 : feats->get_hashed_feature_vector(vec_idx2); float64_t result = vec_1.sparse_dot(vec_2); @@ -121,7 +121,7 @@ float64_t CHashedDenseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, } template -float64_t CHashedDenseFeatures::dot( +float64_t HashedDenseFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { ASSERT(vec2.size() == dim) @@ -135,7 +135,7 @@ float64_t CHashedDenseFeatures::dot( for (index_t i=0; i::dot( for (index_t i=0; i::dot( } template -void CHashedDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, +void HashedDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { - float64_t val = abs_val ? CMath::abs(alpha) : alpha; + float64_t val = abs_val ? Math::abs(alpha) : alpha; ASSERT(vec2_len == dim) SGVector vec = dense_feats->get_feature_vector(vec_idx1); @@ -177,7 +177,7 @@ void CHashedDenseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx for (index_t i=0; i::add_to_dense_vec(float64_t alpha, int32_t vec_idx for (index_t i=0; i::add_to_dense_vec(float64_t alpha, int32_t vec_idx } template -int32_t CHashedDenseFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t HashedDenseFeatures::get_nnz_features_for_vector(int32_t num) const { return dim; } template -void* CHashedDenseFeatures::get_feature_iterator(int32_t vector_index) +void* HashedDenseFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION);; return NULL; } template -bool CHashedDenseFeatures::get_next_feature(int32_t& index, float64_t& value, +bool HashedDenseFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION);; return false; } template -void CHashedDenseFeatures::free_feature_iterator(void* iterator) +void HashedDenseFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION);; } template -const char* CHashedDenseFeatures::get_name() const +const char* HashedDenseFeatures::get_name() const { return "HashedDenseFeatures"; } template -EFeatureType CHashedDenseFeatures::get_feature_type() const +EFeatureType HashedDenseFeatures::get_feature_type() const { return F_UINT; } template -EFeatureClass CHashedDenseFeatures::get_feature_class() const +EFeatureClass HashedDenseFeatures::get_feature_class() const { return C_SPARSE; } template -int32_t CHashedDenseFeatures::get_num_vectors() const +int32_t HashedDenseFeatures::get_num_vectors() const { return dense_feats->get_num_vectors(); } template -SGSparseVector CHashedDenseFeatures::get_hashed_feature_vector(int32_t vec_idx) const +SGSparseVector HashedDenseFeatures::get_hashed_feature_vector(int32_t vec_idx) const { SGVector vec = dense_feats->get_feature_vector(vec_idx); - SGSparseVector hashed_vec = CHashedDenseFeatures::hash_vector( + SGSparseVector hashed_vec = HashedDenseFeatures::hash_vector( vec, dim, use_quadratic, keep_linear_terms); dense_feats->free_feature_vector(vec, vec_idx); return hashed_vec; } template -SGSparseVector CHashedDenseFeatures::hash_vector(SGVector vec, int32_t dim, +SGSparseVector HashedDenseFeatures::hash_vector(SGVector vec, int32_t dim, bool use_quadratic, bool keep_linear_terms) { SGVector h_vec(dim); @@ -275,7 +275,7 @@ SGSparseVector CHashedDenseFeatures::hash_vector(SGVector vec, int32 for (index_t i=0; i CHashedDenseFeatures::hash_vector(SGVector vec, int32 for (index_t i=0; i CHashedDenseFeatures::hash_vector(SGVector vec, int32 return hashed_vector; } -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; -template class CHashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; +template class HashedDenseFeatures; } diff --git a/src/shogun/features/hashed/HashedDenseFeatures.h b/src/shogun/features/hashed/HashedDenseFeatures.h index 2b71c5e643f..0c083acbe71 100644 --- a/src/shogun/features/hashed/HashedDenseFeatures.h +++ b/src/shogun/features/hashed/HashedDenseFeatures.h @@ -14,13 +14,13 @@ namespace shogun { -template class CDenseFeatures; -class CDotFeatures; +template class DenseFeatures; +class DotFeatures; -/** @brief This class is identical to the CDenseFeatures class +/** @brief This class is identical to the DenseFeatures class * except that it hashes each dimension to a new feature space. */ -template class CHashedDenseFeatures : public CDotFeatures +template class HashedDenseFeatures : public DotFeatures { public: @@ -30,7 +30,7 @@ template class CHashedDenseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedDenseFeatures(int32_t size=0, bool use_quadr = false, bool keep_lin_terms = true); + HashedDenseFeatures(int32_t size=0, bool use_quadr = false, bool keep_lin_terms = true); /** constructor * @@ -39,7 +39,7 @@ template class CHashedDenseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedDenseFeatures(CDenseFeatures* feats, int32_t d, bool use_quadr = false, + HashedDenseFeatures(std::shared_ptr> feats, int32_t d, bool use_quadr = false, bool keep_lin_terms = true); /** constructor @@ -49,7 +49,7 @@ template class CHashedDenseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedDenseFeatures(SGMatrix matrix, int32_t dm, bool use_quadr = false, + HashedDenseFeatures(SGMatrix matrix, int32_t dm, bool use_quadr = false, bool keep_lin_terms = true); /** constructor @@ -61,7 +61,7 @@ template class CHashedDenseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec, int32_t d, + HashedDenseFeatures(ST* src, int32_t num_feat, int32_t num_vec, int32_t d, bool use_quadr = false, bool keep_lin_terms = true); /** constructor loading features from file @@ -71,17 +71,17 @@ template class CHashedDenseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedDenseFeatures(CFile* loader, int32_t d, bool use_quadr = false, + HashedDenseFeatures(std::shared_ptr loader, int32_t d, bool use_quadr = false, bool keep_lin_terms = false); /** copy constructor */ - CHashedDenseFeatures(const CHashedDenseFeatures& orig); + HashedDenseFeatures(const HashedDenseFeatures& orig); /** duplicate */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** destructor */ - virtual ~CHashedDenseFeatures(); + virtual ~HashedDenseFeatures(); /** obtain the dimensionality of the feature space * @@ -101,7 +101,7 @@ template class CHashedDenseFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector @@ -209,12 +209,12 @@ template class CHashedDenseFeatures : public CDotFeatures bool keep_linear_terms = true); private: - void init(CDenseFeatures* feats, int32_t d, bool use_quadr, bool keep_lin_terms); + void init(std::shared_ptr> feats, int32_t d, bool use_quadr, bool keep_lin_terms); protected: /** dense features */ - CDenseFeatures* dense_feats; + std::shared_ptr> dense_feats; /** new feature space dimension */ int32_t dim; diff --git a/src/shogun/features/hashed/HashedDocDotFeatures.cpp b/src/shogun/features/hashed/HashedDocDotFeatures.cpp index 7370ba34db2..9fe1c667c63 100644 --- a/src/shogun/features/hashed/HashedDocDotFeatures.cpp +++ b/src/shogun/features/hashed/HashedDocDotFeatures.cpp @@ -13,8 +13,8 @@ namespace shogun { -CHashedDocDotFeatures::CHashedDocDotFeatures(int32_t hash_bits, CStringFeatures* docs, - CTokenizer* tzer, bool normalize, int32_t n_grams, int32_t skips, int32_t size) : CDotFeatures(size) +HashedDocDotFeatures::HashedDocDotFeatures(int32_t hash_bits, std::shared_ptr> docs, + std::shared_ptr tzer, bool normalize, int32_t n_grams, int32_t skips, int32_t size) : DotFeatures(size) { if (n_grams < 1) n_grams = 1; @@ -25,20 +25,20 @@ CHashedDocDotFeatures::CHashedDocDotFeatures(int32_t hash_bits, CStringFeatures< init(hash_bits, docs, tzer, normalize, n_grams, skips); } -CHashedDocDotFeatures::CHashedDocDotFeatures(const CHashedDocDotFeatures& orig) -: CDotFeatures(orig) +HashedDocDotFeatures::HashedDocDotFeatures(const HashedDocDotFeatures& orig) +: DotFeatures(orig) { init(orig.num_bits, orig.doc_collection, orig.tokenizer, orig.should_normalize, orig.ngrams, orig.tokens_to_skip); } -CHashedDocDotFeatures::CHashedDocDotFeatures(CFile* loader) +HashedDocDotFeatures::HashedDocDotFeatures(std::shared_ptr loader) { not_implemented(SOURCE_LOCATION);; } -void CHashedDocDotFeatures::init(int32_t hash_bits, CStringFeatures* docs, - CTokenizer* tzer, bool normalize, int32_t n_grams, int32_t skips) +void HashedDocDotFeatures::init(int32_t hash_bits, std::shared_ptr> docs, + std::shared_ptr tzer, bool normalize, int32_t n_grams, int32_t skips) { num_bits = hash_bits; ngrams = n_grams; @@ -49,43 +49,44 @@ void CHashedDocDotFeatures::init(int32_t hash_bits, CStringFeatures* docs, if (!tokenizer) { - tokenizer = new CDelimiterTokenizer(); - ((CDelimiterTokenizer* )tokenizer)->init_for_whitespace(); + auto dt = std::make_shared(); + dt->init_for_whitespace(); + tokenizer = dt; } SG_ADD(&num_bits, "num_bits", "Number of bits of hash"); SG_ADD(&ngrams, "ngrams", "Number of tokens to combine for quadratic feature support"); SG_ADD(&tokens_to_skip, "tokens_to_skip", "Number of tokens to skip when combining features"); - SG_ADD((CSGObject**) &doc_collection, "doc_collection", "Document collection"); - SG_ADD((CSGObject**) &tokenizer, "tokenizer", "Document tokenizer"); + SG_ADD((std::shared_ptr*) &doc_collection, "doc_collection", "Document collection"); + SG_ADD((std::shared_ptr*) &tokenizer, "tokenizer", "Document tokenizer"); SG_ADD(&should_normalize, "should_normalize", "Normalize or not the dot products"); - SG_REF(doc_collection); - SG_REF(tokenizer); + + } -CHashedDocDotFeatures::~CHashedDocDotFeatures() +HashedDocDotFeatures::~HashedDocDotFeatures() { - SG_UNREF(doc_collection); - SG_UNREF(tokenizer); + + } -int32_t CHashedDocDotFeatures::get_dim_feature_space() const +int32_t HashedDocDotFeatures::get_dim_feature_space() const { - return CMath::pow(2, num_bits); + return Math::pow(2, num_bits); } -float64_t CHashedDocDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t HashedDocDotFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_name() == get_name()) - CHashedDocDotFeatures* hddf = (CHashedDocDotFeatures*) df; + auto hddf = std::static_pointer_cast(df); SGVector sv1 = doc_collection->get_feature_vector(vec_idx1); SGVector sv2 = hddf->doc_collection->get_feature_vector(vec_idx2); - CHashedDocConverter* converter = new CHashedDocConverter(tokenizer, num_bits, + auto converter = std::make_shared(tokenizer, num_bits, should_normalize, ngrams, tokens_to_skip); SGSparseVector cv1 = converter->apply(sv1); SGSparseVector cv2 = converter->apply(sv2); @@ -93,12 +94,12 @@ float64_t CHashedDocDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t doc_collection->free_feature_vector(sv1, vec_idx1); hddf->doc_collection->free_feature_vector(sv2, vec_idx2); - SG_UNREF(converter); + return result; } -float64_t CHashedDocDotFeatures::dot( +float64_t HashedDocDotFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { ASSERT(vec2.size() == std::pow(2,num_bits)) @@ -117,7 +118,7 @@ float64_t CHashedDocDotFeatures::dot( SGVector hashed_indices((ngrams-1)*(tokens_to_skip+1) + 1); float64_t result = 0; - CTokenizer* local_tzer = tokenizer->get_copy(); + auto local_tzer = tokenizer->get_copy(); /** Reading n+k-1 tokens */ const int32_t seed = 0xdeadbeaf; @@ -126,7 +127,7 @@ float64_t CHashedDocDotFeatures::dot( while (hashes_endhas_next()) { index_t end = local_tzer->next_token_idx(start); - uint32_t token_hash = CHash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); + uint32_t token_hash = Hash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); hashes[hashes_end++] = token_hash; } @@ -134,10 +135,10 @@ float64_t CHashedDocDotFeatures::dot( while (local_tzer->has_next()) { index_t end = local_tzer->next_token_idx(start); - uint32_t token_hash = CHash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); + uint32_t token_hash = Hash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); hashes[hashes_end] = token_hash; - CHashedDocConverter::generate_ngram_hashes(hashes, hashes_start, len, hashed_indices, + HashedDocConverter::generate_ngram_hashes(hashes, hashes_start, len, hashed_indices, num_bits, ngrams, tokens_to_skip); for (index_t i=0; ifree_feature_vector(sv, vec_idx1); - SG_UNREF(local_tzer); + return should_normalize ? result / std::sqrt((float64_t)sv.size()) : result; } -void CHashedDocDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, +void HashedDocDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { - ASSERT(vec2_len == CMath::pow(2,num_bits)) + ASSERT(vec2_len == Math::pow(2,num_bits)) if (abs_val) - alpha = CMath::abs(alpha); + alpha = Math::abs(alpha); SGVector sv = doc_collection->get_feature_vector(vec_idx1); const float64_t value = @@ -195,7 +196,7 @@ void CHashedDocDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, * stored here to avoid creating new objects */ SGVector hashed_indices((ngrams-1)*(tokens_to_skip+1) + 1); - CTokenizer* local_tzer = tokenizer->get_copy(); + auto local_tzer = tokenizer->get_copy(); /** Reading n+k-1 tokens */ const int32_t seed = 0xdeadbeaf; @@ -204,17 +205,17 @@ void CHashedDocDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, while (hashes_endhas_next()) { index_t end = local_tzer->next_token_idx(start); - uint32_t token_hash = CHash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); + uint32_t token_hash = Hash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); hashes[hashes_end++] = token_hash; } while (local_tzer->has_next()) { index_t end = local_tzer->next_token_idx(start); - uint32_t token_hash = CHash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); + uint32_t token_hash = Hash::MurmurHash3((uint8_t* ) &sv.vector[start], end-start, seed); hashes[hashes_end] = token_hash; - CHashedDocConverter::generate_ngram_hashes(hashes, hashes_start, len, hashed_indices, + HashedDocConverter::generate_ngram_hashes(hashes, hashes_start, len, hashed_indices, num_bits, ngrams, tokens_to_skip); for (index_t i=0; ifree_feature_vector(sv, vec_idx1); - SG_UNREF(local_tzer); + } -uint32_t CHashedDocDotFeatures::calculate_token_hash(char* token, +uint32_t HashedDocDotFeatures::calculate_token_hash(char* token, int32_t length, int32_t num_bits, uint32_t seed) { - int32_t hash = CHash::MurmurHash3((uint8_t* ) token, length, seed); + int32_t hash = Hash::MurmurHash3((uint8_t* ) token, length, seed); return hash & ((1 << num_bits) - 1); } -void CHashedDocDotFeatures::set_doc_collection(CStringFeatures* docs) +void HashedDocDotFeatures::set_doc_collection(std::shared_ptr> docs) { - SG_UNREF(doc_collection); + doc_collection = docs; } -int32_t CHashedDocDotFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t HashedDocDotFeatures::get_nnz_features_for_vector(int32_t num) const { SGVector sv = doc_collection->get_feature_vector(num); int32_t num_nnz_features = sv.size(); @@ -270,44 +271,44 @@ int32_t CHashedDocDotFeatures::get_nnz_features_for_vector(int32_t num) const return num_nnz_features; } -void* CHashedDocDotFeatures::get_feature_iterator(int32_t vector_index) +void* HashedDocDotFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION);; return NULL; } -bool CHashedDocDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool HashedDocDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION);; return false; } -void CHashedDocDotFeatures::free_feature_iterator(void* iterator) +void HashedDocDotFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION);; } -const char* CHashedDocDotFeatures::get_name() const +const char* HashedDocDotFeatures::get_name() const { return "HashedDocDotFeatures"; } -CFeatures* CHashedDocDotFeatures::duplicate() const +std::shared_ptr HashedDocDotFeatures::duplicate() const { - return new CHashedDocDotFeatures(*this); + return std::make_shared(*this); } -EFeatureType CHashedDocDotFeatures::get_feature_type() const +EFeatureType HashedDocDotFeatures::get_feature_type() const { return F_UINT; } -EFeatureClass CHashedDocDotFeatures::get_feature_class() const +EFeatureClass HashedDocDotFeatures::get_feature_class() const { return C_SPARSE; } -int32_t CHashedDocDotFeatures::get_num_vectors() const +int32_t HashedDocDotFeatures::get_num_vectors() const { return doc_collection->get_num_vectors(); } diff --git a/src/shogun/features/hashed/HashedDocDotFeatures.h b/src/shogun/features/hashed/HashedDocDotFeatures.h index 9bb157ce9c5..e03190cb51d 100644 --- a/src/shogun/features/hashed/HashedDocDotFeatures.h +++ b/src/shogun/features/hashed/HashedDocDotFeatures.h @@ -15,11 +15,11 @@ #include namespace shogun { -template class CStringFeatures; +template class StringFeatures; template class SGMatrix; -class CDotFeatures; -class CHashedDocConverter; -class CTokenizer; +class DotFeatures; +class HashedDocConverter; +class Tokenizer; /** @brief This class can be used to provide on-the-fly vectorization of a document collection. * Like in the standard Bag-of-Words representation, this class considers each document as a collection of tokens, @@ -31,7 +31,7 @@ class CTokenizer; * Eg. for the tokens ["a", "b", "c", "d"], with n_grams = 2 and skips = 2, one would get the following combinations : * ["a", "ab", "ac" (skipped 1), "ad" (skipped 2), "b", "bc", "bd" (skipped 1), "c", "cd", "d"]. */ -class CHashedDocDotFeatures: public CDotFeatures +class HashedDocDotFeatures: public DotFeatures { public: @@ -45,20 +45,20 @@ class CHashedDocDotFeatures: public CDotFeatures * @param skips max number of tokens to skip when combining tokens * @param size cache size */ - CHashedDocDotFeatures(int32_t hash_bits=0, CStringFeatures* docs=NULL, - CTokenizer* tzer=NULL, bool normalize=true, int32_t n_grams=1, int32_t skips=0, int32_t size=0); + HashedDocDotFeatures(int32_t hash_bits=0, std::shared_ptr> docs=NULL, + std::shared_ptr tzer=NULL, bool normalize=true, int32_t n_grams=1, int32_t skips=0, int32_t size=0); /** copy constructor */ - CHashedDocDotFeatures(const CHashedDocDotFeatures& orig); + HashedDocDotFeatures(const HashedDocDotFeatures& orig); /** constructor * * @param loader File object via which to load data */ - CHashedDocDotFeatures(CFile* loader); + HashedDocDotFeatures(std::shared_ptr loader); /** destructor */ - virtual ~CHashedDocDotFeatures(); + virtual ~HashedDocDotFeatures(); /** obtain the dimensionality of the feature space * @@ -76,7 +76,7 @@ class CHashedDocDotFeatures: public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -142,7 +142,7 @@ class CHashedDocDotFeatures: public CDotFeatures * * @param docs the document collection */ - void set_doc_collection(CStringFeatures* docs); + void set_doc_collection(std::shared_ptr> docs); virtual const char* get_name() const; @@ -150,7 +150,7 @@ class CHashedDocDotFeatures: public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -182,18 +182,18 @@ class CHashedDocDotFeatures: public CDotFeatures int32_t num_bits, uint32_t seed); private: - void init(int32_t hash_bits, CStringFeatures* docs, CTokenizer* tzer, + void init(int32_t hash_bits, std::shared_ptr> docs, std::shared_ptr tzer, bool normalize, int32_t n_grams, int32_t skips); protected: /** the document collection*/ - CStringFeatures* doc_collection; + std::shared_ptr> doc_collection; /** number of bits of hash */ int32_t num_bits; /** tokenizer */ - CTokenizer* tokenizer; + std::shared_ptr tokenizer; /** if should normalize the dot product results */ bool should_normalize; diff --git a/src/shogun/features/hashed/HashedDocDotFeatures_benchmark.cc b/src/shogun/features/hashed/HashedDocDotFeatures_benchmark.cc index fe000c2e3f8..83e153e7b89 100644 --- a/src/shogun/features/hashed/HashedDocDotFeatures_benchmark.cc +++ b/src/shogun/features/hashed/HashedDocDotFeatures_benchmark.cc @@ -33,8 +33,8 @@ class HDFixture : public benchmark::Fixture string_list[i].vector[j] = (char) uniform_int_dist(prng); } auto string_feats = new CStringFeatures(string_list, RAWBYTE); - auto tzer = new CNGramTokenizer(3); - f = std::make_shared(st.range(0), string_feats, tzer); + auto tzer = new NGramTokenizer(3); + f = std::make_shared(st.range(0), string_feats, tzer); w = SGVector(f->get_dim_feature_space()); w.range_fill(17.0); @@ -42,7 +42,7 @@ class HDFixture : public benchmark::Fixture void TearDown(const ::benchmark::State&) { f.reset(); } - std::shared_ptr f; + std::shared_ptr f; static constexpr index_t num_strings = 5000; static constexpr index_t max_str_length = 10000; diff --git a/src/shogun/features/hashed/HashedSparseFeatures.cpp b/src/shogun/features/hashed/HashedSparseFeatures.cpp index b0f302339e4..d10bc14e9bf 100644 --- a/src/shogun/features/hashed/HashedSparseFeatures.cpp +++ b/src/shogun/features/hashed/HashedSparseFeatures.cpp @@ -18,96 +18,96 @@ namespace shogun { template -CHashedSparseFeatures::CHashedSparseFeatures(int32_t size, bool use_quadr, - bool keep_lin_terms) : CDotFeatures(size) +HashedSparseFeatures::HashedSparseFeatures(int32_t size, bool use_quadr, + bool keep_lin_terms) : DotFeatures(size) { init(NULL, 0, use_quadr, keep_lin_terms); } template -CHashedSparseFeatures::CHashedSparseFeatures(CSparseFeatures* feats, int32_t d, - bool use_quadr, bool keep_lin_terms) : CDotFeatures() +HashedSparseFeatures::HashedSparseFeatures(std::shared_ptr> feats, int32_t d, + bool use_quadr, bool keep_lin_terms) : DotFeatures() { init(feats, d, use_quadr, keep_lin_terms); } template -CHashedSparseFeatures::CHashedSparseFeatures(SGSparseMatrix matrix, int32_t d, - bool use_quadr, bool keep_lin_terms) : CDotFeatures() +HashedSparseFeatures::HashedSparseFeatures(SGSparseMatrix matrix, int32_t d, + bool use_quadr, bool keep_lin_terms) : DotFeatures() { - CSparseFeatures* feats = new CSparseFeatures(matrix); + auto feats = std::make_shared>(matrix); init(feats, d, use_quadr, keep_lin_terms); } template -CHashedSparseFeatures::CHashedSparseFeatures(CFile* loader, int32_t d, bool use_quadr, - bool keep_lin_terms) : CDotFeatures(loader) +HashedSparseFeatures::HashedSparseFeatures(std::shared_ptr loader, int32_t d, bool use_quadr, + bool keep_lin_terms) : DotFeatures(loader) { - CSparseFeatures* feats = new CSparseFeatures(); + auto feats = std::make_shared>(); feats->load(loader); init(feats, d, use_quadr, keep_lin_terms); } template -void CHashedSparseFeatures::init(CSparseFeatures* feats, int32_t d, bool use_quadr, +void HashedSparseFeatures::init(std::shared_ptr> feats, int32_t d, bool use_quadr, bool keep_lin_terms) { dim = d; use_quadratic = use_quadr; keep_linear_terms = keep_lin_terms; sparse_feats = feats; - SG_REF(sparse_feats); + SG_ADD(&use_quadratic, "use_quadratic", "Whether to use quadratic features"); SG_ADD(&keep_linear_terms, "keep_linear_terms", "Whether to keep the linear terms or not"); SG_ADD(&dim, "dim", "Dimension of new feature space"); - SG_ADD((CSGObject** ) &sparse_feats, "sparse_feats", "Sparse features to work on"); + SG_ADD((std::shared_ptr* ) &sparse_feats, "sparse_feats", "Sparse features to work on"); set_generic(); } template -CHashedSparseFeatures::CHashedSparseFeatures(const CHashedSparseFeatures& orig) -: CDotFeatures(orig) +HashedSparseFeatures::HashedSparseFeatures(const HashedSparseFeatures& orig) +: DotFeatures(orig) { init(orig.sparse_feats, orig.dim, orig.use_quadratic, orig.keep_linear_terms); } template -CHashedSparseFeatures::~CHashedSparseFeatures() +HashedSparseFeatures::~HashedSparseFeatures() { - SG_UNREF(sparse_feats); + } template -CFeatures* CHashedSparseFeatures::duplicate() const +std::shared_ptr HashedSparseFeatures::duplicate() const { - return new CHashedSparseFeatures(*this); + return std::make_shared(*this); } template -int32_t CHashedSparseFeatures::get_dim_feature_space() const +int32_t HashedSparseFeatures::get_dim_feature_space() const { return dim; } template -SGSparseVector CHashedSparseFeatures::get_hashed_feature_vector( +SGSparseVector HashedSparseFeatures::get_hashed_feature_vector( int32_t vec_idx) const { - return CHashedSparseFeatures::hash_vector(sparse_feats->get_sparse_feature_vector(vec_idx), + return HashedSparseFeatures::hash_vector(sparse_feats->get_sparse_feature_vector(vec_idx), dim, use_quadratic, keep_linear_terms); } template -SGSparseVector CHashedSparseFeatures::hash_vector(SGVector vec, int32_t dim, +SGSparseVector HashedSparseFeatures::hash_vector(SGVector vec, int32_t dim, bool use_quadratic, bool keep_linear_terms) { - return CHashedDenseFeatures::hash_vector(vec, dim, use_quadratic, keep_linear_terms); + return HashedDenseFeatures::hash_vector(vec, dim, use_quadratic, keep_linear_terms); } template -SGSparseVector CHashedSparseFeatures::hash_vector(SGSparseVector vec, int32_t dim, +SGSparseVector HashedSparseFeatures::hash_vector(SGSparseVector vec, int32_t dim, bool use_quadratic, bool keep_linear_terms) { SGVector h_vec(dim); @@ -118,7 +118,7 @@ SGSparseVector CHashedSparseFeatures::hash_vector(SGSparseVector vec for (index_t i=0; i CHashedSparseFeatures::hash_vector(SGSparseVector vec for (index_t i=0; i CHashedSparseFeatures::hash_vector(SGSparseVector vec } template -float64_t CHashedSparseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, +float64_t HashedSparseFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) @@ -177,7 +177,7 @@ float64_t CHashedSparseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, ASSERT(df->get_feature_class() == get_feature_class()) ASSERT(strcmp(df->get_name(), get_name())==0) - CHashedSparseFeatures* feats = (CHashedSparseFeatures* ) df; + auto feats = std::dynamic_pointer_cast>(df); SGSparseVector vec_1 = get_hashed_feature_vector(vec_idx1); SGSparseVector vec_2 = feats->get_hashed_feature_vector(vec_idx2); @@ -186,7 +186,7 @@ float64_t CHashedSparseFeatures::dot(int32_t vec_idx1, CDotFeatures* df, } template -float64_t CHashedSparseFeatures::dot( +float64_t HashedSparseFeatures::dot( int32_t vec_idx1, const SGVector& vec2) const { ASSERT(vec2.size() == dim) @@ -199,7 +199,7 @@ float64_t CHashedSparseFeatures::dot( float64_t result = 0; for (index_t i=0; i::dot( for (index_t i=0; i::dot( } template -void CHashedSparseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, +void HashedSparseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { - float64_t val = abs_val ? CMath::abs(alpha) : alpha; + float64_t val = abs_val ? Math::abs(alpha) : alpha; ASSERT(vec2_len == dim) SGSparseVector vec = sparse_feats->get_sparse_feature_vector(vec_idx1); @@ -245,7 +245,7 @@ void CHashedSparseFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_id for (index_t i=0; i::add_to_dense_vec(float64_t alpha, int32_t vec_id for (index_t i=0; i::add_to_dense_vec(float64_t alpha, int32_t vec_id } template -int32_t CHashedSparseFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t HashedSparseFeatures::get_nnz_features_for_vector(int32_t num) const { return dim; } template -void* CHashedSparseFeatures::get_feature_iterator(int32_t vector_index) +void* HashedSparseFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION);; return NULL; } template -bool CHashedSparseFeatures::get_next_feature(int32_t& index, float64_t& value, +bool HashedSparseFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION);; return false; } template -void CHashedSparseFeatures::free_feature_iterator(void* iterator) +void HashedSparseFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION);; } template -const char* CHashedSparseFeatures::get_name() const +const char* HashedSparseFeatures::get_name() const { return "HashedSparseFeatures"; } template -EFeatureType CHashedSparseFeatures::get_feature_type() const +EFeatureType HashedSparseFeatures::get_feature_type() const { return F_UINT; } template -EFeatureClass CHashedSparseFeatures::get_feature_class() const +EFeatureClass HashedSparseFeatures::get_feature_class() const { return C_SPARSE; } template -int32_t CHashedSparseFeatures::get_num_vectors() const +int32_t HashedSparseFeatures::get_num_vectors() const { return sparse_feats ->get_num_vectors(); } -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; -template class CHashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; +template class HashedSparseFeatures ; } diff --git a/src/shogun/features/hashed/HashedSparseFeatures.h b/src/shogun/features/hashed/HashedSparseFeatures.h index 74ddc100ab6..af83b0e54d8 100644 --- a/src/shogun/features/hashed/HashedSparseFeatures.h +++ b/src/shogun/features/hashed/HashedSparseFeatures.h @@ -15,14 +15,14 @@ namespace shogun { -template class CSparseFeatures; +template class SparseFeatures; template class SGSparseVector; -class CDotFeatures; +class DotFeatures; -/** @brief This class is identical to the CDenseFeatures class +/** @brief This class is identical to the DenseFeatures class * except that it hashes each dimension to a new feature space. */ -template class CHashedSparseFeatures : public CDotFeatures +template class HashedSparseFeatures : public DotFeatures { public: @@ -32,7 +32,7 @@ template class CHashedSparseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedSparseFeatures(int32_t size=0, bool use_quadr = false, bool keep_lin_terms = true); + HashedSparseFeatures(int32_t size=0, bool use_quadr = false, bool keep_lin_terms = true); /** constructor * @@ -41,7 +41,7 @@ template class CHashedSparseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedSparseFeatures(CSparseFeatures* feats, int32_t d, bool use_quadr = false, + HashedSparseFeatures(std::shared_ptr> feats, int32_t d, bool use_quadr = false, bool keep_lin_terms = true); /** constructor @@ -51,7 +51,7 @@ template class CHashedSparseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedSparseFeatures(SGSparseMatrix matrix, int32_t d, bool use_quadr = false, + HashedSparseFeatures(SGSparseMatrix matrix, int32_t d, bool use_quadr = false, bool keep_lin_terms = true); /** constructor loading features from file @@ -61,17 +61,17 @@ template class CHashedSparseFeatures : public CDotFeatures * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CHashedSparseFeatures(CFile* loader, int32_t d, bool use_quadr = false, + HashedSparseFeatures(std::shared_ptr loader, int32_t d, bool use_quadr = false, bool keep_lin_terms = true); /** copy constructor */ - CHashedSparseFeatures(const CHashedSparseFeatures & orig); + HashedSparseFeatures(const HashedSparseFeatures & orig); /** duplicate */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** destructor */ - virtual ~CHashedSparseFeatures(); + virtual ~HashedSparseFeatures(); /** obtain the dimensionality of the feature space * @@ -91,7 +91,7 @@ template class CHashedSparseFeatures : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector @@ -210,12 +210,12 @@ template class CHashedSparseFeatures : public CDotFeatures bool use_quadratic = false, bool keep_linear_terms = true); private: - void init(CSparseFeatures* feats, int32_t d, bool use_quadr, bool keep_lin_terms); + void init(std::shared_ptr> feats, int32_t d, bool use_quadr, bool keep_lin_terms); protected: /** sparse features */ - CSparseFeatures* sparse_feats; + std::shared_ptr> sparse_feats; /** new feature space dimension */ int32_t dim; diff --git a/src/shogun/features/hashed/HashedWDFeatures.cpp b/src/shogun/features/hashed/HashedWDFeatures.cpp index 9119d819f96..f1d9d4a0f80 100644 --- a/src/shogun/features/hashed/HashedWDFeatures.cpp +++ b/src/shogun/features/hashed/HashedWDFeatures.cpp @@ -9,7 +9,7 @@ using namespace shogun; -CHashedWDFeatures::CHashedWDFeatures() :CDotFeatures() +HashedWDFeatures::HashedWDFeatures() :DotFeatures() { unstable(SOURCE_LOCATION); @@ -30,9 +30,9 @@ CHashedWDFeatures::CHashedWDFeatures() :CDotFeatures() normalization_const = 0.0; } -CHashedWDFeatures::CHashedWDFeatures(CStringFeatures* str, +HashedWDFeatures::HashedWDFeatures(std::shared_ptr> str, int32_t start_order, int32_t order, int32_t from_order, - int32_t hash_bits) : CDotFeatures() + int32_t hash_bits) : DotFeatures() { ASSERT(start_order>=0) ASSERT(start_order* str, ASSERT(hash_bits>0) ASSERT(str) ASSERT(str->have_same_length()) - SG_REF(str); + strings=str; string_length=str->get_max_vector_length(); num_strings=str->get_num_vectors(); - CAlphabet* alpha=str->get_alphabet(); + auto alpha=str->get_alphabet(); alphabet_size=alpha->get_num_symbols(); - SG_UNREF(alpha); degree=order; start_degree=start_order; @@ -57,22 +56,21 @@ CHashedWDFeatures::CHashedWDFeatures(CStringFeatures* str, set_normalization_const(); } -CHashedWDFeatures::CHashedWDFeatures(const CHashedWDFeatures& orig) - : CDotFeatures(orig), strings(orig.strings), +HashedWDFeatures::HashedWDFeatures(const HashedWDFeatures& orig) + : DotFeatures(orig), strings(orig.strings), degree(orig.degree), start_degree(orig.start_degree), from_degree(orig.from_degree), m_hash_bits(orig.m_hash_bits), normalization_const(orig.normalization_const) { - SG_REF(strings); + if (strings) { string_length=strings->get_max_vector_length(); num_strings=strings->get_num_vectors(); - CAlphabet* alpha=strings->get_alphabet(); + auto alpha=strings->get_alphabet(); alphabet_size=alpha->get_num_symbols(); - SG_UNREF(alpha); } else { @@ -85,18 +83,18 @@ CHashedWDFeatures::CHashedWDFeatures(const CHashedWDFeatures& orig) set_wd_weights(); } -CHashedWDFeatures::~CHashedWDFeatures() +HashedWDFeatures::~HashedWDFeatures() { - SG_UNREF(strings); + SG_FREE(wd_weights); } -float64_t CHashedWDFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t HashedWDFeatures::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CHashedWDFeatures* wdf = (CHashedWDFeatures*) df; + auto wdf = std::static_pointer_cast(df); int32_t len1, len2; bool free_vec1, free_vec2; @@ -120,18 +118,18 @@ float64_t CHashedWDFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec } strings->free_feature_vector(vec1, vec_idx1, free_vec1); wdf->strings->free_feature_vector(vec2, vec_idx2, free_vec2); - return sum/CMath::sq(normalization_const); + return sum/Math::sq(normalization_const); } float64_t -CHashedWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const +HashedWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { require( vec2.size() == w_dim, "Dimensions don't match, vec2_dim={}, w_dim={}", vec2.size(), w_dim); float64_t sum=0; - int32_t lim=CMath::min(degree, string_length); + int32_t lim=Math::min(degree, string_length); int32_t len; bool free_vec1; uint8_t* vec = strings->get_feature_vector(vec_idx1, len, free_vec1); @@ -143,7 +141,7 @@ CHashedWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const { // compute hash for strings of length start_degree-1 for (int32_t i=0; i+start_degree < len; i++) - val[i]=CHash::MurmurHash3(&vec[i], start_degree, 0xDEADBEAF); + val[i]=Hash::MurmurHash3(&vec[i], start_degree, 0xDEADBEAF); } else SGVector::fill_vector(val, len, 0xDEADBEAF); @@ -159,9 +157,9 @@ CHashedWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const for (int32_t i=0; i+k < len; i++) { chunk++; - CHash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); + Hash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); uint32_t h = - CHash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); + Hash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); #ifdef DEBUG_HASHEDWD io::print("vec[i]={}, k={}, offs={} o={}\n", vec[i], k,offs, o); #endif @@ -170,7 +168,7 @@ CHashedWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const o+=partial_w_dim; } val[len-k-1] = - CHash::FinalizeIncrementalMurmurHash3(val[len-k-1], carry, chunk); + Hash::FinalizeIncrementalMurmurHash3(val[len-k-1], carry, chunk); offs+=partial_w_dim*len; } SG_FREE(val); @@ -179,12 +177,12 @@ CHashedWDFeatures::dot(int32_t vec_idx1, const SGVector& vec2) const return sum/normalization_const; } -void CHashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void HashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len != w_dim) error("Dimensions don't match, vec2_dim={}, w_dim={}", vec2_len, w_dim); - int32_t lim=CMath::min(degree, string_length); + int32_t lim=Math::min(degree, string_length); int32_t len; bool free_vec1; uint8_t* vec = strings->get_feature_vector(vec_idx1, len, free_vec1); @@ -196,7 +194,7 @@ void CHashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, floa { // compute hash for strings of length start_degree-1 for (int32_t i=0; i+start_degree < len; i++) - val[i]=CHash::MurmurHash3(&vec[i], start_degree, 0xDEADBEAF); + val[i]=Hash::MurmurHash3(&vec[i], start_degree, 0xDEADBEAF); } else SGVector::fill_vector(val, len, 0xDEADBEAF); @@ -206,7 +204,7 @@ void CHashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, floa float64_t wd = alpha*wd_weights[k]/normalization_const; if (abs_val) - wd=CMath::abs(wd); + wd=Math::abs(wd); uint32_t o=offs; uint32_t carry = 0; @@ -215,8 +213,8 @@ void CHashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, floa for (int32_t i=0; i+k < len; i++) { chunk++; - CHash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); - uint32_t h = CHash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); + Hash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); + uint32_t h = Hash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); #ifdef DEBUG_HASHEDWD io::print("offs={} o={} h={} \n", offs, o, h); @@ -227,7 +225,7 @@ void CHashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, floa o+=partial_w_dim; } val[len-k-1] = - CHash::FinalizeIncrementalMurmurHash3(val[len-k-1], carry, chunk); + Hash::FinalizeIncrementalMurmurHash3(val[len-k-1], carry, chunk); offs+=partial_w_dim*len; } @@ -236,7 +234,7 @@ void CHashedWDFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, floa strings->free_feature_vector(vec, vec_idx1, free_vec1); } -void CHashedWDFeatures::set_wd_weights() +void HashedWDFeatures::set_wd_weights() { ASSERT(degree>0) @@ -256,7 +254,7 @@ void CHashedWDFeatures::set_wd_weights() } -void CHashedWDFeatures::set_normalization_const(float64_t n) +void HashedWDFeatures::set_normalization_const(float64_t n) { if (n==0) { @@ -272,13 +270,13 @@ void CHashedWDFeatures::set_normalization_const(float64_t n) SG_DEBUG("normalization_const:{}", normalization_const) } -CFeatures* CHashedWDFeatures::duplicate() const +std::shared_ptr HashedWDFeatures::duplicate() const { - return new CHashedWDFeatures(*this); + return std::make_shared(*this); } -int32_t CHashedWDFeatures::get_nnz_features_for_vector(int32_t num) const +int32_t HashedWDFeatures::get_nnz_features_for_vector(int32_t num) const { int32_t vlen=-1; bool free_vec; @@ -287,20 +285,20 @@ int32_t CHashedWDFeatures::get_nnz_features_for_vector(int32_t num) const return degree*vlen; } -void* CHashedWDFeatures::get_feature_iterator(int32_t vector_index) +void* HashedWDFeatures::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CHashedWDFeatures::get_next_feature(int32_t& index, float64_t& value, +bool HashedWDFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CHashedWDFeatures::free_feature_iterator(void* iterator) +void HashedWDFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } diff --git a/src/shogun/features/hashed/HashedWDFeatures.h b/src/shogun/features/hashed/HashedWDFeatures.h index f71efac8dee..52731b39e35 100644 --- a/src/shogun/features/hashed/HashedWDFeatures.h +++ b/src/shogun/features/hashed/HashedWDFeatures.h @@ -16,18 +16,18 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /** @brief Features that compute the Weighted Degreee Kernel feature space * explicitly. * * \sa CWeightedDegreeStringKernel */ -class CHashedWDFeatures: public CDotFeatures +class HashedWDFeatures: public DotFeatures { public: /** default constructor */ - CHashedWDFeatures(); + HashedWDFeatures(); /** constructor * @@ -37,14 +37,14 @@ class CHashedWDFeatures: public CDotFeatures * @param from_order use first order weights from higher order weighting * @param hash_bits number of bits in hash */ - CHashedWDFeatures(CStringFeatures* str, int32_t start_order, + HashedWDFeatures(std::shared_ptr> str, int32_t start_order, int32_t order, int32_t from_order, int32_t hash_bits=12); /** copy constructor */ - CHashedWDFeatures(const CHashedWDFeatures & orig); + HashedWDFeatures(const HashedWDFeatures & orig); /** destructor */ - virtual ~CHashedWDFeatures(); + virtual ~HashedWDFeatures(); /** obtain the dimensionality of the feature space * @@ -65,7 +65,7 @@ class CHashedWDFeatures: public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -147,7 +147,7 @@ class CHashedWDFeatures: public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -195,7 +195,7 @@ class CHashedWDFeatures: public CDotFeatures protected: /** stringfeatures the wdfeatures are based on*/ - CStringFeatures* strings; + std::shared_ptr> strings; /** degree */ int32_t degree; diff --git a/src/shogun/features/hashed/HashedWDFeaturesTransposed.cpp b/src/shogun/features/hashed/HashedWDFeaturesTransposed.cpp index 954efa9b200..52a4b96e72f 100644 --- a/src/shogun/features/hashed/HashedWDFeaturesTransposed.cpp +++ b/src/shogun/features/hashed/HashedWDFeaturesTransposed.cpp @@ -20,7 +20,7 @@ using namespace shogun; #ifndef DOXYGEN_SHOULD_SKIP_THIS struct HASHEDWD_THREAD_PARAM { - const CHashedWDFeaturesTransposed* hf; + const HashedWDFeaturesTransposed* hf; int32_t* sub_index; float64_t* output; int32_t start; @@ -34,8 +34,8 @@ struct HASHEDWD_THREAD_PARAM }; #endif // DOXYGEN_SHOULD_SKIP_THIS -CHashedWDFeaturesTransposed::CHashedWDFeaturesTransposed() - :CDotFeatures() +HashedWDFeaturesTransposed::HashedWDFeaturesTransposed() + :DotFeatures() { unstable(SOURCE_LOCATION); @@ -56,9 +56,9 @@ CHashedWDFeaturesTransposed::CHashedWDFeaturesTransposed() normalization_const = 0.0; } -CHashedWDFeaturesTransposed::CHashedWDFeaturesTransposed(CStringFeatures* str, +HashedWDFeaturesTransposed::HashedWDFeaturesTransposed(std::shared_ptr> str, int32_t start_order, int32_t order, int32_t from_order, - int32_t hash_bits) : CDotFeatures() + int32_t hash_bits) : DotFeatures() { ASSERT(start_order>=0) ASSERT(start_order0) ASSERT(str) ASSERT(str->have_same_length()) - SG_REF(str); + strings=str; transposed_strings=std::move(str->get_transposed_matrix()); @@ -79,9 +79,8 @@ CHashedWDFeaturesTransposed::CHashedWDFeaturesTransposed(CStringFeaturesget_alphabet(); + auto alpha=str->get_alphabet(); alphabet_size=alpha->get_num_symbols(); - SG_UNREF(alpha); degree=order; start_degree=start_order; @@ -93,34 +92,32 @@ CHashedWDFeaturesTransposed::CHashedWDFeaturesTransposed(CStringFeaturesget_max_vector_length(); num_strings=strings->get_num_vectors(); - CAlphabet* alpha=strings->get_alphabet(); + auto alpha=strings->get_alphabet(); alphabet_size=alpha->get_num_symbols(); - SG_UNREF(alpha); set_wd_weights(); } -CHashedWDFeaturesTransposed::~CHashedWDFeaturesTransposed() +HashedWDFeaturesTransposed::~HashedWDFeaturesTransposed() { - SG_UNREF(strings); SG_FREE(wd_weights); } -float64_t CHashedWDFeaturesTransposed::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const +float64_t HashedWDFeaturesTransposed::dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CHashedWDFeaturesTransposed* wdf = (CHashedWDFeaturesTransposed*) df; + auto wdf = std::static_pointer_cast(df); int32_t len1, len2; bool free_vec1, free_vec2; @@ -144,10 +141,10 @@ float64_t CHashedWDFeaturesTransposed::dot(int32_t vec_idx1, CDotFeatures* df, i } strings->free_feature_vector(vec1, vec_idx1, free_vec1); wdf->strings->free_feature_vector(vec2, vec_idx2, free_vec2); - return sum/CMath::sq(normalization_const); + return sum/Math::sq(normalization_const); } -float64_t CHashedWDFeaturesTransposed::dot( +float64_t HashedWDFeaturesTransposed::dot( int32_t vec_idx1, const SGVector& vec2) const { require( @@ -174,16 +171,16 @@ float64_t CHashedWDFeaturesTransposed::dot( { const float64_t wd = wd_weights[k]; chunk++; - CHash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); + Hash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); uint32_t h = - CHash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); + Hash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); #ifdef DEBUG_HASHEDWD io::print("vec[i]={}, k={}, offs={} o={} h={} \n", vec[i], k,offs, o, h); #endif sum+=vec2[o+(h & mask)]*wd; o+=partial_w_dim; } - val[i] = CHash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); + val[i] = Hash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); offs+=partial_w_dim*degree; } SG_FREE(val); @@ -192,7 +189,7 @@ float64_t CHashedWDFeaturesTransposed::dot( return sum/normalization_const; } -void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const +void HashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t start, int32_t stop, float64_t* alphas, float64_t* vec, int32_t dim, float64_t b) const { ASSERT(output) // write access is internally between output[start..stop] so the following @@ -259,7 +256,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta params[t].progress_bar = &pb; params[t].index=index; pthread_create(&threads[t], NULL, - CHashedWDFeaturesTransposed::dense_dot_range_helper, (void*)¶ms[t]); + HashedWDFeaturesTransposed::dense_dot_range_helper, (void*)¶ms[t]); } params[t].hf = this; @@ -273,7 +270,7 @@ void CHashedWDFeaturesTransposed::dense_dot_range(float64_t* output, int32_t sta params[t].progress = false; //true; params[t].progress_bar = &pb; params[t].index=index; - CHashedWDFeaturesTransposed::dense_dot_range_helper((void*) ¶ms[t]); + HashedWDFeaturesTransposed::dense_dot_range_helper((void*) ¶ms[t]); for (t=0; thf; + const HashedWDFeaturesTransposed* hf=par->hf; int32_t* sub_index=par->sub_index; float64_t* output=par->output; int32_t start=par->start; @@ -414,11 +411,11 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p) if (k==0) index[j] = 0xDEADBEAF; - CHash::IncrementalMurmurHash3(&index[j], &carry, &bval, 1); + Hash::IncrementalMurmurHash3(&index[j], &carry, &bval, 1); chunk++; uint32_t h = - CHash::FinalizeIncrementalMurmurHash3( + Hash::FinalizeIncrementalMurmurHash3( index[j], carry, chunk); output[j]+=vec[o + (h & mask)]*wd; @@ -426,7 +423,7 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p) } index[stop-1] = - CHash::FinalizeIncrementalMurmurHash3( + Hash::FinalizeIncrementalMurmurHash3( index[stop-1], carry, chunk); o+=partial_w_dim; @@ -466,18 +463,18 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p) if (k==0) index[j] = 0xDEADBEAF; - CHash::IncrementalMurmurHash3(&index[j], &carry, &bval, 1); + Hash::IncrementalMurmurHash3(&index[j], &carry, &bval, 1); chunk++; uint32_t h = - CHash::FinalizeIncrementalMurmurHash3( + Hash::FinalizeIncrementalMurmurHash3( index[j], carry, chunk); index[j] = h; output[j]+=vec[o + (h & mask)]*wd; } - index[stop-1] = CHash::FinalizeIncrementalMurmurHash3( + index[stop-1] = Hash::FinalizeIncrementalMurmurHash3( index[stop-1], carry, chunk); o+=partial_w_dim; @@ -500,7 +497,7 @@ void* CHashedWDFeaturesTransposed::dense_dot_range_helper(void* p) return NULL; } -void CHashedWDFeaturesTransposed::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const +void HashedWDFeaturesTransposed::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) const { if (vec2_len != w_dim) error("Dimensions don't match, vec2_dim={}, w_dim={}", vec2_len, w_dim); @@ -513,7 +510,7 @@ void CHashedWDFeaturesTransposed::add_to_dense_vec(float64_t alpha, int32_t vec_ uint32_t offs=0; float64_t factor=alpha/normalization_const; if (abs_val) - factor=CMath::abs(factor); + factor=Math::abs(factor); SGVector::fill_vector(val, len, 0xDEADBEAF); @@ -527,9 +524,9 @@ void CHashedWDFeaturesTransposed::add_to_dense_vec(float64_t alpha, int32_t vec_ { float64_t wd = wd_weights[k]*factor; chunk++; - CHash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); + Hash::IncrementalMurmurHash3(&(val[i]), &carry, &(vec[i+k]), 1); uint32_t h = - CHash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); + Hash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); #ifdef DEBUG_HASHEDWD io::print("offs={} o={} h={} \n", offs, o, h); io::print("vec[i]={}, k={}, offs={} o={}\n", vec[i], k,offs, o); @@ -539,7 +536,7 @@ void CHashedWDFeaturesTransposed::add_to_dense_vec(float64_t alpha, int32_t vec_ o+=partial_w_dim; } - val[i] = CHash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); + val[i] = Hash::FinalizeIncrementalMurmurHash3(val[i], carry, chunk); offs+=partial_w_dim*degree; } @@ -547,7 +544,7 @@ void CHashedWDFeaturesTransposed::add_to_dense_vec(float64_t alpha, int32_t vec_ strings->free_feature_vector(vec, vec_idx1, free_vec1); } -void CHashedWDFeaturesTransposed::set_wd_weights() +void HashedWDFeaturesTransposed::set_wd_weights() { ASSERT(degree>0) @@ -567,7 +564,7 @@ void CHashedWDFeaturesTransposed::set_wd_weights() } -void CHashedWDFeaturesTransposed::set_normalization_const(float64_t n) +void HashedWDFeaturesTransposed::set_normalization_const(float64_t n) { if (n==0) { @@ -583,24 +580,24 @@ void CHashedWDFeaturesTransposed::set_normalization_const(float64_t n) SG_DEBUG("normalization_const:{}", normalization_const) } -CFeatures* CHashedWDFeaturesTransposed::duplicate() const +std::shared_ptr HashedWDFeaturesTransposed::duplicate() const { - return new CHashedWDFeaturesTransposed(*this); + return std::make_shared(*this); } -void* CHashedWDFeaturesTransposed::get_feature_iterator(int32_t vector_index) +void* HashedWDFeaturesTransposed::get_feature_iterator(int32_t vector_index) { not_implemented(SOURCE_LOCATION); return NULL; } -bool CHashedWDFeaturesTransposed::get_next_feature(int32_t& index, float64_t& value, void* iterator) +bool HashedWDFeaturesTransposed::get_next_feature(int32_t& index, float64_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CHashedWDFeaturesTransposed::free_feature_iterator(void* iterator) +void HashedWDFeaturesTransposed::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); } diff --git a/src/shogun/features/hashed/HashedWDFeaturesTransposed.h b/src/shogun/features/hashed/HashedWDFeaturesTransposed.h index b4260ccec1e..f0116d4db72 100644 --- a/src/shogun/features/hashed/HashedWDFeaturesTransposed.h +++ b/src/shogun/features/hashed/HashedWDFeaturesTransposed.h @@ -16,18 +16,18 @@ namespace shogun { -template class CStringFeatures; +template class StringFeatures; /** @brief Features that compute the Weighted Degreee Kernel feature space * explicitly. * * \sa CWeightedDegreeStringKernel */ -class CHashedWDFeaturesTransposed : public CDotFeatures +class HashedWDFeaturesTransposed : public DotFeatures { public: /** default constructor */ - CHashedWDFeaturesTransposed(); + HashedWDFeaturesTransposed(); /** constructor * @@ -37,14 +37,14 @@ class CHashedWDFeaturesTransposed : public CDotFeatures * @param from_order use first order weights from higher order weighting * @param hash_bits number of bits in hash */ - CHashedWDFeaturesTransposed(CStringFeatures* str, int32_t start_order, + HashedWDFeaturesTransposed(std::shared_ptr> str, int32_t start_order, int32_t order, int32_t from_order, int32_t hash_bits=12); /** copy constructor */ - CHashedWDFeaturesTransposed(const CHashedWDFeaturesTransposed & orig); + HashedWDFeaturesTransposed(const HashedWDFeaturesTransposed & orig); /** destructor */ - virtual ~CHashedWDFeaturesTransposed(); + virtual ~HashedWDFeaturesTransposed(); /** obtain the dimensionality of the feature space * @@ -65,7 +65,7 @@ class CHashedWDFeaturesTransposed : public CDotFeatures * @param df DotFeatures (of same kind) to compute dot product with * @param vec_idx2 index of second vector */ - virtual float64_t dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) const; + virtual float64_t dot(int32_t vec_idx1, std::shared_ptr df, int32_t vec_idx2) const; /** compute dot product between vector1 and a dense vector * @@ -126,7 +126,7 @@ class CHashedWDFeaturesTransposed : public CDotFeatures * * @return feature object */ - virtual CFeatures* duplicate() const; + virtual std::shared_ptr duplicate() const; /** get feature type * @@ -223,7 +223,7 @@ class CHashedWDFeaturesTransposed : public CDotFeatures protected: /** stringfeatures the wdfeatures are based on*/ - CStringFeatures* strings; + std::shared_ptr> strings; /** pointer to transposed strings */ std::vector> transposed_strings; diff --git a/src/shogun/features/iterators/DotIterator.cpp b/src/shogun/features/iterators/DotIterator.cpp index cb4d3ce6c07..20cb7fb0f61 100644 --- a/src/shogun/features/iterators/DotIterator.cpp +++ b/src/shogun/features/iterators/DotIterator.cpp @@ -84,4 +84,4 @@ namespace shogun { return iterator(m_features, m_features->get_num_vectors()); } -} \ No newline at end of file +} diff --git a/src/shogun/features/iterators/DotIterator.h b/src/shogun/features/iterators/DotIterator.h index 0c382505e83..e2a05961f1a 100644 --- a/src/shogun/features/iterators/DotIterator.h +++ b/src/shogun/features/iterators/DotIterator.h @@ -47,10 +47,10 @@ namespace shogun public: /** constructor * - * @param features pointer to CDotFeatures + * @param features pointer to DotFeatures * @param idx position of the iterator */ - feature_vector(CDotFeatures* features, index_t idx) + feature_vector(std::shared_ptr features, index_t idx) : m_features(features), m_idx(idx) { } @@ -78,7 +78,7 @@ namespace shogun void add(float64_t alpha, SGVector& v) const; protected: - CDotFeatures* m_features; + std::shared_ptr m_features; index_t m_idx; friend class iterator; @@ -100,10 +100,10 @@ namespace shogun /** constructor * - * @param features pointer to CDotFeatures + * @param features pointer to DotFeatures * @param idx position of the iterator */ - iterator(CDotFeatures* features, index_t idx = 0) + iterator(std::shared_ptr features, index_t idx = 0) : m_feature_vector(features, idx) { } @@ -130,9 +130,9 @@ namespace shogun public: /** constructor * - * @param features pointer to CDotFeatures + * @param features pointer to DotFeatures */ - DotIterator(CDotFeatures* features) : m_features(features) + DotIterator(std::shared_ptr features) : m_features(features) { } @@ -149,7 +149,7 @@ namespace shogun iterator end() const; protected: - CDotFeatures* m_features; + std::shared_ptr m_features; }; } diff --git a/src/shogun/features/streaming/StreamingDenseFeatures.cpp b/src/shogun/features/streaming/StreamingDenseFeatures.cpp index ba99a2c4723..a1ecb82326b 100644 --- a/src/shogun/features/streaming/StreamingDenseFeatures.cpp +++ b/src/shogun/features/streaming/StreamingDenseFeatures.cpp @@ -13,8 +13,8 @@ namespace shogun { template -CStreamingDenseFeatures::CStreamingDenseFeatures() : - CStreamingDotFeatures() +StreamingDenseFeatures::StreamingDenseFeatures() : + StreamingDotFeatures() { set_read_functions(); init(); @@ -22,27 +22,26 @@ CStreamingDenseFeatures::CStreamingDenseFeatures() : } template -CStreamingDenseFeatures::CStreamingDenseFeatures(CStreamingFile* file, +StreamingDenseFeatures::StreamingDenseFeatures(std::shared_ptr file, bool is_labelled, int32_t size) : - CStreamingDotFeatures() + StreamingDotFeatures() { init(file, is_labelled, size); set_read_functions(); parser.set_free_vector_after_release(false); } -template CStreamingDenseFeatures::CStreamingDenseFeatures( - CDenseFeatures* dense_features, float64_t* lab) : - CStreamingDotFeatures() +template StreamingDenseFeatures::StreamingDenseFeatures( + std::shared_ptr> dense_features, float64_t* lab) : + StreamingDotFeatures() { - require(dense_features, "{}::CStreamingDenseFeatures(): Features needed!"); + require(dense_features, "{}::StreamingDenseFeatures(): Features needed!"); - CStreamingFileFromDenseFeatures* file; bool is_labelled; int32_t size=1024; is_labelled=lab; - file=new CStreamingFileFromDenseFeatures(dense_features, lab); + auto file=std::make_shared>(dense_features, lab); init(file, is_labelled, size); set_read_functions(); parser.set_free_vector_after_release(false); @@ -50,20 +49,20 @@ template CStreamingDenseFeatures::CStreamingDenseFeatures( seekable=true; } -template CStreamingDenseFeatures::~CStreamingDenseFeatures() +template StreamingDenseFeatures::~StreamingDenseFeatures() { - SG_TRACE("entering {}::~CStreamingDenseFeatures()", get_name()); + SG_TRACE("entering {}::~StreamingDenseFeatures()", get_name()); /* needed to prevent double free memory errors */ current_vector.vector=NULL; current_vector.vlen=0; - SG_TRACE("leaving {}::~CStreamingDenseFeatures()", get_name()); + SG_TRACE("leaving {}::~StreamingDenseFeatures()", get_name()); } -template void CStreamingDenseFeatures::reset_stream() +template void StreamingDenseFeatures::reset_stream() { if (seekable) { - ((CStreamingFileFromDenseFeatures*)working_file)->reset_stream(); + std::static_pointer_cast>(working_file)->reset_stream(); if (parser.is_running()) parser.end_parser(); parser.exit_parser(); @@ -74,7 +73,7 @@ template void CStreamingDenseFeatures::reset_stream() } } -template float32_t CStreamingDenseFeatures::dense_dot( +template float32_t StreamingDenseFeatures::dense_dot( const float32_t* vec2, int32_t vec2_len) { ASSERT(vec2_len==current_vector.vlen) @@ -86,7 +85,7 @@ template float32_t CStreamingDenseFeatures::dense_dot( return result; } -template float64_t CStreamingDenseFeatures::dense_dot( +template float64_t StreamingDenseFeatures::dense_dot( const float64_t* vec2, int32_t vec2_len) { ASSERT(vec2_len==current_vector.vlen) @@ -98,7 +97,7 @@ template float64_t CStreamingDenseFeatures::dense_dot( return result; } -template void CStreamingDenseFeatures::add_to_dense_vec( +template void StreamingDenseFeatures::add_to_dense_vec( float32_t alpha, float32_t* vec2, int32_t vec2_len, bool abs_val) { ASSERT(vec2_len==current_vector.vlen) @@ -106,7 +105,7 @@ template void CStreamingDenseFeatures::add_to_dense_vec( if (abs_val) { for (int32_t i=0; i void CStreamingDenseFeatures::add_to_dense_vec( } } -template void CStreamingDenseFeatures::add_to_dense_vec( +template void StreamingDenseFeatures::add_to_dense_vec( float64_t alpha, float64_t* vec2, int32_t vec2_len, bool abs_val) { ASSERT(vec2_len==current_vector.vlen) @@ -123,7 +122,7 @@ template void CStreamingDenseFeatures::add_to_dense_vec( if (abs_val) { for (int32_t i=0; i void CStreamingDenseFeatures::add_to_dense_vec( } } -template int32_t CStreamingDenseFeatures::get_nnz_features_for_vector() +template int32_t StreamingDenseFeatures::get_nnz_features_for_vector() { return current_vector.vlen; } -template int32_t CStreamingDenseFeatures::get_num_vectors() const +template int32_t StreamingDenseFeatures::get_num_vectors() const { return 1; } template -void CStreamingDenseFeatures::set_vector_reader() +void StreamingDenseFeatures::set_vector_reader() { - parser.set_read_vector(&CStreamingFile::get_vector); + parser.set_read_vector(&StreamingFile::get_vector); } template -void CStreamingDenseFeatures::set_vector_and_label_reader() +void StreamingDenseFeatures::set_vector_and_label_reader() { - parser.set_read_vector_and_label(&CStreamingFile::get_vector_and_label); + parser.set_read_vector_and_label(&StreamingFile::get_vector_and_label); } #define GET_FEATURE_TYPE(f_type, sg_type) \ -template<> EFeatureType CStreamingDenseFeatures::get_feature_type() const \ +template<> EFeatureType StreamingDenseFeatures::get_feature_type() const \ { \ return f_type; \ } @@ -176,7 +175,7 @@ GET_FEATURE_TYPE(F_LONGREAL, floatmax_t) #undef GET_FEATURE_TYPE template -void CStreamingDenseFeatures::init() +void StreamingDenseFeatures::init() { working_file=NULL; seekable=false; @@ -189,32 +188,32 @@ void CStreamingDenseFeatures::init() } template -void CStreamingDenseFeatures::init(CStreamingFile* file, bool is_labelled, +void StreamingDenseFeatures::init(std::shared_ptr file, bool is_labelled, int32_t size) { init(); has_labels=is_labelled; working_file=file; - SG_REF(working_file); + parser.init(file, is_labelled, size); seekable=false; } template -void CStreamingDenseFeatures::start_parser() +void StreamingDenseFeatures::start_parser() { if (!parser.is_running()) parser.start_parser(); } template -void CStreamingDenseFeatures::end_parser() +void StreamingDenseFeatures::end_parser() { parser.end_parser(); } template -bool CStreamingDenseFeatures::get_next_example() +bool StreamingDenseFeatures::get_next_example() { SG_TRACE("entering"); bool ret_value; @@ -226,13 +225,13 @@ bool CStreamingDenseFeatures::get_next_example() } template -SGVector CStreamingDenseFeatures::get_vector() +SGVector StreamingDenseFeatures::get_vector() { return current_vector; } template -float64_t CStreamingDenseFeatures::get_label() +float64_t StreamingDenseFeatures::get_label() { ASSERT(has_labels) @@ -240,24 +239,24 @@ float64_t CStreamingDenseFeatures::get_label() } template -void CStreamingDenseFeatures::release_example() +void StreamingDenseFeatures::release_example() { parser.finalize_example(); } template -int32_t CStreamingDenseFeatures::get_dim_feature_space() const +int32_t StreamingDenseFeatures::get_dim_feature_space() const { return current_vector.vlen; } template -float32_t CStreamingDenseFeatures::dot(CStreamingDotFeatures* df) +float32_t StreamingDenseFeatures::dot(std::shared_ptr df) { ASSERT(df) ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(df->get_feature_class() == get_feature_class()) - CStreamingDenseFeatures* sf=(CStreamingDenseFeatures*)df; + auto sf=std::dynamic_pointer_cast>(df); SGVector other_vector=sf->get_vector(); @@ -265,7 +264,7 @@ float32_t CStreamingDenseFeatures::dot(CStreamingDotFeatures* df) } template -float32_t CStreamingDenseFeatures::dot(SGVector sgvec1) +float32_t StreamingDenseFeatures::dot(SGVector sgvec1) { int32_t len1; len1=sgvec1.vlen; @@ -278,19 +277,19 @@ float32_t CStreamingDenseFeatures::dot(SGVector sgvec1) } template -int32_t CStreamingDenseFeatures::get_num_features() +int32_t StreamingDenseFeatures::get_num_features() { return current_vector.vlen; } template -EFeatureClass CStreamingDenseFeatures::get_feature_class() const +EFeatureClass StreamingDenseFeatures::get_feature_class() const { return C_STREAMING_DENSE; } template -CFeatures* CStreamingDenseFeatures::get_streamed_features( +std::shared_ptr StreamingDenseFeatures::get_streamed_features( index_t num_elements) { SG_TRACE("entering"); @@ -350,25 +349,24 @@ CFeatures* CStreamingDenseFeatures::get_streamed_features( } /* create new feature object from collected data */ - CDenseFeatures* result=new CDenseFeatures(matrix); - SG_DEBUG("leaving returning {}x{} matrix", matrix.num_rows, matrix.num_cols); - return result; + /* create new feature object from collected data */ + return std::make_shared>(matrix); } -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; -template class CStreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; +template class StreamingDenseFeatures ; } diff --git a/src/shogun/features/streaming/StreamingDenseFeatures.h b/src/shogun/features/streaming/StreamingDenseFeatures.h index 390963d4972..ed106b6c9aa 100644 --- a/src/shogun/features/streaming/StreamingDenseFeatures.h +++ b/src/shogun/features/streaming/StreamingDenseFeatures.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Vladislav Horbatiuk, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Vladislav Horbatiuk, * Yuyu Zhang, Viktor Gal, Sergey Lisitsyn */ #ifndef _STREAMINGDENSEFEATURES__H__ @@ -23,8 +23,8 @@ namespace shogun * and current_label. Call get_next_example() followed by get_current_vector() * to iterate through the stream. */ -template class CStreamingDenseFeatures: - public CStreamingDotFeatures +template class StreamingDenseFeatures: + public StreamingDotFeatures { public: @@ -35,7 +35,7 @@ template class CStreamingDenseFeatures: * CStreamingFile::get_*_vector and get_*_vector_and_label * depending on the type T. */ - CStreamingDenseFeatures(); + StreamingDenseFeatures(); /** * Constructor taking args. @@ -45,7 +45,7 @@ template class CStreamingDenseFeatures: * @param is_labelled Whether examples are labelled or not. * @param size Number of example objects to be stored in the parser at a time. */ - CStreamingDenseFeatures(CStreamingFile* file, bool is_labelled, + StreamingDenseFeatures(std::shared_ptr file, bool is_labelled, int32_t size); /** @@ -55,7 +55,7 @@ template class CStreamingDenseFeatures: * @param dense_features DenseFeatures object of same type * @param lab labels array, float64_t* */ - CStreamingDenseFeatures(CDenseFeatures* dense_features, float64_t* lab= + StreamingDenseFeatures(std::shared_ptr> dense_features, float64_t* lab= NULL); /** @@ -63,7 +63,7 @@ template class CStreamingDenseFeatures: * * Ends the parsing thread. (Waits for pthread_join to complete) */ - ~CStreamingDenseFeatures(); + ~StreamingDenseFeatures(); /** * Sets the read function (in case the examples are @@ -162,14 +162,14 @@ template class CStreamingDenseFeatures: /** * Dot product taken with another StreamingDotFeatures object. * - * Currently only works if it is a CStreamingDenseFeatures object. + * Currently only works if it is a StreamingDenseFeatures object. * It takes the dot product of the current_vectors of both objects. * * @param df CStreamingDotFeatures object. * * @return Dot product. */ - virtual float32_t dot(CStreamingDotFeatures *df); + virtual float32_t dot(std::shared_ptrdf); /** * Dot product with another dense vector. @@ -261,10 +261,10 @@ template class CStreamingDenseFeatures: * from the underlying stream. The object is not SG_REF'ed. * * @param num_elements num elements to save from stream - * @return CFeatures object of underlying type, might contain less data if + * @return Features object of underlying type, might contain less data if * the stream did end (warning is written) */ - virtual CFeatures* get_streamed_features(index_t num_elements); + virtual std::shared_ptr get_streamed_features(index_t num_elements); private: /** @@ -280,7 +280,7 @@ template class CStreamingDenseFeatures: * @param is_labelled whether labelled or not * @param size number of examples in the parser's ring */ - void init(CStreamingFile *file, bool is_labelled, int32_t size); + void init(std::shared_ptrfile, bool is_labelled, int32_t size); protected: @@ -288,7 +288,7 @@ template class CStreamingDenseFeatures: float32_t combined_weight; /// The parser object, which reads from input and returns parsed example objects. - CInputParser parser; + InputParser parser; /// The current example's feature vector as an SGVector SGVector current_vector; diff --git a/src/shogun/features/streaming/StreamingDotFeatures.cpp b/src/shogun/features/streaming/StreamingDotFeatures.cpp index d8a06d5426c..be3b5392df9 100644 --- a/src/shogun/features/streaming/StreamingDotFeatures.cpp +++ b/src/shogun/features/streaming/StreamingDotFeatures.cpp @@ -7,23 +7,23 @@ using namespace shogun; -CStreamingDotFeatures::CStreamingDotFeatures() : CStreamingFeatures() +StreamingDotFeatures::StreamingDotFeatures() : StreamingFeatures() { set_property(FP_STREAMING_DOT); } -CStreamingDotFeatures::CStreamingDotFeatures(CDotFeatures* dot_features, +StreamingDotFeatures::StreamingDotFeatures(DotFeatures* dot_features, float64_t* lab) { not_implemented(SOURCE_LOCATION); return; } -CStreamingDotFeatures::~CStreamingDotFeatures() +StreamingDotFeatures::~StreamingDotFeatures() { } -void CStreamingDotFeatures::dense_dot_range(float32_t* output, float32_t* alphas, +void StreamingDotFeatures::dense_dot_range(float32_t* output, float32_t* alphas, float32_t* vec, int32_t dim, float32_t b, int32_t num_vec) { ASSERT(num_vec>=0) @@ -46,7 +46,7 @@ void CStreamingDotFeatures::dense_dot_range(float32_t* output, float32_t* alphas end_parser(); } -void CStreamingDotFeatures::expand_if_required(float32_t*& vec, int32_t &len) +void StreamingDotFeatures::expand_if_required(float32_t*& vec, int32_t &len) { int32_t dim = get_dim_feature_space(); if (dim > len) @@ -57,7 +57,7 @@ void CStreamingDotFeatures::expand_if_required(float32_t*& vec, int32_t &len) } } -void CStreamingDotFeatures::expand_if_required(float64_t*& vec, int32_t &len) +void StreamingDotFeatures::expand_if_required(float64_t*& vec, int32_t &len) { int32_t dim = get_dim_feature_space(); if (dim > len) @@ -68,25 +68,25 @@ void CStreamingDotFeatures::expand_if_required(float64_t*& vec, int32_t &len) } } -void* CStreamingDotFeatures::get_feature_iterator() +void* StreamingDotFeatures::get_feature_iterator() { not_implemented(SOURCE_LOCATION); return NULL; } -int32_t CStreamingDotFeatures::get_nnz_features_for_vector() +int32_t StreamingDotFeatures::get_nnz_features_for_vector() { not_implemented(SOURCE_LOCATION); return -1; } -bool CStreamingDotFeatures::get_next_feature(int32_t& index, float32_t& value, void* iterator) +bool StreamingDotFeatures::get_next_feature(int32_t& index, float32_t& value, void* iterator) { not_implemented(SOURCE_LOCATION); return false; } -void CStreamingDotFeatures::free_feature_iterator(void* iterator) +void StreamingDotFeatures::free_feature_iterator(void* iterator) { not_implemented(SOURCE_LOCATION); return; diff --git a/src/shogun/features/streaming/StreamingDotFeatures.h b/src/shogun/features/streaming/StreamingDotFeatures.h index 53de94f6fdc..21866159a77 100644 --- a/src/shogun/features/streaming/StreamingDotFeatures.h +++ b/src/shogun/features/streaming/StreamingDotFeatures.h @@ -15,8 +15,8 @@ namespace shogun { -class CDotFeatures; -class CStreamingFile; +class DotFeatures; +class StreamingFile; /** @brief Streaming features that support dot products among other operations. * @@ -40,12 +40,12 @@ class CStreamingFile; * */ -class CStreamingDotFeatures : public CStreamingFeatures +class StreamingDotFeatures : public StreamingFeatures { public: /** Constructor */ - CStreamingDotFeatures(); + StreamingDotFeatures(); /** * Constructor with input information passed. @@ -54,13 +54,13 @@ class CStreamingDotFeatures : public CStreamingFeatures * @param is_labelled Whether examples are labelled or not. * @param size Number of examples to be held in the parser's "ring". */ - CStreamingDotFeatures(CStreamingFile* file, bool is_labelled, int32_t size) + StreamingDotFeatures(std::shared_ptr file, bool is_labelled, int32_t size) { not_implemented(SOURCE_LOCATION);; } /** - * Constructor taking a CDotFeatures object and optionally, + * Constructor taking a DotFeatures object and optionally, * labels, as args. * * The derived class should implement it so that the @@ -68,12 +68,12 @@ class CStreamingDotFeatures : public CStreamingFeatures * input, getting examples one by one from the DotFeatures * object (and labels, if applicable). * - * @param dot_features CDotFeatures object + * @param dot_features DotFeatures object * @param lab labels (optional) */ - CStreamingDotFeatures(CDotFeatures* dot_features, float64_t* lab=NULL); + StreamingDotFeatures(DotFeatures* dot_features, float64_t* lab=NULL); - virtual ~CStreamingDotFeatures(); + virtual ~StreamingDotFeatures(); /** compute dot product between vectors of two * StreamingDotFeatures objects. @@ -81,7 +81,7 @@ class CStreamingDotFeatures : public CStreamingFeatures * @param df StreamingDotFeatures (of same kind) to compute * dot product with */ - virtual float32_t dot(CStreamingDotFeatures* df)=0; + virtual float32_t dot(std::shared_ptr df)=0; /** compute dot product between current vector and a dense vector * diff --git a/src/shogun/features/streaming/StreamingFeatures.cpp b/src/shogun/features/streaming/StreamingFeatures.cpp index c03ffbd4791..41ab991671f 100644 --- a/src/shogun/features/streaming/StreamingFeatures.cpp +++ b/src/shogun/features/streaming/StreamingFeatures.cpp @@ -9,35 +9,32 @@ using namespace shogun; -CStreamingFeatures::CStreamingFeatures() : CFeatures() +StreamingFeatures::StreamingFeatures() : Features() { working_file=NULL; } -CStreamingFeatures::~CStreamingFeatures() +StreamingFeatures::~StreamingFeatures() { - SG_TRACE("entering CStreamingFeatures::~CStreamingFeatures()"); - SG_UNREF(working_file); - SG_TRACE("leaving CStreamingFeatures::~CStreamingFeatures()"); } -void CStreamingFeatures::set_read_functions() +void StreamingFeatures::set_read_functions() { set_vector_reader(); set_vector_and_label_reader(); } -bool CStreamingFeatures::get_has_labels() +bool StreamingFeatures::get_has_labels() { return has_labels; } -bool CStreamingFeatures::is_seekable() +bool StreamingFeatures::is_seekable() { return seekable; } -void CStreamingFeatures::reset_stream() +void StreamingFeatures::reset_stream() { not_implemented(SOURCE_LOCATION); return; diff --git a/src/shogun/features/streaming/StreamingFeatures.h b/src/shogun/features/streaming/StreamingFeatures.h index 496e69b2fee..2275e7b5b7f 100644 --- a/src/shogun/features/streaming/StreamingFeatures.h +++ b/src/shogun/features/streaming/StreamingFeatures.h @@ -64,7 +64,7 @@ namespace shogun * */ -class CStreamingFeatures : public CFeatures +class StreamingFeatures : public Features { public: @@ -73,14 +73,14 @@ class CStreamingFeatures : public CFeatures * Default constructor with no args. * Doesn't do anything yet. */ - CStreamingFeatures(); + StreamingFeatures(); - CStreamingFeatures(const CStreamingFeatures&) = delete; + StreamingFeatures(const StreamingFeatures&) = delete; /** * Destructor */ - virtual ~CStreamingFeatures(); + virtual ~StreamingFeatures(); /** * Set the vector reading functions. @@ -165,7 +165,7 @@ class CStreamingFeatures : public CFeatures * batch fashion. * * A stream can usually seekable when it comes from a file or - * when it comes from another conventional CFeatures object. + * when it comes from another conventional Features object. * * @return true if seekable, else false. */ @@ -176,16 +176,16 @@ class CStreamingFeatures : public CFeatures */ virtual void reset_stream(); - /** Returns a new CFeatures instance which contains num_elements elements from + /** Returns a new Features instance which contains num_elements elements from * the underlying stream. Not SG_REF'ed * * @param num_elements num elements to save from stream - * @return CFeatures object of underlying type, might be smaller than + * @return Features object of underlying type, might be smaller than * requested if the stream stopped. A warning is issued in this case. * * NOT IMPLEMENTED! */ - virtual CFeatures* get_streamed_features(index_t num_elements) + virtual std::shared_ptr get_streamed_features(index_t num_elements) { error("{}::get_streamed_features() is not yet implemented!", get_name()); @@ -195,9 +195,9 @@ class CStreamingFeatures : public CFeatures /** * Duplicate the object. * - * @return a duplicate object as CFeatures* + * @return a duplicate object as Features* */ - virtual CFeatures* duplicate() const + virtual std::shared_ptr duplicate() const { not_implemented(SOURCE_LOCATION); return NULL; @@ -209,7 +209,7 @@ class CStreamingFeatures : public CFeatures bool has_labels; /// The StreamingFile object to read from. - CStreamingFile* working_file; + std::shared_ptr working_file; /// Whether the stream is seekable bool seekable; diff --git a/src/shogun/features/streaming/StreamingHashedDenseFeatures.cpp b/src/shogun/features/streaming/StreamingHashedDenseFeatures.cpp index f8a9bcec61c..a325210f63e 100644 --- a/src/shogun/features/streaming/StreamingHashedDenseFeatures.cpp +++ b/src/shogun/features/streaming/StreamingHashedDenseFeatures.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evangelos Anagnostopoulos, Heiko Strathmann, Bjoern Esser, + * Authors: Evangelos Anagnostopoulos, Heiko Strathmann, Bjoern Esser, * Sergey Lisitsyn, Viktor Gal */ @@ -12,26 +12,25 @@ namespace shogun { template -CStreamingHashedDenseFeatures::CStreamingHashedDenseFeatures() +StreamingHashedDenseFeatures::StreamingHashedDenseFeatures() { init(NULL, false, 0, 0, false, true); } template -CStreamingHashedDenseFeatures::CStreamingHashedDenseFeatures(CStreamingFile* file, +StreamingHashedDenseFeatures::StreamingHashedDenseFeatures(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms) { init(file, is_labelled, size, d, use_quadr, keep_lin_terms); } template -CStreamingHashedDenseFeatures::CStreamingHashedDenseFeatures(CDenseFeatures* dot_features, +StreamingHashedDenseFeatures::StreamingHashedDenseFeatures(std::shared_ptr> dot_features, int32_t d, bool use_quadr, bool keep_lin_terms, float64_t* lab) { ASSERT(dot_features); - CStreamingFileFromDenseFeatures* file = - new CStreamingFileFromDenseFeatures(dot_features, lab); + auto file = std::make_shared>(dot_features, lab); bool is_labelled = (lab != NULL); int32_t size = 1024; @@ -42,12 +41,12 @@ CStreamingHashedDenseFeatures::CStreamingHashedDenseFeatures(CDenseFeatures< } template -CStreamingHashedDenseFeatures::~CStreamingHashedDenseFeatures() +StreamingHashedDenseFeatures::~StreamingHashedDenseFeatures() { } template -void CStreamingHashedDenseFeatures::init(CStreamingFile* file, bool is_labelled, +void StreamingHashedDenseFeatures::init(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms) { dim = d; @@ -62,7 +61,7 @@ void CStreamingHashedDenseFeatures::init(CStreamingFile* file, bool is_label if (file) { working_file = file; - SG_REF(working_file); + parser.init(file, is_labelled, size); seekable = false; } @@ -76,18 +75,18 @@ void CStreamingHashedDenseFeatures::init(CStreamingFile* file, bool is_label } template -float32_t CStreamingHashedDenseFeatures::dot(CStreamingDotFeatures* df) +float32_t StreamingHashedDenseFeatures::dot(std::shared_ptr df) { ASSERT(df); ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(strcmp(df->get_name(),get_name())==0) - CStreamingHashedDenseFeatures* hdf = (CStreamingHashedDenseFeatures* ) df; + auto hdf = std::static_pointer_cast>(df); return current_vector.sparse_dot(hdf->current_vector); } template -float32_t CStreamingHashedDenseFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) +float32_t StreamingHashedDenseFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) { ASSERT(vec2_len == dim); @@ -99,87 +98,87 @@ float32_t CStreamingHashedDenseFeatures::dense_dot(const float32_t* vec2, in } template -void CStreamingHashedDenseFeatures::add_to_dense_vec(float32_t alpha, float32_t* vec2, +void StreamingHashedDenseFeatures::add_to_dense_vec(float32_t alpha, float32_t* vec2, int32_t vec2_len, bool abs_val) { ASSERT(vec2_len == dim); if (abs_val) - alpha = CMath::abs(alpha); + alpha = Math::abs(alpha); for (index_t i=0; i -int32_t CStreamingHashedDenseFeatures::get_dim_feature_space() const +int32_t StreamingHashedDenseFeatures::get_dim_feature_space() const { return dim; } template -const char* CStreamingHashedDenseFeatures::get_name() const +const char* StreamingHashedDenseFeatures::get_name() const { return "StreamingHashedDenseFeatures"; } template -int32_t CStreamingHashedDenseFeatures::get_num_vectors() const +int32_t StreamingHashedDenseFeatures::get_num_vectors() const { return 1; } template -void CStreamingHashedDenseFeatures::set_vector_reader() +void StreamingHashedDenseFeatures::set_vector_reader() { - parser.set_read_vector(&CStreamingFile::get_vector); + parser.set_read_vector(&StreamingFile::get_vector); } template -void CStreamingHashedDenseFeatures::set_vector_and_label_reader() +void StreamingHashedDenseFeatures::set_vector_and_label_reader() { - parser.set_read_vector_and_label(&CStreamingFile::get_vector_and_label); + parser.set_read_vector_and_label(&StreamingFile::get_vector_and_label); } template -EFeatureType CStreamingHashedDenseFeatures::get_feature_type() const +EFeatureType StreamingHashedDenseFeatures::get_feature_type() const { return F_UINT; } template -EFeatureClass CStreamingHashedDenseFeatures::get_feature_class() const +EFeatureClass StreamingHashedDenseFeatures::get_feature_class() const { return C_STREAMING_SPARSE; } template -void CStreamingHashedDenseFeatures::start_parser() +void StreamingHashedDenseFeatures::start_parser() { if (!parser.is_running()) parser.start_parser(); } template -void CStreamingHashedDenseFeatures::end_parser() +void StreamingHashedDenseFeatures::end_parser() { parser.end_parser(); } template -float64_t CStreamingHashedDenseFeatures::get_label() +float64_t StreamingHashedDenseFeatures::get_label() { return current_label; } template -bool CStreamingHashedDenseFeatures::get_next_example() +bool StreamingHashedDenseFeatures::get_next_example() { SGVector tmp; if (parser.get_next_example(tmp.vector, tmp.vlen, current_label)) { - current_vector = CHashedDenseFeatures::hash_vector(tmp, dim, use_quadratic, + current_vector = HashedDenseFeatures::hash_vector(tmp, dim, use_quadratic, keep_linear_terms); tmp.vector = NULL; tmp.vlen = -1; @@ -189,34 +188,34 @@ bool CStreamingHashedDenseFeatures::get_next_example() } template -void CStreamingHashedDenseFeatures::release_example() +void StreamingHashedDenseFeatures::release_example() { parser.finalize_example(); } template -int32_t CStreamingHashedDenseFeatures::get_num_features() +int32_t StreamingHashedDenseFeatures::get_num_features() { return dim; } template -SGSparseVector CStreamingHashedDenseFeatures::get_vector() +SGSparseVector StreamingHashedDenseFeatures::get_vector() { return current_vector; } -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; -template class CStreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; +template class StreamingHashedDenseFeatures; } diff --git a/src/shogun/features/streaming/StreamingHashedDenseFeatures.h b/src/shogun/features/streaming/StreamingHashedDenseFeatures.h index 636dcd57872..adf8cf06f54 100644 --- a/src/shogun/features/streaming/StreamingHashedDenseFeatures.h +++ b/src/shogun/features/streaming/StreamingHashedDenseFeatures.h @@ -15,9 +15,9 @@ namespace shogun { -class CStreamingDotFeatures; +class StreamingDotFeatures; -/** @brief This class acts as an alternative to the CStreamingDenseFeatures class +/** @brief This class acts as an alternative to the StreamingDenseFeatures class * and their difference is that the current example in this class is hashed into * a smaller dimension dim. * @@ -25,11 +25,11 @@ class CStreamingDotFeatures; * and current_label. Call get_next_example() followed by get_current_vector() * to iterate through the stream. */ -template class CStreamingHashedDenseFeatures : public CStreamingDotFeatures +template class StreamingHashedDenseFeatures : public StreamingDotFeatures { public: /** Constructor */ - CStreamingHashedDenseFeatures(); + StreamingHashedDenseFeatures(); /** * Constructor with input information passed. @@ -41,11 +41,11 @@ template class CStreamingHashedDenseFeatures : public CStreamingDotFe * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CStreamingHashedDenseFeatures(CStreamingFile* file, bool is_labelled, int32_t size, + StreamingHashedDenseFeatures(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d = 512, bool use_quadr = false, bool keep_lin_terms = true); /** - * Constructor taking a CDotFeatures object and optionally, + * Constructor taking a DotFeatures object and optionally, * labels, as args. * * The derived class should implement it so that the @@ -53,17 +53,17 @@ template class CStreamingHashedDenseFeatures : public CStreamingDotFe * input, getting examples one by one from the DotFeatures * object (and labels, if applicable). * - * @param dot_features CDotFeatures object + * @param dot_features DotFeatures object * @param d the dimensionality of the target feature space * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations * @param lab labels (optional) */ - CStreamingHashedDenseFeatures(CDenseFeatures* dot_features, int32_t d = 512, + StreamingHashedDenseFeatures(std::shared_ptr> dot_features, int32_t d = 512, bool use_quadr = false, bool keep_lin_terms = true, float64_t* lab = NULL); /** Destructor */ - virtual ~CStreamingHashedDenseFeatures(); + virtual ~StreamingHashedDenseFeatures(); /** compute dot product between vectors of two * StreamingDotFeatures objects. @@ -71,7 +71,7 @@ template class CStreamingHashedDenseFeatures : public CStreamingDotFe * @param df StreamingDotFeatures (of same kind) to compute * dot product with */ - virtual float32_t dot(CStreamingDotFeatures* df); + virtual float32_t dot(std::shared_ptr df); /** compute dot product between current vector and a dense vector * @@ -197,7 +197,7 @@ template class CStreamingHashedDenseFeatures : public CStreamingDotFe SGSparseVector get_vector(); private: - void init(CStreamingFile* file, bool is_labelled, int32_t size, + void init(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms); protected: @@ -209,7 +209,7 @@ template class CStreamingHashedDenseFeatures : public CStreamingDotFe SGSparseVector current_vector; /** The parser */ - CInputParser parser; + InputParser parser; /** The current example's label */ float64_t current_label; diff --git a/src/shogun/features/streaming/StreamingHashedDocDotFeatures.cpp b/src/shogun/features/streaming/StreamingHashedDocDotFeatures.cpp index e7f5e706e97..69c32d529b0 100644 --- a/src/shogun/features/streaming/StreamingHashedDocDotFeatures.cpp +++ b/src/shogun/features/streaming/StreamingHashedDocDotFeatures.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evangelos Anagnostopoulos, Heiko Strathmann, Bjoern Esser, + * Authors: Evangelos Anagnostopoulos, Heiko Strathmann, Bjoern Esser, * Sergey Lisitsyn, Viktor Gal */ @@ -11,24 +11,24 @@ using namespace shogun; -CStreamingHashedDocDotFeatures::CStreamingHashedDocDotFeatures(CStreamingFile* file, - bool is_labelled, int32_t size, CTokenizer* tzer, int32_t bits) -: CStreamingDotFeatures() +StreamingHashedDocDotFeatures::StreamingHashedDocDotFeatures(std::shared_ptr file, + bool is_labelled, int32_t size, std::shared_ptr tzer, int32_t bits) +: StreamingDotFeatures() { init(file, is_labelled, size, tzer, bits, true, 1, 0); } -CStreamingHashedDocDotFeatures::CStreamingHashedDocDotFeatures() : CStreamingDotFeatures() +StreamingHashedDocDotFeatures::StreamingHashedDocDotFeatures() : StreamingDotFeatures() { init(NULL, false, 0, NULL, 0, false, 1, 0); } -CStreamingHashedDocDotFeatures::CStreamingHashedDocDotFeatures( - CStringFeatures* dot_features, CTokenizer* tzer, int32_t bits, float64_t* lab) -: CStreamingDotFeatures() +StreamingHashedDocDotFeatures::StreamingHashedDocDotFeatures( + std::shared_ptr> dot_features, std::shared_ptr tzer, int32_t bits, float64_t* lab) +: StreamingDotFeatures() { - CStreamingFileFromStringFeatures* file = - new CStreamingFileFromStringFeatures(dot_features, lab); + auto file = + std::make_shared>(dot_features, lab); bool is_labelled = (lab != NULL); int32_t size=1024; @@ -37,28 +37,28 @@ CStreamingHashedDocDotFeatures::CStreamingHashedDocDotFeatures( parser.set_free_vectors_on_destruct(false); seekable= true; } -void CStreamingHashedDocDotFeatures::init(CStreamingFile* file, bool is_labelled, - int32_t size, CTokenizer* tzer, int32_t bits, bool normalize, int32_t n_grams, int32_t skips) +void StreamingHashedDocDotFeatures::init(std::shared_ptr file, bool is_labelled, + int32_t size, std::shared_ptr tzer, int32_t bits, bool normalize, int32_t n_grams, int32_t skips) { num_bits = bits; tokenizer = tzer; if (tokenizer) { - SG_REF(tokenizer); - converter = new CHashedDocConverter(tzer, bits, normalize, n_grams, skips); + + converter = std::make_shared(tzer, bits, normalize, n_grams, skips); } else converter=NULL; SG_ADD(&num_bits, "num_bits", "Number of bits for hash"); - SG_ADD((CSGObject** ) &tokenizer, "tokenizer", "The tokenizer used on the documents"); - SG_ADD((CSGObject** ) &converter, "converter", "Converter"); + SG_ADD((std::shared_ptr* ) &tokenizer, "tokenizer", "The tokenizer used on the documents"); + SG_ADD((std::shared_ptr* ) &converter, "converter", "Converter"); has_labels = is_labelled; if (file) { working_file = file; - SG_REF(working_file); + parser.init(file, is_labelled, size); seekable = false; } @@ -69,28 +69,28 @@ void CStreamingHashedDocDotFeatures::init(CStreamingFile* file, bool is_labelled parser.set_free_vector_after_release(false); } -CStreamingHashedDocDotFeatures::~CStreamingHashedDocDotFeatures() +StreamingHashedDocDotFeatures::~StreamingHashedDocDotFeatures() { if (parser.is_running()) parser.end_parser(); - SG_UNREF(working_file); - SG_UNREF(tokenizer); - SG_UNREF(converter); + + + } -float32_t CStreamingHashedDocDotFeatures::dot(CStreamingDotFeatures* df) +float32_t StreamingHashedDocDotFeatures::dot(std::shared_ptr df) { ASSERT(df) ASSERT(df->get_name() == get_name()) - CStreamingHashedDocDotFeatures* cdf = (CStreamingHashedDocDotFeatures* ) df; + auto cdf = std::static_pointer_cast(df); float32_t result = current_vector.sparse_dot(cdf->current_vector); return result; } -float32_t CStreamingHashedDocDotFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) +float32_t StreamingHashedDocDotFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) { - ASSERT(vec2_len == CMath::pow(2, num_bits)) + ASSERT(vec2_len == Math::pow(2, num_bits)) float32_t result = 0; for (index_t i=0; i tmp; if (parser.get_next_example(tmp.vector, @@ -155,47 +155,47 @@ bool CStreamingHashedDocDotFeatures::get_next_example() return false; } -void CStreamingHashedDocDotFeatures::release_example() +void StreamingHashedDocDotFeatures::release_example() { parser.finalize_example(); } -int32_t CStreamingHashedDocDotFeatures::get_num_features() +int32_t StreamingHashedDocDotFeatures::get_num_features() { - return (int32_t) CMath::pow(2, num_bits); + return (int32_t) Math::pow(2, num_bits); } -float64_t CStreamingHashedDocDotFeatures::get_label() +float64_t StreamingHashedDocDotFeatures::get_label() { return current_label; } -int32_t CStreamingHashedDocDotFeatures::get_num_vectors() const +int32_t StreamingHashedDocDotFeatures::get_num_vectors() const { return 1; } -void CStreamingHashedDocDotFeatures::set_vector_reader() +void StreamingHashedDocDotFeatures::set_vector_reader() { - parser.set_read_vector(&CStreamingFile::get_string); + parser.set_read_vector(&StreamingFile::get_string); } -void CStreamingHashedDocDotFeatures::set_vector_and_label_reader() +void StreamingHashedDocDotFeatures::set_vector_and_label_reader() { - parser.set_read_vector_and_label(&CStreamingFile::get_string_and_label); + parser.set_read_vector_and_label(&StreamingFile::get_string_and_label); } -SGSparseVector CStreamingHashedDocDotFeatures::get_vector() +SGSparseVector StreamingHashedDocDotFeatures::get_vector() { return current_vector; } -void CStreamingHashedDocDotFeatures::set_normalization(bool normalize) +void StreamingHashedDocDotFeatures::set_normalization(bool normalize) { converter->set_normalization(normalize); } -void CStreamingHashedDocDotFeatures::set_k_skip_n_grams(int32_t k, int32_t n) +void StreamingHashedDocDotFeatures::set_k_skip_n_grams(int32_t k, int32_t n) { converter->set_k_skip_n_grams(k, n); } diff --git a/src/shogun/features/streaming/StreamingHashedDocDotFeatures.h b/src/shogun/features/streaming/StreamingHashedDocDotFeatures.h index 87db369a025..6a071791581 100644 --- a/src/shogun/features/streaming/StreamingHashedDocDotFeatures.h +++ b/src/shogun/features/streaming/StreamingHashedDocDotFeatures.h @@ -17,9 +17,9 @@ namespace shogun { -class CStreamingDotFeatures; -class CTokenizer; -class CHashedDocConverter; +class StreamingDotFeatures; +class Tokenizer; +class HashedDocConverter; /** @brief This class implements streaming features for a document collection. * Like in the standard Bag-of-Words representation, this class considers each document as a collection of tokens, @@ -35,11 +35,11 @@ class CHashedDocConverter; * and current_label. Call get_next_example() followed by get_current_vector() * to iterate through the stream. */ -class CStreamingHashedDocDotFeatures : public CStreamingDotFeatures +class StreamingHashedDocDotFeatures : public StreamingDotFeatures { public: /** Constructor */ - CStreamingHashedDocDotFeatures(); + StreamingHashedDocDotFeatures(); /** * Constructor with input information passed. @@ -52,11 +52,11 @@ class CStreamingHashedDocDotFeatures : public CStreamingDotFeatures * @param tzer the tokenizer to use on the document collection * @param bits the number of bits of the new dimension (means a dimension of size 2^bits) */ - CStreamingHashedDocDotFeatures(CStreamingFile* file, bool is_labelled, int32_t size, - CTokenizer* tzer, int32_t bits=20); + StreamingHashedDocDotFeatures(std::shared_ptr file, bool is_labelled, int32_t size, + std::shared_ptr tzer, int32_t bits=20); /** - * Constructor taking a CDotFeatures object and optionally, + * Constructor taking a DotFeatures object and optionally, * labels, as args. * Will use normalization and no quadratic features by default, user should * use the set_normalization() and set_k_skip_n_gram() methods to change that. @@ -66,16 +66,16 @@ class CStreamingHashedDocDotFeatures : public CStreamingDotFeatures * input, getting examples one by one from the DotFeatures * object (and labels, if applicable). * - * @param dot_features CDotFeatures object + * @param dot_features DotFeatures object * @param tzer the tokenizer to use on the document collection * @param bits the number of bits of the new dimension (means a dimension of size 2^bits) * @param lab labels (optional) */ - CStreamingHashedDocDotFeatures(CStringFeatures* dot_features,CTokenizer* tzer, + StreamingHashedDocDotFeatures(std::shared_ptr> dot_features,std::shared_ptr tzer, int32_t bits=20, float64_t* lab=NULL); /** Destructor */ - virtual ~CStreamingHashedDocDotFeatures(); + virtual ~StreamingHashedDocDotFeatures(); /** compute dot product between vectors of two * StreamingDotFeatures objects. @@ -83,7 +83,7 @@ class CStreamingHashedDocDotFeatures : public CStreamingDotFeatures * @param df StreamingDotFeatures (of same kind) to compute * dot product with */ - virtual float32_t dot(CStreamingDotFeatures* df); + virtual float32_t dot(std::shared_ptr df); /** compute dot product between current vector and a dense vector * @@ -224,7 +224,7 @@ class CStreamingHashedDocDotFeatures : public CStreamingDotFeatures void set_k_skip_n_grams(int32_t k, int32_t n); private: - void init(CStreamingFile* file, bool is_labelled, int32_t size, CTokenizer* tzer, + void init(std::shared_ptr file, bool is_labelled, int32_t size, std::shared_ptr tzer, int32_t bits, bool normalize, int32_t n_grams, int32_t skips); protected: @@ -235,14 +235,14 @@ class CStreamingHashedDocDotFeatures : public CStreamingDotFeatures /** Current example */ SGSparseVector current_vector; - /** CTokenizer */ - CTokenizer *tokenizer; + /** Tokenizer */ + std::shared_ptrtokenizer; /** Converter */ - CHashedDocConverter* converter; + std::shared_ptr converter; /** The parser */ - CInputParser parser; + InputParser parser; /** The current example's label */ float64_t current_label; diff --git a/src/shogun/features/streaming/StreamingHashedSparseFeatures.cpp b/src/shogun/features/streaming/StreamingHashedSparseFeatures.cpp index 5f35cb57c11..f12d8c5808d 100644 --- a/src/shogun/features/streaming/StreamingHashedSparseFeatures.cpp +++ b/src/shogun/features/streaming/StreamingHashedSparseFeatures.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evangelos Anagnostopoulos, Heiko Strathmann, Bjoern Esser, + * Authors: Evangelos Anagnostopoulos, Heiko Strathmann, Bjoern Esser, * Sergey Lisitsyn, Viktor Gal */ @@ -13,26 +13,25 @@ namespace shogun { template -CStreamingHashedSparseFeatures::CStreamingHashedSparseFeatures() +StreamingHashedSparseFeatures::StreamingHashedSparseFeatures() { init(NULL, false, 0, 0, false, true); } template -CStreamingHashedSparseFeatures::CStreamingHashedSparseFeatures(CStreamingFile* file, +StreamingHashedSparseFeatures::StreamingHashedSparseFeatures(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms) { init(file, is_labelled, size, d, use_quadr, keep_lin_terms); } template -CStreamingHashedSparseFeatures::CStreamingHashedSparseFeatures(CSparseFeatures* dot_features, +StreamingHashedSparseFeatures::StreamingHashedSparseFeatures(std::shared_ptr> dot_features, int32_t d, bool use_quadr, bool keep_lin_terms, float64_t* lab) { ASSERT(dot_features); - CStreamingFileFromSparseFeatures* file = - new CStreamingFileFromSparseFeatures(dot_features, lab); + auto file = std::make_shared>(dot_features, lab); bool is_labelled = (lab != NULL); int32_t size = 1024; @@ -43,12 +42,12 @@ CStreamingHashedSparseFeatures::CStreamingHashedSparseFeatures(CSparseFeatur } template -CStreamingHashedSparseFeatures::~CStreamingHashedSparseFeatures() +StreamingHashedSparseFeatures::~StreamingHashedSparseFeatures() { } template -void CStreamingHashedSparseFeatures::init(CStreamingFile* file, bool is_labelled, +void StreamingHashedSparseFeatures::init(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms) { dim = d; @@ -64,7 +63,7 @@ void CStreamingHashedSparseFeatures::init(CStreamingFile* file, bool is_labe if (file) { working_file = file; - SG_REF(working_file); + parser.init(file, is_labelled, size); seekable = false; } @@ -78,18 +77,18 @@ void CStreamingHashedSparseFeatures::init(CStreamingFile* file, bool is_labe } template -float32_t CStreamingHashedSparseFeatures::dot(CStreamingDotFeatures* df) +float32_t StreamingHashedSparseFeatures::dot(std::shared_ptr df) { ASSERT(df); ASSERT(df->get_feature_type() == get_feature_type()) ASSERT(strcmp(df->get_name(),get_name())==0) - CStreamingHashedSparseFeatures* hdf = (CStreamingHashedSparseFeatures* ) df; + auto hdf = std::static_pointer_cast>(df); return current_vector.sparse_dot(hdf->current_vector); } template -float32_t CStreamingHashedSparseFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) +float32_t StreamingHashedSparseFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) { ASSERT(vec2_len == dim); @@ -101,88 +100,88 @@ float32_t CStreamingHashedSparseFeatures::dense_dot(const float32_t* vec2, i } template -void CStreamingHashedSparseFeatures::add_to_dense_vec(float32_t alpha, float32_t* vec2, +void StreamingHashedSparseFeatures::add_to_dense_vec(float32_t alpha, float32_t* vec2, int32_t vec2_len, bool abs_val) { ASSERT(vec2_len == dim); if (abs_val) - alpha = CMath::abs(alpha); + alpha = Math::abs(alpha); for (index_t i=0; i -int32_t CStreamingHashedSparseFeatures::get_dim_feature_space() const +int32_t StreamingHashedSparseFeatures::get_dim_feature_space() const { return dim; } template -const char* CStreamingHashedSparseFeatures::get_name() const +const char* StreamingHashedSparseFeatures::get_name() const { return "StreamingHashedSparseFeatures"; } template -int32_t CStreamingHashedSparseFeatures::get_num_vectors() const +int32_t StreamingHashedSparseFeatures::get_num_vectors() const { return 1; } template -void CStreamingHashedSparseFeatures::set_vector_reader() +void StreamingHashedSparseFeatures::set_vector_reader() { SG_DEBUG("called inside set_vector_reader"); - parser.set_read_vector(&CStreamingFile::get_sparse_vector); + parser.set_read_vector(&StreamingFile::get_sparse_vector); } template -void CStreamingHashedSparseFeatures::set_vector_and_label_reader() +void StreamingHashedSparseFeatures::set_vector_and_label_reader() { - parser.set_read_vector_and_label(&CStreamingFile::get_sparse_vector_and_label); + parser.set_read_vector_and_label(&StreamingFile::get_sparse_vector_and_label); } template -EFeatureType CStreamingHashedSparseFeatures::get_feature_type() const +EFeatureType StreamingHashedSparseFeatures::get_feature_type() const { return F_UINT; } template -EFeatureClass CStreamingHashedSparseFeatures::get_feature_class() const +EFeatureClass StreamingHashedSparseFeatures::get_feature_class() const { return C_STREAMING_SPARSE; } template -void CStreamingHashedSparseFeatures::start_parser() +void StreamingHashedSparseFeatures::start_parser() { if (!parser.is_running()) parser.start_parser(); } template -void CStreamingHashedSparseFeatures::end_parser() +void StreamingHashedSparseFeatures::end_parser() { parser.end_parser(); } template -float64_t CStreamingHashedSparseFeatures::get_label() +float64_t StreamingHashedSparseFeatures::get_label() { return current_label; } template -bool CStreamingHashedSparseFeatures::get_next_example() +bool StreamingHashedSparseFeatures::get_next_example() { SGSparseVector tmp; if (parser.get_next_example(tmp.features, tmp.num_feat_entries, current_label)) { - current_vector = CHashedSparseFeatures::hash_vector(tmp, dim, + current_vector = HashedSparseFeatures::hash_vector(tmp, dim, use_quadratic, keep_linear_terms); tmp.features = NULL; tmp.num_feat_entries = -1; @@ -192,34 +191,34 @@ bool CStreamingHashedSparseFeatures::get_next_example() } template -void CStreamingHashedSparseFeatures::release_example() +void StreamingHashedSparseFeatures::release_example() { parser.finalize_example(); } template -int32_t CStreamingHashedSparseFeatures::get_num_features() +int32_t StreamingHashedSparseFeatures::get_num_features() { return dim; } template -SGSparseVector CStreamingHashedSparseFeatures::get_vector() +SGSparseVector StreamingHashedSparseFeatures::get_vector() { return current_vector; } -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; -template class CStreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; +template class StreamingHashedSparseFeatures; } diff --git a/src/shogun/features/streaming/StreamingHashedSparseFeatures.h b/src/shogun/features/streaming/StreamingHashedSparseFeatures.h index cdf3c9a049e..153dd78a47c 100644 --- a/src/shogun/features/streaming/StreamingHashedSparseFeatures.h +++ b/src/shogun/features/streaming/StreamingHashedSparseFeatures.h @@ -15,9 +15,9 @@ namespace shogun { -class CStreamingDotFeatures; +class StreamingDotFeatures; -/** @brief This class acts as an alternative to the CStreamingSparseFeatures class +/** @brief This class acts as an alternative to the StreamingSparseFeatures class * and their difference is that the current example in this class is hashed into * a smaller dimension dim. * @@ -25,11 +25,11 @@ class CStreamingDotFeatures; * and current_label. Call get_next_example() followed by get_current_vector() * to iterate through the stream. */ -template class CStreamingHashedSparseFeatures : public CStreamingDotFeatures +template class StreamingHashedSparseFeatures : public StreamingDotFeatures { public: /** Constructor */ - CStreamingHashedSparseFeatures(); + StreamingHashedSparseFeatures(); /** * Constructor with input information passed. @@ -41,11 +41,11 @@ template class CStreamingHashedSparseFeatures : public CStreamingDotF * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations */ - CStreamingHashedSparseFeatures (CStreamingFile* file, bool is_labelled, int32_t size, + StreamingHashedSparseFeatures (std::shared_ptr file, bool is_labelled, int32_t size, int32_t d = 512, bool use_quadr = false, bool keep_lin_terms = true); /** - * Constructor taking a CDotFeatures object and optionally, + * Constructor taking a DotFeatures object and optionally, * labels, as args. * * The derived class should implement it so that the @@ -53,17 +53,17 @@ template class CStreamingHashedSparseFeatures : public CStreamingDotF * input, getting examples one by one from the DotFeatures * object (and labels, if applicable). * - * @param dot_features CDotFeatures object + * @param dot_features DotFeatures object * @param d the dimensionality of the target feature space * @param use_quadr whether to use quadratic features or not * @param keep_lin_terms whether to maintain the linear terms in the computations * @param lab labels (optional) */ - CStreamingHashedSparseFeatures (CSparseFeatures* dot_features, int32_t d = 512, + StreamingHashedSparseFeatures (std::shared_ptr> dot_features, int32_t d = 512, bool use_quadr = false, bool keep_lin_terms = true, float64_t* lab = NULL); /** Destructor */ - virtual ~CStreamingHashedSparseFeatures (); + virtual ~StreamingHashedSparseFeatures (); /** compute dot product between vectors of two * StreamingDotFeatures objects. @@ -71,7 +71,7 @@ template class CStreamingHashedSparseFeatures : public CStreamingDotF * @param df StreamingDotFeatures (of same kind) to compute * dot product with */ - virtual float32_t dot(CStreamingDotFeatures* df); + virtual float32_t dot(std::shared_ptr df); /** compute dot product between current vector and a dense vector * @@ -197,7 +197,7 @@ template class CStreamingHashedSparseFeatures : public CStreamingDotF SGSparseVector get_vector(); private: - void init(CStreamingFile* file, bool is_labelled, int32_t size, + void init(std::shared_ptr file, bool is_labelled, int32_t size, int32_t d, bool use_quadr, bool keep_lin_terms); protected: @@ -209,7 +209,7 @@ template class CStreamingHashedSparseFeatures : public CStreamingDotF SGSparseVector current_vector; /** The parser */ - CInputParser > parser; + InputParser > parser; /** The current example's label */ float64_t current_label; diff --git a/src/shogun/features/streaming/StreamingSparseFeatures.cpp b/src/shogun/features/streaming/StreamingSparseFeatures.cpp index 607a3d31153..bc8c512b6ab 100644 --- a/src/shogun/features/streaming/StreamingSparseFeatures.cpp +++ b/src/shogun/features/streaming/StreamingSparseFeatures.cpp @@ -12,44 +12,44 @@ namespace shogun { template -CStreamingSparseFeatures::CStreamingSparseFeatures() : CStreamingDotFeatures() +StreamingSparseFeatures::StreamingSparseFeatures() : StreamingDotFeatures() { set_read_functions(); init(); } template -CStreamingSparseFeatures::CStreamingSparseFeatures(CStreamingFile* file, +StreamingSparseFeatures::StreamingSparseFeatures(std::shared_ptr file, bool is_labelled, int32_t size) - : CStreamingDotFeatures() + : StreamingDotFeatures() { set_read_functions(); init(file, is_labelled, size); } template -CStreamingSparseFeatures::~CStreamingSparseFeatures() +StreamingSparseFeatures::~StreamingSparseFeatures() { if (parser.is_running()) parser.end_parser(); } template -T CStreamingSparseFeatures::get_feature(int32_t index) +T StreamingSparseFeatures::get_feature(int32_t index) { ASSERT(index>=0 && index -void CStreamingSparseFeatures::reset_stream() +void StreamingSparseFeatures::reset_stream() { not_implemented(SOURCE_LOCATION); } template -int32_t CStreamingSparseFeatures::set_num_features(int32_t num) +int32_t StreamingSparseFeatures::set_num_features(int32_t num) { int32_t n=current_num_features; ASSERT(n<=num) @@ -58,7 +58,7 @@ int32_t CStreamingSparseFeatures::set_num_features(int32_t num) } template -T CStreamingSparseFeatures::sparse_dot(T alpha, SGSparseVectorEntry* avec, int32_t alen, SGSparseVectorEntry* bvec, int32_t blen) +T StreamingSparseFeatures::sparse_dot(T alpha, SGSparseVectorEntry* avec, int32_t alen, SGSparseVectorEntry* bvec, int32_t blen) { T result=0; @@ -75,7 +75,7 @@ T CStreamingSparseFeatures::sparse_dot(T alpha, SGSparseVectorEntry* avec, } template -T CStreamingSparseFeatures::dense_dot(T alpha, T* vec, int32_t dim, T b) +T StreamingSparseFeatures::dense_dot(T alpha, T* vec, int32_t dim, T b) { ASSERT(vec) ASSERT(dim>=current_num_features) @@ -84,7 +84,7 @@ T CStreamingSparseFeatures::dense_dot(T alpha, T* vec, int32_t dim, T b) } template -float64_t CStreamingSparseFeatures::dense_dot(const float64_t* vec2, int32_t vec2_len) +float64_t StreamingSparseFeatures::dense_dot(const float64_t* vec2, int32_t vec2_len) { ASSERT(vec2) @@ -105,7 +105,7 @@ float64_t CStreamingSparseFeatures::dense_dot(const float64_t* vec2, int32_t } template -float32_t CStreamingSparseFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) +float32_t StreamingSparseFeatures::dense_dot(const float32_t* vec2, int32_t vec2_len) { ASSERT(vec2) @@ -126,7 +126,7 @@ float32_t CStreamingSparseFeatures::dense_dot(const float32_t* vec2, int32_t } template -void CStreamingSparseFeatures::add_to_dense_vec(float64_t alpha, float64_t* vec2, int32_t vec2_len, bool abs_val) +void StreamingSparseFeatures::add_to_dense_vec(float64_t alpha, float64_t* vec2, int32_t vec2_len, bool abs_val) { ASSERT(vec2) if (vec2_len < current_num_features) @@ -143,7 +143,7 @@ void CStreamingSparseFeatures::add_to_dense_vec(float64_t alpha, float64_t* v if (abs_val) { for (int32_t i=0; i::add_to_dense_vec(float64_t alpha, float64_t* v } template -void CStreamingSparseFeatures::add_to_dense_vec(float32_t alpha, float32_t* vec2, int32_t vec2_len, bool abs_val) +void StreamingSparseFeatures::add_to_dense_vec(float32_t alpha, float32_t* vec2, int32_t vec2_len, bool abs_val) { ASSERT(vec2) if (vec2_len < current_num_features) @@ -171,7 +171,7 @@ void CStreamingSparseFeatures::add_to_dense_vec(float32_t alpha, float32_t* v if (abs_val) { for (int32_t i=0; i::add_to_dense_vec(float32_t alpha, float32_t* v } template -int64_t CStreamingSparseFeatures::get_num_nonzero_entries() +int64_t StreamingSparseFeatures::get_num_nonzero_entries() { return current_sgvector.num_feat_entries; } template -float32_t CStreamingSparseFeatures::compute_squared() +float32_t StreamingSparseFeatures::compute_squared() { int32_t current_length = current_sgvector.num_feat_entries; SGSparseVectorEntry* current_vector = current_sgvector.features; @@ -204,7 +204,7 @@ float32_t CStreamingSparseFeatures::compute_squared() } template -void CStreamingSparseFeatures::sort_features() +void StreamingSparseFeatures::sort_features() { SGSparseVectorEntry* old_ptr = current_sgvector.features; @@ -216,26 +216,26 @@ void CStreamingSparseFeatures::sort_features() } template -int32_t CStreamingSparseFeatures::get_num_vectors() const +int32_t StreamingSparseFeatures::get_num_vectors() const { if (current_sgvector.features) return 1; return 0; } -template void CStreamingSparseFeatures::set_vector_reader() +template void StreamingSparseFeatures::set_vector_reader() { - parser.set_read_vector(&CStreamingFile::get_sparse_vector); + parser.set_read_vector(&StreamingFile::get_sparse_vector); } -template void CStreamingSparseFeatures::set_vector_and_label_reader() +template void StreamingSparseFeatures::set_vector_and_label_reader() { parser.set_read_vector_and_label - (&CStreamingFile::get_sparse_vector_and_label); + (&StreamingFile::get_sparse_vector_and_label); } #define GET_FEATURE_TYPE(f_type, sg_type) \ -template<> EFeatureType CStreamingSparseFeatures::get_feature_type() const \ +template<> EFeatureType StreamingSparseFeatures::get_feature_type() const \ { \ return f_type; \ } @@ -257,7 +257,7 @@ GET_FEATURE_TYPE(F_LONGREAL, floatmax_t) template -void CStreamingSparseFeatures::init() +void StreamingSparseFeatures::init() { working_file=NULL; current_vec_index=0; @@ -267,33 +267,33 @@ void CStreamingSparseFeatures::init() } template -void CStreamingSparseFeatures::init(CStreamingFile* file, +void StreamingSparseFeatures::init(std::shared_ptr file, bool is_labelled, int32_t size) { init(); has_labels = is_labelled; working_file = file; - SG_REF(working_file); + parser.init(file, is_labelled, size); parser.set_free_vector_after_release(false); } template -void CStreamingSparseFeatures::start_parser() +void StreamingSparseFeatures::start_parser() { if (!parser.is_running()) parser.start_parser(); } template -void CStreamingSparseFeatures::end_parser() +void StreamingSparseFeatures::end_parser() { parser.end_parser(); } template -bool CStreamingSparseFeatures::get_next_example() +bool StreamingSparseFeatures::get_next_example() { int32_t current_length = 0; SGSparseVectorEntry* current_vector = NULL; @@ -311,20 +311,20 @@ bool CStreamingSparseFeatures::get_next_example() // Update number of features based on highest index int32_t current_dimension = get_vector().get_num_dimensions(); - current_num_features = CMath::max(current_num_features, current_dimension); + current_num_features = Math::max(current_num_features, current_dimension); current_vec_index++; return true; } template -SGSparseVector CStreamingSparseFeatures::get_vector() +SGSparseVector StreamingSparseFeatures::get_vector() { return current_sgvector; } template -float64_t CStreamingSparseFeatures::get_label() +float64_t StreamingSparseFeatures::get_label() { ASSERT(has_labels) @@ -332,53 +332,53 @@ float64_t CStreamingSparseFeatures::get_label() } template -void CStreamingSparseFeatures::release_example() +void StreamingSparseFeatures::release_example() { parser.finalize_example(); } template -int32_t CStreamingSparseFeatures::get_dim_feature_space() const +int32_t StreamingSparseFeatures::get_dim_feature_space() const { return current_num_features; } template - float32_t CStreamingSparseFeatures::dot(CStreamingDotFeatures* df) + float32_t StreamingSparseFeatures::dot(std::shared_ptr df) { not_implemented(SOURCE_LOCATION); return -1; } template -int32_t CStreamingSparseFeatures::get_num_features() +int32_t StreamingSparseFeatures::get_num_features() { return current_num_features; } template -int32_t CStreamingSparseFeatures::get_nnz_features_for_vector() +int32_t StreamingSparseFeatures::get_nnz_features_for_vector() { return current_sgvector.num_feat_entries; } template -EFeatureClass CStreamingSparseFeatures::get_feature_class() const +EFeatureClass StreamingSparseFeatures::get_feature_class() const { return C_STREAMING_SPARSE; } -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; -template class CStreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; +template class StreamingSparseFeatures; } diff --git a/src/shogun/features/streaming/StreamingSparseFeatures.h b/src/shogun/features/streaming/StreamingSparseFeatures.h index 66d44b2038b..e69810fd98a 100644 --- a/src/shogun/features/streaming/StreamingSparseFeatures.h +++ b/src/shogun/features/streaming/StreamingSparseFeatures.h @@ -17,7 +17,7 @@ namespace shogun { -class CStreamingFile; +class StreamingFile; /** @brief This class implements streaming features with sparse feature vectors. * The vector is represented as an SGSparseVector. Each entry is of type @@ -41,7 +41,7 @@ class CStreamingFile; * array to the new dimensionality (if necessary), setting the newer dimensions * to zero, and updates the length parameter to equal the new length of the array. */ -template class CStreamingSparseFeatures : public CStreamingDotFeatures +template class StreamingSparseFeatures : public StreamingDotFeatures { public: @@ -52,7 +52,7 @@ template class CStreamingSparseFeatures : public CStreamingDotFeatures * CStreamingFile::get_*_vector and get_*_vector_and_label * depending on the type T. */ - CStreamingSparseFeatures(); + StreamingSparseFeatures(); /** * Constructor taking args. @@ -62,7 +62,7 @@ template class CStreamingSparseFeatures : public CStreamingDotFeatures * @param is_labelled Whether examples are labelled or not. * @param size Number of example objects to be stored in the parser at a time. */ - CStreamingSparseFeatures(CStreamingFile* file, + StreamingSparseFeatures(std::shared_ptr file, bool is_labelled, int32_t size); @@ -71,7 +71,7 @@ template class CStreamingSparseFeatures : public CStreamingDotFeatures * * Ends the parsing thread. (Waits for pthread_join to complete) */ - virtual ~CStreamingSparseFeatures(); + virtual ~StreamingSparseFeatures(); /** * Sets the read function (in case the examples are @@ -182,14 +182,14 @@ template class CStreamingSparseFeatures : public CStreamingDotFeatures /** * Dot product taken with another StreamingDotFeatures object. * - * Currently only works if it is a CStreamingSparseFeatures object. + * Currently only works if it is a StreamingSparseFeatures object. * It takes the dot product of the current_vectors of both objects. * * @param df CStreamingDotFeatures object. * * @return Dot product. */ - virtual float32_t dot(CStreamingDotFeatures *df); + virtual float32_t dot(std::shared_ptrdf); /** compute the dot product between two sparse feature vectors * alpha * vec^T * vec @@ -333,11 +333,11 @@ template class CStreamingSparseFeatures : public CStreamingDotFeatures * @param is_labelled whether labelled or not * @param size number of examples in the parser's ring */ - virtual void init(CStreamingFile *file, bool is_labelled, int32_t size); + virtual void init(std::shared_ptrfile, bool is_labelled, int32_t size); protected: /// The parser object, which reads from input and returns parsed example objects. - CInputParser< SGSparseVectorEntry > parser; + InputParser< SGSparseVectorEntry > parser; /// The current example's feature vector as an SGVector SGSparseVector current_sgvector; diff --git a/src/shogun/features/streaming/StreamingStringFeatures.cpp b/src/shogun/features/streaming/StreamingStringFeatures.cpp index d0ea2b1aca5..4d25b73b379 100644 --- a/src/shogun/features/streaming/StreamingStringFeatures.cpp +++ b/src/shogun/features/streaming/StreamingStringFeatures.cpp @@ -5,7 +5,7 @@ namespace shogun template -CStreamingStringFeatures::CStreamingStringFeatures() : CStreamingFeatures() +StreamingStringFeatures::StreamingStringFeatures() : StreamingFeatures() { init(); set_read_functions(); @@ -13,10 +13,10 @@ CStreamingStringFeatures::CStreamingStringFeatures() : CStreamingFeatures() } template -CStreamingStringFeatures::CStreamingStringFeatures(CStreamingFile* file, +StreamingStringFeatures::StreamingStringFeatures(std::shared_ptr file, bool is_labelled, int32_t size) - : CStreamingFeatures() + : StreamingFeatures() { init(file, is_labelled, size); set_read_functions(); @@ -24,64 +24,64 @@ CStreamingStringFeatures::CStreamingStringFeatures(CStreamingFile* file, } template -CStreamingStringFeatures::~CStreamingStringFeatures() +StreamingStringFeatures::~StreamingStringFeatures() { if (parser.is_running()) parser.end_parser(); - SG_UNREF(alphabet); + } template -void CStreamingStringFeatures::use_alphabet(EAlphabet alpha) +void StreamingStringFeatures::use_alphabet(EAlphabet alpha) { - SG_UNREF(alphabet); - alphabet=new CAlphabet(alpha); - SG_REF(alphabet); + + alphabet=std::make_shared(alpha); + num_symbols=alphabet->get_num_symbols(); } template -void CStreamingStringFeatures::use_alphabet(CAlphabet* alpha) +void StreamingStringFeatures::use_alphabet(std::shared_ptr alpha) { - SG_UNREF(alphabet); - alphabet=new CAlphabet(alpha); - SG_REF(alphabet); + + alphabet=std::make_shared(alpha); + num_symbols=alphabet->get_num_symbols(); } template -void CStreamingStringFeatures::set_remap(CAlphabet* ascii_alphabet, CAlphabet* binary_alphabet) +void StreamingStringFeatures::set_remap(std::shared_ptr ascii_alphabet, std::shared_ptr binary_alphabet) { remap_to_bin=true; - alpha_ascii=new CAlphabet(ascii_alphabet); - alpha_bin=new CAlphabet(binary_alphabet); + alpha_ascii=std::make_shared(ascii_alphabet); + alpha_bin=std::make_shared(binary_alphabet); } template -void CStreamingStringFeatures::set_remap(EAlphabet ascii_alphabet, EAlphabet binary_alphabet) +void StreamingStringFeatures::set_remap(EAlphabet ascii_alphabet, EAlphabet binary_alphabet) { remap_to_bin=true; - alpha_ascii=new CAlphabet(ascii_alphabet); - alpha_bin=new CAlphabet(binary_alphabet); + alpha_ascii=std::make_shared(ascii_alphabet); + alpha_bin=std::make_shared(binary_alphabet); } template -CAlphabet* CStreamingStringFeatures::get_alphabet() +std::shared_ptr StreamingStringFeatures::get_alphabet() { - SG_REF(alphabet); + return alphabet; } template -floatmax_t CStreamingStringFeatures::get_num_symbols() +floatmax_t StreamingStringFeatures::get_num_symbols() { return num_symbols; } template -int32_t CStreamingStringFeatures::get_num_vectors() const +int32_t StreamingStringFeatures::get_num_vectors() const { if (current_string.vector) return 1; @@ -89,24 +89,24 @@ int32_t CStreamingStringFeatures::get_num_vectors() const } template -int32_t CStreamingStringFeatures::get_num_features() +int32_t StreamingStringFeatures::get_num_features() { return current_string.size(); } -template void CStreamingStringFeatures::set_vector_reader() +template void StreamingStringFeatures::set_vector_reader() { - parser.set_read_vector(&CStreamingFile::get_string); + parser.set_read_vector(&StreamingFile::get_string); } -template void CStreamingStringFeatures::set_vector_and_label_reader() +template void StreamingStringFeatures::set_vector_and_label_reader() { parser.set_read_vector_and_label - (&CStreamingFile::get_string_and_label); + (&StreamingFile::get_string_and_label); } #define GET_FEATURE_TYPE(f_type, sg_type) \ -template<> EFeatureType CStreamingStringFeatures::get_feature_type() const \ +template<> EFeatureType StreamingStringFeatures::get_feature_type() const \ { \ return f_type; \ } @@ -128,17 +128,17 @@ GET_FEATURE_TYPE(F_LONGREAL, floatmax_t) template -void CStreamingStringFeatures::init() +void StreamingStringFeatures::init() { working_file=NULL; - alphabet=new CAlphabet(); + alphabet=std::make_shared(); current_string=SGVector(nullptr, -1, false); set_generic(); } template -void CStreamingStringFeatures::init(CStreamingFile* file, +void StreamingStringFeatures::init(std::shared_ptr file, bool is_labelled, int32_t size) { @@ -151,7 +151,7 @@ void CStreamingStringFeatures::init(CStreamingFile* file, } template -void CStreamingStringFeatures::start_parser() +void StreamingStringFeatures::start_parser() { if (!remap_to_bin) alpha_ascii=alphabet; @@ -161,13 +161,13 @@ void CStreamingStringFeatures::start_parser() } template -void CStreamingStringFeatures::end_parser() +void StreamingStringFeatures::end_parser() { parser.end_parser(); } template -bool CStreamingStringFeatures::get_next_example() +bool StreamingStringFeatures::get_next_example() { bool ret_value; @@ -213,13 +213,13 @@ bool CStreamingStringFeatures::get_next_example() } template -SGVector CStreamingStringFeatures::get_vector() +SGVector StreamingStringFeatures::get_vector() { return current_string; } template -float64_t CStreamingStringFeatures::get_label() +float64_t StreamingStringFeatures::get_label() { ASSERT(has_labels) @@ -227,35 +227,35 @@ float64_t CStreamingStringFeatures::get_label() } template -void CStreamingStringFeatures::release_example() +void StreamingStringFeatures::release_example() { parser.finalize_example(); } template -int32_t CStreamingStringFeatures::get_vector_length() +int32_t StreamingStringFeatures::get_vector_length() { return current_string.size(); } template -EFeatureClass CStreamingStringFeatures::get_feature_class() const +EFeatureClass StreamingStringFeatures::get_feature_class() const { return C_STREAMING_STRING; } -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; -template class CStreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; +template class StreamingStringFeatures; } diff --git a/src/shogun/features/streaming/StreamingStringFeatures.h b/src/shogun/features/streaming/StreamingStringFeatures.h index e1475a34932..796d181867e 100644 --- a/src/shogun/features/streaming/StreamingStringFeatures.h +++ b/src/shogun/features/streaming/StreamingStringFeatures.h @@ -24,7 +24,7 @@ namespace shogun /** @brief This class implements streaming features as strings. * */ -template class CStreamingStringFeatures : public CStreamingFeatures +template class StreamingStringFeatures : public StreamingFeatures { public: @@ -35,7 +35,7 @@ template class CStreamingStringFeatures : public CStreamingFeatures * CStreamingFile::get_*_vector and get_*_vector_and_label * depending on the type T. */ - CStreamingStringFeatures(); + StreamingStringFeatures(); /** * Constructor taking args. @@ -45,7 +45,7 @@ template class CStreamingStringFeatures : public CStreamingFeatures * @param is_labelled Whether examples are labelled or not. * @param size Number of example objects to be stored in the parser at a time. */ - CStreamingStringFeatures(CStreamingFile* file, + StreamingStringFeatures(std::shared_ptr file, bool is_labelled, int32_t size); @@ -54,7 +54,7 @@ template class CStreamingStringFeatures : public CStreamingFeatures * * Ends the parsing thread. (Waits for pthread_join to complete) */ - virtual ~CStreamingStringFeatures(); + virtual ~StreamingStringFeatures(); /** * Sets the read function (in case the examples are @@ -90,18 +90,18 @@ template class CStreamingStringFeatures : public CStreamingFeatures * Set the alphabet to be used. * Call before parsing. * - * @param alpha alphabet as a pointer to a CAlphabet object. + * @param alpha alphabet as a pointer to a Alphabet object. */ - void use_alphabet(CAlphabet* alpha); + void use_alphabet(std::shared_ptr alpha); /** * Set whether remapping to another alphabet is required. * * Call before parsing. - * @param ascii_alphabet the alphabet to convert from, CAlphabet* - * @param binary_alphabet the alphabet to convert to, CAlphabet* + * @param ascii_alphabet the alphabet to convert from, Alphabet* + * @param binary_alphabet the alphabet to convert to, Alphabet* */ - void set_remap(CAlphabet* ascii_alphabet, CAlphabet* binary_alphabet); + void set_remap(std::shared_ptr ascii_alphabet, std::shared_ptr binary_alphabet); /** * Set whether remapping to another alphabet is required. @@ -113,10 +113,10 @@ template class CStreamingStringFeatures : public CStreamingFeatures void set_remap(EAlphabet ascii_alphabet=DNA, EAlphabet binary_alphabet=RAWDNA); /** - * Return the alphabet being used as a CAlphabet* + * Return the alphabet being used as a Alphabet* * @return */ - CAlphabet* get_alphabet(); + std::shared_ptr get_alphabet(); /** get number of symbols * @@ -231,21 +231,21 @@ template class CStreamingStringFeatures : public CStreamingFeatures * @param is_labelled whether labelled or not * @param size number of examples in the parser's ring */ - void init(CStreamingFile *file, bool is_labelled, int32_t size); + void init(std::shared_ptrfile, bool is_labelled, int32_t size); protected: /// The parser object, which reads from input and returns parsed example objects. - CInputParser parser; + InputParser parser; /// Alphabet to use - CAlphabet* alphabet; + std::shared_ptr alphabet; /// If remapping is enabled, this is the source alphabet - CAlphabet* alpha_ascii; + std::shared_ptr alpha_ascii; /// If remapping is enabled, this is the target alphabet - CAlphabet* alpha_bin; + std::shared_ptr alpha_bin; /// The current example's string SGVector current_string; diff --git a/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.cpp b/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.cpp index 95e140cdea9..df26e124625 100644 --- a/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.cpp +++ b/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.cpp @@ -10,25 +10,25 @@ using namespace shogun; -CGaussianBlobsDataGenerator::CGaussianBlobsDataGenerator() : - RandomMixin>() +GaussianBlobsDataGenerator::GaussianBlobsDataGenerator() : + RandomMixin>() { init(); } -CGaussianBlobsDataGenerator::CGaussianBlobsDataGenerator(index_t sqrt_num_blobs, +GaussianBlobsDataGenerator::GaussianBlobsDataGenerator(index_t sqrt_num_blobs, float64_t distance, float64_t stretch, float64_t angle) : - RandomMixin>() + RandomMixin>() { init(); set_blobs_model(sqrt_num_blobs, distance, stretch, angle); } -CGaussianBlobsDataGenerator::~CGaussianBlobsDataGenerator() +GaussianBlobsDataGenerator::~GaussianBlobsDataGenerator() { } -void CGaussianBlobsDataGenerator::set_blobs_model(index_t sqrt_num_blobs, +void GaussianBlobsDataGenerator::set_blobs_model(index_t sqrt_num_blobs, float64_t distance, float64_t stretch, float64_t angle) { m_sqrt_num_blobs=sqrt_num_blobs; @@ -54,7 +54,7 @@ void CGaussianBlobsDataGenerator::set_blobs_model(index_t sqrt_num_blobs, m_cholesky=SGMatrix::matrix_multiply(R, L); } -void CGaussianBlobsDataGenerator::init() +void GaussianBlobsDataGenerator::init() { SG_ADD(&m_sqrt_num_blobs, "sqrt_num_blobs", "Number of Blobs per row"); SG_ADD(&m_distance, "distance", "Distance between blobs"); @@ -75,9 +75,9 @@ void CGaussianBlobsDataGenerator::init() unset_generic(); } -bool CGaussianBlobsDataGenerator::get_next_example() +bool GaussianBlobsDataGenerator::get_next_example() { - SG_TRACE("entering CGaussianBlobsDataGenerator::get_next_example()"); + SG_TRACE("entering GaussianBlobsDataGenerator::get_next_example()"); /* allocate space */ SGVector result=SGVector(2); @@ -97,15 +97,15 @@ bool CGaussianBlobsDataGenerator::get_next_example() result[1]=m_cholesky(1, 0)*x+m_cholesky(1, 1)*y+y_offset; /* save example back to superclass */ - CGaussianBlobsDataGenerator::current_vector=result; + GaussianBlobsDataGenerator::current_vector=result; - SG_TRACE("leaving CGaussianBlobsDataGenerator::get_next_example()"); + SG_TRACE("leaving GaussianBlobsDataGenerator::get_next_example()"); return true; } -void CGaussianBlobsDataGenerator::release_example() +void GaussianBlobsDataGenerator::release_example() { SGVector temp=SGVector(); - CGaussianBlobsDataGenerator::current_vector=temp; + GaussianBlobsDataGenerator::current_vector=temp; } diff --git a/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.h b/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.h index fdaa7e537c3..4ee9e057406 100644 --- a/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.h +++ b/src/shogun/features/streaming/generators/GaussianBlobsDataGenerator.h @@ -27,19 +27,19 @@ namespace shogun * is a 2D grid-like mixture of a number Gaussians at a certain distance from each * other, and where each Gaussian is stretched and rotated by a factor. */ -class CGaussianBlobsDataGenerator: public RandomMixin> +class GaussianBlobsDataGenerator: public RandomMixin> { public: /** Constructor */ - CGaussianBlobsDataGenerator(); + GaussianBlobsDataGenerator(); /** Constructor */ - CGaussianBlobsDataGenerator(index_t sqrt_num_blobs, float64_t distance, + GaussianBlobsDataGenerator(index_t sqrt_num_blobs, float64_t distance, float64_t stretch, float64_t angle); /** Destructor */ - virtual ~CGaussianBlobsDataGenerator(); + virtual ~GaussianBlobsDataGenerator(); /** @return name of SG_SERIALIZABLE */ virtual const char* get_name() const diff --git a/src/shogun/features/streaming/generators/MeanShiftDataGenerator.cpp b/src/shogun/features/streaming/generators/MeanShiftDataGenerator.cpp index 9628c4db0fd..186e016ee90 100644 --- a/src/shogun/features/streaming/generators/MeanShiftDataGenerator.cpp +++ b/src/shogun/features/streaming/generators/MeanShiftDataGenerator.cpp @@ -11,25 +11,25 @@ using namespace shogun; -CMeanShiftDataGenerator::CMeanShiftDataGenerator() : - RandomMixin>() +MeanShiftDataGenerator::MeanShiftDataGenerator() : + RandomMixin>() { init(); } -CMeanShiftDataGenerator::CMeanShiftDataGenerator(float64_t mean_shift, +MeanShiftDataGenerator::MeanShiftDataGenerator(float64_t mean_shift, index_t dimension, index_t dimension_shift) : - RandomMixin>() + RandomMixin>() { init(); set_mean_shift_model(mean_shift, dimension, dimension_shift); } -CMeanShiftDataGenerator::~CMeanShiftDataGenerator() +MeanShiftDataGenerator::~MeanShiftDataGenerator() { } -void CMeanShiftDataGenerator::set_mean_shift_model(float64_t mean_shift, +void MeanShiftDataGenerator::set_mean_shift_model(float64_t mean_shift, index_t dimension, index_t dimension_shift) { require(dimension_shift temp=SGVector(); - CMeanShiftDataGenerator::current_vector=temp; + MeanShiftDataGenerator::current_vector=temp; } diff --git a/src/shogun/features/streaming/generators/MeanShiftDataGenerator.h b/src/shogun/features/streaming/generators/MeanShiftDataGenerator.h index 0d069ab9131..17f16b727a9 100644 --- a/src/shogun/features/streaming/generators/MeanShiftDataGenerator.h +++ b/src/shogun/features/streaming/generators/MeanShiftDataGenerator.h @@ -29,11 +29,11 @@ namespace shogun * multivariate isotropic Gaussian in a certain dimensions, where one selected * dimension has its mean shifted by some value. */ -class CMeanShiftDataGenerator: public RandomMixin> +class MeanShiftDataGenerator: public RandomMixin> { public: /** Constructor */ - CMeanShiftDataGenerator(); + MeanShiftDataGenerator(); /** Constructor * @@ -41,11 +41,11 @@ class CMeanShiftDataGenerator: public RandomMixin*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void BinaryFile::fname(SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ if (!(file)) \ error("File invalid."); \ @@ -157,7 +157,7 @@ void CBinaryFile::fname(SGSparseVector*& matrix, int32_t& num_feat, int if (fread(vec, sizeof(SGSparseVectorEntry), len, file)!= (size_t) len) \ error("Failed to read sparse vector {}", i); \ matrix[i].features=vec; \ - num_feat = CMath::max(num_feat, matrix[i].get_num_dimensions()); \ + num_feat = Math::max(num_feat, matrix[i].get_num_dimensions()); \ } \ } GET_SPARSEMATRIX(get_sparse_matrix, bool, TSGDataType(CT_MATRIX, ST_NONE, PT_BOOL)) @@ -177,7 +177,7 @@ GET_SPARSEMATRIX(get_sparse_matrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, #define GET_STRING_LIST(fname, sg_type, datatype) \ -void CBinaryFile::fname(SGVector*& strings, int32_t& num_str, int32_t& max_string_len) \ +void BinaryFile::fname(SGVector*& strings, int32_t& num_str, int32_t& max_string_len) \ { \ strings=NULL; \ num_str=0; \ @@ -223,7 +223,7 @@ GET_STRING_LIST(get_string_list, floatmax_t, TSGDataType(CT_VECTOR, ST_NONE, PT_ /** set functions - to pass data from shogun to the target interface */ #define SET_VECTOR(fname, sg_type, dtype) \ -void CBinaryFile::fname(const sg_type* vec, int32_t len) \ +void BinaryFile::fname(const sg_type* vec, int32_t len) \ { \ if (!(file && vec)) \ error("File or vector invalid."); \ @@ -249,7 +249,7 @@ SET_VECTOR(set_vector, uint64_t, (CT_VECTOR, ST_NONE, PT_UINT64)) #undef SET_VECTOR #define SET_MATRIX(fname, sg_type, dtype) \ -void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ +void BinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ { \ if (!(file && matrix)) \ error("File or matrix invalid."); \ @@ -276,7 +276,7 @@ SET_MATRIX(set_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX)) #undef SET_MATRIX #define SET_NDARRAY(fname,sg_type,datatype) \ -void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \ +void BinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \ { \ size_t total = 1; \ \ @@ -312,7 +312,7 @@ SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64)); #undef SET_NDARRAY #define SET_SPARSEMATRIX(fname, sg_type, dtype) \ -void CBinaryFile::fname(const SGSparseVector* matrix, \ +void BinaryFile::fname(const SGSparseVector* matrix, \ int32_t num_feat, int32_t num_vec) \ { \ if (!(file && matrix)) \ @@ -348,7 +348,7 @@ SET_SPARSEMATRIX(set_sparse_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX #undef SET_SPARSEMATRIX #define SET_STRING_LIST(fname, sg_type, dtype) \ -void CBinaryFile::fname(const SGVector* strings, int32_t num_str) \ +void BinaryFile::fname(const SGVector* strings, int32_t num_str) \ { \ if (!(file && strings)) \ error("File or strings invalid."); \ @@ -377,18 +377,18 @@ SET_STRING_LIST(set_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX)) #undef SET_STRING_LIST -int32_t CBinaryFile::parse_first_header(TSGDataType& type) +int32_t BinaryFile::parse_first_header(TSGDataType& type) { return -1; } -int32_t CBinaryFile::parse_next_header(TSGDataType& type) +int32_t BinaryFile::parse_next_header(TSGDataType& type) { return -1; } void -CBinaryFile::read_header(TSGDataType* dest) +BinaryFile::read_header(TSGDataType* dest) { ASSERT(file) ASSERT(dest) @@ -414,7 +414,7 @@ CBinaryFile::read_header(TSGDataType* dest) } void -CBinaryFile::write_header(const TSGDataType* datatype) +BinaryFile::write_header(const TSGDataType* datatype) { ASSERT(file) diff --git a/src/shogun/io/BinaryFile.h b/src/shogun/io/BinaryFile.h index b32bd4a05df..144fe6b6fb3 100644 --- a/src/shogun/io/BinaryFile.h +++ b/src/shogun/io/BinaryFile.h @@ -24,18 +24,18 @@ template class SGSparseVector; * data. The current implementation is capable of storing only a single * header/data type. Multiple headers are currently not implemented. */ -class CBinaryFile: public CFile +class BinaryFile: public File { public: /** default constructor */ - CBinaryFile(); + BinaryFile(); /** constructor * * @param f already opened file * @param name variable name (e.g. "x" or "/path/to/x") */ - CBinaryFile(FILE* f, const char* name=NULL); + BinaryFile(FILE* f, const char* name=NULL); /** constructor * @@ -43,10 +43,10 @@ class CBinaryFile: public CFile * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CBinaryFile(const char* fname, char rw='r', const char* name=NULL); + BinaryFile(const char* fname, char rw='r', const char* name=NULL); /** default destructor */ - virtual ~CBinaryFile(); + virtual ~BinaryFile(); #ifndef SWIG // SWIG should skip this /** @name Vector Access Functions @@ -399,7 +399,7 @@ class CBinaryFile: public CFile */ template DT* load_data(DT* target, int64_t& num) { - CSimpleFile
f(filename, file); + SimpleFile
f(filename, file); return f.load(target, num); } @@ -411,7 +411,7 @@ class CBinaryFile: public CFile */ template bool save_data(DT* src, int64_t num) { - CSimpleFile
f(filename, file); + SimpleFile
f(filename, file); return f.save(src, num); } }; diff --git a/src/shogun/io/BinaryStream.h b/src/shogun/io/BinaryStream.h index 44d13ff5ce7..41bd1dbb992 100644 --- a/src/shogun/io/BinaryStream.h +++ b/src/shogun/io/BinaryStream.h @@ -21,15 +21,15 @@ namespace shogun { /** @brief memory mapped emulation via binary streams (files) * -* Implements memory mapped file emulation (\sa CMemoryMappedFile) via standard +* Implements memory mapped file emulation (\sa MemoryMappedFile) via standard * file operations like fseek, fread etc */ -template class CBinaryStream : public CSGObject +template class BinaryStream : public SGObject { public: /** default constructor */ - CBinaryStream() : CSGObject() + BinaryStream() : SGObject() { rw = NULL; m_fname = NULL; @@ -46,8 +46,8 @@ template class CBinaryStream : public CSGObject * @param fname name of file, zero terminated string * @param flag determines read or read write mode (currently only 'r' is supported) */ - CBinaryStream(const char * fname, const char * flag = "r") - : CSGObject() + BinaryStream(const char * fname, const char * flag = "r") + : SGObject() { /* open_stream(bs.m_fname, bs.rw); */ not_implemented(SOURCE_LOCATION); @@ -59,7 +59,7 @@ template class CBinaryStream : public CSGObject * * @param bs binary stream to copy from */ - CBinaryStream(const CBinaryStream &bs) + BinaryStream(const BinaryStream &bs) { open_stream(bs.m_fname, bs.rw); ASSERT(length == bs.length) @@ -68,7 +68,7 @@ template class CBinaryStream : public CSGObject /** destructor */ - virtual ~CBinaryStream() + virtual ~BinaryStream() { close_stream(); } diff --git a/src/shogun/io/CSVFile.cpp b/src/shogun/io/CSVFile.cpp index bf14d5addf1..49ccb16ff17 100644 --- a/src/shogun/io/CSVFile.cpp +++ b/src/shogun/io/CSVFile.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Soeren Sonnenburg, Thoralf Klein, Viktor Gal, + * Authors: Evgeniy Andreev, Soeren Sonnenburg, Thoralf Klein, Viktor Gal, * Fernando Iglesias, Sergey Lisitsyn, Bjoern Esser */ @@ -15,48 +15,44 @@ using namespace shogun; -CCSVFile::CCSVFile() +CSVFile::CSVFile() { init(); } -CCSVFile::CCSVFile(FILE* f, const char* name) : - CFile(f, name) +CSVFile::CSVFile(FILE* f, const char* name) : + File(f, name) { init(); init_with_defaults(); } #ifdef HAVE_FDOPEN -CCSVFile::CCSVFile(int fd, const char* mode, const char* name) : - CFile(fd, mode, name) +CSVFile::CSVFile(int fd, const char* mode, const char* name) : + File(fd, mode, name) { init(); init_with_defaults(); } #endif -CCSVFile::CCSVFile(const char* fname, char rw, const char* name) : - CFile(fname, rw, name) +CSVFile::CSVFile(const char* fname, char rw, const char* name) : + File(fname, rw, name) { init(); init_with_defaults(); } -CCSVFile::~CCSVFile() +CSVFile::~CSVFile() { - SG_UNREF(m_tokenizer); - SG_UNREF(m_line_tokenizer); - SG_UNREF(m_parser); - SG_UNREF(m_line_reader); } -void CCSVFile::set_transpose(bool value) +void CSVFile::set_transpose(bool value) { is_data_transposed=value; } -void CCSVFile::set_delimiter(char delimiter) +void CSVFile::set_delimiter(char delimiter) { m_tokenizer->delimiters[m_delimiter]=0; @@ -66,12 +62,12 @@ void CCSVFile::set_delimiter(char delimiter) m_tokenizer->delimiters[' ']=1; } -void CCSVFile::set_lines_to_skip(int32_t num_lines) +void CSVFile::set_lines_to_skip(int32_t num_lines) { m_num_to_skip=num_lines; } -int32_t CCSVFile::get_stats(int32_t& num_tokens) +int32_t CSVFile::get_stats(int32_t& num_tokens) { int32_t num_lines=0; num_tokens=-1; @@ -100,46 +96,41 @@ int32_t CCSVFile::get_stats(int32_t& num_tokens) return num_lines; } -void CCSVFile::init() +void CSVFile::init() { is_data_transposed=false; m_delimiter=0; m_num_to_skip=0; - - m_tokenizer=NULL; - m_line_tokenizer=NULL; - m_parser=NULL; - m_line_reader=NULL; } -void CCSVFile::init_with_defaults() +void CSVFile::init_with_defaults() { is_data_transposed=false; m_delimiter=','; - m_tokenizer=new CDelimiterTokenizer(true); + m_tokenizer=std::make_shared(true); m_tokenizer->delimiters[m_delimiter]=1; m_tokenizer->delimiters[' ']=1; - SG_REF(m_tokenizer); - m_line_tokenizer=new CDelimiterTokenizer(true); + + m_line_tokenizer=std::make_shared(true); m_line_tokenizer->delimiters['\n']=1; - SG_REF(m_line_tokenizer); - m_parser=new CParser(); + + m_parser=std::make_shared(); m_parser->set_tokenizer(m_tokenizer); - m_line_reader=new CLineReader(file, m_line_tokenizer); + m_line_reader=std::make_shared(file, m_line_tokenizer); } -void CCSVFile::skip_lines(int32_t num_lines) +void CSVFile::skip_lines(int32_t num_lines) { for (int32_t i=0; iskip_line(); } #define GET_VECTOR(read_func, sg_type) \ -void CCSVFile::get_vector(sg_type*& vector, int32_t& len) \ +void CSVFile::get_vector(sg_type*& vector, int32_t& len) \ { \ if (!m_line_reader->has_next()) \ return; \ @@ -178,7 +169,7 @@ GET_VECTOR(read_ulong, uint64_t) #undef GET_VECTOR #define GET_MATRIX(read_func, sg_type) \ -void CCSVFile::get_matrix(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void CSVFile::get_matrix(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ int32_t num_lines=0; \ int32_t num_tokens=-1; \ @@ -239,7 +230,7 @@ GET_MATRIX(read_ulong, uint64_t) #undef GET_MATRIX #define GET_NDARRAY(read_func, sg_type) \ -void CCSVFile::get_ndarray(sg_type*& array, int32_t*& dims, int32_t& num_dims) \ +void CSVFile::get_ndarray(sg_type*& array, int32_t*& dims, int32_t& num_dims) \ { \ not_implemented(SOURCE_LOCATION); \ } @@ -254,7 +245,7 @@ GET_NDARRAY(read_word, uint16_t) #undef GET_NDARRAY #define GET_SPARSE_MATRIX(read_func, sg_type) \ -void CCSVFile::get_sparse_matrix( \ +void CSVFile::get_sparse_matrix( \ SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ not_implemented(SOURCE_LOCATION); \ @@ -276,7 +267,7 @@ GET_SPARSE_MATRIX(read_ulong, uint64_t) #undef GET_SPARSE_MATRIX #define SET_VECTOR(format, sg_type) \ -void CCSVFile::set_vector(const sg_type* vector, int32_t len) \ +void CSVFile::set_vector(const sg_type* vector, int32_t len) \ { \ SG_SET_LOCALE_C; \ \ @@ -311,7 +302,7 @@ SET_VECTOR(SCNu16, uint16_t) #undef SET_VECTOR #define SET_MATRIX(format, sg_type) \ -void CCSVFile::set_matrix(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ +void CSVFile::set_matrix(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ { \ SG_SET_LOCALE_C; \ \ @@ -354,7 +345,7 @@ SET_MATRIX(SCNu16, uint16_t) #undef SET_MATRIX #define SET_SPARSE_MATRIX(format, sg_type) \ -void CCSVFile::set_sparse_matrix( \ +void CSVFile::set_sparse_matrix( \ const SGSparseVector* matrix, int32_t num_feat, int32_t num_vec) \ { \ not_implemented(SOURCE_LOCATION); \ @@ -375,7 +366,7 @@ SET_SPARSE_MATRIX(SCNi16, int16_t) SET_SPARSE_MATRIX(SCNu16, uint16_t) #undef SET_SPARSE_MATRIX -void CCSVFile::get_string_list( +void CSVFile::get_string_list( SGVector*& strings, int32_t& num_str, int32_t& max_string_len) { @@ -402,7 +393,7 @@ void CCSVFile::get_string_list( } #define GET_STRING_LIST(sg_type) \ -void CCSVFile::get_string_list( \ +void CSVFile::get_string_list( \ SGVector*& strings, int32_t& num_str, \ int32_t& max_string_len) \ { \ @@ -422,7 +413,7 @@ GET_STRING_LIST(int16_t) GET_STRING_LIST(uint16_t) #undef GET_STRING_LIST -void CCSVFile::set_string_list( +void CSVFile::set_string_list( const SGVector* strings, int32_t num_str) { for (int32_t i=0; i* strings, int32_t num_str) \ { \ not_implemented(SOURCE_LOCATION); \ diff --git a/src/shogun/io/CSVFile.h b/src/shogun/io/CSVFile.h index 357168092b0..dd9b1dfcb2a 100644 --- a/src/shogun/io/CSVFile.h +++ b/src/shogun/io/CSVFile.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Fernando Iglesias, Sergey Lisitsyn, - * Heiko Strathmann, Yuyu Zhang, Viktor Gal, Thoralf Klein, + * Authors: Evgeniy Andreev, Fernando Iglesias, Sergey Lisitsyn, + * Heiko Strathmann, Yuyu Zhang, Viktor Gal, Thoralf Klein, * Bjoern Esser, Soeren Sonnenburg */ @@ -16,27 +16,27 @@ namespace shogun { -class CDelimiterTokenizer; -class CLineReader; -class CParser; +class DelimiterTokenizer; +class LineReader; +class Parser; template class SGVector; template class SGSparseVector; /** @brief Class CSVFile used to read data from comma-separated values (CSV) * files. See http://en.wikipedia.org/wiki/Comma-separated_values. */ -class CCSVFile : public CFile +class CSVFile : public File { public: /** default constructor */ - CCSVFile(); + CSVFile(); /** constructor * * @param f already opened file * @param name variable name (e.g. "x" or "/path/to/x") */ - CCSVFile(FILE* f, const char* name=NULL); + CSVFile(FILE* f, const char* name=NULL); #ifdef HAVE_FDOPEN /** constructor @@ -45,7 +45,7 @@ class CCSVFile : public CFile * @param mode mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CCSVFile(int fd, const char* mode, const char* name=NULL); + CSVFile(int fd, const char* mode, const char* name=NULL); #endif /** constructor @@ -54,10 +54,10 @@ class CCSVFile : public CFile * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CCSVFile(const char* fname, char rw='r', const char* name=NULL); + CSVFile(const char* fname, char rw='r', const char* name=NULL); /** destructor */ - virtual ~CCSVFile(); + virtual ~CSVFile(); /** set order for data in file * @@ -387,16 +387,16 @@ class CCSVFile : public CFile private: /** object for reading lines from file */ - CLineReader* m_line_reader; + std::shared_ptr m_line_reader; /** parser of lines */ - CParser* m_parser; + std::shared_ptr m_parser; /** tokenizer for line_reader */ - CDelimiterTokenizer* m_line_tokenizer; + std::shared_ptr m_line_tokenizer; /** tokenizer for parser */ - CDelimiterTokenizer* m_tokenizer; + std::shared_ptr m_tokenizer; /** data order */ bool is_data_transposed; diff --git a/src/shogun/io/File.cpp b/src/shogun/io/File.cpp index b3d527d3317..0ef349d6ed9 100644 --- a/src/shogun/io/File.cpp +++ b/src/shogun/io/File.cpp @@ -18,7 +18,7 @@ using namespace shogun; -CFile::CFile() : CSGObject() +File::File() : SGObject() { file=NULL; filename=NULL; @@ -26,7 +26,7 @@ CFile::CFile() : CSGObject() task='\0'; } -CFile::CFile(FILE* f, const char* name) : CSGObject() +File::File(FILE* f, const char* name) : SGObject() { file=f; filename=NULL; @@ -38,7 +38,7 @@ CFile::CFile(FILE* f, const char* name) : CSGObject() } #ifdef HAVE_FDOPEN -CFile::CFile(int fd, const char* mode, const char* name) : CSGObject() +File::File(int fd, const char* mode, const char* name) : SGObject() { file=fdopen(fd, mode); filename=NULL; @@ -50,7 +50,7 @@ CFile::CFile(int fd, const char* mode, const char* name) : CSGObject() } #endif -CFile::CFile(const char* fname, char rw, const char* name) : CSGObject() +File::File(const char* fname, char rw, const char* name) : SGObject() { variable_name=NULL; task=rw; @@ -74,7 +74,7 @@ CFile::CFile(const char* fname, char rw, const char* name) : CSGObject() set_variable_name(name); } -void CFile::get_vector(bool*& vector, int32_t& len) +void File::get_vector(bool*& vector, int32_t& len) { int32_t* int_vector; get_vector(int_vector, len); @@ -88,7 +88,7 @@ void CFile::get_vector(bool*& vector, int32_t& len) SG_FREE(int_vector); } -void CFile::set_vector(const bool* vector, int32_t len) +void File::set_vector(const bool* vector, int32_t len) { int32_t* int_vector = SG_MALLOC(int32_t, len); for (int32_t i=0;i*& strings, int32_t& num_str, int32_t& max_string_len) { @@ -153,7 +153,7 @@ void CFile::get_string_list( SG_FREE(strs); } -void CFile::set_string_list(const SGVector* strings, int32_t num_str) +void File::set_string_list(const SGVector* strings, int32_t num_str) { SGVector * strs = SG_MALLOC(SGVector, num_str); @@ -169,31 +169,31 @@ void CFile::set_string_list(const SGVector* strings, int32_t num_str) SG_FREE(strs); } -CFile::~CFile() +File::~File() { close(); } -void CFile::set_variable_name(const char* name) +void File::set_variable_name(const char* name) { SG_FREE(variable_name); variable_name=get_strdup(name); } -char* CFile::get_variable_name() +char* File::get_variable_name() { return get_strdup(variable_name); } #define SPARSE_VECTOR_GETTER(type) \ -void CFile::set_sparse_vector( \ +void File::set_sparse_vector( \ const SGSparseVectorEntry* entries, int32_t num_feat) \ { \ SGSparseVector v((SGSparseVectorEntry*) entries, num_feat, false); \ set_sparse_matrix(&v, 0, 1); \ } \ \ -void CFile::get_sparse_vector( \ +void File::get_sparse_vector( \ SGSparseVectorEntry*& entries, int32_t& num_feat) \ { \ SGSparseVector* v; \ @@ -221,7 +221,7 @@ SPARSE_VECTOR_GETTER(uint64_t) #undef SPARSE_VECTOR_GETTER -char* CFile::read_whole_file(char* fname, size_t& len) +char* File::read_whole_file(char* fname, size_t& len) { FILE* tmpf=fopen(fname, "r"); ASSERT(tmpf) diff --git a/src/shogun/io/File.h b/src/shogun/io/File.h index 8ce033ef0c8..fa018bf88e0 100644 --- a/src/shogun/io/File.h +++ b/src/shogun/io/File.h @@ -23,23 +23,23 @@ template struct SGSparseVectorEntry; * * A file is assumed to be a seekable raw data stream. * - * \sa CCSVFile + * \sa CSVFile * \sa CBinaryFile - * \sa CHDF5File + * \sa HDF5File * */ -class CFile : public CSGObject +class File : public SGObject { public: /** default constructor */ - CFile(); + File(); /** constructor * * @param f already opened file * @param name variable name (e.g. "x" or "/path/to/x") */ - CFile(FILE* f, const char* name=NULL); + File(FILE* f, const char* name=NULL); #ifdef HAVE_FDOPEN /** constructor @@ -48,7 +48,7 @@ class CFile : public CSGObject * @param mode mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CFile(int fd, const char* mode, const char* name=NULL); + File(int fd, const char* mode, const char* name=NULL); #endif /** constructor @@ -57,10 +57,10 @@ class CFile : public CSGObject * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CFile(const char* fname, char rw='r', const char* name=NULL); + File(const char* fname, char rw='r', const char* name=NULL); /** default destructor */ - virtual ~CFile(); + virtual ~File(); /** close */ void close() @@ -86,7 +86,7 @@ class CFile : public CSGObject /** set the path to the variable to be accessed * - * only supported by some file interfaces like CHDF5File + * only supported by some file interfaces like HDF5File * * @param name variable path & name */ @@ -94,7 +94,7 @@ class CFile : public CSGObject /** get the path to the variable to be accessed * - * only supported by some file interfaces like CHDF5File + * only supported by some file interfaces like HDF5File * * @return name variable path & name */ diff --git a/src/shogun/io/HDF5File.cpp b/src/shogun/io/HDF5File.cpp index 9b25ae344a4..b53d88a07fe 100644 --- a/src/shogun/io/HDF5File.cpp +++ b/src/shogun/io/HDF5File.cpp @@ -18,7 +18,7 @@ using namespace shogun; -CHDF5File::CHDF5File() +HDF5File::HDF5File() { unstable(SOURCE_LOCATION); @@ -26,7 +26,7 @@ CHDF5File::CHDF5File() h5file = -1; } -CHDF5File::CHDF5File(char* fname, char rw, const char* name) : CFile() +HDF5File::HDF5File(char* fname, char rw, const char* name) : File() { get_boolean_type(); H5Eset_auto2(H5E_DEFAULT, NULL, NULL); @@ -55,13 +55,13 @@ CHDF5File::CHDF5File(char* fname, char rw, const char* name) : CFile() error("Could not open file '{}'", fname); } -CHDF5File::~CHDF5File() +HDF5File::~HDF5File() { H5Fclose(h5file); } #define GET_VECTOR(fname, sg_type, datatype) \ -void CHDF5File::fname(sg_type*& vec, int32_t& len) \ +void HDF5File::fname(sg_type*& vec, int32_t& len) \ { \ if (!h5file) \ error("File invalid."); \ @@ -115,7 +115,7 @@ GET_VECTOR(get_vector, uint64_t, (CT_VECTOR, ST_NONE, PT_UINT64)) #undef GET_VECTOR #define GET_MATRIX(fname, sg_type, datatype) \ -void CHDF5File::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void HDF5File::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ if (!h5file) \ error("File invalid."); \ @@ -167,7 +167,7 @@ GET_MATRIX(get_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX)) #undef GET_MATRIX #define GET_SPARSEMATRIX(fname, sg_type, datatype) \ -void CHDF5File::fname(SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void HDF5File::fname(SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ if (!(file)) \ error("File invalid."); \ @@ -189,7 +189,7 @@ GET_SPARSEMATRIX(get_sparse_matrix, floatmax_t, DT_SPARSE_LONGREAL) #define GET_STRING_LIST(fname, sg_type, datatype) \ -void CHDF5File::fname(SGVector*& strings, int32_t& num_str, int32_t& max_string_len) \ +void HDF5File::fname(SGVector*& strings, int32_t& num_str, int32_t& max_string_len) \ { \ } @@ -211,7 +211,7 @@ GET_STRING_LIST(get_string_list, floatmax_t, DT_STRING_LONGREAL) /** set functions - to pass data from shogun to the target interface */ #define SET_VECTOR(fname, sg_type, dtype, h5type) \ -void CHDF5File::fname(const sg_type* vec, int32_t len) \ +void HDF5File::fname(const sg_type* vec, int32_t len) \ { \ if (h5file<0 || !vec) \ error("File or vector invalid."); \ @@ -252,7 +252,7 @@ SET_VECTOR(set_vector, uint64_t, DT_VECTOR_ULONG, H5T_NATIVE_ULLONG) #undef SET_VECTOR #define SET_MATRIX(fname, sg_type, dtype, h5type) \ -void CHDF5File::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ +void HDF5File::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ { \ if (h5file<0 || !matrix) \ error("File or matrix invalid."); \ @@ -293,7 +293,7 @@ SET_MATRIX(set_matrix, floatmax_t, DT_DENSE_LONGREAL, H5T_NATIVE_LDOUBLE) #undef SET_MATRIX #define SET_SPARSEMATRIX(fname, sg_type, dtype) \ -void CHDF5File::fname(const SGSparseVector* matrix, \ +void HDF5File::fname(const SGSparseVector* matrix, \ int32_t num_feat, int32_t num_vec) \ { \ if (!(file && matrix)) \ @@ -316,7 +316,7 @@ SET_SPARSEMATRIX(set_sparse_matrix, floatmax_t, DT_SPARSE_LONGREAL) #undef SET_SPARSEMATRIX #define SET_STRING_LIST(fname, sg_type, dtype) \ -void CHDF5File::fname(const SGVector* strings, int32_t num_str) \ +void HDF5File::fname(const SGVector* strings, int32_t num_str) \ { \ if (!(file && strings)) \ error("File or strings invalid."); \ @@ -337,7 +337,7 @@ SET_STRING_LIST(set_string_list, float64_t, DT_STRING_REAL) SET_STRING_LIST(set_string_list, floatmax_t, DT_STRING_LONGREAL) #undef SET_STRING_LIST -void CHDF5File::get_boolean_type() +void HDF5File::get_boolean_type() { boolean_type=H5T_NATIVE_UCHAR; switch (sizeof(bool)) @@ -359,7 +359,7 @@ void CHDF5File::get_boolean_type() } } -hid_t CHDF5File::get_compatible_type(H5T_class_t t_class, +hid_t HDF5File::get_compatible_type(H5T_class_t t_class, const TSGDataType* datatype) { switch (t_class) @@ -405,7 +405,7 @@ hid_t CHDF5File::get_compatible_type(H5T_class_t t_class, } } -void CHDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, int64_t& total_elements) +void HDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, int64_t& total_elements) { hid_t dataspace = H5Dget_space(dataset); if (dataspace<0) @@ -422,7 +422,7 @@ void CHDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, int64_t& H5Sclose(dataspace); } -void CHDF5File::create_group_hierarchy() +void HDF5File::create_group_hierarchy() { char* vname=get_strdup(variable_name); int32_t vlen=strlen(vname); diff --git a/src/shogun/io/HDF5File.h b/src/shogun/io/HDF5File.h index 3a59a18d60a..c4a424961f1 100644 --- a/src/shogun/io/HDF5File.h +++ b/src/shogun/io/HDF5File.h @@ -29,11 +29,11 @@ struct TSGDataType; * */ #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CHDF5File : public CFile +IGNORE_IN_CLASSLIST class HDF5File : public File { public: /** default constructor */ - CHDF5File(); + HDF5File(); /** constructor * @@ -41,10 +41,10 @@ IGNORE_IN_CLASSLIST class CHDF5File : public CFile * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CHDF5File(char* fname, char rw='r', const char* name=NULL); + HDF5File(char* fname, char rw='r', const char* name=NULL); /** default destructor */ - virtual ~CHDF5File(); + virtual ~HDF5File(); #ifndef SWIG // SWIG should skip this /** @name Vector Access Functions diff --git a/src/shogun/io/IOBuffer.cpp b/src/shogun/io/IOBuffer.cpp index aab284b5a0c..c516e2d39f3 100644 --- a/src/shogun/io/IOBuffer.cpp +++ b/src/shogun/io/IOBuffer.cpp @@ -20,22 +20,22 @@ using namespace shogun; -CIOBuffer::CIOBuffer() +IOBuffer::IOBuffer() { init(); } -CIOBuffer::CIOBuffer(int fd) +IOBuffer::IOBuffer(int fd) { init(); working_file = fd; } -CIOBuffer::~CIOBuffer() +IOBuffer::~IOBuffer() { } -void CIOBuffer::init() +void IOBuffer::init() { size_t s = 1 << 16; space.reserve(s); @@ -43,12 +43,12 @@ void CIOBuffer::init() working_file=-1; } -void CIOBuffer::use_file(int fd) +void IOBuffer::use_file(int fd) { working_file = fd; } -int CIOBuffer::open_file(const char* name, char flag) +int IOBuffer::open_file(const char* name, char flag) { int ret=1; switch(flag) @@ -68,24 +68,24 @@ int CIOBuffer::open_file(const char* name, char flag) return ret; } -void CIOBuffer::reset_file() +void IOBuffer::reset_file() { lseek(working_file, 0, SEEK_SET); endloaded = space.begin; space.end = space.begin; } -void CIOBuffer::set(char *p) +void IOBuffer::set(char *p) { space.end = p; } -ssize_t CIOBuffer::read_file(void* buf, size_t nbytes) +ssize_t IOBuffer::read_file(void* buf, size_t nbytes) { return read(working_file, buf, nbytes); } -size_t CIOBuffer::fill() +size_t IOBuffer::fill() { if (space.end_array - endloaded == 0) { @@ -103,12 +103,12 @@ size_t CIOBuffer::fill() return 0; } -ssize_t CIOBuffer::write_file(const void* buf, size_t nbytes) +ssize_t IOBuffer::write_file(const void* buf, size_t nbytes) { return write(working_file, buf, nbytes); } -void CIOBuffer::flush() +void IOBuffer::flush() { if (working_file>=0) { @@ -123,7 +123,7 @@ void CIOBuffer::flush() #endif } -bool CIOBuffer::close_file() +bool IOBuffer::close_file() { if (working_file < 0) return false; @@ -136,7 +136,7 @@ bool CIOBuffer::close_file() } } -ssize_t CIOBuffer::readto(char* &pointer, char terminal) +ssize_t IOBuffer::readto(char* &pointer, char terminal) { //Return a pointer to the bytes before the terminal. Must be less //than the buffer size. @@ -167,7 +167,7 @@ ssize_t CIOBuffer::readto(char* &pointer, char terminal) } } -void CIOBuffer::buf_write(char* &pointer, int n) +void IOBuffer::buf_write(char* &pointer, int n) { if (space.end + n <= space.end_array) { @@ -187,7 +187,7 @@ void CIOBuffer::buf_write(char* &pointer, int n) } } -unsigned int CIOBuffer::buf_read(char* &pointer, int n) +unsigned int IOBuffer::buf_read(char* &pointer, int n) { // Return a pointer to the next n bytes. // n must be smaller than the maximum size. diff --git a/src/shogun/io/IOBuffer.h b/src/shogun/io/IOBuffer.h index 505451b61c9..b1409ce6cb5 100644 --- a/src/shogun/io/IOBuffer.h +++ b/src/shogun/io/IOBuffer.h @@ -30,7 +30,7 @@ namespace shogun * */ - class CIOBuffer : public CSGObject + class IOBuffer : public SGObject { public: @@ -38,19 +38,19 @@ namespace shogun /** * Constructor. */ - CIOBuffer(); + IOBuffer(); /** * Constructor taking file descriptor as parameter * * @param fd file descriptor to use */ - CIOBuffer(int fd); + IOBuffer(int fd); /** * Destructor. */ - ~CIOBuffer(); + ~IOBuffer(); /** * Initialize the buffer, reserve 64K memory by default. diff --git a/src/shogun/io/LibSVMFile.cpp b/src/shogun/io/LibSVMFile.cpp index 36f46b8042d..d4621e4ab8b 100644 --- a/src/shogun/io/LibSVMFile.cpp +++ b/src/shogun/io/LibSVMFile.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Jiaolong Xu, Thoralf Klein, Bjoern Esser, + * Authors: Evgeniy Andreev, Jiaolong Xu, Thoralf Klein, Bjoern Esser, * Giovanni De Toni, Fernando Iglesias */ @@ -17,36 +17,36 @@ using namespace shogun; -CLibSVMFile::CLibSVMFile() +LibSVMFile::LibSVMFile() { init(); } -CLibSVMFile::CLibSVMFile(FILE* f, const char* name) : - CFile(f, name) +LibSVMFile::LibSVMFile(FILE* f, const char* name) : + File(f, name) { init(); init_with_defaults(); } -CLibSVMFile::CLibSVMFile(const char* fname, char rw, const char* name) : - CFile(fname, rw, name) +LibSVMFile::LibSVMFile(const char* fname, char rw, const char* name) : + File(fname, rw, name) { init(); init_with_defaults(); } -CLibSVMFile::~CLibSVMFile() +LibSVMFile::~LibSVMFile() { - SG_UNREF(m_whitespace_tokenizer); - SG_UNREF(m_delimiter_feat_tokenizer); - SG_UNREF(m_delimiter_label_tokenizer); - SG_UNREF(m_line_tokenizer); - SG_UNREF(m_parser); - SG_UNREF(m_line_reader); + + + + + + } -void CLibSVMFile::init() +void LibSVMFile::init() { m_delimiter_feat=0; @@ -58,33 +58,33 @@ void CLibSVMFile::init() m_line_reader=NULL; } -void CLibSVMFile::init_with_defaults() +void LibSVMFile::init_with_defaults() { m_delimiter_feat=':'; m_delimiter_label=','; - m_whitespace_tokenizer=new CDelimiterTokenizer(true); + m_whitespace_tokenizer=std::make_shared(true); m_whitespace_tokenizer->delimiters[' ']=1; - SG_REF(m_whitespace_tokenizer); - m_delimiter_feat_tokenizer=new CDelimiterTokenizer(true); + + m_delimiter_feat_tokenizer=std::make_shared(true); m_delimiter_feat_tokenizer->delimiters[m_delimiter_feat]=1; - SG_REF(m_delimiter_feat_tokenizer); - m_delimiter_label_tokenizer=new CDelimiterTokenizer(true); + + m_delimiter_label_tokenizer=std::make_shared(true); m_delimiter_label_tokenizer->delimiters[m_delimiter_label]=1; - SG_REF(m_delimiter_label_tokenizer); - m_line_tokenizer=new CDelimiterTokenizer(true); + + m_line_tokenizer=std::make_shared(true); m_line_tokenizer->delimiters['\n']=1; - SG_REF(m_line_tokenizer); - m_parser=new CParser(); - m_line_reader=new CLineReader(file, m_line_tokenizer); + + m_parser=std::make_shared(); + m_line_reader=std::make_shared(file, m_line_tokenizer); } #define GET_SPARSE_MATRIX(read_func, sg_type) \ -void CLibSVMFile::get_sparse_matrix(SGSparseVector*& mat_feat, int32_t& num_feat, int32_t& num_vec) \ +void LibSVMFile::get_sparse_matrix(SGSparseVector*& mat_feat, int32_t& num_feat, int32_t& num_vec) \ { \ SGVector* multilabel; \ int32_t num_classes; \ @@ -107,7 +107,7 @@ GET_SPARSE_MATRIX(read_ulong, uint64_t) #undef GET_SPARSE_MATRIX #define GET_LABELED_SPARSE_MATRIX(read_func, sg_type) \ -void CLibSVMFile::get_sparse_matrix(SGSparseVector*& mat_feat, int32_t& num_feat, int32_t& num_vec, \ +void LibSVMFile::get_sparse_matrix(SGSparseVector*& mat_feat, int32_t& num_feat, int32_t& num_vec, \ float64_t*& labels, bool load_labels) \ { \ SGVector* multilabel; \ @@ -143,7 +143,7 @@ GET_LABELED_SPARSE_MATRIX(read_ulong, uint64_t) #undef GET_LABELED_SPARSE_MATRIX #define GET_MULTI_LABELED_SPARSE_MATRIX(read_func, sg_type) \ - void CLibSVMFile::get_sparse_matrix( \ + void LibSVMFile::get_sparse_matrix( \ SGSparseVector*& mat_feat, int32_t& num_feat, \ int32_t& num_vec, SGVector*& multilabel, \ int32_t& num_classes, bool load_labels) \ @@ -273,7 +273,7 @@ GET_MULTI_LABELED_SPARSE_MATRIX(read_ulong, uint64_t) #undef GET_MULTI_LABELED_SPARSE_MATRIX #define SET_SPARSE_MATRIX(format, sg_type) \ -void CLibSVMFile::set_sparse_matrix( \ +void LibSVMFile::set_sparse_matrix( \ const SGSparseVector* matrix, int32_t num_feat, int32_t num_vec) \ { \ SGVector * labels = NULL; \ @@ -296,7 +296,7 @@ SET_SPARSE_MATRIX(SCNu16, uint16_t) #undef SET_SPARSE_MATRIX #define SET_LABELED_SPARSE_MATRIX(format, sg_type) \ -void CLibSVMFile::set_sparse_matrix( \ +void LibSVMFile::set_sparse_matrix( \ const SGSparseVector* matrix, int32_t num_feat, int32_t num_vec, \ const float64_t* labels) \ { \ @@ -328,7 +328,7 @@ SET_LABELED_SPARSE_MATRIX(SCNu16, uint16_t) #undef SET_LABELED_SPARSE_MATRIX #define SET_MULTI_LABELED_SPARSE_MATRIX(format, sg_type) \ -void CLibSVMFile::set_sparse_matrix( \ +void LibSVMFile::set_sparse_matrix( \ const SGSparseVector* matrix, int32_t num_feat, int32_t num_vec, \ const SGVector* multilabel) \ { \ @@ -380,7 +380,7 @@ SET_MULTI_LABELED_SPARSE_MATRIX(SCNi16, int16_t) SET_MULTI_LABELED_SPARSE_MATRIX(SCNu16, uint16_t) #undef SET_MULTI_LABELED_SPARSE_MATRIX -int32_t CLibSVMFile::get_num_lines() +int32_t LibSVMFile::get_num_lines() { int32_t num_lines=0; while (m_line_reader->has_next()) @@ -393,9 +393,9 @@ int32_t CLibSVMFile::get_num_lines() return num_lines; } -bool CLibSVMFile::is_feat_entry(const SGVector entry) +bool LibSVMFile::is_feat_entry(const SGVector entry) { - CParser* parser = new CParser(); + auto parser = std::make_shared(); parser->set_tokenizer(m_delimiter_feat_tokenizer); parser->set_text(entry); bool isfeat = false; @@ -409,7 +409,7 @@ bool CLibSVMFile::is_feat_entry(const SGVector entry) } - SG_UNREF(parser); + return isfeat; } diff --git a/src/shogun/io/LibSVMFile.h b/src/shogun/io/LibSVMFile.h index 5f1d94def27..51fd9db030f 100644 --- a/src/shogun/io/LibSVMFile.h +++ b/src/shogun/io/LibSVMFile.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jiaolong Xu, Evgeniy Andreev, Bjoern Esser, Soeren Sonnenburg, - * Heiko Strathmann, Yuyu Zhang, Thoralf Klein, Evan Shelhamer, + * Authors: Jiaolong Xu, Evgeniy Andreev, Bjoern Esser, Soeren Sonnenburg, + * Heiko Strathmann, Yuyu Zhang, Thoralf Klein, Evan Shelhamer, * Abhinav Agarwalla */ @@ -15,9 +15,9 @@ namespace shogun { -class CDelimiterTokenizer; -class CLineReader; -class CParser; +class DelimiterTokenizer; +class LineReader; +class Parser; template class SGVector; template class SGSparseVector; @@ -28,18 +28,18 @@ template class SGSparseVector; * dim 2 - value 100.2 * dim 1000 - value 1.3 */ -class CLibSVMFile : public CFile +class LibSVMFile : public File { public: /** default constructor */ - CLibSVMFile(); + LibSVMFile(); /** constructor * * @param f already opened file * @param name variable name (e.g. "x" or "/path/to/x") */ - CLibSVMFile(FILE* f, const char* name=NULL); + LibSVMFile(FILE* f, const char* name=NULL); /** constructor * @@ -47,10 +47,10 @@ class CLibSVMFile : public CFile * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CLibSVMFile(const char* fname, char rw='r', const char* name=NULL); + LibSVMFile(const char* fname, char rw='r', const char* name=NULL); /** destructor */ - virtual ~CLibSVMFile(); + virtual ~LibSVMFile(); #ifndef SWIG // SWIG should skip this part /** @name Sparse Matrix Access Functions @@ -342,22 +342,22 @@ class CLibSVMFile : public CFile char m_delimiter_label; /** object for reading lines from file */ - CLineReader* m_line_reader; + std::shared_ptr m_line_reader; /** parser of lines */ - CParser* m_parser; + std::shared_ptr m_parser; /** tokenizer for line_reader */ - CDelimiterTokenizer* m_line_tokenizer; + std::shared_ptr m_line_tokenizer; /** delimiter for parsing lines */ - CDelimiterTokenizer* m_whitespace_tokenizer; + std::shared_ptr m_whitespace_tokenizer; /** delimiter for parsing sparse entries */ - CDelimiterTokenizer* m_delimiter_feat_tokenizer; + std::shared_ptr m_delimiter_feat_tokenizer; /** delimiter for parsing multiple labels */ - CDelimiterTokenizer* m_delimiter_label_tokenizer; + std::shared_ptr m_delimiter_label_tokenizer; }; } diff --git a/src/shogun/io/LineReader.cpp b/src/shogun/io/LineReader.cpp index e61ea01019d..30e09ed01fe 100644 --- a/src/shogun/io/LineReader.cpp +++ b/src/shogun/io/LineReader.cpp @@ -13,58 +13,58 @@ using namespace shogun; -CLineReader::CLineReader() +LineReader::LineReader() { init(); - m_buffer=new CCircularBuffer(); + m_buffer=std::make_shared(); } -CLineReader::CLineReader(FILE* stream, CTokenizer* tokenizer) +LineReader::LineReader(FILE* stream, std::shared_ptr tokenizer) { init(); m_stream=stream; m_max_token_length=10*1024*1024; - SG_REF(tokenizer); + m_tokenizer=tokenizer; - m_buffer=new CCircularBuffer(m_max_token_length); + m_buffer=std::make_shared(m_max_token_length); m_buffer->set_tokenizer(m_tokenizer); } -CLineReader::CLineReader(int32_t max_token_length, FILE* stream, CTokenizer* tokenizer) +LineReader::LineReader(int32_t max_token_length, FILE* stream, std::shared_ptr tokenizer) { init(); m_stream=stream; m_max_token_length=max_token_length; - SG_REF(tokenizer); + m_tokenizer=tokenizer; - m_buffer=new CCircularBuffer(m_max_token_length); + m_buffer=std::make_shared(m_max_token_length); m_buffer->set_tokenizer(m_tokenizer); } -CLineReader::~CLineReader() +LineReader::~LineReader() { - SG_UNREF(m_tokenizer); - SG_UNREF(m_buffer); + + } -bool CLineReader::has_next() +bool LineReader::has_next() { if (m_stream==NULL || m_max_token_length==0 || m_tokenizer==NULL) { - error("CLineReader::has_next():: Class is not initialized"); + error("LineReader::has_next():: Class is not initialized"); return false; } if (ferror(m_stream)) { - error("CLineReader::has_next():: Error reading file"); + error("LineReader::has_next():: Error reading file"); return false; } @@ -74,7 +74,7 @@ bool CLineReader::has_next() return true; } -void CLineReader::skip_line() +void LineReader::skip_line() { int32_t bytes_to_skip=0; m_next_token_length=read(bytes_to_skip); @@ -84,7 +84,7 @@ void CLineReader::skip_line() m_buffer->skip_characters(bytes_to_skip); } -SGVector CLineReader::read_line() +SGVector LineReader::read_line() { SGVector line; @@ -101,22 +101,22 @@ SGVector CLineReader::read_line() return line; } -void CLineReader::reset() +void LineReader::reset() { rewind(m_stream); m_buffer->clear(); } -void CLineReader::set_tokenizer(CTokenizer* tokenizer) +void LineReader::set_tokenizer(std::shared_ptr tokenizer) { - SG_REF(tokenizer); - SG_UNREF(m_tokenizer); + + m_tokenizer=tokenizer; m_buffer->set_tokenizer(tokenizer); } -void CLineReader::init() +void LineReader::init() { m_buffer=NULL; m_tokenizer=NULL; @@ -126,7 +126,7 @@ void CLineReader::init() m_next_token_length=-1; } -int32_t CLineReader::read(int32_t& bytes_to_skip) +int32_t LineReader::read(int32_t& bytes_to_skip) { int32_t line_end=0; int32_t bytes_to_read=0; @@ -159,13 +159,13 @@ int32_t CLineReader::read(int32_t& bytes_to_skip) if (ferror(m_stream)) { - error("CLineReader::read(int32_t&):: Error reading file"); + error("LineReader::read(int32_t&):: Error reading file"); return -1; } } } -SGVector CLineReader::read_token(int32_t line_len) +SGVector LineReader::read_token(int32_t line_len) { SGVector line; diff --git a/src/shogun/io/LineReader.h b/src/shogun/io/LineReader.h index f07c37d7ce8..4ffd06de0a0 100644 --- a/src/shogun/io/LineReader.h +++ b/src/shogun/io/LineReader.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Thoralf Klein, Heiko Strathmann, Yuyu Zhang, + * Authors: Evgeniy Andreev, Thoralf Klein, Heiko Strathmann, Yuyu Zhang, * Fernando Iglesias, Bjoern Esser, Soeren Sonnenburg, Saurabh Goyal */ @@ -15,22 +15,22 @@ namespace shogun { -class CCircularBuffer; -class CTokenizer; +class CircularBuffer; +class Tokenizer; /** @brief Class for buffered reading from a ascii file */ -class CLineReader : public CSGObject +class LineReader : public SGObject { public: /** default constructor */ - CLineReader(); + LineReader(); /** create object associated with the stream to read * * @param stream readable stream * @param tokenizer enabling to parse different ascii file formats (.csv, ...) */ - CLineReader(FILE* stream, CTokenizer* tokenizer); + LineReader(FILE* stream, std::shared_ptr tokenizer); /** create object associated with the stream to read * and specify maximum length of a string that can be read @@ -39,10 +39,10 @@ class CLineReader : public CSGObject * @param stream readable stream * @param tokenizer enabling to parse different ascii file formats (.csv, ...) */ - CLineReader(int32_t max_string_length, FILE* stream, CTokenizer* tokenizer); + LineReader(int32_t max_string_length, FILE* stream, std::shared_ptr tokenizer); /** deconstructor */ - virtual ~CLineReader(); + virtual ~LineReader(); /** check for next line in the stream * @@ -63,7 +63,7 @@ class CLineReader : public CSGObject * * @param tokenizer tokenizer */ - void set_tokenizer(CTokenizer* tokenizer); + void set_tokenizer(std::shared_ptr tokenizer); /** @return object name */ virtual const char* get_name() const { return "LineReader"; } @@ -80,10 +80,10 @@ class CLineReader : public CSGObject private: /** internal buffer for searching */ - CCircularBuffer* m_buffer; + std::shared_ptr m_buffer; /** */ - CTokenizer* m_tokenizer; + std::shared_ptr m_tokenizer; /** readable stream */ FILE* m_stream; diff --git a/src/shogun/io/MLDataHDF5File.cpp b/src/shogun/io/MLDataHDF5File.cpp index 4455e6c13d0..316b127aebe 100644 --- a/src/shogun/io/MLDataHDF5File.cpp +++ b/src/shogun/io/MLDataHDF5File.cpp @@ -18,7 +18,7 @@ using namespace shogun; -CMLDataHDF5File::CMLDataHDF5File() +MLDataHDF5File::MLDataHDF5File() { unstable(SOURCE_LOCATION); @@ -31,9 +31,9 @@ size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { return written; } -CMLDataHDF5File::CMLDataHDF5File(char* data_name, +MLDataHDF5File::MLDataHDF5File(char* data_name, const char* name, - const char* url_prefix) : CFile() + const char* url_prefix) : File() { get_boolean_type(); H5Eset_auto2(H5E_DEFAULT, NULL, NULL); @@ -82,7 +82,7 @@ CMLDataHDF5File::CMLDataHDF5File(char* data_name, error("Could not open data repository '{}'", data_name); } -CMLDataHDF5File::~CMLDataHDF5File() +MLDataHDF5File::~MLDataHDF5File() { H5Fclose(h5file); remove(fname); @@ -91,7 +91,7 @@ CMLDataHDF5File::~CMLDataHDF5File() } #define GET_VECTOR(fname, sg_type, datatype) \ -void CMLDataHDF5File::fname(sg_type*& vec, int32_t& len) \ +void MLDataHDF5File::fname(sg_type*& vec, int32_t& len) \ { \ if (!h5file) \ error("File invalid."); \ @@ -145,7 +145,7 @@ GET_VECTOR(get_vector, uint64_t, (CT_VECTOR, ST_NONE, PT_UINT64)) #undef GET_VECTOR #define GET_MATRIX(fname, sg_type, datatype) \ -void CMLDataHDF5File::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void MLDataHDF5File::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ if (!h5file) \ error("File invalid."); \ @@ -197,7 +197,7 @@ GET_MATRIX(get_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX)) #undef GET_MATRIX #define GET_SPARSEMATRIX(fname, sg_type, datatype) \ -void CMLDataHDF5File::fname(SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void MLDataHDF5File::fname(SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ if (!(file)) \ error("File invalid."); \ @@ -219,7 +219,7 @@ GET_SPARSEMATRIX(get_sparse_matrix, floatmax_t, DT_SPARSE_LONGREAL) #define GET_STRING_LIST(fname, sg_type, datatype) \ -void CMLDataHDF5File::fname(SGVector*& strings, int32_t& num_str, int32_t& max_string_len) \ +void MLDataHDF5File::fname(SGVector*& strings, int32_t& num_str, int32_t& max_string_len) \ { \ } @@ -238,7 +238,7 @@ GET_STRING_LIST(get_string_list, float64_t, DT_STRING_REAL) GET_STRING_LIST(get_string_list, floatmax_t, DT_STRING_LONGREAL) #undef GET_STRING_LIST -void CMLDataHDF5File::get_boolean_type() +void MLDataHDF5File::get_boolean_type() { boolean_type=H5T_NATIVE_UCHAR; switch (sizeof(bool)) @@ -260,7 +260,7 @@ void CMLDataHDF5File::get_boolean_type() } } -hid_t CMLDataHDF5File::get_compatible_type(H5T_class_t t_class, +hid_t MLDataHDF5File::get_compatible_type(H5T_class_t t_class, const TSGDataType* datatype) { switch (t_class) @@ -306,7 +306,7 @@ hid_t CMLDataHDF5File::get_compatible_type(H5T_class_t t_class, } } -void CMLDataHDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, int64_t& total_elements) +void MLDataHDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, int64_t& total_elements) { hid_t dataspace = H5Dget_space(dataset); if (dataspace<0) @@ -323,7 +323,7 @@ void CMLDataHDF5File::get_dims(hid_t dataset, int32_t*& dims, int32_t& ndims, in H5Sclose(dataspace); } -void CMLDataHDF5File::create_group_hierarchy() +void MLDataHDF5File::create_group_hierarchy() { char* vname=get_strdup(variable_name); int32_t vlen=strlen(vname); diff --git a/src/shogun/io/MLDataHDF5File.h b/src/shogun/io/MLDataHDF5File.h index 589e873ba39..5d83cbb1565 100644 --- a/src/shogun/io/MLDataHDF5File.h +++ b/src/shogun/io/MLDataHDF5File.h @@ -28,11 +28,11 @@ struct TSGDataType; * */ #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CMLDataHDF5File : public CFile +IGNORE_IN_CLASSLIST class MLDataHDF5File : public File { public: /** default constructor */ - CMLDataHDF5File(); + MLDataHDF5File(); /** constructor * @@ -40,12 +40,12 @@ IGNORE_IN_CLASSLIST class CMLDataHDF5File : public CFile * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CMLDataHDF5File(char* fname, + MLDataHDF5File(char* fname, const char* name=NULL, const char* url_prefix="http://mldata.org/repository/data/download/"); /** default destructor */ - virtual ~CMLDataHDF5File(); + virtual ~MLDataHDF5File(); /** @name Vector Access Functions * diff --git a/src/shogun/io/MemoryMappedFile.h b/src/shogun/io/MemoryMappedFile.h index 1ba033bfb5f..cc2325cb018 100644 --- a/src/shogun/io/MemoryMappedFile.h +++ b/src/shogun/io/MemoryMappedFile.h @@ -29,11 +29,11 @@ namespace shogun * * Implements a memory mapped file for super fast file access. */ -template class CMemoryMappedFile : public CSGObject +template class MemoryMappedFile : public SGObject { public: /** default constructor */ - CMemoryMappedFile() :CSGObject() + MemoryMappedFile() :SGObject() { unstable(SOURCE_LOCATION); @@ -59,8 +59,8 @@ template class CMemoryMappedFile : public CSGObject * before closing the file. * */ - CMemoryMappedFile(const char* fname, char flag='r', int64_t fsize=0) - : CSGObject() + MemoryMappedFile(const char* fname, char flag='r', int64_t fsize=0) + : SGObject() { require(flag=='w' || flag=='r', "Only 'r' and 'w' flags are allowed"); @@ -138,7 +138,7 @@ template class CMemoryMappedFile : public CSGObject } /** destructor */ - virtual ~CMemoryMappedFile() + virtual ~MemoryMappedFile() { #ifdef _MSC_VER UnmapViewOfFile(address); @@ -254,7 +254,7 @@ template class CMemoryMappedFile : public CSGObject /** set file size * * When the file is opened for read/write mode, it will be truncated - * upon destruction of the CMemoryMappedFile object. This is + * upon destruction of the MemoryMappedFile object. This is * automagically determined when writing lines, but might have to be * set manually for other data types, which is what this function is for. * diff --git a/src/shogun/io/Parser.cpp b/src/shogun/io/Parser.cpp index 0efe1879bf3..cb56c77d526 100644 --- a/src/shogun/io/Parser.cpp +++ b/src/shogun/io/Parser.cpp @@ -10,30 +10,30 @@ using namespace shogun; -CParser::CParser() +Parser::Parser() { init(); } -CParser::CParser(SGVector text, CTokenizer* tokenizer) +Parser::Parser(SGVector text, std::shared_ptr tokenizer) { init(); m_text=text; - SG_REF(tokenizer); + m_tokenizer=tokenizer; if (m_tokenizer!=NULL) m_tokenizer->set_text(m_text); } -CParser::~CParser() +Parser::~Parser() { - SG_UNREF(m_tokenizer); + } -bool CParser::has_next() +bool Parser::has_next() { if (m_tokenizer!=NULL) return m_tokenizer->has_next(); @@ -41,13 +41,13 @@ bool CParser::has_next() return false; } -void CParser::skip_token() +void Parser::skip_token() { index_t start=0; m_tokenizer->next_token_idx(start); } -SGVector CParser::read_string() +SGVector Parser::read_string() { index_t start=0; index_t end=0; @@ -63,7 +63,7 @@ SGVector CParser::read_string() return result; } -SGVector CParser::read_cstring() +SGVector Parser::read_cstring() { index_t start=0; index_t end=0; @@ -80,7 +80,7 @@ SGVector CParser::read_cstring() return result; } -bool CParser::read_bool() +bool Parser::read_bool() { SGVector token=read_cstring(); @@ -91,7 +91,7 @@ bool CParser::read_bool() } #define READ_INT_METHOD(fname, convf, sg_type) \ -sg_type CParser::fname() \ +sg_type Parser::fname() \ { \ SGVector token=read_cstring(); \ \ @@ -106,7 +106,7 @@ READ_INT_METHOD(read_ulong, strtoull, uint64_t) #undef READ_INT_METHOD #define READ_REAL_METHOD(fname, convf, sg_type) \ -sg_type CParser::fname() \ +sg_type Parser::fname() \ { \ SGVector token=read_cstring(); \ \ @@ -132,7 +132,7 @@ READ_REAL_METHOD(read_long_real, strtod, floatmax_t) #endif #undef READ_REAL_METHOD -void CParser::set_text(SGVector text) +void Parser::set_text(SGVector text) { m_text=text; @@ -140,17 +140,17 @@ void CParser::set_text(SGVector text) m_tokenizer->set_text(m_text); } -void CParser::set_tokenizer(CTokenizer* tokenizer) +void Parser::set_tokenizer(std::shared_ptr tokenizer) { - SG_REF(tokenizer); - SG_UNREF(m_tokenizer); + + m_tokenizer=tokenizer; if (m_tokenizer!=NULL) m_tokenizer->set_text(m_text); } -void CParser::init() +void Parser::init() { m_text=SGVector(); m_tokenizer=NULL; diff --git a/src/shogun/io/Parser.h b/src/shogun/io/Parser.h index db9f3e7b22e..f07269dc210 100644 --- a/src/shogun/io/Parser.h +++ b/src/shogun/io/Parser.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Thoralf Klein, Soeren Sonnenburg, Yuyu Zhang, + * Authors: Evgeniy Andreev, Thoralf Klein, Soeren Sonnenburg, Yuyu Zhang, * Bjoern Esser */ @@ -15,24 +15,24 @@ namespace shogun { -class CTokenizer; +class Tokenizer; /** @brief Class for reading from a string */ -class CParser : public CSGObject +class Parser : public SGObject { public: /** default constructor */ - CParser(); + Parser(); /** constructor * * @param string the text to parse * @param tokenizer tokenizer */ - CParser(SGVector string, CTokenizer* tokenizer); + Parser(SGVector string, std::shared_ptr tokenizer); /** destructor */ - virtual ~CParser(); + virtual ~Parser(); /** check for next line in the stream * @@ -69,7 +69,7 @@ class CParser : public CSGObject * * @param tokenizer tokenizer */ - void set_tokenizer(CTokenizer* tokenizer); + void set_tokenizer(std::shared_ptr tokenizer); /** set the char array that requires tokenization * @@ -89,7 +89,7 @@ class CParser : public CSGObject SGVector m_text; /** tokenizer */ - CTokenizer* m_tokenizer; + std::shared_ptr m_tokenizer; }; } diff --git a/src/shogun/io/ProtobufFile.cpp b/src/shogun/io/ProtobufFile.cpp index 6efa05b93d9..52ffc55aab3 100644 --- a/src/shogun/io/ProtobufFile.cpp +++ b/src/shogun/io/ProtobufFile.cpp @@ -18,29 +18,29 @@ using namespace shogun; -CProtobufFile::CProtobufFile() +ProtobufFile::ProtobufFile() { init(); } -CProtobufFile::CProtobufFile(FILE* f, const char* name) : - CFile(f, name) +ProtobufFile::ProtobufFile(FILE* f, const char* name) : + File(f, name) { init(); } -CProtobufFile::CProtobufFile(const char* fname, char rw, const char* name) : - CFile(fname, rw, name) +ProtobufFile::ProtobufFile(const char* fname, char rw, const char* name) : + File(fname, rw, name) { init(); } -CProtobufFile::~CProtobufFile() +ProtobufFile::~ProtobufFile() { SG_FREE(buffer); } -void CProtobufFile::init() +void ProtobufFile::init() { version=1; message_size=1024*1024; @@ -49,7 +49,7 @@ void CProtobufFile::init() } #define GET_VECTOR(sg_type) \ -void CProtobufFile::get_vector(sg_type*& vector, int32_t& len) \ +void ProtobufFile::get_vector(sg_type*& vector, int32_t& len) \ { \ read_and_validate_global_header(ShogunVersion::VECTOR); \ VectorHeader data_header=read_vector_header(); \ @@ -72,7 +72,7 @@ GET_VECTOR(uint64_t) #undef GET_VECTOR #define GET_MATRIX(read_func, sg_type) \ -void CProtobufFile::get_matrix(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ +void ProtobufFile::get_matrix(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ read_and_validate_global_header(ShogunVersion::MATRIX); \ MatrixHeader data_header=read_matrix_header(); \ @@ -96,7 +96,7 @@ GET_MATRIX(read_ulong, uint64_t) #undef GET_MATRIX #define GET_NDARRAY(read_func, sg_type) \ -void CProtobufFile::get_ndarray(sg_type*& array, int32_t*& dims, int32_t& num_dims) \ +void ProtobufFile::get_ndarray(sg_type*& array, int32_t*& dims, int32_t& num_dims) \ { \ not_implemented(SOURCE_LOCATION); \ } @@ -111,7 +111,7 @@ GET_NDARRAY(read_word, uint16_t) #undef GET_NDARRAY #define GET_SPARSE_MATRIX(sg_type) \ -void CProtobufFile::get_sparse_matrix( \ +void ProtobufFile::get_sparse_matrix( \ SGSparseVector*& matrix, int32_t& num_feat, int32_t& num_vec) \ { \ read_and_validate_global_header(ShogunVersion::SPARSE_MATRIX); \ @@ -137,7 +137,7 @@ GET_SPARSE_MATRIX(uint64_t) #undef GET_SPARSE_MATRIX #define SET_VECTOR(sg_type) \ -void CProtobufFile::set_vector(const sg_type* vector, int32_t len) \ +void ProtobufFile::set_vector(const sg_type* vector, int32_t len) \ { \ int32_t num_messages=compute_num_messages(len, sizeof(sg_type)); \ write_global_header(ShogunVersion::VECTOR); \ @@ -160,7 +160,7 @@ SET_VECTOR(uint16_t) #undef SET_VECTOR #define SET_MATRIX(sg_type) \ -void CProtobufFile::set_matrix(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ +void ProtobufFile::set_matrix(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \ { \ int32_t num_messages=compute_num_messages(num_feat*num_vec, sizeof(sg_type)); \ write_global_header(ShogunVersion::MATRIX); \ @@ -183,7 +183,7 @@ SET_MATRIX(uint16_t) #undef SET_MATRIX #define SET_SPARSE_MATRIX(sg_type) \ -void CProtobufFile::set_sparse_matrix( \ +void ProtobufFile::set_sparse_matrix( \ const SGSparseVector* matrix, int32_t num_feat, int32_t num_vec) \ { \ write_global_header(ShogunVersion::SPARSE_MATRIX); \ @@ -207,7 +207,7 @@ SET_SPARSE_MATRIX(uint16_t) #undef SET_SPARSE_MATRIX #define GET_STRING_LIST(sg_type) \ -void CProtobufFile::get_string_list( \ +void ProtobufFile::get_string_list( \ SGVector*& strings, int32_t& num_str, \ int32_t& max_string_len) \ { \ @@ -233,7 +233,7 @@ GET_STRING_LIST(uint16_t) #undef GET_STRING_LIST #define SET_STRING_LIST(sg_type) \ -void CProtobufFile::set_string_list( \ +void ProtobufFile::set_string_list( \ const SGVector* strings, int32_t num_str) \ { \ write_global_header(ShogunVersion::STRING_LIST); \ @@ -255,7 +255,7 @@ SET_STRING_LIST(int16_t) SET_STRING_LIST(uint16_t) #undef SET_STRING_LIST -void CProtobufFile::write_big_endian_uint(uint32_t number, uint8_t* array, uint32_t size) +void ProtobufFile::write_big_endian_uint(uint32_t number, uint8_t* array, uint32_t size) { if (size<4) error("array is too small to write"); @@ -266,7 +266,7 @@ void CProtobufFile::write_big_endian_uint(uint32_t number, uint8_t* array, uint3 array[3]=number&0xffu; } -uint32_t CProtobufFile::read_big_endian_uint(uint8_t* array, uint32_t size) +uint32_t ProtobufFile::read_big_endian_uint(uint8_t* array, uint32_t size) { if (size<4) error("array is too small to read"); @@ -274,7 +274,7 @@ uint32_t CProtobufFile::read_big_endian_uint(uint8_t* array, uint32_t size) return (array[0]<<24) | (array[1]<<16) | (array[2]<<8) | array[3]; } -int32_t CProtobufFile::compute_num_messages(uint64_t len, int32_t sizeof_type) const +int32_t ProtobufFile::compute_num_messages(uint64_t len, int32_t sizeof_type) const { uint32_t elements_in_message=message_size/sizeof_type; uint32_t num_messages=len/elements_in_message; @@ -284,7 +284,7 @@ int32_t CProtobufFile::compute_num_messages(uint64_t len, int32_t sizeof_type) c return num_messages; } -void CProtobufFile::read_and_validate_global_header(ShogunVersion_SGDataType type) +void ProtobufFile::read_and_validate_global_header(ShogunVersion_SGDataType type) { ShogunVersion header; read_message(header); @@ -292,7 +292,7 @@ void CProtobufFile::read_and_validate_global_header(ShogunVersion_SGDataType typ require(header.data_type()==type, "wrong type"); } -void CProtobufFile::write_global_header(ShogunVersion_SGDataType type) +void ProtobufFile::write_global_header(ShogunVersion_SGDataType type) { ShogunVersion header; header.set_version(version); @@ -300,7 +300,7 @@ void CProtobufFile::write_global_header(ShogunVersion_SGDataType type) write_message(header); } -VectorHeader CProtobufFile::read_vector_header() +VectorHeader ProtobufFile::read_vector_header() { VectorHeader data_header; read_message(data_header); @@ -308,7 +308,7 @@ VectorHeader CProtobufFile::read_vector_header() return data_header; } -SparseMatrixHeader CProtobufFile::read_sparse_matrix_header() +SparseMatrixHeader ProtobufFile::read_sparse_matrix_header() { SparseMatrixHeader data_header; read_message(data_header); @@ -316,7 +316,7 @@ SparseMatrixHeader CProtobufFile::read_sparse_matrix_header() return data_header; } -MatrixHeader CProtobufFile::read_matrix_header() +MatrixHeader ProtobufFile::read_matrix_header() { MatrixHeader data_header; read_message(data_header); @@ -324,7 +324,7 @@ MatrixHeader CProtobufFile::read_matrix_header() return data_header; } -StringListHeader CProtobufFile::read_string_list_header() +StringListHeader ProtobufFile::read_string_list_header() { StringListHeader data_header; read_message(data_header); @@ -332,7 +332,7 @@ StringListHeader CProtobufFile::read_string_list_header() return data_header; } -void CProtobufFile::write_vector_header(int32_t len, int32_t num_messages) +void ProtobufFile::write_vector_header(int32_t len, int32_t num_messages) { VectorHeader data_header; data_header.set_len(len); @@ -340,7 +340,7 @@ void CProtobufFile::write_vector_header(int32_t len, int32_t num_messages) write_message(data_header); } -void CProtobufFile::write_matrix_header(int32_t num_feat, int32_t num_vec, int32_t num_messages) +void ProtobufFile::write_matrix_header(int32_t num_feat, int32_t num_vec, int32_t num_messages) { MatrixHeader data_header; data_header.set_num_cols(num_feat); @@ -350,7 +350,7 @@ void CProtobufFile::write_matrix_header(int32_t num_feat, int32_t num_vec, int32 } #define WRITE_SPARSE_MATRIX_HEADER(sg_type) \ -void CProtobufFile::write_sparse_matrix_header( \ +void ProtobufFile::write_sparse_matrix_header( \ const SGSparseVector* matrix, int32_t num_feat, int32_t num_vec) \ { \ SparseMatrixHeader data_header; \ @@ -380,7 +380,7 @@ WRITE_SPARSE_MATRIX_HEADER(uint16_t) #undef WRITE_SPARSE_MATRIX_HEADER #define WRITE_STRING_LIST_HEADER(sg_type) \ -void CProtobufFile::write_string_list_header(const SGVector* strings, int32_t num_str) \ +void ProtobufFile::write_string_list_header(const SGVector* strings, int32_t num_str) \ { \ int32_t max_string_len=0; \ StringListHeader data_header; \ @@ -409,7 +409,7 @@ WRITE_STRING_LIST_HEADER(int16_t) WRITE_STRING_LIST_HEADER(uint16_t) #undef WRITE_STRING_LIST_HEADER -void CProtobufFile::read_message(google::protobuf::Message& message) +void ProtobufFile::read_message(google::protobuf::Message& message) { uint32_t bytes_read=0; uint32_t msg_size=0; @@ -428,7 +428,7 @@ void CProtobufFile::read_message(google::protobuf::Message& message) require(message.ParseFromArray(buffer, msg_size), "cannot parse header"); } -void CProtobufFile::write_message(const google::protobuf::Message& message) +void ProtobufFile::write_message(const google::protobuf::Message& message) { uint32_t bytes_write=0; uint32_t msg_size=message.ByteSize(); @@ -445,7 +445,7 @@ void CProtobufFile::write_message(const google::protobuf::Message& message) } #define READ_MEMORY_BLOCK(chunk_type, sg_type) \ -void CProtobufFile::read_memory_block(sg_type*& vector, uint64_t len, int32_t num_messages) \ +void ProtobufFile::read_memory_block(sg_type*& vector, uint64_t len, int32_t num_messages) \ { \ vector=SG_MALLOC(sg_type, len); \ \ @@ -481,7 +481,7 @@ READ_MEMORY_BLOCK(UInt64Chunk, uint64_t) #undef READ_MEMORY_BLOCK #define WRITE_MEMORY_BLOCK(chunk_type, sg_type) \ -void CProtobufFile::write_memory_block(const sg_type* vector, uint64_t len, int32_t num_messages) \ +void ProtobufFile::write_memory_block(const sg_type* vector, uint64_t len, int32_t num_messages) \ { \ chunk_type chunk; \ int32_t elements_in_message=message_size/sizeof(sg_type); \ @@ -517,7 +517,7 @@ WRITE_MEMORY_BLOCK(UInt32Chunk, uint16_t) #undef WRITE_MEMORY_BLOCK #define READ_SPARSE_MATRIX(chunk_type, sg_type) \ -void CProtobufFile::read_sparse_matrix( \ +void ProtobufFile::read_sparse_matrix( \ SGSparseVector*& matrix, const SparseMatrixHeader& data_header) \ { \ matrix=SG_MALLOC(SGSparseVector, data_header.num_vectors()); \ @@ -564,7 +564,7 @@ READ_SPARSE_MATRIX(UInt64Chunk, uint64_t) #undef READ_SPARSE_MATRIX #define WRITE_SPARSE_MATRIX(chunk_type, sg_type) \ -void CProtobufFile::write_sparse_matrix( \ +void ProtobufFile::write_sparse_matrix( \ const SGSparseVector* matrix, int32_t num_vec) \ { \ UInt64Chunk feat_index_chunk; \ @@ -613,7 +613,7 @@ WRITE_SPARSE_MATRIX(UInt32Chunk, uint16_t) #undef WRITE_SPARSE_MATRIX #define READ_STRING_LIST(chunk_type, sg_type) \ -void CProtobufFile::read_string_list( \ +void ProtobufFile::read_string_list( \ SGVector*& strings, const StringListHeader& data_header) \ { \ strings=SG_MALLOC(SGVector, data_header.num_str()); \ @@ -654,7 +654,7 @@ READ_STRING_LIST(UInt64Chunk, uint64_t) #undef READ_STRING_LIST #define WRITE_STRING_LIST(chunk_type, sg_type) \ -void CProtobufFile::write_string_list( \ +void ProtobufFile::write_string_list( \ const SGVector* strings, int32_t num_str) \ { \ chunk_type chunk; \ diff --git a/src/shogun/io/ProtobufFile.h b/src/shogun/io/ProtobufFile.h index 933c2b88637..bb7417ce36b 100644 --- a/src/shogun/io/ProtobufFile.h +++ b/src/shogun/io/ProtobufFile.h @@ -49,18 +49,18 @@ namespace shogun * ... * */ -class CProtobufFile : public CFile +class ProtobufFile : public File { public: /** default constructor */ - CProtobufFile(); + ProtobufFile(); /** constructor * * @param f already opened file * @param name variable name (e.g. "x" or "/path/to/x") */ - CProtobufFile(FILE* f, const char* name=NULL); + ProtobufFile(FILE* f, const char* name=NULL); /** constructor * @@ -68,10 +68,10 @@ class CProtobufFile : public CFile * @param rw mode, 'r' or 'w' * @param name variable name (e.g. "x" or "/path/to/x") */ - CProtobufFile(const char* fname, char rw='r', const char* name=NULL); + ProtobufFile(const char* fname, char rw='r', const char* name=NULL); /** destructor */ - virtual ~CProtobufFile(); + virtual ~ProtobufFile(); #ifndef SWIG // SWIG should skip this /** @name Vector Access Functions diff --git a/src/shogun/io/Serializable.h b/src/shogun/io/Serializable.h index 1cff538d688..3e83fb251b0 100644 --- a/src/shogun/io/Serializable.h +++ b/src/shogun/io/Serializable.h @@ -59,20 +59,20 @@ struct extract_value_type> * This only works with classes derived of SGReferencedData (SGVector, SGMatrix, * SGStringList, etc) and fundamental types. */ -template class CSerializable: public CSGObject +template class Serializable: public SGObject { public: /** Default constructor. Do not use. */ - CSerializable() : CSGObject() + Serializable() : SGObject() { init(); } /** Constructor. - * @param value Value to serialize as CSGObject. + * @param value Value to serialize as SGObject. * @param value_name Name under which value is registered. */ - CSerializable(T value, const char* value_name=""): CSGObject(), m_name(value_name) + Serializable(T value, const char* value_name=""): SGObject(), m_name(value_name) { init(); m_value = value; @@ -85,7 +85,7 @@ template class CSerializable: public CSGObject */ virtual T get_value() const { return m_value; } - /** @return name of the CSGObject, without C prefix */ + /** @return name of the SGObject, without C prefix */ virtual const char* get_name() const { return "Serializable"; } private: @@ -104,43 +104,43 @@ template class CSerializable: public CSGObject }; // FIXME: once the class factory is refactored this should be dropped and -// CSerializable should be use directly -template class CVectorSerializable: public CSerializable> +// Serializable should be use directly +template class VectorSerializable: public Serializable> { public: - CVectorSerializable() : CSerializable>() {} - CVectorSerializable(SGVector value, const char* value_name=""): CSerializable>(value, value_name) {} - virtual ~CVectorSerializable() {} + VectorSerializable() : Serializable>() {} + VectorSerializable(SGVector value, const char* value_name=""): Serializable>(value, value_name) {} + virtual ~VectorSerializable() {} - /** @return name of the CSGObject, without C prefix */ + /** @return name of the SGObject, without C prefix */ virtual const char* get_name() const { return "VectorSerializable"; } }; -template class CMatrixSerializable: public CSerializable> +template class MatrixSerializable: public Serializable> { public: - CMatrixSerializable() : CSerializable>() {} - CMatrixSerializable(SGMatrix value, const char* value_name=""): CSerializable>(value, value_name) {} - virtual ~CMatrixSerializable() {} + MatrixSerializable() : Serializable>() {} + MatrixSerializable(SGMatrix value, const char* value_name=""): Serializable>(value, value_name) {} + virtual ~MatrixSerializable() {} - /** @return name of the CSGObject, without C prefix */ + /** @return name of the SGObject, without C prefix */ virtual const char* get_name() const { return "MatrixSerializable"; } }; // FIXME: there is no SG_ADD for std::vector so need to do that manually. // can be dropped once SG_ADD works with std::vector // Note: cannot inherit from CSerializable as need to overload/change init() -template class CStdVectorSerializable: public CSGObject +template class StdVectorSerializable: public SGObject { public: - CStdVectorSerializable() : CSGObject() { init(); } - CStdVectorSerializable(const std::vector& value, const char* value_name="") - : CSGObject(), m_name(value_name) + StdVectorSerializable() : SGObject() { init(); } + StdVectorSerializable(const std::vector& value, const char* value_name="") + : SGObject(), m_name(value_name) { init(); m_value = value; } - virtual ~CStdVectorSerializable() {} + virtual ~StdVectorSerializable() {} virtual const char* get_name() const { return "StdVectorSerializable"; } protected: @@ -162,15 +162,15 @@ template class CStdVectorSerializable: public CSGObject std::string m_name; }; -template class CVectorListSerializable: public CStdVectorSerializable> +template class VectorListSerializable: public StdVectorSerializable> { public: - CVectorListSerializable(const std::vector>& value, const char* value_name="") - : CStdVectorSerializable>(value, value_name) + VectorListSerializable(const std::vector>& value, const char* value_name="") + : StdVectorSerializable>(value, value_name) { init(); } - CVectorListSerializable() : CStdVectorSerializable>() + VectorListSerializable() : StdVectorSerializable>() { init(); } @@ -180,7 +180,7 @@ template class CVectorListSerializable: public CStdVectorSerializable(); + SGObject::set_generic(); } }; diff --git a/src/shogun/io/Serializable.h~HEAD b/src/shogun/io/Serializable.h~HEAD new file mode 100644 index 00000000000..c9bab7985cd --- /dev/null +++ b/src/shogun/io/Serializable.h~HEAD @@ -0,0 +1,101 @@ +/* + * -*- coding: utf-8 -*- + * vim: set fileencoding=utf-8 + * + * Copyright (c) 2017, Shogun-Toolbox e.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef SHOGUN_SERIALIZABLE_H__ +#define SHOGUN_SERIALIZABLE_H__ + +#include + +namespace shogun +{ + +template +struct extract_value_type +{ + typedef T value_type; +}; + +template class X, typename T, typename ...Args> +struct extract_value_type> +{ + typedef T value_type; +}; + +/** @brief A trait that makes a none SGObject SG-serializable + * This only works with classes derived of SGReferencedData (SGVector, SGMatrix etc) + * and fundamental types (std::is_arithmetic) + */ +template class CSerializable: public CSGObject +{ +public: + /** Default constructor. Do not use. */ + CSerializable() : CSGObject() + { + init(); + } + + /** Constructor. + * @param value Value to serialize as CSGObject. + * @param value_name Name under which value is registered. + */ + CSerializable(T value, const char* value_name="") + { + init(); + m_value = value; + m_value_name = value_name; + } + + /** @return name of the CSGObject, without C prefix */ + virtual const char* get_name() const { return "Serializable"; } + +private: + void init() + { + set_generic::value_type>(); + m_value = 0; + m_value_name = "Unnamed"; + + SG_ADD(&m_value, "value", "Serialized value", MS_NOT_AVAILABLE); + } + +protected: + /** Serialized value. */ + T m_value; + + /** Name of serialized value */ + const char* m_value_name; +}; + +}; +#endif // SHOGUN_SERIALIZABLE_H_ diff --git a/src/shogun/io/Serializable.h~fde49f830248c86872a33ff89593575bb68c19d0 b/src/shogun/io/Serializable.h~fde49f830248c86872a33ff89593575bb68c19d0 new file mode 100644 index 00000000000..c9bab7985cd --- /dev/null +++ b/src/shogun/io/Serializable.h~fde49f830248c86872a33ff89593575bb68c19d0 @@ -0,0 +1,101 @@ +/* + * -*- coding: utf-8 -*- + * vim: set fileencoding=utf-8 + * + * Copyright (c) 2017, Shogun-Toolbox e.V. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ +#ifndef SHOGUN_SERIALIZABLE_H__ +#define SHOGUN_SERIALIZABLE_H__ + +#include + +namespace shogun +{ + +template +struct extract_value_type +{ + typedef T value_type; +}; + +template class X, typename T, typename ...Args> +struct extract_value_type> +{ + typedef T value_type; +}; + +/** @brief A trait that makes a none SGObject SG-serializable + * This only works with classes derived of SGReferencedData (SGVector, SGMatrix etc) + * and fundamental types (std::is_arithmetic) + */ +template class CSerializable: public CSGObject +{ +public: + /** Default constructor. Do not use. */ + CSerializable() : CSGObject() + { + init(); + } + + /** Constructor. + * @param value Value to serialize as CSGObject. + * @param value_name Name under which value is registered. + */ + CSerializable(T value, const char* value_name="") + { + init(); + m_value = value; + m_value_name = value_name; + } + + /** @return name of the CSGObject, without C prefix */ + virtual const char* get_name() const { return "Serializable"; } + +private: + void init() + { + set_generic::value_type>(); + m_value = 0; + m_value_name = "Unnamed"; + + SG_ADD(&m_value, "value", "Serialized value", MS_NOT_AVAILABLE); + } + +protected: + /** Serialized value. */ + T m_value; + + /** Name of serialized value */ + const char* m_value_name; +}; + +}; +#endif // SHOGUN_SERIALIZABLE_H_ diff --git a/src/shogun/io/SimpleFile.h b/src/shogun/io/SimpleFile.h index ff22b1bfe9e..6a6a672e651 100644 --- a/src/shogun/io/SimpleFile.h +++ b/src/shogun/io/SimpleFile.h @@ -26,11 +26,11 @@ namespace shogun * * Currently only simple reading and writing of blocks is supported. */ -template class CSimpleFile : public CSGObject +template class SimpleFile : public SGObject { public: /** default constructor */ - CSimpleFile() :CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL) + SimpleFile() :SGObject(), line_buffer_size(1024*1024), line_buffer(NULL) { unstable(SOURCE_LOCATION); @@ -47,15 +47,15 @@ template class CSimpleFile : public CSGObject * @param fname filename * @param f file descriptor */ - CSimpleFile(char* fname, FILE* f) - : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL) + SimpleFile(char* fname, FILE* f) + : SGObject(), line_buffer_size(1024*1024), line_buffer(NULL) { file=f; filename=get_strdup(fname); status = (file!=NULL && filename!=NULL); } - virtual ~CSimpleFile() + virtual ~SimpleFile() { SG_FREE(filename); free_line_buffer(); diff --git a/src/shogun/io/TBOutputFormat.h b/src/shogun/io/TBOutputFormat.h index 9cd058c5342..21baa3e7fdb 100644 --- a/src/shogun/io/TBOutputFormat.h +++ b/src/shogun/io/TBOutputFormat.h @@ -51,7 +51,7 @@ namespace shogun * Convert an std::pair to a tensorflow::Event, * which can be written to file and used with tools like Tensorboard. */ - class TBOutputFormat : public CSGObject + class TBOutputFormat : public SGObject { public: diff --git a/src/shogun/io/UAIFile.cpp b/src/shogun/io/UAIFile.cpp index 4878205e99d..07f707247b1 100644 --- a/src/shogun/io/UAIFile.cpp +++ b/src/shogun/io/UAIFile.cpp @@ -11,51 +11,51 @@ using namespace shogun; -CUAIFile::CUAIFile() +UAIFile::UAIFile() { init(); } -CUAIFile::CUAIFile(FILE* f, const char* name) : - CFile(f, name) +UAIFile::UAIFile(FILE* f, const char* name) : + File(f, name) { init(); init_with_defaults(); } #ifdef HAVE_FDOPEN -CUAIFile::CUAIFile(int fd, const char* mode, const char* name) : - CFile(fd, mode, name) +UAIFile::UAIFile(int fd, const char* mode, const char* name) : + File(fd, mode, name) { init(); init_with_defaults(); } #endif -CUAIFile::CUAIFile(const char* fname, char rw, const char* name) : - CFile(fname, rw, name) +UAIFile::UAIFile(const char* fname, char rw, const char* name) : + File(fname, rw, name) { init(); init_with_defaults(); } -CUAIFile::~CUAIFile() +UAIFile::~UAIFile() { - SG_UNREF(m_tokenizer); - SG_UNREF(m_line_tokenizer); - SG_UNREF(m_parser); - SG_UNREF(m_line_reader); + + + + SG_FREE(m_factors_table); SG_FREE(m_factors_scope); } -void CUAIFile::init() +void UAIFile::init() { - SG_ADD((CSGObject**)&m_line_reader, "line_reader", "line reader used to read lines from file"); - SG_ADD((CSGObject**)&m_parser, "parser", "parser used to parse file"); - SG_ADD((CSGObject**)&m_line_tokenizer, "line_tokenizer", "line tokenizer used to parse file"); - SG_ADD((CSGObject**)&m_tokenizer, "tokenizer", "tokenizer used to parse file"); + SG_ADD((std::shared_ptr*)&m_line_reader, "line_reader", "line reader used to read lines from file"); + SG_ADD((std::shared_ptr*)&m_parser, "parser", "parser used to parse file"); + SG_ADD((std::shared_ptr*)&m_line_tokenizer, "line_tokenizer", "line tokenizer used to parse file"); + SG_ADD((std::shared_ptr*)&m_tokenizer, "tokenizer", "tokenizer used to parse file"); SG_ADD(&m_delimiter, "delimiter", "delimiter used in get_vector function"); SG_ADD(&m_num_vars, "num_vars", "number of variables"); @@ -81,28 +81,28 @@ void CUAIFile::init() m_factors_scope = NULL; } -void CUAIFile::init_with_defaults() +void UAIFile::init_with_defaults() { m_delimiter=' '; - m_tokenizer=new CDelimiterTokenizer(true); + m_tokenizer=std::make_shared(true); m_tokenizer->delimiters[m_delimiter]=1; - SG_REF(m_tokenizer); - m_line_tokenizer=new CDelimiterTokenizer(true); + + m_line_tokenizer=std::make_shared(true); m_line_tokenizer->delimiters['\n']=1; - SG_REF(m_line_tokenizer); - m_parser=new CParser(); + + m_parser=std::make_shared(); m_parser->set_tokenizer(m_tokenizer); - SG_REF(m_parser); - m_line_reader=new CLineReader(file, m_line_tokenizer); - SG_REF(m_line_reader); + + m_line_reader=std::make_shared(file, m_line_tokenizer); + } #define GET_VECTOR(read_func, sg_type) \ - void CUAIFile::get_vector(sg_type*& vector, int32_t& len) \ + void UAIFile::get_vector(sg_type*& vector, int32_t& len) \ { \ SG_SET_LOCALE_C; \ \ @@ -145,7 +145,7 @@ GET_VECTOR(read_ulong, uint64_t) #undef GET_VECTOR #define SET_VECTOR(format, sg_type) \ -void CUAIFile::set_vector(const sg_type* vector, int32_t len) \ +void UAIFile::set_vector(const sg_type* vector, int32_t len) \ { \ SG_SET_LOCALE_C; \ \ @@ -171,7 +171,7 @@ SET_VECTOR(SCNi16, int16_t) SET_VECTOR(SCNu16, uint16_t) #undef SET_VECTOR -void CUAIFile::parse() +void UAIFile::parse() { if (!file) error("No file specified"); @@ -221,7 +221,7 @@ void CUAIFile::parse() } } -void CUAIFile::set_net_type(const char* net_type) +void UAIFile::set_net_type(const char* net_type) { require ((strncmp(net_type, "BAYES", 5) == 0 || strncmp(net_type, "MARKOV", 6) == 0), "Network type should be either MARKOV or BAYES"); @@ -233,13 +233,13 @@ void CUAIFile::set_net_type(const char* net_type) fprintf(file, "%s\n", net_type); } -void CUAIFile::set_num_vars(int32_t num_vars) +void UAIFile::set_num_vars(int32_t num_vars) { m_num_vars = num_vars; fprintf(file, "%d\n", num_vars); } -void CUAIFile::set_vars_card(SGVector vars_card) +void UAIFile::set_vars_card(SGVector vars_card) { require (m_num_vars == vars_card.vlen, "Variables mismatch. Expected {} variables, got {} variables", @@ -249,13 +249,13 @@ void CUAIFile::set_vars_card(SGVector vars_card) set_vector(vars_card.vector, vars_card.vlen); } -void CUAIFile::set_num_factors(int32_t num_factors) +void UAIFile::set_num_factors(int32_t num_factors) { m_num_factors = num_factors; fprintf(file, "%d\n", num_factors); } -void CUAIFile::set_factors_scope(int num_factors, +void UAIFile::set_factors_scope(int num_factors, const SGVector* factors_scope) { require(num_factors == m_num_factors, "Factors mismatch. Expected %d factors; \ @@ -273,7 +273,7 @@ void CUAIFile::set_factors_scope(int num_factors, } } -void CUAIFile::set_factors_table(int32_t num_factors, +void UAIFile::set_factors_table(int32_t num_factors, const SGVector* factors_table) { require(num_factors == m_num_factors, "Factors mismatch. Expected {} factors; \ @@ -290,7 +290,7 @@ void CUAIFile::set_factors_table(int32_t num_factors, } } -void CUAIFile::get_preamble(SGVector& net_type, +void UAIFile::get_preamble(SGVector& net_type, int32_t& num_vars, SGVector& vars_card, int32_t& num_factors, @@ -306,7 +306,7 @@ void CUAIFile::get_preamble(SGVector& net_type, factors_scope[i] = m_factors_scope[i]; } -void CUAIFile::get_factors_table(SGVector*& factors_table) +void UAIFile::get_factors_table(SGVector*& factors_table) { factors_table = new SGVector [m_num_factors]; for (int32_t i=0; i m_line_reader; /** parser of lines */ - CParser* m_parser; + std::shared_ptr m_parser; /** tokenizer for line_reader */ - CDelimiterTokenizer* m_line_tokenizer; + std::shared_ptr m_line_tokenizer; /** tokenizer for parser */ - CDelimiterTokenizer* m_tokenizer; + std::shared_ptr m_tokenizer; /** delimiter */ char m_delimiter; diff --git a/src/shogun/io/serialization/BitseryDeserializer.cpp b/src/shogun/io/serialization/BitseryDeserializer.cpp index caa8bab9a1f..40d193dd919 100644 --- a/src/shogun/io/serialization/BitseryDeserializer.cpp +++ b/src/shogun/io/serialization/BitseryDeserializer.cpp @@ -49,14 +49,10 @@ class BitseryReaderVisitor: public detail::BitseryVisitor* v) { SG_DEBUG("reading SGObject: "); - if (*v != nullptr) - SG_UNREF(*v); *v = object_reader(s, this); - if (*v != nullptr) - SG_REF(*v); } private: @@ -94,13 +90,13 @@ struct InputStreamAdapter // ignore } - Some m_stream; + std::shared_ptr m_stream; string m_buffer; error_condition m_status; }; template -CSGObject* object_reader(Reader& reader, BitseryReaderVisitor* visitor, CSGObject* _this = nullptr) +std::shared_ptr object_reader(Reader& reader, BitseryReaderVisitor* visitor, std::shared_ptr _this = nullptr) { size_t obj_magic; reader.value8b(obj_magic); @@ -111,7 +107,7 @@ CSGObject* object_reader(Reader& reader, BitseryReaderVisitor* visitor, reader.text1b(obj_name, 64); uint16_t primitive_type; reader.value2b(primitive_type); - CSGObject* obj = nullptr; + std::shared_ptr obj = nullptr; if (_this) { require(_this->get_name() == obj_name, ""); @@ -144,7 +140,6 @@ CSGObject* object_reader(Reader& reader, BitseryReaderVisitor* visitor, { io::warn("Error while deserializeing {}: ShogunException: " "{}", obj_name.c_str(), e.what()); - SG_UNREF(obj); return nullptr; } @@ -152,28 +147,28 @@ CSGObject* object_reader(Reader& reader, BitseryReaderVisitor* visitor, } using InputAdapter = AdapterReader; -using BitseryDeserializer = BasicDeserializer; +using BitseryDeser = BasicDeserializer; -CBitseryDeserializer::CBitseryDeserializer() : CDeserializer() +BitseryDeserializer::BitseryDeserializer() : Deserializer() { } -CBitseryDeserializer::~CBitseryDeserializer() +BitseryDeserializer::~BitseryDeserializer() { } -Some CBitseryDeserializer::read_object() +std::shared_ptr BitseryDeserializer::read_object() { InputStreamAdapter adapter { stream() }; - BitseryDeserializer deser {std::move(adapter)}; - BitseryReaderVisitor reader_visitor(deser); - return wrap(object_reader(deser, addressof(reader_visitor))); + BitseryDeser deser {std::move(adapter)}; + BitseryReaderVisitor reader_visitor(deser); + return object_reader(deser, addressof(reader_visitor)); } -void CBitseryDeserializer::read(CSGObject* _this) +void BitseryDeserializer::read(std::shared_ptr _this) { InputStreamAdapter adapter { stream() }; - BitseryDeserializer deser {std::move(adapter)}; - BitseryReaderVisitor reader_visitor(deser); + BitseryDeser deser {std::move(adapter)}; + BitseryReaderVisitor reader_visitor(deser); object_reader(deser, addressof(reader_visitor), _this); } diff --git a/src/shogun/io/serialization/BitseryDeserializer.h b/src/shogun/io/serialization/BitseryDeserializer.h index e430f8c1614..d36b6fce4f7 100644 --- a/src/shogun/io/serialization/BitseryDeserializer.h +++ b/src/shogun/io/serialization/BitseryDeserializer.h @@ -11,13 +11,13 @@ namespace shogun { namespace io { - class CBitseryDeserializer : public CDeserializer + class BitseryDeserializer : public Deserializer { public: - CBitseryDeserializer(); - ~CBitseryDeserializer() override; - Some read_object() override; - void read(CSGObject* _this) override; + BitseryDeserializer(); + ~BitseryDeserializer() override; + std::shared_ptr read_object() override; + void read(std::shared_ptr _this) override; const char* get_name() const override { diff --git a/src/shogun/io/serialization/BitserySerializer.cpp b/src/shogun/io/serialization/BitserySerializer.cpp index a4539c2229d..124106dc3d1 100644 --- a/src/shogun/io/serialization/BitserySerializer.cpp +++ b/src/shogun/io/serialization/BitserySerializer.cpp @@ -55,13 +55,12 @@ class BitseryWriterVisitor : public detail::BitseryVisitor* v) { if (*v) { SG_DEBUG("writing SGObject: {}", (*v)->get_name()); - SG_REF(*v); - write_object(writer, this, wrap(*v)); + write_object(writer, this, *v); } else { @@ -93,15 +92,15 @@ struct OutputStreamAdapter return written_bytes; } - Some m_stream; + std::shared_ptr m_stream; size_t written_bytes = 0; }; // cannot use context because of circular dependency :( template -void write_object(Writer& writer, BitseryWriterVisitor* visitor, Some o) noexcept(false) +void write_object(Writer& writer, BitseryWriterVisitor* visitor, std::shared_ptr o) noexcept(false) { - pre_serialize(o.get()); + pre_serialize(o); writer.value8b(sizeof(o.get())); string name(o->get_name()); writer.text1b(name, 64); @@ -120,24 +119,24 @@ void write_object(Writer& writer, BitseryWriterVisitor* visitor, Someget_value().visit(visitor); } - post_serialize(o.get()); + post_serialize(o); } using OutputAdapter = AdapterWriter; -using BitserySerializer = BasicSerializer; +using BitserySer = BasicSerializer; -CBitserySerializer::CBitserySerializer() : CSerializer() +BitserySerializer::BitserySerializer() : Serializer() { } -CBitserySerializer::~CBitserySerializer() +BitserySerializer::~BitserySerializer() { } -void CBitserySerializer::write(Some object) noexcept(false) +void BitserySerializer::write(std::shared_ptr object) noexcept(false) { OutputStreamAdapter adapter { stream() }; - BitserySerializer serializer {std::move(adapter)}; - BitseryWriterVisitor writer_visitor(serializer); + BitserySer serializer {std::move(adapter)}; + BitseryWriterVisitor writer_visitor(serializer); write_object(serializer, addressof(writer_visitor), object); } diff --git a/src/shogun/io/serialization/BitserySerializer.h b/src/shogun/io/serialization/BitserySerializer.h index 2f36ee35517..1bb8bf9d4c5 100644 --- a/src/shogun/io/serialization/BitserySerializer.h +++ b/src/shogun/io/serialization/BitserySerializer.h @@ -11,12 +11,12 @@ namespace shogun { namespace io { - class CBitserySerializer : public CSerializer + class BitserySerializer : public Serializer { public: - CBitserySerializer(); - ~CBitserySerializer() override; - virtual void write(Some object) noexcept(false); + BitserySerializer(); + ~BitserySerializer() override; + virtual void write(std::shared_ptr object) noexcept(false); virtual const char* get_name() const { diff --git a/src/shogun/io/serialization/BitseryVisitor.h b/src/shogun/io/serialization/BitseryVisitor.h index 674a241c13d..444d9c13bda 100644 --- a/src/shogun/io/serialization/BitseryVisitor.h +++ b/src/shogun/io/serialization/BitseryVisitor.h @@ -100,7 +100,7 @@ namespace shogun { } - void on(CSGObject** v) override + void on(std::shared_ptr* v) override { static_cast(this)->on_object(m_s, v); } diff --git a/src/shogun/io/serialization/Deserializer.cpp b/src/shogun/io/serialization/Deserializer.cpp index 595bb55c0fc..b42493662bc 100644 --- a/src/shogun/io/serialization/Deserializer.cpp +++ b/src/shogun/io/serialization/Deserializer.cpp @@ -12,26 +12,26 @@ using namespace shogun; using namespace shogun::io; -CDeserializer::CDeserializer() : CSGObject(), m_stream(empty()) +Deserializer::Deserializer() : SGObject() { } -CDeserializer::~CDeserializer() +Deserializer::~Deserializer() { } -void CDeserializer::attach(Some stream) +void Deserializer::attach(std::shared_ptr stream) { m_stream = stream; } -Some CDeserializer::stream() const +std::shared_ptr Deserializer::stream() const { require(m_stream, "Deserializer has no stream, attach() it to a stream"); return m_stream; } -void shogun::io::pre_deserialize(CSGObject* obj) noexcept(false) +void shogun::io::pre_deserialize(std::shared_ptr obj) noexcept(false) { obj->load_serializable_pre(); @@ -43,7 +43,7 @@ void shogun::io::pre_deserialize(CSGObject* obj) noexcept(false) } } -void shogun::io::post_deserialize(CSGObject* obj) noexcept(false) +void shogun::io::post_deserialize(std::shared_ptr obj) noexcept(false) { obj->load_serializable_post(); @@ -55,7 +55,7 @@ void shogun::io::post_deserialize(CSGObject* obj) noexcept(false) } } -CSGObject* shogun::io::deserialize(const std::string& _path, CDeserializer* _deser) +std::shared_ptr shogun::io::deserialize(const std::string& _path, std::shared_ptr _deser) { auto fs = env(); std::error_condition ec; @@ -66,7 +66,7 @@ CSGObject* shogun::io::deserialize(const std::string& _path, CDeserializer* _des if ((ec = fs->new_random_access_file(_path, &raf))) throw to_system_error(ec); - auto fis = some(raf.get()); + auto fis = std::make_shared(raf.get()); _deser->attach(fis); - return _deser->read_object().get(); + return _deser->read_object(); } diff --git a/src/shogun/io/serialization/Deserializer.h b/src/shogun/io/serialization/Deserializer.h index a654b914a26..da11743d51d 100644 --- a/src/shogun/io/serialization/Deserializer.h +++ b/src/shogun/io/serialization/Deserializer.h @@ -12,23 +12,23 @@ namespace shogun { namespace io { - class CDeserializer : public CSGObject + class Deserializer : public SGObject { public: - CDeserializer(); - virtual ~CDeserializer(); - virtual void attach(Some stream); - virtual Some read_object() = 0; - virtual void read(CSGObject* _this) = 0; - Some stream() const; + Deserializer(); + virtual ~Deserializer(); + virtual void attach(std::shared_ptr stream); + virtual std::shared_ptr read_object() = 0; + virtual void read(std::shared_ptr _this) = 0; + std::shared_ptr stream() const; private: - Some m_stream; + std::shared_ptr m_stream; }; - CSGObject* deserialize(const std::string& _path, CDeserializer* _deser); - void pre_deserialize(CSGObject* obj) noexcept(false); - void post_deserialize(CSGObject* obj) noexcept(false); + std::shared_ptr deserialize(const std::string& _path, std::shared_ptr _deser); + void pre_deserialize(std::shared_ptr obj) noexcept(false); + void post_deserialize(std::shared_ptr obj) noexcept(false); } } diff --git a/src/shogun/io/serialization/JsonDeserializer.cpp b/src/shogun/io/serialization/JsonDeserializer.cpp index 4aafa5872ab..39015f1861b 100644 --- a/src/shogun/io/serialization/JsonDeserializer.cpp +++ b/src/shogun/io/serialization/JsonDeserializer.cpp @@ -25,7 +25,7 @@ template class JSONReaderVisitor; template -CSGObject* object_reader(V& v, JSONReaderVisitor* visitor); +std::shared_ptr object_reader(V& v, JSONReaderVisitor* visitor); extern const char* const kNameKey; extern const char* const kGenericKey; @@ -130,14 +130,10 @@ class JSONReaderVisitor: public AnyVisitor *v = next_element(&ValueType::GetString); SG_DEBUG("reading std::string: {}", v->c_str()); } - void on(CSGObject** v) override + void on(std::shared_ptr* v) override { SG_DEBUG("reading SGObject: "); - if (*v != nullptr) - SG_UNREF(*v); *v = object_reader(m_value_stack.top(), this); - if (*v != nullptr) - SG_REF(*v); m_value_stack.pop(); } void enter_matrix(index_t* rows, index_t* cols) override @@ -238,12 +234,12 @@ class JSONReaderVisitor: public AnyVisitor SG_DELETE_COPY_AND_ASSIGN(JSONReaderVisitor); }; -class CIStreamAdapter +class IStreamAdapter { public: typedef char Ch; - CIStreamAdapter(Some is, size_t buffer_size = 65536): + IStreamAdapter(std::shared_ptr is, size_t buffer_size = 65536): m_stream(is), m_buffer_size(buffer_size) { @@ -252,7 +248,7 @@ class CIStreamAdapter } - ~CIStreamAdapter() + ~IStreamAdapter() { m_buffer.clear(); } @@ -303,17 +299,17 @@ class CIStreamAdapter } private: - Some m_stream; + std::shared_ptr m_stream; string m_buffer; size_t m_buffer_size; size_t m_pos = 0; size_t m_limit = 0; bool m_eof = false; - SG_DELETE_COPY_AND_ASSIGN(CIStreamAdapter); + SG_DELETE_COPY_AND_ASSIGN(IStreamAdapter); }; template -CSGObject* object_reader(const V* v, JSONReaderVisitor* visitor, CSGObject* _this = nullptr) +std::shared_ptr object_reader(const V* v, JSONReaderVisitor* visitor, std::shared_ptr _this = nullptr) { const V& value = *v; require(v != nullptr, "Value should be set!"); @@ -325,7 +321,7 @@ CSGObject* object_reader(const V* v, JSONReaderVisitor* visitor, CSGObject* _ string obj_name(value[kNameKey].GetString()); require(value.HasMember(kGenericKey), "Not a valid serialized SGObject, it does not have a 'generic'!"); EPrimitiveType primitive_type((EPrimitiveType) value[kGenericKey].GetInt()); - CSGObject* obj = nullptr; + std::shared_ptr obj = nullptr; if (_this) { require(_this->get_name() == obj_name, ""); @@ -357,38 +353,37 @@ CSGObject* object_reader(const V* v, JSONReaderVisitor* visitor, CSGObject* _ { io::warn("Error while deserializeing {}: ShogunException: " "{}", obj_name.c_str(), e.what()); - SG_UNREF(obj); return nullptr; } return obj; } -CJsonDeserializer::CJsonDeserializer() : CDeserializer() +JsonDeserializer::JsonDeserializer() : Deserializer() { } -CJsonDeserializer::~CJsonDeserializer() +JsonDeserializer::~JsonDeserializer() { } -Some CJsonDeserializer::read_object() +std::shared_ptr JsonDeserializer::read_object() { - CIStreamAdapter is(stream()); + IStreamAdapter is(stream()); // FIXME: use SAX parser interface! Document reader; reader.ParseStream(is); auto reader_visitor = make_unique>(); - return wrap(object_reader( + return object_reader( dynamic_cast(&reader), - reader_visitor.get()) + reader_visitor.get() ); } -void CJsonDeserializer::read(CSGObject* _this) +void JsonDeserializer::read(std::shared_ptr _this) { - CIStreamAdapter is(stream()); + IStreamAdapter is(stream()); // FIXME: use SAX parser interface! Document reader; reader.ParseStream(is); diff --git a/src/shogun/io/serialization/JsonDeserializer.h b/src/shogun/io/serialization/JsonDeserializer.h index 8cfce7ec329..6bb814f33a0 100644 --- a/src/shogun/io/serialization/JsonDeserializer.h +++ b/src/shogun/io/serialization/JsonDeserializer.h @@ -11,13 +11,13 @@ namespace shogun { namespace io { - class CJsonDeserializer : public CDeserializer + class JsonDeserializer : public Deserializer { public: - CJsonDeserializer(); - ~CJsonDeserializer() override; - Some read_object() override; - void read(CSGObject* _this) override; + JsonDeserializer(); + ~JsonDeserializer() override; + std::shared_ptr read_object() override; + void read(std::shared_ptr _this) override; virtual const char* get_name() const { return "JsonDeserializer"; diff --git a/src/shogun/io/serialization/JsonSerializer.cpp b/src/shogun/io/serialization/JsonSerializer.cpp index 9c757495b5e..d7df394ffab 100644 --- a/src/shogun/io/serialization/JsonSerializer.cpp +++ b/src/shogun/io/serialization/JsonSerializer.cpp @@ -25,7 +25,7 @@ const char* const kGenericKey = "generic"; extern const char* const kParametersKey; const char* const kParametersKey = "parameters"; -struct COutputStreamAdapter +struct OutputStreamAdapter { typedef char Ch; void Put(Ch c) @@ -39,10 +39,10 @@ struct COutputStreamAdapter m_stream->flush(); } - Some m_stream; + std::shared_ptr m_stream; }; -template void write_object(Writer& writer, Some object); +template void write_object(Writer& writer, std::shared_ptr object); template class JSONWriterVisitor : public AnyVisitor @@ -167,12 +167,12 @@ class JSONWriterVisitor : public AnyVisitor SG_DEBUG("writing std::string with value {}", v->c_str()); m_json_writer.String(v->c_str()); } - void on(CSGObject** v) override + void on(std::shared_ptr* v) override { if (*v) { SG_DEBUG("writing SGObject: {}", (*v)->get_name()); - write_object(m_json_writer, this, wrap(*v)); + write_object(m_json_writer, this, *v); } else { @@ -284,9 +284,9 @@ class JSONWriterVisitor : public AnyVisitor }; template -void write_object(Writer& writer, JSONWriterVisitor* visitor, Some object) noexcept(false) +void write_object(Writer& writer, JSONWriterVisitor* visitor, std::shared_ptr object) noexcept(false) { - pre_serialize(object.get()); + pre_serialize(object); visitor->start_object(); writer.StartObject(); @@ -310,22 +310,22 @@ void write_object(Writer& writer, JSONWriterVisitor* visitor, Someend_object(); - post_serialize(object.get()); + post_serialize(object); } -using JsonWriter = Writer, UTF8<>, CrtAllocator, kWriteNanAndInfFlag>; +using JsonWriter = Writer, UTF8<>, CrtAllocator, kWriteNanAndInfFlag>; -CJsonSerializer::CJsonSerializer() : CSerializer() +JsonSerializer::JsonSerializer() : Serializer() { } -CJsonSerializer::~CJsonSerializer() +JsonSerializer::~JsonSerializer() { } -void CJsonSerializer::write(Some object) noexcept(false) +void JsonSerializer::write(std::shared_ptr object) noexcept(false) { - COutputStreamAdapter adapter { stream() }; + OutputStreamAdapter adapter { stream() }; JsonWriter writer(adapter); auto writer_visitor = make_unique>(writer); diff --git a/src/shogun/io/serialization/JsonSerializer.h b/src/shogun/io/serialization/JsonSerializer.h index ea8a138cd77..c5632d569f0 100644 --- a/src/shogun/io/serialization/JsonSerializer.h +++ b/src/shogun/io/serialization/JsonSerializer.h @@ -11,12 +11,12 @@ namespace shogun { namespace io { - class CJsonSerializer : public CSerializer + class JsonSerializer : public Serializer { public: - CJsonSerializer(); - ~CJsonSerializer() override; - virtual void write(Some object) noexcept(false); + JsonSerializer(); + ~JsonSerializer() override; + virtual void write(std::shared_ptr object) noexcept(false); virtual const char* get_name() const { diff --git a/src/shogun/io/serialization/Serializer.cpp b/src/shogun/io/serialization/Serializer.cpp index d35dd568afb..eb2428bfca0 100644 --- a/src/shogun/io/serialization/Serializer.cpp +++ b/src/shogun/io/serialization/Serializer.cpp @@ -12,26 +12,26 @@ using namespace shogun; using namespace shogun::io; -CSerializer::CSerializer() : CSGObject(), m_stream(empty()) +Serializer::Serializer() : SGObject() { } -CSerializer::~CSerializer() +Serializer::~Serializer() { } -void CSerializer::attach(Some stream) +void Serializer::attach(std::shared_ptr stream) { m_stream = stream; } -Some CSerializer::stream() const +std::shared_ptr Serializer::stream() const { require(m_stream, "Serializer has no stream, attach() it to a stream"); return m_stream; } -void shogun::io::pre_serialize(CSGObject* obj) noexcept(false) +void shogun::io::pre_serialize(std::shared_ptr obj) noexcept(false) { obj->save_serializable_pre(); @@ -43,7 +43,7 @@ void shogun::io::pre_serialize(CSGObject* obj) noexcept(false) } } -void shogun::io::post_serialize(CSGObject* obj) noexcept(false) +void shogun::io::post_serialize(std::shared_ptr obj) noexcept(false) { obj->save_serializable_post(); @@ -55,7 +55,7 @@ void shogun::io::post_serialize(CSGObject* obj) noexcept(false) } } -void shogun::io::serialize(const std::string& _path, CSGObject* _obj, CSerializer* _serializer) +void shogun::io::serialize(const std::string& _path, std::shared_ptr _obj, std::shared_ptr _serializer) { auto fs = env(); std::error_condition ec; @@ -63,7 +63,7 @@ void shogun::io::serialize(const std::string& _path, CSGObject* _obj, CSerialize if ((ec = fs->new_writable_file(_path, &file))) throw to_system_error(ec); - auto fos = some(file.get()); + auto fos = std::make_shared(file.get()); _serializer->attach(fos); - _serializer->write(wrap(_obj)); -} \ No newline at end of file + _serializer->write(_obj); +} diff --git a/src/shogun/io/serialization/Serializer.h b/src/shogun/io/serialization/Serializer.h index e1be7856e9a..149bc30d5d4 100644 --- a/src/shogun/io/serialization/Serializer.h +++ b/src/shogun/io/serialization/Serializer.h @@ -12,22 +12,22 @@ namespace shogun { namespace io { - class CSerializer : public CSGObject + class Serializer : public SGObject { public: - CSerializer(); - virtual ~CSerializer(); - virtual void attach(Some stream); - virtual void write(Some object) noexcept(false) = 0; - Some stream() const; + Serializer(); + virtual ~Serializer(); + virtual void attach(std::shared_ptr stream); + virtual void write(std::shared_ptr object) noexcept(false) = 0; + std::shared_ptr stream() const; private: - Some m_stream; + std::shared_ptr m_stream; }; - void serialize(const std::string& _path, CSGObject* _obj, CSerializer* _serializer); - void pre_serialize(CSGObject* obj) noexcept(false); - void post_serialize(CSGObject* obj) noexcept(false); + void serialize(const std::string& _path, std::shared_ptr _obj, std::shared_ptr _serializer); + void pre_serialize(std::shared_ptr obj) noexcept(false); + void post_serialize(std::shared_ptr obj) noexcept(false); } } diff --git a/src/shogun/io/stream/BufferedInputStream.cpp b/src/shogun/io/stream/BufferedInputStream.cpp index 70c29fe8c27..0ee1e57f040 100644 --- a/src/shogun/io/stream/BufferedInputStream.cpp +++ b/src/shogun/io/stream/BufferedInputStream.cpp @@ -4,20 +4,20 @@ using namespace std; using namespace shogun::io; -CBufferedInputStream::CBufferedInputStream(CInputStream* is, size_t buffer_bytes): - CInputStream(), +BufferedInputStream::BufferedInputStream(InputStream* is, size_t buffer_bytes): + InputStream(), m_is(is), m_size(buffer_bytes) { m_buffer.reserve(m_size); } -CBufferedInputStream::~CBufferedInputStream() +BufferedInputStream::~BufferedInputStream() { } -error_condition CBufferedInputStream::read(string* buffer, int64_t size) +error_condition BufferedInputStream::read(string* buffer, int64_t size) { if (size < 0) return make_error_condition(errc::invalid_argument); @@ -55,7 +55,7 @@ error_condition CBufferedInputStream::read(string* buffer, int64_t size) return r; } -error_condition CBufferedInputStream::skip(int64_t bytes) +error_condition BufferedInputStream::skip(int64_t bytes) { if (bytes < 0) return make_error_condition(errc::invalid_argument); @@ -77,12 +77,12 @@ error_condition CBufferedInputStream::skip(int64_t bytes) return {}; } -int64_t CBufferedInputStream::tell() const +int64_t BufferedInputStream::tell() const { return m_is->tell() - (m_limit - m_pos); } -void CBufferedInputStream::reset() +void BufferedInputStream::reset() { m_is->reset(); m_pos = 0; @@ -91,7 +91,7 @@ void CBufferedInputStream::reset() } -error_condition CBufferedInputStream::read_line(std::string* result) +error_condition BufferedInputStream::read_line(std::string* result) { result->clear(); error_condition r; @@ -123,7 +123,7 @@ error_condition CBufferedInputStream::read_line(std::string* result) return r; } -error_condition CBufferedInputStream::read_all(std::string* result) +error_condition BufferedInputStream::read_all(std::string* result) { result->clear(); error_condition r; @@ -145,7 +145,7 @@ error_condition CBufferedInputStream::read_all(std::string* result) return r; } -error_condition CBufferedInputStream::fill() +error_condition BufferedInputStream::fill() { if (m_status) { diff --git a/src/shogun/io/stream/BufferedInputStream.h b/src/shogun/io/stream/BufferedInputStream.h index 4466ba47d5b..727d70a4007 100644 --- a/src/shogun/io/stream/BufferedInputStream.h +++ b/src/shogun/io/stream/BufferedInputStream.h @@ -14,7 +14,7 @@ namespace shogun namespace io { #define IGNORE_IN_CLASSLIST - IGNORE_IN_CLASSLIST class CBufferedInputStream : public CInputStream + IGNORE_IN_CLASSLIST class BufferedInputStream : public InputStream { public: /** @@ -23,9 +23,9 @@ namespace shogun * @param os * @param buffer_bytes */ - CBufferedInputStream(CInputStream* is, size_t buffer_bytes = 4096); + BufferedInputStream(InputStream* is, size_t buffer_bytes = 4096); - ~CBufferedInputStream() override; + ~BufferedInputStream() override; std::error_condition read(std::string* buffer, int64_t size) override; std::error_condition skip(int64_t bytes) override; @@ -44,14 +44,14 @@ namespace shogun private: std::error_condition fill(); private: - CInputStream* m_is; + InputStream* m_is; size_t m_size; std::string m_buffer; size_t m_pos = 0; size_t m_limit = 0; std::error_condition m_status; - SG_DELETE_COPY_AND_ASSIGN(CBufferedInputStream); + SG_DELETE_COPY_AND_ASSIGN(BufferedInputStream); }; } } diff --git a/src/shogun/io/stream/BufferedOutputStream.h b/src/shogun/io/stream/BufferedOutputStream.h index 22be908d2a9..23f3b691aa6 100644 --- a/src/shogun/io/stream/BufferedOutputStream.h +++ b/src/shogun/io/stream/BufferedOutputStream.h @@ -11,7 +11,7 @@ namespace shogun { - IGNORE_IN_CLASSLIST class CBufferedOutputStream : public COutputStream + IGNORE_IN_CLASSLIST class BufferedOutputStream : public OutputStream { public: /** @@ -20,25 +20,25 @@ namespace shogun * @param os * @param buffer_bytes */ - CBufferedOutputStream(std::shared_ptr os, index_t buffer_bytes = 4096): - COutputStream(), m_os(std::move(os)) + BufferedOutputStream(std::shared_ptr os, index_t buffer_bytes = 4096): + OutputStream(), m_os(std::move(os)) { } - CBufferedOutputStream(CBufferedOutputStream&& src): - COutputStream(), m_os(std::move(src.m_os)) + BufferedOutputStream(BufferedOutputStream&& src): + OutputStream(), m_os(std::move(src.m_os)) { src.m_os = nullptr; } - CBufferedOutputStream& operator=(CBufferedOutputStream&& src) + BufferedOutputStream& operator=(BufferedOutputStream&& src) { m_os = std::move(src.m_os); return *this; } - ~CBufferedOutputStream() override + ~BufferedOutputStream() override { m_os->flush(); m_os->close(); @@ -65,9 +65,9 @@ namespace shogun } private: - std::shared_ptr m_os; + std::shared_ptr m_os; - SG_DELETE_COPY_AND_ASSIGN(CBufferedOutputStream); + SG_DELETE_COPY_AND_ASSIGN(BufferedOutputStream); }; } diff --git a/src/shogun/io/stream/ByteArrayInputStream.h b/src/shogun/io/stream/ByteArrayInputStream.h index a76a1ed5c85..6399f64d077 100644 --- a/src/shogun/io/stream/ByteArrayInputStream.h +++ b/src/shogun/io/stream/ByteArrayInputStream.h @@ -17,16 +17,16 @@ namespace shogun { #define IGNORE_IN_CLASSLIST - IGNORE_IN_CLASSLIST class CByteArrayInputStream : public CInputStream + IGNORE_IN_CLASSLIST class ByteArrayInputStream : public InputStream { public: - CByteArrayInputStream(const char* _buffer, const size_t _length): - CInputStream(), m_buffer(_buffer, _length) {} + ByteArrayInputStream(const char* _buffer, const size_t _length): + InputStream(), m_buffer(_buffer, _length) {} - CByteArrayInputStream(const std::string& _buffer): - CByteArrayInputStream(_buffer.c_str(), _buffer.length()) {} + ByteArrayInputStream(const std::string& _buffer): + ByteArrayInputStream(_buffer.c_str(), _buffer.length()) {} - ~CByteArrayInputStream() override {} + ~ByteArrayInputStream() override {} std::error_condition read(std::string* buffer, int64_t size) override { @@ -73,7 +73,7 @@ namespace shogun const std::string_view m_buffer; size_t m_pos = 0; - SG_DELETE_COPY_AND_ASSIGN(CByteArrayInputStream); + SG_DELETE_COPY_AND_ASSIGN(ByteArrayInputStream); }; } } diff --git a/src/shogun/io/stream/ByteArrayOutputStream.h b/src/shogun/io/stream/ByteArrayOutputStream.h index cece2378bf2..e3f09d6e1d8 100644 --- a/src/shogun/io/stream/ByteArrayOutputStream.h +++ b/src/shogun/io/stream/ByteArrayOutputStream.h @@ -16,11 +16,11 @@ namespace shogun { #define IGNORE_IN_CLASSLIST - IGNORE_IN_CLASSLIST class CByteArrayOutputStream : public COutputStream + IGNORE_IN_CLASSLIST class ByteArrayOutputStream : public OutputStream { public: - CByteArrayOutputStream(): COutputStream() {} - ~CByteArrayOutputStream() override {} + ByteArrayOutputStream(): OutputStream() {} + ~ByteArrayOutputStream() override {} std::error_condition close() override { @@ -56,7 +56,7 @@ namespace shogun private: std::vector m_byte_array; - SG_DELETE_COPY_AND_ASSIGN(CByteArrayOutputStream); + SG_DELETE_COPY_AND_ASSIGN(ByteArrayOutputStream); }; } } diff --git a/src/shogun/io/stream/FileInputStream.cpp b/src/shogun/io/stream/FileInputStream.cpp index d8d07116665..69538cd9fb9 100644 --- a/src/shogun/io/stream/FileInputStream.cpp +++ b/src/shogun/io/stream/FileInputStream.cpp @@ -3,15 +3,15 @@ using namespace std; using namespace shogun::io; -CFileInputStream::CFileInputStream(RandomAccessFile* src, bool free): - CInputStream(), m_src(src), m_free(free), m_pos(0) {} -CFileInputStream::~CFileInputStream() +FileInputStream::FileInputStream(RandomAccessFile* src, bool free): + InputStream(), m_src(src), m_free(free), m_pos(0) {} +FileInputStream::~FileInputStream() { if (m_free) delete m_src; } -error_condition CFileInputStream::read(string* buffer, int64_t size) +error_condition FileInputStream::read(string* buffer, int64_t size) { if (size < 0) return make_error_condition(errc::invalid_argument); @@ -33,7 +33,7 @@ error_condition CFileInputStream::read(string* buffer, int64_t size) static constexpr int64_t kMaxSkipSize = 8 * 1024 * 1024; -error_condition CFileInputStream::skip(int64_t bytes) +error_condition FileInputStream::skip(int64_t bytes) { if (bytes < 0) return make_error_condition(errc::invalid_argument); @@ -69,12 +69,12 @@ error_condition CFileInputStream::skip(int64_t bytes) return {}; } -int64_t CFileInputStream::tell() const +int64_t FileInputStream::tell() const { return m_pos; } -void CFileInputStream::reset() +void FileInputStream::reset() { m_pos = 0; } diff --git a/src/shogun/io/stream/FileInputStream.h b/src/shogun/io/stream/FileInputStream.h index 458303d82f8..ed282d42df5 100644 --- a/src/shogun/io/stream/FileInputStream.h +++ b/src/shogun/io/stream/FileInputStream.h @@ -16,11 +16,11 @@ namespace shogun { #define IGNORE_IN_CLASSLIST - IGNORE_IN_CLASSLIST class CFileInputStream : public CInputStream + IGNORE_IN_CLASSLIST class FileInputStream : public InputStream { public: - CFileInputStream(RandomAccessFile* src, bool free = false); - ~CFileInputStream() override; + FileInputStream(RandomAccessFile* src, bool free = false); + ~FileInputStream() override; std::error_condition read(std::string* buffer, int64_t size) override; @@ -35,7 +35,7 @@ namespace shogun bool m_free; int64_t m_pos; - SG_DELETE_COPY_AND_ASSIGN(CFileInputStream); + SG_DELETE_COPY_AND_ASSIGN(FileInputStream); }; } } diff --git a/src/shogun/io/stream/FileOutputStream.h b/src/shogun/io/stream/FileOutputStream.h index 28375b91aa3..429247d60d6 100644 --- a/src/shogun/io/stream/FileOutputStream.h +++ b/src/shogun/io/stream/FileOutputStream.h @@ -16,12 +16,12 @@ namespace shogun { #define IGNORE_IN_CLASSLIST - IGNORE_IN_CLASSLIST class CFileOutputStream : public COutputStream + IGNORE_IN_CLASSLIST class FileOutputStream : public OutputStream { public: - CFileOutputStream(WritableFile* dest, bool free = false): - COutputStream(), m_dst(dest), m_free(free) {} - ~CFileOutputStream() override + FileOutputStream(WritableFile* dest, bool free = false): + OutputStream(), m_dst(dest), m_free(free) {} + ~FileOutputStream() override { if (m_free) delete m_dst; @@ -48,7 +48,7 @@ namespace shogun WritableFile* m_dst; bool m_free; - SG_DELETE_COPY_AND_ASSIGN(CFileOutputStream); + SG_DELETE_COPY_AND_ASSIGN(FileOutputStream); }; } } diff --git a/src/shogun/io/stream/InputStream.cpp b/src/shogun/io/stream/InputStream.cpp index 858525a63e8..5bb33c41b2f 100644 --- a/src/shogun/io/stream/InputStream.cpp +++ b/src/shogun/io/stream/InputStream.cpp @@ -7,10 +7,10 @@ using namespace shogun::io; -CInputStream::CInputStream() : CSGObject() +InputStream::InputStream() : SGObject() { } -CInputStream::~CInputStream() +InputStream::~InputStream() { } diff --git a/src/shogun/io/stream/InputStream.h b/src/shogun/io/stream/InputStream.h index eb4d974800c..4431dad84e5 100644 --- a/src/shogun/io/stream/InputStream.h +++ b/src/shogun/io/stream/InputStream.h @@ -13,11 +13,11 @@ namespace shogun { namespace io { - class CInputStream : public CSGObject + class InputStream : public SGObject { public: - CInputStream(); - virtual ~CInputStream(); + InputStream(); + virtual ~InputStream(); virtual std::error_condition read(std::string* buffer, int64_t size) = 0; virtual std::error_condition skip(int64_t bytes) = 0; diff --git a/src/shogun/io/stream/OutputStream.cpp b/src/shogun/io/stream/OutputStream.cpp index 1049d95e45d..b73da1de3f8 100644 --- a/src/shogun/io/stream/OutputStream.cpp +++ b/src/shogun/io/stream/OutputStream.cpp @@ -7,10 +7,10 @@ using namespace shogun::io; -COutputStream::COutputStream() : CSGObject() +OutputStream::OutputStream() : SGObject() { } -COutputStream::~COutputStream() +OutputStream::~OutputStream() { } diff --git a/src/shogun/io/stream/OutputStream.h b/src/shogun/io/stream/OutputStream.h index 9be1f992367..ff3df0a16a4 100644 --- a/src/shogun/io/stream/OutputStream.h +++ b/src/shogun/io/stream/OutputStream.h @@ -11,11 +11,11 @@ namespace shogun { namespace io { - class COutputStream : public CSGObject + class OutputStream : public SGObject { public: - COutputStream(); - virtual ~COutputStream(); + OutputStream(); + virtual ~OutputStream(); virtual std::error_condition close() = 0; virtual std::error_condition flush() = 0; diff --git a/src/shogun/io/streaming/InputParser.h b/src/shogun/io/streaming/InputParser.h index 18b5da27be1..dafad5c4c0d 100644 --- a/src/shogun/io/streaming/InputParser.h +++ b/src/shogun/io/streaming/InputParser.h @@ -31,7 +31,7 @@ namespace shogun E_UNLABELLED = 2 }; -/** @brief Class CInputParser is a templated class used to +/** @brief Class InputParser is a templated class used to * maintain the reading/parsing/providing of examples. * * Parsing is done in a thread separate from the learner. @@ -43,13 +43,13 @@ namespace shogun * through the set_read_vector* functions) * * The template type should be the type of feature vector the - * parser should return. Eg. CInputParser means it + * parser should return. Eg. InputParser means it * will expect a float32_t* vector to be returned from the get_vector * function. Other parameters returned are length of feature vector * and the label, if applicable. * * If the vectors cannot be directly represented as say float32_t* - * one can instantiate eg. CInputParser and it looks for + * one can instantiate eg. InputParser and it looks for * a get_vector function which returns a VwExample, which may contain * any kind of data, including label, vector, weights, etc. It is then * up to the external algorithm to handle such objects. @@ -57,13 +57,13 @@ namespace shogun * The parser should first be started with a call to the start_parser() * function which starts a new thread for continuous parsing of examples. * - * Parsing is done through the CParseBuffer object, which in its + * Parsing is done through the ParseBuffer object, which in its * current implementation is a ring of a specified number of examples. - * It is the task of the CInputParser object to ensure that this ring + * It is the task of the InputParser object to ensure that this ring * is being updated with new parsed examples. * - * CInputParser provides mainly the get_next_example function which - * returns the next example from the CParseBuffer object to the caller + * InputParser provides mainly the get_next_example function which + * returns the next example from the ParseBuffer object to the caller * (usually a StreamingFeatures object). When one is done using * the example, finalize_example() should be called, leaving the * spot free for a new example to be loaded. @@ -72,14 +72,14 @@ namespace shogun * exit_parser() may be used to cancel the parse thread if needed. * * Options are provided for automatic SG_FREEing of example objects - * after each finalize_example() and also on CInputParser destruction. + * after each finalize_example() and also on InputParser destruction. * They are set through the set_free_vector* functions. * Do not free vectors on finalize_example() if you intend to reuse the * same vector memory locations for different examples. * Do not free vectors on destruction if you are bound to free them * manually later. */ -template class CInputParser +template class InputParser { public: @@ -87,13 +87,13 @@ template class CInputParser * Constructor * */ - CInputParser(); + InputParser(); /** * Destructor * */ - ~CInputParser(); + ~InputParser(); /** * Initializer @@ -102,11 +102,11 @@ template class CInputParser * is_example_used is initialized to EMPTY. * example_type is LABELLED by default. * - * @param input_file CStreamingFile object + * @param input_file StreamingFile object * @param is_labelled Whether example is labelled or not (bool), optional * @param size Size of the buffer in number of examples */ - void init(CStreamingFile* input_file, bool is_labelled = true, int32_t size = PARSER_DEFAULT_BUFFSIZE); + void init(std::shared_ptr input_file, bool is_labelled = true, int32_t size = PARSER_DEFAULT_BUFFSIZE); /** * Test if parser is running. @@ -134,7 +134,7 @@ template class CInputParser * * The argument is a function pointer to that function. */ - void set_read_vector(void (CStreamingFile::*func_ptr)(T* &vec, int32_t &len)); + void set_read_vector(void (StreamingFile::*func_ptr)(T* &vec, int32_t &len)); /** * Sets the function used for reading a vector and @@ -147,7 +147,7 @@ template class CInputParser * * The argument is a function pointer to that function. */ - void set_read_vector_and_label(void (CStreamingFile::*func_ptr)(T* &vec, int32_t &len, float64_t &label)); + void set_read_vector_and_label(void (StreamingFile::*func_ptr)(T* &vec, int32_t &len, float64_t &label)); /** * Gets feature vector, length and label. @@ -301,7 +301,7 @@ template class CInputParser * * It is called while reading a vector. */ - void (CStreamingFile::*read_vector) (T* &vec, int32_t &len); + void (StreamingFile::*read_vector) (T* &vec, int32_t &len); /** * This is the function pointer to the function to @@ -309,16 +309,16 @@ template class CInputParser * * It is called while reading a vector and a label. */ - void (CStreamingFile::*read_vector_and_label) (T* &vec, int32_t &len, float64_t &label); + void (StreamingFile::*read_vector_and_label) (T* &vec, int32_t &len, float64_t &label); - /// Input source, CStreamingFile object - CStreamingFile* input_source; + /// Input source, StreamingFile object + std::shared_ptr input_source; /// Thread in which the parser runs std::thread parse_thread; /// The ring of examples, stored as they are parsed - CParseBuffer* examples_ring; + std::shared_ptr> examples_ring; /// Number of features in dataset (max of 'seen' features upto point of access) int32_t number_of_features; @@ -359,21 +359,21 @@ template class CInputParser }; template - void CInputParser::set_read_vector(void (CStreamingFile::*func_ptr)(T* &vec, int32_t &len)) + void InputParser::set_read_vector(void (StreamingFile::*func_ptr)(T* &vec, int32_t &len)) { // Set read_vector to point to the function passed as arg read_vector=func_ptr; } template - void CInputParser::set_read_vector_and_label(void (CStreamingFile::*func_ptr)(T* &vec, int32_t &len, float64_t &label)) + void InputParser::set_read_vector_and_label(void (StreamingFile::*func_ptr)(T* &vec, int32_t &len, float64_t &label)) { // Set read_vector_and_label to point to the function passed as arg read_vector_and_label=func_ptr; } template - CInputParser::CInputParser() + InputParser::InputParser() { examples_ring = nullptr; parsing_done=true; @@ -382,24 +382,16 @@ template } template - CInputParser::~CInputParser() + InputParser::~InputParser() { - SG_UNREF(examples_ring); } template - void CInputParser::init(CStreamingFile* input_file, bool is_labelled, int32_t size) + void InputParser::init(std::shared_ptr input_file, bool is_labelled, int32_t size) { input_source = input_file; - - if (is_labelled == true) - example_type = E_LABELLED; - else - example_type = E_UNLABELLED; - - SG_UNREF(examples_ring); - examples_ring = new CParseBuffer(size); - SG_REF(examples_ring); + example_type = is_labelled ? E_LABELLED : E_UNLABELLED; + examples_ring = std::make_shared>(size); parsing_done = false; reading_done = false; @@ -415,21 +407,21 @@ template } template - void CInputParser::set_free_vector_after_release(bool free_vec) + void InputParser::set_free_vector_after_release(bool free_vec) { free_after_release=free_vec; } template - void CInputParser::set_free_vectors_on_destruct(bool destroy) + void InputParser::set_free_vectors_on_destruct(bool destroy) { examples_ring->set_free_vectors_on_destruct(destroy); } template - void CInputParser::start_parser() + void InputParser::start_parser() { - SG_TRACE("entering CInputParser::start_parser()"); + SG_TRACE("entering InputParser::start_parser()"); if (is_running()) { error("Parser thread is already running! Multiple parse threads not supported."); @@ -441,21 +433,21 @@ template keep_running.store(true, std::memory_order_release); parse_thread = std::thread(&parse_loop_entry_point, this); - SG_TRACE("leaving CInputParser::start_parser()"); + SG_TRACE("leaving InputParser::start_parser()"); } template - void* CInputParser::parse_loop_entry_point(void* params) + void* InputParser::parse_loop_entry_point(void* params) { - ((CInputParser *) params)->main_parse_loop(params); + ((InputParser *) params)->main_parse_loop(params); return NULL; } template - bool CInputParser::is_running() + bool InputParser::is_running() { - SG_TRACE("entering CInputParser::is_running()"); + SG_TRACE("entering InputParser::is_running()"); bool ret; std::lock_guard lock(examples_state_lock); @@ -467,16 +459,16 @@ template else ret = false; - SG_TRACE("leaving CInputParser::is_running(), returning {}", ret); + SG_TRACE("leaving InputParser::is_running(), returning {}", ret); return ret; } template - int32_t CInputParser::get_vector_and_label(T* &feature_vector, + int32_t InputParser::get_vector_and_label(T* &feature_vector, int32_t &length, float64_t &label) { - (input_source->*read_vector_and_label)(feature_vector, length, label); + (input_source.get()->*read_vector_and_label)(feature_vector, length, label); if (length < 1) { @@ -488,10 +480,10 @@ template } template - int32_t CInputParser::get_vector_only(T* &feature_vector, + int32_t InputParser::get_vector_only(T* &feature_vector, int32_t &length) { - (input_source->*read_vector)(feature_vector, length); + (input_source.get()->*read_vector)(feature_vector, length); if (length < 1) { @@ -503,16 +495,16 @@ template } template - void CInputParser::copy_example_into_buffer(Example* ex) + void InputParser::copy_example_into_buffer(Example* ex) { examples_ring->copy_example(ex); } -template void* CInputParser::main_parse_loop(void* params) +template void* InputParser::main_parse_loop(void* params) { // Read the examples into current_* objects // Instead of allocating mem for new objects each time - CInputParser* this_obj = (CInputParser *) params; + InputParser* this_obj = (InputParser *) params; this->input_source = this_obj->input_source; while (keep_running.load(std::memory_order_acquire)) @@ -555,7 +547,7 @@ template void* CInputParser::main_parse_loop(void* params) return NULL; } -template Example* CInputParser::retrieve_example() +template Example* InputParser::retrieve_example() { /* This function should be guarded by mutexes while calling */ Example *ex; @@ -585,7 +577,7 @@ template Example* CInputParser::retrieve_example() return ex; } -template int32_t CInputParser::get_next_example(T* &fv, +template int32_t InputParser::get_next_example(T* &fv, int32_t &length, float64_t &label) { /* if reading is done, no more examples can be fetched. return 0 @@ -632,7 +624,7 @@ template int32_t CInputParser::get_next_example(T* &fv, } template - int32_t CInputParser::get_next_example(T* &fv, int32_t &length) + int32_t InputParser::get_next_example(T* &fv, int32_t &length) { float64_t label_dummy; @@ -640,21 +632,21 @@ template } template - void CInputParser::finalize_example() + void InputParser::finalize_example() { examples_ring->finalize_example(free_after_release); } -template void CInputParser::end_parser() +template void InputParser::end_parser() { - SG_TRACE("entering CInputParser::end_parser"); + SG_TRACE("entering InputParser::end_parser"); SG_TRACE("joining parse thread"); if (parse_thread.joinable()) parse_thread.join(); - SG_TRACE("leaving CInputParser::end_parser"); + SG_TRACE("leaving InputParser::end_parser"); } -template void CInputParser::exit_parser() +template void InputParser::exit_parser() { SG_TRACE("cancelling parse thread"); keep_running.store(false, std::memory_order_release); diff --git a/src/shogun/io/streaming/ParseBuffer.h b/src/shogun/io/streaming/ParseBuffer.h index 3db4e9a4377..ac5e4de4f09 100644 --- a/src/shogun/io/streaming/ParseBuffer.h +++ b/src/shogun/io/streaming/ParseBuffer.h @@ -36,7 +36,7 @@ enum E_IS_EXAMPLE_USED * label is a float64_t. * * Objects of this type are stored in the ring of - * class CParseBuffer. + * class ParseBuffer. */ template class Example @@ -49,12 +49,12 @@ class Example index_t length; }; -/** @brief Class CParseBuffer implements a ring of +/** @brief Class ParseBuffer implements a ring of * examples of a defined size. The ring stores * objects of the Example type. * * The feature vector and label are provided to this - * class from an external source. CParseBuffer is + * class from an external source. ParseBuffer is * only responsible for managing how these examples * are represented and stored in the memory. It is * also responsible for returning stored examples to be @@ -65,7 +65,7 @@ class Example * Writing of examples is done into whichever position * in the ring is free to be overwritten, or empty. */ -template class CParseBuffer: public CSGObject +template class ParseBuffer: public SGObject { public: /** @@ -73,13 +73,13 @@ template class CParseBuffer: public CSGObject * * @param size Ring size as number of examples */ - CParseBuffer(int32_t size = 1024); + ParseBuffer(int32_t size = 1024); /** * Destructor, frees up buffer. * */ - ~CParseBuffer(); + ~ParseBuffer(); /** * Return the next position to write the example @@ -223,7 +223,7 @@ template class CParseBuffer: public CSGObject }; -template void CParseBuffer::init_vector() +template void ParseBuffer::init_vector() { if (!free_vectors_on_destruct) return; @@ -234,7 +234,7 @@ template void CParseBuffer::init_vector() } } -template CParseBuffer::CParseBuffer(int32_t size) +template ParseBuffer::ParseBuffer(int32_t size) { ring_size = size; ex_ring = SG_CALLOC(Example, ring_size); @@ -260,7 +260,7 @@ template CParseBuffer::CParseBuffer(int32_t size) free_vectors_on_destruct = true; } -template CParseBuffer::~CParseBuffer() +template ParseBuffer::~ParseBuffer() { for (int32_t i=0; i CParseBuffer::~CParseBuffer() } template -int32_t CParseBuffer::write_example(Example *ex) +int32_t ParseBuffer::write_example(Example *ex) { ex_ring[ex_write_index].label = ex->label; ex_ring[ex_write_index].fv = ex->fv; @@ -293,7 +293,7 @@ int32_t CParseBuffer::write_example(Example *ex) } template -Example* CParseBuffer::return_example_to_read() +Example* ParseBuffer::return_example_to_read() { if (ex_read_index >= 0) return &ex_ring[ex_read_index]; @@ -302,7 +302,7 @@ Example* CParseBuffer::return_example_to_read() } template -Example* CParseBuffer::get_unused_example() +Example* ParseBuffer::get_unused_example() { std::lock_guard read_lk(*read_mutex); @@ -321,7 +321,7 @@ Example* CParseBuffer::get_unused_example() } template -int32_t CParseBuffer::copy_example(Example *ex) +int32_t ParseBuffer::copy_example(Example *ex) { std::lock_guard write_lk(*write_mutex); int32_t ret; @@ -339,7 +339,7 @@ int32_t CParseBuffer::copy_example(Example *ex) } template -void CParseBuffer::finalize_example(bool free_after_release) +void ParseBuffer::finalize_example(bool free_after_release) { std::lock_guard read_lk(*read_mutex); std::unique_lock current_ex_lock(*ex_in_use_mutex[ex_read_index]); diff --git a/src/shogun/io/streaming/StreamingAsciiFile.cpp b/src/shogun/io/streaming/StreamingAsciiFile.cpp index 810c0f04fbf..da3d6a77dda 100644 --- a/src/shogun/io/streaming/StreamingAsciiFile.cpp +++ b/src/shogun/io/streaming/StreamingAsciiFile.cpp @@ -14,27 +14,27 @@ using namespace shogun; -CStreamingAsciiFile::CStreamingAsciiFile() - : CStreamingFile() +StreamingAsciiFile::StreamingAsciiFile() + : StreamingFile() { unstable(SOURCE_LOCATION); m_delimiter = ' '; } -CStreamingAsciiFile::CStreamingAsciiFile(const char* fname, char rw) - : CStreamingFile(fname, rw) +StreamingAsciiFile::StreamingAsciiFile(const char* fname, char rw) + : StreamingFile(fname, rw) { m_delimiter = ' '; } -CStreamingAsciiFile::~CStreamingAsciiFile() +StreamingAsciiFile::~StreamingAsciiFile() { } /* Methods for reading dense vectors from an ascii file */ #define GET_VECTOR(fname, conv, sg_type) \ -void CStreamingAsciiFile::get_vector(sg_type*& vector, int32_t& num_feat) \ +void StreamingAsciiFile::get_vector(sg_type*& vector, int32_t& num_feat) \ { \ char* buffer = NULL; \ ssize_t bytes_read; \ @@ -118,7 +118,7 @@ GET_VECTOR(get_longreal_vector, atoi, floatmax_t) #undef GET_VECTOR #define GET_FLOAT_VECTOR(sg_type) \ - void CStreamingAsciiFile::get_vector(sg_type*& vector, int32_t& len)\ + void StreamingAsciiFile::get_vector(sg_type*& vector, int32_t& len)\ { \ char *line=NULL; \ SG_SET_LOCALE_C; \ @@ -157,7 +157,7 @@ GET_FLOAT_VECTOR(float64_t) /* Methods for reading a dense vector and a label from an ascii file */ #define GET_VECTOR_AND_LABEL(fname, conv, sg_type) \ - void CStreamingAsciiFile::get_vector_and_label(sg_type*& vector, int32_t& num_feat, float64_t& label) \ + void StreamingAsciiFile::get_vector_and_label(sg_type*& vector, int32_t& num_feat, float64_t& label) \ { \ char* buffer = NULL; \ ssize_t bytes_read; \ @@ -243,7 +243,7 @@ GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t) #undef GET_VECTOR_AND_LABEL #define GET_FLOAT_VECTOR_AND_LABEL(sg_type) \ - void CStreamingAsciiFile::get_vector_and_label(sg_type*& vector, int32_t& len, float64_t& label) \ + void StreamingAsciiFile::get_vector_and_label(sg_type*& vector, int32_t& len, float64_t& label) \ { \ char *line=NULL; \ SG_SET_LOCALE_C; \ @@ -284,7 +284,7 @@ GET_FLOAT_VECTOR_AND_LABEL(float64_t) /* Methods for reading a string vector from an ascii file (see StringFeatures) */ #define GET_STRING(fname, conv, sg_type) \ -void CStreamingAsciiFile::get_string(sg_type*& vector, int32_t& len) \ +void StreamingAsciiFile::get_string(sg_type*& vector, int32_t& len) \ { \ char* buffer = NULL; \ ssize_t bytes_read; \ @@ -331,7 +331,7 @@ GET_STRING(get_longreal_string, atoi, floatmax_t) /* Methods for reading a string vector and a label from an ascii file */ #define GET_STRING_AND_LABEL(fname, conv, sg_type) \ -void CStreamingAsciiFile::get_string_and_label(sg_type*& vector, int32_t& len, float64_t& label) \ +void StreamingAsciiFile::get_string_and_label(sg_type*& vector, int32_t& len, float64_t& label) \ { \ char* buffer = NULL; \ ssize_t bytes_read; \ @@ -398,7 +398,7 @@ GET_STRING_AND_LABEL(get_longreal_string_and_label, atoi, floatmax_t) /* Methods for reading a sparse vector from an ascii file */ #define GET_SPARSE_VECTOR(fname, conv, sg_type) \ -void CStreamingAsciiFile::get_sparse_vector(SGSparseVectorEntry*& vector, int32_t& len) \ +void StreamingAsciiFile::get_sparse_vector(SGSparseVectorEntry*& vector, int32_t& len) \ { \ char* buffer = NULL; \ ssize_t bytes_read; \ @@ -492,7 +492,7 @@ GET_SPARSE_VECTOR(get_longreal_sparse_vector, atoi, floatmax_t) /* Methods for reading a sparse vector and a label from an ascii file */ #define GET_SPARSE_VECTOR_AND_LABEL(fname, conv, sg_type) \ -void CStreamingAsciiFile::get_sparse_vector_and_label(SGSparseVectorEntry*& vector, int32_t& len, float64_t& label) \ +void StreamingAsciiFile::get_sparse_vector_and_label(SGSparseVectorEntry*& vector, int32_t& len, float64_t& label) \ { \ char* buffer = NULL; \ ssize_t bytes_read; \ @@ -606,7 +606,7 @@ GET_SPARSE_VECTOR_AND_LABEL(get_longreal_sparse_vector_and_label, atoi, floatmax #undef GET_SPARSE_VECTOR_AND_LABEL template -void CStreamingAsciiFile::append_item( +void StreamingAsciiFile::append_item( DynArray* items, char* ptr_data, char* ptr_item) { require(ptr_data && ptr_item, "Data and Item to append should not be NULL"); @@ -620,11 +620,11 @@ void CStreamingAsciiFile::append_item( items->append_element(item); } -void CStreamingAsciiFile::set_delimiter(char delimiter) +void StreamingAsciiFile::set_delimiter(char delimiter) { m_delimiter = delimiter; } -void CStreamingAsciiFile::tokenize(char delim, substring s, v_array& ret) +void StreamingAsciiFile::tokenize(char delim, substring s, v_array& ret) { ret.erase(); char *last = s.start; diff --git a/src/shogun/io/streaming/StreamingAsciiFile.h b/src/shogun/io/streaming/StreamingAsciiFile.h index f7245d244f0..cf6fa18e4ae 100644 --- a/src/shogun/io/streaming/StreamingAsciiFile.h +++ b/src/shogun/io/streaming/StreamingAsciiFile.h @@ -21,9 +21,9 @@ template class DynArray; /** @brief Class StreamingAsciiFile to read vector-by-vector from ASCII files. * - * The object must be initialized like a CCSVFile. + * The object must be initialized like a CSVFile. */ -class CStreamingAsciiFile: public CStreamingFile +class StreamingAsciiFile: public StreamingFile { public: @@ -31,7 +31,7 @@ class CStreamingAsciiFile: public CStreamingFile * Default constructor * */ - CStreamingAsciiFile(); + StreamingAsciiFile(); /** * Constructor taking file name argument @@ -39,12 +39,12 @@ class CStreamingAsciiFile: public CStreamingFile * @param fname file name * @param rw read/write mode */ - CStreamingAsciiFile(const char* fname, char rw='r'); + StreamingAsciiFile(const char* fname, char rw='r'); /** * Destructor */ - virtual ~CStreamingAsciiFile(); + virtual ~StreamingAsciiFile(); /** set delimiting character * diff --git a/src/shogun/io/streaming/StreamingFile.cpp b/src/shogun/io/streaming/StreamingFile.cpp index af1bb3e93e4..5acb0e4d518 100644 --- a/src/shogun/io/streaming/StreamingFile.cpp +++ b/src/shogun/io/streaming/StreamingFile.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Thoralf Klein, Soeren Sonnenburg, Saloni Nigam, Sergey Lisitsyn, + * Authors: Thoralf Klein, Soeren Sonnenburg, Saloni Nigam, Sergey Lisitsyn, * Viktor Gal */ @@ -27,7 +27,7 @@ namespace shogun /* For dense vectors */ #define GET_VECTOR(fname, conv, sg_type) \ - void CStreamingFile::get_vector \ + void StreamingFile::get_vector \ (sg_type*& vector, int32_t& num_feat) \ { \ vector=NULL; \ @@ -52,7 +52,7 @@ GET_VECTOR(get_longreal_vector, atoi, floatmax_t) /* For dense vectors with labels */ #define GET_VECTOR_AND_LABEL(fname, conv, sg_type) \ - void CStreamingFile::get_vector_and_label \ + void StreamingFile::get_vector_and_label \ (sg_type*& vector, int32_t& num_feat, float64_t& label) \ { \ vector=NULL; \ @@ -77,7 +77,7 @@ GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t) /* For string vectors */ #define GET_STRING(fname, conv, sg_type) \ - void CStreamingFile::get_string \ + void StreamingFile::get_string \ (sg_type*& vector, int32_t& num_feat) \ { \ vector=NULL; \ @@ -102,7 +102,7 @@ GET_STRING(get_longreal_string, atoi, floatmax_t) /* For string vectors with labels */ #define GET_STRING_AND_LABEL(fname, conv, sg_type) \ - void CStreamingFile::get_string_and_label \ + void StreamingFile::get_string_and_label \ (sg_type*& vector, int32_t& num_feat, float64_t& label) \ { \ vector=NULL; \ @@ -128,7 +128,7 @@ GET_STRING_AND_LABEL(get_longreal_string_and_label, atoi, floatmax_t) /* For sparse vectors */ #define GET_SPARSE_VECTOR(fname, conv, sg_type) \ \ - void CStreamingFile::get_sparse_vector \ + void StreamingFile::get_sparse_vector \ (SGSparseVectorEntry*& vector, int32_t& num_feat) \ { \ vector=NULL; \ @@ -154,7 +154,7 @@ GET_SPARSE_VECTOR(get_longreal_sparse_vector, atoi, floatmax_t) /* For sparse vectors with labels */ #define GET_SPARSE_VECTOR_AND_LABEL(fname, conv, sg_type) \ \ - void CStreamingFile::get_sparse_vector_and_label \ + void StreamingFile::get_sparse_vector_and_label \ (SGSparseVectorEntry*& vector, \ int32_t& num_feat, \ float64_t& label) \ @@ -178,18 +178,18 @@ GET_SPARSE_VECTOR_AND_LABEL(get_long_sparse_vector_and_label, atoi, int64_t) GET_SPARSE_VECTOR_AND_LABEL(get_ulong_sparse_vector_and_label, atoi, uint64_t) GET_SPARSE_VECTOR_AND_LABEL(get_longreal_sparse_vector_and_label, atoi, floatmax_t) #undef GET_SPARSE_VECTOR_AND_LABEL - + } using namespace shogun; -CStreamingFile::CStreamingFile() : CSGObject() +StreamingFile::StreamingFile() : SGObject() { buf=NULL; filename=NULL; } -CStreamingFile::CStreamingFile(const char* fname, char rw) : CSGObject() +StreamingFile::StreamingFile(const char* fname, char rw) : SGObject() { task=rw; filename=get_strdup(fname); @@ -213,15 +213,13 @@ CStreamingFile::CStreamingFile(const char* fname, char rw) : CSGObject() if (file < 0) error("Error opening file '{}'", filename); - buf = new CIOBuffer(file); - SG_REF(buf); + buf = std::make_shared(file); } else error("Error getting the file name!"); } -CStreamingFile::~CStreamingFile() +StreamingFile::~StreamingFile() { SG_FREE(filename); - SG_UNREF(buf); } diff --git a/src/shogun/io/streaming/StreamingFile.h b/src/shogun/io/streaming/StreamingFile.h index c8ac7b6c935..2d847a4b6cd 100644 --- a/src/shogun/io/streaming/StreamingFile.h +++ b/src/shogun/io/streaming/StreamingFile.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Thoralf Klein, Soeren Sonnenburg, Heiko Strathmann, Saloni Nigam, + * Authors: Thoralf Klein, Soeren Sonnenburg, Heiko Strathmann, Saloni Nigam, * Sergey Lisitsyn */ #ifndef __STREAMING_FILE_H__ @@ -28,21 +28,21 @@ template struct SGSparseVectorEntry; * - Strings are written out as one string per line * */ - class CStreamingFile: public CSGObject + class StreamingFile: public SGObject { public: /** default constructor */ - CStreamingFile(); + StreamingFile(); /** constructor * * @param fname filename to open * @param rw mode, 'r' or 'w' */ - CStreamingFile(const char* fname, char rw='r'); + StreamingFile(const char* fname, char rw='r'); /** default destructor */ - virtual ~CStreamingFile(); + virtual ~StreamingFile(); #ifndef SWIG // SWIG should skip this /** @@ -271,7 +271,7 @@ template struct SGSparseVectorEntry; protected: /// Buffer to hold stuff in memory - CIOBuffer* buf; + std::shared_ptr buf; /// Task char task; /// Name of the handled file diff --git a/src/shogun/io/streaming/StreamingFileFromDenseFeatures.h b/src/shogun/io/streaming/StreamingFileFromDenseFeatures.h index 7ebe24c7ee4..510b7a0377e 100644 --- a/src/shogun/io/streaming/StreamingFileFromDenseFeatures.h +++ b/src/shogun/io/streaming/StreamingFileFromDenseFeatures.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Yuyu Zhang, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Yuyu Zhang, * Evangelos Anagnostopoulos, Sergey Lisitsyn */ #ifndef __STREAMING_FILEFROMDENSE_H__ @@ -14,23 +14,23 @@ namespace shogun { -/** @brief Class CStreamingFileFromDenseFeatures is a derived +/** @brief Class StreamingFileFromDenseFeatures is a derived * class of CStreamingFile which creates an input source - * for the online framework from a CDenseFeatures object. + * for the online framework from a DenseFeatures object. * * This kind of input is seekable, and hence can be used for * making multiple passes over data. * * It is useful for testing/comparison purposes. */ -template class CStreamingFileFromDenseFeatures: - public CStreamingFileFromFeatures +template class StreamingFileFromDenseFeatures: + public StreamingFileFromFeatures { public: /** * Default constructor */ - CStreamingFileFromDenseFeatures(); + StreamingFileFromDenseFeatures(); /** * Constructor taking a DenseFeatures object as arg @@ -38,13 +38,13 @@ template class CStreamingFileFromDenseFeatures: * @param feat DenseFeatures object * @param lab Labels as float64_t*, optional */ - CStreamingFileFromDenseFeatures(CDenseFeatures* feat, + StreamingFileFromDenseFeatures(std::shared_ptr> feat, float64_t* lab=NULL); /** * Destructor */ - virtual ~CStreamingFileFromDenseFeatures(); + virtual ~StreamingFileFromDenseFeatures(); /** * This function will be called for reading vectors from the @@ -93,7 +93,7 @@ template class CStreamingFileFromDenseFeatures: protected: /// DenseFeatures object - CDenseFeatures* features; + std::shared_ptr> features; /// Index of vector to be returned from the feature matrix int32_t vector_num; @@ -101,36 +101,30 @@ template class CStreamingFileFromDenseFeatures: }; template -CStreamingFileFromDenseFeatures::CStreamingFileFromDenseFeatures() : - CStreamingFileFromFeatures() +StreamingFileFromDenseFeatures::StreamingFileFromDenseFeatures() : + StreamingFileFromFeatures() { init(); } template -CStreamingFileFromDenseFeatures::CStreamingFileFromDenseFeatures( - CDenseFeatures* feat, float64_t* lab) : - CStreamingFileFromFeatures() +StreamingFileFromDenseFeatures::StreamingFileFromDenseFeatures( + std::shared_ptr> feat, float64_t* lab) : + StreamingFileFromDenseFeatures() { - init(); - - require(feat,"{}::CStreamingFileFromDenseFeatures() features required!", + require(feat,"{}::StreamingFileFromDenseFeatures() features required!", get_name()); features=feat; - SG_REF(feat); - labels=lab; - } template -CStreamingFileFromDenseFeatures::~CStreamingFileFromDenseFeatures() +StreamingFileFromDenseFeatures::~StreamingFileFromDenseFeatures() { - SG_UNREF(features); } template -void CStreamingFileFromDenseFeatures::init() +void StreamingFileFromDenseFeatures::init() { vector_num=0; features=NULL; @@ -142,7 +136,7 @@ void CStreamingFileFromDenseFeatures::init() * If the class is of type T, specialize this function to work for * vectors of that type. */ template -void CStreamingFileFromDenseFeatures::get_vector(T*& vector, +void StreamingFileFromDenseFeatures::get_vector(T*& vector, int32_t& num_feat) { if (vector_num>=features->get_num_vectors()) @@ -162,7 +156,7 @@ void CStreamingFileFromDenseFeatures::get_vector(T*& vector, /* Functions to return the vector from the DenseFeatures object with label */ template -void CStreamingFileFromDenseFeatures::get_vector_and_label(T*& vector, +void StreamingFileFromDenseFeatures::get_vector_and_label(T*& vector, int32_t& num_feat, float64_t& label) { if (vector_num>=features->get_num_vectors()) diff --git a/src/shogun/io/streaming/StreamingFileFromFeatures.cpp b/src/shogun/io/streaming/StreamingFileFromFeatures.cpp index 71b76d6509b..8860b202614 100644 --- a/src/shogun/io/streaming/StreamingFileFromFeatures.cpp +++ b/src/shogun/io/streaming/StreamingFileFromFeatures.cpp @@ -8,28 +8,28 @@ using namespace shogun; -CStreamingFileFromFeatures::CStreamingFileFromFeatures() - : CStreamingFile() +StreamingFileFromFeatures::StreamingFileFromFeatures() + : StreamingFile() { features=NULL; labels=NULL; } -CStreamingFileFromFeatures::CStreamingFileFromFeatures(CFeatures* feat) - : CStreamingFile() +StreamingFileFromFeatures::StreamingFileFromFeatures(std::shared_ptr feat) + : StreamingFile() { features=feat; labels=NULL; } -CStreamingFileFromFeatures::CStreamingFileFromFeatures(CFeatures* feat, float64_t* lab) - : CStreamingFile() +StreamingFileFromFeatures::StreamingFileFromFeatures(std::shared_ptr feat, float64_t* lab) + : StreamingFile() { features=feat; labels=lab; } -CStreamingFileFromFeatures::~CStreamingFileFromFeatures() +StreamingFileFromFeatures::~StreamingFileFromFeatures() { features=NULL; labels=NULL; diff --git a/src/shogun/io/streaming/StreamingFileFromFeatures.h b/src/shogun/io/streaming/StreamingFileFromFeatures.h index 33b85e3bdaf..f5fcfd0e6bc 100644 --- a/src/shogun/io/streaming/StreamingFileFromFeatures.h +++ b/src/shogun/io/streaming/StreamingFileFromFeatures.h @@ -13,17 +13,17 @@ namespace shogun { -class CFeatures; +class Features; /** @brief Class StreamingFileFromFeatures to read vector-by-vector - * from a CFeatures object. + * from a Features object. * - * The object must be initialized with another CFeatures object. + * The object must be initialized with another Features object. * It is upto the derived class to implement specialized functions * to return the vector. * * Only a subset of the functions defined in StreamingFile.h need - * to be implemented, as appropriate for the CFeatures object + * to be implemented, as appropriate for the Features object * which the class works with. * * For example, a derived class based on DenseFeatures should only @@ -31,40 +31,40 @@ class CFeatures; * StringFeatures should only implement the get_(type)*_string() * functions. */ -class CStreamingFileFromFeatures: public CStreamingFile +class StreamingFileFromFeatures: public StreamingFile { public: /** * Default constructor */ - CStreamingFileFromFeatures(); + StreamingFileFromFeatures(); /** - * Constructor taking a CFeatures object as argument + * Constructor taking a Features object as argument * * @param feat features object */ - CStreamingFileFromFeatures(CFeatures* feat); + StreamingFileFromFeatures(std::shared_ptr feat); /** - * Constructor taking a CFeatures object and labels as arguments + * Constructor taking a Features object and labels as arguments * * @param feat features object * @param lab labels as float64_t* */ - CStreamingFileFromFeatures(CFeatures* feat, float64_t* lab); + StreamingFileFromFeatures(std::shared_ptr feat, float64_t* lab); /** * Destructor */ - virtual ~CStreamingFileFromFeatures(); + virtual ~StreamingFileFromFeatures(); /** * Set the features object * * @param feat features object as argument */ - virtual void set_features(CFeatures* feat) + virtual void set_features(std::shared_ptr feat) { ASSERT(feat) features=feat; @@ -92,7 +92,7 @@ class CStreamingFileFromFeatures: public CStreamingFile protected: /// Features object - CFeatures* features; + std::shared_ptr features; /// Labels (if applicable) float64_t* labels; diff --git a/src/shogun/io/streaming/StreamingFileFromSparseFeatures.h b/src/shogun/io/streaming/StreamingFileFromSparseFeatures.h index ba76feb50d4..bc162cff3ff 100644 --- a/src/shogun/io/streaming/StreamingFileFromSparseFeatures.h +++ b/src/shogun/io/streaming/StreamingFileFromSparseFeatures.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Yuyu Zhang, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Yuyu Zhang, * Evangelos Anagnostopoulos, Sergey Lisitsyn */ #ifndef __STREAMING_FILEFROMSPARSE_H__ @@ -14,24 +14,24 @@ namespace shogun { -/** @brief Class CStreamingFileFromSparseFeatures is derived from CStreamingFile +/** @brief Class StreamingFileFromSparseFeatures is derived from CStreamingFile * and provides an input source for the online framework. It uses an existing - * CSparseFeatures object to generate online examples. + * SparseFeatures object to generate online examples. */ -template class CStreamingFileFromSparseFeatures: public CStreamingFileFromFeatures +template class StreamingFileFromSparseFeatures: public StreamingFileFromFeatures { public: /** * Default constructor */ - CStreamingFileFromSparseFeatures(); + StreamingFileFromSparseFeatures(); /** * Constructor taking a SparseFeatures object as arg * * @param feat SparseFeatures object */ - CStreamingFileFromSparseFeatures(CSparseFeatures* feat); + StreamingFileFromSparseFeatures(std::shared_ptr> feat); /** * Constructor taking a SparseFeatures object as arg @@ -39,12 +39,12 @@ template class CStreamingFileFromSparseFeatures: public CStreamingFile * @param feat SparseFeatures object * @param lab Labels as float64_t* */ - CStreamingFileFromSparseFeatures(CSparseFeatures* feat, float64_t* lab); + StreamingFileFromSparseFeatures(std::shared_ptr> feat, float64_t* lab); /** * Destructor */ - virtual ~CStreamingFileFromSparseFeatures(); + virtual ~StreamingFileFromSparseFeatures(); /** * This function will be called for reading vectors from the @@ -88,11 +88,11 @@ template class CStreamingFileFromSparseFeatures: public CStreamingFile /** * Initialize members to defaults */ - void init(CSparseFeatures* feat); + void init(std::shared_ptr> feat); protected: /// SparseFeatures object - CSparseFeatures* features; + std::shared_ptr> features; /// Index of vector to be returned from the feature matrix int32_t vector_num; @@ -100,37 +100,35 @@ template class CStreamingFileFromSparseFeatures: public CStreamingFile }; template -CStreamingFileFromSparseFeatures::CStreamingFileFromSparseFeatures() - : CStreamingFileFromFeatures() +StreamingFileFromSparseFeatures::StreamingFileFromSparseFeatures() + : StreamingFileFromFeatures() { init(NULL); } template -CStreamingFileFromSparseFeatures::CStreamingFileFromSparseFeatures(CSparseFeatures* feat) - : CStreamingFileFromFeatures(feat) +StreamingFileFromSparseFeatures::StreamingFileFromSparseFeatures(std::shared_ptr> feat) + : StreamingFileFromFeatures(feat) { init(feat); } template -CStreamingFileFromSparseFeatures::CStreamingFileFromSparseFeatures(CSparseFeatures* feat, float64_t* lab) - : CStreamingFileFromFeatures(feat,lab) +StreamingFileFromSparseFeatures::StreamingFileFromSparseFeatures(std::shared_ptr> feat, float64_t* lab) + : StreamingFileFromFeatures(feat,lab) { init(feat); } template -CStreamingFileFromSparseFeatures::~CStreamingFileFromSparseFeatures() +StreamingFileFromSparseFeatures::~StreamingFileFromSparseFeatures() { - SG_UNREF(features); } template -void CStreamingFileFromSparseFeatures::init(CSparseFeatures* feat) +void StreamingFileFromSparseFeatures::init(std::shared_ptr> feat) { features = feat; - SG_REF(features); vector_num=0; set_generic(); @@ -138,7 +136,7 @@ void CStreamingFileFromSparseFeatures::init(CSparseFeatures* feat) /* Functions to return the vector from the SparseFeatures object */ template -void CStreamingFileFromSparseFeatures::get_sparse_vector +void StreamingFileFromSparseFeatures::get_sparse_vector (SGSparseVectorEntry*& vector, int32_t& len) { if (vector_num >= features->get_num_vectors()) @@ -160,7 +158,7 @@ void CStreamingFileFromSparseFeatures::get_sparse_vector /* Functions to return the vector from the SparseFeatures object */ template -void CStreamingFileFromSparseFeatures::get_sparse_vector_and_label +void StreamingFileFromSparseFeatures::get_sparse_vector_and_label (SGSparseVectorEntry*& vector, int32_t& len, float64_t& label) { get_sparse_vector(vector, len); diff --git a/src/shogun/io/streaming/StreamingFileFromStringFeatures.h b/src/shogun/io/streaming/StreamingFileFromStringFeatures.h index 0719ef394ff..fa4f9908c24 100644 --- a/src/shogun/io/streaming/StreamingFileFromStringFeatures.h +++ b/src/shogun/io/streaming/StreamingFileFromStringFeatures.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Yuyu Zhang, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Yuyu Zhang, * Evangelos Anagnostopoulos, Sergey Lisitsyn */ #ifndef __STREAMING_FILEFROMSTRING_H__ @@ -14,24 +14,24 @@ namespace shogun { -/** @brief Class CStreamingFileFromStringFeatures is derived from +/** @brief Class StreamingFileFromStringFeatures is derived from * CStreamingFile and provides an input source for the online * framework from a CStringFeatures object. */ -template class CStreamingFileFromStringFeatures: public CStreamingFileFromFeatures +template class StreamingFileFromStringFeatures: public StreamingFileFromFeatures { public: /** * Default constructor */ - CStreamingFileFromStringFeatures(); + StreamingFileFromStringFeatures(); /** * Constructor taking a StringFeatures object as arg * * @param feat StringFeatures object */ - CStreamingFileFromStringFeatures(CStringFeatures* feat); + StreamingFileFromStringFeatures(std::shared_ptr> feat); /** * Constructor taking a StringFeatures object as arg @@ -39,12 +39,12 @@ template class CStreamingFileFromStringFeatures: public CStreamingFile * @param feat StringFeatures object * @param lab Labels as float64_t* */ - CStreamingFileFromStringFeatures(CStringFeatures* feat, float64_t* lab); + StreamingFileFromStringFeatures(std::shared_ptr> feat, float64_t* lab); /** * Destructor */ - virtual ~CStreamingFileFromStringFeatures(); + virtual ~StreamingFileFromStringFeatures(); /** * This function will be called for reading strings from the @@ -88,12 +88,12 @@ template class CStreamingFileFromStringFeatures: public CStreamingFile /** * Initialize members to defaults */ - void init(CStringFeatures* feat=NULL); + void init(std::shared_ptr> feat=NULL); protected: /// StringFeatures object - CStringFeatures* features; + std::shared_ptr> features; /// Index of vector to be returned from the feature matrix int32_t vector_num; @@ -101,33 +101,33 @@ template class CStreamingFileFromStringFeatures: public CStreamingFile }; template -CStreamingFileFromStringFeatures::CStreamingFileFromStringFeatures() - : CStreamingFileFromFeatures() +StreamingFileFromStringFeatures::StreamingFileFromStringFeatures() + : StreamingFileFromFeatures() { init(); } template -CStreamingFileFromStringFeatures::CStreamingFileFromStringFeatures(CStringFeatures* feat) - : CStreamingFileFromFeatures(feat) +StreamingFileFromStringFeatures::StreamingFileFromStringFeatures(std::shared_ptr> feat) + : StreamingFileFromFeatures(feat) { init(feat); } template -CStreamingFileFromStringFeatures::CStreamingFileFromStringFeatures(CStringFeatures* feat, float64_t* lab) - : CStreamingFileFromFeatures(feat,lab) +StreamingFileFromStringFeatures::StreamingFileFromStringFeatures(std::shared_ptr> feat, float64_t* lab) + : StreamingFileFromFeatures(feat,lab) { init(feat); } template -CStreamingFileFromStringFeatures::~CStreamingFileFromStringFeatures() +StreamingFileFromStringFeatures::~StreamingFileFromStringFeatures() { } template -void CStreamingFileFromStringFeatures::init(CStringFeatures* feat) +void StreamingFileFromStringFeatures::init(std::shared_ptr> feat) { vector_num=0; features = feat; @@ -137,7 +137,7 @@ void CStreamingFileFromStringFeatures::init(CStringFeatures* feat) /* Functions to return the vector from the StringFeatures object */ template -void CStreamingFileFromStringFeatures::get_string(T*& vector, int32_t& num_feat) +void StreamingFileFromStringFeatures::get_string(T*& vector, int32_t& num_feat) { if (vector_num >= features->get_num_vectors()) { @@ -158,7 +158,7 @@ void CStreamingFileFromStringFeatures::get_string(T*& vector, int32_t& num_fe /* Functions to return the vector from the StringFeatures object with label */ template -void CStreamingFileFromStringFeatures::get_string_and_label +void StreamingFileFromStringFeatures::get_string_and_label (T*& vector, int32_t& num_feat, float64_t& label) { if (vector_num >= features->get_num_vectors()) diff --git a/src/shogun/io/visitors/ToStringVisitor.cpp b/src/shogun/io/visitors/ToStringVisitor.cpp index 5525b81e9d8..1c6e992ba9a 100644 --- a/src/shogun/io/visitors/ToStringVisitor.cpp +++ b/src/shogun/io/visitors/ToStringVisitor.cpp @@ -33,7 +33,7 @@ void ToStringVisitor::on(std::string *v) { stream() << *v << m_buffer; } -void ToStringVisitor::on(CSGObject **v) { +void ToStringVisitor::on(std::shared_ptr* v) { if (*v) { stream() << (*v)->get_name() << "(...)" << m_buffer; } else { diff --git a/src/shogun/io/visitors/ToStringVisitor.h b/src/shogun/io/visitors/ToStringVisitor.h index 80b98c64dc9..00c8ee4d07f 100644 --- a/src/shogun/io/visitors/ToStringVisitor.h +++ b/src/shogun/io/visitors/ToStringVisitor.h @@ -43,7 +43,7 @@ namespace shogun { virtual void on(std::string *v); - virtual void on(CSGObject **v); + virtual void on(std::shared_ptr* v); virtual void on(char *string); diff --git a/src/shogun/kernel/ANOVAKernel.cpp b/src/shogun/kernel/ANOVAKernel.cpp index a779e19fad6..1cb48223f39 100644 --- a/src/shogun/kernel/ANOVAKernel.cpp +++ b/src/shogun/kernel/ANOVAKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evan Shelhamer, Heiko Strathmann, Saurabh Goyal, + * Authors: Soeren Sonnenburg, Evan Shelhamer, Heiko Strathmann, Saurabh Goyal, * Sergey Lisitsyn */ @@ -11,90 +11,90 @@ using namespace shogun; -CANOVAKernel::CANOVAKernel(): CDotKernel(0), cardinality(1.0) +ANOVAKernel::ANOVAKernel(): DotKernel(0), cardinality(1.0) { register_params(); } -CANOVAKernel::CANOVAKernel(int32_t cache, int32_t d) -: CDotKernel(cache), cardinality(d) +ANOVAKernel::ANOVAKernel(int32_t cache, int32_t d) +: DotKernel(cache), cardinality(d) { register_params(); } -CANOVAKernel::CANOVAKernel( - CDenseFeatures* l, CDenseFeatures* r, int32_t d, int32_t cache) - : CDotKernel(cache), cardinality(d) +ANOVAKernel::ANOVAKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d, int32_t cache) + : DotKernel(cache), cardinality(d) { register_params(); init(l, r); } -CANOVAKernel::~CANOVAKernel() +ANOVAKernel::~ANOVAKernel() { cleanup(); } -bool CANOVAKernel::init(CFeatures* l, CFeatures* r) +bool ANOVAKernel::init(std::shared_ptr l, std::shared_ptr r) { cleanup(); - bool result = CDotKernel::init(l,r); + bool result = DotKernel::init(l,r); init_normalizer(); return result; } -float64_t CANOVAKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t ANOVAKernel::compute(int32_t idx_a, int32_t idx_b) { return compute_rec1(idx_a, idx_b); } -float64_t CANOVAKernel::compute_rec1(int32_t idx_a, int32_t idx_b) +float64_t ANOVAKernel::compute_rec1(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result = compute_recursive1(avec, bvec, alen); - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } -float64_t CANOVAKernel::compute_rec2(int32_t idx_a, int32_t idx_b) +float64_t ANOVAKernel::compute_rec2(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result = compute_recursive2(avec, bvec, alen); - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } -void CANOVAKernel::register_params() +void ANOVAKernel::register_params() { SG_ADD(&cardinality, "cardinality", "Kernel cardinality.", ParameterProperties::HYPER); } -float64_t CANOVAKernel::compute_recursive1(float64_t* avec, float64_t* bvec, int32_t len) +float64_t ANOVAKernel::compute_recursive1(float64_t* avec, float64_t* bvec, int32_t len) { int32_t DP_len=(cardinality+1)*(len+1); float64_t* DP = SG_MALLOC(float64_t, DP_len); @@ -126,7 +126,7 @@ float64_t CANOVAKernel::compute_recursive1(float64_t* avec, float64_t* bvec, int return result; } -float64_t CANOVAKernel::compute_recursive2(float64_t* avec, float64_t* bvec, int32_t len) +float64_t ANOVAKernel::compute_recursive2(float64_t* avec, float64_t* bvec, int32_t len) { float64_t* KD = SG_MALLOC(float64_t, cardinality+1); float64_t* KS = SG_MALLOC(float64_t, cardinality+1); @@ -173,7 +173,7 @@ float64_t CANOVAKernel::compute_recursive2(float64_t* avec, float64_t* bvec, int return result; } -CANOVAKernel* CANOVAKernel::obtain_from_generic(CKernel* kernel) +std::shared_ptr ANOVAKernel::obtain_from_generic(std::shared_ptr kernel) { if (!kernel) return NULL; @@ -183,6 +183,6 @@ CANOVAKernel* CANOVAKernel::obtain_from_generic(CKernel* kernel) kernel->get_kernel_type()); /* since an additional reference is returned */ - SG_REF(kernel); - return (CANOVAKernel*)kernel; + + return std::static_pointer_cast(kernel); } diff --git a/src/shogun/kernel/ANOVAKernel.h b/src/shogun/kernel/ANOVAKernel.h index 6b79b86ece5..1225d4f5d37 100644 --- a/src/shogun/kernel/ANOVAKernel.h +++ b/src/shogun/kernel/ANOVAKernel.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Heiko Strathmann, * Evan Shelhamer, Bjoern Esser */ @@ -18,7 +18,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief ANOVA (ANalysis Of VAriances) kernel * @@ -31,17 +31,17 @@ class CDistance; * this function is computed recusively */ -class CANOVAKernel: public CDotKernel +class ANOVAKernel: public DotKernel { public: /** default constructor */ - CANOVAKernel(); + ANOVAKernel(); /** constructor * @param cache size of cache * @param d kernel parameter cardinality */ - CANOVAKernel(int32_t cache, int32_t d); + ANOVAKernel(int32_t cache, int32_t d); /** constructor * @param l features left-side @@ -49,17 +49,17 @@ class CANOVAKernel: public CDotKernel * @param d kernel parameter cardinality * @param cache cache size */ - CANOVAKernel( - CDenseFeatures* l, CDenseFeatures* r, int32_t d, int32_t cache); + ANOVAKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d, int32_t cache); - virtual ~CANOVAKernel(); + virtual ~ANOVAKernel(); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -109,7 +109,7 @@ class CANOVAKernel: public CDotKernel * @param kernel Kernel to cast. Must be CANOVAKernel. Might be NULL * @return casted CANOVAKernel object, NULL if input was NULL */ - static CANOVAKernel* obtain_from_generic(CKernel* kernel); + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); protected: /** diff --git a/src/shogun/kernel/AUCKernel.cpp b/src/shogun/kernel/AUCKernel.cpp index 31cad7b6ca1..3b67c5f67c4 100644 --- a/src/shogun/kernel/AUCKernel.cpp +++ b/src/shogun/kernel/AUCKernel.cpp @@ -13,35 +13,31 @@ using namespace shogun; -void CAUCKernel::init() +void AUCKernel::init() { SG_ADD( &subkernel, "subkernel", "The subkernel.", ParameterProperties::HYPER); SG_ADD(&labels, "labels", "The labels."); - watch_method("setup_auc_maximization", &CAUCKernel::setup_auc_maximization); + watch_method("setup_auc_maximization", &AUCKernel::setup_auc_maximization); } -CAUCKernel::CAUCKernel() : CDotKernel(0), subkernel(nullptr), labels(nullptr) +AUCKernel::AUCKernel() : DotKernel(0), subkernel(nullptr), labels(nullptr) { init(); } -CAUCKernel::CAUCKernel(int32_t size, CKernel* s, CLabels* l) - : CDotKernel(size), subkernel(s), labels(l) +AUCKernel::AUCKernel(int32_t size, std::shared_ptr s, std::shared_ptr l) + : DotKernel(size), subkernel(s), labels(l) { init(); - SG_REF(subkernel); - SG_REF(labels) } -CAUCKernel::~CAUCKernel() +AUCKernel::~AUCKernel() { - SG_UNREF(subkernel); - SG_UNREF(labels) cleanup(); } -bool CAUCKernel::setup_auc_maximization() +bool AUCKernel::setup_auc_maximization() { io::info("setting up AUC maximization"); ASSERT(labels) @@ -49,7 +45,8 @@ bool CAUCKernel::setup_auc_maximization() labels->ensure_valid(); // get the original labels - SGVector int_labels = ((CBinaryLabels*)labels)->get_int_labels(); + SGVector int_labels = binary_labels(labels)->get_int_labels(); + ASSERT(subkernel->get_num_vec_rhs() == int_labels.vlen) // count positive and negative @@ -103,12 +100,12 @@ bool CAUCKernel::setup_auc_maximization() } // create label object and attach it to svm - auto* lab_auc = new CBinaryLabels(num_auc); + auto lab_auc = std::make_shared(num_auc); lab_auc->set_int_labels(SGVector(labels_auc, num_auc, false)); - SG_REF(lab_auc); + // create feature object - CDenseFeatures* f = new CDenseFeatures(features_auc); + auto f = std::make_shared>(features_auc); // create AUC kernel and attach the features init(f, f); @@ -118,22 +115,20 @@ bool CAUCKernel::setup_auc_maximization() return true; } -bool CAUCKernel::init(CFeatures* l, CFeatures* r) +bool AUCKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); init_normalizer(); return true; } -float64_t CAUCKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t AUCKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; - uint16_t* avec = ((CDenseFeatures*)lhs) - ->get_feature_vector(idx_a, alen, afree); - uint16_t* bvec = ((CDenseFeatures*)rhs) - ->get_feature_vector(idx_b, blen, bfree); + uint16_t* avec=lhs->as>()->get_feature_vector(idx_a, alen, afree); + uint16_t* bvec=rhs->as>()->get_feature_vector(idx_b, blen, bfree); ASSERT(alen == 2) ASSERT(blen == 2) @@ -150,9 +145,8 @@ float64_t CAUCKernel::compute(int32_t idx_a, int32_t idx_b) k22 = subkernel->kernel(idx_a2, idx_b2); float64_t result = k11 + k22 - k21 - k12; - - ((CDenseFeatures*)lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*)rhs)->free_feature_vector(bvec, idx_b, bfree); + lhs->as>()->free_feature_vector(avec, idx_a, afree); + rhs->as>()->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/kernel/AUCKernel.h b/src/shogun/kernel/AUCKernel.h index 071c274cfaa..ce4c2eb6766 100644 --- a/src/shogun/kernel/AUCKernel.h +++ b/src/shogun/kernel/AUCKernel.h @@ -16,9 +16,9 @@ namespace shogun { - class CLabels; + class Labels; template - class CDenseFeatures; + class DenseFeatures; /** @brief The AUC kernel can be used to maximize the area under the * receiver operator characteristic curve (AUC) instead of margin in SVM @@ -29,13 +29,12 @@ namespace shogun * are created that ensure that all positive examples get a higher score * than all negative examples in training. */ - class CAUCKernel : public CDotKernel + class AUCKernel : public DotKernel { void init(); - public: /** default constructor */ - CAUCKernel(); + AUCKernel(); /** constructor * @@ -43,10 +42,10 @@ namespace shogun * @param subkernel the subkernel * @param labels the labels for AUC maximization */ - CAUCKernel(int32_t size, CKernel* subkernel, CLabels* labels); + AUCKernel(int32_t size, std::shared_ptr subkernel, std::shared_ptr labels); /** destructor */ - virtual ~CAUCKernel(); + virtual ~AUCKernel(); /** initialize kernel based on current labeling and subkernel * @@ -61,7 +60,7 @@ namespace shogun * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * @@ -112,9 +111,9 @@ namespace shogun protected: /** the subkernel */ - CKernel* subkernel; + std::shared_ptr subkernel; /** the labels */ - CLabels* labels; + std::shared_ptr labels; }; } // namespace shogun #endif /* _AUCKERNEL_H__ */ diff --git a/src/shogun/kernel/BesselKernel.cpp b/src/shogun/kernel/BesselKernel.cpp index f999d60253b..bd52ac84a34 100644 --- a/src/shogun/kernel/BesselKernel.cpp +++ b/src/shogun/kernel/BesselKernel.cpp @@ -10,56 +10,56 @@ using namespace shogun; -CBesselKernel::CBesselKernel():CDistanceKernel(),order(0.0),degree(0) +BesselKernel::BesselKernel():DistanceKernel(),order(0.0),degree(0) { init(); } -CBesselKernel::CBesselKernel(int32_t size, float64_t v, float64_t w, - int32_t n, CDistance* dist) : - CDistanceKernel(size,w,dist), order(v), degree(n) +BesselKernel::BesselKernel(int32_t size, float64_t v, float64_t w, + int32_t n, std::shared_ptr dist) : + DistanceKernel(size,w,dist), order(v), degree(n) { ASSERT(distance) - SG_REF(distance); + init(); } -CBesselKernel::CBesselKernel(CFeatures* l, CFeatures* r, float64_t v, - float64_t w, int32_t n, CDistance* dist, int32_t size) : - CDistanceKernel(size,w,dist), order(v), degree(n) +BesselKernel::BesselKernel(std::shared_ptr l, std::shared_ptr r, float64_t v, + float64_t w, int32_t n, std::shared_ptr dist, int32_t size) : + DistanceKernel(size,w,dist), order(v), degree(n) { init(); ASSERT(distance) - SG_REF(distance); + init(l,r); } -CBesselKernel::~CBesselKernel() +BesselKernel::~BesselKernel() { cleanup(); - SG_UNREF(distance); + } -void CBesselKernel::cleanup() +void BesselKernel::cleanup() { } -bool CBesselKernel::init(CFeatures* l, CFeatures* r) +bool BesselKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(distance) - CDistanceKernel::init(l,r); + DistanceKernel::init(l,r); distance->init(l,r); return init_normalizer(); } -void CBesselKernel::init() +void BesselKernel::init() { SG_ADD(&order, "order", "Kernel order.", ParameterProperties::HYPER); SG_ADD(°ree, "degree", "Kernel degree.", ParameterProperties::HYPER); } -float64_t CBesselKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t BesselKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = distance->distance(idx_a, idx_b); - return jn(order,dist/width)/CMath::pow(dist,-degree*order); + return jn(order,dist/width)/Math::pow(dist,-degree*order); } diff --git a/src/shogun/kernel/BesselKernel.h b/src/shogun/kernel/BesselKernel.h index d3034e68ccd..c0f98723e34 100644 --- a/src/shogun/kernel/BesselKernel.h +++ b/src/shogun/kernel/BesselKernel.h @@ -15,7 +15,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief the class Bessel kernel * * It is defined as @@ -27,11 +27,11 @@ class CDistance; * \f$\tau\f$ is the kernel width, and * \f$n\f$ is the kernel degree. * */ -class CBesselKernel: public CDistanceKernel +class BesselKernel: public DistanceKernel { public: /** default constructor */ - CBesselKernel(); + BesselKernel(); /** constructor * @@ -41,9 +41,9 @@ class CBesselKernel: public CDistanceKernel * @param degree the kernel degree * @param dist distance to be used */ - CBesselKernel(int32_t size, float64_t order, + BesselKernel(int32_t size, float64_t order, float64_t width, int32_t degree, - CDistance* dist); + std::shared_ptr dist); /** constructor * @@ -55,14 +55,14 @@ class CBesselKernel: public CDistanceKernel * @param dist distance to be used * @param size cache size */ - CBesselKernel(CFeatures* l, CFeatures* r, + BesselKernel(std::shared_ptr l, std::shared_ptr r, float64_t order, float64_t width, int32_t degree, - CDistance* dist, int32_t size=10); + std::shared_ptr dist, int32_t size=10); /** * clean up kernel */ - virtual ~CBesselKernel(); + virtual ~BesselKernel(); /** initialize kernel * @@ -70,7 +70,7 @@ class CBesselKernel: public CDistanceKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** cleanup */ virtual void cleanup(); diff --git a/src/shogun/kernel/CauchyKernel.cpp b/src/shogun/kernel/CauchyKernel.cpp index 0924d39bf53..1d482b78b46 100644 --- a/src/shogun/kernel/CauchyKernel.cpp +++ b/src/shogun/kernel/CauchyKernel.cpp @@ -9,49 +9,49 @@ using namespace shogun; -CCauchyKernel::CCauchyKernel(): CKernel(0), m_distance(NULL), m_sigma(1.0) +CauchyKernel::CauchyKernel(): Kernel(0), m_distance(NULL), m_sigma(1.0) { init(); } -CCauchyKernel::CCauchyKernel(int32_t cache, float64_t sigma, CDistance* dist) -: CKernel(cache), m_distance(dist), m_sigma(sigma) +CauchyKernel::CauchyKernel(int32_t cache, float64_t sigma, std::shared_ptr dist) +: Kernel(cache), m_distance(dist), m_sigma(sigma) { init(); ASSERT(m_distance) - SG_REF(m_distance); + } -CCauchyKernel::CCauchyKernel(CFeatures *l, CFeatures *r, float64_t sigma, CDistance* dist) -: CKernel(10), m_distance(dist), m_sigma(sigma) +CauchyKernel::CauchyKernel(std::shared_ptrl, std::shared_ptrr, float64_t sigma, std::shared_ptr dist) +: Kernel(10), m_distance(dist), m_sigma(sigma) { init(); ASSERT(m_distance) - SG_REF(m_distance); + init(l, r); } -CCauchyKernel::~CCauchyKernel() +CauchyKernel::~CauchyKernel() { cleanup(); - SG_UNREF(m_distance); + } -bool CCauchyKernel::init(CFeatures* l, CFeatures* r) +bool CauchyKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(m_distance) - CKernel::init(l,r); + Kernel::init(l,r); m_distance->init(l,r); return init_normalizer(); } -void CCauchyKernel::init() +void CauchyKernel::init() { SG_ADD(&m_sigma, "sigma", "Sigma kernel parameter.", ParameterProperties::HYPER); SG_ADD(&m_distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } -float64_t CCauchyKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t CauchyKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = m_distance->distance(idx_a, idx_b); return 1.0/(1.0+dist*dist/m_sigma); diff --git a/src/shogun/kernel/CauchyKernel.h b/src/shogun/kernel/CauchyKernel.h index d1138876d20..a168c652234 100644 --- a/src/shogun/kernel/CauchyKernel.h +++ b/src/shogun/kernel/CauchyKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Cauchy kernel * @@ -28,18 +28,18 @@ class CDistance; * */ -class CCauchyKernel: public CKernel +class CauchyKernel: public Kernel { public: /** default constructor */ - CCauchyKernel(); + CauchyKernel(); /** constructor * @param cache size of cache * @param sigma kernel parameter sigma * @param dist distance to be used */ - CCauchyKernel(int32_t cache, float64_t sigma, CDistance* dist); + CauchyKernel(int32_t cache, float64_t sigma, std::shared_ptr dist); /** constructor * @param l features left-side @@ -47,14 +47,14 @@ class CCauchyKernel: public CKernel * @param sigma kernel parameter sigma * @param dist distance to be used */ - CCauchyKernel(CFeatures *l, CFeatures *r, float64_t sigma, CDistance* dist); + CauchyKernel(std::shared_ptrl, std::shared_ptrr, float64_t sigma, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -76,7 +76,7 @@ class CCauchyKernel: public CKernel */ virtual const char* get_name() const { return "CauchyKernel"; } - virtual ~CCauchyKernel(); + virtual ~CauchyKernel(); protected: @@ -96,7 +96,7 @@ class CCauchyKernel: public CKernel protected: /// distance to be used - CDistance* m_distance; + std::shared_ptr m_distance; /// sigma parameter of kernel float64_t m_sigma; diff --git a/src/shogun/kernel/Chi2Kernel.cpp b/src/shogun/kernel/Chi2Kernel.cpp index 888dbcb2b9a..2824bc1b2f7 100644 --- a/src/shogun/kernel/Chi2Kernel.cpp +++ b/src/shogun/kernel/Chi2Kernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Evan Shelhamer, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Evan Shelhamer, * Sergey Lisitsyn */ @@ -13,44 +13,44 @@ using namespace shogun; void -CChi2Kernel::init() +Chi2Kernel::init() { SG_ADD(&width, "width", "Kernel width.", ParameterProperties::HYPER); } -CChi2Kernel::CChi2Kernel() -: CDotKernel(0), width(1) +Chi2Kernel::Chi2Kernel() +: DotKernel(0), width(1) { init(); } -CChi2Kernel::CChi2Kernel(int32_t size, float64_t w) -: CDotKernel(size), width(w) +Chi2Kernel::Chi2Kernel(int32_t size, float64_t w) +: DotKernel(size), width(w) { init(); } -CChi2Kernel::CChi2Kernel( - CDenseFeatures* l, CDenseFeatures* r, float64_t w, int32_t size) -: CDotKernel(size), width(w) +Chi2Kernel::Chi2Kernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t w, int32_t size) +: DotKernel(size), width(w) { init(); init(l,r); } -CChi2Kernel::~CChi2Kernel() +Chi2Kernel::~Chi2Kernel() { cleanup(); } -bool CChi2Kernel::init(CFeatures* l, CFeatures* r) +bool Chi2Kernel::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDotKernel::init(l,r); + bool result=DotKernel::init(l,r); init_normalizer(); return result; } -float64_t CChi2Kernel::compute(int32_t idx_a, int32_t idx_b) +float64_t Chi2Kernel::compute(int32_t idx_a, int32_t idx_b) { require(width>0, "width not set to positive value. Current width {} ", width); @@ -58,9 +58,9 @@ float64_t CChi2Kernel::compute(int32_t idx_a, int32_t idx_b) bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result=0; @@ -74,18 +74,18 @@ float64_t CChi2Kernel::compute(int32_t idx_a, int32_t idx_b) result=exp(-result/width); - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } -float64_t CChi2Kernel::get_width() +float64_t Chi2Kernel::get_width() { return width; } -CChi2Kernel* CChi2Kernel::obtain_from_generic(CKernel* kernel) +std::shared_ptr Chi2Kernel::obtain_from_generic(std::shared_ptr kernel) { if (kernel->get_kernel_type()!=K_CHI2) { @@ -94,12 +94,12 @@ CChi2Kernel* CChi2Kernel::obtain_from_generic(CKernel* kernel) } /* since an additional reference is returned */ - SG_REF(kernel); - return (CChi2Kernel*)kernel; + + return std::static_pointer_cast(kernel); } -void CChi2Kernel::set_width(int32_t w) +void Chi2Kernel::set_width(int32_t w) { require(w>0, "Parameter width should be > 0"); width=w; -} \ No newline at end of file +} diff --git a/src/shogun/kernel/Chi2Kernel.h b/src/shogun/kernel/Chi2Kernel.h index c58059ae726..b3f8c123120 100644 --- a/src/shogun/kernel/Chi2Kernel.h +++ b/src/shogun/kernel/Chi2Kernel.h @@ -27,20 +27,20 @@ namespace shogun * \f] * * */ -class CChi2Kernel: public CDotKernel +class Chi2Kernel: public DotKernel { void init(); public: /** default constructor */ - CChi2Kernel(); + Chi2Kernel(); /** constructor * * @param size cache size * @param width width */ - CChi2Kernel(int32_t size, float64_t width); + Chi2Kernel(int32_t size, float64_t width); /** constructor * @@ -49,10 +49,10 @@ class CChi2Kernel: public CDotKernel * @param width width * @param size cache size */ - CChi2Kernel( CDenseFeatures* l, CDenseFeatures* r, + Chi2Kernel(std::shared_ptr> l, std::shared_ptr> r, float64_t width, int32_t size=10); - virtual ~CChi2Kernel(); + virtual ~Chi2Kernel(); /** initialize kernel * @@ -60,16 +60,16 @@ class CChi2Kernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** @return width of the kernel */ virtual float64_t get_width(); /** @param kernel is casted to CChi2Kernel, error if not possible * is SG_REF'ed - * @return casted CGaussianKernel object + * @return casted GaussianKernel object */ - static CChi2Kernel* obtain_from_generic(CKernel* kernel); + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); /** return what type of kernel we are * diff --git a/src/shogun/kernel/CircularKernel.cpp b/src/shogun/kernel/CircularKernel.cpp index 0edb44b1e9b..3754983c558 100644 --- a/src/shogun/kernel/CircularKernel.cpp +++ b/src/shogun/kernel/CircularKernel.cpp @@ -9,59 +9,59 @@ using namespace shogun; -CCircularKernel::CCircularKernel(): CKernel(0), distance(NULL) +CircularKernel::CircularKernel(): Kernel(0), distance(NULL) { init(); set_sigma(1.0); } -CCircularKernel::CCircularKernel(int32_t size, float64_t sig, CDistance* dist) -: CKernel(size), distance(dist) +CircularKernel::CircularKernel(int32_t size, float64_t sig, std::shared_ptr dist) +: Kernel(size), distance(dist) { ASSERT(distance) - SG_REF(distance); + set_sigma(sig); init(); } -CCircularKernel::CCircularKernel( - CFeatures *l, CFeatures *r, float64_t sig, CDistance* dist) -: CKernel(10), distance(dist) +CircularKernel::CircularKernel( + std::shared_ptrl, std::shared_ptrr, float64_t sig, std::shared_ptr dist) +: Kernel(10), distance(dist) { ASSERT(distance) - SG_REF(distance); + set_sigma(sig); init(); init(l, r); } -CCircularKernel::~CCircularKernel() +CircularKernel::~CircularKernel() { cleanup(); - SG_UNREF(distance); + } -bool CCircularKernel::init(CFeatures* l, CFeatures* r) +bool CircularKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(distance) - CKernel::init(l,r); + Kernel::init(l,r); distance->init(l,r); return init_normalizer(); } -void CCircularKernel::load_serializable_post() noexcept(false) +void CircularKernel::load_serializable_post() noexcept(false) { - CKernel::load_serializable_post(); + Kernel::load_serializable_post(); } -void CCircularKernel::init() +void CircularKernel::init() { SG_ADD(&distance, "distance", "Distance to be used.", ParameterProperties::HYPER); SG_ADD(&sigma, "sigma", "Sigma kernel parameter.", ParameterProperties::HYPER); } -float64_t CCircularKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t CircularKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist=distance->distance(idx_a, idx_b); float64_t ds_ratio=dist/sigma; diff --git a/src/shogun/kernel/CircularKernel.h b/src/shogun/kernel/CircularKernel.h index 2c6b157a9b9..9b0ec7d4d01 100644 --- a/src/shogun/kernel/CircularKernel.h +++ b/src/shogun/kernel/CircularKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Circular kernel * @@ -30,11 +30,11 @@ class CDistance; * */ -class CCircularKernel: public CKernel +class CircularKernel: public Kernel { public: /** default constructor */ - CCircularKernel(); + CircularKernel(); /** constructor * @@ -42,7 +42,7 @@ class CCircularKernel: public CKernel * @param sigma kernel parameter sigma * @param dist distance */ - CCircularKernel(int32_t size, float64_t sigma, CDistance* dist); + CircularKernel(int32_t size, float64_t sigma, std::shared_ptr dist); /** constructor * @@ -51,7 +51,7 @@ class CCircularKernel: public CKernel * @param sigma kernel parameter sigma * @param dist distance */ - CCircularKernel(CFeatures *l, CFeatures *r, float64_t sigma, CDistance* dist); + CircularKernel(std::shared_ptrl, std::shared_ptrr, float64_t sigma, std::shared_ptr dist); /** initialize kernel with features * @@ -59,7 +59,7 @@ class CCircularKernel: public CKernel * @param r features of right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -99,7 +99,7 @@ class CCircularKernel: public CKernel return sigma; } - virtual ~CCircularKernel(); + virtual ~CircularKernel(); /** Can (optionally) be overridden to post-initialize some * member variables which are not PARAMETER::ADD'ed. Make @@ -128,7 +128,7 @@ class CCircularKernel: public CKernel protected: /** distance */ - CDistance* distance; + std::shared_ptr distance; /** width */ float64_t sigma; diff --git a/src/shogun/kernel/CombinedKernel.cpp b/src/shogun/kernel/CombinedKernel.cpp index c60638ee6ce..afa7a41a725 100644 --- a/src/shogun/kernel/CombinedKernel.cpp +++ b/src/shogun/kernel/CombinedKernel.cpp @@ -22,27 +22,27 @@ using namespace shogun; using namespace Eigen; -CCombinedKernel::CCombinedKernel() : CKernel() +CombinedKernel::CombinedKernel() : Kernel() { init(); } -CCombinedKernel::CCombinedKernel(int32_t size, bool asw) -: CKernel(size), append_subkernel_weights(asw) +CombinedKernel::CombinedKernel(int32_t size, bool asw) +: Kernel(size), append_subkernel_weights(asw) { init(); } -CCombinedKernel::~CCombinedKernel() +CombinedKernel::~CombinedKernel() { SG_FREE(subkernel_weights_buffer); subkernel_weights_buffer=NULL; cleanup(); - SG_UNREF(kernel_array); + } -void CCombinedKernel::init_subkernel_weights() +void CombinedKernel::init_subkernel_weights() { weight_update=true; ASSERT(subkernel_log_weights.vlen>0); @@ -59,24 +59,24 @@ void CCombinedKernel::init_subkernel_weights() eigen_wt = eigen_wt.array().exp(); set_subkernel_weights(wt); } -bool CCombinedKernel::init_with_extracted_subsets( - CFeatures* l, CFeatures* r, SGVector lhs_subset, +bool CombinedKernel::init_with_extracted_subsets( + std::shared_ptr l, std::shared_ptr r, SGVector lhs_subset, SGVector rhs_subset) { - auto l_combined = dynamic_cast(l); - auto r_combined = dynamic_cast(r); + auto l_combined = std::dynamic_pointer_cast(l); + auto r_combined = std::dynamic_pointer_cast(r); if (!l_combined || !r_combined) error("Cast failed - unsupported features passed"); - CKernel::init(l, r); + Kernel::init(l, r); ASSERT(l->get_feature_type() == F_UNKNOWN) ASSERT(r->get_feature_type() == F_UNKNOWN) - CFeatures* lf = NULL; - CFeatures* rf = NULL; - CKernel* k = NULL; + std::shared_ptr lf = NULL; + std::shared_ptr rf = NULL; + std::shared_ptr k = NULL; bool result = true; index_t f_idx = 0; @@ -102,9 +102,6 @@ bool CCombinedKernel::init_with_extracted_subsets( f_idx++; if (!lf || !rf) { - SG_UNREF(lf); - SG_UNREF(rf); - SG_UNREF(k); error( "CombinedKernel: Number of features/kernels does not " "match - bailing out"); @@ -112,8 +109,8 @@ bool CCombinedKernel::init_with_extracted_subsets( SG_DEBUG("Initializing 0x{} - \"{}\"", fmt::ptr(this), k->get_name()) result = k->init(lf, rf); - SG_UNREF(lf); - SG_UNREF(rf); + + if (!result) break; @@ -128,7 +125,7 @@ bool CCombinedKernel::init_with_extracted_subsets( error( "No kernel matrix was assigned to this Custom kernel"); - auto k_custom = dynamic_cast(k); + auto k_custom = std::dynamic_pointer_cast(k); if (!k_custom) error("Dynamic cast to custom kernel failed"); @@ -153,7 +150,7 @@ bool CCombinedKernel::init_with_extracted_subsets( num_rhs, k->get_num_vec_rhs()); } - SG_UNREF(k); + } if (!result) @@ -162,7 +159,7 @@ bool CCombinedKernel::init_with_extracted_subsets( if (k) { k->list_kernel(); - SG_UNREF(k); + } else io::info(""); @@ -180,7 +177,7 @@ bool CCombinedKernel::init_with_extracted_subsets( return true; } -bool CCombinedKernel::init(CFeatures* l, CFeatures* r) +bool CombinedKernel::init(std::shared_ptr l, std::shared_ptr r) { if (enable_subkernel_weight_opt && !weight_update) { @@ -195,8 +192,8 @@ bool CCombinedKernel::init(CFeatures* l, CFeatures* r) SGVector lhs_subset; SGVector rhs_subset; - CCombinedFeatures* combined_l; - CCombinedFeatures* combined_r; + std::shared_ptr combined_l; + std::shared_ptr combined_r; auto l_subset_stack = l->get_subset_stack(); auto r_subset_stack = r->get_subset_stack(); @@ -221,8 +218,8 @@ bool CCombinedKernel::init(CFeatures* l, CFeatures* r) rhs_subset.range_fill(); } - SG_UNREF(l_subset_stack); - SG_UNREF(r_subset_stack); + + /* if the specified features are not combined features, but a single other * feature type, assume that the caller wants to use all kernels on these */ @@ -237,8 +234,8 @@ bool CCombinedKernel::init(CFeatures* l, CFeatures* r) * The we must make sure that we make any custom kernels aware of any * subsets present! */ - combined_l = new CCombinedFeatures(); - combined_r = new CCombinedFeatures(); + combined_l = std::make_shared(); + combined_r = std::make_shared(); for (index_t i = 0; i < get_num_subkernels(); ++i) { @@ -248,116 +245,116 @@ bool CCombinedKernel::init(CFeatures* l, CFeatures* r) } else { - combined_l = (CCombinedFeatures*)l; - combined_r = (CCombinedFeatures*)r; + combined_l = std::static_pointer_cast(l); + combined_r = std::static_pointer_cast(r); } return init_with_extracted_subsets( combined_l, combined_r, lhs_subset, rhs_subset); } -void CCombinedKernel::remove_lhs() +void CombinedKernel::remove_lhs() { delete_optimization(); for (index_t k_idx=0; k_idxget_kernel_type() != K_CUSTOM) k->remove_lhs(); - SG_UNREF(k); + } - CKernel::remove_lhs(); + Kernel::remove_lhs(); num_lhs=0; } -void CCombinedKernel::remove_rhs() +void CombinedKernel::remove_rhs() { delete_optimization(); for (index_t k_idx=0; k_idxget_kernel_type() != K_CUSTOM) k->remove_rhs(); - SG_UNREF(k); + } - CKernel::remove_rhs(); + Kernel::remove_rhs(); num_rhs=0; } -void CCombinedKernel::remove_lhs_and_rhs() +void CombinedKernel::remove_lhs_and_rhs() { delete_optimization(); for (index_t k_idx=0; k_idxget_kernel_type() != K_CUSTOM) k->remove_lhs_and_rhs(); - SG_UNREF(k); + } - CKernel::remove_lhs_and_rhs(); + Kernel::remove_lhs_and_rhs(); num_lhs=0; num_rhs=0; } -void CCombinedKernel::cleanup() +void CombinedKernel::cleanup() { for (index_t k_idx=0; k_idxcleanup(); - SG_UNREF(k); + } delete_optimization(); - CKernel::cleanup(); + Kernel::cleanup(); num_lhs=0; num_rhs=0; } -void CCombinedKernel::list_kernels() +void CombinedKernel::list_kernels() { io::info("BEGIN COMBINED KERNEL LIST - "); this->list_kernel(); for (index_t k_idx=0; k_idxlist_kernel(); - SG_UNREF(k); + } io::info("END COMBINED KERNEL LIST - "); } -float64_t CCombinedKernel::compute(int32_t x, int32_t y) +float64_t CombinedKernel::compute(int32_t x, int32_t y) { float64_t result=0; for (index_t k_idx=0; k_idxget_combined_kernel_weight()!=0) result += k->get_combined_kernel_weight() * k->kernel(x,y); - SG_UNREF(k); + } return result; } -bool CCombinedKernel::init_optimization( +bool CombinedKernel::init_optimization( int32_t count, int32_t *IDX, float64_t *weights) { - SG_TRACE("initializing CCombinedKernel optimization"); + SG_TRACE("initializing CombinedKernel optimization"); delete_optimization(); @@ -365,7 +362,7 @@ bool CCombinedKernel::init_optimization( for (index_t k_idx=0; k_idxinit_optimization(count, IDX, weights); else { - io::warn("non-optimizable kernel 0x%X in kernel-list", fmt::ptr(k)); + io::warn("non-optimizable kernel 0x%X in kernel-list", fmt::ptr(k.get())); have_non_optimizable=true; } if (!ret) { have_non_optimizable=true; - io::warn("init_optimization of kernel 0x%X failed", fmt::ptr(k)); + io::warn("init_optimization of kernel 0x%X failed", fmt::ptr(k.get())); } - SG_UNREF(k); + } if (have_non_optimizable) @@ -404,15 +401,15 @@ bool CCombinedKernel::init_optimization( return true; } -bool CCombinedKernel::delete_optimization() +bool CombinedKernel::delete_optimization() { for (index_t k_idx=0; k_idxhas_property(KP_LINADD)) k->delete_optimization(); - SG_UNREF(k); + } SG_FREE(sv_idx); @@ -427,7 +424,7 @@ bool CCombinedKernel::delete_optimization() return true; } -void CCombinedKernel::compute_batch( +void CombinedKernel::compute_batch( int32_t num_vec, int32_t* vec_idx, float64_t* result, int32_t num_suppvec, int32_t* IDX, float64_t* weights, float64_t factor) { @@ -442,7 +439,7 @@ void CCombinedKernel::compute_batch( for (index_t k_idx=0; k_idxhas_property(KP_BATCHEVALUATION)) { if (k->get_combined_kernel_weight()!=0) @@ -451,15 +448,15 @@ void CCombinedKernel::compute_batch( else emulate_compute_batch(k, num_vec, vec_idx, result, num_suppvec, IDX, weights); - SG_UNREF(k); + } //clean up delete_optimization(); } -void CCombinedKernel::emulate_compute_batch( - CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* result, +void CombinedKernel::emulate_compute_batch( + std::shared_ptr k, int32_t num_vec, int32_t* vec_idx, float64_t* result, int32_t num_suppvec, int32_t* IDX, float64_t* weights) { ASSERT(k) @@ -498,11 +495,11 @@ void CCombinedKernel::emulate_compute_batch( } } -float64_t CCombinedKernel::compute_optimized(int32_t idx) +float64_t CombinedKernel::compute_optimized(int32_t idx) { if (!get_is_initialized()) { - error("CCombinedKernel optimization not initialized"); + error("CombinedKernel optimization not initialized"); return 0; } @@ -510,7 +507,7 @@ float64_t CCombinedKernel::compute_optimized(int32_t idx) for (index_t k_idx=0; k_idxhas_property(KP_LINADD) && k->get_is_initialized()) { @@ -535,35 +532,35 @@ float64_t CCombinedKernel::compute_optimized(int32_t idx) } } - SG_UNREF(k); + } return result; } -void CCombinedKernel::add_to_normal(int32_t idx, float64_t weight) +void CombinedKernel::add_to_normal(int32_t idx, float64_t weight) { for (index_t k_idx=0; k_idxadd_to_normal(idx, weight); - SG_UNREF(k); + } set_is_initialized(true) ; } -void CCombinedKernel::clear_normal() +void CombinedKernel::clear_normal() { for (index_t k_idx=0; k_idxclear_normal() ; - SG_UNREF(k); + } set_is_initialized(true) ; } -void CCombinedKernel::compute_by_subkernel( +void CombinedKernel::compute_by_subkernel( int32_t idx, float64_t * subkernel_contrib) { if (append_subkernel_weights) @@ -571,7 +568,7 @@ void CCombinedKernel::compute_by_subkernel( int32_t i=0 ; for (index_t k_idx=0; k_idxget_subkernel_weights(num); if (num>1) @@ -579,7 +576,7 @@ void CCombinedKernel::compute_by_subkernel( else subkernel_contrib[i] += k->get_combined_kernel_weight() * k->compute_optimized(idx) ; - SG_UNREF(k); + i += num ; } } @@ -588,19 +585,19 @@ void CCombinedKernel::compute_by_subkernel( int32_t i=0 ; for (index_t k_idx=0; k_idxget_combined_kernel_weight()!=0) subkernel_contrib[i] += k->get_combined_kernel_weight() * k->compute_optimized(idx) ; - SG_UNREF(k); + i++ ; } } } -const float64_t* CCombinedKernel::get_subkernel_weights(int32_t& num_weights) +const float64_t* CombinedKernel::get_subkernel_weights(int32_t& num_weights) { - SG_TRACE("entering CCombinedKernel::get_subkernel_weights()"); + SG_TRACE("entering CombinedKernel::get_subkernel_weights()"); num_weights = get_num_subkernels() ; SG_FREE(subkernel_weights_buffer); @@ -613,14 +610,14 @@ const float64_t* CCombinedKernel::get_subkernel_weights(int32_t& num_weights) int32_t i=0 ; for (index_t k_idx=0; k_idxget_subkernel_weights(num); ASSERT(num==k->get_num_subkernels()) for (int32_t j=0; jget_combined_kernel_weight(); - SG_UNREF(k); ++i; } } - SG_TRACE("leaving CCombinedKernel::get_subkernel_weights()"); + SG_TRACE("leaving CombinedKernel::get_subkernel_weights()"); return subkernel_weights_buffer ; } -SGVector CCombinedKernel::get_subkernel_weights() +SGVector CombinedKernel::get_subkernel_weights() { if (enable_subkernel_weight_opt && !weight_update) { @@ -661,19 +657,19 @@ SGVector CCombinedKernel::get_subkernel_weights() return SGVector(weights, num); } -void CCombinedKernel::set_subkernel_weights(SGVector weights) +void CombinedKernel::set_subkernel_weights(SGVector weights) { if (append_subkernel_weights) { int32_t i=0 ; for (index_t k_idx=0; k_idxget_num_subkernels() ; ASSERT(iset_subkernel_weights(SGVector(&weights.vector[i],num, false)); - SG_UNREF(k); + i += num ; } } @@ -682,52 +678,52 @@ void CCombinedKernel::set_subkernel_weights(SGVector weights) int32_t i=0 ; for (index_t k_idx=0; k_idxset_combined_kernel_weight(weights.vector[i]); - SG_UNREF(k); + i++ ; } } } -void CCombinedKernel::set_optimization_type(EOptimizationType t) +void CombinedKernel::set_optimization_type(EOptimizationType t) { for (index_t k_idx=0; k_idxset_optimization_type(t); - SG_UNREF(k); + } - CKernel::set_optimization_type(t); + Kernel::set_optimization_type(t); } -bool CCombinedKernel::precompute_subkernels() +bool CombinedKernel::precompute_subkernels() { if (get_num_kernels()==0) return false; - CDynamicObjectArray* new_kernel_array = new CDynamicObjectArray(); + auto new_kernel_array = std::make_shared(); for (index_t k_idx=0; k_idxappend_element(new CCustomKernel(k)); + auto k = get_kernel(k_idx); + new_kernel_array->append_element(std::make_shared(k)); + - SG_UNREF(k); } - SG_UNREF(kernel_array); + kernel_array=new_kernel_array; - SG_REF(kernel_array); + return true; } -void CCombinedKernel::init() +void CombinedKernel::init() { sv_count=0; sv_idx=NULL; @@ -737,17 +733,17 @@ void CCombinedKernel::init() append_subkernel_weights = false; properties |= KP_LINADD | KP_KERNCOMBINATION | KP_BATCHEVALUATION; - kernel_array=new CDynamicObjectArray(); - SG_REF(kernel_array); + kernel_array=std::make_shared(); + SG_ADD(&kernel_array, "kernel_array", "Array of kernels.", ParameterProperties::HYPER); - m_parameters->add_vector(&sv_idx, &sv_count, "sv_idx", - "Support vector index."); + /*m_parameters->add_vector(&sv_idx, &sv_count, "sv_idx", + "Support vector index.");*/ watch_param("sv_idx", &sv_idx, &sv_count); - m_parameters->add_vector(&sv_weight, &sv_count, "sv_weight", - "Support vector weights."); + /*m_parameters->add_vector(&sv_weight, &sv_count, "sv_weight", + "Support vector weights.");*/ watch_param("sv_weight", &sv_weight, &sv_count); SG_ADD(&append_subkernel_weights, "append_subkernel_weights", @@ -767,7 +763,7 @@ void CCombinedKernel::init() "weight update"); } -void CCombinedKernel::enable_subkernel_weight_learning() +void CombinedKernel::enable_subkernel_weight_learning() { weight_update = false; enable_subkernel_weight_opt=false; @@ -782,7 +778,7 @@ void CCombinedKernel::enable_subkernel_weight_learning() } } -SGMatrix CCombinedKernel::get_parameter_gradient( +SGMatrix CombinedKernel::get_parameter_gradient( const TParameter* param, index_t index) { SGMatrix result; @@ -793,10 +789,10 @@ SGMatrix CCombinedKernel::get_parameter_gradient( { for (index_t k_idx=0; k_idxget_parameter_gradient(param, index); - SG_UNREF(k); + if (result.num_cols*result.num_rows>0) return result; @@ -806,10 +802,10 @@ SGMatrix CCombinedKernel::get_parameter_gradient( { for (index_t k_idx=0; k_idxget_kernel_matrix(); - SG_UNREF(k); + return result; } @@ -822,9 +818,9 @@ SGMatrix CCombinedKernel::get_parameter_gradient( if(enable_subkernel_weight_opt) { ASSERT(index>=0 && indexget_kernel_matrix(); - SG_UNREF(k); + if (weight_update) weight_update = false; float64_t factor = 1.0; @@ -842,9 +838,9 @@ SGMatrix CCombinedKernel::get_parameter_gradient( } else { - CKernel* k=get_kernel(0); + auto k=get_kernel(0); result=k->get_kernel_matrix(); - SG_UNREF(k); + result.zero(); } return result; @@ -854,7 +850,7 @@ SGMatrix CCombinedKernel::get_parameter_gradient( float64_t coeff; for (index_t k_idx=0; k_idx derivative= k->get_parameter_gradient(param, index); @@ -883,7 +879,7 @@ SGMatrix CCombinedKernel::get_parameter_gradient( } } - SG_UNREF(k); + } } } @@ -891,23 +887,23 @@ SGMatrix CCombinedKernel::get_parameter_gradient( return result; } -CCombinedKernel* CCombinedKernel::obtain_from_generic(CKernel* kernel) +std::shared_ptr CombinedKernel::obtain_from_generic(std::shared_ptr kernel) { if (kernel->get_kernel_type()!=K_COMBINED) { - error("CCombinedKernel::obtain_from_generic(): provided kernel is " - "not of type CCombinedKernel!"); + error("CombinedKernel::obtain_from_generic(): provided kernel is " + "not of type CombinedKernel!"); } /* since an additional reference is returned */ - SG_REF(kernel); - return (CCombinedKernel*)kernel; + + return std::static_pointer_cast(kernel); } -CList* CCombinedKernel::combine_kernels(CList* kernel_list) +std::shared_ptr CombinedKernel::combine_kernels(std::shared_ptr kernel_list) { - CList* return_list = new CList(true); - SG_REF(return_list); + auto return_list = std::make_shared(true); + if (!kernel_list) return return_list; @@ -919,43 +915,43 @@ CList* CCombinedKernel::combine_kernels(CList* kernel_list) int32_t list_index = 0; /* calculation of total combinations */ - CSGObject* list = kernel_list->get_first_element(); + auto list = kernel_list->get_first_element(); while (list) { - CList* c_list= dynamic_cast(list); + auto c_list= std::dynamic_pointer_cast(list); if (!c_list) { - error("CCombinedKernel::combine_kernels() : Failed to cast list of type " - "{} to type CList", list->get_name()); + error("CombinedKernel::combine_kernels() : Failed to cast list of type " + "{} to type List", list->get_name()); } if (c_list->get_num_elements()==0) { - error("CCombinedKernel::combine_kernels() : Sub-list in position {} " + error("CombinedKernel::combine_kernels() : Sub-list in position {} " "is empty.", list_index); } num_combinations *= c_list->get_num_elements(); if (kernel_list->get_delete_data()) - SG_UNREF(list); + list = kernel_list->get_next_element(); ++list_index; } - /* creation of CCombinedKernels */ - CDynamicObjectArray kernel_array(num_combinations); + /* creation of CombinedKernels */ + DynamicObjectArray kernel_array(num_combinations); for (index_t i=0; i(); return_list->append_element(c_kernel); kernel_array.push_back(c_kernel); } /* first pass */ list = kernel_list->get_first_element(); - CList* c_list = dynamic_cast(list); + auto c_list = std::dynamic_pointer_cast(list); /* kernel index in the list */ index_t kernel_index = 0; @@ -965,15 +961,15 @@ CList* CCombinedKernel::combine_kernels(CList* kernel_list) */ EKernelType prev_kernel_type = K_UNKNOWN; bool first_kernel = true; - for (CSGObject* kernel=c_list->get_first_element(); kernel; kernel=c_list->get_next_element()) + for (auto kernel=c_list->get_first_element(); kernel; kernel=c_list->get_next_element()) { - CKernel* c_kernel = dynamic_cast(kernel); + auto c_kernel = std::dynamic_pointer_cast(kernel); if (first_kernel) first_kernel = false; else if (c_kernel->get_kernel_type()!=prev_kernel_type) { - error("CCombinedKernel::combine_kernels() : Sub-list in position " + error("CombinedKernel::combine_kernels() : Sub-list in position " "0 contains different types of kernels"); } @@ -981,19 +977,14 @@ CList* CCombinedKernel::combine_kernels(CList* kernel_list) for (index_t index=kernel_index; indexget_num_elements()) { - CCombinedKernel* comb_kernel = - dynamic_cast(kernel_array.get_element(index)); + auto comb_kernel = + std::dynamic_pointer_cast(kernel_array.get_element(index)); comb_kernel->append_kernel(c_kernel); - SG_UNREF(comb_kernel); + } ++kernel_index; - if (c_list->get_delete_data()) - SG_UNREF(kernel); } - if (kernel_list->get_delete_data()) - SG_UNREF(list); - /* how often each kernel of the sub-list must appear */ int32_t freq = c_list->get_num_elements(); @@ -1004,20 +995,20 @@ CList* CCombinedKernel::combine_kernels(CList* kernel_list) list_index = 1; while (list) { - c_list = dynamic_cast(list); + c_list = std::dynamic_pointer_cast(list); /* index of kernel in the list */ kernel_index = 0; first_kernel = true; - for (CSGObject* kernel=c_list->get_first_element(); kernel; kernel=c_list->get_next_element()) + for (auto kernel=c_list->get_first_element(); kernel; kernel=c_list->get_next_element()) { - CKernel* c_kernel = dynamic_cast(kernel); + auto c_kernel = std::dynamic_pointer_cast(kernel); if (first_kernel) first_kernel = false; else if (c_kernel->get_kernel_type()!=prev_kernel_type) { - error("CCombinedKernel::combine_kernels() : Sub-list in position " + error("CombinedKernel::combine_kernels() : Sub-list in position " "{} contains different types of kernels", list_index); } @@ -1029,21 +1020,18 @@ CList* CCombinedKernel::combine_kernels(CList* kernel_list) /* inserts freq consecutives times the current kernel */ for (index_t index=0; index(kernel_array.get_element(base+index)); + auto comb_kernel = + std::dynamic_pointer_cast(kernel_array.get_element(base+index)); comb_kernel->append_kernel(c_kernel); - SG_UNREF(comb_kernel); + } } ++kernel_index; - if (c_list->get_delete_data()) - SG_UNREF(kernel); } freq *= c_list->get_num_elements(); - if (kernel_list->get_delete_data()) - SG_UNREF(list); + list = kernel_list->get_next_element(); ++list_index; } diff --git a/src/shogun/kernel/CombinedKernel.h b/src/shogun/kernel/CombinedKernel.h index 20db7120e72..08b81cf7644 100644 --- a/src/shogun/kernel/CombinedKernel.h +++ b/src/shogun/kernel/CombinedKernel.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evangelos Anagnostopoulos, Jacob Walker, - * Sergey Lisitsyn, Roman Votyakov, Michele Mazzoni, Heiko Strathmann, + * Authors: Soeren Sonnenburg, Evangelos Anagnostopoulos, Jacob Walker, + * Sergey Lisitsyn, Roman Votyakov, Michele Mazzoni, Heiko Strathmann, * Yuyu Zhang, Evgeniy Andreev, Evan Shelhamer, Wu Lin */ @@ -21,10 +21,10 @@ namespace shogun { -class CFeatures; -class CCombinedFeatures; -class CList; -class CListElement; +class Features; +class CombinedFeatures; +class List; +class ListElement; /** * @brief The Combined kernel is used to combine a number of kernels into a * single CombinedKernel object by linear combination. @@ -34,7 +34,7 @@ class CListElement; * * It is especially useful to combine kernels working on different domains and * to combine kernels looking at independent features and requires - * CCombinedFeatures to be used. + * CombinedFeatures to be used. * * It is defined as: * @@ -43,11 +43,11 @@ class CListElement; * \f] * */ -class CCombinedKernel : public CKernel +class CombinedKernel : public Kernel { public: /** Default constructor */ - CCombinedKernel(); + CombinedKernel(); /** constructor * @@ -55,9 +55,9 @@ class CCombinedKernel : public CKernel * @param append_subkernel_weights if subkernel weights shall be * appended */ - CCombinedKernel(int32_t size, bool append_subkernel_weights); + CombinedKernel(int32_t size, bool append_subkernel_weights); - virtual ~CCombinedKernel(); + virtual ~CombinedKernel(); /** initialize kernel. Provided features have to be combined features. * If they are not, all subkernels are tried to be initialised with the @@ -67,7 +67,7 @@ class CCombinedKernel : public CKernel * @param rhs features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* lhs, CFeatures* rhs); + virtual bool init(std::shared_ptr lhs, std::shared_ptr rhs); /** clean up kernel */ virtual void cleanup(); @@ -112,7 +112,7 @@ class CCombinedKernel : public CKernel * * @return first kernel */ - inline CKernel* get_first_kernel() + inline std::shared_ptr get_first_kernel() { return get_kernel(0); } @@ -122,11 +122,11 @@ class CCombinedKernel : public CKernel * @param idx index of kernel * @return kernel at index idx */ - inline CKernel* get_kernel(int32_t idx) + inline std::shared_ptr get_kernel(int32_t idx) { if (idx < get_num_kernels()) { - return (CKernel*)kernel_array->get_element(idx); + return std::static_pointer_cast(kernel_array->get_element(idx)); } else { @@ -138,7 +138,7 @@ class CCombinedKernel : public CKernel * * @return last kernel */ - inline CKernel* get_last_kernel() + inline std::shared_ptr get_last_kernel() { return get_kernel(get_num_kernels()-1); } @@ -150,7 +150,7 @@ class CCombinedKernel : public CKernel * @param idx the index of the position where the kernel should be added * @return if inserting was successful */ - inline bool insert_kernel(CKernel* k, int32_t idx) + inline bool insert_kernel(std::shared_ptr k, int32_t idx) { ASSERT(k) adjust_num_lhs_rhs_initialized(k); @@ -169,7 +169,7 @@ class CCombinedKernel : public CKernel virtual bool has_property(EKernelProperty p) { if (p != KP_LINADD) - return CKernel::has_property(p); + return Kernel::has_property(p); if (!kernel_array || !kernel_array->get_num_elements()) return false; @@ -178,8 +178,8 @@ class CCombinedKernel : public CKernel for (auto i : range(kernel_array->get_num_elements())) { auto cur = kernel_array->get_element(i); - all_linadd &= ((CKernel*)cur)->has_property(p); - SG_UNREF(cur); + all_linadd &= (std::static_pointer_cast(cur))->has_property(p); + if (!all_linadd) break; } @@ -192,7 +192,7 @@ class CCombinedKernel : public CKernel * @param k kernel * @return if appending was successful */ - inline bool append_kernel(CKernel* k) + inline bool append_kernel(std::shared_ptr k) { ASSERT(k) adjust_num_lhs_rhs_initialized(k); @@ -252,9 +252,9 @@ class CCombinedKernel : public CKernel for (index_t k_idx=0; k_idxget_num_subkernels(); - SG_UNREF(k); + } return num_subkernels; } @@ -337,7 +337,7 @@ class CCombinedKernel : public CKernel * @param weights weights */ void emulate_compute_batch( - CKernel* k, int32_t num_vec, int32_t* vec_idx, float64_t* target, + std::shared_ptr k, int32_t num_vec, int32_t* vec_idx, float64_t* target, int32_t num_suppvec, int32_t* IDX, float64_t* weights); /** add to normal vector @@ -393,7 +393,7 @@ class CCombinedKernel : public CKernel * @param kernel kernel to cast to CombinedKernel * @return casted version of kernel. */ - static CCombinedKernel* obtain_from_generic(CKernel* kernel); + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); /** return derivative with respect to specified parameter * @@ -409,9 +409,9 @@ class CCombinedKernel : public CKernel * * @return kernel array */ - inline CDynamicObjectArray* get_array() + inline std::shared_ptr get_array() { - SG_REF(kernel_array); + return kernel_array; } @@ -424,7 +424,7 @@ class CCombinedKernel : public CKernel * * @return a list of CombinedKernels. */ - static CList* combine_kernels(CList* kernel_list); + static std::shared_ptr combine_kernels(std::shared_ptr kernel_list); /** Enable to find weight for subkernels during model selection */ @@ -432,7 +432,7 @@ class CCombinedKernel : public CKernel protected: virtual void init_subkernel_weights(); - + /** compute kernel function * * @param x x @@ -446,7 +446,7 @@ class CCombinedKernel : public CKernel * * @param k kernel */ - inline void adjust_num_lhs_rhs_initialized(CKernel* k) + inline void adjust_num_lhs_rhs_initialized(std::shared_ptr k) { ASSERT(k) @@ -490,7 +490,7 @@ class CCombinedKernel : public CKernel /** * The purpose of this function is to make customkernels aware of any * subsets present, regardless whether the features passed are of type - * CCombinedFeatures or not + * CombinedFeatures or not * @param lhs combined features * @param rhs rombined features * @param lhs_subset subset present on lhs - pass identity subset if @@ -500,12 +500,12 @@ class CCombinedKernel : public CKernel * @return init succesful */ bool init_with_extracted_subsets( - CFeatures* lhs, CFeatures* rhs, SGVector lhs_subset, + std::shared_ptr lhs, std::shared_ptr rhs, SGVector lhs_subset, SGVector rhs_subset); protected: /** list of kernels */ - CDynamicObjectArray* kernel_array; + std::shared_ptr kernel_array; /** support vector count */ int32_t sv_count; /** support vector index */ diff --git a/src/shogun/kernel/ConstKernel.cpp b/src/shogun/kernel/ConstKernel.cpp index 8c480ac996a..cbaa97be8ff 100644 --- a/src/shogun/kernel/ConstKernel.cpp +++ b/src/shogun/kernel/ConstKernel.cpp @@ -14,38 +14,38 @@ using namespace shogun; -CConstKernel::CConstKernel() -: CKernel() +ConstKernel::ConstKernel() +: Kernel() { init(); } -CConstKernel::CConstKernel(float64_t c) -: CKernel() +ConstKernel::ConstKernel(float64_t c) +: Kernel() { init(); const_value=c; } -CConstKernel::CConstKernel(CFeatures* l, CFeatures* r, float64_t c) -: CKernel() +ConstKernel::ConstKernel(std::shared_ptr l, std::shared_ptr r, float64_t c) +: Kernel() { init(); const_value=c; init(l, r); } -CConstKernel::~CConstKernel() +ConstKernel::~ConstKernel() { } -bool CConstKernel::init(CFeatures* l, CFeatures* r) +bool ConstKernel::init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l, r); + Kernel::init(l, r); return init_normalizer(); } -void CConstKernel::init() +void ConstKernel::init() { const_value=1.0; SG_ADD(&const_value, "const_value", "Value for kernel elements.", diff --git a/src/shogun/kernel/ConstKernel.h b/src/shogun/kernel/ConstKernel.h index 64313acd724..717e0c81522 100644 --- a/src/shogun/kernel/ConstKernel.h +++ b/src/shogun/kernel/ConstKernel.h @@ -22,17 +22,17 @@ namespace shogun * \f$k({\bf x}, {\bf x'})= c\f$ * */ -class CConstKernel: public CKernel +class ConstKernel: public Kernel { public: /** default constructor */ - CConstKernel(); + ConstKernel(); /** constructor * * @param c constant c */ - CConstKernel(float64_t c); + ConstKernel(float64_t c); /** constructor * @@ -40,9 +40,9 @@ class CConstKernel: public CKernel * @param r features of right-hand side * @param c constant c */ - CConstKernel(CFeatures* l, CFeatures *r, float64_t c); + ConstKernel(std::shared_ptr l, std::shared_ptrr, float64_t c); - virtual ~CConstKernel(); + virtual ~ConstKernel(); /** initialize kernel * @@ -50,7 +50,7 @@ class CConstKernel: public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/CustomKernel.cpp b/src/shogun/kernel/CustomKernel.cpp index 832a08e6639..d494fbe8e69 100644 --- a/src/shogun/kernel/CustomKernel.cpp +++ b/src/shogun/kernel/CustomKernel.cpp @@ -17,18 +17,18 @@ using namespace shogun; using namespace linalg; -void CCustomKernel::init() +void CustomKernel::init() { - m_row_subset_stack=new CSubsetStack(); - SG_REF(m_row_subset_stack) - m_col_subset_stack=new CSubsetStack(); - SG_REF(m_col_subset_stack) + m_row_subset_stack=std::make_shared(); + + m_col_subset_stack=std::make_shared(); + m_is_symmetric=false; m_free_km=true; - SG_ADD((CSGObject**)&m_row_subset_stack, "row_subset_stack", + SG_ADD((std::shared_ptr*)&m_row_subset_stack, "row_subset_stack", "Subset stack of rows"); - SG_ADD((CSGObject**)&m_col_subset_stack, "col_subset_stack", + SG_ADD((std::shared_ptr*)&m_col_subset_stack, "col_subset_stack", "Subset stack of columns"); SG_ADD(&m_free_km, "free_km", "Whether kernel matrix should be freed in " "destructor"); @@ -37,23 +37,23 @@ void CCustomKernel::init() SG_ADD(&upper_diagonal, "upper_diagonal", "Upper diagonal"); } -CCustomKernel::CCustomKernel() -: CKernel(10), kmatrix(), upper_diagonal(false) +CustomKernel::CustomKernel() +: Kernel(10), kmatrix(), upper_diagonal(false) { - SG_TRACE("created CCustomKernel"); + SG_TRACE("created CustomKernel"); init(); } -CCustomKernel::CCustomKernel(CKernel* k) -: CKernel(10) +CustomKernel::CustomKernel(std::shared_ptr k) +: Kernel(10) { - SG_TRACE("created CCustomKernel"); + SG_TRACE("created CustomKernel"); init(); /* if constructed from a custom kernel, use same kernel matrix */ if (k->get_kernel_type()==K_CUSTOM) { - CCustomKernel* casted=(CCustomKernel*)k; + auto casted=std::static_pointer_cast(k); m_is_symmetric=casted->m_is_symmetric; set_full_kernel_matrix_from_full(casted->get_float32_kernel_matrix()); m_free_km=false; @@ -65,8 +65,8 @@ CCustomKernel::CCustomKernel(CKernel* k) } } -CCustomKernel::CCustomKernel(SGMatrix km) -: CKernel(10), upper_diagonal(false) +CustomKernel::CustomKernel(SGMatrix km) +: Kernel(10), upper_diagonal(false) { SG_TRACE("Entering"); init(); @@ -74,8 +74,8 @@ CCustomKernel::CCustomKernel(SGMatrix km) SG_TRACE("Leaving"); } -CCustomKernel::CCustomKernel(SGMatrix km) -: CKernel(10), upper_diagonal(false) +CustomKernel::CustomKernel(SGMatrix km) +: Kernel(10), upper_diagonal(false) { SG_TRACE("Entering"); init(); @@ -83,21 +83,19 @@ CCustomKernel::CCustomKernel(SGMatrix km) SG_TRACE("Leaving"); } -CCustomKernel::~CCustomKernel() +CustomKernel::~CustomKernel() { SG_TRACE("Entering"); cleanup(); - SG_UNREF(m_row_subset_stack); - SG_UNREF(m_col_subset_stack); SG_TRACE("Leaving"); } -bool CCustomKernel::dummy_init(int32_t rows, int32_t cols) +bool CustomKernel::dummy_init(int32_t rows, int32_t cols) { - return init(new CDummyFeatures(rows), new CDummyFeatures(cols)); + return init(std::make_shared(rows), std::make_shared(cols)); } -bool CCustomKernel::init(CFeatures* l, CFeatures* r) +bool CustomKernel::init(std::shared_ptr l, std::shared_ptr r) { /* make it possible to call with NULL values since features are useless * for custom kernel matrix */ @@ -108,8 +106,8 @@ bool CCustomKernel::init(CFeatures* l, CFeatures* r) r=rhs; /* Make sure l and r should not be NULL */ - require(l, "CFeatures l should not be NULL"); - require(r, "CFeatures r should not be NULL"); + require(l, "Features l should not be NULL"); + require(r, "Features r should not be NULL"); /* Make sure l and r have the same type of CFeatures */ require(l->get_feature_class()==r->get_feature_class(), @@ -119,15 +117,15 @@ bool CCustomKernel::init(CFeatures* l, CFeatures* r) "Different FeatureType: l is {}, r is {}", l->get_feature_type(),r->get_feature_type()); - /* If l and r are the type of CIndexFeatures, + /* If l and r are the type of IndexFeatures, * the init function adds a subset to kernel matrix. * Then call get_kernel_matrix will get the submatrix * of the kernel matrix. */ if (l->get_feature_class()==C_INDEX && r->get_feature_class()==C_INDEX) { - CIndexFeatures* l_idx = (CIndexFeatures*)l; - CIndexFeatures* r_idx = (CIndexFeatures*)r; + auto l_idx = std::static_pointer_cast(l); + auto r_idx = std::static_pointer_cast(r); remove_all_col_subsets(); remove_all_row_subsets(); @@ -140,8 +138,8 @@ bool CCustomKernel::init(CFeatures* l, CFeatures* r) return true; } - /* For other types of CFeatures do the default actions below */ - CKernel::init(l, r); + /* For other types of Features do the default actions below */ + Kernel::init(l, r); lhs_equals_rhs=m_is_symmetric; @@ -152,7 +150,7 @@ bool CCustomKernel::init(CFeatures* l, CFeatures* r) return init_normalizer(); } -float64_t CCustomKernel::sum_symmetric_block(index_t block_begin, +float64_t CustomKernel::sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag) { SG_TRACE("Entering"); @@ -160,8 +158,8 @@ float64_t CCustomKernel::sum_symmetric_block(index_t block_begin, if (m_row_subset_stack->has_subsets() || m_col_subset_stack->has_subsets()) { io::info("Row/col subsets initialized! Falling back to " - "CKernel::sum_symmetric_block (slower)!"); - return CKernel::sum_symmetric_block(block_begin, block_size, no_diag); + "Kernel::sum_symmetric_block (slower)!"); + return Kernel::sum_symmetric_block(block_begin, block_size, no_diag); } require(kmatrix.matrix, "The kernel matrix is not initialized!"); @@ -179,7 +177,7 @@ float64_t CCustomKernel::sum_symmetric_block(index_t block_begin, block_begin, block_size, block_size), no_diag); } -float64_t CCustomKernel::sum_block(index_t block_begin_row, +float64_t CustomKernel::sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag) { @@ -188,8 +186,8 @@ float64_t CCustomKernel::sum_block(index_t block_begin_row, if (m_row_subset_stack->has_subsets() || m_col_subset_stack->has_subsets()) { io::info("Row/col subsets initialized! Falling back to " - "CKernel::sum_block (slower)!"); - return CKernel::sum_block(block_begin_row, block_begin_col, + "Kernel::sum_block (slower)!"); + return Kernel::sum_block(block_begin_row, block_begin_col, block_size_row, block_size_col, no_diag); } @@ -219,7 +217,7 @@ float64_t CCustomKernel::sum_block(index_t block_begin_row, block_size_row, block_size_col), no_diag); } -SGVector CCustomKernel::row_wise_sum_symmetric_block(index_t +SGVector CustomKernel::row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag) { SG_TRACE("Entering"); @@ -227,8 +225,8 @@ SGVector CCustomKernel::row_wise_sum_symmetric_block(index_t if (m_row_subset_stack->has_subsets() || m_col_subset_stack->has_subsets()) { io::info("Row/col subsets initialized! Falling back to " - "CKernel::row_wise_sum_symmetric_block (slower)!"); - return CKernel::row_wise_sum_symmetric_block(block_begin, block_size, + "Kernel::row_wise_sum_symmetric_block (slower)!"); + return Kernel::row_wise_sum_symmetric_block(block_begin, block_size, no_diag); } @@ -254,7 +252,7 @@ SGVector CCustomKernel::row_wise_sum_symmetric_block(index_t return sum; } -SGMatrix CCustomKernel::row_wise_sum_squared_sum_symmetric_block( +SGMatrix CustomKernel::row_wise_sum_squared_sum_symmetric_block( index_t block_begin, index_t block_size, bool no_diag) { SG_TRACE("Entering"); @@ -262,8 +260,8 @@ SGMatrix CCustomKernel::row_wise_sum_squared_sum_symmetric_block( if (m_row_subset_stack->has_subsets() || m_col_subset_stack->has_subsets()) { io::info("Row/col subsets initialized! Falling back to " - "CKernel::row_wise_sum_squared_sum_symmetric_block (slower)!"); - return CKernel::row_wise_sum_squared_sum_symmetric_block(block_begin, + "Kernel::row_wise_sum_squared_sum_symmetric_block (slower)!"); + return Kernel::row_wise_sum_squared_sum_symmetric_block(block_begin, block_size, no_diag); } @@ -299,7 +297,7 @@ SGMatrix CCustomKernel::row_wise_sum_squared_sum_symmetric_block( return row_sum; } -SGVector CCustomKernel::row_col_wise_sum_block(index_t +SGVector CustomKernel::row_col_wise_sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag) { @@ -308,8 +306,8 @@ SGVector CCustomKernel::row_col_wise_sum_block(index_t if (m_row_subset_stack->has_subsets() || m_col_subset_stack->has_subsets()) { io::info("Row/col subsets initialized! Falling back to " - "CKernel::row_col_wise_sum_block (slower)!"); - return CKernel::row_col_wise_sum_block(block_begin_row, block_begin_col, + "Kernel::row_col_wise_sum_block (slower)!"); + return Kernel::row_col_wise_sum_block(block_begin_row, block_begin_col, block_size_row, block_size_col, no_diag); } @@ -357,7 +355,7 @@ SGVector CCustomKernel::row_col_wise_sum_block(index_t return sum; } -void CCustomKernel::cleanup_custom() +void CustomKernel::cleanup_custom() { SG_TRACE("Entering"); remove_all_row_subsets(); @@ -369,37 +367,37 @@ void CCustomKernel::cleanup_custom() SG_TRACE("Leaving"); } -void CCustomKernel::cleanup() +void CustomKernel::cleanup() { cleanup_custom(); - CKernel::cleanup(); + Kernel::cleanup(); } -void CCustomKernel::add_row_subset(SGVector subset) +void CustomKernel::add_row_subset(SGVector subset) { m_row_subset_stack->add_subset(subset); row_subset_changed_post(); } -void CCustomKernel::add_row_subset_in_place(SGVector subset) +void CustomKernel::add_row_subset_in_place(SGVector subset) { m_row_subset_stack->add_subset_in_place(subset); row_subset_changed_post(); } -void CCustomKernel::remove_row_subset() +void CustomKernel::remove_row_subset() { m_row_subset_stack->remove_subset(); row_subset_changed_post(); } -void CCustomKernel::remove_all_row_subsets() +void CustomKernel::remove_all_row_subsets() { m_row_subset_stack->remove_all_subsets(); row_subset_changed_post(); } -void CCustomKernel::row_subset_changed_post() +void CustomKernel::row_subset_changed_post() { if (m_row_subset_stack->has_subsets()) num_lhs=m_row_subset_stack->get_size(); @@ -407,31 +405,31 @@ void CCustomKernel::row_subset_changed_post() num_lhs=kmatrix.num_rows; } -void CCustomKernel::add_col_subset(SGVector subset) +void CustomKernel::add_col_subset(SGVector subset) { m_col_subset_stack->add_subset(subset); col_subset_changed_post(); } -void CCustomKernel::add_col_subset_in_place(SGVector subset) +void CustomKernel::add_col_subset_in_place(SGVector subset) { m_col_subset_stack->add_subset_in_place(subset); col_subset_changed_post(); } -void CCustomKernel::remove_col_subset() +void CustomKernel::remove_col_subset() { m_col_subset_stack->remove_subset(); col_subset_changed_post(); } -void CCustomKernel::remove_all_col_subsets() +void CustomKernel::remove_all_col_subsets() { m_col_subset_stack->remove_all_subsets(); col_subset_changed_post(); } -void CCustomKernel::col_subset_changed_post() +void CustomKernel::col_subset_changed_post() { if (m_col_subset_stack->has_subsets()) num_rhs=m_col_subset_stack->get_size(); diff --git a/src/shogun/kernel/CustomKernel.h b/src/shogun/kernel/CustomKernel.h index c4fd674d2ec..12e099317a9 100644 --- a/src/shogun/kernel/CustomKernel.h +++ b/src/shogun/kernel/CustomKernel.h @@ -25,24 +25,24 @@ namespace shogun * representation. Also note that values are stored as 32bit floats. * * The custom kernel supports subsets each on the rows and the columns. See - * documentation in CFeatures, CLabels how this works. The interface is similar. + * documentation in Features, Labels how this works. The interface is similar. * * */ -class CCustomKernel: public CKernel +class CustomKernel: public Kernel { void init(); public: /** default constructor */ - CCustomKernel(); + CustomKernel(); /** constructor * * compute custom kernel from given kernel matrix * @param k kernel matrix */ - CCustomKernel(CKernel* k); + CustomKernel(std::shared_ptr k); /** constructor * @@ -51,7 +51,7 @@ class CCustomKernel: public CKernel * * @param km kernel matrix */ - CCustomKernel(SGMatrix km); + CustomKernel(SGMatrix km); /** constructor * @@ -60,12 +60,12 @@ class CCustomKernel: public CKernel * * @param km kernel matrix */ - CCustomKernel(SGMatrix km); + CustomKernel(SGMatrix km); /** * */ - virtual ~CCustomKernel(); + virtual ~CustomKernel(); /** initialize kernel with dummy features * @@ -89,7 +89,7 @@ class CCustomKernel: public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -317,9 +317,9 @@ class CCustomKernel: public CKernel } /** - * Overrides the sum_symmetric_block method of CKernel to compute the + * Overrides the sum_symmetric_block method of Kernel to compute the * sum directly from the precomputed kernel matrix. - * (Falls back to CKernel method if subsets are specified). + * (Falls back to Kernel method if subsets are specified). * * @param block_begin the row and col index at which the block starts * @param block_size the number of rows and cols in the block @@ -336,9 +336,9 @@ class CCustomKernel: public CKernel index_t block_size, bool no_diag=true); /** - * Overrides the sum_block method of CKernel to compute the + * Overrides the sum_block method of Kernel to compute the * sum directly from the precomputed kernel matrix. - * (Falls back to CKernel method if subsets are specified). + * (Falls back to Kernel method if subsets are specified). * * @param block_begin_row the row index at which the block starts * @param block_begin_col the col index at which the block starts @@ -361,9 +361,9 @@ class CCustomKernel: public CKernel index_t block_size_col, bool no_diag=false); /** - * Overrides the row_wise_sum_symmetric_block method of CKernel to compute the + * Overrides the row_wise_sum_symmetric_block method of Kernel to compute the * sum directly from the precomputed kernel matrix. - * (Falls back to CKernel method if subsets are specified). + * (Falls back to Kernel method if subsets are specified). * * @param block_begin the row and col index at which the block starts * @param block_size the number of rows and cols in the block @@ -381,8 +381,8 @@ class CCustomKernel: public CKernel /** * Overrides the row_wise_sum_squared_sum_symmetric_block method of - * CKernel to compute the sum directly from the precomputed kernel matrix. - * (Falls back to CKernel method if subsets are specified). + * Kernel to compute the sum directly from the precomputed kernel matrix. + * (Falls back to Kernel method if subsets are specified). * * @param block_begin the row and col index at which the block starts * @param block_size the number of rows and cols in the block @@ -404,9 +404,9 @@ class CCustomKernel: public CKernel index_t block_begin, index_t block_size, bool no_diag=true); /** - * Overrides the row_wise_sum_block method of CKernel to compute the sum + * Overrides the row_wise_sum_block method of Kernel to compute the sum * directly from the precomputed kernel matrix. - * (Falls back to CKernel method if subsets are specified). + * (Falls back to Kernel method if subsets are specified). * * @param block_begin_row the row index at which the block starts * @param block_begin_col the col index at which the block starts @@ -607,10 +607,10 @@ class CCustomKernel: public CKernel bool m_is_symmetric; /** row subset stack */ - CSubsetStack* m_row_subset_stack; + std::shared_ptr m_row_subset_stack; /** column subset stack */ - CSubsetStack* m_col_subset_stack; + std::shared_ptr m_col_subset_stack; /** indicates whether kernel matrix is to be freed in destructor */ bool m_free_km; diff --git a/src/shogun/kernel/DiagKernel.cpp b/src/shogun/kernel/DiagKernel.cpp index f5904eaa3d9..0fc2af7ad40 100644 --- a/src/shogun/kernel/DiagKernel.cpp +++ b/src/shogun/kernel/DiagKernel.cpp @@ -13,38 +13,38 @@ using namespace shogun; -CDiagKernel::CDiagKernel() -: CKernel() +DiagKernel::DiagKernel() +: Kernel() { init(); } -CDiagKernel::CDiagKernel(int32_t size, float64_t d) -: CKernel(size) +DiagKernel::DiagKernel(int32_t size, float64_t d) +: Kernel(size) { init(); diag=d; } -CDiagKernel::CDiagKernel(CFeatures* l, CFeatures* r, float64_t d) -: CKernel() +DiagKernel::DiagKernel(std::shared_ptr l, std::shared_ptr r, float64_t d) +: Kernel() { init(); diag=d; init(l, r); } -CDiagKernel::~CDiagKernel() +DiagKernel::~DiagKernel() { } -bool CDiagKernel::init(CFeatures* l, CFeatures* r) +bool DiagKernel::init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l, r); + Kernel::init(l, r); return init_normalizer(); } -void CDiagKernel::init() +void DiagKernel::init() { diag=1.0; SG_ADD(&diag, "diag", "Value on kernel diagonal.", ParameterProperties::HYPER); diff --git a/src/shogun/kernel/DiagKernel.h b/src/shogun/kernel/DiagKernel.h index c3222d89597..f1b871e8442 100644 --- a/src/shogun/kernel/DiagKernel.h +++ b/src/shogun/kernel/DiagKernel.h @@ -21,18 +21,18 @@ namespace shogun * constant otherwise, i.e.\f$k({\bf x_i}, {\bf x_j})= \delta_{ij} c\f$ * */ -class CDiagKernel: public CKernel +class DiagKernel: public Kernel { public: /** default constructor */ - CDiagKernel(); + DiagKernel(); /** constructor * * @param size cache size * @param diag diagonal */ - CDiagKernel(int32_t size, float64_t diag=1.0); + DiagKernel(int32_t size, float64_t diag=1.0); /** constructor * @@ -40,9 +40,9 @@ class CDiagKernel: public CKernel * @param r features of right-hand side * @param diag diagonal */ - CDiagKernel(CFeatures* l, CFeatures* r, float64_t diag=1.0); + DiagKernel(std::shared_ptr l, std::shared_ptr r, float64_t diag=1.0); - virtual ~CDiagKernel(); + virtual ~DiagKernel(); /** initialize kernel * @@ -50,7 +50,7 @@ class CDiagKernel: public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return feature type the kernel can deal with * diff --git a/src/shogun/kernel/DirectorKernel.h b/src/shogun/kernel/DirectorKernel.h index 8830f3585cf..a22d1601129 100644 --- a/src/shogun/kernel/DirectorKernel.h +++ b/src/shogun/kernel/DirectorKernel.h @@ -18,36 +18,36 @@ namespace shogun { #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel +IGNORE_IN_CLASSLIST class DirectorKernel: public Kernel { public: /** default constructor * */ - CDirectorKernel() - : CKernel(), external_features(false) + DirectorKernel() + : Kernel(), external_features(false) { } /** */ - CDirectorKernel(bool is_external_features) - : CKernel(), external_features(is_external_features) + DirectorKernel(bool is_external_features) + : Kernel(), external_features(is_external_features) { } /** constructor * */ - CDirectorKernel(int32_t size, bool is_external_features) - : CKernel(size), external_features(is_external_features) + DirectorKernel(int32_t size, bool is_external_features) + : Kernel(size), external_features(is_external_features) { } /** default constructor * */ - virtual ~CDirectorKernel() + virtual ~DirectorKernel() { cleanup(); } @@ -58,32 +58,32 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { if (env()->get_num_threads()!=1) { io::warn("Enforcing to use only one thread due to restrictions of directors"); env()->set_num_threads(1); } - return CKernel::init(l, r); + return Kernel::init(l, r); } /** set the current kernel normalizer * * @return if successful */ - virtual bool set_normalizer(CKernelNormalizer* normalizer) + virtual bool set_normalizer(std::shared_ptr normalizer) { - return CKernel::set_normalizer(normalizer); + return Kernel::set_normalizer(normalizer); } /** obtain the current kernel normalizer * * @return the kernel normalizer */ - virtual CKernelNormalizer* get_normalizer() + virtual std::shared_ptr get_normalizer() { - return CKernel::get_normalizer(); + return Kernel::get_normalizer(); } /** initialize the current kernel normalizer @@ -91,18 +91,18 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual bool init_normalizer() { - return CKernel::init_normalizer(); + return Kernel::init_normalizer(); } /** clean up your kernel * * base method only removes lhs and rhs - * overload to add further cleanup but make sure CKernel::cleanup() is + * overload to add further cleanup but make sure Kernel::cleanup() is * called */ virtual void cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } virtual float64_t kernel_function(int32_t idx_a, int32_t idx_b) @@ -118,7 +118,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual SGVector get_kernel_col(int32_t j) { - return CKernel::get_kernel_col(j); + return Kernel::get_kernel_col(j); } /** @@ -128,7 +128,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual SGVector get_kernel_row(int32_t i) { - return CKernel::get_kernel_row(i); + return Kernel::get_kernel_row(i); } /** get number of vectors of lhs features @@ -137,7 +137,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual int32_t get_num_vec_lhs() { - return CKernel::get_num_vec_lhs(); + return Kernel::get_num_vec_lhs(); } /** get number of vectors of rhs features @@ -146,7 +146,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual int32_t get_num_vec_rhs() { - return CKernel::get_num_vec_rhs(); + return Kernel::get_num_vec_rhs(); } /** set number of vectors of lhs features @@ -174,7 +174,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel virtual bool has_features() { if (!external_features) - return CKernel::has_features(); + return Kernel::has_features(); else return true; } @@ -182,19 +182,19 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel /** remove lhs and rhs from kernel */ virtual void remove_lhs_and_rhs() { - CKernel::remove_lhs_and_rhs(); + Kernel::remove_lhs_and_rhs(); } /** remove lhs from kernel */ virtual void remove_lhs() { - CKernel::remove_lhs(); + Kernel::remove_lhs(); } /** remove rhs from kernel */ virtual void remove_rhs() { - CKernel::remove_rhs(); + Kernel::remove_rhs(); } /** return what type of kernel we are @@ -226,7 +226,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void clear_normal() { - CKernel::clear_normal(); + Kernel::clear_normal(); } /** add vector*factor to 'virtual' normal vector @@ -236,7 +236,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void add_to_normal(int32_t vector_idx, float64_t weight) { - CKernel::add_to_normal(vector_idx, weight); + Kernel::add_to_normal(vector_idx, weight); } /** set optimization type @@ -245,7 +245,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void set_optimization_type(EOptimizationType t) { - CKernel::set_optimization_type(t); + Kernel::set_optimization_type(t); } /** initialize optimization @@ -258,7 +258,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel virtual bool init_optimization( int32_t count, int32_t *IDX, float64_t *weights) { - return CKernel::init_optimization(count, IDX, weights); + return Kernel::init_optimization(count, IDX, weights); } /** delete optimization @@ -267,7 +267,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual bool delete_optimization() { - return CKernel::delete_optimization(); + return Kernel::delete_optimization(); } /** compute optimized @@ -277,7 +277,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual float64_t compute_optimized(int32_t vector_idx) { - return CKernel::compute_optimized(vector_idx); + return Kernel::compute_optimized(vector_idx); } /** computes output for a batch of examples in an optimized fashion @@ -293,7 +293,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel int32_t num_suppvec, int32_t* IDX, float64_t* alphas, float64_t factor=1.0) { - CKernel::compute_batch(num_vec, vec_idx, target, num_suppvec, IDX, alphas, factor); + Kernel::compute_batch(num_vec, vec_idx, target, num_suppvec, IDX, alphas, factor); } /** get number of subkernels @@ -302,7 +302,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual int32_t get_num_subkernels() { - return CKernel::get_num_subkernels(); + return Kernel::get_num_subkernels(); } /** compute by subkernel @@ -313,7 +313,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel virtual void compute_by_subkernel( int32_t vector_idx, float64_t * subkernel_contrib) { - CKernel::compute_by_subkernel(vector_idx, subkernel_contrib); + Kernel::compute_by_subkernel(vector_idx, subkernel_contrib); } /** get subkernel weights @@ -323,7 +323,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual const float64_t* get_subkernel_weights(int32_t& num_weights) { - return CKernel::get_subkernel_weights(num_weights); + return Kernel::get_subkernel_weights(num_weights); } /** set subkernel weights @@ -332,7 +332,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void set_subkernel_weights(SGVector weights) { - CKernel::set_subkernel_weights(weights); + Kernel::set_subkernel_weights(weights); } /** Can (optionally) be overridden to pre-initialize some member @@ -345,7 +345,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void load_serializable_pre() noexcept(false) { - CKernel::load_serializable_pre(); + Kernel::load_serializable_pre(); } /** Can (optionally) be overridden to post-initialize some member @@ -358,7 +358,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void load_serializable_post() noexcept(false) { - CKernel::load_serializable_post(); + Kernel::load_serializable_post(); } /** Can (optionally) be overridden to pre-initialize some member @@ -371,7 +371,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void save_serializable_pre() noexcept(false) { - CKernel::save_serializable_pre(); + Kernel::save_serializable_pre(); } /** Can (optionally) be overridden to post-initialize some member @@ -384,7 +384,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel */ virtual void save_serializable_post() noexcept(false) { - CKernel::save_serializable_post(); + Kernel::save_serializable_post(); } protected: @@ -403,7 +403,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernel: public CKernel virtual void register_params() { - CKernel::register_params(); + Kernel::register_params(); } protected: diff --git a/src/shogun/kernel/DistanceKernel.cpp b/src/shogun/kernel/DistanceKernel.cpp index ee4c06af764..c2154a26e00 100644 --- a/src/shogun/kernel/DistanceKernel.cpp +++ b/src/shogun/kernel/DistanceKernel.cpp @@ -13,55 +13,55 @@ using namespace shogun; -CDistanceKernel::CDistanceKernel() -: CKernel(0), distance(NULL), width(0.0) +DistanceKernel::DistanceKernel() +: Kernel(0), distance(NULL), width(0.0) { register_params(); } -CDistanceKernel::CDistanceKernel(int32_t size, float64_t w, CDistance* d) -: CKernel(size), distance(d) +DistanceKernel::DistanceKernel(int32_t size, float64_t w, std::shared_ptr d) +: Kernel(size), distance(d) { ASSERT(distance) set_width(w); - SG_REF(distance); + register_params(); } -CDistanceKernel::CDistanceKernel( - CFeatures *l, CFeatures *r, float64_t w , CDistance* d) -: CKernel(10), distance(d) +DistanceKernel::DistanceKernel( + std::shared_ptrl, std::shared_ptrr, float64_t w , std::shared_ptr d) +: Kernel(10), distance(d) { set_width(w); ASSERT(distance) - SG_REF(distance); + init(l, r); register_params(); } -CDistanceKernel::~CDistanceKernel() +DistanceKernel::~DistanceKernel() { - // important to have the cleanup of CKernel first, it calls get_name which + // important to have the cleanup of Kernel first, it calls get_name which // uses the distance cleanup(); - SG_UNREF(distance); + } -bool CDistanceKernel::init(CFeatures* l, CFeatures* r) +bool DistanceKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(distance) - CKernel::init(l,r); + Kernel::init(l,r); distance->init(l,r); return init_normalizer(); } -float64_t CDistanceKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t DistanceKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t result=distance->distance(idx_a, idx_b); return exp(-result/width); } -void CDistanceKernel::register_params() +void DistanceKernel::register_params() { SG_ADD(&width, "width", "Kernel width.", ParameterProperties::HYPER); SG_ADD(&distance, "distance", "Distance to be used.", diff --git a/src/shogun/kernel/DistanceKernel.h b/src/shogun/kernel/DistanceKernel.h index 7d9a55d279b..5ede905fff0 100644 --- a/src/shogun/kernel/DistanceKernel.h +++ b/src/shogun/kernel/DistanceKernel.h @@ -15,7 +15,7 @@ namespace shogun { - class CDistance; + class Distance; /** @brief The Distance kernel takes a distance as input. * @@ -25,11 +25,11 @@ namespace shogun * k({\bf x}, {\bf x'}) = e^{-\frac{dist({\bf x}, {\bf x'})}{width}} * \f] */ -class CDistanceKernel: public CKernel +class DistanceKernel: public Kernel { public: /** default constructor */ - CDistanceKernel(); + DistanceKernel(); /** constructor * @@ -37,7 +37,7 @@ class CDistanceKernel: public CKernel * @param width width * @param dist distance */ - CDistanceKernel(int32_t cache, float64_t width, CDistance* dist); + DistanceKernel(int32_t cache, float64_t width, std::shared_ptr dist); /** constructor * @@ -46,10 +46,10 @@ class CDistanceKernel: public CKernel * @param width width * @param dist distance */ - CDistanceKernel( - CFeatures *l, CFeatures *r, float64_t width, CDistance* dist); + DistanceKernel( + std::shared_ptrl, std::shared_ptrr, float64_t width, std::shared_ptr dist); - virtual ~CDistanceKernel(); + virtual ~DistanceKernel(); /** initialize kernel * @@ -57,7 +57,7 @@ class CDistanceKernel: public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** register the parameters (serialization support) * @@ -118,7 +118,7 @@ class CDistanceKernel: public CKernel float64_t compute(int32_t idx_a, int32_t idx_b); /** distance */ - CDistance* distance; + std::shared_ptr distance; /** width */ float64_t width; }; diff --git a/src/shogun/kernel/DotKernel.h b/src/shogun/kernel/DotKernel.h index 19fb0decb7f..08dbb32c2aa 100644 --- a/src/shogun/kernel/DotKernel.h +++ b/src/shogun/kernel/DotKernel.h @@ -18,32 +18,32 @@ namespace shogun /** @brief Template class DotKernel is the base class for kernels working on * DotFeatures. * - * CDotFeatures are features supporting operations like dot product, dot product + * DotFeatures are features supporting operations like dot product, dot product * with a dense vector and addition to a dense vector. Therefore several dot - * product based kernels derive from this class (cf., e.g., CLinearKernel) + * product based kernels derive from this class (cf., e.g., LinearKernel) * - * \sa CDotFeatures + * \sa DotFeatures */ -class CDotKernel : public CKernel +class DotKernel : public Kernel { public: /** default constructor * */ - CDotKernel() : CKernel() {} + DotKernel() : Kernel() {} /** constructor * * @param cachesize cache size */ - CDotKernel(int32_t cachesize) : CKernel(cachesize) {} + DotKernel(int32_t cachesize) : Kernel(cachesize) {} /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CDotKernel(CFeatures* l, CFeatures* r) : CKernel(10) + DotKernel(std::shared_ptr l, std::shared_ptr r) : Kernel(10) { init(l, r); } @@ -58,9 +58,9 @@ class CDotKernel : public CKernel * @param r features for right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l,r); + Kernel::init(l,r); init_auto_params(); ASSERT(l->has_property(FP_DOT)) @@ -79,10 +79,11 @@ class CDotKernel : public CKernel l->get_name(), r->get_name()); } - if ( ((CDotFeatures*) l)->get_dim_feature_space() != ((CDotFeatures*) r)->get_dim_feature_space() ) + if ( (std::static_pointer_cast(l))->get_dim_feature_space() != (std::static_pointer_cast(r))->get_dim_feature_space() ) { error("train or test features #dimension mismatch (l:{} vs. r:{})", - ((CDotFeatures*) l)->get_dim_feature_space(),((CDotFeatures*) r)->get_dim_feature_space()); + (std::static_pointer_cast(l))->get_dim_feature_space(), + (std::static_pointer_cast(r))->get_dim_feature_space()); } return true; } @@ -130,7 +131,7 @@ class CDotKernel : public CKernel */ virtual float64_t compute(int32_t idx_a, int32_t idx_b) { - return ((CDotFeatures*) lhs)->dot(idx_a, ((CDotFeatures*) rhs), idx_b); + return (std::static_pointer_cast(lhs))->dot(idx_a, (std::static_pointer_cast(rhs)), idx_b); } }; } diff --git a/src/shogun/kernel/ExponentialARDKernel.cpp b/src/shogun/kernel/ExponentialARDKernel.cpp index 77d43f54b24..b3fb93f4b77 100644 --- a/src/shogun/kernel/ExponentialARDKernel.cpp +++ b/src/shogun/kernel/ExponentialARDKernel.cpp @@ -11,17 +11,17 @@ using namespace shogun; -CExponentialARDKernel::CExponentialARDKernel() : CDotKernel() +ExponentialARDKernel::ExponentialARDKernel() : DotKernel() { init(); } -CExponentialARDKernel::~CExponentialARDKernel() +ExponentialARDKernel::~ExponentialARDKernel() { - CKernel::cleanup(); + Kernel::cleanup(); } -void CExponentialARDKernel::init() +void ExponentialARDKernel::init() { m_ARD_type=KT_SCALAR; @@ -44,21 +44,21 @@ void CExponentialARDKernel::init() } -SGVector CExponentialARDKernel::get_feature_vector(int32_t idx, CFeatures* hs) +SGVector ExponentialARDKernel::get_feature_vector(int32_t idx, std::shared_ptr hs) { require(hs, "Features not set!"); - CDenseFeatures * dense_hs=dynamic_cast *>(hs); + auto dense_hs=std::dynamic_pointer_cast>(hs); if (dense_hs) return dense_hs->get_feature_vector(idx); - CDotFeatures * dot_hs=dynamic_cast(hs); + auto dot_hs=std::dynamic_pointer_cast(hs); require(dot_hs, "Kernel only supports DotFeatures"); return dot_hs->get_computed_dot_feature_vector(idx); } -void CExponentialARDKernel::set_weights(SGMatrix weights) +void ExponentialARDKernel::set_weights(SGMatrix weights) { require(weights.num_rows>0 && weights.num_cols>0, "Weights matrix is non-empty"); if (weights.num_rows==1) @@ -75,7 +75,7 @@ void CExponentialARDKernel::set_weights(SGMatrix weights) set_matrix_weights(weights); } -void CExponentialARDKernel::lazy_update_weights() +void ExponentialARDKernel::lazy_update_weights() { if (parameter_hash_changed()) { @@ -105,13 +105,13 @@ void CExponentialARDKernel::lazy_update_weights() } } -SGMatrix CExponentialARDKernel::get_weights() +SGMatrix ExponentialARDKernel::get_weights() { lazy_update_weights(); return SGMatrix(m_weights_raw); } -void CExponentialARDKernel::set_scalar_weights(float64_t weight) +void ExponentialARDKernel::set_scalar_weights(float64_t weight) { require(weight>0, "Scalar ({}) weight should be positive",weight); m_log_weights=SGVector(1); @@ -122,7 +122,7 @@ void CExponentialARDKernel::set_scalar_weights(float64_t weight) m_weights_cols=1.0; } -void CExponentialARDKernel::set_vector_weights(SGVector weights) +void ExponentialARDKernel::set_vector_weights(SGVector weights) { require(rhs==NULL && lhs==NULL, "Setting vector weights must be before initialize features"); @@ -140,7 +140,7 @@ void CExponentialARDKernel::set_vector_weights(SGVector weights) m_weights_cols=weights.vlen; } -void CExponentialARDKernel::set_matrix_weights(SGMatrix weights) +void ExponentialARDKernel::set_matrix_weights(SGMatrix weights) { require(rhs==NULL && lhs==NULL, "Setting matrix weights must be before initialize features"); @@ -167,23 +167,23 @@ void CExponentialARDKernel::set_matrix_weights(SGMatrix weights) } } -CExponentialARDKernel::CExponentialARDKernel(int32_t size) : CDotKernel(size) +ExponentialARDKernel::ExponentialARDKernel(int32_t size) : DotKernel(size) { init(); } -CExponentialARDKernel::CExponentialARDKernel(CDotFeatures* l, - CDotFeatures* r, int32_t size) : CDotKernel(size) +ExponentialARDKernel::ExponentialARDKernel(std::shared_ptr l, + std::shared_ptr r, int32_t size) : DotKernel(size) { init(); init(l,r); } -bool CExponentialARDKernel::init(CFeatures* l, CFeatures* r) +bool ExponentialARDKernel::init(std::shared_ptr l, std::shared_ptr r) { cleanup(); - CDotKernel::init(l, r); - int32_t dim=((CDotFeatures*) l)->get_dim_feature_space(); + DotKernel::init(l, r); + int32_t dim=(std::static_pointer_cast(l))->get_dim_feature_space(); if (m_ARD_type==KT_FULL) { require(m_weights_rows==dim, "Dimension mismatch between features ({}) and weights ({})", @@ -198,7 +198,7 @@ bool CExponentialARDKernel::init(CFeatures* l, CFeatures* r) } -SGMatrix CExponentialARDKernel::get_weighted_vector(SGVector vec) +SGMatrix ExponentialARDKernel::get_weighted_vector(SGVector vec) { require(m_ARD_type==KT_FULL || m_ARD_type==KT_DIAG, "This method only supports vector weights or matrix weights"); SGMatrix res; @@ -230,7 +230,7 @@ SGMatrix CExponentialARDKernel::get_weighted_vector(SGVector CExponentialARDKernel::compute_right_product(SGVectorvec, +SGMatrix ExponentialARDKernel::compute_right_product(SGVectorvec, float64_t & scalar_weight) { SGMatrix right; @@ -249,7 +249,7 @@ SGMatrix CExponentialARDKernel::compute_right_product(SGVector get_feature_vector(int32_t idx, CFeatures* hs); + virtual SGVector get_feature_vector(int32_t idx, std::shared_ptr hs); /** compute the distance between features a and b * idx_{a,b} denote the index of the feature vectors @@ -149,7 +149,7 @@ class CExponentialARDKernel: public CDotKernel * * @param size cache size */ - CExponentialARDKernel(int32_t size); + ExponentialARDKernel(int32_t size); /** constructor * @@ -157,7 +157,7 @@ class CExponentialARDKernel: public CDotKernel * @param r features of right-hand side * @param size cache size */ - CExponentialARDKernel(CDotFeatures* l, CDotFeatures* r, + ExponentialARDKernel(std::shared_ptr l, std::shared_ptr r, int32_t size=10); @@ -167,7 +167,7 @@ class CExponentialARDKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return current feature/dimension weights in matrix form diff --git a/src/shogun/kernel/ExponentialKernel.cpp b/src/shogun/kernel/ExponentialKernel.cpp index 167a36c4837..38ca0f00a2f 100644 --- a/src/shogun/kernel/ExponentialKernel.cpp +++ b/src/shogun/kernel/ExponentialKernel.cpp @@ -12,55 +12,55 @@ using namespace shogun; -CExponentialKernel::CExponentialKernel() - : CDotKernel(), m_distance(NULL), m_width(1) +ExponentialKernel::ExponentialKernel() + : DotKernel(), m_distance(NULL), m_width(1) { init(); } -CExponentialKernel::CExponentialKernel( - CDotFeatures* l, CDotFeatures* r, float64_t width, CDistance* distance, int32_t size) -: CDotKernel(size), m_distance(distance), m_width(width) +ExponentialKernel::ExponentialKernel( + std::shared_ptr l, std::shared_ptr r, float64_t width, std::shared_ptr distance, int32_t size) +: DotKernel(size), m_distance(distance), m_width(width) { init(); ASSERT(distance) - SG_REF(distance); + init(l,r); } -CExponentialKernel::~CExponentialKernel() +ExponentialKernel::~ExponentialKernel() { cleanup(); - SG_UNREF(m_distance); + } -void CExponentialKernel::cleanup() +void ExponentialKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -bool CExponentialKernel::init(CFeatures* l, CFeatures* r) +bool ExponentialKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(m_distance) - CDotKernel::init(l, r); + DotKernel::init(l, r); m_distance->init(l, r); return init_normalizer(); } -float64_t CExponentialKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t ExponentialKernel::compute(int32_t idx_a, int32_t idx_b) { ASSERT(m_distance) float64_t dist=m_distance->distance(idx_a, idx_b); return exp(-dist/m_width); } -void CExponentialKernel::load_serializable_post() noexcept(false) +void ExponentialKernel::load_serializable_post() noexcept(false) { - CKernel::load_serializable_post(); + Kernel::load_serializable_post(); } -void CExponentialKernel::init() +void ExponentialKernel::init() { SG_ADD(&m_width, "width", "Kernel width.", ParameterProperties::HYPER); SG_ADD(&m_distance, "distance", "Distance to be used.", diff --git a/src/shogun/kernel/ExponentialKernel.h b/src/shogun/kernel/ExponentialKernel.h index 4495b03af6c..ad82a0f543d 100644 --- a/src/shogun/kernel/ExponentialKernel.h +++ b/src/shogun/kernel/ExponentialKernel.h @@ -16,9 +16,9 @@ namespace shogun { - class CDotFeatures; + class DotFeatures; /** @brief The Exponential Kernel, closely related to the Gaussian Kernel - * computed on CDotFeatures. + * computed on DotFeatures. * * It is computed as * @@ -28,13 +28,13 @@ namespace shogun * * where \f$\tau\f$ is the kernel width. */ -class CExponentialKernel: public CDotKernel +class ExponentialKernel: public DotKernel { public: /** default constructor * */ - CExponentialKernel(); + ExponentialKernel(); /** constructor * @@ -44,11 +44,11 @@ class CExponentialKernel: public CDotKernel * @param distance distance to be used * @param size cache size */ - CExponentialKernel(CDotFeatures* l, CDotFeatures* r, - float64_t width, CDistance* distance, int32_t size); + ExponentialKernel(std::shared_ptr l, std::shared_ptr r, + float64_t width, std::shared_ptr distance, int32_t size); /** destructor */ - virtual ~CExponentialKernel(); + virtual ~ExponentialKernel(); /** initialize kernel * @@ -56,7 +56,7 @@ class CExponentialKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -108,7 +108,7 @@ class CExponentialKernel: public CDotKernel protected: /** distance **/ - CDistance* m_distance; + std::shared_ptr m_distance; /** width */ float64_t m_width; }; diff --git a/src/shogun/kernel/GaussianARDKernel.cpp b/src/shogun/kernel/GaussianARDKernel.cpp index 9e2b0398bc1..35b2caa5883 100644 --- a/src/shogun/kernel/GaussianARDKernel.cpp +++ b/src/shogun/kernel/GaussianARDKernel.cpp @@ -11,16 +11,16 @@ using namespace shogun; -CGaussianARDKernel::CGaussianARDKernel() : CExponentialARDKernel() +GaussianARDKernel::GaussianARDKernel() : ExponentialARDKernel() { init(); } -CGaussianARDKernel::~CGaussianARDKernel() +GaussianARDKernel::~GaussianARDKernel() { } -void CGaussianARDKernel::init() +void GaussianARDKernel::init() { m_sq_lhs=SGVector(); m_sq_rhs=SGVector(); @@ -28,7 +28,7 @@ void CGaussianARDKernel::init() SG_ADD(&m_sq_rhs, "sq_rhs", "squared right-hand side"); } -float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b) +float64_t GaussianARDKernel::distance(int32_t idx_a, int32_t idx_b) { float64_t result=0.0; require(lhs, "Left features (lhs) not set!"); @@ -39,7 +39,7 @@ float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b) if (m_ARD_type==KT_SCALAR) { - result=(m_sq_lhs[idx_a]+m_sq_rhs[idx_b]-2.0*CDotKernel::compute(idx_a,idx_b)); + result=(m_sq_lhs[idx_a]+m_sq_rhs[idx_b]-2.0*DotKernel::compute(idx_a,idx_b)); result *= std::exp(2.0 * m_log_weights[0]); } else @@ -52,22 +52,22 @@ float64_t CGaussianARDKernel::distance(int32_t idx_a, int32_t idx_b) return result * 0.5; } -CGaussianARDKernel::CGaussianARDKernel(int32_t size) - : CExponentialARDKernel(size) +GaussianARDKernel::GaussianARDKernel(int32_t size) + : ExponentialARDKernel(size) { init(); } -CGaussianARDKernel::CGaussianARDKernel(CDotFeatures* l, - CDotFeatures* r, int32_t size) - : CExponentialARDKernel(size) +GaussianARDKernel::GaussianARDKernel(std::shared_ptr l, + std::shared_ptr r, int32_t size) + : ExponentialARDKernel(size) { init(); } -bool CGaussianARDKernel::init(CFeatures* l, CFeatures* r) +bool GaussianARDKernel::init(std::shared_ptr l, std::shared_ptr r) { - bool status=CExponentialARDKernel::init(l,r); + bool status=ExponentialARDKernel::init(l,r); if (m_ARD_type==KT_SCALAR) precompute_squared(); @@ -75,7 +75,7 @@ bool CGaussianARDKernel::init(CFeatures* l, CFeatures* r) return status; } -SGVector CGaussianARDKernel::precompute_squared_helper(CDotFeatures* df) +SGVector GaussianARDKernel::precompute_squared_helper(std::shared_ptr df) { require(df, "Features not set"); int32_t num_vec=df->get_num_vectors(); @@ -85,32 +85,32 @@ SGVector CGaussianARDKernel::precompute_squared_helper(CDotFeatures* return sq; } -void CGaussianARDKernel::precompute_squared() +void GaussianARDKernel::precompute_squared() { if (!lhs || !rhs) return; - m_sq_lhs=precompute_squared_helper((CDotFeatures*) lhs); + m_sq_lhs=precompute_squared_helper(std::static_pointer_cast(lhs)); if (lhs==rhs) m_sq_rhs=m_sq_lhs; else - m_sq_rhs=precompute_squared_helper((CDotFeatures*) rhs); + m_sq_rhs=precompute_squared_helper(std::static_pointer_cast(rhs)); } -CGaussianARDKernel* CGaussianARDKernel::obtain_from_generic(CKernel* kernel) +std::shared_ptr GaussianARDKernel::obtain_from_generic(std::shared_ptr kernel) { if (kernel->get_kernel_type()!=K_GAUSSIANARD) { - error("Provided kernel is not of type CGaussianARDKernel!"); + error("Provided kernel is not of type GaussianARDKernel!"); } /* since an additional reference is returned */ - SG_REF(kernel); - return (CGaussianARDKernel*)kernel; + + return std::static_pointer_cast(kernel); } -float64_t CGaussianARDKernel::compute_helper(SGVector avec, SGVectorbvec) +float64_t GaussianARDKernel::compute_helper(SGVector avec, SGVectorbvec) { SGMatrix left; SGMatrix left_transpose; @@ -132,7 +132,7 @@ float64_t CGaussianARDKernel::compute_helper(SGVector avec, SGVector< return res[0]*scalar_weight; } -float64_t CGaussianARDKernel::compute_gradient_helper(SGVector avec, +float64_t GaussianARDKernel::compute_gradient_helper(SGVector avec, SGVector bvec, float64_t scale, index_t index) { float64_t result=0.0; @@ -197,7 +197,7 @@ float64_t CGaussianARDKernel::compute_gradient_helper(SGVector avec, } -SGVector CGaussianARDKernel::get_parameter_gradient_diagonal( +SGVector GaussianARDKernel::get_parameter_gradient_diagonal( const TParameter* param, index_t index) { require(param, "Param not set"); @@ -215,7 +215,7 @@ SGVector CGaussianARDKernel::get_parameter_gradient_diagonal( } else { - int32_t length=CMath::min(num_lhs, num_rhs); + int32_t length=Math::min(num_lhs, num_rhs); SGVector derivative(length); check_weight_gradient_index(index); for (index_t j=0; j CGaussianARDKernel::get_parameter_gradient_diagonal( } -float64_t CGaussianARDKernel::get_parameter_gradient_helper( +float64_t GaussianARDKernel::get_parameter_gradient_helper( const TParameter* param, index_t index, int32_t idx_a, int32_t idx_b, SGVector avec, SGVector bvec) { @@ -263,7 +263,7 @@ float64_t CGaussianARDKernel::get_parameter_gradient_helper( } } -SGMatrix CGaussianARDKernel::get_parameter_gradient( +SGMatrix GaussianARDKernel::get_parameter_gradient( const TParameter* param, index_t index) { require(param, "Param not set"); diff --git a/src/shogun/kernel/GaussianARDKernel.h b/src/shogun/kernel/GaussianARDKernel.h index ce790cab9b5..fa1e1045c63 100644 --- a/src/shogun/kernel/GaussianARDKernel.h +++ b/src/shogun/kernel/GaussianARDKernel.h @@ -17,7 +17,7 @@ namespace shogun { /** @brief Gaussian Kernel with Automatic Relevance Detection computed - * on CDotFeatures. + * on DotFeatures. * * It is computed as * @@ -51,14 +51,14 @@ namespace shogun * When \f$\Lambda=\lambda I\f$ is, the last case becomes the first case. * When \f$\Lambda=\textbf{diag}(\lambda) \f$ is, the last case becomes the second case. */ -class CGaussianARDKernel: public CExponentialARDKernel +class GaussianARDKernel: public ExponentialARDKernel { public: /** default constructor */ - CGaussianARDKernel(); + GaussianARDKernel(); /** destructor */ - virtual ~CGaussianARDKernel(); + virtual ~GaussianARDKernel(); /** return what type of kernel we are * @@ -94,7 +94,7 @@ class CGaussianARDKernel: public CExponentialARDKernel * @param size cache size * @param width kernel width */ - CGaussianARDKernel(int32_t size); + GaussianARDKernel(int32_t size); /** constructor * @@ -103,14 +103,14 @@ class CGaussianARDKernel: public CExponentialARDKernel * @param size cache size * @param width kernel width */ - CGaussianARDKernel(CDotFeatures* l, CDotFeatures* r, + GaussianARDKernel(std::shared_ptr l, std::shared_ptr r, int32_t size=10); - /** @param kernel is casted to CGaussianARDKernel, error if not possible + /** @param kernel is casted to GaussianARDKernel, error if not possible * is SG_REF'ed - * @return casted CGaussianARDKernel object + * @return casted GaussianARDKernel object */ - static CGaussianARDKernel* obtain_from_generic(CKernel* kernel); + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); /** initialize kernel * @@ -118,7 +118,7 @@ class CGaussianARDKernel: public CExponentialARDKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return derivative with respect to specified parameter * @@ -154,7 +154,7 @@ class CGaussianARDKernel: public CExponentialARDKernel * @param buf buffer to store squared terms (will be allocated) * @param df dot feature object based on which k(i,i) is computed * */ - virtual SGVector precompute_squared_helper(CDotFeatures* df); + virtual SGVector precompute_squared_helper(std::shared_ptr df); /** squared left-hand side */ SGVector m_sq_lhs; diff --git a/src/shogun/kernel/GaussianCompactKernel.cpp b/src/shogun/kernel/GaussianCompactKernel.cpp index 5fb665895a2..15ab802c701 100644 --- a/src/shogun/kernel/GaussianCompactKernel.cpp +++ b/src/shogun/kernel/GaussianCompactKernel.cpp @@ -3,30 +3,30 @@ using namespace shogun; -CGaussianCompactKernel::CGaussianCompactKernel() : CGaussianKernel() +GaussianCompactKernel::GaussianCompactKernel() : GaussianKernel() { } -CGaussianCompactKernel::CGaussianCompactKernel(int32_t size, float64_t width) - : CGaussianKernel(size, width) +GaussianCompactKernel::GaussianCompactKernel(int32_t size, float64_t width) + : GaussianKernel(size, width) { } -CGaussianCompactKernel::CGaussianCompactKernel(CDotFeatures* l, CDotFeatures* r, +GaussianCompactKernel::GaussianCompactKernel(std::shared_ptr l, std::shared_ptr r, float64_t width, int32_t size) - : CGaussianKernel(l, r, + : GaussianKernel(l, r, width, size) { } -CGaussianCompactKernel::~CGaussianCompactKernel() +GaussianCompactKernel::~GaussianCompactKernel() { } -float64_t CGaussianCompactKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t GaussianCompactKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t len_features, power; - len_features=((CDotFeatures*) lhs)->get_dim_feature_space(); + len_features=(std::static_pointer_cast(lhs))->get_dim_feature_space(); power=(len_features%2==0) ? (len_features+1):len_features; float64_t result=distance(idx_a,idx_b); @@ -35,5 +35,5 @@ float64_t CGaussianCompactKernel::compute(int32_t idx_a, int32_t idx_b) if (result_multiplier <= 0) return 0; - return CMath::pow(result_multiplier, power) * std::exp(-result); + return Math::pow(result_multiplier, power) * std::exp(-result); } diff --git a/src/shogun/kernel/GaussianCompactKernel.h b/src/shogun/kernel/GaussianCompactKernel.h index e8778b72cde..38a7bd75476 100644 --- a/src/shogun/kernel/GaussianCompactKernel.h +++ b/src/shogun/kernel/GaussianCompactKernel.h @@ -5,7 +5,7 @@ namespace shogun { -class CDotFeatures; +class DotFeatures; /** @brief The compact version as given in Bart Hamers' thesis * Kernel Models for Large Scale Applications * (Eq. 4.10) is computed as @@ -19,18 +19,18 @@ class CDotFeatures; * */ -class CGaussianCompactKernel: public CGaussianKernel +class GaussianCompactKernel: public GaussianKernel { public: /** default constructor */ - CGaussianCompactKernel(); + GaussianCompactKernel(); /** constructor * * @param size cache size * @param width width */ - CGaussianCompactKernel(int32_t size, float64_t width); + GaussianCompactKernel(int32_t size, float64_t width); /** constructor * @@ -39,11 +39,11 @@ class CGaussianCompactKernel: public CGaussianKernel * @param width width * @param size cache size */ - CGaussianCompactKernel(CDotFeatures* l, CDotFeatures* r, + GaussianCompactKernel(std::shared_ptr l, std::shared_ptr r, float64_t width, int32_t size=10); /* destructor */ - virtual ~CGaussianCompactKernel(); + virtual ~GaussianCompactKernel(); /** return what type of kernel we are * diff --git a/src/shogun/kernel/GaussianKernel.cpp b/src/shogun/kernel/GaussianKernel.cpp index c3c2d51ce44..256f52871df 100644 --- a/src/shogun/kernel/GaussianKernel.cpp +++ b/src/shogun/kernel/GaussianKernel.cpp @@ -14,25 +14,25 @@ using namespace shogun; -CGaussianKernel::CGaussianKernel() : CShiftInvariantKernel() +GaussianKernel::GaussianKernel() : ShiftInvariantKernel() { register_params(); } -CGaussianKernel::CGaussianKernel(float64_t w) : CShiftInvariantKernel() +GaussianKernel::GaussianKernel(float64_t w) : ShiftInvariantKernel() { register_params(); set_width(w); } -CGaussianKernel::CGaussianKernel(int32_t size, float64_t w) : CShiftInvariantKernel() +GaussianKernel::GaussianKernel(int32_t size, float64_t w) : ShiftInvariantKernel() { register_params(); set_width(w); set_cache_size(size); } -CGaussianKernel::CGaussianKernel(CDotFeatures* l, CDotFeatures* r, float64_t w, int32_t size) : CShiftInvariantKernel() +GaussianKernel::GaussianKernel(std::shared_ptr l, std::shared_ptr r, float64_t w, int32_t size) : ShiftInvariantKernel() { register_params(); set_width(w); @@ -40,28 +40,27 @@ CGaussianKernel::CGaussianKernel(CDotFeatures* l, CDotFeatures* r, float64_t w, init(l, r); } -CGaussianKernel::~CGaussianKernel() +GaussianKernel::~GaussianKernel() { cleanup(); } -CGaussianKernel* CGaussianKernel::obtain_from_generic(CKernel* kernel) +std::shared_ptr GaussianKernel::obtain_from_generic(std::shared_ptr kernel) { require(kernel->get_kernel_type()==K_GAUSSIAN, - "Provided kernel ({}) must be of type CGaussianKernel!", kernel->get_name()); + "Provided kernel ({}) must be of type GaussianKernel!", kernel->get_name()); - SG_REF(kernel); - return (CGaussianKernel*)kernel; + return std::static_pointer_cast(kernel); } #include -CSGObject *CGaussianKernel::shallow_copy() const +std::shared_ptrGaussianKernel::shallow_copy() const { // TODO: remove this after all the classes get shallow_copy properly implemented - // this assert is to avoid any subclass of CGaussianKernel accidentally called + // this assert is to avoid any subclass of GaussianKernel accidentally called // with the implement here - ASSERT(typeid(*this) == typeid(CGaussianKernel)) - CGaussianKernel *ker = new CGaussianKernel(cache_size, get_width()); + ASSERT(typeid(*this) == typeid(GaussianKernel)) + auto ker = std::make_shared(cache_size, get_width()); if (lhs && rhs) { ker->init(lhs, rhs); @@ -70,25 +69,25 @@ CSGObject *CGaussianKernel::shallow_copy() const return ker; } -void CGaussianKernel::cleanup() +void GaussianKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); m_distance->cleanup(); } -bool CGaussianKernel::init(CFeatures* l, CFeatures* r) +bool GaussianKernel::init(std::shared_ptr l, std::shared_ptr r) { cleanup(); - return CShiftInvariantKernel::init(l, r); + return ShiftInvariantKernel::init(l, r); } -void CGaussianKernel::set_width(float64_t w) +void GaussianKernel::set_width(float64_t w) { require(w>0, "width ({}) must be positive",w); m_log_width = std::log(w / 2.0) / 2.0; } -SGMatrix CGaussianKernel::get_parameter_gradient(const TParameter* param, index_t index) +SGMatrix GaussianKernel::get_parameter_gradient(const TParameter* param, index_t index) { require(lhs, "Left hand side features must be set!"); require(rhs, "Rightt hand side features must be set!"); @@ -114,33 +113,33 @@ SGMatrix CGaussianKernel::get_parameter_gradient(const TParameter* pa } } -float64_t CGaussianKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t GaussianKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t result=distance(idx_a, idx_b); return std::exp(-result); } -void CGaussianKernel::load_serializable_post() noexcept(false) +void GaussianKernel::load_serializable_post() noexcept(false) { - CKernel::load_serializable_post(); + Kernel::load_serializable_post(); if (lhs && rhs) m_distance->init(lhs, rhs); } -float64_t CGaussianKernel::distance(int32_t idx_a, int32_t idx_b) const +float64_t GaussianKernel::distance(int32_t idx_a, int32_t idx_b) const { - return CShiftInvariantKernel::distance(idx_a, idx_b)/get_width(); + return ShiftInvariantKernel::distance(idx_a, idx_b)/get_width(); } -void CGaussianKernel::register_params() +void GaussianKernel::register_params() { set_width(1.0); set_cache_size(10); - CEuclideanDistance* dist=new CEuclideanDistance(); + auto dist=std::make_shared(); dist->set_disable_sqrt(true); m_distance=dist; - SG_REF(m_distance); + SG_ADD(&m_log_width, "log_width", "Kernel width in log domain", ParameterProperties::HYPER | ParameterProperties::GRADIENT); } diff --git a/src/shogun/kernel/GaussianKernel.h b/src/shogun/kernel/GaussianKernel.h index a5dbb7a1a4a..d2944f1d74d 100644 --- a/src/shogun/kernel/GaussianKernel.h +++ b/src/shogun/kernel/GaussianKernel.h @@ -15,11 +15,11 @@ namespace shogun { -class CFeatures; -class CDotFeatures; +class Features; +class DotFeatures; /** @brief The well known Gaussian kernel (swiss army knife for SVMs) computed - * on CDotFeatures. + * on DotFeatures. * * It is computed as * @@ -30,24 +30,24 @@ class CDotFeatures; * where \f$\tau\f$ is the kernel width. * */ -class CGaussianKernel: public CShiftInvariantKernel +class GaussianKernel: public ShiftInvariantKernel { public: /** default constructor */ - CGaussianKernel(); + GaussianKernel(); /** constructor * * @param width width */ - CGaussianKernel(float64_t width); + GaussianKernel(float64_t width); /** constructor * * @param size cache size * @param width width */ - CGaussianKernel(int32_t size, float64_t width); + GaussianKernel(int32_t size, float64_t width); /** constructor * @@ -56,19 +56,19 @@ class CGaussianKernel: public CShiftInvariantKernel * @param width width * @param size cache size */ - CGaussianKernel(CDotFeatures* l, CDotFeatures* r, float64_t width, int32_t size=10); + GaussianKernel(std::shared_ptr l, std::shared_ptr r, float64_t width, int32_t size=10); /** destructor */ - virtual ~CGaussianKernel(); + virtual ~GaussianKernel(); - /** @param kernel is casted to CGaussianKernel, error if not possible + /** @param kernel is casted to GaussianKernel, error if not possible * is SG_REF'ed - * @return casted CGaussianKernel object + * @return casted GaussianKernel object */ - static CGaussianKernel* obtain_from_generic(CKernel* kernel); + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); /** Make a shallow copy of the kernel */ - virtual CSGObject* shallow_copy() const; + virtual std::shared_ptr shallow_copy() const; /** initialize kernel * @@ -76,7 +76,7 @@ class CGaussianKernel: public CShiftInvariantKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/GaussianShiftKernel.cpp b/src/shogun/kernel/GaussianShiftKernel.cpp index 81d064cb5bf..4910dd5a546 100644 --- a/src/shogun/kernel/GaussianShiftKernel.cpp +++ b/src/shogun/kernel/GaussianShiftKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Roman Votyakov, Evan Shelhamer, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Roman Votyakov, Evan Shelhamer, Sergey Lisitsyn, * Wu Lin */ @@ -12,41 +12,41 @@ using namespace shogun; -CGaussianShiftKernel::CGaussianShiftKernel() -: CGaussianKernel(), max_shift(0), shift_step(0) +GaussianShiftKernel::GaussianShiftKernel() +: GaussianKernel(), max_shift(0), shift_step(0) { init(); } -CGaussianShiftKernel::CGaussianShiftKernel( +GaussianShiftKernel::GaussianShiftKernel( int32_t size, float64_t w, int32_t ms, int32_t ss) -: CGaussianKernel(size, w), max_shift(ms), shift_step(ss) +: GaussianKernel(size, w), max_shift(ms), shift_step(ss) { init(); } -CGaussianShiftKernel::CGaussianShiftKernel( - CDenseFeatures* l, CDenseFeatures* r, float64_t w, int32_t ms, int32_t ss, +GaussianShiftKernel::GaussianShiftKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t w, int32_t ms, int32_t ss, int32_t size) -: CGaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss) +: GaussianKernel(l, r, w, size), max_shift(ms), shift_step(ss) { init(); init(l,r); } -CGaussianShiftKernel::~CGaussianShiftKernel() +GaussianShiftKernel::~GaussianShiftKernel() { } -float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t GaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + lhs->as>()->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + rhs->as>()->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result = 0.0 ; @@ -68,13 +68,13 @@ float64_t CGaussianShiftKernel::compute(int32_t idx_a, int32_t idx_b) result += exp(-sum/get_width())/(2*s) ; } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + lhs->as>()->free_feature_vector(avec, idx_a, afree); + rhs->as>()->free_feature_vector(bvec, idx_b, bfree); return result; } -void CGaussianShiftKernel::init() +void GaussianShiftKernel::init() { SG_ADD(&max_shift, "max_shift", "Maximum shift.", ParameterProperties::HYPER); SG_ADD(&shift_step, "shift_step", "Shift stepsize.", ParameterProperties::HYPER); diff --git a/src/shogun/kernel/GaussianShiftKernel.h b/src/shogun/kernel/GaussianShiftKernel.h index a2664403d7b..179d25a7d08 100644 --- a/src/shogun/kernel/GaussianShiftKernel.h +++ b/src/shogun/kernel/GaussianShiftKernel.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Roman Votyakov, Evan Shelhamer, Yuyu Zhang, + * Authors: Soeren Sonnenburg, Roman Votyakov, Evan Shelhamer, Yuyu Zhang, * Sergey Lisitsyn */ @@ -34,11 +34,11 @@ namespace shogun * (parameter shift_step) of the shifts and \f$S_{\mathrm{max}}\f$ (parameter * max_shift) is the maximal shift. */ -class CGaussianShiftKernel: public CGaussianKernel +class GaussianShiftKernel: public GaussianKernel { public: /** default constructor */ - CGaussianShiftKernel(); + GaussianShiftKernel(); /** constructor * @@ -47,7 +47,7 @@ class CGaussianShiftKernel: public CGaussianKernel * @param max_shift maximum shift * @param shift_step shift step */ - CGaussianShiftKernel( + GaussianShiftKernel( int32_t size, float64_t width, int32_t max_shift, int32_t shift_step); @@ -60,8 +60,8 @@ class CGaussianShiftKernel: public CGaussianKernel * @param shift_step shift step * @param size cache size */ - CGaussianShiftKernel( - CDenseFeatures* l, CDenseFeatures* r, + GaussianShiftKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t width, int32_t max_shift, int32_t shift_step, int32_t size=10); @@ -71,12 +71,12 @@ class CGaussianShiftKernel: public CGaussianKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - return CGaussianKernel::init(l,r); + return GaussianKernel::init(l,r); } - virtual ~CGaussianShiftKernel(); + virtual ~GaussianShiftKernel(); /** return what type of kernel we are * diff --git a/src/shogun/kernel/GaussianShortRealKernel.cpp b/src/shogun/kernel/GaussianShortRealKernel.cpp index e2be9441694..3c31d29d07f 100644 --- a/src/shogun/kernel/GaussianShortRealKernel.cpp +++ b/src/shogun/kernel/GaussianShortRealKernel.cpp @@ -11,58 +11,58 @@ using namespace shogun; -CGaussianShortRealKernel::CGaussianShortRealKernel() -: CDotKernel(0), width(0.0) +GaussianShortRealKernel::GaussianShortRealKernel() +: DotKernel(0), width(0.0) { register_params(); } -CGaussianShortRealKernel::CGaussianShortRealKernel(int32_t size, float64_t w) -: CDotKernel(size), width(w) +GaussianShortRealKernel::GaussianShortRealKernel(int32_t size, float64_t w) +: DotKernel(size), width(w) { register_params(); } -CGaussianShortRealKernel::CGaussianShortRealKernel( - CDenseFeatures* l, CDenseFeatures* r, float64_t w, int32_t size) -: CDotKernel(size), width(w) +GaussianShortRealKernel::GaussianShortRealKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t w, int32_t size) +: DotKernel(size), width(w) { init(l,r); register_params(); } -CGaussianShortRealKernel::~CGaussianShortRealKernel() +GaussianShortRealKernel::~GaussianShortRealKernel() { } -bool CGaussianShortRealKernel::init(CFeatures* l, CFeatures* r) +bool GaussianShortRealKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } -float64_t CGaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t GaussianShortRealKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; - float32_t* avec=((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); - float32_t* bvec=((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + float32_t* avec=(std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); + float32_t* bvec=(std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result=0; for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } -void CGaussianShortRealKernel::register_params() +void GaussianShortRealKernel::register_params() { SG_ADD(&width, "width", "kernel width", ParameterProperties::HYPER); } diff --git a/src/shogun/kernel/GaussianShortRealKernel.h b/src/shogun/kernel/GaussianShortRealKernel.h index 0b3cd54f068..931e6ffcd14 100644 --- a/src/shogun/kernel/GaussianShortRealKernel.h +++ b/src/shogun/kernel/GaussianShortRealKernel.h @@ -26,18 +26,18 @@ namespace shogun * * where \f$\tau\f$ is the kernel width. */ -class CGaussianShortRealKernel: public CDotKernel +class GaussianShortRealKernel: public DotKernel { public: /** default constructor */ - CGaussianShortRealKernel(); + GaussianShortRealKernel(); /** constructor * * @param size cache size * @param width width */ - CGaussianShortRealKernel(int32_t size, float64_t width); + GaussianShortRealKernel(int32_t size, float64_t width); /** constructor * @@ -46,10 +46,10 @@ class CGaussianShortRealKernel: public CDotKernel * @param width width * @param size cache size */ - CGaussianShortRealKernel(CDenseFeatures* l, CDenseFeatures* r, + GaussianShortRealKernel(std::shared_ptr> l, std::shared_ptr> r, float64_t width, int32_t size=10); - virtual ~CGaussianShortRealKernel(); + virtual ~GaussianShortRealKernel(); /** initialize kernel * @@ -57,7 +57,7 @@ class CGaussianShortRealKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/HistogramIntersectionKernel.cpp b/src/shogun/kernel/HistogramIntersectionKernel.cpp index 83fce9d35da..7fa9a02f662 100644 --- a/src/shogun/kernel/HistogramIntersectionKernel.cpp +++ b/src/shogun/kernel/HistogramIntersectionKernel.cpp @@ -11,48 +11,48 @@ using namespace shogun; -CHistogramIntersectionKernel::CHistogramIntersectionKernel() -: CDotKernel(0), m_beta(1.0) +HistogramIntersectionKernel::HistogramIntersectionKernel() +: DotKernel(0), m_beta(1.0) { register_params(); } -CHistogramIntersectionKernel::CHistogramIntersectionKernel(int32_t size) -: CDotKernel(size), m_beta(1.0) +HistogramIntersectionKernel::HistogramIntersectionKernel(int32_t size) +: DotKernel(size), m_beta(1.0) { register_params(); } -CHistogramIntersectionKernel::CHistogramIntersectionKernel( - CDenseFeatures* l, CDenseFeatures* r, +HistogramIntersectionKernel::HistogramIntersectionKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t beta, int32_t size) -: CDotKernel(size), m_beta(beta) +: DotKernel(size), m_beta(beta) { init(l,r); register_params(); } -CHistogramIntersectionKernel::~CHistogramIntersectionKernel() +HistogramIntersectionKernel::~HistogramIntersectionKernel() { cleanup(); } -bool CHistogramIntersectionKernel::init(CFeatures* l, CFeatures* r) +bool HistogramIntersectionKernel::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDotKernel::init(l,r); + bool result=DotKernel::init(l,r); init_normalizer(); return result; } -float64_t CHistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t HistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result=0; @@ -68,15 +68,15 @@ float64_t CHistogramIntersectionKernel::compute(int32_t idx_a, int32_t idx_b) { //compute generalized histogram intersection kernel for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } -void CHistogramIntersectionKernel::register_params() +void HistogramIntersectionKernel::register_params() { SG_ADD(&m_beta, "beta", "the beta parameter of the kernel", ParameterProperties::HYPER); } diff --git a/src/shogun/kernel/HistogramIntersectionKernel.h b/src/shogun/kernel/HistogramIntersectionKernel.h index ec8907c8f3c..d31ebfed2bb 100644 --- a/src/shogun/kernel/HistogramIntersectionKernel.h +++ b/src/shogun/kernel/HistogramIntersectionKernel.h @@ -26,17 +26,17 @@ namespace shogun * \f] * with \f$\beta=1\f$ by default * */ -class CHistogramIntersectionKernel: public CDotKernel +class HistogramIntersectionKernel: public DotKernel { public: /** default constructor */ - CHistogramIntersectionKernel(); + HistogramIntersectionKernel(); /** constructor * * @param size cache size */ - CHistogramIntersectionKernel(int32_t size); + HistogramIntersectionKernel(int32_t size); /** constructor * @@ -45,11 +45,11 @@ class CHistogramIntersectionKernel: public CDotKernel * @param beta kernel parameter * @param size cache size */ - CHistogramIntersectionKernel( - CDenseFeatures* l, CDenseFeatures* r, + HistogramIntersectionKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t beta=1.0, int32_t size=10); - virtual ~CHistogramIntersectionKernel(); + virtual ~HistogramIntersectionKernel(); /** initialize kernel * @@ -57,7 +57,7 @@ class CHistogramIntersectionKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /* register the parameters */ virtual void register_params(); diff --git a/src/shogun/kernel/InverseMultiQuadricKernel.cpp b/src/shogun/kernel/InverseMultiQuadricKernel.cpp index 331b8f7e561..a3972280daf 100644 --- a/src/shogun/kernel/InverseMultiQuadricKernel.cpp +++ b/src/shogun/kernel/InverseMultiQuadricKernel.cpp @@ -9,52 +9,52 @@ using namespace shogun; -CInverseMultiQuadricKernel::CInverseMultiQuadricKernel(): CKernel(0), distance(NULL), coef(0.0001) +InverseMultiQuadricKernel::InverseMultiQuadricKernel(): Kernel(0), distance(NULL), coef(0.0001) { init(); } -CInverseMultiQuadricKernel::CInverseMultiQuadricKernel(int32_t cache, float64_t coefficient, CDistance* dist) -: CKernel(cache), distance(dist), coef(coefficient) +InverseMultiQuadricKernel::InverseMultiQuadricKernel(int32_t cache, float64_t coefficient, std::shared_ptr dist) +: Kernel(cache), distance(dist), coef(coefficient) { - SG_REF(distance); + init(); } -CInverseMultiQuadricKernel::CInverseMultiQuadricKernel(CFeatures *l, CFeatures *r, float64_t coefficient, CDistance* dist) -: CKernel(10), distance(dist), coef(coefficient) +InverseMultiQuadricKernel::InverseMultiQuadricKernel(std::shared_ptrl, std::shared_ptrr, float64_t coefficient, std::shared_ptr dist) +: Kernel(10), distance(dist), coef(coefficient) { - SG_REF(distance); + init(); init(l, r); } -CInverseMultiQuadricKernel::~CInverseMultiQuadricKernel() +InverseMultiQuadricKernel::~InverseMultiQuadricKernel() { cleanup(); - SG_UNREF(distance); + } -bool CInverseMultiQuadricKernel::init(CFeatures* l, CFeatures* r) +bool InverseMultiQuadricKernel::init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l,r); + Kernel::init(l,r); distance->init(l,r); return init_normalizer(); } -void CInverseMultiQuadricKernel::load_serializable_post() noexcept(false) +void InverseMultiQuadricKernel::load_serializable_post() noexcept(false) { - CKernel::load_serializable_post(); + Kernel::load_serializable_post(); } -void CInverseMultiQuadricKernel::init() +void InverseMultiQuadricKernel::init() { SG_ADD(&coef, "coef", "Kernel Coefficient.", ParameterProperties::HYPER); SG_ADD(&distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } -float64_t CInverseMultiQuadricKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t InverseMultiQuadricKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = distance->distance(idx_a, idx_b); return 1/sqrt(dist*dist + coef*coef); diff --git a/src/shogun/kernel/InverseMultiQuadricKernel.h b/src/shogun/kernel/InverseMultiQuadricKernel.h index 9da995c30dd..e3263c6e58c 100644 --- a/src/shogun/kernel/InverseMultiQuadricKernel.h +++ b/src/shogun/kernel/InverseMultiQuadricKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief InverseMultiQuadricKernel * * \f[ @@ -24,18 +24,18 @@ class CDistance; * \f] */ -class CInverseMultiQuadricKernel: public CKernel +class InverseMultiQuadricKernel: public Kernel { public: /** default constructor */ - CInverseMultiQuadricKernel(); + InverseMultiQuadricKernel(); /** constructor * @param cache size of cache * @param coef kernel parameter coef * @param dist distance to be used */ - CInverseMultiQuadricKernel(int32_t cache, float64_t coef, CDistance* dist); + InverseMultiQuadricKernel(int32_t cache, float64_t coef, std::shared_ptr dist); /** constructor * @param l features left-side @@ -43,14 +43,14 @@ class CInverseMultiQuadricKernel: public CKernel * @param coef kernel parameter coef * @param dist distance to be used */ - CInverseMultiQuadricKernel(CFeatures *l, CFeatures *r, float64_t coef, CDistance* dist); + InverseMultiQuadricKernel(std::shared_ptrl, std::shared_ptrr, float64_t coef, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -82,7 +82,7 @@ class CInverseMultiQuadricKernel: public CKernel */ inline void set_coef(float64_t value) { this->coef = value; } - virtual ~CInverseMultiQuadricKernel(); + virtual ~InverseMultiQuadricKernel(); /** Can (optionally) be overridden to post-initialize some * member variables which are not PARAMETER::ADD'ed. Make @@ -98,7 +98,7 @@ class CInverseMultiQuadricKernel: public CKernel /** distance to be used */ - CDistance* distance; + std::shared_ptr distance; /** theta parameter of kernel - coefficient */ diff --git a/src/shogun/kernel/JensenShannonKernel.cpp b/src/shogun/kernel/JensenShannonKernel.cpp index 661e434e773..27119c0cae0 100644 --- a/src/shogun/kernel/JensenShannonKernel.cpp +++ b/src/shogun/kernel/JensenShannonKernel.cpp @@ -11,44 +11,44 @@ using namespace shogun; -CJensenShannonKernel::CJensenShannonKernel() -: CDotKernel(0) +JensenShannonKernel::JensenShannonKernel() +: DotKernel(0) { } -CJensenShannonKernel::CJensenShannonKernel(int32_t size) -: CDotKernel(size) +JensenShannonKernel::JensenShannonKernel(int32_t size) +: DotKernel(size) { } -CJensenShannonKernel::CJensenShannonKernel( - CDenseFeatures* l, CDenseFeatures* r, int32_t size) -: CDotKernel(size) +JensenShannonKernel::JensenShannonKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t size) +: DotKernel(size) { init(l,r); } -CJensenShannonKernel::~CJensenShannonKernel() +JensenShannonKernel::~JensenShannonKernel() { cleanup(); } -bool CJensenShannonKernel::init(CFeatures* l, CFeatures* r) +bool JensenShannonKernel::init(std::shared_ptr l, std::shared_ptr r) { - bool result=CDotKernel::init(l,r); + bool result=DotKernel::init(l,r); init_normalizer(); return result; } -float64_t CJensenShannonKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t JensenShannonKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result=0; @@ -58,15 +58,15 @@ float64_t CJensenShannonKernel::compute(int32_t idx_a, int32_t idx_b) float64_t a_i = 0, b_i = 0; float64_t ab = avec[i]+bvec[i]; if (avec[i] != 0) - a_i = avec[i] * CMath::log2(ab/avec[i]); + a_i = avec[i] * Math::log2(ab/avec[i]); if (bvec[i] != 0) - b_i = bvec[i] * CMath::log2(ab/bvec[i]); + b_i = bvec[i] * Math::log2(ab/bvec[i]); result += 0.5*(a_i + b_i); } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/kernel/JensenShannonKernel.h b/src/shogun/kernel/JensenShannonKernel.h index b1d505fc9d9..4416aec2e02 100644 --- a/src/shogun/kernel/JensenShannonKernel.h +++ b/src/shogun/kernel/JensenShannonKernel.h @@ -23,17 +23,17 @@ namespace shogun * k({\bf x},{\bf x'})= \sum_{i=0}^{l} \frac{x_i}{2} \log_2\frac{x_i+x'_i}{x_i} + \frac{x'_i}{2} \log_2\frac{x_i+x'_i}{x'_i} * \f] * */ -class CJensenShannonKernel: public CDotKernel +class JensenShannonKernel: public DotKernel { public: /** default constructor */ - CJensenShannonKernel(); + JensenShannonKernel(); /** constructor * * @param size cache size */ - CJensenShannonKernel(int32_t size); + JensenShannonKernel(int32_t size); /** constructor * @@ -41,11 +41,11 @@ class CJensenShannonKernel: public CDotKernel * @param r features of right-hand side * @param size cache size */ - CJensenShannonKernel( - CDenseFeatures* l, CDenseFeatures* r, + JensenShannonKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t size=10); - virtual ~CJensenShannonKernel(); + virtual ~JensenShannonKernel(); /** initialize kernel * @@ -53,7 +53,7 @@ class CJensenShannonKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/Kernel.cpp b/src/shogun/kernel/Kernel.cpp index cfa42f46167..cfabb384f57 100644 --- a/src/shogun/kernel/Kernel.cpp +++ b/src/shogun/kernel/Kernel.cpp @@ -33,13 +33,13 @@ using namespace shogun; -CKernel::CKernel() : CSGObject() +Kernel::Kernel() : SGObject() { init(); register_params(); } -CKernel::CKernel(int32_t size) : CSGObject() +Kernel::Kernel(int32_t size) : SGObject() { init(); @@ -51,7 +51,7 @@ CKernel::CKernel(int32_t size) : CSGObject() } -CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, int32_t size) : CSGObject() +Kernel::Kernel(std::shared_ptr p_lhs, std::shared_ptr p_rhs, int32_t size) : SGObject() { init(); @@ -60,22 +60,22 @@ CKernel::CKernel(CFeatures* p_lhs, CFeatures* p_rhs, int32_t size) : CSGObject() cache_size=size; - set_normalizer(new CIdentityKernelNormalizer()); + set_normalizer(std::make_shared()); init(p_lhs, p_rhs); register_params(); } -CKernel::~CKernel() +Kernel::~Kernel() { if (get_is_initialized()) error("Kernel still initialized on destruction."); remove_lhs_and_rhs(); - SG_UNREF(normalizer); + } #ifdef USE_SVMLIGHT -void CKernel::resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack) +void Kernel::resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack) { if (size<10) size=10; @@ -88,11 +88,11 @@ void CKernel::resize_kernel_cache(KERNELCACHE_IDX size, bool regression_hack) } #endif //USE_SVMLIGHT -bool CKernel::init(CFeatures* l, CFeatures* r) +bool Kernel::init(std::shared_ptr l, std::shared_ptr r) { //make sure features were indeed supplied - require(l, "CKernel::init({}, {}): Left hand side features required!", fmt::ptr(l), fmt::ptr(r)); - require(r, "CKernel::init({}, {}): Right hand side features required!", fmt::ptr(l), fmt::ptr(r)); + require(l, "Kernel::init({}, {}): Left hand side features required!", fmt::ptr(l.get()), fmt::ptr(r.get())); + require(r, "Kernel::init({}, {}): Right hand side features required!", fmt::ptr(l.get()), fmt::ptr(r.get())); //make sure features are compatible if (l->support_compatible_class()) @@ -112,11 +112,10 @@ bool CKernel::init(CFeatures* l, CFeatures* r) //remove references to previous features remove_lhs_and_rhs(); - SG_REF(l); + if (l==r) lhs_equals_rhs=true; - else // l!=r - SG_REF(r); + lhs=l; rhs=r; @@ -127,34 +126,34 @@ bool CKernel::init(CFeatures* l, CFeatures* r) num_lhs=l->get_num_vectors(); num_rhs=r->get_num_vectors(); - SG_TRACE("leaving CKernel::init({}, {})", fmt::ptr(l), fmt::ptr(r)); + SG_TRACE("leaving Kernel::init({}, {})", fmt::ptr(l.get()), fmt::ptr(r.get())); return true; } -bool CKernel::set_normalizer(CKernelNormalizer* n) +bool Kernel::set_normalizer(std::shared_ptr n) { - SG_REF(n); + if (lhs && rhs) n->init(this); - SG_UNREF(normalizer); + normalizer=n; return (normalizer!=NULL); } -CKernelNormalizer* CKernel::get_normalizer() const +std::shared_ptr Kernel::get_normalizer() const { - SG_REF(normalizer) + return normalizer; } -bool CKernel::init_normalizer() +bool Kernel::init_normalizer() { return normalizer->init(this); } -void CKernel::cleanup() +void Kernel::cleanup() { remove_lhs_and_rhs(); } @@ -162,7 +161,7 @@ void CKernel::cleanup() #ifdef USE_SVMLIGHT /****************************** Cache handling *******************************/ -void CKernel::kernel_cache_init(int32_t buffsize, bool regression_hack) +void Kernel::kernel_cache_init(int32_t buffsize, bool regression_hack) { int32_t totdoc=get_num_vec_lhs(); if (totdoc<=0) @@ -219,7 +218,7 @@ void CKernel::kernel_cache_init(int32_t buffsize, bool regression_hack) kernel_cache.time=0; } -void CKernel::get_kernel_row( +void Kernel::get_kernel_row( int32_t docnum, int32_t *active2dnum, float64_t *buffer, bool full_line) { int32_t i,j; @@ -283,7 +282,7 @@ void CKernel::get_kernel_row( // Fills cache for the row m -void CKernel::cache_kernel_row(int32_t m) +void Kernel::cache_kernel_row(int32_t m) { int32_t j,k,l; KERNELCACHE_ELEM *cache; @@ -322,7 +321,7 @@ void CKernel::cache_kernel_row(int32_t m) } -void* CKernel::cache_multiple_kernel_row_helper(void* p) +void* Kernel::cache_multiple_kernel_row_helper(void* p) { int32_t j,k,l; S_KTHREAD_PARAM* params = (S_KTHREAD_PARAM*) p; @@ -357,7 +356,7 @@ void* CKernel::cache_multiple_kernel_row_helper(void* p) } // Fills cache for the rows in key -void CKernel::cache_multiple_kernel_rows(int32_t* rows, int32_t num_rows) +void Kernel::cache_multiple_kernel_rows(int32_t* rows, int32_t num_rows) { int32_t nthreads=env()->get_num_threads(); @@ -453,7 +452,7 @@ void CKernel::cache_multiple_kernel_rows(int32_t* rows, int32_t num_rows) // remove numshrink columns in the cache // which correspond to examples marked -void CKernel::kernel_cache_shrink( +void Kernel::kernel_cache_shrink( int32_t totdoc, int32_t numshrink, int32_t *after) { ASSERT(totdoc > 0); @@ -512,7 +511,7 @@ void CKernel::kernel_cache_shrink( } -void CKernel::kernel_cache_reset_lru() +void Kernel::kernel_cache_reset_lru() { int32_t maxlru=0,k; @@ -525,7 +524,7 @@ void CKernel::kernel_cache_reset_lru() } } -void CKernel::kernel_cache_cleanup() +void Kernel::kernel_cache_cleanup() { SG_FREE(kernel_cache.index); SG_FREE(kernel_cache.occu); @@ -537,7 +536,7 @@ void CKernel::kernel_cache_cleanup() memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE)); } -int32_t CKernel::kernel_cache_malloc() +int32_t Kernel::kernel_cache_malloc() { int32_t i; @@ -553,7 +552,7 @@ int32_t CKernel::kernel_cache_malloc() return(-1); } -void CKernel::kernel_cache_free(int32_t cacheidx) +void Kernel::kernel_cache_free(int32_t cacheidx) { kernel_cache.occu[cacheidx]=0; kernel_cache.elems--; @@ -561,7 +560,7 @@ void CKernel::kernel_cache_free(int32_t cacheidx) // remove least recently used cache // element -int32_t CKernel::kernel_cache_free_lru() +int32_t Kernel::kernel_cache_free_lru() { int32_t k,least_elem=-1,least_time; @@ -586,7 +585,7 @@ int32_t CKernel::kernel_cache_free_lru() // Get a free cache entry. In case cache is full, the lru // element is removed. -KERNELCACHE_ELEM* CKernel::kernel_cache_clean_and_malloc(int32_t cacheidx) +KERNELCACHE_ELEM* Kernel::kernel_cache_clean_and_malloc(int32_t cacheidx) { int32_t result; if((result = kernel_cache_malloc()) == -1) { @@ -604,13 +603,13 @@ KERNELCACHE_ELEM* CKernel::kernel_cache_clean_and_malloc(int32_t cacheidx) } #endif //USE_SVMLIGHT -void CKernel::load(CFile* loader) +void Kernel::load(std::shared_ptr loader) { SG_SET_LOCALE_C; SG_RESET_LOCALE; } -void CKernel::save(CFile* writer) +void Kernel::save(std::shared_ptr writer) { SGMatrix k_matrix=get_kernel_matrix(); SG_SET_LOCALE_C; @@ -618,14 +617,14 @@ void CKernel::save(CFile* writer) SG_RESET_LOCALE; } -void CKernel::remove_lhs_and_rhs() +void Kernel::remove_lhs_and_rhs() { if (rhs!=lhs) - SG_UNREF(rhs); + rhs = NULL; num_rhs=0; - SG_UNREF(lhs); + lhs = NULL; num_lhs=0; lhs_equals_rhs=false; @@ -635,11 +634,11 @@ void CKernel::remove_lhs_and_rhs() #endif //USE_SVMLIGHT } -void CKernel::remove_lhs() +void Kernel::remove_lhs() { if (rhs==lhs) rhs=NULL; - SG_UNREF(lhs); + lhs = NULL; num_lhs=0; lhs_equals_rhs=false; @@ -649,10 +648,10 @@ void CKernel::remove_lhs() } /// takes all necessary steps if the rhs is removed from kernel -void CKernel::remove_rhs() +void Kernel::remove_rhs() { if (rhs!=lhs) - SG_UNREF(rhs); + rhs = NULL; num_rhs=0; lhs_equals_rhs=false; @@ -664,7 +663,7 @@ void CKernel::remove_rhs() #define ENUM_CASE(n) case n: io::info(#n " "); break; -void CKernel::list_kernel() +void Kernel::list_kernel() { io::info("{} - \"{}\" weight={:1.2f} OPT:{}", fmt::ptr(this), get_name(), get_combined_kernel_weight(), @@ -784,67 +783,67 @@ void CKernel::list_kernel() } #undef ENUM_CASE -bool CKernel::init_optimization( +bool Kernel::init_optimization( int32_t count, int32_t *IDX, float64_t * weights) { error("kernel does not support linadd optimization"); return false ; } -bool CKernel::delete_optimization() +bool Kernel::delete_optimization() { error("kernel does not support linadd optimization"); return false; } -float64_t CKernel::compute_optimized(int32_t vector_idx) +float64_t Kernel::compute_optimized(int32_t vector_idx) { error("kernel does not support linadd optimization"); return 0; } -void CKernel::compute_batch( +void Kernel::compute_batch( int32_t num_vec, int32_t* vec_idx, float64_t* target, int32_t num_suppvec, int32_t* IDX, float64_t* weights, float64_t factor) { error("kernel does not support batch computation"); } -void CKernel::add_to_normal(int32_t vector_idx, float64_t weight) +void Kernel::add_to_normal(int32_t vector_idx, float64_t weight) { error("kernel does not support linadd optimization, add_to_normal not implemented"); } -void CKernel::clear_normal() +void Kernel::clear_normal() { error("kernel does not support linadd optimization, clear_normal not implemented"); } -int32_t CKernel::get_num_subkernels() +int32_t Kernel::get_num_subkernels() { return 1; } -void CKernel::compute_by_subkernel( +void Kernel::compute_by_subkernel( int32_t vector_idx, float64_t * subkernel_contrib) { error("kernel compute_by_subkernel not implemented"); } -const float64_t* CKernel::get_subkernel_weights(int32_t &num_weights) +const float64_t* Kernel::get_subkernel_weights(int32_t &num_weights) { num_weights=1 ; return &combined_kernel_weight ; } -SGVector CKernel::get_subkernel_weights() +SGVector Kernel::get_subkernel_weights() { int num_weights = 1; const float64_t* weight = get_subkernel_weights(num_weights); return SGVector(const_cast(weight),1,false); } -void CKernel::set_subkernel_weights(const SGVector weights) +void Kernel::set_subkernel_weights(const SGVector weights) { ASSERT(weights.vector) if (weights.vlen!=1) @@ -853,13 +852,13 @@ void CKernel::set_subkernel_weights(const SGVector weights) combined_kernel_weight = weights.vector[0] ; } -CKernel* CKernel::obtain_from_generic(CSGObject* kernel) +std::shared_ptr Kernel::obtain_from_generic(std::shared_ptr kernel) { if (kernel) { - CKernel* casted=dynamic_cast(kernel); - require(casted, "CKernel::obtain_from_generic(): Error, provided object" - " of class \"{}\" is not a subclass of CKernel!", + auto casted=std::dynamic_pointer_cast(kernel); + require(casted, "Kernel::obtain_from_generic(): Error, provided object" + " of class \"{}\" is not a subclass of Kernel!", kernel->get_name()); return casted; } @@ -867,7 +866,7 @@ CKernel* CKernel::obtain_from_generic(CSGObject* kernel) return NULL; } -bool CKernel::init_optimization_svm(CSVM * svm) +bool Kernel::init_optimization_svm(std::shared_ptr svm) { int32_t num_suppvec=svm->get_num_support_vectors(); int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec); @@ -885,30 +884,30 @@ bool CKernel::init_optimization_svm(CSVM * svm) return ret; } -void CKernel::load_serializable_post() noexcept(false) +void Kernel::load_serializable_post() noexcept(false) { - CSGObject::load_serializable_post(); + SGObject::load_serializable_post(); if (lhs_equals_rhs) rhs=lhs; } -void CKernel::save_serializable_pre() noexcept(false) +void Kernel::save_serializable_pre() noexcept(false) { - CSGObject::save_serializable_pre(); + SGObject::save_serializable_pre(); if (lhs_equals_rhs) rhs=NULL; } -void CKernel::save_serializable_post() noexcept(false) +void Kernel::save_serializable_post() noexcept(false) { - CSGObject::save_serializable_post(); + SGObject::save_serializable_post(); if (lhs_equals_rhs) rhs=lhs; } -void CKernel::register_params() +void Kernel::register_params() { SG_ADD(&cache_size, "cache_size", "Cache size in MB."); SG_ADD( @@ -940,7 +939,7 @@ void CKernel::register_params() } -void CKernel::init() +void Kernel::init() { cache_size=10; kernel_matrix=NULL; @@ -959,7 +958,7 @@ void CKernel::init() memset(&kernel_cache, 0x0, sizeof(KERNEL_CACHE)); #endif //USE_SVMLIGHT - set_normalizer(new CIdentityKernelNormalizer()); + set_normalizer(std::make_shared()); } namespace shogun @@ -968,7 +967,7 @@ namespace shogun template struct K_THREAD_PARAM { /** kernel */ - CKernel* kernel; + Kernel* kernel; /** start (unit row) */ int32_t start; /** end (unit row) */ @@ -990,7 +989,7 @@ template struct K_THREAD_PARAM }; } -float64_t CKernel::sum_symmetric_block(index_t block_begin, index_t block_size, +float64_t Kernel::sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag) { SG_TRACE("Entering"); @@ -1041,7 +1040,7 @@ float64_t CKernel::sum_symmetric_block(index_t block_begin, index_t block_size, return sum; } -float64_t CKernel::sum_block(index_t block_begin_row, index_t block_begin_col, +float64_t Kernel::sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag) { SG_TRACE("Entering"); @@ -1086,7 +1085,7 @@ float64_t CKernel::sum_block(index_t block_begin_row, index_t block_begin_col, return sum; } -SGVector CKernel::row_wise_sum_symmetric_block(index_t block_begin, +SGVector Kernel::row_wise_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag) { SG_TRACE("Entering"); @@ -1140,7 +1139,7 @@ SGVector CKernel::row_wise_sum_symmetric_block(index_t block_begin, return row_sum; } -SGMatrix CKernel::row_wise_sum_squared_sum_symmetric_block(index_t +SGMatrix Kernel::row_wise_sum_squared_sum_symmetric_block(index_t block_begin, index_t block_size, bool no_diag) { SG_TRACE("Entering"); @@ -1199,7 +1198,7 @@ SGMatrix CKernel::row_wise_sum_squared_sum_symmetric_block(index_t return row_sum; } -SGVector CKernel::row_col_wise_sum_block(index_t block_begin_row, +SGVector Kernel::row_col_wise_sum_block(index_t block_begin_row, index_t block_begin_col, index_t block_size_row, index_t block_size_col, bool no_diag) { @@ -1253,12 +1252,12 @@ SGVector CKernel::row_col_wise_sum_block(index_t block_begin_row, return sum; } -template void* CKernel::get_kernel_matrix_helper(void* p) +template void* Kernel::get_kernel_matrix_helper(void* p) { K_THREAD_PARAM* params= (K_THREAD_PARAM*) p; int32_t i_start=params->start; int32_t i_end=params->end; - CKernel* k=params->kernel; + Kernel* k=params->kernel; T* result=params->result; bool symmetric=params->symmetric; int32_t n=params->n; @@ -1293,7 +1292,7 @@ template void* CKernel::get_kernel_matrix_helper(void* p) pb->print_progress(); // TODO: replace with the new signal - // if (CSignal::cancel_computations()) + // if (Signal::cancel_computations()) // break; } } @@ -1304,7 +1303,7 @@ template void* CKernel::get_kernel_matrix_helper(void* p) } template -SGMatrix CKernel::get_kernel_matrix() +SGMatrix Kernel::get_kernel_matrix() { T* result = NULL; @@ -1340,7 +1339,7 @@ SGMatrix CKernel::get_kernel_matrix() params.symmetric=symmetric; params.verbose=false; params.pb = &pb; - CKernel::get_kernel_matrix_helper((void*)¶ms); + Kernel::get_kernel_matrix_helper((void*)¶ms); } if (total_num % num_threads != 0) @@ -1355,7 +1354,7 @@ SGMatrix CKernel::get_kernel_matrix() params.symmetric=symmetric; params.verbose=false; params.pb = &pb; - CKernel::get_kernel_matrix_helper((void*)¶ms); + Kernel::get_kernel_matrix_helper((void*)¶ms); } pb.complete(); @@ -1364,8 +1363,8 @@ SGMatrix CKernel::get_kernel_matrix() } -template SGMatrix CKernel::get_kernel_matrix(); -template SGMatrix CKernel::get_kernel_matrix(); +template SGMatrix Kernel::get_kernel_matrix(); +template SGMatrix Kernel::get_kernel_matrix(); -template void* CKernel::get_kernel_matrix_helper(void* p); -template void* CKernel::get_kernel_matrix_helper(void* p); +template void* Kernel::get_kernel_matrix_helper(void* p); +template void* Kernel::get_kernel_matrix_helper(void* p); diff --git a/src/shogun/kernel/Kernel.h b/src/shogun/kernel/Kernel.h index 3b1fc0b7967..12ca4700df3 100644 --- a/src/shogun/kernel/Kernel.h +++ b/src/shogun/kernel/Kernel.h @@ -25,9 +25,9 @@ namespace shogun { - class CFile; - class CFeatures; - class CKernelNormalizer; + class File; + class Features; + class KernelNormalizer; #ifdef USE_SHORTREAL_KERNELCACHE /** kernel cache element */ @@ -124,7 +124,7 @@ enum EKernelProperty KP_BATCHEVALUATION = 4 // Kernels that can on the fly generate normals in linadd and more quickly/memory efficient process batches instead of single examples }; -class CSVM; +class SVM; /** @brief The Kernel base class. * @@ -151,20 +151,20 @@ class CSVM; * the kernel type get_kernel_type()). A good example to look at is the * GaussianKernel. */ -class CKernel : public CSGObject +class Kernel : public SGObject { - friend class CVarianceKernelNormalizer; - friend class CSqrtDiagKernelNormalizer; - friend class CAvgDiagKernelNormalizer; - friend class CRidgeKernelNormalizer; - friend class CFirstElementKernelNormalizer; - friend class CMultitaskKernelNormalizer; - friend class CMultitaskKernelMklNormalizer; - friend class CMultitaskKernelMaskNormalizer; - friend class CMultitaskKernelMaskPairNormalizer; - friend class CTanimotoKernelNormalizer; - friend class CDiceKernelNormalizer; - friend class CZeroMeanCenterKernelNormalizer; + friend class VarianceKernelNormalizer; + friend class SqrtDiagKernelNormalizer; + friend class AvgDiagKernelNormalizer; + friend class RidgeKernelNormalizer; + friend class FirstElementKernelNormalizer; + friend class MultitaskKernelNormalizer; + friend class MultitaskKernelMklNormalizer; + friend class MultitaskKernelMaskNormalizer; + friend class MultitaskKernelMaskPairNormalizer; + friend class TanimotoKernelNormalizer; + friend class DiceKernelNormalizer; + friend class ZeroMeanCenterKernelNormalizer; friend class CStreamingKernel; @@ -173,14 +173,14 @@ class CKernel : public CSGObject /** default constructor * */ - CKernel(); + Kernel(); /** constructor * * @param size cache size */ - CKernel(int32_t size); + Kernel(int32_t size); /** constructor * @@ -188,9 +188,9 @@ class CKernel : public CSGObject * @param r features for right-hand side * @param size cache size */ - CKernel(CFeatures* l, CFeatures* r, int32_t size); + Kernel(std::shared_ptr l, std::shared_ptr r, int32_t size); - virtual ~CKernel(); + virtual ~Kernel(); /** get kernel function for lhs feature vector a * and rhs feature vector b @@ -226,13 +226,13 @@ class CKernel : public CSGObject SGVector get_kernel_diagonal(SGVector preallocated=SGVector()) { - require(lhs, "CKernel::get_kernel_diagonal(): Left-handside " + require(lhs, "Kernel::get_kernel_diagonal(): Left-handside " "features missing!"); - require(rhs, "CKernel::get_kernel_diagonal(): Right-handside " + require(rhs, "Kernel::get_kernel_diagonal(): Right-handside " "features missing!"); - int32_t length=CMath::min(lhs->get_num_vectors(),rhs->get_num_vectors()); + int32_t length=Math::min(lhs->get_num_vectors(),rhs->get_num_vectors()); /* allocate space if necessary */ if (!preallocated.vector) @@ -454,19 +454,19 @@ class CKernel : public CSGObject * @param rhs features for right-hand side * @return if init was successful */ - virtual bool init(CFeatures* lhs, CFeatures* rhs); + virtual bool init(std::shared_ptr lhs, std::shared_ptr rhs); /** set the current kernel normalizer * * @return if successful */ - virtual bool set_normalizer(CKernelNormalizer* normalizer); + virtual bool set_normalizer(std::shared_ptr normalizer); /** obtain the current kernel normalizer * * @return the kernel normalizer */ - virtual CKernelNormalizer* get_normalizer() const; + virtual std::shared_ptr get_normalizer() const; /** initialize the current kernel normalizer * @return if init was successful @@ -476,7 +476,7 @@ class CKernel : public CSGObject /** clean up your kernel * * base method only removes lhs and rhs - * overload to add further cleanup but make sure CKernel::cleanup() is + * overload to add further cleanup but make sure Kernel::cleanup() is * called */ virtual void cleanup(); @@ -485,25 +485,25 @@ class CKernel : public CSGObject * * @param loader File object via which to load data */ - void load(CFile* loader); + void load(std::shared_ptr loader); /** save kernel matrix * * @param writer File object via which to save data */ - void save(CFile* writer); + void save(std::shared_ptr writer); /** get left-hand side of features used in kernel * * @return features of left-hand side */ - inline CFeatures* get_lhs() { SG_REF(lhs); return lhs; } + inline std::shared_ptr get_lhs() { return lhs; } /** get right-hand side of features used in kernel * * @return features of right-hand side */ - inline CFeatures* get_rhs() { SG_REF(rhs); return rhs; } + inline std::shared_ptr get_rhs() { return rhs; } /** get number of vectors of lhs features * @@ -772,7 +772,7 @@ class CKernel : public CSGObject * @param svm svm model * @return if initializing was successful */ - bool init_optimization_svm(CSVM * svm) ; + bool init_optimization_svm(std::shared_ptr svm) ; /** compute optimized * @@ -868,11 +868,10 @@ class CKernel : public CSGObject /** Obtains a kernel from a generic SGObject with error checking. Note * that if passing NULL, result will be NULL - * @param kernel Object to cast to CKernel, is *not* SG_REFed - * @return object casted to CKernel, NULL if not possible + * @param kernel Object to cast to Kernel, is *not* SG_REFed + * @return object casted to Kernel, NULL if not possible */ - static CKernel* obtain_from_generic(CSGObject* kernel); - + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); protected: /** set property * @@ -921,8 +920,8 @@ class CKernel : public CSGObject int32_t i_start; if (symmetric) - i_start = (int32_t)CMath::floor( - n - std::sqrt(CMath::sq((float64_t)n) - offs)); + i_start = (int32_t)Math::floor( + n - std::sqrt(Math::sq((float64_t)n) - offs)); else i_start=(int32_t) (offs/int64_t(n)); @@ -1012,7 +1011,7 @@ class CKernel : public CSGObject struct S_KTHREAD_PARAM { /** kernel */ - CKernel* kernel; + Kernel* kernel; /** kernel cache */ KERNEL_CACHE* kernel_cache; /** cache */ @@ -1057,9 +1056,9 @@ class CKernel : public CSGObject KERNELCACHE_ELEM* kernel_matrix; /// feature vectors to occur on left hand side - CFeatures* lhs; + std::shared_ptr lhs; /// feature vectors to occur on right hand side - CFeatures* rhs; + std::shared_ptr rhs; /// lhs bool lhs_equals_rhs; @@ -1084,7 +1083,7 @@ class CKernel : public CSGObject /** normalize the kernel(i,j) function based on this normalization * function */ - CKernelNormalizer* normalizer; + std::shared_ptr normalizer; }; } diff --git a/src/shogun/kernel/LinearKernel.cpp b/src/shogun/kernel/LinearKernel.cpp index 92435933aca..fef33feeb33 100644 --- a/src/shogun/kernel/LinearKernel.cpp +++ b/src/shogun/kernel/LinearKernel.cpp @@ -12,46 +12,46 @@ using namespace shogun; -CLinearKernel::CLinearKernel() -: CDotKernel(0) +LinearKernel::LinearKernel() +: DotKernel(0) { properties |= KP_LINADD; } -CLinearKernel::CLinearKernel(CDotFeatures* l, CDotFeatures* r) -: CDotKernel(0) +LinearKernel::LinearKernel(std::shared_ptr l, std::shared_ptr r) +: DotKernel(0) { properties |= KP_LINADD; init(l,r); } -CLinearKernel::~CLinearKernel() +LinearKernel::~LinearKernel() { cleanup(); } -bool CLinearKernel::init(CFeatures* l, CFeatures* r) +bool LinearKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } -void CLinearKernel::cleanup() +void LinearKernel::cleanup() { delete_optimization(); - CKernel::cleanup(); + Kernel::cleanup(); } -void CLinearKernel::add_to_normal(int32_t idx, float64_t weight) +void LinearKernel::add_to_normal(int32_t idx, float64_t weight) { - ((CDotFeatures*) lhs)->add_to_dense_vec( + lhs->as()->add_to_dense_vec( normalizer->normalize_lhs(weight, idx), idx, normal.vector, normal.size()); set_is_initialized(true); } -bool CLinearKernel::init_optimization( +bool LinearKernel::init_optimization( int32_t num_suppvec, int32_t* sv_idx, float64_t* alphas) { clear_normal(); @@ -63,7 +63,7 @@ bool CLinearKernel::init_optimization( return true; } -bool CLinearKernel::init_optimization(CKernelMachine* km) +bool LinearKernel::init_optimization(std::shared_ptr km) { clear_normal(); @@ -76,7 +76,7 @@ bool CLinearKernel::init_optimization(CKernelMachine* km) return true; } -bool CLinearKernel::delete_optimization() +bool LinearKernel::delete_optimization() { normal = SGVector(); set_is_initialized(false); @@ -84,9 +84,9 @@ bool CLinearKernel::delete_optimization() return true; } -float64_t CLinearKernel::compute_optimized(int32_t idx) +float64_t LinearKernel::compute_optimized(int32_t idx) { ASSERT(get_is_initialized()) - float64_t result = ((CDotFeatures*) rhs)->dot(idx, normal); + float64_t result = rhs->as()->dot(idx, normal); return normalizer->normalize_rhs(result, idx); } diff --git a/src/shogun/kernel/LinearKernel.h b/src/shogun/kernel/LinearKernel.h index 653012bda3a..a573cbd8242 100644 --- a/src/shogun/kernel/LinearKernel.h +++ b/src/shogun/kernel/LinearKernel.h @@ -16,10 +16,10 @@ namespace shogun { - class CKernelMachine; - class CDotFeatures; + class KernelMachine; + class DotFeatures; -/** @brief Computes the standard linear kernel on CDotFeatures. +/** @brief Computes the standard linear kernel on DotFeatures. * * Formally, it computes * @@ -27,21 +27,21 @@ namespace shogun * k({\bf x},{\bf x'})= {\bf x}\cdot {\bf x'} * \f] */ -class CLinearKernel: public CDotKernel +class LinearKernel: public DotKernel { public: /** constructor */ - CLinearKernel(); + LinearKernel(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CLinearKernel(CDotFeatures* l, CDotFeatures* r); + LinearKernel(std::shared_ptr l, std::shared_ptr r); - virtual ~CLinearKernel(); + virtual ~LinearKernel(); /** initialize kernel * @@ -49,7 +49,7 @@ class CLinearKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -80,7 +80,7 @@ class CLinearKernel: public CDotKernel /** init optimization * @param km */ - virtual bool init_optimization(CKernelMachine* km); + virtual bool init_optimization(std::shared_ptr km); /** delete optimization * @@ -97,7 +97,7 @@ class CLinearKernel: public CDotKernel virtual void clear_normal() { - normal = SGVector(((CDotFeatures*)lhs)->get_dim_feature_space()); + normal = SGVector((std::static_pointer_cast(lhs))->get_dim_feature_space()); normal.zero(); set_is_initialized(false); } @@ -125,7 +125,7 @@ class CLinearKernel: public CDotKernel */ void set_w(SGVector w) { - ASSERT(lhs && w.size()==((CDotFeatures*) lhs)->get_dim_feature_space()) + ASSERT(lhs && w.size()==(std::static_pointer_cast(lhs))->get_dim_feature_space()) this->normal = w; } diff --git a/src/shogun/kernel/LogKernel.cpp b/src/shogun/kernel/LogKernel.cpp index 36b18fc6985..1847793585c 100644 --- a/src/shogun/kernel/LogKernel.cpp +++ b/src/shogun/kernel/LogKernel.cpp @@ -9,49 +9,49 @@ using namespace shogun; -CLogKernel::CLogKernel(): CKernel(0), m_distance(NULL), m_degree(1.8) +LogKernel::LogKernel(): Kernel(0), m_distance(NULL), m_degree(1.8) { init(); } -CLogKernel::CLogKernel(int32_t cache, float64_t degree, CDistance* dist) -: CKernel(cache), m_distance(dist), m_degree(degree) +LogKernel::LogKernel(int32_t cache, float64_t degree, std::shared_ptr dist) +: Kernel(cache), m_distance(dist), m_degree(degree) { init(); ASSERT(m_distance) - SG_REF(m_distance); + } -CLogKernel::CLogKernel(CFeatures *l, CFeatures *r, float64_t degree, CDistance* dist) -: CKernel(10), m_distance(dist), m_degree(degree) +LogKernel::LogKernel(std::shared_ptrl, std::shared_ptrr, float64_t degree, std::shared_ptr dist) +: Kernel(10), m_distance(dist), m_degree(degree) { init(); ASSERT(m_distance) - SG_REF(m_distance); + init(l, r); } -CLogKernel::~CLogKernel() +LogKernel::~LogKernel() { cleanup(); - SG_UNREF(m_distance); + } -bool CLogKernel::init(CFeatures* l, CFeatures* r) +bool LogKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(m_distance) - CKernel::init(l,r); + Kernel::init(l,r); m_distance->init(l,r); return init_normalizer(); } -void CLogKernel::init() +void LogKernel::init() { SG_ADD(&m_degree, "degree", "Degree kernel parameter.", ParameterProperties::HYPER); SG_ADD(&m_distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } -float64_t CLogKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t LogKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = m_distance->distance(idx_a, idx_b); float64_t temp = pow(dist, m_degree); diff --git a/src/shogun/kernel/LogKernel.h b/src/shogun/kernel/LogKernel.h index 258038af6ea..7bc4eb5d052 100644 --- a/src/shogun/kernel/LogKernel.h +++ b/src/shogun/kernel/LogKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Log kernel * @@ -28,18 +28,18 @@ class CDistance; * */ -class CLogKernel: public CKernel +class LogKernel: public Kernel { public: /** default constructor */ - CLogKernel(); + LogKernel(); /** constructor * @param cache size of cache * @param degree kernel parameter degree * @param dist distance to be used */ - CLogKernel(int32_t cache, float64_t degree, CDistance* dist); + LogKernel(int32_t cache, float64_t degree, std::shared_ptr dist); /** constructor * @param l features left-side @@ -47,14 +47,14 @@ class CLogKernel: public CKernel * @param degree kernel parameter degree * @param dist distance to be used */ - CLogKernel(CFeatures *l, CFeatures *r, float64_t degree, CDistance* dist); + LogKernel(std::shared_ptrl, std::shared_ptrr, float64_t degree, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -76,7 +76,7 @@ class CLogKernel: public CKernel */ virtual const char* get_name() const { return "LogKernel"; } - virtual ~CLogKernel(); + virtual ~LogKernel(); protected: /** @@ -93,7 +93,7 @@ class CLogKernel: public CKernel protected: /// distance to be used - CDistance* m_distance; + std::shared_ptr m_distance; /// degree parameter of kernel float64_t m_degree; diff --git a/src/shogun/kernel/MultiquadricKernel.cpp b/src/shogun/kernel/MultiquadricKernel.cpp index 018af762c02..ca8c5b7ba62 100644 --- a/src/shogun/kernel/MultiquadricKernel.cpp +++ b/src/shogun/kernel/MultiquadricKernel.cpp @@ -9,49 +9,49 @@ using namespace shogun; -CMultiquadricKernel::CMultiquadricKernel(): CKernel(0), m_distance(NULL), m_coef(0.0001) +MultiquadricKernel::MultiquadricKernel(): Kernel(0), m_distance(NULL), m_coef(0.0001) { init(); } -CMultiquadricKernel::CMultiquadricKernel(int32_t cache, float64_t coef, CDistance* dist) -: CKernel(cache), m_distance(dist), m_coef(coef) +MultiquadricKernel::MultiquadricKernel(int32_t cache, float64_t coef, std::shared_ptr dist) +: Kernel(cache), m_distance(dist), m_coef(coef) { ASSERT(m_distance) - SG_REF(m_distance); + init(); } -CMultiquadricKernel::CMultiquadricKernel(CFeatures *l, CFeatures *r, float64_t coef, CDistance* dist) -: CKernel(10), m_distance(dist), m_coef(coef) +MultiquadricKernel::MultiquadricKernel(std::shared_ptrl, std::shared_ptrr, float64_t coef, std::shared_ptr dist) +: Kernel(10), m_distance(dist), m_coef(coef) { ASSERT(m_distance) - SG_REF(m_distance); + init(l, r); init(); } -CMultiquadricKernel::~CMultiquadricKernel() +MultiquadricKernel::~MultiquadricKernel() { cleanup(); - SG_UNREF(m_distance); + } -bool CMultiquadricKernel::init(CFeatures* l, CFeatures* r) +bool MultiquadricKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(m_distance) - CKernel::init(l,r); + Kernel::init(l,r); m_distance->init(l,r); return init_normalizer(); } -float64_t CMultiquadricKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t MultiquadricKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = m_distance->distance(idx_a, idx_b); - return sqrt(CMath::sq(dist) + CMath::sq(m_coef)); + return sqrt(Math::sq(dist) + Math::sq(m_coef)); } -void CMultiquadricKernel::init() +void MultiquadricKernel::init() { SG_ADD(&m_coef, "coef", "Kernel coefficient.", ParameterProperties::HYPER); SG_ADD(&m_distance, "distance", "Distance to be used.", diff --git a/src/shogun/kernel/MultiquadricKernel.h b/src/shogun/kernel/MultiquadricKernel.h index 5a393046458..2371c7f9776 100644 --- a/src/shogun/kernel/MultiquadricKernel.h +++ b/src/shogun/kernel/MultiquadricKernel.h @@ -16,25 +16,25 @@ namespace shogun { -class CDistance; +class Distance; /** @brief MultiquadricKernel * * \f[ * K(x,x') = \sqrt{\| x - x' \|^2 +c^2} * \f] */ -class CMultiquadricKernel: public CKernel +class MultiquadricKernel: public Kernel { public: /** default constructor */ - CMultiquadricKernel(); + MultiquadricKernel(); /** constructor * @param cache size of cache * @param coef kernel parameter coef * @param dist distance to be used */ - CMultiquadricKernel(int32_t cache, float64_t coef, CDistance* dist); + MultiquadricKernel(int32_t cache, float64_t coef, std::shared_ptr dist); /** constructor * @param l features left-side @@ -42,14 +42,14 @@ class CMultiquadricKernel: public CKernel * @param coef kernel parameter coef * @param dist distance to be used */ - CMultiquadricKernel(CFeatures *l, CFeatures *r, float64_t coef, CDistance* dist); + MultiquadricKernel(std::shared_ptrl, std::shared_ptrr, float64_t coef, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -81,7 +81,7 @@ class CMultiquadricKernel: public CKernel */ inline void set_coef(float64_t value) { m_coef = value; } - virtual ~CMultiquadricKernel(); + virtual ~MultiquadricKernel(); protected: /** @@ -99,7 +99,7 @@ class CMultiquadricKernel: public CKernel protected: /// distance to be used - CDistance* m_distance; + std::shared_ptr m_distance; /// theta parameter of kernel - coefficient float64_t m_coef; diff --git a/src/shogun/kernel/PeriodicKernel.cpp b/src/shogun/kernel/PeriodicKernel.cpp index ac517708564..6717f1341aa 100644 --- a/src/shogun/kernel/PeriodicKernel.cpp +++ b/src/shogun/kernel/PeriodicKernel.cpp @@ -32,20 +32,20 @@ using namespace shogun; -CPeriodicKernel::CPeriodicKernel() : CDotKernel() +PeriodicKernel::PeriodicKernel() : DotKernel() { init(); } -CPeriodicKernel::CPeriodicKernel(float64_t ls, float64_t p, int32_t s) : CDotKernel(s) +PeriodicKernel::PeriodicKernel(float64_t ls, float64_t p, int32_t s) : DotKernel(s) { init(); set_length_scale(ls); set_period(p); } -CPeriodicKernel::CPeriodicKernel(CDotFeatures* l, CDotFeatures* r, float64_t ls, - float64_t p, int32_t s) : CDotKernel(s) +PeriodicKernel::PeriodicKernel(std::shared_ptr l, std::shared_ptr r, float64_t ls, + float64_t p, int32_t s) : DotKernel(s) { init(); set_length_scale(ls); @@ -53,8 +53,8 @@ CPeriodicKernel::CPeriodicKernel(CDotFeatures* l, CDotFeatures* r, float64_t ls, init(l,r); } -void CPeriodicKernel::precompute_squared_helper(SGVector& buf, - CDotFeatures* df) +void PeriodicKernel::precompute_squared_helper(SGVector& buf, + std::shared_ptr df) { int32_t num_vec=df->get_num_vectors(); buf=SGVector(num_vec); @@ -63,14 +63,14 @@ void CPeriodicKernel::precompute_squared_helper(SGVector& buf, buf[i]=df->dot(i,df, i); } -bool CPeriodicKernel::init(CFeatures* l, CFeatures* r) +bool PeriodicKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); precompute_squared(); return init_normalizer(); } -float64_t CPeriodicKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t PeriodicKernel::compute(int32_t idx_a, int32_t idx_b) { /* Periodic kernel defined as by David Duvenaud in http://mlg.eng.cam.ac.uk/duvenaud/cookbook/index.html @@ -79,15 +79,15 @@ float64_t CPeriodicKernel::compute(int32_t idx_a, int32_t idx_b) */ float64_t sin_term = std::sin(M_PI * distance(idx_a, idx_b) / m_period); return std::exp( - -2 * CMath::pow(sin_term, 2) / CMath::pow(m_length_scale, 2)); + -2 * Math::pow(sin_term, 2) / Math::pow(m_length_scale, 2)); } -void CPeriodicKernel::precompute_squared() +void PeriodicKernel::precompute_squared() { if (!lhs || !rhs) return; - CDotFeatures* dotlhs=dynamic_cast(lhs); + auto dotlhs=std::dynamic_pointer_cast(lhs); require(dotlhs!=NULL, "Left-hand-side features must be of type CDotFeatures"); precompute_squared_helper(m_sq_lhs, dotlhs); @@ -96,14 +96,14 @@ void CPeriodicKernel::precompute_squared() m_sq_rhs=m_sq_lhs; else { - CDotFeatures *dotrhs=dynamic_cast(rhs); + auto dotrhs=std::dynamic_pointer_cast(rhs); require(dotrhs!=NULL, "Left-hand-side features must be of type CDotFeatures"); precompute_squared_helper(m_sq_rhs, dotrhs); } } -SGMatrix CPeriodicKernel::get_parameter_gradient( +SGMatrix PeriodicKernel::get_parameter_gradient( const TParameter* param, index_t index) { require(lhs, "Left-hand-side features not set!"); @@ -151,7 +151,7 @@ SGMatrix CPeriodicKernel::get_parameter_gradient( } } -void CPeriodicKernel::init() +void PeriodicKernel::init() { set_length_scale(1.0); set_period(1.0); @@ -166,7 +166,7 @@ void CPeriodicKernel::init() "Vector of dot products of each right-hand-side vector with itself."); } -float64_t CPeriodicKernel::distance(int32_t idx_a, int32_t idx_b) +float64_t PeriodicKernel::distance(int32_t idx_a, int32_t idx_b) { - return sqrt(m_sq_lhs[idx_a]+m_sq_rhs[idx_b]-2*CDotKernel::compute(idx_a,idx_b)); + return sqrt(m_sq_lhs[idx_a]+m_sq_rhs[idx_b]-2*DotKernel::compute(idx_a,idx_b)); } diff --git a/src/shogun/kernel/PeriodicKernel.h b/src/shogun/kernel/PeriodicKernel.h index e999d808466..02d33319ba6 100644 --- a/src/shogun/kernel/PeriodicKernel.h +++ b/src/shogun/kernel/PeriodicKernel.h @@ -35,7 +35,7 @@ namespace shogun { - class CDotFeatures; + class DotFeatures; /** @brief The periodic kernel as described in The Kernel Cookbook * by David Duvenaud: http://people.seas.harvard.edu/~dduvenaud/cookbook/ @@ -50,11 +50,11 @@ namespace shogun * of the kernel. */ -class CPeriodicKernel: public CDotKernel +class PeriodicKernel: public DotKernel { public: /** default constructor */ - CPeriodicKernel(); + PeriodicKernel(); /** constructor * @@ -62,7 +62,7 @@ class CPeriodicKernel: public CDotKernel * @param period period * @param size cache size. Default value: 10 */ - CPeriodicKernel(float64_t length_scale, float64_t period, int32_t size=10); + PeriodicKernel(float64_t length_scale, float64_t period, int32_t size=10); /** constructor * @@ -72,10 +72,10 @@ class CPeriodicKernel: public CDotKernel * @param period period * @param size cache size. Default value: 10 */ - CPeriodicKernel(CDotFeatures* l, CDotFeatures* r, + PeriodicKernel(std::shared_ptr l, std::shared_ptr r, float64_t length_scale, float64_t period, int32_t size=10); - virtual ~CPeriodicKernel() { }; + virtual ~PeriodicKernel() { }; /** initialize kernel * @@ -83,7 +83,7 @@ class CPeriodicKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * @@ -152,9 +152,9 @@ class CPeriodicKernel: public CDotKernel * @return computed the distance * * Note that this function is very similar the distance function in - * CGaussianKernel. However, this function computes the standard + * GaussianKernel. However, this function computes the standard * euclidean distance without any factors or squaring, unlike the one - * in CGaussianKernel. + * in GaussianKernel. */ virtual float64_t distance(int32_t idx_a, int32_t idx_b); private: @@ -169,7 +169,7 @@ class CPeriodicKernel: public CDotKernel * @param buf buffer to store squared terms * @param df dot feature object based on which k(i,i) is computed * */ - void precompute_squared_helper(SGVector& buf, CDotFeatures* df); + void precompute_squared_helper(SGVector& buf, std::shared_ptr df); void init(); diff --git a/src/shogun/kernel/PolyKernel.cpp b/src/shogun/kernel/PolyKernel.cpp index 82a27f65aa7..1503ab8370d 100644 --- a/src/shogun/kernel/PolyKernel.cpp +++ b/src/shogun/kernel/PolyKernel.cpp @@ -15,13 +15,13 @@ using namespace shogun; -CPolyKernel::CPolyKernel() : CDotKernel(0) +PolyKernel::PolyKernel() : DotKernel(0) { init(); } -CPolyKernel::CPolyKernel(int32_t size, int32_t d, float64_t c, float64_t gamma) - : CDotKernel(size) +PolyKernel::PolyKernel(int32_t size, int32_t d, float64_t c, float64_t gamma) + : DotKernel(size) { require(c >= 0.0, "c parameter must be positive!"); init(); @@ -30,10 +30,10 @@ CPolyKernel::CPolyKernel(int32_t size, int32_t d, float64_t c, float64_t gamma) m_gamma = gamma; } -CPolyKernel::CPolyKernel( - CDotFeatures* l, CDotFeatures* r, int32_t d, float64_t c, float64_t gamma, +PolyKernel::PolyKernel( + std::shared_ptr l, std::shared_ptr r, int32_t d, float64_t c, float64_t gamma, int32_t size) - : CDotKernel(size) + : DotKernel(size) { require(c >= 0.0, "c parameter must be positive!"); init(); @@ -44,34 +44,34 @@ CPolyKernel::CPolyKernel( m_gamma = gamma; } -CPolyKernel::~CPolyKernel() +PolyKernel::~PolyKernel() { cleanup(); } -bool CPolyKernel::init(CFeatures* l, CFeatures* r) +bool PolyKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } -void CPolyKernel::cleanup() +void PolyKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CPolyKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t PolyKernel::compute(int32_t idx_a, int32_t idx_b) { - auto result = m_gamma * CDotKernel::compute(idx_a, idx_b) + m_c; - return CMath::pow(result, degree); + auto result = m_gamma * DotKernel::compute(idx_a, idx_b) + m_c; + return Math::pow(result, degree); } -void CPolyKernel::init() +void PolyKernel::init() { degree = 0; m_c = 0.0; m_gamma = 1.0; - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); SG_ADD( °ree, "degree", "Degree of polynomial kernel", ParameterProperties::HYPER); diff --git a/src/shogun/kernel/PolyKernel.h b/src/shogun/kernel/PolyKernel.h index 8a381d9c031..6291f21e998 100644 --- a/src/shogun/kernel/PolyKernel.h +++ b/src/shogun/kernel/PolyKernel.h @@ -15,9 +15,9 @@ namespace shogun { - class CDotFeatures; + class DotFeatures; -/** @brief Computes the standard polynomial kernel on CDotFeatures +/** @brief Computes the standard polynomial kernel on DotFeatures * * Formally, it computes * @@ -30,11 +30,11 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CPolyKernel: public CDotKernel +class PolyKernel: public DotKernel { public: /** default constructor */ - CPolyKernel(); + PolyKernel(); /** constructor * @@ -44,7 +44,7 @@ class CPolyKernel: public CDotKernel * @param c trade-off parameter * @param size cache size */ - CPolyKernel(CDotFeatures* l, CDotFeatures* r, + PolyKernel(std::shared_ptr l, std::shared_ptr r, int32_t d, float64_t c, float64_t gamma, int32_t size=10); /** constructor @@ -54,9 +54,9 @@ class CPolyKernel: public CDotKernel * @param c trade-off parameter * @param gamma scaler for the dot product */ - CPolyKernel(int32_t size, int32_t degree, float64_t c=1.0, float64_t gamma=1.0); + PolyKernel(int32_t size, int32_t degree, float64_t c=1.0, float64_t gamma=1.0); - virtual ~CPolyKernel(); + virtual ~PolyKernel(); /** initialize kernel * @@ -64,7 +64,7 @@ class CPolyKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/PowerKernel.cpp b/src/shogun/kernel/PowerKernel.cpp index 18f39352d77..473f5fe781d 100644 --- a/src/shogun/kernel/PowerKernel.cpp +++ b/src/shogun/kernel/PowerKernel.cpp @@ -9,50 +9,50 @@ using namespace shogun; -CPowerKernel::CPowerKernel(): CKernel(0), distance(NULL), m_degree(1.8) +PowerKernel::PowerKernel(): Kernel(0), distance(NULL), m_degree(1.8) { init(); } -CPowerKernel::CPowerKernel(int32_t cache, float64_t degree, CDistance* dist) -: CKernel(cache), distance(dist), m_degree(degree) +PowerKernel::PowerKernel(int32_t cache, float64_t degree, std::shared_ptr dist) +: Kernel(cache), distance(dist), m_degree(degree) { init(); ASSERT(distance) - SG_REF(distance); + } -CPowerKernel::CPowerKernel(CFeatures *l, CFeatures *r, float64_t degree, CDistance* dist) -: CKernel(10), distance(dist), m_degree(degree) +PowerKernel::PowerKernel(std::shared_ptrl, std::shared_ptrr, float64_t degree, std::shared_ptr dist) +: Kernel(10), distance(dist), m_degree(degree) { init(); ASSERT(distance) - SG_REF(distance); + init(l, r); } -CPowerKernel::~CPowerKernel() +PowerKernel::~PowerKernel() { cleanup(); - SG_UNREF(distance); + } -bool CPowerKernel::init(CFeatures* l, CFeatures* r) +bool PowerKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(distance) - CKernel::init(l,r); + Kernel::init(l,r); distance->init(l,r); return init_normalizer(); } -void CPowerKernel::init() +void PowerKernel::init() { SG_ADD(&m_degree, "degree", "Degree kernel parameter.", ParameterProperties::HYPER); SG_ADD(&distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } -float64_t CPowerKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t PowerKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = distance->distance(idx_a, idx_b); float64_t temp = pow(dist, m_degree); diff --git a/src/shogun/kernel/PowerKernel.h b/src/shogun/kernel/PowerKernel.h index c4aca26f2a6..a81ae5144d1 100644 --- a/src/shogun/kernel/PowerKernel.h +++ b/src/shogun/kernel/PowerKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Power kernel * @@ -28,18 +28,18 @@ class CDistance; * */ -class CPowerKernel: public CKernel +class PowerKernel: public Kernel { public: /** default constructor */ - CPowerKernel(); + PowerKernel(); /** constructor * @param cache size of cache * @param degree kernel parameter degree * @param dist distance to be used */ - CPowerKernel(int32_t cache, float64_t degree, CDistance* dist); + PowerKernel(int32_t cache, float64_t degree, std::shared_ptr dist); /** constructor * @param l features left-side @@ -47,14 +47,14 @@ class CPowerKernel: public CKernel * @param degree kernel parameter degree * @param dist distance to be used */ - CPowerKernel(CFeatures *l, CFeatures *r, float64_t degree, CDistance* dist); + PowerKernel(std::shared_ptrl, std::shared_ptrr, float64_t degree, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -76,7 +76,7 @@ class CPowerKernel: public CKernel */ virtual const char* get_name() const { return "PowerKernel"; } - virtual ~CPowerKernel(); + virtual ~PowerKernel(); protected: /** @@ -94,7 +94,7 @@ class CPowerKernel: public CKernel protected: /// distance to be used - CDistance* distance; + std::shared_ptr distance; /// degree parameter of kernel float64_t m_degree; diff --git a/src/shogun/kernel/ProductKernel.cpp b/src/shogun/kernel/ProductKernel.cpp index ae92b739dea..7f87723d288 100644 --- a/src/shogun/kernel/ProductKernel.cpp +++ b/src/shogun/kernel/ProductKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Roman Votyakov, Soeren Sonnenburg, + * Authors: Jacob Walker, Roman Votyakov, Soeren Sonnenburg, * Evangelos Anagnostopoulos */ @@ -10,33 +10,33 @@ using namespace shogun; -CProductKernel::CProductKernel(int32_t size) : CKernel(size) +ProductKernel::ProductKernel(int32_t size) : Kernel(size) { init(); io::info("Product kernel created ({})", fmt::ptr(this)); } -CProductKernel::~CProductKernel() +ProductKernel::~ProductKernel() { cleanup(); - SG_UNREF(kernel_array); + io::info("Product kernel deleted ({}).", fmt::ptr(this)); } -//Adapted from CCombinedKernel -bool CProductKernel::init(CFeatures* l, CFeatures* r) +//Adapted from CombinedKernel +bool ProductKernel::init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l,r); + Kernel::init(l,r); ASSERT(l->get_feature_class()==C_COMBINED) ASSERT(r->get_feature_class()==C_COMBINED) ASSERT(l->get_feature_type()==F_UNKNOWN) ASSERT(r->get_feature_type()==F_UNKNOWN) - CFeatures* lf=NULL; - CFeatures* rf=NULL; - CKernel* k=NULL; + std::shared_ptr lf=NULL; + std::shared_ptr rf=NULL; + std::shared_ptr k=NULL; bool result=true; @@ -50,22 +50,19 @@ bool CProductKernel::init(CFeatures* l, CFeatures* r) // skip over features - the custom kernel does not need any if (k->get_kernel_type() != K_CUSTOM) { - lf=((CCombinedFeatures*) l)->get_feature_obj(f_idx); - rf=((CCombinedFeatures*) r)->get_feature_obj(f_idx); + lf=(std::static_pointer_cast(l))->get_feature_obj(f_idx); + rf=(std::static_pointer_cast(r))->get_feature_obj(f_idx); f_idx++; if (!lf || !rf) { - SG_UNREF(lf); - SG_UNREF(rf); - SG_UNREF(k); error("ProductKernel: Number of features/kernels does not match - bailing out"); } SG_DEBUG("Initializing 0x{} - \"{}\"", fmt::ptr(this), k->get_name()) result=k->init(lf,rf); - SG_UNREF(lf); - SG_UNREF(rf); + + if (!result) break; @@ -81,7 +78,7 @@ bool CProductKernel::init(CFeatures* l, CFeatures* r) error("Number of rhs-feature vectors ({}) not match with number of cols ({}) of custom kernel", num_rhs, k->get_num_vec_rhs()); } - SG_UNREF(k); + } if (!result) @@ -90,155 +87,155 @@ bool CProductKernel::init(CFeatures* l, CFeatures* r) if (k) { k->list_kernel(); - SG_UNREF(k); + } else io::info(""); return false; } - if ( (f_idx!=((CCombinedFeatures*) l)->get_num_feature_obj()) || - (f_idx!=((CCombinedFeatures*) r)->get_num_feature_obj()) ) + if ( (f_idx!=(std::static_pointer_cast(l))->get_num_feature_obj()) || + (f_idx!=(std::static_pointer_cast(r))->get_num_feature_obj()) ) error("ProductKernel: Number of features/kernels does not match - bailing out"); initialized=true; return true; } -//Adapted from CCombinedKernel -void CProductKernel::remove_lhs() +//Adapted from CombinedKernel +void ProductKernel::remove_lhs() { for (index_t k_idx=0; k_idxget_kernel_type() != K_CUSTOM) k->remove_lhs(); - SG_UNREF(k); + } - CKernel::remove_lhs(); + Kernel::remove_lhs(); num_lhs=0; } -//Adapted from CCombinedKernel -void CProductKernel::remove_rhs() +//Adapted from CombinedKernel +void ProductKernel::remove_rhs() { for (index_t k_idx=0; k_idxget_kernel_type() != K_CUSTOM) k->remove_rhs(); - SG_UNREF(k); + } - CKernel::remove_rhs(); + Kernel::remove_rhs(); num_rhs=0; } -//Adapted from CCombinedKernel -void CProductKernel::remove_lhs_and_rhs() +//Adapted from CombinedKernel +void ProductKernel::remove_lhs_and_rhs() { for (index_t k_idx=0; k_idxget_kernel_type() != K_CUSTOM) k->remove_lhs_and_rhs(); - SG_UNREF(k); + } - CKernel::remove_lhs_and_rhs(); + Kernel::remove_lhs_and_rhs(); num_lhs=0; num_rhs=0; } -//Adapted from CCombinedKernel -void CProductKernel::cleanup() +//Adapted from CombinedKernel +void ProductKernel::cleanup() { for (index_t k_idx=0; k_idxcleanup(); - SG_UNREF(k); + } - CKernel::cleanup(); + Kernel::cleanup(); num_lhs=0; num_rhs=0; } -//Adapted from CCombinedKernel -void CProductKernel::list_kernels() +//Adapted from CombinedKernel +void ProductKernel::list_kernels() { io::info("BEGIN PRODUCT KERNEL LIST - "); this->list_kernel(); for (index_t k_idx=0; k_idxlist_kernel(); - SG_UNREF(k); + } io::info("END PRODUCT KERNEL LIST - "); } -//Adapted from CCombinedKernel -float64_t CProductKernel::compute(int32_t x, int32_t y) +//Adapted from CombinedKernel +float64_t ProductKernel::compute(int32_t x, int32_t y) { float64_t result=1; for (index_t k_idx=0; k_idxget_combined_kernel_weight() * k->kernel(x,y); - SG_UNREF(k); + } return result; } -//Adapted from CCombinedKernel -bool CProductKernel::precompute_subkernels() +//Adapted from CombinedKernel +bool ProductKernel::precompute_subkernels() { if (get_num_subkernels()==0) return false; - CDynamicObjectArray* new_kernel_array=new CDynamicObjectArray(); + auto new_kernel_array=std::make_shared(); for (index_t k_idx=0; k_idxappend_element(new CCustomKernel(k)); - SG_UNREF(k); + auto k=get_kernel(k_idx); + new_kernel_array->append_element(std::make_shared(k)); + } - SG_UNREF(kernel_array); + kernel_array=new_kernel_array; - SG_REF(kernel_array); + return true; } -void CProductKernel::init() +void ProductKernel::init() { initialized=false; properties=KP_NONE; - kernel_array=new CDynamicObjectArray(); - SG_REF(kernel_array); + kernel_array=std::make_shared(); + - SG_ADD((CSGObject**) &kernel_array, "kernel_array", "Array of kernels", + SG_ADD((std::shared_ptr*) &kernel_array, "kernel_array", "Array of kernels", ParameterProperties::HYPER); SG_ADD(&initialized, "initialized", "Whether kernel is ready to be used"); } -SGMatrix CProductKernel::get_parameter_gradient( +SGMatrix ProductKernel::get_parameter_gradient( const TParameter* param, index_t index) { - CKernel* k=get_kernel(0); + auto k=get_kernel(0); SGMatrix temp_kernel=k->get_kernel_matrix(); - SG_UNREF(k); + bool found_derivative=false; @@ -273,7 +270,7 @@ SGMatrix CProductKernel::get_parameter_gradient( } } - SG_UNREF(k); + } if (found_derivative) diff --git a/src/shogun/kernel/ProductKernel.h b/src/shogun/kernel/ProductKernel.h index 452a6736af6..39882b19287 100644 --- a/src/shogun/kernel/ProductKernel.h +++ b/src/shogun/kernel/ProductKernel.h @@ -19,9 +19,9 @@ namespace shogun { -class CFeatures; -class CCombinedFeatures; -class CDynamicObjectArray; +class Features; +class CombinedFeatures; +class DynamicObjectArray; /** @brief The Product kernel is used to combine a number of kernels into a * single ProductKernel object by element multiplication. @@ -34,16 +34,16 @@ class CDynamicObjectArray; * k_{product}({\bf x}, {\bf x'}) = \prod_{m=1}^M k_m({\bf x}, {\bf x'}) * \f] */ -class CProductKernel : public CKernel +class ProductKernel : public Kernel { public: /** constructor * * @param size cache size */ - CProductKernel(int32_t size=10); + ProductKernel(int32_t size=10); - virtual ~CProductKernel(); + virtual ~ProductKernel(); /** initialize kernel * @@ -51,7 +51,7 @@ class CProductKernel : public CKernel * @param rhs features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* lhs, CFeatures* rhs); + virtual bool init(std::shared_ptr lhs, std::shared_ptr rhs); /** clean up kernel */ virtual void cleanup(); @@ -88,9 +88,9 @@ class CProductKernel : public CKernel * @param idx index of kernel * @return kernel at index idx */ - inline CKernel* get_kernel(int32_t idx) + inline std::shared_ptr get_kernel(int32_t idx) { - return (CKernel*) kernel_array->get_element(idx); + return std::static_pointer_cast( kernel_array->get_element(idx)); } /** insert kernel at position idx @@ -100,7 +100,7 @@ class CProductKernel : public CKernel * @param idx the position where to add the kernel * @return if inserting was successful */ - inline bool insert_kernel(CKernel* k, int32_t idx) + inline bool insert_kernel(std::shared_ptr k, int32_t idx) { ASSERT(k) adjust_num_lhs_rhs_initialized(k); @@ -116,7 +116,7 @@ class CProductKernel : public CKernel * @param k kernel * @return if appending was successful */ - inline bool append_kernel(CKernel* k) + inline bool append_kernel(std::shared_ptr k) { ASSERT(k) adjust_num_lhs_rhs_initialized(k); @@ -172,9 +172,9 @@ class CProductKernel : public CKernel /** casts kernel to combined kernel * @param n kernel to cast */ - CProductKernel* KernelToProductKernel(CKernel* n) + std::shared_ptr KernelToProductKernel(std::shared_ptr n) { - return dynamic_cast(n); + return std::dynamic_pointer_cast(n); } /** return derivative with respect to specified parameter @@ -191,9 +191,9 @@ class CProductKernel : public CKernel * * @return kernel array */ - inline CDynamicObjectArray* get_array() + inline std::shared_ptr get_array() { - SG_REF(kernel_array); + return kernel_array; } @@ -211,7 +211,7 @@ class CProductKernel : public CKernel * * @param k kernel */ - inline void adjust_num_lhs_rhs_initialized(CKernel* k) + inline void adjust_num_lhs_rhs_initialized(std::shared_ptr k) { ASSERT(k) @@ -255,7 +255,7 @@ class CProductKernel : public CKernel protected: /** array of kernels */ - CDynamicObjectArray* kernel_array; + std::shared_ptr kernel_array; /** whether kernel is ready to be used */ bool initialized; }; diff --git a/src/shogun/kernel/PyramidChi2.cpp b/src/shogun/kernel/PyramidChi2.cpp index 3ee37da163e..06a2a16b228 100644 --- a/src/shogun/kernel/PyramidChi2.cpp +++ b/src/shogun/kernel/PyramidChi2.cpp @@ -14,7 +14,7 @@ using namespace shogun; -CPyramidChi2::CPyramidChi2() +PyramidChi2::PyramidChi2() : weights(NULL) { // this will produce an erro in kernel computation! @@ -24,17 +24,17 @@ CPyramidChi2::CPyramidChi2() num_randfeats_forwidthcomputation=-1; } -CPyramidChi2::CPyramidChi2( +PyramidChi2::PyramidChi2( int32_t size, int32_t num_cells2, float64_t* weights_foreach_cell2, int32_t width_computation_type2, float64_t width2) -: RandomMixin(size), num_cells(num_cells2),weights(NULL), +: RandomMixin(size), num_cells(num_cells2),weights(NULL), width_computation_type(width_computation_type2), width(width2), num_randfeats_forwidthcomputation(-1) { if(num_cells<=0) - error("CPyramidChi2 Constructor fatal error: parameter num_cells2 NOT positive"); + error("PyramidChi2 Constructor fatal error: parameter num_cells2 NOT positive"); weights=SG_MALLOC(float64_t, num_cells); if(weights_foreach_cell2) { @@ -48,14 +48,14 @@ width_computation_type(width_computation_type2), width(width2), if (width_computation_type>0 ) { - num_randfeats_forwidthcomputation=(int32_t)CMath::round(width); + num_randfeats_forwidthcomputation=(int32_t)Math::round(width); width=-1; } } -void CPyramidChi2::cleanup() +void PyramidChi2::cleanup() { // this will produce an erro in kernel computation! num_cells=0; @@ -67,27 +67,27 @@ void CPyramidChi2::cleanup() SG_FREE(weights); weights=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } -bool CPyramidChi2::init(CFeatures* l, CFeatures* r) +bool PyramidChi2::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } -CPyramidChi2::CPyramidChi2( - CDenseFeatures* l, CDenseFeatures* r, +PyramidChi2::PyramidChi2( + std::shared_ptr> l, std::shared_ptr> r, int32_t size, int32_t num_cells2, float64_t* weights_foreach_cell2, int32_t width_computation_type2, float64_t width2) -: RandomMixin(size), num_cells(num_cells2), weights(NULL), +: RandomMixin(size), num_cells(num_cells2), weights(NULL), width_computation_type(width_computation_type2), width(width2), num_randfeats_forwidthcomputation(-1) { if(num_cells<=0) - error("CPyramidChi2 Constructor fatal error: parameter num_cells2 NOT positive"); + error("PyramidChi2 Constructor fatal error: parameter num_cells2 NOT positive"); weights=SG_MALLOC(float64_t, num_cells); if(weights_foreach_cell2) { @@ -101,35 +101,37 @@ width_computation_type(width_computation_type2), width(width2), if (width_computation_type>0 ) { - num_randfeats_forwidthcomputation=(int32_t)CMath::round(width); + num_randfeats_forwidthcomputation=(int32_t)Math::round(width); width=-1; } init(l, r); } -CPyramidChi2::~CPyramidChi2() +PyramidChi2::~PyramidChi2() { cleanup(); } -float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b) +float64_t PyramidChi2::compute(int32_t idx_a, int32_t idx_b) { if(num_cells<=0) - error("CPyramidChi2::compute(...) fatal error: parameter num_cells NOT positive"); + error("PyramidChi2::compute(...) fatal error: parameter num_cells NOT positive"); int32_t alen, blen; bool afree, bfree; - float64_t* avec=((CDenseFeatures*) lhs)->get_feature_vector(idx_a, + auto df_lhs = std::static_pointer_cast>(lhs); + auto df_rhs = std::static_pointer_cast>(rhs); + float64_t* avec=df_lhs->get_feature_vector(idx_a, alen, afree); - float64_t* bvec=((CDenseFeatures*) rhs)->get_feature_vector(idx_b, + float64_t* bvec=df_rhs->get_feature_vector(idx_b, blen, bfree); if(alen!=blen) - error("CPyramidChi2::compute(...) fatal error: lhs feature dim != rhs feature dim"); + error("PyramidChi2::compute(...) fatal error: lhs feature dim != rhs feature dim"); int32_t dims=alen/num_cells; @@ -144,11 +146,11 @@ float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b) if (num_randfeats_forwidthcomputation >1) { - numind=CMath::min( ((CDenseFeatures*) lhs)->get_num_vectors() , num_randfeats_forwidthcomputation); + numind=Math::min(df_lhs->get_num_vectors() , num_randfeats_forwidthcomputation); } else { - numind= ((CDenseFeatures*) lhs)->get_num_vectors(); + numind= df_lhs->get_num_vectors(); } float64_t* featindices = SG_MALLOC(float64_t, numind); @@ -156,7 +158,7 @@ float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b) { random::fill_array( featindices, featindices + numind, 0, - ((CDenseFeatures*) lhs)->get_num_vectors()-1, m_prng); + lhs->as>()->get_num_vectors()-1, m_prng); } else { @@ -170,12 +172,12 @@ float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b) //get avec, get bvec only from lhs, do not free for (int32_t li=0; li < numind;++li) { - avec=((CDenseFeatures*) lhs)->get_feature_vector(featindices[li], + avec=df_lhs->get_feature_vector(featindices[li], alen, afree); for (int32_t ri=0; ri <=li;++ri) { // lhs is right here!!! - bvec=((CDenseFeatures*) lhs)->get_feature_vector(featindices[ri], + bvec=df_lhs->get_feature_vector(featindices[ri], blen, bfree); float64_t result=0; @@ -201,15 +203,15 @@ float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b) } else { - error("CPyramidChi2::compute(...) fatal error: width<=0"); + error("PyramidChi2::compute(...) fatal error: width<=0"); } } //the actual kernel computation - avec=((CDenseFeatures*) lhs)->get_feature_vector(idx_a, + avec=df_lhs->get_feature_vector(idx_a, alen, afree); - bvec=((CDenseFeatures*) rhs)->get_feature_vector(idx_b, + bvec=df_rhs->get_feature_vector(idx_b, blen, bfree); float64_t result=0; @@ -229,13 +231,13 @@ float64_t CPyramidChi2::compute(int32_t idx_a, int32_t idx_b) } result = std::exp(-result / width); - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + df_lhs->free_feature_vector(avec, idx_a, afree); + df_rhs->free_feature_vector(bvec, idx_b, bfree); return (result); } -void CPyramidChi2::setparams_pychi2(int32_t num_cells2, +void PyramidChi2::setparams_pychi2(int32_t num_cells2, float64_t* weights_foreach_cell2, int32_t width_computation_type2, float64_t width2) @@ -246,7 +248,7 @@ void CPyramidChi2::setparams_pychi2(int32_t num_cells2, num_randfeats_forwidthcomputation=-1; if(num_cells<=0) - error("CPyramidChi2::setparams_pychi2(...) fatal error: parameter num_cells2 NOT positive"); + error("PyramidChi2::setparams_pychi2(...) fatal error: parameter num_cells2 NOT positive"); if(weights) SG_FREE(weights); weights=SG_MALLOC(float64_t, num_cells); @@ -262,7 +264,7 @@ void CPyramidChi2::setparams_pychi2(int32_t num_cells2, if (width_computation_type>0 ) { - num_randfeats_forwidthcomputation=(int32_t)CMath::round(width); + num_randfeats_forwidthcomputation=(int32_t)Math::round(width); width=-1; } } diff --git a/src/shogun/kernel/PyramidChi2.h b/src/shogun/kernel/PyramidChi2.h index f4a0a005560..4253663ace3 100644 --- a/src/shogun/kernel/PyramidChi2.h +++ b/src/shogun/kernel/PyramidChi2.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Yuyu Zhang, Evan Shelhamer, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Yuyu Zhang, Evan Shelhamer, * Bjoern Esser, Sergey Lisitsyn */ @@ -18,18 +18,18 @@ namespace shogun { - template class CDenseFeatures; + template class DenseFeatures; /** @brief Pyramid Kernel over Chi2 matched histograms. * * * */ -class CPyramidChi2 : public RandomMixin +class PyramidChi2 : public RandomMixin { public: /** default constructor protected to avoid its usage */ - CPyramidChi2(); + PyramidChi2(); /** constructor * @@ -45,7 +45,7 @@ class CPyramidChi2 : public RandomMixin * in case of width_computation_type > 0 set width2 <=1 to use all * LEFT HAND SIDE features for width estimation */ - CPyramidChi2(int32_t size, int32_t num_cells2, + PyramidChi2(int32_t size, int32_t num_cells2, float64_t* weights_foreach_cell2, int32_t width_computation_type2, float64_t width2); @@ -68,8 +68,8 @@ class CPyramidChi2 : public RandomMixin * in case of width_computation_type > 0 set width2 <=1 to use all * LEFT HAND SIDE features for width estimation */ - CPyramidChi2( - CDenseFeatures* l, CDenseFeatures* r, + PyramidChi2( + std::shared_ptr> l, std::shared_ptr> r, int32_t size, int32_t num_cells2, float64_t* weights_foreach_cell2, int32_t width_computation_type2, @@ -80,10 +80,10 @@ class CPyramidChi2 : public RandomMixin * @param l features lhs * @param r reatures rhs */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); - virtual ~CPyramidChi2(); + virtual ~PyramidChi2(); /** cleanup */ virtual void cleanup(); diff --git a/src/shogun/kernel/RationalQuadraticKernel.cpp b/src/shogun/kernel/RationalQuadraticKernel.cpp index e3465053b9e..d9781f0c663 100644 --- a/src/shogun/kernel/RationalQuadraticKernel.cpp +++ b/src/shogun/kernel/RationalQuadraticKernel.cpp @@ -9,50 +9,50 @@ using namespace shogun; -CRationalQuadraticKernel::CRationalQuadraticKernel(): CKernel(0), m_distance(NULL), m_coef(0.001) +RationalQuadraticKernel::RationalQuadraticKernel(): Kernel(0), m_distance(NULL), m_coef(0.001) { init(); } -CRationalQuadraticKernel::CRationalQuadraticKernel(int32_t cache, float64_t coef, CDistance* distance) -: CKernel(cache), m_distance(distance), m_coef(coef) +RationalQuadraticKernel::RationalQuadraticKernel(int32_t cache, float64_t coef, std::shared_ptr distance) +: Kernel(cache), m_distance(distance), m_coef(coef) { ASSERT(m_distance) - SG_REF(m_distance); + init(); } -CRationalQuadraticKernel::CRationalQuadraticKernel(CFeatures *l, CFeatures *r, float64_t coef, CDistance* dist) -: CKernel(10), m_distance(dist), m_coef(coef) +RationalQuadraticKernel::RationalQuadraticKernel(std::shared_ptrl, std::shared_ptrr, float64_t coef, std::shared_ptr dist) +: Kernel(10), m_distance(dist), m_coef(coef) { ASSERT(m_distance) - SG_REF(m_distance); + init(); init(l, r); } -CRationalQuadraticKernel::~CRationalQuadraticKernel() +RationalQuadraticKernel::~RationalQuadraticKernel() { cleanup(); - SG_UNREF(m_distance); + } -bool CRationalQuadraticKernel::init(CFeatures* l, CFeatures* r) +bool RationalQuadraticKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(m_distance) - CKernel::init(l,r); + Kernel::init(l,r); m_distance->init(l,r); return init_normalizer(); } -float64_t CRationalQuadraticKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t RationalQuadraticKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = m_distance->distance(idx_a, idx_b); float64_t pDist = dist * dist; return 1-pDist/(pDist+m_coef); } -void CRationalQuadraticKernel::init() +void RationalQuadraticKernel::init() { SG_ADD(&m_coef, "coef", "Kernel coefficient.", ParameterProperties::HYPER); SG_ADD(&m_distance, "distance", "Distance to be used.", diff --git a/src/shogun/kernel/RationalQuadraticKernel.h b/src/shogun/kernel/RationalQuadraticKernel.h index a3d2c697d66..0fed3542943 100644 --- a/src/shogun/kernel/RationalQuadraticKernel.h +++ b/src/shogun/kernel/RationalQuadraticKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Rational Quadratic kernel * @@ -28,18 +28,18 @@ class CDistance; * */ -class CRationalQuadraticKernel: public CKernel +class RationalQuadraticKernel: public Kernel { public: /** default constructor */ - CRationalQuadraticKernel(); + RationalQuadraticKernel(); /** constructor * @param cache size of cache * @param coef kernel parameter coefficient * @param dist distance to be used */ - CRationalQuadraticKernel(int32_t cache, float64_t coef, CDistance* dist); + RationalQuadraticKernel(int32_t cache, float64_t coef, std::shared_ptr dist); /** constructor * @param l features left-side @@ -47,14 +47,14 @@ class CRationalQuadraticKernel: public CKernel * @param c kernel parameter coefficient * @param dist distance to be used */ - CRationalQuadraticKernel(CFeatures *l, CFeatures *r, float64_t c, CDistance* dist); + RationalQuadraticKernel(std::shared_ptrl, std::shared_ptrr, float64_t c, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -86,7 +86,7 @@ class CRationalQuadraticKernel: public CKernel */ inline void set_coef(float64_t coef) { m_coef=coef; } - virtual ~CRationalQuadraticKernel(); + virtual ~RationalQuadraticKernel(); protected: /** * compute kernel for specific feature vectors @@ -103,7 +103,7 @@ class CRationalQuadraticKernel: public CKernel protected: /// distance to be used - CDistance* m_distance; + std::shared_ptr m_distance; /// coefficient parameter of kernel float64_t m_coef; diff --git a/src/shogun/kernel/ShiftInvariantKernel.cpp b/src/shogun/kernel/ShiftInvariantKernel.cpp index 0cce7c0fd56..9245a0d7858 100644 --- a/src/shogun/kernel/ShiftInvariantKernel.cpp +++ b/src/shogun/kernel/ShiftInvariantKernel.cpp @@ -34,32 +34,32 @@ using namespace shogun; -CShiftInvariantKernel::CShiftInvariantKernel() : CKernel(0) +ShiftInvariantKernel::ShiftInvariantKernel() : Kernel(0) { register_params(); } -CShiftInvariantKernel::CShiftInvariantKernel(CFeatures *l, CFeatures *r) : CKernel(l, r, 0) +ShiftInvariantKernel::ShiftInvariantKernel(std::shared_ptrl, std::shared_ptrr) : Kernel(l, r, 0) { register_params(); init(l, r); } -CShiftInvariantKernel::~CShiftInvariantKernel() +ShiftInvariantKernel::~ShiftInvariantKernel() { cleanup(); - SG_UNREF(m_distance); + } -bool CShiftInvariantKernel::init(CFeatures* l, CFeatures* r) +bool ShiftInvariantKernel::init(std::shared_ptr l, std::shared_ptr r) { require(m_distance, "The distance instance cannot be NULL!"); - CKernel::init(l,r); + Kernel::init(l,r); m_distance->init(l, r); return init_normalizer(); } -void CShiftInvariantKernel::precompute_distance() +void ShiftInvariantKernel::precompute_distance() { require(m_distance, "The distance instance cannot be NULL!"); require(m_distance->init(lhs, rhs), "Could not initialize the distance instance!"); @@ -67,8 +67,8 @@ void CShiftInvariantKernel::precompute_distance() SGMatrix dist_mat=m_distance->get_distance_matrix(); if (m_precomputed_distance==NULL) { - m_precomputed_distance=new CCustomDistance(); - SG_REF(m_precomputed_distance); + m_precomputed_distance=std::make_shared(); + } if (lhs==rhs) @@ -77,21 +77,21 @@ void CShiftInvariantKernel::precompute_distance() m_precomputed_distance->set_full_distance_matrix_from_full(dist_mat.data(), dist_mat.num_rows, dist_mat.num_cols); } -void CShiftInvariantKernel::cleanup() +void ShiftInvariantKernel::cleanup() { - SG_UNREF(m_precomputed_distance); + m_precomputed_distance=NULL; - CKernel::cleanup(); + Kernel::cleanup(); m_distance->cleanup(); } -EDistanceType CShiftInvariantKernel::get_distance_type() const +EDistanceType ShiftInvariantKernel::get_distance_type() const { require(m_distance, "The distance instance cannot be NULL!"); return m_distance->get_distance_type(); } -float64_t CShiftInvariantKernel::distance(int32_t a, int32_t b) const +float64_t ShiftInvariantKernel::distance(int32_t a, int32_t b) const { require(m_distance, "The distance instance cannot be NULL!"); if (m_precomputed_distance!=NULL) @@ -100,26 +100,23 @@ float64_t CShiftInvariantKernel::distance(int32_t a, int32_t b) const return m_distance->distance(a, b); } -void CShiftInvariantKernel::register_params() +void ShiftInvariantKernel::register_params() { - SG_ADD((CSGObject**) &m_distance, "m_distance", "Distance to be used."); - SG_ADD((CSGObject**) &m_precomputed_distance, "m_precomputed_distance", "Precomputed istance to be used."); + SG_ADD((std::shared_ptr*) &m_distance, "m_distance", "Distance to be used."); + SG_ADD((std::shared_ptr*) &m_precomputed_distance, "m_precomputed_distance", "Precomputed istance to be used."); m_distance=NULL; m_precomputed_distance=NULL; } -void CShiftInvariantKernel::set_precomputed_distance(CCustomDistance* precomputed_distance) +void ShiftInvariantKernel::set_precomputed_distance(std::shared_ptr precomputed_distance) { require(precomputed_distance, "The precomputed distance instance cannot be NULL!"); - SG_REF(precomputed_distance); - SG_UNREF(m_precomputed_distance); m_precomputed_distance=precomputed_distance; } -CCustomDistance* CShiftInvariantKernel::get_precomputed_distance() const +std::shared_ptr ShiftInvariantKernel::get_precomputed_distance() const { require(m_precomputed_distance, "The precomputed distance instance cannot be NULL!"); - SG_REF(m_precomputed_distance); return m_precomputed_distance; } diff --git a/src/shogun/kernel/ShiftInvariantKernel.h b/src/shogun/kernel/ShiftInvariantKernel.h index 3f75646a45d..5a95ad12cdf 100644 --- a/src/shogun/kernel/ShiftInvariantKernel.h +++ b/src/shogun/kernel/ShiftInvariantKernel.h @@ -52,14 +52,14 @@ namespace internal * \f] * For example, Gaussian (RBF) kernel is a shfit invariant kernel. */ -class CShiftInvariantKernel: public CKernel +class ShiftInvariantKernel: public Kernel { friend class internal::KernelManager; public: /** Default constructor. */ - CShiftInvariantKernel(); + ShiftInvariantKernel(); /** * Constructor that initializes the kernel with two feature instances. @@ -67,10 +67,10 @@ class CShiftInvariantKernel: public CKernel * @param l features of left-hand side * @param r features of right-hand side */ - CShiftInvariantKernel(CFeatures *l, CFeatures *r); + ShiftInvariantKernel(std::shared_ptrl, std::shared_ptrr); /** Destructor. */ - virtual ~CShiftInvariantKernel(); + virtual ~ShiftInvariantKernel(); /** * Initialize kernel. @@ -79,7 +79,7 @@ class CShiftInvariantKernel: public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** Method that precomputes the distance */ virtual void precompute_distance(); @@ -132,24 +132,24 @@ class CShiftInvariantKernel: public CKernel virtual float64_t distance(int32_t idx_a, int32_t idx_b) const; /** Distance instance for the kernel. MUST be initialized by the subclasses */ - CDistance* m_distance; + std::shared_ptr m_distance; private: /** Registers the parameters (serialization support). */ virtual void register_params(); /** Precomputed distance instance */ - CCustomDistance* m_precomputed_distance; + std::shared_ptr m_precomputed_distance; /** * Method that sets a precomputed distance. * * @param precomputed_distance The precomputed distance object. */ - void set_precomputed_distance(CCustomDistance* precomputed_distance); + void set_precomputed_distance(std::shared_ptr precomputed_distance); /** @return the precomputed distance. */ - CCustomDistance* get_precomputed_distance() const; + std::shared_ptr get_precomputed_distance() const; }; diff --git a/src/shogun/kernel/SigmoidKernel.cpp b/src/shogun/kernel/SigmoidKernel.cpp index 26d8826b1a1..0ad7d979da9 100644 --- a/src/shogun/kernel/SigmoidKernel.cpp +++ b/src/shogun/kernel/SigmoidKernel.cpp @@ -11,13 +11,13 @@ using namespace shogun; -CSigmoidKernel::CSigmoidKernel() : CDotKernel() +SigmoidKernel::SigmoidKernel() : DotKernel() { init(); } -CSigmoidKernel::CSigmoidKernel(int32_t size, float64_t g, float64_t c) - : CDotKernel(size) +SigmoidKernel::SigmoidKernel(int32_t size, float64_t g, float64_t c) + : DotKernel(size) { init(); @@ -25,9 +25,9 @@ CSigmoidKernel::CSigmoidKernel(int32_t size, float64_t g, float64_t c) coef0 = c; } -CSigmoidKernel::CSigmoidKernel( - CDotFeatures* l, CDotFeatures* r, int32_t size, float64_t g, float64_t c) - : CDotKernel(size) +SigmoidKernel::SigmoidKernel( + std::shared_ptr l, std::shared_ptr r, int32_t size, float64_t g, float64_t c) + : DotKernel(size) { init(); @@ -37,22 +37,22 @@ CSigmoidKernel::CSigmoidKernel( init(l, r); } -CSigmoidKernel::~CSigmoidKernel() +SigmoidKernel::~SigmoidKernel() { cleanup(); } -void CSigmoidKernel::cleanup() +void SigmoidKernel::cleanup() { } -bool CSigmoidKernel::init(CFeatures* l, CFeatures* r) +bool SigmoidKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } -void CSigmoidKernel::init() +void SigmoidKernel::init() { gamma = 0.0; coef0 = 0.0; diff --git a/src/shogun/kernel/SigmoidKernel.h b/src/shogun/kernel/SigmoidKernel.h index 8dd2bfe20ee..248a775e869 100644 --- a/src/shogun/kernel/SigmoidKernel.h +++ b/src/shogun/kernel/SigmoidKernel.h @@ -23,11 +23,11 @@ namespace shogun * k({\bf x},{\bf x'})=\mbox{tanh}(\gamma {\bf x}\cdot{\bf x'}+c) * \f] */ -class CSigmoidKernel: public CDotKernel +class SigmoidKernel: public DotKernel { public: /** default constructor */ - CSigmoidKernel(); + SigmoidKernel(); /** constructor * @@ -35,7 +35,7 @@ class CSigmoidKernel: public CDotKernel * @param gamma gamma * @param coef0 coefficient 0 */ - CSigmoidKernel(int32_t size, float64_t gamma, float64_t coef0); + SigmoidKernel(int32_t size, float64_t gamma, float64_t coef0); /** constructor * @@ -45,10 +45,10 @@ class CSigmoidKernel: public CDotKernel * @param gamma gamma * @param coef0 coefficient 0 */ - CSigmoidKernel(CDotFeatures* l, CDotFeatures* r, int32_t size, + SigmoidKernel(std::shared_ptr l, std::shared_ptr r, int32_t size, float64_t gamma, float64_t coef0); - virtual ~CSigmoidKernel(); + virtual ~SigmoidKernel(); /** initialize kernel * @@ -56,7 +56,7 @@ class CSigmoidKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -84,7 +84,7 @@ class CSigmoidKernel: public CDotKernel */ virtual float64_t compute(int32_t idx_a, int32_t idx_b) { - return tanh(gamma*CDotKernel::compute(idx_a,idx_b)+coef0); + return tanh(gamma*DotKernel::compute(idx_a,idx_b)+coef0); } private: diff --git a/src/shogun/kernel/SparseKernel.h b/src/shogun/kernel/SparseKernel.h index ec7e54d1d0e..fcd37823358 100644 --- a/src/shogun/kernel/SparseKernel.h +++ b/src/shogun/kernel/SparseKernel.h @@ -19,21 +19,21 @@ namespace shogun * * See e.g. the CSparseGaussianKernel for an example. */ -template class CSparseKernel : public CKernel +template class SparseKernel : public Kernel { public: /** constructor * * @param cachesize cache size */ - CSparseKernel(int32_t cachesize) : CKernel(cachesize) {} + SparseKernel(int32_t cachesize) : Kernel(cachesize) {} /** constructor * * @param l features for left-hand side * @param r features for right-hand side */ - CSparseKernel(CFeatures* l, CFeatures* r) : CKernel(10) + SparseKernel(std::shared_ptr l, std::shared_ptr r) : Kernel(10) { init(l, r); } @@ -44,19 +44,22 @@ template class CSparseKernel : public CKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l,r); + Kernel::init(l,r); ASSERT(l->get_feature_class()==C_SPARSE) ASSERT(r->get_feature_class()==C_SPARSE) ASSERT(l->get_feature_type()==this->get_feature_type()) ASSERT(r->get_feature_type()==this->get_feature_type()) - if (((CSparseFeatures*) lhs)->get_num_features() != ((CSparseFeatures*) rhs)->get_num_features()) + auto sf_lhs = lhs->as>(); + auto sf_rhs = rhs->as>(); + + if (sf_lhs->get_num_features() != sf_rhs->get_num_features()) { error("train or test features #dimension mismatch (l:{} vs. r:{})", - ((CSparseFeatures*) lhs)->get_num_features(),((CSparseFeatures*)rhs)->get_num_features()); + sf_lhs->get_num_features(),sf_rhs->get_num_features()); } return true; } @@ -91,18 +94,18 @@ template class CSparseKernel : public CKernel virtual EKernelType get_kernel_type()=0; }; -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_DREAL; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_DREAL; } -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_ULONG; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_ULONG; } -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_INT; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_INT; } -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_WORD; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_WORD; } -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_SHORT; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_SHORT; } -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_BYTE; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_BYTE; } -template<> inline EFeatureType CSparseKernel::get_feature_type() { return F_CHAR; } +template<> inline EFeatureType SparseKernel::get_feature_type() { return F_CHAR; } } #endif /* _SPARSEKERNEL_H__ */ diff --git a/src/shogun/kernel/SphericalKernel.cpp b/src/shogun/kernel/SphericalKernel.cpp index 6513cc319ba..b2271960a34 100644 --- a/src/shogun/kernel/SphericalKernel.cpp +++ b/src/shogun/kernel/SphericalKernel.cpp @@ -9,54 +9,54 @@ using namespace shogun; -CSphericalKernel::CSphericalKernel(): CKernel(0), distance(NULL) +SphericalKernel::SphericalKernel(): Kernel(0), distance(NULL) { register_params(); set_sigma(1.0); } -CSphericalKernel::CSphericalKernel(int32_t size, float64_t sig, CDistance* dist) -: CKernel(size), distance(dist) +SphericalKernel::SphericalKernel(int32_t size, float64_t sig, std::shared_ptr dist) +: Kernel(size), distance(dist) { ASSERT(distance) - SG_REF(distance); + register_params(); set_sigma(sig); } -CSphericalKernel::CSphericalKernel( - CFeatures *l, CFeatures *r, float64_t sig, CDistance* dist) -: CKernel(10), distance(dist) +SphericalKernel::SphericalKernel( + std::shared_ptrl, std::shared_ptrr, float64_t sig, std::shared_ptr dist) +: Kernel(10), distance(dist) { ASSERT(distance) - SG_REF(distance); + register_params(); set_sigma(sig); init(l, r); } -CSphericalKernel::~CSphericalKernel() +SphericalKernel::~SphericalKernel() { cleanup(); - SG_UNREF(distance); + } -bool CSphericalKernel::init(CFeatures* l, CFeatures* r) +bool SphericalKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(distance) - CKernel::init(l,r); + Kernel::init(l,r); distance->init(l,r); return init_normalizer(); } -void CSphericalKernel::register_params() +void SphericalKernel::register_params() { SG_ADD(&distance, "distance", "Distance to be used.", ParameterProperties::HYPER); SG_ADD(&sigma, "sigma", "Sigma kernel parameter.", ParameterProperties::HYPER); } -float64_t CSphericalKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SphericalKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist=distance->distance(idx_a, idx_b); float64_t ds_ratio=dist/sigma; diff --git a/src/shogun/kernel/SphericalKernel.h b/src/shogun/kernel/SphericalKernel.h index b1180bc7b8d..e8bd3f63a64 100644 --- a/src/shogun/kernel/SphericalKernel.h +++ b/src/shogun/kernel/SphericalKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Spherical kernel * @@ -30,11 +30,11 @@ class CDistance; * */ -class CSphericalKernel: public CKernel +class SphericalKernel: public Kernel { public: /** default constructor */ - CSphericalKernel(); + SphericalKernel(); /** constructor * @@ -42,7 +42,7 @@ class CSphericalKernel: public CKernel * @param sigma kernel parameter sigma * @param dist distance */ - CSphericalKernel(int32_t size, float64_t sigma, CDistance* dist); + SphericalKernel(int32_t size, float64_t sigma, std::shared_ptr dist); /** constructor * @@ -51,7 +51,7 @@ class CSphericalKernel: public CKernel * @param sigma kernel parameter sigma * @param dist distance */ - CSphericalKernel(CFeatures *l, CFeatures *r, float64_t sigma, CDistance* dist); + SphericalKernel(std::shared_ptrl, std::shared_ptrr, float64_t sigma, std::shared_ptr dist); /** initialize kernel with features * @@ -59,7 +59,7 @@ class CSphericalKernel: public CKernel * @param r features of right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -99,7 +99,7 @@ class CSphericalKernel: public CKernel return sigma; } - virtual ~CSphericalKernel(); + virtual ~SphericalKernel(); private: /** register parameters */ @@ -108,7 +108,7 @@ class CSphericalKernel: public CKernel protected: /** distance */ - CDistance* distance; + std::shared_ptr distance; /** width */ float64_t sigma; diff --git a/src/shogun/kernel/SplineKernel.cpp b/src/shogun/kernel/SplineKernel.cpp index 3fd3a9993cd..132724e4ac0 100644 --- a/src/shogun/kernel/SplineKernel.cpp +++ b/src/shogun/kernel/SplineKernel.cpp @@ -13,21 +13,21 @@ using namespace shogun; -CSplineKernel::CSplineKernel() : CDotKernel() +SplineKernel::SplineKernel() : DotKernel() { } -CSplineKernel::CSplineKernel(CDotFeatures* l, CDotFeatures* r) : CDotKernel() +SplineKernel::SplineKernel(std::shared_ptr l, std::shared_ptr r) : DotKernel() { init(l,r); } -CSplineKernel::~CSplineKernel() +SplineKernel::~SplineKernel() { cleanup(); } -bool CSplineKernel::init(CFeatures* l, CFeatures* r) +bool SplineKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(l->get_feature_type()==F_DREAL) ASSERT(l->get_feature_type()==r->get_feature_type()) @@ -35,33 +35,33 @@ bool CSplineKernel::init(CFeatures* l, CFeatures* r) ASSERT(l->get_feature_class()==C_DENSE) ASSERT(l->get_feature_class()==r->get_feature_class()) - CDotKernel::init(l,r); + DotKernel::init(l,r); return init_normalizer(); } -void CSplineKernel::cleanup() +void SplineKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CSplineKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SplineKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; - float64_t* avec = ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); - float64_t* bvec = ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + float64_t* avec = (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); + float64_t* bvec = (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen == blen) float64_t result = 0; for (int32_t i = 0; i < alen; i++) { const float64_t x = avec[i], y = bvec[i]; - const float64_t min = CMath::min(avec[i], bvec[i]); + const float64_t min = Math::min(avec[i], bvec[i]); result += 1 + x*y + x*y*min - ((x+y)/2)*min*min + min*min*min/3; } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/kernel/SplineKernel.h b/src/shogun/kernel/SplineKernel.h index 4b4fb84dcd4..5b5dadf58b2 100644 --- a/src/shogun/kernel/SplineKernel.h +++ b/src/shogun/kernel/SplineKernel.h @@ -16,8 +16,8 @@ namespace shogun { - class CKernelMachine; - class CDotFeatures; + class KernelMachine; + class DotFeatures; /** @brief Computes the Spline Kernel function which is the cubic polynomial * @@ -30,21 +30,21 @@ namespace shogun * \frac{ \mbox{min}(\bf x, \bf x')^3}{3} * \f] */ -class CSplineKernel: public CDotKernel +class SplineKernel: public DotKernel { public: /** constructor */ - CSplineKernel(); + SplineKernel(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CSplineKernel(CDotFeatures* l, CDotFeatures* r); + SplineKernel(std::shared_ptr l, std::shared_ptr r); - virtual ~CSplineKernel(); + virtual ~SplineKernel(); /** initialize kernel * @@ -52,7 +52,7 @@ class CSplineKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/TStudentKernel.cpp b/src/shogun/kernel/TStudentKernel.cpp index c3147298648..bf784378d96 100644 --- a/src/shogun/kernel/TStudentKernel.cpp +++ b/src/shogun/kernel/TStudentKernel.cpp @@ -9,51 +9,51 @@ using namespace shogun; -void CTStudentKernel::init() +void TStudentKernel::init() { SG_ADD(°ree, "degree", "Kernel degree.", ParameterProperties::HYPER); SG_ADD(&distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } -CTStudentKernel::CTStudentKernel(): CKernel(0), distance(NULL), degree(1.0) +TStudentKernel::TStudentKernel(): Kernel(0), distance(NULL), degree(1.0) { init(); } -CTStudentKernel::CTStudentKernel(int32_t cache, float64_t d, CDistance* dist) -: CKernel(cache), distance(dist), degree(d) +TStudentKernel::TStudentKernel(int32_t cache, float64_t d, std::shared_ptr dist) +: Kernel(cache), distance(dist), degree(d) { init(); ASSERT(distance) - SG_REF(distance); + } -CTStudentKernel::CTStudentKernel(CFeatures *l, CFeatures *r, float64_t d, CDistance* dist) -: CKernel(10), distance(dist), degree(d) +TStudentKernel::TStudentKernel(std::shared_ptrl, std::shared_ptrr, float64_t d, std::shared_ptr dist) +: Kernel(10), distance(dist), degree(d) { init(); ASSERT(distance) - SG_REF(distance); + init(l, r); } -CTStudentKernel::~CTStudentKernel() +TStudentKernel::~TStudentKernel() { cleanup(); - SG_UNREF(distance); + } -bool CTStudentKernel::init(CFeatures* l, CFeatures* r) +bool TStudentKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(distance) - CKernel::init(l,r); + Kernel::init(l,r); distance->init(l,r); return init_normalizer(); } -float64_t CTStudentKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t TStudentKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = distance->distance(idx_a, idx_b); - return 1.0/(1.0+CMath::pow(dist, this->degree)); + return 1.0/(1.0+Math::pow(dist, this->degree)); } diff --git a/src/shogun/kernel/TStudentKernel.h b/src/shogun/kernel/TStudentKernel.h index 1208313912c..0148cce1d92 100644 --- a/src/shogun/kernel/TStudentKernel.h +++ b/src/shogun/kernel/TStudentKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Generalized T-Student kernel * @@ -28,18 +28,18 @@ class CDistance; * with degree=1 by default */ -class CTStudentKernel: public CKernel +class TStudentKernel: public Kernel { public: /** default constructor */ - CTStudentKernel(); + TStudentKernel(); /** constructor * @param cache size of cache * @param d kernel parameter degree * @param dist distance to be used */ - CTStudentKernel(int32_t cache, float64_t d, CDistance* dist); + TStudentKernel(int32_t cache, float64_t d, std::shared_ptr dist); /** constructor * @param l features left-side @@ -47,16 +47,16 @@ class CTStudentKernel: public CKernel * @param d kernel parameter degree * @param dist distance to be used */ - CTStudentKernel(CFeatures *l, CFeatures *r, float64_t d, CDistance* dist); + TStudentKernel(std::shared_ptrl, std::shared_ptrr, float64_t d, std::shared_ptr dist); - virtual ~CTStudentKernel(); + virtual ~TStudentKernel(); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -94,7 +94,7 @@ class CTStudentKernel: public CKernel protected: /// distance to be used - CDistance* distance; + std::shared_ptr distance; /// degree parameter of kernel float64_t degree; diff --git a/src/shogun/kernel/TensorProductPairKernel.cpp b/src/shogun/kernel/TensorProductPairKernel.cpp index dfe9876ecfa..25b28629784 100644 --- a/src/shogun/kernel/TensorProductPairKernel.cpp +++ b/src/shogun/kernel/TensorProductPairKernel.cpp @@ -11,52 +11,52 @@ using namespace shogun; -CTensorProductPairKernel::CTensorProductPairKernel() -: CDotKernel(0), subkernel(NULL) +TensorProductPairKernel::TensorProductPairKernel() +: DotKernel(0), subkernel(NULL) { register_params(); } -CTensorProductPairKernel::CTensorProductPairKernel(int32_t size, CKernel* s) -: CDotKernel(size), subkernel(s) +TensorProductPairKernel::TensorProductPairKernel(int32_t size, std::shared_ptr s) +: DotKernel(size), subkernel(s) { - SG_REF(subkernel); + register_params(); } -CTensorProductPairKernel::CTensorProductPairKernel(CDenseFeatures* l, CDenseFeatures* r, CKernel* s) -: CDotKernel(10), subkernel(s) +TensorProductPairKernel::TensorProductPairKernel(std::shared_ptr> l, std::shared_ptr> r, std::shared_ptr s) +: DotKernel(10), subkernel(s) { - SG_REF(subkernel); + init(l, r); register_params(); } -CTensorProductPairKernel::~CTensorProductPairKernel() +TensorProductPairKernel::~TensorProductPairKernel() { - SG_UNREF(subkernel); + cleanup(); } -bool CTensorProductPairKernel::init(CFeatures* l, CFeatures* r) +bool TensorProductPairKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); init_normalizer(); return true; } -float64_t CTensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t TensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; - int32_t* avec=((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); - int32_t* bvec=((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + int32_t* avec=(std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); + int32_t* bvec=(std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==2) ASSERT(blen==2) - CKernel* k=subkernel; + auto k=subkernel; ASSERT(k && k->has_features()) int32_t a=avec[0]; @@ -66,13 +66,13 @@ float64_t CTensorProductPairKernel::compute(int32_t idx_a, int32_t idx_b) float64_t result = k->kernel(a,c)*k->kernel(b,d) + k->kernel(a,d)*k->kernel(b,c); - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } -void CTensorProductPairKernel::register_params() +void TensorProductPairKernel::register_params() { - SG_ADD((CSGObject**)&subkernel, "subkernel", "the subkernel", ParameterProperties::HYPER); + SG_ADD((std::shared_ptr*)&subkernel, "subkernel", "the subkernel", ParameterProperties::HYPER); } diff --git a/src/shogun/kernel/TensorProductPairKernel.h b/src/shogun/kernel/TensorProductPairKernel.h index 6b3639a6bbd..d5d4c3628aa 100644 --- a/src/shogun/kernel/TensorProductPairKernel.h +++ b/src/shogun/kernel/TensorProductPairKernel.h @@ -32,18 +32,18 @@ namespace shogun * * It is often used in bioinformatics, e.g., to predict protein-protein interactions. */ -class CTensorProductPairKernel: public CDotKernel +class TensorProductPairKernel: public DotKernel { public: /** default constructor */ - CTensorProductPairKernel(); + TensorProductPairKernel(); /** constructor * * @param size cache size * @param subkernel the subkernel */ - CTensorProductPairKernel(int32_t size, CKernel* subkernel); + TensorProductPairKernel(int32_t size, std::shared_ptr subkernel); /** constructor * @@ -51,9 +51,9 @@ class CTensorProductPairKernel: public CDotKernel * @param r features of right-hand side * @param subkernel the subkernel */ - CTensorProductPairKernel(CDenseFeatures *l, CDenseFeatures *r, CKernel* subkernel); + TensorProductPairKernel(std::shared_ptr> l, std::shared_ptr> r, std::shared_ptr subkernel); - virtual ~CTensorProductPairKernel(); + virtual ~TensorProductPairKernel(); /** initialize kernel * @@ -61,7 +61,7 @@ class CTensorProductPairKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * @@ -104,7 +104,7 @@ class CTensorProductPairKernel: public CDotKernel protected: /** the subkernel */ - CKernel* subkernel; + std::shared_ptr subkernel; }; } #endif /* _TPPKKERNEL_H__ */ diff --git a/src/shogun/kernel/WaveKernel.cpp b/src/shogun/kernel/WaveKernel.cpp index f634145fcc9..1a0c7b18e3c 100644 --- a/src/shogun/kernel/WaveKernel.cpp +++ b/src/shogun/kernel/WaveKernel.cpp @@ -9,50 +9,50 @@ using namespace shogun; -CWaveKernel::CWaveKernel(): CKernel(0), m_distance(NULL), m_theta(1.0) +WaveKernel::WaveKernel(): Kernel(0), m_distance(NULL), m_theta(1.0) { init(); } -CWaveKernel::CWaveKernel(int32_t cache, float64_t theta, CDistance* dist) -: CKernel(cache), m_distance(dist), m_theta(theta) +WaveKernel::WaveKernel(int32_t cache, float64_t theta, std::shared_ptr dist) +: Kernel(cache), m_distance(dist), m_theta(theta) { init(); ASSERT(m_distance) - SG_REF(m_distance); + } -CWaveKernel::CWaveKernel(CFeatures *l, CFeatures *r, float64_t theta, CDistance* dist) -: CKernel(10), m_distance(dist), m_theta(theta) +WaveKernel::WaveKernel(std::shared_ptrl, std::shared_ptrr, float64_t theta, std::shared_ptr dist) +: Kernel(10), m_distance(dist), m_theta(theta) { init(); ASSERT(m_distance) - SG_REF(m_distance); + init(l, r); } -CWaveKernel::~CWaveKernel() +WaveKernel::~WaveKernel() { cleanup(); - SG_UNREF(m_distance); + } -bool CWaveKernel::init(CFeatures* l, CFeatures* r) +bool WaveKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(m_distance) - CKernel::init(l,r); + Kernel::init(l,r); m_distance->init(l,r); return init_normalizer(); } -void CWaveKernel::init() +void WaveKernel::init() { SG_ADD(&m_theta, "theta", "Theta kernel parameter.", ParameterProperties::HYPER); SG_ADD(&m_distance, "distance", "Distance to be used.", ParameterProperties::HYPER); } -float64_t CWaveKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t WaveKernel::compute(int32_t idx_a, int32_t idx_b) { float64_t dist = m_distance->distance(idx_a, idx_b); diff --git a/src/shogun/kernel/WaveKernel.h b/src/shogun/kernel/WaveKernel.h index 8e224193634..615744fad12 100644 --- a/src/shogun/kernel/WaveKernel.h +++ b/src/shogun/kernel/WaveKernel.h @@ -16,7 +16,7 @@ namespace shogun { -class CDistance; +class Distance; /** @brief Wave kernel * @@ -28,18 +28,18 @@ class CDistance; * */ -class CWaveKernel: public CKernel +class WaveKernel: public Kernel { public: /** default constructor */ - CWaveKernel(); + WaveKernel(); /** constructor * @param cache size of cache * @param theta kernel parameter theta * @param dist distance to be used */ - CWaveKernel(int32_t cache, float64_t theta, CDistance* dist); + WaveKernel(int32_t cache, float64_t theta, std::shared_ptr dist); /** constructor * @param l features left-side @@ -47,14 +47,14 @@ class CWaveKernel: public CKernel * @param theta kernel parameter theta * @param dist distance to be used */ - CWaveKernel(CFeatures *l, CFeatures *r, float64_t theta, CDistance* dist); + WaveKernel(std::shared_ptrl, std::shared_ptrr, float64_t theta, std::shared_ptr dist); /** initialize kernel with features * @param l features left-side * @param r features right-side * @return true if successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** * @return kernel type @@ -76,7 +76,7 @@ class CWaveKernel: public CKernel */ virtual const char* get_name() const { return "WaveKernel"; } - virtual ~CWaveKernel(); + virtual ~WaveKernel(); protected: /** @@ -94,7 +94,7 @@ class CWaveKernel: public CKernel protected: /// distance to be used - CDistance* m_distance; + std::shared_ptr m_distance; /// theta parameter of kernel float64_t m_theta; diff --git a/src/shogun/kernel/WaveletKernel.cpp b/src/shogun/kernel/WaveletKernel.cpp index 0fedf37721f..c4a62762ee7 100644 --- a/src/shogun/kernel/WaveletKernel.cpp +++ b/src/shogun/kernel/WaveletKernel.cpp @@ -10,55 +10,55 @@ using namespace shogun; -CWaveletKernel::CWaveletKernel() : CDotKernel(), Wdilation(0.0), Wtranslation(0.0) +WaveletKernel::WaveletKernel() : DotKernel(), Wdilation(0.0), Wtranslation(0.0) { init(); } -CWaveletKernel::CWaveletKernel(int32_t size, float64_t a, float64_t c) -: CDotKernel(size), Wdilation(a), Wtranslation(c) +WaveletKernel::WaveletKernel(int32_t size, float64_t a, float64_t c) +: DotKernel(size), Wdilation(a), Wtranslation(c) { init(); } -CWaveletKernel::CWaveletKernel( - CDotFeatures* l, CDotFeatures* r, int32_t size, float64_t a, float64_t c) -: CDotKernel(size), Wdilation(a), Wtranslation(c) +WaveletKernel::WaveletKernel( + std::shared_ptr l, std::shared_ptr r, int32_t size, float64_t a, float64_t c) +: DotKernel(size), Wdilation(a), Wtranslation(c) { init(); init(l,r); } -CWaveletKernel::~CWaveletKernel() +WaveletKernel::~WaveletKernel() { cleanup(); } -void CWaveletKernel::cleanup() +void WaveletKernel::cleanup() { } -bool CWaveletKernel::init(CFeatures* l, CFeatures* r) +bool WaveletKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); return init_normalizer(); } -void CWaveletKernel::init() +void WaveletKernel::init() { SG_ADD(&Wdilation, "dilation", "Dilation coefficient", ParameterProperties::HYPER); SG_ADD(&Wtranslation, "translation", "Translation coefficient", ParameterProperties::HYPER); } -float64_t CWaveletKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t WaveletKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; float64_t* avec= - ((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); + (std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); float64_t* bvec= - ((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + (std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) float64_t result=1; @@ -75,8 +75,8 @@ float64_t CWaveletKernel::compute(int32_t idx_a, int32_t idx_b) } } - ((CDenseFeatures*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CDenseFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, afree); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, bfree); return result; } diff --git a/src/shogun/kernel/WaveletKernel.h b/src/shogun/kernel/WaveletKernel.h index cf3afc0e0e3..17bbc3d2a51 100644 --- a/src/shogun/kernel/WaveletKernel.h +++ b/src/shogun/kernel/WaveletKernel.h @@ -30,11 +30,11 @@ namespace shogun * \f] * */ -class CWaveletKernel: public CDotKernel +class WaveletKernel: public DotKernel { public: /** default constructor */ - CWaveletKernel(); + WaveletKernel(); /** constructor * @@ -42,7 +42,7 @@ class CWaveletKernel: public CDotKernel * @param Wdilation is Dilation coefficient * @param Wtranslation is Translation coefficient */ - CWaveletKernel(int32_t size, float64_t Wdilation, float64_t Wtranslation); + WaveletKernel(int32_t size, float64_t Wdilation, float64_t Wtranslation); /** constructor * @@ -52,9 +52,9 @@ class CWaveletKernel: public CDotKernel * @param Wdilation is Dilation coefficient * @param Wtranslation is Translation coefficient */ - CWaveletKernel(CDotFeatures* l, CDotFeatures* r, int32_t size,float64_t Wdilation, float64_t Wtranslation); + WaveletKernel(std::shared_ptr l, std::shared_ptr r, int32_t size,float64_t Wdilation, float64_t Wtranslation); - virtual ~CWaveletKernel(); + virtual ~WaveletKernel(); /** initialize kernel * @@ -62,7 +62,7 @@ class CWaveletKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); virtual void cleanup(); diff --git a/src/shogun/kernel/WeightedDegreeRBFKernel.cpp b/src/shogun/kernel/WeightedDegreeRBFKernel.cpp index 00f94c00716..9359b7bb202 100644 --- a/src/shogun/kernel/WeightedDegreeRBFKernel.cpp +++ b/src/shogun/kernel/WeightedDegreeRBFKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Evan Shelhamer, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Evan Shelhamer, Sergey Lisitsyn, * Leon Kuchenbecker */ @@ -12,43 +12,43 @@ using namespace shogun; -CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel() -: CDotKernel(), width(1), degree(1), weights(0) +WeightedDegreeRBFKernel::WeightedDegreeRBFKernel() +: DotKernel(), width(1), degree(1), weights(0) { register_params(); } -CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel(int32_t size, float64_t w, int32_t d, int32_t nof_prop) -: CDotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0) +WeightedDegreeRBFKernel::WeightedDegreeRBFKernel(int32_t size, float64_t w, int32_t d, int32_t nof_prop) +: DotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0) { init_wd_weights(); register_params(); } -CWeightedDegreeRBFKernel::CWeightedDegreeRBFKernel( - CDenseFeatures* l, CDenseFeatures* r, float64_t w, int32_t d, int32_t nof_prop, int32_t size) -: CDotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0) +WeightedDegreeRBFKernel::WeightedDegreeRBFKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t w, int32_t d, int32_t nof_prop, int32_t size) +: DotKernel(size), width(w), degree(d), nof_properties(nof_prop), weights(0) { init_wd_weights(); register_params(); init(l,r); } -CWeightedDegreeRBFKernel::~CWeightedDegreeRBFKernel() +WeightedDegreeRBFKernel::~WeightedDegreeRBFKernel() { SG_FREE(weights); weights=NULL; } -bool CWeightedDegreeRBFKernel::init(CFeatures* l, CFeatures* r) +bool WeightedDegreeRBFKernel::init(std::shared_ptr l, std::shared_ptr r) { - CDotKernel::init(l, r); + DotKernel::init(l, r); SG_DEBUG("Initialized WeightedDegreeRBFKernel ({}).", fmt::ptr(this)) return init_normalizer(); } -bool CWeightedDegreeRBFKernel::init_wd_weights() +bool WeightedDegreeRBFKernel::init_wd_weights() { ASSERT(degree>0) @@ -74,13 +74,13 @@ bool CWeightedDegreeRBFKernel::init_wd_weights() } -float64_t CWeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t WeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; - float64_t* avec=((CDenseFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); - float64_t* bvec=((CDenseFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + float64_t* avec=(std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, afree); + float64_t* bvec=(std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, bfree); ASSERT(alen==blen) ASSERT(alen%nof_properties == 0) @@ -96,7 +96,7 @@ float64_t CWeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b) int32_t limit = (d + 1 ) * nof_properties; for (int32_t k=0; k < limit; k++) { - resultid+=CMath::sq(avec[i+k]-bvec[i+k]); + resultid+=Math::sq(avec[i+k]-bvec[i+k]); } resulti += weights[d] * exp(-resultid/width); @@ -108,7 +108,7 @@ float64_t CWeightedDegreeRBFKernel::compute(int32_t idx_a, int32_t idx_b) return result; } -void CWeightedDegreeRBFKernel::register_params() +void WeightedDegreeRBFKernel::register_params() { SG_ADD(&width, "width", "Kernel width", ParameterProperties::HYPER); SG_ADD(°ree, "degree", "Kernel degree", ParameterProperties::HYPER); diff --git a/src/shogun/kernel/WeightedDegreeRBFKernel.h b/src/shogun/kernel/WeightedDegreeRBFKernel.h index 1d811c0e0bf..6ca4a786233 100644 --- a/src/shogun/kernel/WeightedDegreeRBFKernel.h +++ b/src/shogun/kernel/WeightedDegreeRBFKernel.h @@ -11,13 +11,13 @@ namespace shogun { /** @brief weighted degree RBF kernel */ -class CWeightedDegreeRBFKernel: public CDotKernel +class WeightedDegreeRBFKernel: public DotKernel { public: /** default constructor * */ - CWeightedDegreeRBFKernel(); + WeightedDegreeRBFKernel(); /** constructor * @@ -26,7 +26,7 @@ class CWeightedDegreeRBFKernel: public CDotKernel * @param degree degree * @param nof_properties number of properties per amino acid */ - CWeightedDegreeRBFKernel(int32_t size, float64_t width, int32_t degree, int32_t nof_properties); + WeightedDegreeRBFKernel(int32_t size, float64_t width, int32_t degree, int32_t nof_properties); /** constructor * @@ -37,10 +37,10 @@ class CWeightedDegreeRBFKernel: public CDotKernel * @param nof_properties number of properties per amino acid * @param size cache size */ - CWeightedDegreeRBFKernel(CDenseFeatures* l, CDenseFeatures* r, + WeightedDegreeRBFKernel(std::shared_ptr> l, std::shared_ptr> r, float64_t width, int32_t degree, int32_t nof_properties, int32_t size=10); - virtual ~CWeightedDegreeRBFKernel(); + virtual ~WeightedDegreeRBFKernel(); /** initialize kernel * @@ -48,7 +48,7 @@ class CWeightedDegreeRBFKernel: public CDotKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/normalizer/AvgDiagKernelNormalizer.h b/src/shogun/kernel/normalizer/AvgDiagKernelNormalizer.h index bf5aabe0faf..dc3abf4e2a4 100644 --- a/src/shogun/kernel/normalizer/AvgDiagKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/AvgDiagKernelNormalizer.h @@ -26,7 +26,7 @@ namespace shogun * k'(x,x')= \frac{k(x,x')}{scale} * \f] */ -class CAvgDiagKernelNormalizer : public CKernelNormalizer +class AvgDiagKernelNormalizer : public KernelNormalizer { public: /** constructor @@ -34,22 +34,22 @@ class CAvgDiagKernelNormalizer : public CKernelNormalizer * @param c scale parameter, if <= 0 scaling will be computed from the * avg of the kernel diagonal elements */ - CAvgDiagKernelNormalizer(float64_t c=0.0) : CKernelNormalizer() + AvgDiagKernelNormalizer(float64_t c=0.0) : KernelNormalizer() { scale=c; - SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", - ParameterProperties::HYPER); + /*SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", + ParameterProperties::HYPER)*/; } /** default destructor */ - virtual ~CAvgDiagKernelNormalizer() + virtual ~AvgDiagKernelNormalizer() { } /** initialization of the normalizer (if needed) * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { if (scale<=0) { @@ -57,8 +57,8 @@ class CAvgDiagKernelNormalizer : public CKernelNormalizer int32_t num=k->get_num_vec_lhs(); ASSERT(num>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; diff --git a/src/shogun/kernel/normalizer/DiceKernelNormalizer.h b/src/shogun/kernel/normalizer/DiceKernelNormalizer.h index fcbacf2550e..04b0b7ea660 100644 --- a/src/shogun/kernel/normalizer/DiceKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/DiceKernelNormalizer.h @@ -21,32 +21,32 @@ namespace shogun * k'({\bf x},{\bf x'}) = \frac{2k({\bf x},{\bf x'})}{k({\bf x},{\bf x})+k({\bf x'},{\bf x'}} * \f] */ -class CDiceKernelNormalizer : public CKernelNormalizer +class DiceKernelNormalizer : public KernelNormalizer { public: /** default constructor * @param use_opt_diag - some kernels support faster diagonal compuation * via compute_diag(idx), this flag enables this */ - CDiceKernelNormalizer(bool use_opt_diag=false) : CKernelNormalizer(), + DiceKernelNormalizer(bool use_opt_diag=false) : KernelNormalizer(), diag_lhs(NULL), num_diag_lhs(0), diag_rhs(NULL), num_diag_rhs(0), use_optimized_diagonal_computation(use_opt_diag) { - m_parameters->add_vector(&diag_lhs, &num_diag_lhs, "diag_lhs", - "K(x,x) for left hand side examples."); - watch_param("diag_lhs", &diag_lhs, &num_diag_lhs); + /*m_parameters->add_vector(&diag_lhs, &num_diag_lhs, "diag_lhs", + "K(x,x) for left hand side examples.")*/; + /*watch_param("diag_lhs", &diag_lhs, &num_diag_lhs)*/; - m_parameters->add_vector(&diag_rhs, &num_diag_rhs, "diag_rhs", - "K(x,x) for right hand side examples."); - watch_param("diag_rhs", &diag_rhs, &num_diag_rhs); + /*m_parameters->add_vector(&diag_rhs, &num_diag_rhs, "diag_rhs", + "K(x,x) for right hand side examples.")*/; + /*watch_param("diag_rhs", &diag_rhs, &num_diag_rhs)*/; - SG_ADD(&use_optimized_diagonal_computation, + /*SG_ADD(&use_optimized_diagonal_computation, "use_optimized_diagonal_computation", - "flat if optimized diagonal computation is used"); + "flat if optimized diagonal computation is used");*/ } /** default destructor */ - virtual ~CDiceKernelNormalizer() + virtual ~DiceKernelNormalizer() { SG_FREE(diag_lhs); SG_FREE(diag_rhs); @@ -54,7 +54,7 @@ class CDiceKernelNormalizer : public CKernelNormalizer /** initialization of the normalizer * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { ASSERT(k) num_diag_lhs=k->get_num_vec_lhs(); @@ -62,8 +62,8 @@ class CDiceKernelNormalizer : public CKernelNormalizer ASSERT(num_diag_lhs>0) ASSERT(num_diag_rhs>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; @@ -124,7 +124,7 @@ class CDiceKernelNormalizer : public CKernelNormalizer * alloc and compute the vector containing the square root of the * diagonal elements of this kernel. */ - bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num) const + bool alloc_and_compute_diag(Kernel* k, float64_t* &v, int32_t num) const { SG_FREE(v); v=SG_MALLOC(float64_t, num); @@ -133,10 +133,11 @@ class CDiceKernelNormalizer : public CKernelNormalizer { if (k->get_kernel_type() == K_COMMWORDSTRING) { + auto cwsk = k->as(); if (use_optimized_diagonal_computation) - v[i]=((CCommWordStringKernel*) k)->compute_diag(i); + v[i]=cwsk->compute_diag(i); else - v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true); + v[i]=cwsk->compute_helper(i,i, true); } else v[i]=k->compute(i,i); diff --git a/src/shogun/kernel/normalizer/FirstElementKernelNormalizer.h b/src/shogun/kernel/normalizer/FirstElementKernelNormalizer.h index 584b9e4ef93..3883d534632 100644 --- a/src/shogun/kernel/normalizer/FirstElementKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/FirstElementKernelNormalizer.h @@ -23,28 +23,28 @@ namespace shogun * useful if the kernel returns constant elements along the diagonal anyway and * all one wants is to scale the kernel down to 1 on the diagonal. */ -class CFirstElementKernelNormalizer : public CKernelNormalizer +class FirstElementKernelNormalizer : public KernelNormalizer { public: /** constructor */ - CFirstElementKernelNormalizer() : CKernelNormalizer(), scale(1.0) + FirstElementKernelNormalizer() : KernelNormalizer(), scale(1.0) { - SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", - ParameterProperties::HYPER); + /*SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", + ParameterProperties::HYPER)*/; } /** default destructor */ - virtual ~CFirstElementKernelNormalizer() + virtual ~FirstElementKernelNormalizer() { } /** initialization of the normalizer (if needed) * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; diff --git a/src/shogun/kernel/normalizer/IdentityKernelNormalizer.h b/src/shogun/kernel/normalizer/IdentityKernelNormalizer.h index 8ea9c0f9f50..4082e0fae79 100644 --- a/src/shogun/kernel/normalizer/IdentityKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/IdentityKernelNormalizer.h @@ -14,22 +14,22 @@ namespace shogun { /** @brief Identity Kernel Normalization, i.e. no normalization is applied. */ -class CIdentityKernelNormalizer : public CKernelNormalizer +class IdentityKernelNormalizer : public KernelNormalizer { public: /** default constructor */ - CIdentityKernelNormalizer() : CKernelNormalizer() + IdentityKernelNormalizer() : KernelNormalizer() { } /** default destructor */ - virtual ~CIdentityKernelNormalizer() + virtual ~IdentityKernelNormalizer() { } /** initialization of the normalizer (if needed) * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { return true; } diff --git a/src/shogun/kernel/normalizer/KernelNormalizer.h b/src/shogun/kernel/normalizer/KernelNormalizer.h index 234256be187..3cf22902458 100644 --- a/src/shogun/kernel/normalizer/KernelNormalizer.h +++ b/src/shogun/kernel/normalizer/KernelNormalizer.h @@ -22,7 +22,7 @@ enum ENormalizerType N_MULTITASK = 1 }; -class CKernel; +class Kernel; /** @brief The class Kernel Normalizer defines a function to post-process kernel values. * * Formally it defines f(.,.,.) @@ -46,24 +46,24 @@ class CKernel; * k'({\bf x},{\bf x'}) = \frac{k({\bf x},{\bf x'})}{\sqrt{k({\bf x},{\bf x})k({\bf x'},{\bf x'})}} * \f] */ -class CKernelNormalizer : public CSGObject +class KernelNormalizer : public SGObject { public: /** default constructor */ - CKernelNormalizer() : CSGObject() + KernelNormalizer() : SGObject() { register_params(); m_type = N_REGULAR; } /** default destructor */ - virtual ~CKernelNormalizer() { } + virtual ~KernelNormalizer() { } /** initialization of the normalizer (if needed) * @param k kernel */ - virtual bool init(CKernel* k)=0; + virtual bool init(Kernel* k)=0; /** normalize the kernel value * @param value kernel value diff --git a/src/shogun/kernel/normalizer/RidgeKernelNormalizer.h b/src/shogun/kernel/normalizer/RidgeKernelNormalizer.h index 930bdce1655..44a2e5bf52c 100644 --- a/src/shogun/kernel/normalizer/RidgeKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/RidgeKernelNormalizer.h @@ -38,7 +38,7 @@ namespace shogun * \f] * */ -class CRidgeKernelNormalizer : public CKernelNormalizer +class RidgeKernelNormalizer : public KernelNormalizer { public: /** constructor @@ -51,25 +51,25 @@ class CRidgeKernelNormalizer : public CKernelNormalizer * - r=1e-10 and c=0.0 will add mean(diag(K))*1e-10 to the diagonal * - r=0.1 and c=1 will add 0.1 to the diagonal */ - CRidgeKernelNormalizer(float64_t r=1e-10, float64_t c=0.0) - : CKernelNormalizer() + RidgeKernelNormalizer(float64_t r=1e-10, float64_t c=0.0) + : KernelNormalizer() { - SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", - ParameterProperties::HYPER); - SG_ADD(&ridge, "ridge", "Ridge added to diagonal.", ParameterProperties::HYPER); + /*SG_ADD(&scale, "scale", "Scale quotient by which kernel is scaled.", + ParameterProperties::HYPER)*/; + /*SG_ADD(&ridge, "ridge", "Ridge added to diagonal.", ParameterProperties::HYPER)*/; scale=c; ridge=r; } /** default destructor */ - virtual ~CRidgeKernelNormalizer() + virtual ~RidgeKernelNormalizer() { } /** initialization of the normalizer (if needed) * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { if (scale<=0) { @@ -77,8 +77,8 @@ class CRidgeKernelNormalizer : public CKernelNormalizer int32_t num=k->get_num_vec_lhs(); ASSERT(num>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; diff --git a/src/shogun/kernel/normalizer/ScatterKernelNormalizer.h b/src/shogun/kernel/normalizer/ScatterKernelNormalizer.h index 743c8853dd2..0b219506b11 100644 --- a/src/shogun/kernel/normalizer/ScatterKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/ScatterKernelNormalizer.h @@ -19,21 +19,21 @@ namespace shogun { /** @brief the scatter kernel normalizer */ -class CScatterKernelNormalizer: public CKernelNormalizer +class ScatterKernelNormalizer: public KernelNormalizer { public: /** default constructor */ - CScatterKernelNormalizer() : CKernelNormalizer() + ScatterKernelNormalizer() : KernelNormalizer() { init(); } /** default constructor */ - CScatterKernelNormalizer(float64_t const_diag, float64_t const_offdiag, - CLabels* labels,CKernelNormalizer* normalizer=NULL) - : CKernelNormalizer() + ScatterKernelNormalizer(float64_t const_diag, float64_t const_offdiag, + std::shared_ptr labels,std::shared_ptr normalizer=NULL) + : KernelNormalizer() { init(); @@ -42,14 +42,14 @@ class CScatterKernelNormalizer: public CKernelNormalizer m_const_offdiag=const_offdiag; ASSERT(labels) - SG_REF(labels); - m_labels=labels; + ASSERT(labels->get_label_type()==LT_MULTICLASS) + m_labels = labels->as(); labels->ensure_valid(); - if (normalizer==NULL) - normalizer=new CIdentityKernelNormalizer(); - SG_REF(normalizer); + if (!normalizer) + normalizer=std::make_shared(); + m_normalizer=normalizer; SG_DEBUG("Constructing ScatterKernelNormalizer with const_diag={:g}" @@ -59,15 +59,15 @@ class CScatterKernelNormalizer: public CKernelNormalizer } /** default destructor */ - virtual ~CScatterKernelNormalizer() + virtual ~ScatterKernelNormalizer() { - SG_UNREF(m_labels); - SG_UNREF(m_normalizer); + + } /** initialization of the normalizer * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { m_normalizer->init(k); return true; @@ -104,12 +104,12 @@ class CScatterKernelNormalizer: public CKernelNormalizer if (m_testing_class>=0) { - if (((CMulticlassLabels*) m_labels)->get_label(idx_lhs) == m_testing_class) + if (m_labels->get_label(idx_lhs) == m_testing_class) c=m_const_diag; } else { - if (((CMulticlassLabels*) m_labels)->get_label(idx_lhs) == ((CMulticlassLabels*) m_labels)->get_label(idx_rhs)) + if (m_labels->get_label(idx_lhs) == m_labels->get_label(idx_rhs)) c=m_const_diag; } @@ -160,8 +160,8 @@ class CScatterKernelNormalizer: public CKernelNormalizer SG_ADD(&m_const_offdiag, "m_const_offdiag", "Factor to multiply to off-diagonal elements.", ParameterProperties::HYPER); - SG_ADD((CSGObject**) &m_labels, "m_labels", "Labels"); - SG_ADD((CSGObject**) &m_normalizer, "m_normalizer", "Kernel normalizer.", + SG_ADD((std::shared_ptr*) &m_labels, "m_labels", "Labels"); + SG_ADD((std::shared_ptr*) &m_normalizer, "m_normalizer", "Kernel normalizer.", ParameterProperties::HYPER); } @@ -173,10 +173,10 @@ class CScatterKernelNormalizer: public CKernelNormalizer float64_t m_const_offdiag; /** labels **/ - CLabels* m_labels; + std::shared_ptr m_labels; /** labels **/ - CKernelNormalizer* m_normalizer; + std::shared_ptr m_normalizer; /** upon testing which class to test for */ int32_t m_testing_class; diff --git a/src/shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h b/src/shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h index 150c23f871f..2648df9d3eb 100644 --- a/src/shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/SqrtDiagKernelNormalizer.h @@ -24,19 +24,19 @@ namespace shogun * k'({\bf x},{\bf x'}) = \frac{k({\bf x},{\bf x'})}{\sqrt{k({\bf x},{\bf x})k({\bf x'},{\bf x'})}} * \f] */ -class CSqrtDiagKernelNormalizer : public CKernelNormalizer +class SqrtDiagKernelNormalizer : public KernelNormalizer { public: /** default constructor * @param use_opt_diag - some kernels support faster diagonal compuation * via compute_diag(idx), this flag enables this */ - CSqrtDiagKernelNormalizer(bool use_opt_diag=false): CKernelNormalizer(), + SqrtDiagKernelNormalizer(bool use_opt_diag=false): KernelNormalizer(), sqrtdiag_lhs(NULL), num_sqrtdiag_lhs(0), sqrtdiag_rhs(NULL), num_sqrtdiag_rhs(0), use_optimized_diagonal_computation(use_opt_diag) { - m_parameters->add_vector(&sqrtdiag_lhs, &num_sqrtdiag_lhs, "sqrtdiag_lhs", + /*m_parameters->add_vector(&sqrtdiag_lhs, &num_sqrtdiag_lhs, "sqrtdiag_lhs", "sqrt(K(x,x)) for left hand side examples."); watch_param("sqrtdiag_lhs", &sqrtdiag_lhs, &num_sqrtdiag_lhs); @@ -46,11 +46,11 @@ class CSqrtDiagKernelNormalizer : public CKernelNormalizer SG_ADD(&use_optimized_diagonal_computation, "use_optimized_diagonal_computation", - "flat if optimized diagonal computation is used"); + "flat if optimized diagonal computation is used");*/ } /** default destructor */ - virtual ~CSqrtDiagKernelNormalizer() + virtual ~SqrtDiagKernelNormalizer() { SG_FREE(sqrtdiag_lhs); SG_FREE(sqrtdiag_rhs); @@ -58,7 +58,7 @@ class CSqrtDiagKernelNormalizer : public CKernelNormalizer /** initialization of the normalizer * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { ASSERT(k) num_sqrtdiag_lhs=k->get_num_vec_lhs(); @@ -66,8 +66,8 @@ class CSqrtDiagKernelNormalizer : public CKernelNormalizer ASSERT(num_sqrtdiag_lhs>0) ASSERT(num_sqrtdiag_rhs>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; @@ -121,7 +121,7 @@ class CSqrtDiagKernelNormalizer : public CKernelNormalizer * alloc and compute the vector containing the square root of the * diagonal elements of this kernel. */ - bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num) const + bool alloc_and_compute_diag(Kernel* k, float64_t* &v, int32_t num) const { SG_FREE(v); v=SG_MALLOC(float64_t, num); @@ -130,10 +130,11 @@ class CSqrtDiagKernelNormalizer : public CKernelNormalizer { if (k->get_kernel_type() == K_COMMWORDSTRING) { + auto cwk = k->as(); if (use_optimized_diagonal_computation) - v[i]=sqrt(((CCommWordStringKernel*) k)->compute_diag(i)); + v[i]=sqrt(cwk->compute_diag(i)); else - v[i]=sqrt(((CCommWordStringKernel*) k)->compute_helper(i,i, true)); + v[i]=sqrt(cwk->compute_helper(i,i, true)); } else v[i]=sqrt(k->compute(i,i)); diff --git a/src/shogun/kernel/normalizer/TanimotoKernelNormalizer.h b/src/shogun/kernel/normalizer/TanimotoKernelNormalizer.h index 129832a07b7..b14aae4f7c9 100644 --- a/src/shogun/kernel/normalizer/TanimotoKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/TanimotoKernelNormalizer.h @@ -21,21 +21,21 @@ namespace shogun * k'({\bf x},{\bf x'}) = \frac{k({\bf x},{\bf x'})}{k({\bf x},{\bf x})+k({\bf x'},{\bf x'})-k({\bf x},{\bf x'})} * \f] */ -class CTanimotoKernelNormalizer : public CKernelNormalizer +class TanimotoKernelNormalizer : public KernelNormalizer { public: /** default constructor * @param use_opt_diag - some kernels support faster diagonal compuation * via compute_diag(idx), this flag enables this */ - CTanimotoKernelNormalizer(bool use_opt_diag=false) - : CKernelNormalizer(), diag_lhs(NULL), diag_rhs(NULL), + TanimotoKernelNormalizer(bool use_opt_diag=false) + : KernelNormalizer(), diag_lhs(NULL), diag_rhs(NULL), use_optimized_diagonal_computation(use_opt_diag) { } /** default destructor */ - virtual ~CTanimotoKernelNormalizer() + virtual ~TanimotoKernelNormalizer() { SG_FREE(diag_lhs); SG_FREE(diag_rhs); @@ -43,7 +43,7 @@ class CTanimotoKernelNormalizer : public CKernelNormalizer /** initialization of the normalizer * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { ASSERT(k) int32_t num_lhs=k->get_num_vec_lhs(); @@ -51,8 +51,8 @@ class CTanimotoKernelNormalizer : public CKernelNormalizer ASSERT(num_lhs>0) ASSERT(num_rhs>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; @@ -113,7 +113,7 @@ class CTanimotoKernelNormalizer : public CKernelNormalizer * alloc and compute the vector containing the square root of the * diagonal elements of this kernel. */ - bool alloc_and_compute_diag(CKernel* k, float64_t* &v, int32_t num) const + bool alloc_and_compute_diag(Kernel* k, float64_t* &v, int32_t num) const { SG_FREE(v); v=SG_MALLOC(float64_t, num); @@ -122,10 +122,11 @@ class CTanimotoKernelNormalizer : public CKernelNormalizer { if (k->get_kernel_type() == K_COMMWORDSTRING) { + auto cwsk = k->as(); if (use_optimized_diagonal_computation) - v[i]=((CCommWordStringKernel*) k)->compute_diag(i); + v[i]=cwsk->compute_diag(i); else - v[i]=((CCommWordStringKernel*) k)->compute_helper(i,i, true); + v[i]=cwsk->compute_helper(i,i, true); } else v[i]=k->compute(i,i); diff --git a/src/shogun/kernel/normalizer/VarianceKernelNormalizer.h b/src/shogun/kernel/normalizer/VarianceKernelNormalizer.h index 8af50842960..5be1f4e6b4b 100644 --- a/src/shogun/kernel/normalizer/VarianceKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/VarianceKernelNormalizer.h @@ -16,40 +16,40 @@ namespace shogun /** @brief VarianceKernelNormalizer divides by the ``variance'' * * This effectively normalizes the vectors in feature space to variance 1 (see - * CVarianceKernelNormalizer) + * VarianceKernelNormalizer) * * \f[ * k'({\bf x},{\bf x'}) = \frac{k({\bf x},{\bf x'})}{\frac{1}{N}\sum_{i=1}^N k({\bf x}_i, {\bf x}_i) - \sum_{i,j=1}^N, k({\bf x}_i,{\bf x'}_j)/N^2} * \f] */ -class CVarianceKernelNormalizer : public CKernelNormalizer +class VarianceKernelNormalizer : public KernelNormalizer { public: /** default constructor */ - CVarianceKernelNormalizer() - : CKernelNormalizer(), meandiff(1.0), sqrt_meandiff(1.0) + VarianceKernelNormalizer() + : KernelNormalizer(), meandiff(1.0), sqrt_meandiff(1.0) { - SG_ADD(&meandiff, "meandiff", "Scaling constant.", ParameterProperties::HYPER); - SG_ADD(&sqrt_meandiff, "sqrt_meandiff", - "Square root of scaling constant.", ParameterProperties::HYPER); + /*SG_ADD(&meandiff, "meandiff", "Scaling constant.", ParameterProperties::HYPER)*/; + /*SG_ADD(&sqrt_meandiff, "sqrt_meandiff", + "Square root of scaling constant.", ParameterProperties::HYPER)*/; } /** default destructor */ - virtual ~CVarianceKernelNormalizer() + virtual ~VarianceKernelNormalizer() { } /** initialization of the normalizer * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { ASSERT(k) int32_t n=k->get_num_vec_lhs(); ASSERT(n>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; k->lhs=old_lhs; k->rhs=old_lhs; diff --git a/src/shogun/kernel/normalizer/ZeroMeanCenterKernelNormalizer.h b/src/shogun/kernel/normalizer/ZeroMeanCenterKernelNormalizer.h index 922c88f0544..53d593da7ce 100644 --- a/src/shogun/kernel/normalizer/ZeroMeanCenterKernelNormalizer.h +++ b/src/shogun/kernel/normalizer/ZeroMeanCenterKernelNormalizer.h @@ -36,26 +36,26 @@ namespace shogun * K_{c}^t = (K - 1'_M K - K^{t} 1_M + 1'_M K 1_M) * \f] */ -class CZeroMeanCenterKernelNormalizer : public CKernelNormalizer +class ZeroMeanCenterKernelNormalizer : public KernelNormalizer { public: /** default constructor */ - CZeroMeanCenterKernelNormalizer() - : CKernelNormalizer(), ktrain_row_means(NULL), num_ktrain(0), + ZeroMeanCenterKernelNormalizer() + : KernelNormalizer(), ktrain_row_means(NULL), num_ktrain(0), ktest_row_means(NULL), num_ktest(0) { - m_parameters->add_vector(&ktrain_row_means, &num_ktrain, - "num_ktrain", "Train row means."); - watch_param("num_ktrain", &ktrain_row_means, &num_ktrain); + /*m_parameters->add_vector(&ktrain_row_means, &num_ktrain, + "num_ktrain", "Train row means.")*/; + /*watch_param("num_ktrain", &ktrain_row_means, &num_ktrain)*/; - m_parameters->add_vector(&ktest_row_means, &num_ktest, - "num_ktest","Test row means."); - watch_param("num_ktest", &ktest_row_means, &num_ktest); + /*m_parameters->add_vector(&ktest_row_means, &num_ktest, + "num_ktest","Test row means.")*/; + /*watch_param("num_ktest", &ktest_row_means, &num_ktest)*/; } /** default destructor */ - virtual ~CZeroMeanCenterKernelNormalizer() + virtual ~ZeroMeanCenterKernelNormalizer() { SG_FREE(ktrain_row_means); SG_FREE(ktest_row_means); @@ -63,7 +63,7 @@ class CZeroMeanCenterKernelNormalizer : public CKernelNormalizer /** initialization of the normalizer * @param k kernel */ - virtual bool init(CKernel* k) + virtual bool init(Kernel* k) { ASSERT(k) int32_t num_lhs=k->get_num_vec_lhs(); @@ -71,8 +71,8 @@ class CZeroMeanCenterKernelNormalizer : public CKernelNormalizer ASSERT(num_lhs>0) ASSERT(num_rhs>0) - CFeatures* old_lhs=k->lhs; - CFeatures* old_rhs=k->rhs; + auto old_lhs=k->lhs; + auto old_rhs=k->rhs; /* compute mean for each row of the train matrix*/ k->lhs=old_lhs; @@ -133,7 +133,7 @@ class CZeroMeanCenterKernelNormalizer : public CKernelNormalizer * alloc and compute the vector containing the row margins of all rows * for a kernel matrix. */ - bool alloc_and_compute_row_means(CKernel* k, float64_t* &v, int32_t num_lhs, int32_t num_rhs) const + bool alloc_and_compute_row_means(Kernel* k, float64_t* &v, int32_t num_lhs, int32_t num_rhs) const { SG_FREE(v); v=SG_MALLOC(float64_t, num_rhs); diff --git a/src/shogun/kernel/string/CommUlongStringKernel.cpp b/src/shogun/kernel/string/CommUlongStringKernel.cpp index bc5b9bf2f35..98bb2e53708 100644 --- a/src/shogun/kernel/string/CommUlongStringKernel.cpp +++ b/src/shogun/kernel/string/CommUlongStringKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Giovanni De Toni, Soeren Sonnenburg, Sergey Lisitsyn, + * Authors: Giovanni De Toni, Soeren Sonnenburg, Sergey Lisitsyn, * Evangelos Anagnostopoulos, Leon Kuchenbecker */ @@ -15,13 +15,13 @@ using namespace shogun; -CCommUlongStringKernel::CCommUlongStringKernel() : CStringKernel() +CommUlongStringKernel::CommUlongStringKernel() : StringKernel() { init_params(); } -CCommUlongStringKernel::CCommUlongStringKernel(bool us, int32_t size) -: CStringKernel(size) +CommUlongStringKernel::CommUlongStringKernel(bool us, int32_t size) +: StringKernel(size) { init_params(); @@ -30,34 +30,34 @@ CCommUlongStringKernel::CCommUlongStringKernel(bool us, int32_t size) properties |= KP_LINADD; clear_normal(); - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); } -CCommUlongStringKernel::CCommUlongStringKernel( - CStringFeatures* l, CStringFeatures* r, bool us, +CommUlongStringKernel::CommUlongStringKernel( + std::shared_ptr> l, std::shared_ptr> r, bool us, int32_t size) -: CStringKernel(size) +: StringKernel(size) { init_params(); use_sign=us; properties |= KP_LINADD; clear_normal(); - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); init(l,r); } -void CCommUlongStringKernel::init_params() +void CommUlongStringKernel::init_params() { use_sign = false; - SG_ADD(&use_sign, "use_sign", "Whether or not to use sign.") + SG_ADD(&use_sign, "use_sign", "Whether or not to use sign."); } -CCommUlongStringKernel::~CCommUlongStringKernel() +CommUlongStringKernel::~CommUlongStringKernel() { cleanup(); } -void CCommUlongStringKernel::remove_lhs() +void CommUlongStringKernel::remove_lhs() { delete_optimization(); @@ -70,7 +70,7 @@ void CCommUlongStringKernel::remove_lhs() rhs = NULL ; } -void CCommUlongStringKernel::remove_rhs() +void CommUlongStringKernel::remove_rhs() { #ifdef SVMLIGHT if (rhs) @@ -80,25 +80,25 @@ void CCommUlongStringKernel::remove_rhs() rhs = lhs; } -bool CCommUlongStringKernel::init(CFeatures* l, CFeatures* r) +bool CommUlongStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l,r); + StringKernel::init(l,r); return init_normalizer(); } -void CCommUlongStringKernel::cleanup() +void CommUlongStringKernel::cleanup() { delete_optimization(); clear_normal(); - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CCommUlongStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t CommUlongStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint64_t* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - uint64_t* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + uint64_t* avec=(std::static_pointer_cast>(lhs))->get_feature_vector(idx_a, alen, free_avec); + uint64_t* bvec=(std::static_pointer_cast>(rhs))->get_feature_vector(idx_b, blen, free_bvec); float64_t result=0; @@ -152,13 +152,13 @@ float64_t CCommUlongStringKernel::compute(int32_t idx_a, int32_t idx_b) right_idx++; } } - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + (std::static_pointer_cast>(lhs))->free_feature_vector(avec, idx_a, free_avec); + (std::static_pointer_cast>(rhs))->free_feature_vector(bvec, idx_b, free_bvec); return result; } -void CCommUlongStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) +void CommUlongStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) { int32_t t=0; int32_t j=0; @@ -166,7 +166,7 @@ void CCommUlongStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) int32_t last_j=0; int32_t len=-1; bool free_vec; - uint64_t* vec=((CStringFeatures*) lhs)->get_feature_vector(vec_idx, len, free_vec); + uint64_t* vec=(std::static_pointer_cast>(lhs))->get_feature_vector(vec_idx, len, free_vec); if (vec && len>0) { @@ -221,19 +221,19 @@ void CCommUlongStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) dictionary = dic; dictionary_weights = dic_weights; } - ((CStringFeatures*) lhs)->free_feature_vector(vec, vec_idx, free_vec); + (std::static_pointer_cast>(lhs))->free_feature_vector(vec, vec_idx, free_vec); set_is_initialized(true); } -void CCommUlongStringKernel::clear_normal() +void CommUlongStringKernel::clear_normal() { dictionary.resize_vector(0); dictionary_weights.resize_vector(0); set_is_initialized(false); } -bool CCommUlongStringKernel::init_optimization( +bool CommUlongStringKernel::init_optimization( int32_t count, int32_t *IDX, float64_t * weights) { clear_normal(); @@ -258,7 +258,7 @@ bool CCommUlongStringKernel::init_optimization( return true; } -bool CCommUlongStringKernel::delete_optimization() +bool CommUlongStringKernel::delete_optimization() { SG_DEBUG("deleting CCommUlongStringKernel optimization") clear_normal(); @@ -267,7 +267,7 @@ bool CCommUlongStringKernel::delete_optimization() // binary search for each feature. trick: as features are sorted save last found idx in old_idx and // only search in the remainder of the dictionary -float64_t CCommUlongStringKernel::compute_optimized(int32_t i) +float64_t CommUlongStringKernel::compute_optimized(int32_t i) { float64_t result = 0; int32_t j, last_j=0; @@ -283,7 +283,7 @@ float64_t CCommUlongStringKernel::compute_optimized(int32_t i) int32_t alen = -1; bool free_avec; - uint64_t* avec=((CStringFeatures*) rhs)-> + uint64_t* avec=(std::static_pointer_cast>(rhs))-> get_feature_vector(i, alen, free_avec); if (avec && alen>0) @@ -295,7 +295,7 @@ float64_t CCommUlongStringKernel::compute_optimized(int32_t i) if (avec[j]==avec[j-1]) continue; - int32_t idx = CMath::binary_search_max_lower_equal(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[j-1]); + int32_t idx = Math::binary_search_max_lower_equal(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[j-1]); if (idx!=-1) { @@ -306,7 +306,7 @@ float64_t CCommUlongStringKernel::compute_optimized(int32_t i) } } - int32_t idx = CMath::binary_search(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[alen-1]); + int32_t idx = Math::binary_search(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[alen-1]); if (idx!=-1) result += dictionary_weights[idx+old_idx]; } @@ -317,7 +317,7 @@ float64_t CCommUlongStringKernel::compute_optimized(int32_t i) if (avec[j]==avec[j-1]) continue; - int32_t idx = CMath::binary_search_max_lower_equal(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[j-1]); + int32_t idx = Math::binary_search_max_lower_equal(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[j-1]); if (idx!=-1) { @@ -330,13 +330,13 @@ float64_t CCommUlongStringKernel::compute_optimized(int32_t i) last_j = j; } - int32_t idx = CMath::binary_search(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[alen-1]); + int32_t idx = Math::binary_search(&(dictionary[old_idx]), dictionary.vlen-old_idx, avec[alen-1]); if (idx!=-1) result += dictionary_weights[idx+old_idx]*(alen-last_j); } } - ((CStringFeatures*) rhs)->free_feature_vector(avec, i, free_avec); + (std::static_pointer_cast>(rhs))->free_feature_vector(avec, i, free_avec); return normalizer->normalize_rhs(result, i); } diff --git a/src/shogun/kernel/string/CommUlongStringKernel.h b/src/shogun/kernel/string/CommUlongStringKernel.h index b4add8b66c5..a88e131bc23 100644 --- a/src/shogun/kernel/string/CommUlongStringKernel.h +++ b/src/shogun/kernel/string/CommUlongStringKernel.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Yuyu Zhang, Bjoern Esser, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Yuyu Zhang, Bjoern Esser, * Evangelos Anagnostopoulos */ @@ -17,8 +17,8 @@ namespace shogun { -template class CDynamicArray; -template class CStringFeatures; +template class DynamicArray; +template class StringFeatures; /** @brief The CommUlongString kernel may be used to compute the spectrum kernel * from strings that have been mapped into unsigned 64bit integers. @@ -44,17 +44,17 @@ template class CStringFeatures; * improvement here when a whole set of sequences is ADDed) using sorted lists. * */ -class CCommUlongStringKernel: public CStringKernel +class CommUlongStringKernel: public StringKernel { public: - CCommUlongStringKernel(); + CommUlongStringKernel(); /** constructor * * @param size cache size * @param use_sign if sign shall be used */ - CCommUlongStringKernel(bool use_sign, int32_t size=10); + CommUlongStringKernel(bool use_sign, int32_t size=10); /** constructor * @@ -63,12 +63,12 @@ class CCommUlongStringKernel: public CStringKernel * @param use_sign if sign shall be used * @param size cache size */ - CCommUlongStringKernel( - CStringFeatures* l, CStringFeatures* r, + CommUlongStringKernel( + std::shared_ptr> l, std::shared_ptr> r, bool use_sign=false, int32_t size=10); - virtual ~CCommUlongStringKernel(); + virtual ~CommUlongStringKernel(); /** initialize kernel * @@ -76,7 +76,7 @@ class CCommUlongStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/CommWordStringKernel.cpp b/src/shogun/kernel/string/CommWordStringKernel.cpp index 6cffd2fb88f..6fcb8827477 100644 --- a/src/shogun/kernel/string/CommWordStringKernel.cpp +++ b/src/shogun/kernel/string/CommWordStringKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Giovanni De Toni, Heiko Strathmann, Viktor Gal, + * Authors: Soeren Sonnenburg, Giovanni De Toni, Heiko Strathmann, Viktor Gal, * Weijie Lin, Bjoern Esser, Sergey Lisitsyn */ @@ -17,22 +17,22 @@ using namespace shogun; -CCommWordStringKernel::CCommWordStringKernel() -: CStringKernel() +CommWordStringKernel::CommWordStringKernel() +: StringKernel() { init(); } -CCommWordStringKernel::CCommWordStringKernel(int32_t size, bool s) -: CStringKernel(size) +CommWordStringKernel::CommWordStringKernel(int32_t size, bool s) +: StringKernel(size) { init(); use_sign=s; } -CCommWordStringKernel::CCommWordStringKernel( - CStringFeatures* l, CStringFeatures* r, - bool s, int32_t size) : CStringKernel(size) +CommWordStringKernel::CommWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, + bool s, int32_t size) : StringKernel(size) { init(); use_sign=s; @@ -41,7 +41,7 @@ CCommWordStringKernel::CCommWordStringKernel( } -bool CCommWordStringKernel::init_dictionary(int32_t size) +bool CommWordStringKernel::init_dictionary(int32_t size) { dictionary_weights=SGVector(size); SG_DEBUG("using dictionary of {} words", size) @@ -50,38 +50,40 @@ bool CCommWordStringKernel::init_dictionary(int32_t size) return dictionary_weights.vector!=NULL; } -CCommWordStringKernel::~CCommWordStringKernel() +CommWordStringKernel::~CommWordStringKernel() { cleanup(); SG_FREE(dict_diagonal_optimization); } -bool CCommWordStringKernel::init(CFeatures* l, CFeatures* r) +bool CommWordStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l,r); + StringKernel::init(l,r); if (use_dict_diagonal_optimization) { SG_FREE(dict_diagonal_optimization); - dict_diagonal_optimization=SG_MALLOC(int32_t, int32_t(((CStringFeatures*)l)->get_num_symbols())); - ASSERT(((CStringFeatures*)l)->get_num_symbols() == ((CStringFeatures*)r)->get_num_symbols()) + auto sf_l = std::dynamic_pointer_cast>(l); + auto sf_r = std::dynamic_pointer_cast>(r); + dict_diagonal_optimization=SG_MALLOC(int32_t, int32_t(sf_l->get_num_symbols())); + ASSERT(sf_l->get_num_symbols() == sf_r->get_num_symbols()) } return init_normalizer(); } -void CCommWordStringKernel::cleanup() +void CommWordStringKernel::cleanup() { delete_optimization(); - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CCommWordStringKernel::compute_diag(int32_t idx_a) +float64_t CommWordStringKernel::compute_diag(int32_t idx_a) { int32_t alen; - CStringFeatures* l = (CStringFeatures*) lhs; - CStringFeatures* r = (CStringFeatures*) rhs; + auto l = std::static_pointer_cast>(lhs); + auto r = std::static_pointer_cast>(rhs); bool free_av; uint16_t* av=l->get_feature_vector(idx_a, alen, free_av); @@ -121,14 +123,14 @@ float64_t CCommWordStringKernel::compute_diag(int32_t idx_a) return result; } -float64_t CCommWordStringKernel::compute_helper( +float64_t CommWordStringKernel::compute_helper( int32_t idx_a, int32_t idx_b, bool do_sort) { int32_t alen, blen; bool free_av, free_bv; - CStringFeatures* l = (CStringFeatures*) lhs; - CStringFeatures* r = (CStringFeatures*) rhs; + auto l = std::static_pointer_cast>(lhs); + auto r = std::static_pointer_cast>(rhs); uint16_t* av=l->get_feature_vector(idx_a, alen, free_av); uint16_t* bv=r->get_feature_vector(idx_b, blen, free_bv); @@ -142,7 +144,7 @@ float64_t CCommWordStringKernel::compute_helper( { avec=SG_MALLOC(uint16_t, alen); sg_memcpy(avec, av, sizeof(uint16_t)*alen); - CMath::radix_sort(avec, alen); + Math::radix_sort(avec, alen); } else avec=NULL; @@ -151,7 +153,7 @@ float64_t CCommWordStringKernel::compute_helper( { bvec=SG_MALLOC(uint16_t, blen); sg_memcpy(bvec, bv, sizeof(uint16_t)*blen); - CMath::radix_sort(bvec, blen); + Math::radix_sort(bvec, blen); } else bvec=NULL; @@ -223,11 +225,11 @@ float64_t CCommWordStringKernel::compute_helper( return result; } -void CCommWordStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) +void CommWordStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) { int32_t len=-1; bool free_vec; - uint16_t* vec=((CStringFeatures*) lhs)-> + uint16_t* vec=(std::static_pointer_cast>(lhs))-> get_feature_vector(vec_idx, len, free_vec); if (len>0) @@ -265,16 +267,16 @@ void CCommWordStringKernel::add_to_normal(int32_t vec_idx, float64_t weight) set_is_initialized(true); } - ((CStringFeatures*) lhs)->free_feature_vector(vec, vec_idx, free_vec); + (std::static_pointer_cast>(lhs))->free_feature_vector(vec, vec_idx, free_vec); } -void CCommWordStringKernel::clear_normal() +void CommWordStringKernel::clear_normal() { dictionary_weights.zero(); set_is_initialized(false); } -bool CCommWordStringKernel::init_optimization( +bool CommWordStringKernel::init_optimization( int32_t count, int32_t* IDX, float64_t* weights) { delete_optimization(); @@ -297,7 +299,7 @@ bool CCommWordStringKernel::init_optimization( return true; } -bool CCommWordStringKernel::delete_optimization() +bool CommWordStringKernel::delete_optimization() { SG_DEBUG("deleting CCommWordStringKernel optimization") @@ -305,7 +307,7 @@ bool CCommWordStringKernel::delete_optimization() return true; } -float64_t CCommWordStringKernel::compute_optimized(int32_t i) +float64_t CommWordStringKernel::compute_optimized(int32_t i) { if (!get_is_initialized()) { @@ -316,7 +318,7 @@ float64_t CCommWordStringKernel::compute_optimized(int32_t i) float64_t result = 0; int32_t len = -1; bool free_vec; - uint16_t* vec=((CStringFeatures*) rhs)-> + uint16_t* vec=(std::static_pointer_cast>(rhs))-> get_feature_vector(i, len, free_vec); int32_t j, last_j=0; @@ -350,18 +352,18 @@ float64_t CCommWordStringKernel::compute_optimized(int32_t i) result=normalizer->normalize_rhs(result, i); } - ((CStringFeatures*) rhs)->free_feature_vector(vec, i, free_vec); + (std::static_pointer_cast>(rhs))->free_feature_vector(vec, i, free_vec); return result; } -float64_t* CCommWordStringKernel::compute_scoring( +float64_t* CommWordStringKernel::compute_scoring( int32_t max_degree, int32_t& num_feat, int32_t& num_sym, float64_t* target, int32_t num_suppvec, int32_t* IDX, float64_t* alphas, bool do_init) { ASSERT(lhs) - CStringFeatures* str=((CStringFeatures*) lhs); + auto str=(std::static_pointer_cast>(lhs)); num_feat=1;//str->get_max_vector_length(); - CAlphabet* alpha=str->get_alphabet(); + auto alpha=str->get_alphabet(); ASSERT(alpha) int32_t num_bits=alpha->get_num_bits(); int32_t order=str->get_order(); @@ -373,7 +375,7 @@ float64_t* CCommWordStringKernel::compute_scoring( num_sym=0; for (int32_t i=0; iget_masked_symbols(0xffff, 1); @@ -423,7 +425,7 @@ float64_t* CCommWordStringKernel::compute_scoring( } float64_t marginalizer= - 1.0/CMath::pow((int32_t) num_words,(int32_t) m_sym); + 1.0/Math::pow((int32_t) num_words,(int32_t) m_sym); for (uint32_t i=0; i* str=((CStringFeatures*) lhs); + auto str=(std::static_pointer_cast>(lhs)); int32_t num_words=(int32_t) str->get_num_symbols(); int32_t num_feat=str->get_max_vector_length(); int64_t total_len=((int64_t) num_feat) * num_words; - CAlphabet* alpha=((CStringFeatures*) lhs)->get_alphabet(); + auto alpha=(std::static_pointer_cast>(lhs))->get_alphabet(); ASSERT(alpha) int32_t num_bits=alpha->get_num_bits(); int32_t order=str->get_order(); @@ -578,11 +580,11 @@ char* CCommWordStringKernel::compute_consensus( SG_FREE(bt); SG_FREE(score); - SG_UNREF(alpha); + return result; } -void CCommWordStringKernel::init() +void CommWordStringKernel::init() { use_sign=false; use_dict_diagonal_optimization=false; @@ -590,7 +592,7 @@ void CCommWordStringKernel::init() properties |= KP_LINADD; init_dictionary(1<<(sizeof(uint16_t)*8)); - set_normalizer(new CSqrtDiagKernelNormalizer(use_dict_diagonal_optimization)); + set_normalizer(std::make_shared(use_dict_diagonal_optimization)); SG_ADD(&dictionary_weights, "dictionary_weights", "Dictionary for applying kernel."); diff --git a/src/shogun/kernel/string/CommWordStringKernel.h b/src/shogun/kernel/string/CommWordStringKernel.h index 04bdeb7d203..2ce86f514d4 100644 --- a/src/shogun/kernel/string/CommWordStringKernel.h +++ b/src/shogun/kernel/string/CommWordStringKernel.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Yuyu Zhang, Bjoern Esser, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Yuyu Zhang, Bjoern Esser, * Viktor Gal */ @@ -41,26 +41,26 @@ namespace shogun * direct maps. * */ -class CCommWordStringKernel : public CStringKernel +class CommWordStringKernel : public StringKernel { - friend class CVarianceKernelNormalizer; - friend class CSqrtDiagKernelNormalizer; - friend class CAvgDiagKernelNormalizer; - friend class CRidgeKernelNormalizer; - friend class CFirstElementKernelNormalizer; - friend class CTanimotoKernelNormalizer; - friend class CDiceKernelNormalizer; + friend class VarianceKernelNormalizer; + friend class SqrtDiagKernelNormalizer; + friend class AvgDiagKernelNormalizer; + friend class RidgeKernelNormalizer; + friend class FirstElementKernelNormalizer; + friend class TanimotoKernelNormalizer; + friend class DiceKernelNormalizer; public: /** default constructor */ - CCommWordStringKernel(); + CommWordStringKernel(); /** constructor * * @param size cache size * @param use_sign if sign shall be used */ - CCommWordStringKernel(int32_t size, bool use_sign); + CommWordStringKernel(int32_t size, bool use_sign); /** constructor * @@ -69,11 +69,11 @@ class CCommWordStringKernel : public CStringKernel * @param use_sign if sign shall be used * @param size cache size */ - CCommWordStringKernel( - CStringFeatures* l, CStringFeatures* r, + CommWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, bool use_sign=false, int32_t size=10); - virtual ~CCommWordStringKernel(); + virtual ~CommWordStringKernel(); /** initialize kernel * @@ -81,7 +81,7 @@ class CCommWordStringKernel : public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/FixedDegreeStringKernel.cpp b/src/shogun/kernel/string/FixedDegreeStringKernel.cpp index c234fcd3a98..fef316f35a2 100644 --- a/src/shogun/kernel/string/FixedDegreeStringKernel.cpp +++ b/src/shogun/kernel/string/FixedDegreeStringKernel.cpp @@ -14,55 +14,55 @@ using namespace shogun; void -CFixedDegreeStringKernel::init() +FixedDegreeStringKernel::init() { SG_ADD(°ree, "degree", "The degree.", ParameterProperties::HYPER); - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); } -CFixedDegreeStringKernel::CFixedDegreeStringKernel() -: CStringKernel(0), degree(0) +FixedDegreeStringKernel::FixedDegreeStringKernel() +: StringKernel(0), degree(0) { init(); } -CFixedDegreeStringKernel::CFixedDegreeStringKernel(int32_t size, int32_t d) -: CStringKernel(size), degree(d) +FixedDegreeStringKernel::FixedDegreeStringKernel(int32_t size, int32_t d) +: StringKernel(size), degree(d) { init(); } -CFixedDegreeStringKernel::CFixedDegreeStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t d) -: CStringKernel(10), degree(d) +FixedDegreeStringKernel::FixedDegreeStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d) +: StringKernel(10), degree(d) { init(); init(l, r); } -CFixedDegreeStringKernel::~CFixedDegreeStringKernel() +FixedDegreeStringKernel::~FixedDegreeStringKernel() { cleanup(); } -bool CFixedDegreeStringKernel::init(CFeatures* l, CFeatures* r) +bool FixedDegreeStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CFixedDegreeStringKernel::cleanup() +void FixedDegreeStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CFixedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t FixedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec = std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) @@ -77,8 +77,8 @@ float64_t CFixedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b) if (match) sum++; } - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return sum; } diff --git a/src/shogun/kernel/string/FixedDegreeStringKernel.h b/src/shogun/kernel/string/FixedDegreeStringKernel.h index 1aa19414ced..8dd24434773 100644 --- a/src/shogun/kernel/string/FixedDegreeStringKernel.h +++ b/src/shogun/kernel/string/FixedDegreeStringKernel.h @@ -26,20 +26,20 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CFixedDegreeStringKernel: public CStringKernel +class FixedDegreeStringKernel: public StringKernel { void init(); public: /** default constructor */ - CFixedDegreeStringKernel(); + FixedDegreeStringKernel(); /** constructor * * @param size cache size * @param degree the degree */ - CFixedDegreeStringKernel(int32_t size, int32_t degree); + FixedDegreeStringKernel(int32_t size, int32_t degree); /** constructor * @@ -47,11 +47,11 @@ class CFixedDegreeStringKernel: public CStringKernel * @param r features of right-hand side * @param degree the degree */ - CFixedDegreeStringKernel( - CStringFeatures* l, CStringFeatures* r, + FixedDegreeStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t degree); - virtual ~CFixedDegreeStringKernel(); + virtual ~FixedDegreeStringKernel(); /** initialize kernel * @@ -59,7 +59,7 @@ class CFixedDegreeStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/GaussianMatchStringKernel.cpp b/src/shogun/kernel/string/GaussianMatchStringKernel.cpp index a6a918c9992..846a35ea334 100644 --- a/src/shogun/kernel/string/GaussianMatchStringKernel.cpp +++ b/src/shogun/kernel/string/GaussianMatchStringKernel.cpp @@ -13,52 +13,52 @@ using namespace shogun; -CGaussianMatchStringKernel::CGaussianMatchStringKernel() -: CStringKernel(0), width(0.0) +GaussianMatchStringKernel::GaussianMatchStringKernel() +: StringKernel(0), width(0.0) { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); register_params(); } -CGaussianMatchStringKernel::CGaussianMatchStringKernel(int32_t size, float64_t w) -: CStringKernel(size), width(w) +GaussianMatchStringKernel::GaussianMatchStringKernel(int32_t size, float64_t w) +: StringKernel(size), width(w) { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); register_params(); } -CGaussianMatchStringKernel::CGaussianMatchStringKernel( - CStringFeatures* l, CStringFeatures* r, float64_t w) -: CStringKernel(10), width(w) +GaussianMatchStringKernel::GaussianMatchStringKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t w) +: StringKernel(10), width(w) { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); init(l, r); register_params(); } -CGaussianMatchStringKernel::~CGaussianMatchStringKernel() +GaussianMatchStringKernel::~GaussianMatchStringKernel() { cleanup(); } -bool CGaussianMatchStringKernel::init(CFeatures* l, CFeatures* r) +bool GaussianMatchStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CGaussianMatchStringKernel::cleanup() +void GaussianMatchStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t GaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t i, alen, blen ; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = lhs->as>()->get_feature_vector(idx_a, alen, free_avec); + char* bvec = rhs->as>()->get_feature_vector(idx_b, blen, free_bvec); float64_t result=0; @@ -70,12 +70,12 @@ float64_t CGaussianMatchStringKernel::compute(int32_t idx_a, int32_t idx_b) result=exp(-result/width); - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + lhs->as>()->free_feature_vector(avec, idx_a, free_avec); + rhs->as>()->free_feature_vector(bvec, idx_b, free_bvec); return result; } -void CGaussianMatchStringKernel::register_params() +void GaussianMatchStringKernel::register_params() { SG_ADD(&width, "width", "kernel width", ParameterProperties::HYPER); } diff --git a/src/shogun/kernel/string/GaussianMatchStringKernel.h b/src/shogun/kernel/string/GaussianMatchStringKernel.h index 7ab447d7913..0d77d69fbf4 100644 --- a/src/shogun/kernel/string/GaussianMatchStringKernel.h +++ b/src/shogun/kernel/string/GaussianMatchStringKernel.h @@ -29,18 +29,18 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CGaussianMatchStringKernel: public CStringKernel +class GaussianMatchStringKernel: public StringKernel { public: /** default constructor */ - CGaussianMatchStringKernel(); + GaussianMatchStringKernel(); /** constructor * * @param size cache size * @param width width */ - CGaussianMatchStringKernel(int32_t size, float64_t width); + GaussianMatchStringKernel(int32_t size, float64_t width); /** constructor * @@ -48,11 +48,11 @@ class CGaussianMatchStringKernel: public CStringKernel * @param r features of right-hand side * @param width width */ - CGaussianMatchStringKernel( - CStringFeatures* l, CStringFeatures* r, + GaussianMatchStringKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t width); - virtual ~CGaussianMatchStringKernel(); + virtual ~GaussianMatchStringKernel(); /** initialize kernel * @@ -60,7 +60,7 @@ class CGaussianMatchStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/HistogramWordStringKernel.cpp b/src/shogun/kernel/string/HistogramWordStringKernel.cpp index 90656f45707..6d475021a6d 100644 --- a/src/shogun/kernel/string/HistogramWordStringKernel.cpp +++ b/src/shogun/kernel/string/HistogramWordStringKernel.cpp @@ -13,34 +13,34 @@ using namespace shogun; -CHistogramWordStringKernel::CHistogramWordStringKernel() -: CStringKernel() +HistogramWordStringKernel::HistogramWordStringKernel() +: StringKernel() { init(); } -CHistogramWordStringKernel::CHistogramWordStringKernel(int32_t size, CPluginEstimate* pie) -: CStringKernel(size) +HistogramWordStringKernel::HistogramWordStringKernel(int32_t size, std::shared_ptr pie) +: StringKernel(size) { init(); - SG_REF(pie); + estimate=pie; } -CHistogramWordStringKernel::CHistogramWordStringKernel( - CStringFeatures* l, CStringFeatures* r, CPluginEstimate* pie) -: CStringKernel() +HistogramWordStringKernel::HistogramWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, std::shared_ptr pie) +: StringKernel() { init(); - SG_REF(pie); + estimate=pie; init(l, r); } -CHistogramWordStringKernel::~CHistogramWordStringKernel() +HistogramWordStringKernel::~HistogramWordStringKernel() { - SG_UNREF(estimate); + SG_FREE(variance); SG_FREE(mean); @@ -55,15 +55,15 @@ CHistogramWordStringKernel::~CHistogramWordStringKernel() SG_FREE(plo_lhs); } -bool CHistogramWordStringKernel::init(CFeatures* p_l, CFeatures* p_r) +bool HistogramWordStringKernel::init(std::shared_ptr p_l, std::shared_ptr p_r) { - CStringKernel::init(p_l,p_r); - CStringFeatures* l=(CStringFeatures*) p_l; - CStringFeatures* r=(CStringFeatures*) p_r; + StringKernel::init(p_l,p_r); + auto l=std::static_pointer_cast>(p_l); + auto r=std::static_pointer_cast>(p_r); ASSERT(l) ASSERT(r) - SG_DEBUG("init: lhs: {} rhs: {}", fmt::ptr(l), fmt::ptr(r)) + SG_DEBUG("init: lhs: {} rhs: {}", fmt::ptr(l.get()), fmt::ptr(r.get())) int32_t i; initialized=false; @@ -172,7 +172,7 @@ bool CHistogramWordStringKernel::init(CFeatures* p_l, CFeatures* p_r) bool free_vec; uint16_t* vec=l->get_feature_vector(i, len, free_vec); - variance[0] += CMath::sq(estimate->posterior_log_odds_obsolete(vec, len)-mean[0])/num_vectors; + variance[0] += Math::sq(estimate->posterior_log_odds_obsolete(vec, len)-mean[0])/num_vectors; for (int32_t j=0; jlog_derivative_pos_obsolete(vec[j], j) + variance[idx] += Math::sq(estimate->log_derivative_pos_obsolete(vec[j], j) -mean[idx])/num_vectors; - variance[idx+num_params] += CMath::sq(estimate->log_derivative_neg_obsolete(vec[j], j) + variance[idx+num_params] += Math::sq(estimate->log_derivative_neg_obsolete(vec[j], j) -mean[idx+num_params])/num_vectors; } } @@ -311,7 +311,7 @@ bool CHistogramWordStringKernel::init(CFeatures* p_l, CFeatures* p_r) return init_normalizer(); } -void CHistogramWordStringKernel::cleanup() +void HistogramWordStringKernel::cleanup() { SG_FREE(variance); variance=NULL; @@ -346,15 +346,15 @@ void CHistogramWordStringKernel::cleanup() sum_m2_s2=0; initialized = false; - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CHistogramWordStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t HistogramWordStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + uint16_t* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + uint16_t* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) @@ -382,12 +382,12 @@ float64_t CHistogramWordStringKernel::compute(int32_t idx_a, int32_t idx_b) if (fabs(result - result2)>1e-10) error("new={:e} old = {:e} diff = {:e}", result, result2, result - result2); #endif - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return result; } -void CHistogramWordStringKernel::init() +void HistogramWordStringKernel::init() { estimate=NULL; mean=NULL; @@ -410,31 +410,31 @@ void CHistogramWordStringKernel::init() SG_ADD(&initialized, "initialized", "If kernel is initalized."); - m_parameters->add_vector(&plo_lhs, &num_lhs, "plo_lhs"); + /*m_parameters->add_vector(&plo_lhs, &num_lhs, "plo_lhs");*/ watch_param("plo_lhs", &plo_lhs, &num_lhs); - m_parameters->add_vector(&plo_rhs, &num_rhs, "plo_rhs"); + /*m_parameters->add_vector(&plo_rhs, &num_rhs, "plo_rhs");*/ watch_param("plo_rhs", &plo_rhs, &num_rhs); - m_parameters->add_vector(&ld_mean_lhs, &num_lhs, "ld_mean_lhs"); + /*m_parameters->add_vector(&ld_mean_lhs, &num_lhs, "ld_mean_lhs");*/ watch_param("ld_mean_lhs", &ld_mean_lhs, &num_lhs); - m_parameters->add_vector(&ld_mean_rhs, &num_rhs, "ld_mean_rhs"); + /*m_parameters->add_vector(&ld_mean_rhs, &num_rhs, "ld_mean_rhs");*/ watch_param("ld_mean_rhs", &ld_mean_rhs, &num_rhs); - m_parameters->add_vector(&sqrtdiag_lhs, &num_lhs, "sqrtdiag_lhs"); + /*m_parameters->add_vector(&sqrtdiag_lhs, &num_lhs, "sqrtdiag_lhs");*/ watch_param("sqrtdiag_lhs", &sqrtdiag_lhs, &num_lhs); - m_parameters->add_vector(&sqrtdiag_rhs, &num_rhs, "sqrtdiag_rhs"); + /*m_parameters->add_vector(&sqrtdiag_rhs, &num_rhs, "sqrtdiag_rhs");*/ watch_param("sqrtdiag_rhs", &sqrtdiag_rhs, &num_rhs); - m_parameters->add_vector(&mean, &num_params2, "mean"); + /*m_parameters->add_vector(&mean, &num_params2, "mean");*/ watch_param("mean", &mean, &num_params2); - m_parameters->add_vector(&variance, &num_params2, "variance"); + /*m_parameters->add_vector(&variance, &num_params2, "variance");*/ watch_param("variance", &variance, &num_params2); - SG_ADD((CSGObject**) &estimate, "estimate", "Plugin Estimate."); + SG_ADD((std::shared_ptr*) &estimate, "estimate", "Plugin Estimate."); } #ifdef DEBUG_HWSK_COMPUTATION @@ -442,8 +442,8 @@ float64_t CHistogramWordStringKernel::compute_slow(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + uint16_t* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + uint16_t* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) @@ -473,8 +473,8 @@ float64_t CHistogramWordStringKernel::compute_slow(int32_t idx_a, int32_t idx_b) if (initialized) result /= (sqrtdiag_lhs[idx_a]*sqrtdiag_rhs[idx_b]) ; - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return result; } diff --git a/src/shogun/kernel/string/HistogramWordStringKernel.h b/src/shogun/kernel/string/HistogramWordStringKernel.h index 5fe2f802cc6..1a19c9745c3 100644 --- a/src/shogun/kernel/string/HistogramWordStringKernel.h +++ b/src/shogun/kernel/string/HistogramWordStringKernel.h @@ -16,22 +16,22 @@ namespace shogun { - class CPluginEstimate; - template class CStringFeatures; + class PluginEstimate; + template class StringFeatures; /** @brief The HistogramWordString computes the TOP kernel on inhomogeneous * Markov Chains. */ -class CHistogramWordStringKernel: public CStringKernel +class HistogramWordStringKernel: public StringKernel { public: /** default constructor */ - CHistogramWordStringKernel(); + HistogramWordStringKernel(); /** constructor * * @param size cache size * @param pie plugin estimate */ - CHistogramWordStringKernel(int32_t size, CPluginEstimate* pie); + HistogramWordStringKernel(int32_t size, std::shared_ptr pie); /** constructor * @@ -39,11 +39,11 @@ class CHistogramWordStringKernel: public CStringKernel * @param r features of right-hand side * @param pie plugin estimate */ - CHistogramWordStringKernel( - CStringFeatures* l, CStringFeatures* r, - CPluginEstimate* pie); + HistogramWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, + std::shared_ptr pie); - virtual ~CHistogramWordStringKernel(); + virtual ~HistogramWordStringKernel(); /** initialize kernel * @@ -51,7 +51,7 @@ class CHistogramWordStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -95,7 +95,7 @@ class CHistogramWordStringKernel: public CStringKernel protected: /** plugin estimate */ - CPluginEstimate* estimate; + std::shared_ptr estimate; /** mean */ float64_t* mean; diff --git a/src/shogun/kernel/string/LinearStringKernel.cpp b/src/shogun/kernel/string/LinearStringKernel.cpp index 1074c993c0e..7a8fa7b2e5e 100644 --- a/src/shogun/kernel/string/LinearStringKernel.cpp +++ b/src/shogun/kernel/string/LinearStringKernel.cpp @@ -12,73 +12,74 @@ using namespace shogun; -CLinearStringKernel::CLinearStringKernel() -: CStringKernel(0) +LinearStringKernel::LinearStringKernel() +: StringKernel(0) { } -CLinearStringKernel::CLinearStringKernel( - CStringFeatures* l, CStringFeatures* r) -: CStringKernel(0) +LinearStringKernel::LinearStringKernel( + std::shared_ptr> l, std::shared_ptr> r) +: StringKernel(0) { init(l, r); } -CLinearStringKernel::~CLinearStringKernel() +LinearStringKernel::~LinearStringKernel() { cleanup(); } -bool CLinearStringKernel::init(CFeatures *l, CFeatures *r) +bool LinearStringKernel::init(std::shared_ptrl, std::shared_ptrr) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CLinearStringKernel::cleanup() +void LinearStringKernel::cleanup() { delete_optimization(); - CKernel::cleanup(); + Kernel::cleanup(); } -void CLinearStringKernel::clear_normal() +void LinearStringKernel::clear_normal() { memset(m_normal.vector, 0, lhs->get_num_vectors()*sizeof(float64_t)); } -void CLinearStringKernel::add_to_normal(int32_t idx, float64_t weight) +void LinearStringKernel::add_to_normal(int32_t idx, float64_t weight) { int32_t vlen; bool vfree; - char* vec = ((CStringFeatures*) lhs)->get_feature_vector(idx, vlen, vfree); + char* vec = std::static_pointer_cast>(lhs)->get_feature_vector(idx, vlen, vfree); for (int32_t i=0; inormalize_lhs(vec[i], idx); - ((CStringFeatures*) lhs)->free_feature_vector(vec, idx, vfree); + std::static_pointer_cast>(lhs)->free_feature_vector(vec, idx, vfree); } -float64_t CLinearStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t LinearStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = lhs->as>()->get_feature_vector(idx_a, alen, free_avec); + char* bvec = rhs->as>()->get_feature_vector(idx_b, blen, free_bvec); ASSERT(alen==blen) SGVector a_wrap(avec, alen, false); SGVector b_wrap(bvec, blen, false); float64_t result = linalg::dot(a_wrap, b_wrap); - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + lhs->as>()->free_feature_vector(avec, idx_a, free_avec); + rhs->as>()->free_feature_vector(bvec, idx_b, free_bvec); return result; } -bool CLinearStringKernel::init_optimization( +bool LinearStringKernel::init_optimization( int32_t num_suppvec, int32_t *sv_idx, float64_t *alphas) { - int32_t num_feat = ((CStringFeatures*) lhs)->get_max_vector_length(); + auto sf_lhs = lhs->as>(); + int32_t num_feat = sf_lhs->get_max_vector_length(); ASSERT(num_feat) m_normal = SGVector(num_feat); @@ -89,7 +90,7 @@ bool CLinearStringKernel::init_optimization( { int32_t alen; bool free_avec; - char *avec = ((CStringFeatures*) lhs)->get_feature_vector(sv_idx[i], alen, free_avec); + char *avec = sf_lhs->get_feature_vector(sv_idx[i], alen, free_avec); ASSERT(avec) for (int32_t j = 0; jnormalize_lhs(((float64_t) avec[j]), sv_idx[i]); } - ((CStringFeatures*) lhs)->free_feature_vector(avec, sv_idx[i], free_avec); + sf_lhs->free_feature_vector(avec, sv_idx[i], free_avec); } set_is_initialized(true); return true; } -bool CLinearStringKernel::delete_optimization() +bool LinearStringKernel::delete_optimization() { m_normal = SGVector(); set_is_initialized(false); return true; } -float64_t CLinearStringKernel::compute_optimized(int32_t idx_b) +float64_t LinearStringKernel::compute_optimized(int32_t idx_b) { int32_t blen; bool free_bvec; - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* bvec = lhs->as>()->get_feature_vector(idx_b, blen, free_bvec); float64_t dot = 0.0; for (auto i = 0; m_normal.vlen; ++i) dot += m_normal[i]*(float64_t)bvec[i]; float64_t result=normalizer->normalize_rhs(dot, idx_b); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + rhs->as>()->free_feature_vector(bvec, idx_b, free_bvec); return result; } diff --git a/src/shogun/kernel/string/LinearStringKernel.h b/src/shogun/kernel/string/LinearStringKernel.h index 8f466846420..241a9ae4047 100644 --- a/src/shogun/kernel/string/LinearStringKernel.h +++ b/src/shogun/kernel/string/LinearStringKernel.h @@ -24,21 +24,21 @@ namespace shogun * * Note: Basically the same as LinearByteKernel but on signed chars. */ -class CLinearStringKernel: public CStringKernel +class LinearStringKernel: public StringKernel { public: /** constructor */ - CLinearStringKernel(); + LinearStringKernel(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CLinearStringKernel(CStringFeatures* l, CStringFeatures* r); + LinearStringKernel(std::shared_ptr> l, std::shared_ptr> r); - virtual ~CLinearStringKernel(); + virtual ~LinearStringKernel(); /** initialize kernel * @@ -46,7 +46,7 @@ class CLinearStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/LocalAlignmentStringKernel.cpp b/src/shogun/kernel/string/LocalAlignmentStringKernel.cpp index d3e6291b26d..e5aefc621b2 100644 --- a/src/shogun/kernel/string/LocalAlignmentStringKernel.cpp +++ b/src/shogun/kernel/string/LocalAlignmentStringKernel.cpp @@ -20,14 +20,14 @@ using namespace shogun; const int32_t NAA=20; /* Number of amino-acids */ const int32_t NLET=26; /* Number of letters in the alphabet */ -const char* CLocalAlignmentStringKernel::aaList="ARNDCQEGHILKMFPSTWYV"; /* The list of amino acids */ +const char* LocalAlignmentStringKernel::aaList="ARNDCQEGHILKMFPSTWYV"; /* The list of amino acids */ /*****************/ /* SW parameters */ /*****************/ /* mutation matrix */ -const int32_t CLocalAlignmentStringKernel::blosum[] = { +const int32_t LocalAlignmentStringKernel::blosum[] = { 6, -2, 8, -2, -1, 9, @@ -78,19 +78,19 @@ const float64_t SCALING=0.1; /* Factor to scale all SW parameters */ const float64_t LOG0=-10000; /* log(0) */ const float64_t INTSCALE=1000.0; /* critical for speed and precise computation*/ -int32_t CLocalAlignmentStringKernel::logsum_lookup[LOGSUM_TBL]; +int32_t LocalAlignmentStringKernel::logsum_lookup[LOGSUM_TBL]; -CLocalAlignmentStringKernel::CLocalAlignmentStringKernel(int32_t size) -: CStringKernel(size) +LocalAlignmentStringKernel::LocalAlignmentStringKernel(int32_t size) +: StringKernel(size) { init(); init_static_variables(); } -CLocalAlignmentStringKernel::CLocalAlignmentStringKernel( - CStringFeatures* l, CStringFeatures* r, +LocalAlignmentStringKernel::LocalAlignmentStringKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t opening, float64_t extension) -: CStringKernel() +: StringKernel() { init(); m_opening=opening; @@ -99,19 +99,19 @@ CLocalAlignmentStringKernel::CLocalAlignmentStringKernel( init(l, r); } -CLocalAlignmentStringKernel::~CLocalAlignmentStringKernel() +LocalAlignmentStringKernel::~LocalAlignmentStringKernel() { cleanup(); } -bool CLocalAlignmentStringKernel::init(CFeatures* l, CFeatures* r) +bool LocalAlignmentStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); initialized=true; return init_normalizer(); } -void CLocalAlignmentStringKernel::cleanup() +void LocalAlignmentStringKernel::cleanup() { SG_FREE(scaled_blosum); scaled_blosum=NULL; @@ -121,20 +121,20 @@ void CLocalAlignmentStringKernel::cleanup() SG_FREE(aaIndex); aaIndex=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } /* LogSum - default log funciotion. fast, but not exact */ /* LogSum2 - precise, but slow. Note that these two functions need different figure types */ -void CLocalAlignmentStringKernel::init_logsum(){ +void LocalAlignmentStringKernel::init_logsum(){ int32_t i; for (i=0; ip2) return (p1-p2>50.) ? p1 : p1+log(1.+exp(p2-p1)); @@ -166,7 +166,7 @@ float32_t CLocalAlignmentStringKernel::LogSum2(float32_t p1, float32_t p2) } -void CLocalAlignmentStringKernel::init_static_variables() +void LocalAlignmentStringKernel::init_static_variables() /* Initialize all static variables. This function should be called once before computing the first pair HMM score */ { int32_t i; @@ -196,7 +196,7 @@ void CLocalAlignmentStringKernel::init_static_variables() /* Implementation of the * convolution kernel which generalizes the Smith-Waterman algorithm */ -float64_t CLocalAlignmentStringKernel::LAkernelcompute( +float64_t LocalAlignmentStringKernel::LAkernelcompute( int32_t* aaX, int32_t* aaY, /* the two amino-acid sequences (as sequences of indexes in [0..NAA-1] indicating the position of the amino-acid in the variable 'aaList') */ int32_t nX, int32_t nY /* the lengths of both sequences */) { @@ -345,15 +345,15 @@ float64_t CLocalAlignmentStringKernel::LAkernelcompute( /* Return the log-probability of two sequences x and y under a pair HMM model */ /* x and y are strings of aminoacid letters, e.g., "AABRS" */ -float64_t CLocalAlignmentStringKernel::compute(int32_t idx_x, int32_t idx_y) +float64_t LocalAlignmentStringKernel::compute(int32_t idx_x, int32_t idx_y) { int32_t *aax, *aay; /* to convert x and y into sequences of amino-acid indexes */ int32_t lx=0, ly=0; /* lengths of x and y */ int32_t i, j; bool free_x, free_y; - char* x=((CStringFeatures*) lhs)->get_feature_vector(idx_x, lx, free_x); - char* y=((CStringFeatures*) rhs)->get_feature_vector(idx_y, ly, free_y); + char* x=std::static_pointer_cast>(lhs)->get_feature_vector(idx_x, lx, free_x); + char* y=std::static_pointer_cast>(rhs)->get_feature_vector(idx_y, ly, free_y); ASSERT(x && y) if ( (lx<1) || (ly<1) ) @@ -385,15 +385,15 @@ float64_t CLocalAlignmentStringKernel::compute(int32_t idx_x, int32_t idx_y) SG_FREE(aax); SG_FREE(aay); - ((CStringFeatures*)lhs)->free_feature_vector(x, idx_x, free_x); - ((CStringFeatures*)rhs)->free_feature_vector(y, idx_y, free_y); + std::static_pointer_cast>(lhs)->free_feature_vector(x, idx_x, free_x); + std::static_pointer_cast>(rhs)->free_feature_vector(y, idx_y, free_y); return result; } -void CLocalAlignmentStringKernel::init() +void LocalAlignmentStringKernel::init() { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); initialized=false; isAA=NULL; diff --git a/src/shogun/kernel/string/LocalAlignmentStringKernel.h b/src/shogun/kernel/string/LocalAlignmentStringKernel.h index 6d072541304..234d58cc75c 100644 --- a/src/shogun/kernel/string/LocalAlignmentStringKernel.h +++ b/src/shogun/kernel/string/LocalAlignmentStringKernel.h @@ -24,13 +24,13 @@ const int32_t LOGSUM_TBL=10000; * The implementation is taken from http://www.mloss.org/software/view/40/ and * only adjusted to work with shogun. */ -class CLocalAlignmentStringKernel: public CStringKernel +class LocalAlignmentStringKernel: public StringKernel { public: /** constructor * @param size cache size */ - CLocalAlignmentStringKernel(int32_t size=0); + LocalAlignmentStringKernel(int32_t size=0); /** constructor * @@ -39,11 +39,11 @@ class CLocalAlignmentStringKernel: public CStringKernel * @param opening gap opening penalty * @param extension gap extension penalty */ - CLocalAlignmentStringKernel( - CStringFeatures* l, CStringFeatures* r, + LocalAlignmentStringKernel( + std::shared_ptr> l, std::shared_ptr> r, float64_t opening=10, float64_t extension=2); - virtual ~CLocalAlignmentStringKernel(); + virtual ~LocalAlignmentStringKernel(); /** initialize kernel * @@ -51,7 +51,7 @@ class CLocalAlignmentStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/LocalityImprovedStringKernel.cpp b/src/shogun/kernel/string/LocalityImprovedStringKernel.cpp index 68dee8da1e2..f5987659a8f 100644 --- a/src/shogun/kernel/string/LocalityImprovedStringKernel.cpp +++ b/src/shogun/kernel/string/LocalityImprovedStringKernel.cpp @@ -12,15 +12,15 @@ using namespace shogun; -CLocalityImprovedStringKernel::CLocalityImprovedStringKernel() -: CStringKernel() +LocalityImprovedStringKernel::LocalityImprovedStringKernel() +: StringKernel() { init(); } -CLocalityImprovedStringKernel::CLocalityImprovedStringKernel( +LocalityImprovedStringKernel::LocalityImprovedStringKernel( int32_t size, int32_t l, int32_t id, int32_t od) -: CStringKernel(size) +: StringKernel(size) { init(); @@ -31,10 +31,10 @@ CLocalityImprovedStringKernel::CLocalityImprovedStringKernel( SG_DEBUG("LIK with parms: l={}, id={}, od={} created!", l, id, od) } -CLocalityImprovedStringKernel::CLocalityImprovedStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t len, +LocalityImprovedStringKernel::LocalityImprovedStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t len, int32_t id, int32_t od) -: CStringKernel() +: StringKernel() { init(); @@ -47,24 +47,24 @@ CLocalityImprovedStringKernel::CLocalityImprovedStringKernel( init(l, r); } -CLocalityImprovedStringKernel::~CLocalityImprovedStringKernel() +LocalityImprovedStringKernel::~LocalityImprovedStringKernel() { cleanup(); } -bool CLocalityImprovedStringKernel::init(CFeatures* l, CFeatures* r) +bool LocalityImprovedStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l,r); + StringKernel::init(l,r); return init_normalizer(); } -float64_t CLocalityImprovedStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t LocalityImprovedStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec = std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen && alen>0) @@ -89,14 +89,14 @@ float64_t CLocalityImprovedStringKernel::compute(int32_t idx_a, int32_t idx_b) } SG_FREE(match); - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return pow(outer_sum, outer_degree + 1); } -void CLocalityImprovedStringKernel::init() +void LocalityImprovedStringKernel::init() { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); length = 0; inner_degree = 0; diff --git a/src/shogun/kernel/string/LocalityImprovedStringKernel.h b/src/shogun/kernel/string/LocalityImprovedStringKernel.h index f55aac381ba..1f3921d5908 100644 --- a/src/shogun/kernel/string/LocalityImprovedStringKernel.h +++ b/src/shogun/kernel/string/LocalityImprovedStringKernel.h @@ -24,11 +24,11 @@ namespace shogun * where * \f$ I_i({\bf x},{\bf x'})=1\f$ if \f$x_i=x'_i\f$ and 0 otherwise. */ -class CLocalityImprovedStringKernel: public CStringKernel +class LocalityImprovedStringKernel: public StringKernel { public: /** default constructor */ - CLocalityImprovedStringKernel(); + LocalityImprovedStringKernel(); /** constructor * @@ -37,7 +37,7 @@ class CLocalityImprovedStringKernel: public CStringKernel * @param inner_degree inner degree * @param outer_degree outer degree */ - CLocalityImprovedStringKernel(int32_t size, int32_t length, + LocalityImprovedStringKernel(int32_t size, int32_t length, int32_t inner_degree, int32_t outer_degree); /** constructor @@ -48,11 +48,11 @@ class CLocalityImprovedStringKernel: public CStringKernel * @param inner_degree inner degree * @param outer_degree outer degree */ - CLocalityImprovedStringKernel( - CStringFeatures* l, CStringFeatures* r, + LocalityImprovedStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t length, int32_t inner_degree, int32_t outer_degree); - virtual ~CLocalityImprovedStringKernel(); + virtual ~LocalityImprovedStringKernel(); /** initialize kernel * @@ -60,7 +60,7 @@ class CLocalityImprovedStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/string/MatchWordStringKernel.cpp b/src/shogun/kernel/string/MatchWordStringKernel.cpp index bf92ec94b5d..b88d91fd857 100644 --- a/src/shogun/kernel/string/MatchWordStringKernel.cpp +++ b/src/shogun/kernel/string/MatchWordStringKernel.cpp @@ -13,45 +13,45 @@ using namespace shogun; -CMatchWordStringKernel::CMatchWordStringKernel() : CStringKernel() +MatchWordStringKernel::MatchWordStringKernel() : StringKernel() { init(); } -CMatchWordStringKernel::CMatchWordStringKernel(int32_t size, int32_t d) -: CStringKernel(size) +MatchWordStringKernel::MatchWordStringKernel(int32_t size, int32_t d) +: StringKernel(size) { init(); degree=d; } -CMatchWordStringKernel::CMatchWordStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t d) -: CStringKernel() +MatchWordStringKernel::MatchWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d) +: StringKernel() { init(); degree=d; init(l, r); } -CMatchWordStringKernel::~CMatchWordStringKernel() +MatchWordStringKernel::~MatchWordStringKernel() { cleanup(); } -bool CMatchWordStringKernel::init(CFeatures* l, CFeatures* r) +bool MatchWordStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -float64_t CMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t MatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + uint16_t* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + uint16_t* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) @@ -59,15 +59,15 @@ float64_t CMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b) for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); - return CMath::pow(sum, degree); + return Math::pow(sum, degree); } -void CMatchWordStringKernel::init() +void MatchWordStringKernel::init() { degree=0; - set_normalizer(new CAvgDiagKernelNormalizer()); + set_normalizer(std::make_shared()); SG_ADD(°ree, "degree", "Degree of poly kernel", ParameterProperties::HYPER); } diff --git a/src/shogun/kernel/string/MatchWordStringKernel.h b/src/shogun/kernel/string/MatchWordStringKernel.h index 983606fdc9d..2a12cbca62d 100644 --- a/src/shogun/kernel/string/MatchWordStringKernel.h +++ b/src/shogun/kernel/string/MatchWordStringKernel.h @@ -32,18 +32,18 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CMatchWordStringKernel: public CStringKernel +class MatchWordStringKernel: public StringKernel { public: /** default constructor */ - CMatchWordStringKernel(); + MatchWordStringKernel(); /** constructor * * @param size cache size * @param d degree */ - CMatchWordStringKernel(int32_t size, int32_t d); + MatchWordStringKernel(int32_t size, int32_t d); /** constructor * @@ -51,9 +51,9 @@ class CMatchWordStringKernel: public CStringKernel * @param r features of right-hand side * @param degree degree */ - CMatchWordStringKernel(CStringFeatures* l, CStringFeatures* r, int32_t degree); + MatchWordStringKernel(std::shared_ptr> l, std::shared_ptr> r, int32_t degree); - virtual ~CMatchWordStringKernel(); + virtual ~MatchWordStringKernel(); /** initialize kernel * @@ -61,7 +61,7 @@ class CMatchWordStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/string/OligoStringKernel.cpp b/src/shogun/kernel/string/OligoStringKernel.cpp index 12572b6fa5a..61194ef077b 100644 --- a/src/shogun/kernel/string/OligoStringKernel.cpp +++ b/src/shogun/kernel/string/OligoStringKernel.cpp @@ -13,14 +13,14 @@ using namespace shogun; -COligoStringKernel::COligoStringKernel() - : CStringKernel() +OligoStringKernel::OligoStringKernel() + : StringKernel() { init(); } -COligoStringKernel::COligoStringKernel(int32_t cache_sz, int32_t kmer_len, float64_t w) -: CStringKernel(cache_sz) +OligoStringKernel::OligoStringKernel(int32_t cache_sz, int32_t kmer_len, float64_t w) +: StringKernel(cache_sz) { init(); @@ -28,10 +28,10 @@ COligoStringKernel::COligoStringKernel(int32_t cache_sz, int32_t kmer_len, float width=w; } -COligoStringKernel::COligoStringKernel( - CStringFeatures* l, CStringFeatures* r, +OligoStringKernel::OligoStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t kmer_len, float64_t w) -: CStringKernel() +: StringKernel() { init(); @@ -41,25 +41,25 @@ COligoStringKernel::COligoStringKernel( init(l, r); } -COligoStringKernel::~COligoStringKernel() +OligoStringKernel::~OligoStringKernel() { cleanup(); } -void COligoStringKernel::cleanup() +void OligoStringKernel::cleanup() { gauss_table=SGVector(); - CKernel::cleanup(); + Kernel::cleanup(); } -bool COligoStringKernel::init(CFeatures* l, CFeatures* r) +bool OligoStringKernel::init(std::shared_ptr l, std::shared_ptr r) { cleanup(); - CStringKernel::init(l,r); - int32_t max_len=CMath::max( - ((CStringFeatures*) l)->get_max_vector_length(), - ((CStringFeatures*) r)->get_max_vector_length() + StringKernel::init(l,r); + int32_t max_len=Math::max( + std::static_pointer_cast>(l)->get_max_vector_length(), + std::static_pointer_cast>(r)->get_max_vector_length() ); require(k>0, "k must be >0"); @@ -69,7 +69,7 @@ bool COligoStringKernel::init(CFeatures* l, CFeatures* r) return init_normalizer(); } -void COligoStringKernel::encodeOligo( +void OligoStringKernel::encodeOligo( const std::string& sequence, uint32_t k_mer_length, const std::string& allowed_characters, std::vector< std::pair >& values) @@ -127,7 +127,7 @@ void COligoStringKernel::encodeOligo( } } -void COligoStringKernel::getSequences( +void OligoStringKernel::getSequences( const std::vector& sequences, uint32_t k_mer_length, const std::string& allowed_characters, std::vector< std::vector< std::pair > >& encoded_sequences) @@ -143,16 +143,16 @@ void COligoStringKernel::getSequences( } } -void COligoStringKernel::getExpFunctionCache(uint32_t sequence_length) +void OligoStringKernel::getExpFunctionCache(uint32_t sequence_length) { gauss_table=SGVector(sequence_length); gauss_table[0] = 1; for (uint32_t i = 1; i < sequence_length; i++) - gauss_table[i] = exp(-CMath::sq((float64_t) i) / width); + gauss_table[i] = exp(-Math::sq((float64_t) i) / width); } -float64_t COligoStringKernel::kernelOligoFast( +float64_t OligoStringKernel::kernelOligoFast( const std::vector< std::pair >& x, const std::vector< std::pair >& y, int32_t max_distance) @@ -231,7 +231,7 @@ float64_t COligoStringKernel::kernelOligoFast( return result; } -float64_t COligoStringKernel::kernelOligo( +float64_t OligoStringKernel::kernelOligo( const std::vector< std::pair >& x, const std::vector< std::pair >& y) { @@ -246,7 +246,7 @@ float64_t COligoStringKernel::kernelOligo( { if (x[i1].second == y[i2].second) { - result += exp(-CMath::sq(x[i1].first - y[i2].first) / width); + result += exp(-Math::sq(x[i1].first - y[i2].first) / width); if (((uint32_t) i1+1) < x_size && x[i1].second == x[i1 + 1].second) { @@ -278,29 +278,29 @@ float64_t COligoStringKernel::kernelOligo( return result; } -float64_t COligoStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t OligoStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_a, free_b; - char* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_a); - char* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_b); + char* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_a); + char* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_b); std::vector< std::pair > aenc; std::vector< std::pair > benc; encodeOligo(std::string(avec, alen), k, "ACGT", aenc); encodeOligo(std::string(bvec, alen), k, "ACGT", benc); //float64_t result=kernelOligo(aenc, benc); float64_t result=kernelOligoFast(aenc, benc); - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_a); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_b); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_a); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_b); return result; } -void COligoStringKernel::init() +void OligoStringKernel::init() { k=0; width=0.0; - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); SG_ADD(&k, "k", "K-mer length.", ParameterProperties::HYPER); SG_ADD(&width, "width", "Width of Gaussian.", ParameterProperties::HYPER); diff --git a/src/shogun/kernel/string/OligoStringKernel.h b/src/shogun/kernel/string/OligoStringKernel.h index 23fb95a33ad..e7588efbc1b 100644 --- a/src/shogun/kernel/string/OligoStringKernel.h +++ b/src/shogun/kernel/string/OligoStringKernel.h @@ -27,24 +27,24 @@ namespace shogun * applicable only to academic small scale problems: * * - the kernel should only ever see encoded sequences, which however - * requires another OligoFeatures object (using CDenseFeatures of pairs) + * requires another OligoFeatures object (using DenseFeatures of pairs) * * Uses CSqrtDiagKernelNormalizer, as the vanilla kernel seems to be very * diagonally dominant. * */ -class COligoStringKernel : public CStringKernel +class OligoStringKernel : public StringKernel { public: /** default constructor */ - COligoStringKernel(); + OligoStringKernel(); /** Constructor * @param cache_size cache size for kernel * @param k k-mer length * @param width - equivalent to 2*sigma^2 */ - COligoStringKernel(int32_t cache_size, int32_t k, float64_t width); + OligoStringKernel(int32_t cache_size, int32_t k, float64_t width); /** Constructor * @param l features of left-hand side @@ -52,12 +52,12 @@ class COligoStringKernel : public CStringKernel * @param k k-mer length * @param width - equivalent to 2*sigma^2 */ - COligoStringKernel( - CStringFeatures* l, CStringFeatures* r, + OligoStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t k, float64_t width); /** Destructor */ - virtual ~COligoStringKernel(); + virtual ~OligoStringKernel(); /** initialize kernel * @@ -65,7 +65,7 @@ class COligoStringKernel : public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * diff --git a/src/shogun/kernel/string/PolyMatchStringKernel.cpp b/src/shogun/kernel/string/PolyMatchStringKernel.cpp index 846a84b098f..585456152f0 100644 --- a/src/shogun/kernel/string/PolyMatchStringKernel.cpp +++ b/src/shogun/kernel/string/PolyMatchStringKernel.cpp @@ -13,14 +13,14 @@ using namespace shogun; -CPolyMatchStringKernel::CPolyMatchStringKernel() -: CStringKernel() +PolyMatchStringKernel::PolyMatchStringKernel() +: StringKernel() { init(); } -CPolyMatchStringKernel::CPolyMatchStringKernel(int32_t size, int32_t d, bool i) -: CStringKernel(size) +PolyMatchStringKernel::PolyMatchStringKernel(int32_t size, int32_t d, bool i) +: StringKernel(size) { init(); @@ -28,9 +28,9 @@ CPolyMatchStringKernel::CPolyMatchStringKernel(int32_t size, int32_t d, bool i) inhomogene=i; } -CPolyMatchStringKernel::CPolyMatchStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t d, bool i) -: CStringKernel(10) +PolyMatchStringKernel::PolyMatchStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d, bool i) +: StringKernel(10) { init(); @@ -40,29 +40,29 @@ CPolyMatchStringKernel::CPolyMatchStringKernel( init(l, r); } -CPolyMatchStringKernel::~CPolyMatchStringKernel() +PolyMatchStringKernel::~PolyMatchStringKernel() { cleanup(); } -bool CPolyMatchStringKernel::init(CFeatures* l, CFeatures* r) +bool PolyMatchStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CPolyMatchStringKernel::cleanup() +void PolyMatchStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CPolyMatchStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t PolyMatchStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t i, alen, blen, sum; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec = std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); ASSERT(alen==blen) for (i = 0, sum = inhomogene; i*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); - return CMath::pow(result , degree); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); + return Math::pow(result , degree); } -void CPolyMatchStringKernel::init() +void PolyMatchStringKernel::init() { degree=0; inhomogene=false; rescaling=false; - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); SG_ADD(°ree, "degree", "Degree of poly-kernel.", ParameterProperties::HYPER); SG_ADD(&inhomogene, "inhomogene", "True for inhomogene poly-kernel."); diff --git a/src/shogun/kernel/string/PolyMatchStringKernel.h b/src/shogun/kernel/string/PolyMatchStringKernel.h index 3889b438d26..121efa13e20 100644 --- a/src/shogun/kernel/string/PolyMatchStringKernel.h +++ b/src/shogun/kernel/string/PolyMatchStringKernel.h @@ -31,11 +31,11 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CPolyMatchStringKernel: public CStringKernel +class PolyMatchStringKernel: public StringKernel { public: /** default constructor */ - CPolyMatchStringKernel(); + PolyMatchStringKernel(); /** constructor * @@ -43,7 +43,7 @@ class CPolyMatchStringKernel: public CStringKernel * @param degree degree * @param inhomogene is inhomogeneous */ - CPolyMatchStringKernel(int32_t size, int32_t degree, bool inhomogene); + PolyMatchStringKernel(int32_t size, int32_t degree, bool inhomogene); /** constructor * @@ -52,11 +52,11 @@ class CPolyMatchStringKernel: public CStringKernel * @param degree degree * @param inhomogene is inhomogeneous */ - CPolyMatchStringKernel( - CStringFeatures* l, CStringFeatures* r, + PolyMatchStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t degree, bool inhomogene); - virtual ~CPolyMatchStringKernel(); + virtual ~PolyMatchStringKernel(); /** initialize kernel * @@ -64,7 +64,7 @@ class CPolyMatchStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/PolyMatchWordStringKernel.cpp b/src/shogun/kernel/string/PolyMatchWordStringKernel.cpp index 61e7c489c5e..b84156bcd4a 100644 --- a/src/shogun/kernel/string/PolyMatchWordStringKernel.cpp +++ b/src/shogun/kernel/string/PolyMatchWordStringKernel.cpp @@ -13,14 +13,14 @@ using namespace shogun; -CPolyMatchWordStringKernel::CPolyMatchWordStringKernel() -: CStringKernel() +PolyMatchWordStringKernel::PolyMatchWordStringKernel() +: StringKernel() { init(); } -CPolyMatchWordStringKernel::CPolyMatchWordStringKernel(int32_t size, int32_t d, bool i) -: CStringKernel(size) +PolyMatchWordStringKernel::PolyMatchWordStringKernel(int32_t size, int32_t d, bool i) +: StringKernel(size) { init(); @@ -28,9 +28,9 @@ CPolyMatchWordStringKernel::CPolyMatchWordStringKernel(int32_t size, int32_t d, inhomogene=i; } -CPolyMatchWordStringKernel::CPolyMatchWordStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t d, bool i) -: CStringKernel() +PolyMatchWordStringKernel::PolyMatchWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d, bool i) +: StringKernel() { init(); @@ -40,29 +40,29 @@ CPolyMatchWordStringKernel::CPolyMatchWordStringKernel( init(l, r); } -CPolyMatchWordStringKernel::~CPolyMatchWordStringKernel() +PolyMatchWordStringKernel::~PolyMatchWordStringKernel() { cleanup(); } -bool CPolyMatchWordStringKernel::init(CFeatures* l, CFeatures* r) +bool PolyMatchWordStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l,r); + StringKernel::init(l,r); return init_normalizer(); } -void CPolyMatchWordStringKernel::cleanup() +void PolyMatchWordStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CPolyMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t PolyMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + uint16_t* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + uint16_t* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); ASSERT(alen==blen) @@ -79,16 +79,16 @@ float64_t CPolyMatchWordStringKernel::compute(int32_t idx_a, int32_t idx_b) for (int32_t j=1; j*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return result; } -void CPolyMatchWordStringKernel::init() +void PolyMatchWordStringKernel::init() { degree=0; inhomogene=false; - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); SG_ADD(°ree, "degree", "Degree of poly-kernel.", ParameterProperties::HYPER); SG_ADD(&inhomogene, "inhomogene", "True for inhomogene poly-kernel."); diff --git a/src/shogun/kernel/string/PolyMatchWordStringKernel.h b/src/shogun/kernel/string/PolyMatchWordStringKernel.h index d283713147e..9ece3814d2f 100644 --- a/src/shogun/kernel/string/PolyMatchWordStringKernel.h +++ b/src/shogun/kernel/string/PolyMatchWordStringKernel.h @@ -33,11 +33,11 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CPolyMatchWordStringKernel: public CStringKernel +class PolyMatchWordStringKernel: public StringKernel { public: /** default constructor */ - CPolyMatchWordStringKernel(); + PolyMatchWordStringKernel(); /** constructor * @@ -45,7 +45,7 @@ class CPolyMatchWordStringKernel: public CStringKernel * @param degree degree * @param inhomogene is inhomogeneous */ - CPolyMatchWordStringKernel(int32_t size, int32_t degree, bool inhomogene); + PolyMatchWordStringKernel(int32_t size, int32_t degree, bool inhomogene); /** constructor * @@ -54,9 +54,9 @@ class CPolyMatchWordStringKernel: public CStringKernel * @param degree degree * @param inhomogene is inhomogeneous */ - CPolyMatchWordStringKernel(CStringFeatures* l, CStringFeatures* r, int32_t degree, bool inhomogene); + PolyMatchWordStringKernel(std::shared_ptr> l, std::shared_ptr> r, int32_t degree, bool inhomogene); - virtual ~CPolyMatchWordStringKernel(); + virtual ~PolyMatchWordStringKernel(); /** initialize kernel * @@ -64,7 +64,7 @@ class CPolyMatchWordStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/RegulatoryModulesStringKernel.cpp b/src/shogun/kernel/string/RegulatoryModulesStringKernel.cpp index bd66bba3844..b716e6fd2ed 100644 --- a/src/shogun/kernel/string/RegulatoryModulesStringKernel.cpp +++ b/src/shogun/kernel/string/RegulatoryModulesStringKernel.cpp @@ -11,36 +11,36 @@ using namespace shogun; -CRegulatoryModulesStringKernel::CRegulatoryModulesStringKernel() -: CStringKernel(0) +RegulatoryModulesStringKernel::RegulatoryModulesStringKernel() +: StringKernel(0) { init(); } -CRegulatoryModulesStringKernel::CRegulatoryModulesStringKernel( +RegulatoryModulesStringKernel::RegulatoryModulesStringKernel( int32_t size, float64_t w, int32_t d, int32_t s, int32_t wl) -: CStringKernel(size) +: StringKernel(size) { init(); } -CRegulatoryModulesStringKernel::CRegulatoryModulesStringKernel(CStringFeatures* lstr, CStringFeatures* rstr, - CDenseFeatures* lpos, CDenseFeatures* rpos, +RegulatoryModulesStringKernel::RegulatoryModulesStringKernel(std::shared_ptr> lstr, std::shared_ptr> rstr, + std::shared_ptr> lpos, std::shared_ptr> rpos, float64_t w, int32_t d, int32_t s, int32_t wl, int32_t size) -: CStringKernel(size) +: StringKernel(size) { init(); set_motif_positions(lpos, rpos); init(lstr,rstr); } -CRegulatoryModulesStringKernel::~CRegulatoryModulesStringKernel() +RegulatoryModulesStringKernel::~RegulatoryModulesStringKernel() { - SG_UNREF(motif_positions_lhs); - SG_UNREF(motif_positions_rhs); + + } -void CRegulatoryModulesStringKernel::init() +void RegulatoryModulesStringKernel::init() { width=0; degree=0; @@ -55,15 +55,15 @@ void CRegulatoryModulesStringKernel::init() SG_ADD(&shift, "shift", "the shift of weighted degree with shifts kernel part", ParameterProperties::HYPER); SG_ADD(&window, "window", "the size of window around motifs", ParameterProperties::HYPER); - SG_ADD((CSGObject**)&motif_positions_lhs, "motif_positions_lhs", + SG_ADD((std::shared_ptr*)&motif_positions_lhs, "motif_positions_lhs", "the matrix of motif positions from sequences left-hand side"); - SG_ADD((CSGObject**)&motif_positions_rhs, "motif_positions_rhs", + SG_ADD((std::shared_ptr*)&motif_positions_rhs, "motif_positions_rhs", "the matrix of motif positions from sequences right-hand side"); SG_ADD(&position_weights, "position_weights", "scaling weights in window"); SG_ADD(&weights, "weights", "weights of WD kernel"); } -bool CRegulatoryModulesStringKernel::init(CFeatures* l, CFeatures* r) +bool RegulatoryModulesStringKernel::init(std::shared_ptr l, std::shared_ptr r) { ASSERT(motif_positions_lhs) ASSERT(motif_positions_rhs) @@ -76,27 +76,27 @@ bool CRegulatoryModulesStringKernel::init(CFeatures* l, CFeatures* r) r->get_num_vectors(), motif_positions_rhs->get_num_vectors()); set_wd_weights(); - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CRegulatoryModulesStringKernel::set_motif_positions( - CDenseFeatures* positions_lhs, CDenseFeatures* positions_rhs) +void RegulatoryModulesStringKernel::set_motif_positions( + std::shared_ptr> positions_lhs, std::shared_ptr> positions_rhs) { ASSERT(positions_lhs) ASSERT(positions_rhs) - SG_UNREF(motif_positions_lhs); - SG_UNREF(motif_positions_rhs); + + if (positions_lhs->get_num_features() != positions_rhs->get_num_features()) error("Number of dimensions does not agree."); motif_positions_lhs=positions_lhs; motif_positions_rhs=positions_rhs; - SG_REF(positions_lhs); - SG_REF(positions_rhs); + + } -float64_t CRegulatoryModulesStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t RegulatoryModulesStringKernel::compute(int32_t idx_a, int32_t idx_b) { ASSERT(motif_positions_lhs) ASSERT(motif_positions_rhs) @@ -104,8 +104,8 @@ float64_t CRegulatoryModulesStringKernel::compute(int32_t idx_a, int32_t idx_b) bool free_avec, free_bvec; int32_t alen=0; int32_t blen=0; - char* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); int32_t alen_pos, blen_pos; bool afree_pos, bfree_pos; @@ -120,17 +120,17 @@ float64_t CRegulatoryModulesStringKernel::compute(int32_t idx_a, int32_t idx_b) for (int32_t p=0; p alen) limit = alen - positions_a[p]; if (window + positions_b[p] > blen) - limit = CMath::min(limit, blen - positions_b[p]); + limit = Math::min(limit, blen - positions_b[p]); result_wds+=compute_wds(&avec[positions_a[p]], &bvec[positions_b[p]], limit); @@ -138,15 +138,15 @@ float64_t CRegulatoryModulesStringKernel::compute(int32_t idx_a, int32_t idx_b) float64_t result=exp(-result_rbf/width)+result_wds; - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); - ((CDenseFeatures*) lhs)->free_feature_vector(positions_a, idx_a, afree_pos); - ((CDenseFeatures*) rhs)->free_feature_vector(positions_b, idx_b, bfree_pos); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(positions_a, idx_a, afree_pos); + std::static_pointer_cast>(rhs)->free_feature_vector(positions_b, idx_b, bfree_pos); return result; } -float64_t CRegulatoryModulesStringKernel::compute_wds( +float64_t RegulatoryModulesStringKernel::compute_wds( char* avec, char* bvec, int32_t len) { float64_t* max_shift_vec = SG_MALLOC(float64_t, shift); @@ -211,7 +211,7 @@ float64_t CRegulatoryModulesStringKernel::compute_wds( return result ; } -void CRegulatoryModulesStringKernel::set_wd_weights() +void RegulatoryModulesStringKernel::set_wd_weights() { ASSERT(degree>0) diff --git a/src/shogun/kernel/string/RegulatoryModulesStringKernel.h b/src/shogun/kernel/string/RegulatoryModulesStringKernel.h index 255e96e4f04..e5dea1d71bb 100644 --- a/src/shogun/kernel/string/RegulatoryModulesStringKernel.h +++ b/src/shogun/kernel/string/RegulatoryModulesStringKernel.h @@ -20,11 +20,11 @@ namespace shogun * on regulatory sequences. * */ -class CRegulatoryModulesStringKernel: public CStringKernel +class RegulatoryModulesStringKernel: public StringKernel { public: /** default constructor */ - CRegulatoryModulesStringKernel(); + RegulatoryModulesStringKernel(); /** constructor * @@ -34,7 +34,7 @@ class CRegulatoryModulesStringKernel: public CStringKernel * @param shift shift of wds kernel * @param window size of window around motifs to compute wds kernels on */ - CRegulatoryModulesStringKernel(int32_t size, float64_t width, int32_t degree, int32_t shift, int32_t window); + RegulatoryModulesStringKernel(int32_t size, float64_t width, int32_t degree, int32_t shift, int32_t window); /** constructor * @@ -48,12 +48,12 @@ class CRegulatoryModulesStringKernel: public CStringKernel * @param window size of window around motifs to compute wds kernels on * @param size cache size */ - CRegulatoryModulesStringKernel(CStringFeatures* lstr, CStringFeatures* rstr, - CDenseFeatures* lpos, CDenseFeatures* rpos, + RegulatoryModulesStringKernel(std::shared_ptr> lstr, std::shared_ptr> rstr, + std::shared_ptr> lpos, std::shared_ptr> rpos, float64_t width, int32_t degree, int32_t shift, int32_t window, int32_t size=10); /** default destructor */ - virtual ~CRegulatoryModulesStringKernel(); + virtual ~RegulatoryModulesStringKernel(); /** initialize kernel * @@ -61,7 +61,7 @@ class CRegulatoryModulesStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** return what type of kernel we are * @@ -81,7 +81,7 @@ class CRegulatoryModulesStringKernel: public CStringKernel * @param positions_rhs motif positions on rhs */ void set_motif_positions( - CDenseFeatures* positions_lhs, CDenseFeatures* positions_rhs); + std::shared_ptr> positions_lhs, std::shared_ptr> positions_rhs); protected: /** compute kernel function for features a and b @@ -124,10 +124,10 @@ class CRegulatoryModulesStringKernel: public CStringKernel int32_t window; /** Matrix of motif positions from sequences left-hand side */ - CDenseFeatures* motif_positions_lhs; + std::shared_ptr> motif_positions_lhs; /** Matrix of motif positions from sequences right-hand side */ - CDenseFeatures* motif_positions_rhs; + std::shared_ptr> motif_positions_rhs; /** scaling weights in window */ SGVector position_weights; diff --git a/src/shogun/kernel/string/SNPStringKernel.cpp b/src/shogun/kernel/string/SNPStringKernel.cpp index 368c76a5c63..4095b00df8b 100644 --- a/src/shogun/kernel/string/SNPStringKernel.cpp +++ b/src/shogun/kernel/string/SNPStringKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Fernando Iglesias, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Fernando Iglesias, * Sergey Lisitsyn */ @@ -14,69 +14,70 @@ using namespace shogun; -CSNPStringKernel::CSNPStringKernel() -: CStringKernel(0), +SNPStringKernel::SNPStringKernel() +: StringKernel(0), m_degree(0), m_win_len(0), m_inhomogene(false) { init(); - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); register_params(); } -CSNPStringKernel::CSNPStringKernel(int32_t size, +SNPStringKernel::SNPStringKernel(int32_t size, int32_t degree, int32_t win_len, bool inhomogene) -: CStringKernel(size), +: StringKernel(size), m_degree(degree), m_win_len(2*win_len), m_inhomogene(inhomogene) { init(); - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); register_params(); } -CSNPStringKernel::CSNPStringKernel( - CStringFeatures* l, CStringFeatures* r, +SNPStringKernel::SNPStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t degree, int32_t win_len, bool inhomogene) -: CStringKernel(10), m_degree(degree), m_win_len(2*win_len), +: StringKernel(10), m_degree(degree), m_win_len(2*win_len), m_inhomogene(inhomogene) { init(); - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); if (l==r) obtain_base_strings(); init(l, r); register_params(); } -CSNPStringKernel::~CSNPStringKernel() +SNPStringKernel::~SNPStringKernel() { cleanup(); } -bool CSNPStringKernel::init(CFeatures* l, CFeatures* r) +bool SNPStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CSNPStringKernel::cleanup() +void SNPStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); SG_FREE(m_str_min); SG_FREE(m_str_maj); } -void CSNPStringKernel::obtain_base_strings() +void SNPStringKernel::obtain_base_strings() { //should only be called on training data ASSERT(lhs==rhs) m_str_len=0; + auto sf = std::static_pointer_cast>(lhs); for (int32_t i=0; i*) lhs)->get_feature_vector(i, len, free_vec); + char* vec = sf->get_feature_vector(i, len, free_vec); if (m_str_len==0) { @@ -101,7 +102,7 @@ void CSNPStringKernel::obtain_base_strings() m_str_maj[j]=vec[j]; } - ((CStringFeatures*) lhs)->free_feature_vector(vec, i, free_vec); + sf->free_feature_vector(vec, i, free_vec); } for (int32_t j=0; jm_str_maj[j]) - CMath::swap(m_str_min[j], m_str_maj[j]); + Math::swap(m_str_min[j], m_str_maj[j]); } } -float64_t CSNPStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SNPStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec = std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); ASSERT(alen==blen) if (alen!=m_str_len) @@ -166,30 +167,30 @@ float64_t CSNPStringKernel::compute(int32_t idx_a, int32_t idx_b) } } - total+=CMath::pow(float64_t(sumaa+sumbb+sumab+inhomogene), + total+=Math::pow(float64_t(sumaa+sumbb+sumab+inhomogene), (int32_t) m_degree); } - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return total; } -void CSNPStringKernel::register_params() +void SNPStringKernel::register_params() { SG_ADD(&m_degree, "m_degree", "the order of the kernel", ParameterProperties::HYPER); SG_ADD(&m_win_len, "m_win_len", "the window length", ParameterProperties::HYPER); SG_ADD(&m_inhomogene, "m_inhomogene", "the mark of whether it's an inhomogeneous poly kernel"); - m_parameters->add_vector(&m_str_min, &m_str_len, "m_str_min", "allele A"); + /*m_parameters->add_vector(&m_str_min, &m_str_len, "m_str_min", "allele A");*/ watch_param("m_str_min", &m_str_min, &m_str_len); - m_parameters->add_vector(&m_str_maj, &m_str_len, "m_str_maj", "allele B"); + /*m_parameters->add_vector(&m_str_maj, &m_str_len, "m_str_maj", "allele B");*/ watch_param("m_str_maj", &m_str_maj, &m_str_len); } -void CSNPStringKernel::init() +void SNPStringKernel::init() { m_str_min=NULL; m_str_maj=NULL; diff --git a/src/shogun/kernel/string/SNPStringKernel.h b/src/shogun/kernel/string/SNPStringKernel.h index e4cdf04ed8d..2e526b47ea6 100644 --- a/src/shogun/kernel/string/SNPStringKernel.h +++ b/src/shogun/kernel/string/SNPStringKernel.h @@ -32,11 +32,11 @@ namespace shogun * k'({\bf x}, {\bf x'})=\frac{k({\bf x}, {\bf x'})}{\sqrt{k({\bf x}, {\bf x})k({\bf x'}, {\bf x'})}} * \f] */ -class CSNPStringKernel: public CStringKernel +class SNPStringKernel: public StringKernel { public: /** default constructor */ - CSNPStringKernel(); + SNPStringKernel(); /** constructor * @@ -45,7 +45,7 @@ class CSNPStringKernel: public CStringKernel * @param win_len length of local window * @param inhomogene whether inhomogeneous poly */ - CSNPStringKernel(int32_t size, int32_t degree, int32_t win_len, bool inhomogene); + SNPStringKernel(int32_t size, int32_t degree, int32_t win_len, bool inhomogene); /** constructor * @@ -55,11 +55,11 @@ class CSNPStringKernel: public CStringKernel * @param win_len length of local window * @param inhomogene whether inhomogeneous poly */ - CSNPStringKernel( - CStringFeatures* l, CStringFeatures* r, + SNPStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t degree, int32_t win_len, bool inhomogene); - virtual ~CSNPStringKernel(); + virtual ~SNPStringKernel(); /** initialize kernel * @@ -67,7 +67,7 @@ class CSNPStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/SalzbergWordStringKernel.cpp b/src/shogun/kernel/string/SalzbergWordStringKernel.cpp index 803f4b1ee2c..f64d7db7725 100644 --- a/src/shogun/kernel/string/SalzbergWordStringKernel.cpp +++ b/src/shogun/kernel/string/SalzbergWordStringKernel.cpp @@ -15,14 +15,14 @@ using namespace shogun; -CSalzbergWordStringKernel::CSalzbergWordStringKernel() -: CStringKernel(0) +SalzbergWordStringKernel::SalzbergWordStringKernel() +: StringKernel(0) { init(); } -CSalzbergWordStringKernel::CSalzbergWordStringKernel(int32_t size, CPluginEstimate* pie, CLabels* labels) -: CStringKernel(size) +SalzbergWordStringKernel::SalzbergWordStringKernel(int32_t size, std::shared_ptr pie, std::shared_ptr labels) +: StringKernel(size) { init(); estimate=pie; @@ -31,10 +31,10 @@ CSalzbergWordStringKernel::CSalzbergWordStringKernel(int32_t size, CPluginEstima set_prior_probs_from_labels(labels); } -CSalzbergWordStringKernel::CSalzbergWordStringKernel( - CStringFeatures* l, CStringFeatures* r, - CPluginEstimate* pie, CLabels* labels) -: CStringKernel(10),estimate(pie) +SalzbergWordStringKernel::SalzbergWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, + std::shared_ptr pie, std::shared_ptr labels) +: StringKernel(10),estimate(pie) { init(); estimate=pie; @@ -45,17 +45,17 @@ CSalzbergWordStringKernel::CSalzbergWordStringKernel( init(l, r); } -CSalzbergWordStringKernel::~CSalzbergWordStringKernel() +SalzbergWordStringKernel::~SalzbergWordStringKernel() { cleanup(); } -bool CSalzbergWordStringKernel::init(CFeatures* p_l, CFeatures* p_r) +bool SalzbergWordStringKernel::init(std::shared_ptr p_l, std::shared_ptr p_r) { - CStringKernel::init(p_l,p_r); - CStringFeatures* l=(CStringFeatures*) p_l; + StringKernel::init(p_l,p_r); + auto l=std::static_pointer_cast>(p_l); ASSERT(l) - CStringFeatures* r=(CStringFeatures*) p_r; + auto r=std::static_pointer_cast>(p_r); ASSERT(r) int32_t i; @@ -168,7 +168,7 @@ bool CSalzbergWordStringKernel::init(CFeatures* p_l, CFeatures* p_r) float64_t theta_n = 1/estimate->log_derivative_neg_obsolete(vec[j], j) ; float64_t value = (theta_p/(pos_prior*theta_p+neg_prior*theta_n)) ; - variance[idx] += CMath::sq(value-mean[idx])/num_vectors; + variance[idx] += Math::sq(value-mean[idx])/num_vectors; } } } @@ -286,7 +286,7 @@ bool CSalzbergWordStringKernel::init(CFeatures* p_l, CFeatures* p_r) return init_normalizer(); } -void CSalzbergWordStringKernel::cleanup() +void SalzbergWordStringKernel::cleanup() { SG_FREE(variance); variance=NULL; @@ -308,15 +308,15 @@ void CSalzbergWordStringKernel::cleanup() SG_FREE(ld_mean_lhs); ld_mean_lhs=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CSalzbergWordStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SalzbergWordStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - uint16_t* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - uint16_t* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + uint16_t* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + uint16_t* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) @@ -337,8 +337,8 @@ float64_t CSalzbergWordStringKernel::compute(int32_t idx_a, int32_t idx_b) } result += ld_mean_lhs[idx_a] + ld_mean_rhs[idx_b] ; - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); if (initialized) result /= (sqrtdiag_lhs[idx_a]*sqrtdiag_rhs[idx_b]) ; @@ -346,18 +346,19 @@ float64_t CSalzbergWordStringKernel::compute(int32_t idx_a, int32_t idx_b) return result; } -void CSalzbergWordStringKernel::set_prior_probs_from_labels(CLabels* labels) +void SalzbergWordStringKernel::set_prior_probs_from_labels(std::shared_ptr labels) { ASSERT(labels) ASSERT(labels->get_label_type() == LT_BINARY) labels->ensure_valid(); int32_t num_pos=0, num_neg=0; + auto bl = binary_labels(labels); for (int32_t i=0; iget_num_labels(); i++) { - if (((CBinaryLabels*) labels)->get_int_label(i)==1) + if (bl->get_int_label(i)==1) num_pos++; - if (((CBinaryLabels*) labels)->get_int_label(i)==-1) + if (bl->get_int_label(i)==-1) num_neg++; } @@ -370,7 +371,7 @@ void CSalzbergWordStringKernel::set_prior_probs_from_labels(CLabels* labels) (float64_t)num_neg/(num_pos+num_neg)); } -void CSalzbergWordStringKernel::init() +void SalzbergWordStringKernel::init() { estimate=NULL; mean=NULL; diff --git a/src/shogun/kernel/string/SalzbergWordStringKernel.h b/src/shogun/kernel/string/SalzbergWordStringKernel.h index 30475d76fa6..73f7f07649f 100644 --- a/src/shogun/kernel/string/SalzbergWordStringKernel.h +++ b/src/shogun/kernel/string/SalzbergWordStringKernel.h @@ -24,11 +24,11 @@ namespace shogun * A. Zien, G.Raetsch, S. Mika, B. Schoelkopf, T. Lengauer, K.-R. Mueller * */ -class CSalzbergWordStringKernel: public CStringKernel +class SalzbergWordStringKernel: public StringKernel { public: /** default constructor */ - CSalzbergWordStringKernel(); + SalzbergWordStringKernel(); /** constructor * @@ -36,7 +36,7 @@ class CSalzbergWordStringKernel: public CStringKernel * @param pie the plugin estimate * @param labels optional labels to set prior from */ - CSalzbergWordStringKernel(int32_t size, CPluginEstimate* pie, CLabels* labels=NULL); + SalzbergWordStringKernel(int32_t size, std::shared_ptr pie, std::shared_ptr labels=NULL); /** constructor * @@ -45,11 +45,11 @@ class CSalzbergWordStringKernel: public CStringKernel * @param pie the plugin estimate * @param labels optional labels to set prior from */ - CSalzbergWordStringKernel( - CStringFeatures* l, CStringFeatures* r, - CPluginEstimate *pie, CLabels* labels=NULL); + SalzbergWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, + std::shared_ptrpie, std::shared_ptr labels=NULL); - virtual ~CSalzbergWordStringKernel(); + virtual ~SalzbergWordStringKernel(); /** set prior probs * @@ -68,7 +68,7 @@ class CSalzbergWordStringKernel: public CStringKernel * * @param labels labels to set prior probabilites from */ - void set_prior_probs_from_labels(CLabels* labels); + void set_prior_probs_from_labels(std::shared_ptr labels); /** initialize kernel * @@ -76,7 +76,7 @@ class CSalzbergWordStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -120,7 +120,7 @@ class CSalzbergWordStringKernel: public CStringKernel protected: /** the plugin estimate */ - CPluginEstimate* estimate; + std::shared_ptr estimate; /** mean */ float64_t* mean; diff --git a/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.cpp b/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.cpp index 413e85598a2..abcc9a825c9 100644 --- a/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.cpp +++ b/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.cpp @@ -13,15 +13,15 @@ using namespace shogun; -CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel() -: CStringKernel() +SimpleLocalityImprovedStringKernel::SimpleLocalityImprovedStringKernel() +: StringKernel() { init(); } -CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel( +SimpleLocalityImprovedStringKernel::SimpleLocalityImprovedStringKernel( int32_t size, int32_t l, int32_t id, int32_t od) -: CStringKernel(size) +: StringKernel(size) { init(); @@ -30,10 +30,10 @@ CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel( outer_degree=od; } -CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel( - CStringFeatures* l, CStringFeatures* r, +SimpleLocalityImprovedStringKernel::SimpleLocalityImprovedStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t len, int32_t id, int32_t od) -: CStringKernel() +: StringKernel() { init(); @@ -44,18 +44,18 @@ CSimpleLocalityImprovedStringKernel::CSimpleLocalityImprovedStringKernel( init(l, r); } -CSimpleLocalityImprovedStringKernel::~CSimpleLocalityImprovedStringKernel() +SimpleLocalityImprovedStringKernel::~SimpleLocalityImprovedStringKernel() { cleanup(); } -bool CSimpleLocalityImprovedStringKernel::init(CFeatures* l, CFeatures* r) +bool SimpleLocalityImprovedStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - bool result = CStringKernel::init(l,r); + bool result = StringKernel::init(l,r); if (!result) return false; - const int32_t num_features = ((CStringFeatures*) l)->get_max_vector_length(); + const int32_t num_features = std::static_pointer_cast>(l)->get_max_vector_length(); const int32_t PYRAL = 2 * length - 1; // total window length const int32_t pyra_len = num_features-PYRAL+1; const int32_t pyra_len2 = (int32_t) pyra_len/2; @@ -99,13 +99,13 @@ bool CSimpleLocalityImprovedStringKernel::init(CFeatures* l, CFeatures* r) return init_normalizer(); } -void CSimpleLocalityImprovedStringKernel::cleanup() +void SimpleLocalityImprovedStringKernel::cleanup() { pyramid_weights = SGVector(); - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CSimpleLocalityImprovedStringKernel::dot_pyr (const char* const x1, +float64_t SimpleLocalityImprovedStringKernel::dot_pyr (const char* const x1, const char* const x2, const int32_t NOF_NTS, const int32_t NTWIDTH, const int32_t DEGREE1, const int32_t DEGREE2, float64_t *pyra) { @@ -167,14 +167,14 @@ float64_t CSimpleLocalityImprovedStringKernel::dot_pyr (const char* const x1, return pot; } -float64_t CSimpleLocalityImprovedStringKernel::compute( +float64_t SimpleLocalityImprovedStringKernel::compute( int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec = std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec = std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) @@ -184,14 +184,14 @@ float64_t CSimpleLocalityImprovedStringKernel::compute( dpt = dot_pyr(avec, bvec, alen, length, inner_degree, outer_degree, pyramid_weights); dpt = dpt / pow((float64_t) alen, (float64_t) outer_degree); - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return (float64_t) dpt; } -void CSimpleLocalityImprovedStringKernel::init() +void SimpleLocalityImprovedStringKernel::init() { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); length = 3; inner_degree = 3; diff --git a/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.h b/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.h index 6702705b147..a64bd2db21d 100644 --- a/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.h +++ b/src/shogun/kernel/string/SimpleLocalityImprovedStringKernel.h @@ -25,11 +25,11 @@ namespace shogun * where * \f$ I_i({\bf x},{\bf x'})=1\f$ if \f$x_i=x'_i\f$ and 0 otherwise. */ -class CSimpleLocalityImprovedStringKernel: public CStringKernel +class SimpleLocalityImprovedStringKernel: public StringKernel { public: /** default constructor */ - CSimpleLocalityImprovedStringKernel(); + SimpleLocalityImprovedStringKernel(); /** constructor * @@ -38,7 +38,7 @@ class CSimpleLocalityImprovedStringKernel: public CStringKernel * @param inner_degree inner degree * @param outer_degree outer degree */ - CSimpleLocalityImprovedStringKernel(int32_t size, int32_t length, + SimpleLocalityImprovedStringKernel(int32_t size, int32_t length, int32_t inner_degree, int32_t outer_degree); /** constructor @@ -49,11 +49,11 @@ class CSimpleLocalityImprovedStringKernel: public CStringKernel * @param inner_degree inner degree * @param outer_degree outer degree */ - CSimpleLocalityImprovedStringKernel( - CStringFeatures* l, CStringFeatures* r, + SimpleLocalityImprovedStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t length, int32_t inner_degree, int32_t outer_degree); - virtual ~CSimpleLocalityImprovedStringKernel(); + virtual ~SimpleLocalityImprovedStringKernel(); /** initialize kernel * @@ -61,7 +61,7 @@ class CSimpleLocalityImprovedStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures *l, CFeatures *r); + virtual bool init(std::shared_ptrl, std::shared_ptrr); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/SparseSpatialSampleStringKernel.cpp b/src/shogun/kernel/string/SparseSpatialSampleStringKernel.cpp index 4829fc65fbb..8a539e95074 100644 --- a/src/shogun/kernel/string/SparseSpatialSampleStringKernel.cpp +++ b/src/shogun/kernel/string/SparseSpatialSampleStringKernel.cpp @@ -12,33 +12,33 @@ using namespace shogun; -CSparseSpatialSampleStringKernel::CSparseSpatialSampleStringKernel() -: CStringKernel(0), t(2), d(5) +SparseSpatialSampleStringKernel::SparseSpatialSampleStringKernel() +: StringKernel(0), t(2), d(5) { } -CSparseSpatialSampleStringKernel::CSparseSpatialSampleStringKernel(CStringFeatures* l, - CStringFeatures* r) : CStringKernel(0), t(2), d(5) +SparseSpatialSampleStringKernel::SparseSpatialSampleStringKernel(std::shared_ptr> l, + std::shared_ptr> r) : StringKernel(0), t(2), d(5) { init(l, r); } -bool CSparseSpatialSampleStringKernel::init(CFeatures* l, CFeatures* r) +bool SparseSpatialSampleStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CSparseSpatialSampleStringKernel::cleanup() +void SparseSpatialSampleStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -CSparseSpatialSampleStringKernel::~CSparseSpatialSampleStringKernel() +SparseSpatialSampleStringKernel::~SparseSpatialSampleStringKernel() { } -SSKFeatures *CSparseSpatialSampleStringKernel::extractTriple(int **S, int *len, int nStr, int d1, int d2) +SSKFeatures *SparseSpatialSampleStringKernel::extractTriple(int **S, int *len, int nStr, int d1, int d2) { int i, j; int n, nfeat; @@ -76,7 +76,7 @@ SSKFeatures *CSparseSpatialSampleStringKernel::extractTriple(int **S, int *len, } -SSKFeatures *CSparseSpatialSampleStringKernel::extractDouble(int **S, int *len, int nStr, int d1) +SSKFeatures *SparseSpatialSampleStringKernel::extractDouble(int **S, int *len, int nStr, int d1) { int i, j; int n, nfeat; @@ -114,7 +114,7 @@ SSKFeatures *CSparseSpatialSampleStringKernel::extractDouble(int **S, int *len, } -void CSparseSpatialSampleStringKernel::compute_double(int32_t idx_a, int32_t idx_b) +void SparseSpatialSampleStringKernel::compute_double(int32_t idx_a, int32_t idx_b) { int d1; SSKFeatures *features; @@ -179,7 +179,7 @@ void CSparseSpatialSampleStringKernel::compute_double(int32_t idx_a, int32_t idx } } -void CSparseSpatialSampleStringKernel::compute_triple(int32_t idx_a, int32_t idx_b) +void SparseSpatialSampleStringKernel::compute_triple(int32_t idx_a, int32_t idx_b) { int d1, d2; SSKFeatures *features; @@ -255,7 +255,7 @@ void CSparseSpatialSampleStringKernel::compute_triple(int32_t idx_a, int32_t idx } } -void CSparseSpatialSampleStringKernel::countAndUpdate(int *outK, int *sx, int *g, int k, int r, int nStr) +void SparseSpatialSampleStringKernel::countAndUpdate(int *outK, int *sx, int *g, int k, int r, int nStr) { char same; int i, j; @@ -324,7 +324,7 @@ void CSparseSpatialSampleStringKernel::countAndUpdate(int *outK, int *sx, int *g SG_FREE(curfeat); } -int *CSparseSpatialSampleStringKernel::cntsrtna(int *sx, int k, int r, int na) +int *SparseSpatialSampleStringKernel::cntsrtna(int *sx, int k, int r, int na) { int *sxc, *bc, *sxl, *cc, *regroup; int i, j; @@ -359,7 +359,7 @@ int *CSparseSpatialSampleStringKernel::cntsrtna(int *sx, int k, int r, int na) return regroup; } -float64_t CSparseSpatialSampleStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SparseSpatialSampleStringKernel::compute(int32_t idx_a, int32_t idx_b) { if (t==2) compute_double(idx_a, idx_b); diff --git a/src/shogun/kernel/string/SparseSpatialSampleStringKernel.h b/src/shogun/kernel/string/SparseSpatialSampleStringKernel.h index beb51963ee5..b68997d3a50 100644 --- a/src/shogun/kernel/string/SparseSpatialSampleStringKernel.h +++ b/src/shogun/kernel/string/SparseSpatialSampleStringKernel.h @@ -29,21 +29,21 @@ namespace shogun * by Pavel Kuksa and * Vladimir Pavlovic */ -class CSparseSpatialSampleStringKernel: public CStringKernel +class SparseSpatialSampleStringKernel: public StringKernel { public: /** constructor */ - CSparseSpatialSampleStringKernel(); + SparseSpatialSampleStringKernel(); /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CSparseSpatialSampleStringKernel(CStringFeatures* l, CStringFeatures* r); + SparseSpatialSampleStringKernel(std::shared_ptr> l, std::shared_ptr> r); - virtual ~CSparseSpatialSampleStringKernel(); + virtual ~SparseSpatialSampleStringKernel(); /** initialize kernel * @@ -51,7 +51,7 @@ class CSparseSpatialSampleStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/SpectrumMismatchRBFKernel.cpp b/src/shogun/kernel/string/SpectrumMismatchRBFKernel.cpp index 4e395a77da7..db36c0d5b97 100644 --- a/src/shogun/kernel/string/SpectrumMismatchRBFKernel.cpp +++ b/src/shogun/kernel/string/SpectrumMismatchRBFKernel.cpp @@ -27,17 +27,17 @@ using namespace shogun; -CSpectrumMismatchRBFKernel::CSpectrumMismatchRBFKernel() : - CStringKernel(0) +SpectrumMismatchRBFKernel::SpectrumMismatchRBFKernel() : + StringKernel(0) { init(); register_params(); } -CSpectrumMismatchRBFKernel::CSpectrumMismatchRBFKernel(int32_t size, +SpectrumMismatchRBFKernel::SpectrumMismatchRBFKernel(int32_t size, float64_t* AA_matrix_, int32_t nr, int32_t nc, int32_t degree_, int32_t max_mismatch_, float64_t width_) : - CStringKernel(size), alphabet(NULL), degree(degree_), max_mismatch( + StringKernel(size), alphabet(NULL), degree(degree_), max_mismatch( max_mismatch_), width(width_) { init(); @@ -46,11 +46,11 @@ CSpectrumMismatchRBFKernel::CSpectrumMismatchRBFKernel(int32_t size, register_params(); } -CSpectrumMismatchRBFKernel::CSpectrumMismatchRBFKernel(CStringFeatures* l, - CStringFeatures* r, int32_t size, float64_t* AA_matrix_, +SpectrumMismatchRBFKernel::SpectrumMismatchRBFKernel(std::shared_ptr> l, + std::shared_ptr> r, int32_t size, float64_t* AA_matrix_, int32_t nr, int32_t nc, int32_t degree_, int32_t max_mismatch_, float64_t width_) : - CStringKernel(size), alphabet(NULL), degree(degree_), max_mismatch( + StringKernel(size), alphabet(NULL), degree(degree_), max_mismatch( max_mismatch_), width(width_) { target_letter_0=-1; @@ -60,50 +60,50 @@ CSpectrumMismatchRBFKernel::CSpectrumMismatchRBFKernel(CStringFeatures* l, register_params(); } -CSpectrumMismatchRBFKernel::~CSpectrumMismatchRBFKernel() +SpectrumMismatchRBFKernel::~SpectrumMismatchRBFKernel() { cleanup(); - SG_UNREF(kernel_matrix); + } -bool CSpectrumMismatchRBFKernel::init(CFeatures* l, CFeatures* r) +bool SpectrumMismatchRBFKernel::init(std::shared_ptr l, std::shared_ptr r) { int32_t lhs_changed=(lhs!=l); int32_t rhs_changed=(rhs!=r); - CStringKernel::init(l, r); + StringKernel::init(l, r); SG_DEBUG("lhs_changed: {}", lhs_changed) SG_DEBUG("rhs_changed: {}", rhs_changed) - CStringFeatures* sf_l=(CStringFeatures*)l; - CStringFeatures* sf_r=(CStringFeatures*)r; + auto sf_l=std::static_pointer_cast>(l); + auto sf_r=std::static_pointer_cast>(r); + - SG_UNREF(alphabet); alphabet=sf_l->get_alphabet(); - CAlphabet* ralphabet=sf_r->get_alphabet(); + auto ralphabet=sf_r->get_alphabet(); if (!((alphabet->get_alphabet()==DNA) || (alphabet->get_alphabet()==RNA))) properties&=((uint64_t)(-1))^(KP_LINADD|KP_BATCHEVALUATION); ASSERT(ralphabet->get_alphabet()==alphabet->get_alphabet()) - SG_UNREF(ralphabet); + compute_all(); return init_normalizer(); } -void CSpectrumMismatchRBFKernel::cleanup() +void SpectrumMismatchRBFKernel::cleanup() { - SG_UNREF(alphabet); + alphabet=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CSpectrumMismatchRBFKernel::AA_helper(std::string &path, +float64_t SpectrumMismatchRBFKernel::AA_helper(std::string &path, const char* joint_seq, unsigned int index) { float64_t diff=0.0; @@ -122,7 +122,7 @@ float64_t CSpectrumMismatchRBFKernel::AA_helper(std::string &path, return exp(-diff/width); } -void CSpectrumMismatchRBFKernel::compute_helper_all(const char *joint_seq, +void SpectrumMismatchRBFKernel::compute_helper_all(const char *joint_seq, std::vector &joint_list, std::string path, unsigned int d) { @@ -175,7 +175,7 @@ void CSpectrumMismatchRBFKernel::compute_helper_all(const char *joint_seq, } else { - CDynamicArray feats; + DynamicArray feats; feats.resize_array(kernel_matrix->get_dim1()); feats.set_const(0); @@ -227,7 +227,7 @@ void CSpectrumMismatchRBFKernel::compute_helper_all(const char *joint_seq, } } -void CSpectrumMismatchRBFKernel::compute_all() +void SpectrumMismatchRBFKernel::compute_all() { std::string joint_seq; std::vector joint_list; @@ -239,12 +239,12 @@ void CSpectrumMismatchRBFKernel::compute_all() for (int j=0; jget_num_vectors(); j++) kernel_matrix->set_element(0, i, j); + auto sf_lhs = std::static_pointer_cast>(lhs); for (int i=0; iget_num_vectors(); i++) { int32_t alen; bool free_avec; - char* avec=((CStringFeatures*)lhs)->get_feature_vector(i, alen, - free_avec); + char* avec=sf_lhs->get_feature_vector(i, alen, free_avec); for (int apos=0; apos+degree-1*)lhs)->free_feature_vector(avec, i, free_avec); + sf_lhs->free_feature_vector(avec, i, free_avec); } compute_helper_all(joint_seq.c_str(), joint_list, "", 0); } -float64_t CSpectrumMismatchRBFKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SpectrumMismatchRBFKernel::compute(int32_t idx_a, int32_t idx_b) { return kernel_matrix->element(idx_a, idx_b); } -bool CSpectrumMismatchRBFKernel::set_AA_matrix(float64_t* AA_matrix_, +bool SpectrumMismatchRBFKernel::set_AA_matrix(float64_t* AA_matrix_, int32_t nr, int32_t nc) { if (AA_matrix_) @@ -285,7 +285,7 @@ bool CSpectrumMismatchRBFKernel::set_AA_matrix(float64_t* AA_matrix_, return false; } -bool CSpectrumMismatchRBFKernel::set_max_mismatch(int32_t max) +bool SpectrumMismatchRBFKernel::set_max_mismatch(int32_t max) { max_mismatch=max; @@ -295,31 +295,31 @@ bool CSpectrumMismatchRBFKernel::set_max_mismatch(int32_t max) return true; } -void CSpectrumMismatchRBFKernel::register_params() +void SpectrumMismatchRBFKernel::register_params() { SG_ADD(°ree, "degree", "degree of the kernel", ParameterProperties::HYPER); SG_ADD(&AA_matrix, "AA_matrix", "128*128 scalar product matrix"); SG_ADD(&width, "width", "width of Gaussian", ParameterProperties::HYPER); SG_ADD(&target_letter_0, "target_letter_0", "target letter 0"); SG_ADD(&initialized, "initialized", "the mark of initialization status"); - SG_ADD((CSGObject** )&kernel_matrix, "kernel_matrix", + SG_ADD((std::shared_ptr* )&kernel_matrix, "kernel_matrix", "the kernel matrix with its length " "defined by the number of vectors of the string features"); } -void CSpectrumMismatchRBFKernel::register_alphabet() +void SpectrumMismatchRBFKernel::register_alphabet() { - SG_ADD((CSGObject** )&alphabet, "alphabet", "the alphabet used by kernel"); + SG_ADD((std::shared_ptr* )&alphabet, "alphabet", "the alphabet used by kernel"); } -void CSpectrumMismatchRBFKernel::init() +void SpectrumMismatchRBFKernel::init() { alphabet=NULL; degree=0; max_mismatch=0; width=0.0; - kernel_matrix=new CDynamicArray(); - SG_REF(kernel_matrix); + kernel_matrix=std::make_shared>(); + initialized=false; target_letter_0=0; } diff --git a/src/shogun/kernel/string/SpectrumMismatchRBFKernel.h b/src/shogun/kernel/string/SpectrumMismatchRBFKernel.h index 56e2be7b7ad..85893dbb656 100644 --- a/src/shogun/kernel/string/SpectrumMismatchRBFKernel.h +++ b/src/shogun/kernel/string/SpectrumMismatchRBFKernel.h @@ -35,11 +35,11 @@ struct joint_list_struct #endif /** @brief spectrum mismatch rbf kernel */ -class CSpectrumMismatchRBFKernel: public CStringKernel +class SpectrumMismatchRBFKernel: public StringKernel { public: /** default constructor */ - CSpectrumMismatchRBFKernel(); + SpectrumMismatchRBFKernel(); /** constructor * @@ -51,7 +51,7 @@ class CSpectrumMismatchRBFKernel: public CStringKernel * @param max_mismatch * @param width */ - CSpectrumMismatchRBFKernel(int32_t size, float64_t* AA_matrix_, int32_t nr_, + SpectrumMismatchRBFKernel(int32_t size, float64_t* AA_matrix_, int32_t nr_, int32_t nc_, int32_t degree, int32_t max_mismatch, float64_t width); /** constructor @@ -66,13 +66,13 @@ class CSpectrumMismatchRBFKernel: public CStringKernel * @param max_mismatch * @param width */ - CSpectrumMismatchRBFKernel(CStringFeatures* l, - CStringFeatures* r, int32_t size, float64_t* AA_matrix_, + SpectrumMismatchRBFKernel(std::shared_ptr> l, + std::shared_ptr> r, int32_t size, float64_t* AA_matrix_, int32_t nr_, int32_t nc_, int32_t degree, int32_t max_mismatch, float64_t width); /** destructor */ - virtual ~CSpectrumMismatchRBFKernel(); + virtual ~SpectrumMismatchRBFKernel(); /** initialize kernel * @@ -80,7 +80,7 @@ class CSpectrumMismatchRBFKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -206,7 +206,7 @@ class CSpectrumMismatchRBFKernel: public CStringKernel protected: /** alphabet of features */ - CAlphabet* alphabet; + std::shared_ptr alphabet; /** degree */ int32_t degree; /** maximum mismatch */ @@ -220,7 +220,7 @@ class CSpectrumMismatchRBFKernel: public CStringKernel bool initialized; /** kernel matrix */ - CDynamicArray* kernel_matrix; // 2d + std::shared_ptr> kernel_matrix; // 2d /** kernel matrix length */ int32_t kernel_matrix_length; /** target letter 0 */ diff --git a/src/shogun/kernel/string/SpectrumRBFKernel.cpp b/src/shogun/kernel/string/SpectrumRBFKernel.cpp index b2544caa3d0..aa73f735ab6 100644 --- a/src/shogun/kernel/string/SpectrumRBFKernel.cpp +++ b/src/shogun/kernel/string/SpectrumRBFKernel.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Viktor Gal, Soeren Sonnenburg, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Viktor Gal, Soeren Sonnenburg, * Weijie Lin, Bjoern Esser, Saurabh Goyal */ @@ -26,15 +26,15 @@ using namespace shogun; -CSpectrumRBFKernel::CSpectrumRBFKernel() - : CStringKernel(0) +SpectrumRBFKernel::SpectrumRBFKernel() + : StringKernel(0) { init(); register_param(); } -CSpectrumRBFKernel::CSpectrumRBFKernel (int32_t size, float64_t *AA_matrix_, int32_t degree_, float64_t width_) - : CStringKernel(size), alphabet(NULL), degree(degree_), width(width_), sequences(NULL), string_features(NULL), nof_sequences(0), max_sequence_length(0) +SpectrumRBFKernel::SpectrumRBFKernel (int32_t size, float64_t *AA_matrix_, int32_t degree_, float64_t width_) + : StringKernel(size), alphabet(NULL), degree(degree_), width(width_), sequences(NULL), string_features(NULL), nof_sequences(0), max_sequence_length(0) { init(); register_param(); @@ -51,14 +51,14 @@ CSpectrumRBFKernel::CSpectrumRBFKernel (int32_t size, float64_t *AA_matrix_, int std::copy_n(sequences, nof_sequences, std::back_inserter(string_list)); //string_features = new CStringFeatures(sequences, nof_sequences, max_sequence_length, PROTEIN); - string_features = new CStringFeatures(string_list, IUPAC_AMINO_ACID); - SG_REF(string_features) + string_features = std::make_shared>(string_list, IUPAC_AMINO_ACID); + init(string_features, string_features); } -CSpectrumRBFKernel::CSpectrumRBFKernel( - CStringFeatures* l, CStringFeatures* r, int32_t size, float64_t* AA_matrix_, int32_t degree_, float64_t width_) -: CStringKernel(size), alphabet(NULL), degree(degree_), width(width_), sequences(NULL), string_features(NULL), nof_sequences(0), max_sequence_length(0) +SpectrumRBFKernel::SpectrumRBFKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t size, float64_t* AA_matrix_, int32_t degree_, float64_t width_) +: StringKernel(size), alphabet(NULL), degree(degree_), width(width_), sequences(NULL), string_features(NULL), nof_sequences(0), max_sequence_length(0) { target_letter_0=-1 ; @@ -69,14 +69,14 @@ CSpectrumRBFKernel::CSpectrumRBFKernel( register_param(); } -CSpectrumRBFKernel::~CSpectrumRBFKernel() +SpectrumRBFKernel::~SpectrumRBFKernel() { cleanup(); - SG_UNREF(string_features); + SG_FREE(sequences); } -void CSpectrumRBFKernel::read_profiles_and_sequences() +void SpectrumRBFKernel::read_profiles_and_sequences() { int32_t aa_to_index[128];//profile @@ -264,7 +264,7 @@ void CSpectrumRBFKernel::read_profiles_and_sequences() } -bool CSpectrumRBFKernel::init(CFeatures* l, CFeatures* r) +bool SpectrumRBFKernel::init(std::shared_ptr l, std::shared_ptr r) { // >> profile /* @@ -277,35 +277,35 @@ bool CSpectrumRBFKernel::init(CFeatures* l, CFeatures* r) int32_t lhs_changed=(lhs!=l); int32_t rhs_changed=(rhs!=r); - CStringKernel::init(l,r); + StringKernel::init(l,r); SG_DEBUG("lhs_changed: {}", lhs_changed) SG_DEBUG("rhs_changed: {}", rhs_changed) - CStringFeatures* sf_l=(CStringFeatures*) l; - CStringFeatures* sf_r=(CStringFeatures*) r; + auto sf_l=std::static_pointer_cast>(l); + auto sf_r=std::static_pointer_cast>(r); + - SG_UNREF(alphabet); alphabet=sf_l->get_alphabet(); - CAlphabet* ralphabet=sf_r->get_alphabet(); + auto ralphabet=sf_r->get_alphabet(); if (!((alphabet->get_alphabet()==DNA) || (alphabet->get_alphabet()==RNA))) properties &= ((uint64_t) (-1)) ^ (KP_LINADD | KP_BATCHEVALUATION); ASSERT(ralphabet->get_alphabet()==alphabet->get_alphabet()) - SG_UNREF(ralphabet); + return init_normalizer(); } -void CSpectrumRBFKernel::cleanup() +void SpectrumRBFKernel::cleanup() { - SG_UNREF(alphabet); + alphabet=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } inline bool isaa(char c) @@ -315,7 +315,7 @@ inline bool isaa(char c) return true ; } -float64_t CSpectrumRBFKernel::AA_helper(const char* path, const int seq_degree, const char* joint_seq, unsigned int index) +float64_t SpectrumRBFKernel::AA_helper(const char* path, const int seq_degree, const char* joint_seq, unsigned int index) { //const char* AA = "ARNDCQEGHILKMFPSTWYV"; float64_t diff=0.0 ; @@ -329,7 +329,7 @@ float64_t CSpectrumRBFKernel::AA_helper(const char* path, const int seq_degree, diff += AA_matrix.matrix[ (path[i]-1)*128 + path[i] - 1] ; diff -= 2*AA_matrix.matrix[ (path[i]-1)*128 + joint_seq[index+i] - 1] ; diff += AA_matrix.matrix[ (joint_seq[index+i]-1)*128 + joint_seq[index+i] - 1] ; - if (CMath::is_nan(diff)) + if (Math::is_nan(diff)) fprintf(stderr, "nan occurred: '%c' '%c'\n", path[i], joint_seq[index+i]) ; } } @@ -337,13 +337,13 @@ float64_t CSpectrumRBFKernel::AA_helper(const char* path, const int seq_degree, return exp( - diff/width) ; } -float64_t CSpectrumRBFKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SpectrumRBFKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool afree, bfree; - char* avec = ((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, afree); - char* bvec = ((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, bfree); + char* avec = std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, afree); + char* bvec = std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, bfree); float64_t result=0; for (int32_t i=0; i*) lhs)->free_feature_vector(avec, idx_a, afree); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, bfree); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, afree); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, bfree); return result; } -bool CSpectrumRBFKernel::set_AA_matrix( +bool SpectrumRBFKernel::set_AA_matrix( float64_t* AA_matrix_) { @@ -374,26 +374,26 @@ bool CSpectrumRBFKernel::set_AA_matrix( return false; } -void CSpectrumRBFKernel::register_param() +void SpectrumRBFKernel::register_param() { SG_ADD(°ree, "degree", "degree of the kernel", ParameterProperties::HYPER); SG_ADD(&AA_matrix, "AA_matrix", "128*128 scalar product matrix"); SG_ADD(&width, "width", "width of Gaussian", ParameterProperties::HYPER); SG_ADD(&nof_sequences, "nof_sequences", "length of the sequence"); - m_parameters->add_vector(&sequences, &nof_sequences, "sequences", "the sequences as a part of profile"); + /*m_parameters->add_vector(&sequences, &nof_sequences, "sequences", "the sequences as a part of profile");*/ watch_param("sequences", &sequences, &nof_sequences); SG_ADD(&max_sequence_length, "max_sequence_length", "max length of the sequence"); } -void CSpectrumRBFKernel::register_alphabet() +void SpectrumRBFKernel::register_alphabet() { - SG_ADD((CSGObject**)&alphabet, "alphabet", "the alphabet used by kernel"); + SG_ADD((std::shared_ptr*)&alphabet, "alphabet", "the alphabet used by kernel"); } -void CSpectrumRBFKernel::init() +void SpectrumRBFKernel::init() { alphabet = NULL; degree = 0; diff --git a/src/shogun/kernel/string/SpectrumRBFKernel.h b/src/shogun/kernel/string/SpectrumRBFKernel.h index e4cd1827fb5..23429f80daf 100644 --- a/src/shogun/kernel/string/SpectrumRBFKernel.h +++ b/src/shogun/kernel/string/SpectrumRBFKernel.h @@ -24,11 +24,11 @@ namespace shogun { /** @brief spectrum rbf kernel */ -class CSpectrumRBFKernel: public CStringKernel +class SpectrumRBFKernel: public StringKernel { public: /** default constructor */ - CSpectrumRBFKernel(); + SpectrumRBFKernel(); /** constructor * @param size @@ -36,7 +36,7 @@ class CSpectrumRBFKernel: public CStringKernel * @param degree * @param width */ - CSpectrumRBFKernel(int32_t size, float64_t* AA_matrix, int32_t degree, float64_t width); + SpectrumRBFKernel(int32_t size, float64_t* AA_matrix, int32_t degree, float64_t width); /** constructor * @@ -47,11 +47,11 @@ class CSpectrumRBFKernel: public CStringKernel * @param degree * @param width */ - CSpectrumRBFKernel( - CStringFeatures* l, CStringFeatures* r, int32_t size, float64_t* AA_matrix, int32_t degree, float64_t width); + SpectrumRBFKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t size, float64_t* AA_matrix, int32_t degree, float64_t width); /** destructor */ - virtual ~CSpectrumRBFKernel(); + virtual ~SpectrumRBFKernel(); /** initialize kernel * @@ -59,7 +59,7 @@ class CSpectrumRBFKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -134,7 +134,7 @@ class CSpectrumRBFKernel: public CStringKernel protected: /** alphabet of features */ - CAlphabet* alphabet; + std::shared_ptr alphabet; /** degree */ int32_t degree; /** maximum mismatch */ @@ -154,7 +154,7 @@ class CSpectrumRBFKernel: public CStringKernel /** sequences */ SGVector* sequences; // profile /** string features */ - CStringFeatures* string_features; + std::shared_ptr> string_features; /** nof sequences */ int32_t nof_sequences; /** max sequence length */ @@ -163,7 +163,7 @@ class CSpectrumRBFKernel: public CStringKernel /** if kernel is initialized */ bool initialized; /** kernel matrix */ - CDynamicArray kernel_matrix; // 2d + DynamicArray kernel_matrix; // 2d /** target letter 0 */ int32_t target_letter_0; diff --git a/src/shogun/kernel/string/StringKernel.h b/src/shogun/kernel/string/StringKernel.h index a1f2430950a..120c20f31b3 100644 --- a/src/shogun/kernel/string/StringKernel.h +++ b/src/shogun/kernel/string/StringKernel.h @@ -19,21 +19,21 @@ namespace shogun * For a (very complex) example see e.g. CWeightedDegreeStringKernel * */ -template class CStringKernel : public CKernel +template class StringKernel : public Kernel { public: /** constructor * * @param cachesize cache size */ - CStringKernel(int32_t cachesize=0) : CKernel(cachesize) {} + StringKernel(int32_t cachesize=0) : Kernel(cachesize) {} /** constructor * * @param l features of left-hand side * @param r features of right-hand side */ - CStringKernel(CFeatures *l, CFeatures *r) : CKernel(10) + StringKernel(std::shared_ptr l, std::shared_ptr r) : Kernel(10) { init(l, r); } @@ -48,9 +48,9 @@ template class CStringKernel : public CKernel * @param r features for right-hand side * @return if init was successful */ - virtual bool init(CFeatures* l, CFeatures* r) + virtual bool init(std::shared_ptr l, std::shared_ptr r) { - CKernel::init(l,r); + Kernel::init(l,r); ASSERT(l->get_feature_class()==C_STRING) ASSERT(r->get_feature_class()==C_STRING) @@ -90,19 +90,19 @@ template class CStringKernel : public CKernel virtual EKernelType get_kernel_type()=0; }; -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_DREAL; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_DREAL; } -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_ULONG; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_ULONG; } -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_INT; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_INT; } -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_WORD; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_WORD; } -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_SHORT; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_SHORT; } -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_BYTE; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_BYTE; } -template<> inline EFeatureType CStringKernel::get_feature_type() { return F_CHAR; } +template<> inline EFeatureType StringKernel::get_feature_type() { return F_CHAR; } } #endif /* _STRINGKERNEL_H__ */ diff --git a/src/shogun/kernel/string/SubsequenceStringKernel.cpp b/src/shogun/kernel/string/SubsequenceStringKernel.cpp index c3a09dabfeb..db548b70260 100644 --- a/src/shogun/kernel/string/SubsequenceStringKernel.cpp +++ b/src/shogun/kernel/string/SubsequenceStringKernel.cpp @@ -10,47 +10,47 @@ using namespace shogun; -CSubsequenceStringKernel::CSubsequenceStringKernel() -: CStringKernel(0), m_maxlen(1), m_lambda(1.0) +SubsequenceStringKernel::SubsequenceStringKernel() +: StringKernel(0), m_maxlen(1), m_lambda(1.0) { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); register_params(); } -CSubsequenceStringKernel::CSubsequenceStringKernel(int32_t size, int32_t maxlen, +SubsequenceStringKernel::SubsequenceStringKernel(int32_t size, int32_t maxlen, float64_t lambda) -: CStringKernel(size), m_maxlen(maxlen), m_lambda(lambda) +: StringKernel(size), m_maxlen(maxlen), m_lambda(lambda) { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); register_params(); } -CSubsequenceStringKernel::CSubsequenceStringKernel(CStringFeatures* l, - CStringFeatures* r, int32_t maxlen, float64_t lambda) -: CStringKernel(10), m_maxlen(maxlen), m_lambda(lambda) +SubsequenceStringKernel::SubsequenceStringKernel(std::shared_ptr> l, + std::shared_ptr> r, int32_t maxlen, float64_t lambda) +: StringKernel(10), m_maxlen(maxlen), m_lambda(lambda) { - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); init(l, r); register_params(); } -CSubsequenceStringKernel::~CSubsequenceStringKernel() +SubsequenceStringKernel::~SubsequenceStringKernel() { cleanup(); } -bool CSubsequenceStringKernel::init(CFeatures* l, CFeatures* r) +bool SubsequenceStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - CStringKernel::init(l, r); + StringKernel::init(l, r); return init_normalizer(); } -void CSubsequenceStringKernel::cleanup() +void SubsequenceStringKernel::cleanup() { - CKernel::cleanup(); + Kernel::cleanup(); } -float64_t CSubsequenceStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t SubsequenceStringKernel::compute(int32_t idx_a, int32_t idx_b) { // sanity check require(lhs, "lhs feature vector is not set!"); @@ -59,9 +59,9 @@ float64_t CSubsequenceStringKernel::compute(int32_t idx_a, int32_t idx_b) int32_t alen, blen; bool free_avec, free_bvec; - char* avec=dynamic_cast*>(lhs) + char* avec=std::dynamic_pointer_cast>(lhs) ->get_feature_vector(idx_a, alen, free_avec); - char* bvec=dynamic_cast*>(rhs) + char* bvec=std::dynamic_pointer_cast>(rhs) ->get_feature_vector(idx_b, blen, free_bvec); require(avec, "Feature vector for lhs is NULL!"); @@ -113,9 +113,9 @@ float64_t CSubsequenceStringKernel::compute(int32_t idx_a, int32_t idx_b) } // cleanup - dynamic_cast*>(lhs)->free_feature_vector(avec, idx_a, + std::dynamic_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); - dynamic_cast*>(rhs)->free_feature_vector(bvec, idx_b, + std::dynamic_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); for (index_t i=0; i +class SubsequenceStringKernel: public StringKernel { public: /** default constructor */ - CSubsequenceStringKernel(); + SubsequenceStringKernel(); /** * constructor @@ -62,7 +62,7 @@ class CSubsequenceStringKernel: public CStringKernel * @param maxlen maximum length of the subsequence * @param lambda the penalty parameter */ - CSubsequenceStringKernel(int32_t size, int32_t maxlen, float64_t lambda); + SubsequenceStringKernel(int32_t size, int32_t maxlen, float64_t lambda); /** * constructor @@ -72,11 +72,11 @@ class CSubsequenceStringKernel: public CStringKernel * @param maxlen maximum length of the subsequence * @param lambda the penalty parameter */ - CSubsequenceStringKernel(CStringFeatures* lhs, CStringFeatures* rhs, + SubsequenceStringKernel(std::shared_ptr> l, std::shared_ptr> r, int32_t maxlen, float64_t lambda); /** destructor */ - virtual ~CSubsequenceStringKernel(); + virtual ~SubsequenceStringKernel(); /** * initialize kernel @@ -85,7 +85,7 @@ class CSubsequenceStringKernel: public CStringKernel * @param rhs features of right-hand side * @return true if initialization was successful, false otherwise */ - virtual bool init(CFeatures* lhs, CFeatures* rhs); + virtual bool init(std::shared_ptr lhs, std::shared_ptr rhs); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/WeightedCommWordStringKernel.cpp b/src/shogun/kernel/string/WeightedCommWordStringKernel.cpp index 74d189a71b5..8fc685c7455 100644 --- a/src/shogun/kernel/string/WeightedCommWordStringKernel.cpp +++ b/src/shogun/kernel/string/WeightedCommWordStringKernel.cpp @@ -11,24 +11,24 @@ using namespace shogun; -CWeightedCommWordStringKernel::CWeightedCommWordStringKernel() - : CCommWordStringKernel(0, false) +WeightedCommWordStringKernel::WeightedCommWordStringKernel() + : CommWordStringKernel(0, false) { init(); } -CWeightedCommWordStringKernel::CWeightedCommWordStringKernel( +WeightedCommWordStringKernel::WeightedCommWordStringKernel( int32_t size, bool us) -: CCommWordStringKernel(size, us) +: CommWordStringKernel(size, us) { ASSERT(us==false) init(); } -CWeightedCommWordStringKernel::CWeightedCommWordStringKernel( - CStringFeatures* l, CStringFeatures* r, bool us, +WeightedCommWordStringKernel::WeightedCommWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, bool us, int32_t size) -: CCommWordStringKernel(size, us) +: CommWordStringKernel(size, us) { ASSERT(us==false) init(); @@ -36,31 +36,32 @@ CWeightedCommWordStringKernel::CWeightedCommWordStringKernel( init(l,r); } -CWeightedCommWordStringKernel::~CWeightedCommWordStringKernel() +WeightedCommWordStringKernel::~WeightedCommWordStringKernel() { SG_FREE(weights); } -bool CWeightedCommWordStringKernel::init(CFeatures* l, CFeatures* r) +bool WeightedCommWordStringKernel::init(std::shared_ptr l, std::shared_ptr r) { - ASSERT(((CStringFeatures*) l)->get_order() == - ((CStringFeatures*) r)->get_order()); - degree=((CStringFeatures*) l)->get_order(); + auto sf_l = std::static_pointer_cast>(l); + auto sf_r = std::static_pointer_cast>(r); + ASSERT(sf_l->get_order() == sf_r->get_order()); + degree=sf_l->get_order(); set_wd_weights(); - CCommWordStringKernel::init(l,r); + CommWordStringKernel::init(l,r); return init_normalizer(); } -void CWeightedCommWordStringKernel::cleanup() +void WeightedCommWordStringKernel::cleanup() { SG_FREE(weights); weights=NULL; - CCommWordStringKernel::cleanup(); + CommWordStringKernel::cleanup(); } -bool CWeightedCommWordStringKernel::set_wd_weights() +bool WeightedCommWordStringKernel::set_wd_weights() { SG_FREE(weights); weights=SG_MALLOC(float64_t, degree); @@ -78,7 +79,7 @@ bool CWeightedCommWordStringKernel::set_wd_weights() return weights!=NULL; } -bool CWeightedCommWordStringKernel::set_weights(SGVector w) +bool WeightedCommWordStringKernel::set_weights(SGVector w) { ASSERT(w.vlen==degree) @@ -89,14 +90,14 @@ bool CWeightedCommWordStringKernel::set_weights(SGVector w) return true; } -float64_t CWeightedCommWordStringKernel::compute_helper( +float64_t WeightedCommWordStringKernel::compute_helper( int32_t idx_a, int32_t idx_b, bool do_sort) { int32_t alen, blen; bool free_avec, free_bvec; - CStringFeatures* l = (CStringFeatures*) lhs; - CStringFeatures* r = (CStringFeatures*) rhs; + auto l = std::static_pointer_cast>(lhs); + auto r = std::static_pointer_cast>(rhs); uint16_t* av=l->get_feature_vector(idx_a, alen, free_avec); uint16_t* bv=r->get_feature_vector(idx_b, blen, free_bvec); @@ -110,7 +111,7 @@ float64_t CWeightedCommWordStringKernel::compute_helper( { avec=SG_MALLOC(uint16_t, alen); sg_memcpy(avec, av, sizeof(uint16_t)*alen); - CMath::radix_sort(avec, alen); + Math::radix_sort(avec, alen); } else avec=NULL; @@ -119,7 +120,7 @@ float64_t CWeightedCommWordStringKernel::compute_helper( { bvec=SG_MALLOC(uint16_t, blen); sg_memcpy(bvec, bv, sizeof(uint16_t)*blen); - CMath::radix_sort(bvec, blen); + Math::radix_sort(bvec, blen); } else bvec=NULL; @@ -131,7 +132,7 @@ float64_t CWeightedCommWordStringKernel::compute_helper( for (int32_t d=0; d*) lhs)->get_masked_symbols(0xffff, mask); + uint16_t masked=std::static_pointer_cast>(lhs)->get_masked_symbols(0xffff, mask); int32_t left_idx=0; int32_t right_idx=0; @@ -174,12 +175,12 @@ float64_t CWeightedCommWordStringKernel::compute_helper( return result; } -void CWeightedCommWordStringKernel::add_to_normal( +void WeightedCommWordStringKernel::add_to_normal( int32_t vec_idx, float64_t weight) { int32_t len=-1; bool free_vec; - CStringFeatures* s=(CStringFeatures*) lhs; + auto s=std::static_pointer_cast>(lhs); uint16_t* vec=s->get_feature_vector(vec_idx, len, free_vec); if (len>0) @@ -204,12 +205,12 @@ void CWeightedCommWordStringKernel::add_to_normal( s->free_feature_vector(vec, vec_idx, free_vec); } -void CWeightedCommWordStringKernel::merge_normal() +void WeightedCommWordStringKernel::merge_normal() { ASSERT(get_is_initialized()) ASSERT(use_sign==false) - CStringFeatures* s=(CStringFeatures*) rhs; + auto s=std::static_pointer_cast>(rhs); uint32_t num_symbols=(uint32_t) s->get_num_symbols(); int32_t dic_size=1<<(sizeof(uint16_t)*8); float64_t* dic=SG_MALLOC(float64_t, dic_size); @@ -236,7 +237,7 @@ void CWeightedCommWordStringKernel::merge_normal() SG_FREE(dic); } -float64_t CWeightedCommWordStringKernel::compute_optimized(int32_t i) +float64_t WeightedCommWordStringKernel::compute_optimized(int32_t i) { if (!get_is_initialized()) error("CCommWordStringKernel optimization not initialized"); @@ -246,7 +247,7 @@ float64_t CWeightedCommWordStringKernel::compute_optimized(int32_t i) float64_t result=0; bool free_vec; int32_t len=-1; - CStringFeatures* s=(CStringFeatures*) rhs; + auto s=std::static_pointer_cast>(rhs); uint16_t* vec=s->get_feature_vector(i, len, free_vec); if (vec && len>0) @@ -271,19 +272,19 @@ float64_t CWeightedCommWordStringKernel::compute_optimized(int32_t i) return result; } -float64_t* CWeightedCommWordStringKernel::compute_scoring( +float64_t* WeightedCommWordStringKernel::compute_scoring( int32_t max_degree, int32_t& num_feat, int32_t& num_sym, float64_t* target, int32_t num_suppvec, int32_t* IDX, float64_t* alphas, bool do_init) { if (do_init) - CCommWordStringKernel::init_optimization(num_suppvec, IDX, alphas); + CommWordStringKernel::init_optimization(num_suppvec, IDX, alphas); int32_t dic_size=1<<(sizeof(uint16_t)*9); float64_t* dic=SG_MALLOC(float64_t, dic_size); sg_memcpy(dic, dictionary_weights, sizeof(float64_t)*dic_size); merge_normal(); - float64_t* result=CCommWordStringKernel::compute_scoring(max_degree, num_feat, + float64_t* result=CommWordStringKernel::compute_scoring(max_degree, num_feat, num_sym, target, num_suppvec, IDX, alphas, false); init_dictionary(1<<(sizeof(uint16_t)*9)); @@ -293,14 +294,14 @@ float64_t* CWeightedCommWordStringKernel::compute_scoring( return result; } -void CWeightedCommWordStringKernel::init() +void WeightedCommWordStringKernel::init() { degree=0; weights=NULL; init_dictionary(1<<(sizeof(uint16_t)*9)); - m_parameters->add_vector(&weights, °ree, "weights", - "weights for each of the subkernels of degree 1...d"); + /*m_parameters->add_vector(&weights, °ree, "weights", + "weights for each of the subkernels of degree 1...d");*/ watch_param("weights", &weights, °ree); } diff --git a/src/shogun/kernel/string/WeightedCommWordStringKernel.h b/src/shogun/kernel/string/WeightedCommWordStringKernel.h index c3b45979bee..e51be581561 100644 --- a/src/shogun/kernel/string/WeightedCommWordStringKernel.h +++ b/src/shogun/kernel/string/WeightedCommWordStringKernel.h @@ -15,7 +15,7 @@ namespace shogun { -class CCommWordStringKernel; +class CommWordStringKernel; /** @brief The WeightedCommWordString kernel may be used to compute the weighted * spectrum kernel (i.e. a spectrum kernel for 1 to K-mers, where each k-mer @@ -44,18 +44,18 @@ class CCommWordStringKernel; * direct maps. * */ -class CWeightedCommWordStringKernel: public CCommWordStringKernel +class WeightedCommWordStringKernel: public CommWordStringKernel { public: /** default constructor */ - CWeightedCommWordStringKernel(); + WeightedCommWordStringKernel(); /** constructor * * @param size cache size * @param use_sign if sign shall be used */ - CWeightedCommWordStringKernel(int32_t size, bool use_sign); + WeightedCommWordStringKernel(int32_t size, bool use_sign); /** constructor * @@ -64,11 +64,11 @@ class CWeightedCommWordStringKernel: public CCommWordStringKernel * @param use_sign if sign shall be used * @param size cache size */ - CWeightedCommWordStringKernel( - CStringFeatures* l, CStringFeatures* r, + WeightedCommWordStringKernel( + std::shared_ptr> l, std::shared_ptr> r, bool use_sign=false, int32_t size=10); - virtual ~CWeightedCommWordStringKernel(); + virtual ~WeightedCommWordStringKernel(); /** initialize kernel * @@ -76,7 +76,7 @@ class CWeightedCommWordStringKernel: public CCommWordStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); diff --git a/src/shogun/kernel/string/WeightedDegreePositionStringKernel.cpp b/src/shogun/kernel/string/WeightedDegreePositionStringKernel.cpp index b92b0d125f6..bfc5c891e68 100644 --- a/src/shogun/kernel/string/WeightedDegreePositionStringKernel.cpp +++ b/src/shogun/kernel/string/WeightedDegreePositionStringKernel.cpp @@ -34,7 +34,7 @@ template struct S_THREAD_PARAM_WDS int32_t* vec; float64_t* result; float64_t* weights; - CWeightedDegreePositionStringKernel* kernel; + WeightedDegreePositionStringKernel* kernel; CTrie* tries; float64_t factor; int32_t j; @@ -47,16 +47,16 @@ template struct S_THREAD_PARAM_WDS }; #endif // DOXYGEN_SHOULD_SKIP_THIS -CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( +WeightedDegreePositionStringKernel::WeightedDegreePositionStringKernel( void) -: CStringKernel() +: StringKernel() { init(); } -CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( +WeightedDegreePositionStringKernel::WeightedDegreePositionStringKernel( int32_t size, int32_t d, int32_t mm, int32_t mkls) -: CStringKernel(size) +: StringKernel(size) { init(); @@ -71,10 +71,10 @@ CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( ASSERT(weights) } -CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( +WeightedDegreePositionStringKernel::WeightedDegreePositionStringKernel( int32_t size, SGVector w, int32_t d, int32_t mm, SGVector s, int32_t mkls) -: CStringKernel(size) +: StringKernel(size) { init(); @@ -95,9 +95,9 @@ CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( set_shifts(s); } -CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t d) -: CStringKernel() +WeightedDegreePositionStringKernel::WeightedDegreePositionStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d) +: StringKernel() { init(); @@ -114,7 +114,7 @@ CWeightedDegreePositionStringKernel::CWeightedDegreePositionStringKernel( } -CWeightedDegreePositionStringKernel::~CWeightedDegreePositionStringKernel() +WeightedDegreePositionStringKernel::~WeightedDegreePositionStringKernel() { cleanup(); cleanup_POIM2(); @@ -140,7 +140,7 @@ CWeightedDegreePositionStringKernel::~CWeightedDegreePositionStringKernel() weights_buffer=NULL; } -void CWeightedDegreePositionStringKernel::remove_lhs() +void WeightedDegreePositionStringKernel::remove_lhs() { SG_DEBUG("deleting CWeightedDegreePositionStringKernel optimization") delete_optimization(); @@ -148,13 +148,13 @@ void CWeightedDegreePositionStringKernel::remove_lhs() tries.destroy(); poim_tries.destroy(); - CKernel::remove_lhs(); + Kernel::remove_lhs(); } -void CWeightedDegreePositionStringKernel::create_empty_tries() +void WeightedDegreePositionStringKernel::create_empty_tries() { ASSERT(lhs) - seq_length = ((CStringFeatures*) lhs)->get_max_vector_length(); + seq_length = std::static_pointer_cast>(lhs)->get_max_vector_length(); if (opt_type==SLOWBUTMEMEFFICIENT) { @@ -170,18 +170,18 @@ void CWeightedDegreePositionStringKernel::create_empty_tries() error("unknown optimization type"); } -bool CWeightedDegreePositionStringKernel::init(CFeatures* l, CFeatures* r) +bool WeightedDegreePositionStringKernel::init(std::shared_ptr l, std::shared_ptr r) { int32_t lhs_changed = (lhs!=l) ; int32_t rhs_changed = (rhs!=r) ; - CStringKernel::init(l,r); + StringKernel::init(l,r); SG_DEBUG("lhs_changed: {}", lhs_changed) SG_DEBUG("rhs_changed: {}", rhs_changed) - CStringFeatures* sf_l=(CStringFeatures*) l; - CStringFeatures* sf_r=(CStringFeatures*) r; + auto sf_l=std::static_pointer_cast>(l); + auto sf_r=std::static_pointer_cast>(r); /* set shift */ if (shift_len==0) { @@ -203,15 +203,15 @@ bool CWeightedDegreePositionStringKernel::init(CFeatures* l, CFeatures* r) if (rhs_changed && !sf_r->have_same_length(len)) error("All strings in WD kernel must have same length (rhs wrong)!"); - SG_UNREF(alphabet); + alphabet= sf_l->get_alphabet(); - CAlphabet* ralphabet=sf_r->get_alphabet(); + auto ralphabet=sf_r->get_alphabet(); if (!((alphabet->get_alphabet()==DNA) || (alphabet->get_alphabet()==RNA))) properties &= ((uint64_t) (-1)) ^ (KP_LINADD | KP_BATCHEVALUATION); ASSERT(ralphabet->get_alphabet()==alphabet->get_alphabet()) - SG_UNREF(ralphabet); + //whenever init is called also init tries and block weights create_empty_tries(); @@ -220,7 +220,7 @@ bool CWeightedDegreePositionStringKernel::init(CFeatures* l, CFeatures* r) return init_normalizer(); } -void CWeightedDegreePositionStringKernel::cleanup() +void WeightedDegreePositionStringKernel::cleanup() { SG_DEBUG("deleting CWeightedDegreePositionStringKernel optimization") delete_optimization(); @@ -233,13 +233,13 @@ void CWeightedDegreePositionStringKernel::cleanup() seq_length = 0; tree_initialized = false; - SG_UNREF(alphabet); + alphabet=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } -bool CWeightedDegreePositionStringKernel::init_optimization( +bool WeightedDegreePositionStringKernel::init_optimization( int32_t p_count, int32_t * IDX, float64_t * alphas, int32_t tree_num, int32_t upto_tree) { @@ -280,7 +280,7 @@ bool CWeightedDegreePositionStringKernel::init_optimization( return true ; } -bool CWeightedDegreePositionStringKernel::delete_optimization() +bool WeightedDegreePositionStringKernel::delete_optimization() { if ((opt_type==FASTBUTMEMHUNGRY) && (tries.get_use_compact_terminal_nodes())) { @@ -305,7 +305,7 @@ bool CWeightedDegreePositionStringKernel::delete_optimization() return false; } -float64_t CWeightedDegreePositionStringKernel::compute_with_mismatch( +float64_t WeightedDegreePositionStringKernel::compute_with_mismatch( char* avec, int32_t alen, char* bvec, int32_t blen) { float64_t* max_shift_vec= SG_MALLOC(float64_t, max_shift); @@ -385,7 +385,7 @@ float64_t CWeightedDegreePositionStringKernel::compute_with_mismatch( return result ; } -float64_t CWeightedDegreePositionStringKernel::compute_without_mismatch( +float64_t WeightedDegreePositionStringKernel::compute_without_mismatch( char* avec, int32_t alen, char* bvec, int32_t blen) { float64_t* max_shift_vec = SG_MALLOC(float64_t, max_shift); @@ -451,7 +451,7 @@ float64_t CWeightedDegreePositionStringKernel::compute_without_mismatch( return result ; } -float64_t CWeightedDegreePositionStringKernel::compute_without_mismatch_matrix( +float64_t WeightedDegreePositionStringKernel::compute_without_mismatch_matrix( char* avec, int32_t alen, char* bvec, int32_t blen) { float64_t* max_shift_vec = SG_MALLOC(float64_t, max_shift); @@ -515,7 +515,7 @@ float64_t CWeightedDegreePositionStringKernel::compute_without_mismatch_matrix( return result ; } -float64_t CWeightedDegreePositionStringKernel::compute_without_mismatch_position_weights( +float64_t WeightedDegreePositionStringKernel::compute_without_mismatch_position_weights( char* avec, float64_t* pos_weights_lhs, int32_t alen, char* bvec, float64_t* pos_weights_rhs, int32_t blen) { @@ -595,14 +595,14 @@ float64_t CWeightedDegreePositionStringKernel::compute_without_mismatch_position } -float64_t CWeightedDegreePositionStringKernel::compute( +float64_t WeightedDegreePositionStringKernel::compute( int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec=std::static_pointer_cast>(lhs)->get_feature_vector(idx_a, alen, free_avec); + char* bvec=std::static_pointer_cast>(rhs)->get_feature_vector(idx_b, blen, free_bvec); // can only deal with strings of same length ASSERT(alen==blen) ASSERT(shift_len==alen) @@ -624,14 +624,14 @@ float64_t CWeightedDegreePositionStringKernel::compute( else result = compute_without_mismatch_matrix(avec, alen, bvec, blen) ; - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + std::static_pointer_cast>(lhs)->free_feature_vector(avec, idx_a, free_avec); + std::static_pointer_cast>(rhs)->free_feature_vector(bvec, idx_b, free_bvec); return result ; } -void CWeightedDegreePositionStringKernel::add_example_to_tree( +void WeightedDegreePositionStringKernel::add_example_to_tree( int32_t idx, float64_t alpha) { ASSERT(position_weights_lhs==NULL) @@ -641,13 +641,13 @@ void CWeightedDegreePositionStringKernel::add_example_to_tree( int32_t len=0; bool free_vec; - char* char_vec=((CStringFeatures*) lhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=std::static_pointer_cast>(lhs)->get_feature_vector(idx, len, free_vec); ASSERT(max_mismatch==0) int32_t *vec = SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + std::static_pointer_cast>(lhs)->free_feature_vector(char_vec, idx, free_vec); if (opt_type==FASTBUTMEMHUNGRY) { @@ -682,7 +682,7 @@ void CWeightedDegreePositionStringKernel::add_example_to_tree( tree_initialized=true ; } -void CWeightedDegreePositionStringKernel::add_example_to_single_tree( +void WeightedDegreePositionStringKernel::add_example_to_single_tree( int32_t idx, float64_t alpha, int32_t tree_num) { ASSERT(position_weights_lhs==NULL) @@ -692,7 +692,7 @@ void CWeightedDegreePositionStringKernel::add_example_to_single_tree( int32_t len=0; bool free_vec; - char* char_vec=((CStringFeatures*) lhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=std::static_pointer_cast>(lhs)->get_feature_vector(idx, len, free_vec); ASSERT(max_mismatch==0) int32_t *vec=SG_MALLOC(int32_t, len); int32_t max_s=-1; @@ -707,12 +707,12 @@ void CWeightedDegreePositionStringKernel::add_example_to_single_tree( else { error("unknown optimization type"); } - for (int32_t i=CMath::max(0,tree_num-max_shift); - iremap_to_bin(char_vec[i]); } - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + std::static_pointer_cast>(lhs)->free_feature_vector(char_vec, idx, free_vec); for (int32_t s=max_s; s>=0; s--) { @@ -722,7 +722,7 @@ void CWeightedDegreePositionStringKernel::add_example_to_single_tree( if (opt_type==FASTBUTMEMHUNGRY) { - for (int32_t i=CMath::max(0,tree_num-max_shift); i=1) && (s<=shift[i])) @@ -736,7 +736,7 @@ void CWeightedDegreePositionStringKernel::add_example_to_single_tree( tree_initialized=true ; } -float64_t CWeightedDegreePositionStringKernel::compute_by_tree(int32_t idx) +float64_t WeightedDegreePositionStringKernel::compute_by_tree(int32_t idx) { ASSERT(position_weights_lhs==NULL) ASSERT(position_weights_rhs==NULL) @@ -746,14 +746,14 @@ float64_t CWeightedDegreePositionStringKernel::compute_by_tree(int32_t idx) float64_t sum=0; int32_t len=0; bool free_vec; - char* char_vec=((CStringFeatures*) rhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=std::static_pointer_cast>(rhs)->get_feature_vector(idx, len, free_vec); ASSERT(max_mismatch==0) int32_t *vec=SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) rhs)->free_feature_vector(char_vec, idx, free_vec); + std::static_pointer_cast>(rhs)->free_feature_vector(char_vec, idx, free_vec); for (int32_t i=0; inormalize_rhs(sum, idx); } -void CWeightedDegreePositionStringKernel::compute_by_tree( +void WeightedDegreePositionStringKernel::compute_by_tree( int32_t idx, float64_t* LevelContrib) { ASSERT(position_weights_lhs==NULL) @@ -785,14 +785,14 @@ void CWeightedDegreePositionStringKernel::compute_by_tree( int32_t len=0; bool free_vec; - char* char_vec=((CStringFeatures*) rhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=std::static_pointer_cast>(rhs)->get_feature_vector(idx, len, free_vec); ASSERT(max_mismatch==0) int32_t *vec=SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) rhs)->free_feature_vector(char_vec, idx, free_vec); + std::static_pointer_cast>(rhs)->free_feature_vector(char_vec, idx, free_vec); for (int32_t i=0; i shifts) +void WeightedDegreePositionStringKernel::set_shifts(SGVector shifts) { SG_FREE(shift); @@ -838,14 +838,14 @@ void CWeightedDegreePositionStringKernel::set_shifts(SGVector shifts) for (int32_t i=0; i=0 && max_shift<=shift_len) } } -bool CWeightedDegreePositionStringKernel::set_wd_weights() +bool WeightedDegreePositionStringKernel::set_wd_weights() { ASSERT(degree>0) @@ -872,8 +872,8 @@ bool CWeightedDegreePositionStringKernel::set_wd_weights() { if (j new_weights) +bool WeightedDegreePositionStringKernel::set_weights(SGMatrix new_weights) { float64_t* ws=new_weights.matrix; int32_t d=new_weights.num_rows; @@ -915,7 +915,7 @@ bool CWeightedDegreePositionStringKernel::set_weights(SGMatrix new_we return true; } -void CWeightedDegreePositionStringKernel::set_position_weights(SGVector pws) +void WeightedDegreePositionStringKernel::set_position_weights(SGVector pws) { if (seq_length==0) seq_length=pws.vlen; @@ -932,7 +932,7 @@ void CWeightedDegreePositionStringKernel::set_position_weights(SGVector(std::max(seq_length,degree)); @@ -1000,7 +1000,7 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_from_wd() for (k=0; k 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_from_wd_external() +bool WeightedDegreePositionStringKernel::init_block_weights_from_wd_external() { ASSERT(weights) block_weights=SGVector(std::max(seq_length,degree)); @@ -1017,15 +1017,15 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_from_wd_external() { int32_t i=0; block_weights[0]=weights[0]; - for (i=1; i 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_const() +bool WeightedDegreePositionStringKernel::init_block_weights_const() { block_weights=SGVector(seq_length); @@ -1048,7 +1048,7 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_const() return (block_weights.vlen > 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_linear() +bool WeightedDegreePositionStringKernel::init_block_weights_linear() { block_weights=SGVector(seq_length); @@ -1061,7 +1061,7 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_linear() return (block_weights.vlen > 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_sqpoly() +bool WeightedDegreePositionStringKernel::init_block_weights_sqpoly() { block_weights=SGVector(seq_length); @@ -1077,7 +1077,7 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_sqpoly() return (block_weights.vlen > 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_cubicpoly() +bool WeightedDegreePositionStringKernel::init_block_weights_cubicpoly() { block_weights=SGVector(seq_length); @@ -1093,7 +1093,7 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_cubicpoly() return (block_weights.vlen > 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_exp() +bool WeightedDegreePositionStringKernel::init_block_weights_exp() { block_weights=SGVector(seq_length); @@ -1109,24 +1109,24 @@ bool CWeightedDegreePositionStringKernel::init_block_weights_exp() return (block_weights.vlen > 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights_log() +bool WeightedDegreePositionStringKernel::init_block_weights_log() { block_weights=SGVector(seq_length); if (block_weights.vlen) { for (int32_t i=1; i 0); } -bool CWeightedDegreePositionStringKernel::init_block_weights() +bool WeightedDegreePositionStringKernel::init_block_weights() { switch (type) { @@ -1152,11 +1152,11 @@ bool CWeightedDegreePositionStringKernel::init_block_weights() -void* CWeightedDegreePositionStringKernel::compute_batch_helper(void* p) +void* WeightedDegreePositionStringKernel::compute_batch_helper(void* p) { S_THREAD_PARAM_WDS* params = (S_THREAD_PARAM_WDS*) p; int32_t j=params->j; - CWeightedDegreePositionStringKernel* wd=params->kernel; + WeightedDegreePositionStringKernel* wd=params->kernel; CTrie* tries=params->tries; float64_t* weights=params->weights; int32_t length=params->length; @@ -1170,22 +1170,22 @@ void* CWeightedDegreePositionStringKernel::compute_batch_helper(void* p) for (int32_t i=params->start; iend; i++) { int32_t len=0; - CStringFeatures* rhs_feat=((CStringFeatures*) wd->get_rhs()); - CAlphabet* alpha=wd->alphabet; + auto rhs_feat=(StringFeatures*)(wd->get_rhs().get()); + auto alpha=wd->alphabet; bool free_vec; char* char_vec=rhs_feat->get_feature_vector(vec_idx[i], len, free_vec); - for (int32_t k=CMath::max(0,j-max_shift); kget_degree()+max_shift); k++) + for (int32_t k=Math::max(0,j-max_shift); kget_degree()+max_shift); k++) vec[k]=alpha->remap_to_bin(char_vec[k]); rhs_feat->free_feature_vector(char_vec, vec_idx[i], free_vec); - SG_UNREF(rhs_feat); + result[i] += factor*wd->normalizer->normalize_rhs(tries->compute_by_tree_helper(vec, len, j, j, j, weights, (length!=0)), vec_idx[i]); if (wd->get_optimization_type()==SLOWBUTMEMEFFICIENT) { - for (int32_t q=CMath::max(0,j-max_shift); q=1) && (s<=shift[q]) && (q+s*) rhs)->get_max_vector_length(); + int32_t num_feat=std::static_pointer_cast>(rhs)->get_max_vector_length(); ASSERT(num_feat>0) // TODO: port to use OpenMP backend instead of pthread #ifdef HAVE_PTHREAD @@ -1239,7 +1239,7 @@ void CWeightedDegreePositionStringKernel::compute_batch( if (num_threads < 2) { // TODO: replace with the new signal - // for (int32_t j=0; j*) rhs)->get_max_vector_length(); + num_feat=std::static_pointer_cast>(rhs)->get_max_vector_length(); ASSERT(num_feat>0) ASSERT(alphabet) ASSERT(alphabet->get_alphabet()==DNA || alphabet->get_alphabet()==RNA) @@ -1351,7 +1351,7 @@ float64_t* CWeightedDegreePositionStringKernel::compute_scoring( int32_t bigtabSize=0; for (k=0; k 0 ) { const int32_t j = k - 1; - const int32_t nofJmers = (int32_t) CMath::pow( num_sym, j+1 ); + const int32_t nofJmers = (int32_t) Math::pow( num_sym, j+1 ); for(int32_t p = 0; p < num_feat; ++p ) { const int32_t offsetJ = nofJmers * p; const int32_t offsetJ1 = nofJmers * (p+1); @@ -1476,7 +1476,7 @@ float64_t* CWeightedDegreePositionStringKernel::compute_scoring( return result; } -char* CWeightedDegreePositionStringKernel::compute_consensus( +char* WeightedDegreePositionStringKernel::compute_consensus( int32_t &num_feat, int32_t num_suppvec, int32_t* IDX, float64_t* alphas) { ASSERT(position_weights_lhs==NULL) @@ -1484,7 +1484,7 @@ char* CWeightedDegreePositionStringKernel::compute_consensus( //only works for order <= 32 ASSERT(degree<=32) ASSERT(!tries.get_use_compact_terminal_nodes()) - num_feat=((CStringFeatures*) rhs)->get_max_vector_length(); + num_feat=std::static_pointer_cast>(rhs)->get_max_vector_length(); ASSERT(num_feat>0) ASSERT(alphabet) ASSERT(alphabet->get_alphabet()==DNA || alphabet->get_alphabet()==RNA) @@ -1493,7 +1493,7 @@ char* CWeightedDegreePositionStringKernel::compute_consensus( char* result=SG_MALLOC(char, num_feat); //backtracking and scoring table - int32_t num_tables=CMath::max(1,num_feat-degree+1); + int32_t num_tables=Math::max(1,num_feat-degree+1); DynArray** table=SG_MALLOC(DynArray*, num_tables); for (int32_t i=0; i*) rhs)->get_max_vector_length(); + num_feat=std::static_pointer_cast>(rhs)->get_max_vector_length(); ASSERT(num_feat>0) ASSERT(alphabet->get_alphabet()==DNA) ASSERT(max_degree>0) @@ -1623,7 +1623,7 @@ float64_t* CWeightedDegreePositionStringKernel::extract_w( offset = 0; for( k = 0; k < max_degree; ++k ) { offsets[k] = offset; - const int32_t nofsKmers = (int32_t) CMath::pow( NUM_SYMS, k+1 ); + const int32_t nofsKmers = (int32_t) Math::pow( NUM_SYMS, k+1 ); const int32_t tabSize = nofsKmers * seqLen; offset += tabSize; } @@ -1654,7 +1654,7 @@ float64_t* CWeightedDegreePositionStringKernel::extract_w( return w_result; } -float64_t* CWeightedDegreePositionStringKernel::compute_POIM( +float64_t* WeightedDegreePositionStringKernel::compute_POIM( int32_t max_degree, int32_t& num_feat, int32_t& num_sym, float64_t* poim_result, int32_t num_suppvec, int32_t* IDX, float64_t* alphas, float64_t* distrib ) @@ -1666,7 +1666,7 @@ float64_t* CWeightedDegreePositionStringKernel::compute_POIM( // === check ASSERT(position_weights_lhs==NULL) ASSERT(position_weights_rhs==NULL) - num_feat=((CStringFeatures*) rhs)->get_max_vector_length(); + num_feat=std::static_pointer_cast>(rhs)->get_max_vector_length(); ASSERT(num_feat>0) ASSERT(alphabet->get_alphabet()==DNA) ASSERT(max_degree!=0) @@ -1725,7 +1725,7 @@ float64_t* CWeightedDegreePositionStringKernel::compute_POIM( offset = 0; for( k = 0; k < max_degree; ++k ) { offsets[k] = offset; - const int32_t nofsKmers = (int32_t) CMath::pow( NUM_SYMS, k+1 ); + const int32_t nofsKmers = (int32_t) Math::pow( NUM_SYMS, k+1 ); const int32_t tabSize = nofsKmers * seqLen; offset += tabSize; } @@ -1750,8 +1750,8 @@ float64_t* CWeightedDegreePositionStringKernel::compute_POIM( if( debug==0 || debug==1 ) { poim_tries.POIMs_extract_W( subs, max_degree ); for( k = 1; k < max_degree; ++k ) { - const int32_t nofKmers2 = ( k > 1 ) ? (int32_t) CMath::pow(NUM_SYMS,k-1) : 0; - const int32_t nofKmers1 = (int32_t) CMath::pow( NUM_SYMS, k ); + const int32_t nofKmers2 = ( k > 1 ) ? (int32_t) Math::pow(NUM_SYMS,k-1) : 0; + const int32_t nofKmers1 = (int32_t) Math::pow( NUM_SYMS, k ); const int32_t nofKmers0 = nofKmers1 * NUM_SYMS; for( i = 0; i < seqLen; ++i ) { float64_t* const subs_k2i1 = ( k>1 && i distrib) +void WeightedDegreePositionStringKernel::prepare_POIM2(SGMatrix distrib) { SG_FREE(m_poim_distrib); int32_t num_sym=distrib.num_cols; @@ -1801,8 +1801,8 @@ void CWeightedDegreePositionStringKernel::prepare_POIM2(SGMatrix dist m_poim_num_feat=num_feat; } -void CWeightedDegreePositionStringKernel::compute_POIM2( - int32_t max_degree, CSVM* svm) +void WeightedDegreePositionStringKernel::compute_POIM2( + int32_t max_degree, std::shared_ptr svm) { ASSERT(svm) int32_t num_suppvec=svm->get_num_support_vectors(); @@ -1836,13 +1836,13 @@ void CWeightedDegreePositionStringKernel::compute_POIM2( SG_FREE(sv_idx); } -SGVector CWeightedDegreePositionStringKernel::get_POIM2() +SGVector WeightedDegreePositionStringKernel::get_POIM2() { SGVector poim(m_poim, m_poim_result_len, false); return poim; } -void CWeightedDegreePositionStringKernel::cleanup_POIM2() +void WeightedDegreePositionStringKernel::cleanup_POIM2() { SG_FREE(m_poim) ; m_poim=NULL ; @@ -1853,9 +1853,9 @@ void CWeightedDegreePositionStringKernel::cleanup_POIM2() m_poim_result_len=0 ; } -void CWeightedDegreePositionStringKernel::load_serializable_post() noexcept(false) +void WeightedDegreePositionStringKernel::load_serializable_post() noexcept(false) { - CKernel::load_serializable_post(); + Kernel::load_serializable_post(); tries=CTrie(degree); poim_tries=CTrie(degree); @@ -1864,7 +1864,7 @@ void CWeightedDegreePositionStringKernel::load_serializable_post() noexcept(fals init_block_weights(); } -void CWeightedDegreePositionStringKernel::init() +void WeightedDegreePositionStringKernel::init() { weights = NULL; weights_length = 0; @@ -1908,33 +1908,33 @@ void CWeightedDegreePositionStringKernel::init() properties |= KP_LINADD | KP_KERNCOMBINATION | KP_BATCHEVALUATION; - set_normalizer(new CSqrtDiagKernelNormalizer()); + set_normalizer(std::make_shared()); - m_parameters->add_matrix( + /*m_parameters->add_matrix( &weights, &weights_degree, &weights_length, "weights", - "WD Kernel weights."); + "WD Kernel weights.");*/ watch_param("weights", &weights, &weights_degree, &weights_length); - m_parameters->add_vector( + /*m_parameters->add_vector( &position_weights, &position_weights_len, "position_weights", - "Weights per position."); + "Weights per position.");*/ watch_param("position_weights", &position_weights, &position_weights_len); - m_parameters->add_vector( + /*m_parameters->add_vector( &position_weights_lhs, &position_weights_lhs_len, - "position_weights_lhs", "Weights per position left hand side."); + "position_weights_lhs", "Weights per position left hand side.");*/ watch_param( "position_weights_lhs", &position_weights_lhs, &position_weights_lhs_len); - m_parameters->add_vector( + /*m_parameters->add_vector( &position_weights_rhs, &position_weights_rhs_len, - "position_weights_rhs", "Weights per position right hand side."); + "position_weights_rhs", "Weights per position right hand side.");*/ watch_param( "position_weights_rhs", &position_weights_rhs, &position_weights_rhs_len); - m_parameters->add_vector(&shift, &shift_len, "shift", "Shift Vector."); + /*m_parameters->add_vector(&shift, &shift_len, "shift", "Shift Vector.");*/ watch_param("shift", &shift, &shift_len); SG_ADD( @@ -1954,7 +1954,7 @@ void CWeightedDegreePositionStringKernel::init() &which_degree, "which_degree", "The selected degree. All degrees are used by default (for value -1).", ParameterProperties::HYPER); - SG_ADD((CSGObject**)&alphabet, "alphabet", "Alphabet of Features."); + SG_ADD((std::shared_ptr*)&alphabet, "alphabet", "Alphabet of Features."); SG_ADD_OPTIONS( (machine_int_t*)&type, "type", "WeightedDegree kernel type.", ParameterProperties::HYPER, diff --git a/src/shogun/kernel/string/WeightedDegreePositionStringKernel.h b/src/shogun/kernel/string/WeightedDegreePositionStringKernel.h index 2d3c4919d76..6778d58de24 100644 --- a/src/shogun/kernel/string/WeightedDegreePositionStringKernel.h +++ b/src/shogun/kernel/string/WeightedDegreePositionStringKernel.h @@ -18,7 +18,7 @@ namespace shogun { -class CSVM; +class SVM; /** @brief The Weighted Degree Position String kernel (Weighted Degree kernel * with shifts). @@ -43,11 +43,11 @@ class CSVM; * to shifts (in either direction) of extent s, and S(l) determines * the shift range at position l. */ -class CWeightedDegreePositionStringKernel: public CStringKernel +class WeightedDegreePositionStringKernel: public StringKernel { public: /** default constructor */ - CWeightedDegreePositionStringKernel(); + WeightedDegreePositionStringKernel(); /** constructor * @@ -56,7 +56,7 @@ class CWeightedDegreePositionStringKernel: public CStringKernel * @param max_mismatch maximum mismatch * @param mkl_stepsize MKL stepsize */ - CWeightedDegreePositionStringKernel( + WeightedDegreePositionStringKernel( int32_t size, int32_t degree, int32_t max_mismatch=0, int32_t mkl_stepsize=1); @@ -69,7 +69,7 @@ class CWeightedDegreePositionStringKernel: public CStringKernel * @param shifts position shifts * @param mkl_stepsize MKL stepsize */ - CWeightedDegreePositionStringKernel( + WeightedDegreePositionStringKernel( int32_t size, SGVector weights, int32_t degree, int32_t max_mismatch, SGVector shifts, int32_t mkl_stepsize=1); @@ -80,10 +80,10 @@ class CWeightedDegreePositionStringKernel: public CStringKernel * @param r features of right-hand side * @param degree degree */ - CWeightedDegreePositionStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t degree); + WeightedDegreePositionStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t degree); - virtual ~CWeightedDegreePositionStringKernel(); + virtual ~WeightedDegreePositionStringKernel(); /** initialize kernel * @@ -91,7 +91,7 @@ class CWeightedDegreePositionStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -585,7 +585,7 @@ class CWeightedDegreePositionStringKernel: public CStringKernel * @param svm SVM */ - void compute_POIM2(int32_t max_degree, CSVM* svm); + void compute_POIM2(int32_t max_degree, std::shared_ptr svm); /** get POIM2 * @@ -771,7 +771,7 @@ class CWeightedDegreePositionStringKernel: public CStringKernel int32_t m_poim_result_len; /** alphabet of features */ - CAlphabet* alphabet; + std::shared_ptr alphabet; }; } #endif /* _WEIGHTEDDEGREEPOSITIONSTRINGKERNEL_H__ */ diff --git a/src/shogun/kernel/string/WeightedDegreeStringKernel.cpp b/src/shogun/kernel/string/WeightedDegreeStringKernel.cpp index d71e09013f5..f96fa704ef0 100644 --- a/src/shogun/kernel/string/WeightedDegreeStringKernel.cpp +++ b/src/shogun/kernel/string/WeightedDegreeStringKernel.cpp @@ -32,7 +32,7 @@ struct S_THREAD_PARAM_WD int32_t* vec; float64_t* result; float64_t* weights; - CWeightedDegreeStringKernel* kernel; + WeightedDegreeStringKernel* kernel; CTrie* tries; float64_t factor; int32_t j; @@ -43,16 +43,16 @@ struct S_THREAD_PARAM_WD }; #endif // DOXYGEN_SHOULD_SKIP_THIS -CWeightedDegreeStringKernel::CWeightedDegreeStringKernel () -: CStringKernel() +WeightedDegreeStringKernel::WeightedDegreeStringKernel () +: StringKernel() { init(); } -CWeightedDegreeStringKernel::CWeightedDegreeStringKernel ( +WeightedDegreeStringKernel::WeightedDegreeStringKernel ( int32_t d, EWDKernType t) -: CStringKernel() +: StringKernel() { init(); @@ -60,8 +60,8 @@ CWeightedDegreeStringKernel::CWeightedDegreeStringKernel ( type=t; } -CWeightedDegreeStringKernel::CWeightedDegreeStringKernel(SGVector w) -: CStringKernel(10) +WeightedDegreeStringKernel::WeightedDegreeStringKernel(SGVector w) +: StringKernel(10) { init(); @@ -76,17 +76,19 @@ CWeightedDegreeStringKernel::CWeightedDegreeStringKernel(SGVector w) weights[i]=w.vector[i]; } -CWeightedDegreeStringKernel::CWeightedDegreeStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t d) -: CStringKernel(10) +WeightedDegreeStringKernel::WeightedDegreeStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t d) +: StringKernel(10) { init(); degree=d; - set_normalizer(new CFirstElementKernelNormalizer()); + type=E_WD; + set_wd_weights_by_type(type); + set_normalizer(std::make_shared()); init(l, r); } -CWeightedDegreeStringKernel::~CWeightedDegreeStringKernel() +WeightedDegreeStringKernel::~WeightedDegreeStringKernel() { cleanup(); @@ -106,7 +108,7 @@ CWeightedDegreeStringKernel::~CWeightedDegreeStringKernel() } -void CWeightedDegreeStringKernel::remove_lhs() +void WeightedDegreeStringKernel::remove_lhs() { SG_DEBUG("deleting CWeightedDegreeStringKernel optimization") delete_optimization(); @@ -114,14 +116,14 @@ void CWeightedDegreeStringKernel::remove_lhs() if (tries!=NULL) tries->destroy(); - CKernel::remove_lhs(); + Kernel::remove_lhs(); } -void CWeightedDegreeStringKernel::create_empty_tries() +void WeightedDegreeStringKernel::create_empty_tries() { ASSERT(lhs) - seq_length=((CStringFeatures*) lhs)->get_max_vector_length(); + seq_length=lhs->as>()->get_max_vector_length(); if (tries!=NULL) { @@ -130,7 +132,7 @@ void CWeightedDegreeStringKernel::create_empty_tries() } } -bool CWeightedDegreeStringKernel::init(CFeatures* l, CFeatures* r) +bool WeightedDegreeStringKernel::init(std::shared_ptr l, std::shared_ptr r) { if (type != E_EXTERNAL) set_wd_weights_by_type(type); @@ -138,13 +140,13 @@ bool CWeightedDegreeStringKernel::init(CFeatures* l, CFeatures* r) int32_t lhs_changed=(lhs!=l); int32_t rhs_changed=(rhs!=r); - CStringKernel::init(l,r); + StringKernel::init(l,r); SG_DEBUG("lhs_changed: {}", lhs_changed) SG_DEBUG("rhs_changed: {}", rhs_changed) - CStringFeatures* sf_l=(CStringFeatures*) l; - CStringFeatures* sf_r=(CStringFeatures*) r; + auto sf_l=l->as>(); + auto sf_r=r->as>(); int32_t len=sf_l->get_max_vector_length(); if (lhs_changed && !sf_l->have_same_length(len)) @@ -153,21 +155,21 @@ bool CWeightedDegreeStringKernel::init(CFeatures* l, CFeatures* r) if (rhs_changed && !sf_r->have_same_length(len)) error("All strings in WD kernel must have same length (rhs wrong)!"); - SG_UNREF(alphabet); + alphabet=sf_l->get_alphabet(); - CAlphabet* ralphabet=sf_r->get_alphabet(); + auto ralphabet=sf_r->get_alphabet(); if (!((alphabet->get_alphabet()==DNA) || (alphabet->get_alphabet()==RNA))) properties &= ((uint64_t) (-1)) ^ (KP_LINADD | KP_BATCHEVALUATION); ASSERT(ralphabet->get_alphabet()==alphabet->get_alphabet()) - SG_UNREF(ralphabet); + if (tries!=NULL) { tries->delete_trees(max_mismatch==0); - SG_UNREF(tries); + } - tries=new CTrie(degree, max_mismatch==0); + tries=std::make_shared>(degree, max_mismatch==0); create_empty_tries(); init_block_weights(); @@ -175,7 +177,7 @@ bool CWeightedDegreeStringKernel::init(CFeatures* l, CFeatures* r) return init_normalizer(); } -void CWeightedDegreeStringKernel::cleanup() +void WeightedDegreeStringKernel::cleanup() { SG_DEBUG("deleting CWeightedDegreeStringKernel optimization") delete_optimization(); @@ -186,20 +188,20 @@ void CWeightedDegreeStringKernel::cleanup() if (tries!=NULL) { tries->destroy(); - SG_UNREF(tries); + tries=NULL; } seq_length=0; tree_initialized = false; - SG_UNREF(alphabet); + alphabet=NULL; - CKernel::cleanup(); + Kernel::cleanup(); } -bool CWeightedDegreeStringKernel::init_optimization(int32_t count, int32_t* IDX, float64_t* alphas, int32_t tree_num) +bool WeightedDegreeStringKernel::init_optimization(int32_t count, int32_t* IDX, float64_t* alphas, int32_t tree_num) { if (tree_num<0) SG_DEBUG("deleting CWeightedDegreeStringKernel optimization") @@ -236,7 +238,7 @@ bool CWeightedDegreeStringKernel::init_optimization(int32_t count, int32_t* IDX, return true ; } -bool CWeightedDegreeStringKernel::delete_optimization() +bool WeightedDegreeStringKernel::delete_optimization() { if (get_is_initialized()) { @@ -250,7 +252,7 @@ bool CWeightedDegreeStringKernel::delete_optimization() } -float64_t CWeightedDegreeStringKernel::compute_with_mismatch( +float64_t WeightedDegreeStringKernel::compute_with_mismatch( char* avec, int32_t alen, char* bvec, int32_t blen) { float64_t sum = 0.0; @@ -278,7 +280,7 @@ float64_t CWeightedDegreeStringKernel::compute_with_mismatch( return sum ; } -float64_t CWeightedDegreeStringKernel::compute_using_block( +float64_t WeightedDegreeStringKernel::compute_using_block( char* avec, int32_t alen, char* bvec, int32_t blen) { ASSERT(alen==blen) @@ -304,7 +306,7 @@ float64_t CWeightedDegreeStringKernel::compute_using_block( return sum; } -float64_t CWeightedDegreeStringKernel::compute_without_mismatch( +float64_t WeightedDegreeStringKernel::compute_without_mismatch( char* avec, int32_t alen, char* bvec, int32_t blen) { float64_t sum = 0.0; @@ -327,7 +329,7 @@ float64_t CWeightedDegreeStringKernel::compute_without_mismatch( return sum ; } -float64_t CWeightedDegreeStringKernel::compute_without_mismatch_matrix( +float64_t WeightedDegreeStringKernel::compute_without_mismatch_matrix( char* avec, int32_t alen, char* bvec, int32_t blen) { float64_t sum = 0.0; @@ -351,12 +353,12 @@ float64_t CWeightedDegreeStringKernel::compute_without_mismatch_matrix( } -float64_t CWeightedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b) +float64_t WeightedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b) { int32_t alen, blen; bool free_avec, free_bvec; - char* avec=((CStringFeatures*) lhs)->get_feature_vector(idx_a, alen, free_avec); - char* bvec=((CStringFeatures*) rhs)->get_feature_vector(idx_b, blen, free_bvec); + char* avec=lhs->as>()->get_feature_vector(idx_a, alen, free_avec); + char* bvec=rhs->as>()->get_feature_vector(idx_b, blen, free_bvec); float64_t result=0; if (max_mismatch==0 && length==0 && block_computation) @@ -370,14 +372,14 @@ float64_t CWeightedDegreeStringKernel::compute(int32_t idx_a, int32_t idx_b) else result=compute_without_mismatch_matrix(avec, alen, bvec, blen); } - ((CStringFeatures*) lhs)->free_feature_vector(avec, idx_a, free_avec); - ((CStringFeatures*) rhs)->free_feature_vector(bvec, idx_b, free_bvec); + lhs->as>()->free_feature_vector(avec, idx_a, free_avec); + rhs->as>()->free_feature_vector(bvec, idx_b, free_bvec); return result; } -void CWeightedDegreeStringKernel::add_example_to_tree( +void WeightedDegreeStringKernel::add_example_to_tree( int32_t idx, float64_t alpha) { ASSERT(alphabet) @@ -385,13 +387,13 @@ void CWeightedDegreeStringKernel::add_example_to_tree( int32_t len=0; bool free_vec; - char* char_vec=((CStringFeatures*) lhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=lhs->as>()->get_feature_vector(idx, len, free_vec); ASSERT(max_mismatch==0) int32_t *vec=SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + lhs->as>()->free_feature_vector(char_vec, idx, free_vec); if (length == 0 || max_mismatch > 0) { @@ -423,7 +425,7 @@ void CWeightedDegreeStringKernel::add_example_to_tree( tree_initialized=true ; } -void CWeightedDegreeStringKernel::add_example_to_single_tree( +void WeightedDegreeStringKernel::add_example_to_single_tree( int32_t idx, float64_t alpha, int32_t tree_num) { ASSERT(alphabet) @@ -431,13 +433,13 @@ void CWeightedDegreeStringKernel::add_example_to_single_tree( int32_t len; bool free_vec; - char* char_vec=((CStringFeatures*) lhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=lhs->as>()->get_feature_vector(idx, len, free_vec); ASSERT(max_mismatch==0) int32_t *vec = SG_MALLOC(int32_t, len); for (int32_t i=tree_num; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + lhs->as>()->free_feature_vector(char_vec, idx, free_vec); ASSERT(tries) @@ -448,7 +450,7 @@ void CWeightedDegreeStringKernel::add_example_to_single_tree( tree_initialized=true ; } -void CWeightedDegreeStringKernel::add_example_to_tree_mismatch(int32_t idx, float64_t alpha) +void WeightedDegreeStringKernel::add_example_to_tree_mismatch(int32_t idx, float64_t alpha) { ASSERT(tries) ASSERT(alphabet) @@ -456,13 +458,13 @@ void CWeightedDegreeStringKernel::add_example_to_tree_mismatch(int32_t idx, floa int32_t len ; bool free_vec; - char* char_vec=((CStringFeatures*) lhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=lhs->as>()->get_feature_vector(idx, len, free_vec); int32_t *vec = SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + lhs->as>()->free_feature_vector(char_vec, idx, free_vec); for (int32_t i=0; i*) lhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=lhs->as>()->get_feature_vector(idx, len, free_vec); int32_t *vec=SG_MALLOC(int32_t, len); for (int32_t i=tree_num; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + lhs->as>()->free_feature_vector(char_vec, idx, free_vec); if (alpha!=0.0) { @@ -502,20 +504,20 @@ void CWeightedDegreeStringKernel::add_example_to_single_tree_mismatch( } -float64_t CWeightedDegreeStringKernel::compute_by_tree(int32_t idx) +float64_t WeightedDegreeStringKernel::compute_by_tree(int32_t idx) { ASSERT(alphabet) ASSERT(alphabet->get_alphabet()==DNA || alphabet->get_alphabet()==RNA) int32_t len=0; bool free_vec; - char* char_vec=((CStringFeatures*) rhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=rhs->as>()->get_feature_vector(idx, len, free_vec); ASSERT(char_vec && len>0) int32_t *vec=SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + lhs->as>()->free_feature_vector(char_vec, idx, free_vec); float64_t sum=0; ASSERT(tries) @@ -526,7 +528,7 @@ float64_t CWeightedDegreeStringKernel::compute_by_tree(int32_t idx) return normalizer->normalize_rhs(sum, idx); } -void CWeightedDegreeStringKernel::compute_by_tree( +void WeightedDegreeStringKernel::compute_by_tree( int32_t idx, float64_t* LevelContrib) { ASSERT(alphabet) @@ -534,13 +536,13 @@ void CWeightedDegreeStringKernel::compute_by_tree( int32_t len ; bool free_vec; - char* char_vec=((CStringFeatures*) rhs)->get_feature_vector(idx, len, free_vec); + char* char_vec=rhs->as>()->get_feature_vector(idx, len, free_vec); int32_t *vec = SG_MALLOC(int32_t, len); for (int32_t i=0; iremap_to_bin(char_vec[i]); - ((CStringFeatures*) lhs)->free_feature_vector(char_vec, idx, free_vec); + lhs->as>()->free_feature_vector(char_vec, idx, free_vec); ASSERT(tries) for (int32_t i=0; icompute_abs_weights(len); } -bool CWeightedDegreeStringKernel::set_wd_weights_by_type(EWDKernType p_type) +bool WeightedDegreeStringKernel::set_wd_weights_by_type(EWDKernType p_type) { ASSERT(degree>0) ASSERT(p_type==E_WD) /// if we know a better weighting later on do a switch @@ -587,8 +589,8 @@ bool CWeightedDegreeStringKernel::set_wd_weights_by_type(EWDKernType p_type) { if (j new_weights) +bool WeightedDegreeStringKernel::set_weights(SGMatrix new_weights) { float64_t* ws=new_weights.matrix; int32_t d=new_weights.num_rows; @@ -642,7 +644,7 @@ bool CWeightedDegreeStringKernel::set_weights(SGMatrix new_weights) return true; } -bool CWeightedDegreeStringKernel::set_position_weights( +bool WeightedDegreeStringKernel::set_position_weights( float64_t* pws, int32_t len) { if (len==0) @@ -672,40 +674,40 @@ bool CWeightedDegreeStringKernel::set_position_weights( return false; } -bool CWeightedDegreeStringKernel::init_block_weights_from_wd() +bool WeightedDegreeStringKernel::init_block_weights_from_wd() { SG_FREE(block_weights); - block_weights=SG_MALLOC(float64_t, CMath::max(seq_length,degree)); + block_weights=SG_MALLOC(float64_t, Math::max(seq_length,degree)); int32_t k; float64_t d=degree; // use float to evade rounding errors below for (k=0; kj; - CWeightedDegreeStringKernel* wd=params->kernel; + WeightedDegreeStringKernel* wd=params->kernel; CTrie* tries=params->tries; float64_t* weights=params->weights; int32_t length=params->length; @@ -828,15 +830,15 @@ void* CWeightedDegreeStringKernel::compute_batch_helper(void* p) float64_t factor=params->factor; int32_t* vec_idx=params->vec_idx; - CStringFeatures* rhs_feat=((CStringFeatures*) wd->get_rhs()); - CAlphabet* alpha=wd->alphabet; + StringFeatures* rhs_feat=(StringFeatures*)(wd->get_rhs().get()); + auto alpha=wd->alphabet; for (int32_t i=params->start; iend; i++) { int32_t len=0; bool free_vec; char* char_vec=rhs_feat->get_feature_vector(vec_idx[i], len, free_vec); - for (int32_t k=j; kget_degree()); k++) + for (int32_t k=j; kget_degree()); k++) vec[k]=alpha->remap_to_bin(char_vec[k]); rhs_feat->free_feature_vector(char_vec, vec_idx[i], free_vec); @@ -846,12 +848,12 @@ void* CWeightedDegreeStringKernel::compute_batch_helper(void* p) wd->normalizer->normalize_rhs(tries->compute_by_tree_helper(vec, len, j, j, j, weights, (length!=0)), vec_idx[i]); } - SG_UNREF(rhs_feat); + return NULL; } -void CWeightedDegreeStringKernel::compute_batch( +void WeightedDegreeStringKernel::compute_batch( int32_t num_vec, int32_t* vec_idx, float64_t* result, int32_t num_suppvec, int32_t* IDX, float64_t* alphas, float64_t factor) { @@ -865,7 +867,7 @@ void CWeightedDegreeStringKernel::compute_batch( ASSERT(result) create_empty_tries(); - int32_t num_feat=((CStringFeatures*) rhs)->get_max_vector_length(); + int32_t num_feat=rhs->as>()->get_max_vector_length(); ASSERT(num_feat>0) // TODO: port to use OpenMP backend instead of pthread #ifdef HAVE_PTHREAD @@ -880,7 +882,7 @@ void CWeightedDegreeStringKernel::compute_batch( if (num_threads < 2) { // TODO: replace with the new signal - // for (int32_t j=0; j()); - m_parameters->add_matrix( + /*m_parameters->add_matrix( &weights, &weights_degree, &weights_length, "weights", - "WD Kernel weights."); + "WD Kernel weights.");*/ watch_param("weights", &weights, &weights_degree, &weights_length); - m_parameters->add_vector( + /*m_parameters->add_vector( &position_weights, &position_weights_len, "position_weights", - "Weights per position."); + "Weights per position.");*/ watch_param("position_weights", &position_weights, &position_weights_len); SG_ADD( @@ -1034,7 +1036,7 @@ void CWeightedDegreeStringKernel::init() &which_degree, "which_degree", "The selected degree. All degrees are used by default (for value -1).", ParameterProperties::HYPER); - SG_ADD((CSGObject**)&alphabet, "alphabet", "Alphabet of Features."); + SG_ADD((std::shared_ptr*)&alphabet, "alphabet", "Alphabet of Features."); SG_ADD_OPTIONS( (machine_int_t*)&type, "type", "WeightedDegree kernel type.", ParameterProperties::HYPER, diff --git a/src/shogun/kernel/string/WeightedDegreeStringKernel.h b/src/shogun/kernel/string/WeightedDegreeStringKernel.h index c7e711e5599..39a06a01e86 100644 --- a/src/shogun/kernel/string/WeightedDegreeStringKernel.h +++ b/src/shogun/kernel/string/WeightedDegreeStringKernel.h @@ -47,14 +47,14 @@ enum EWDKernType * which evaluates to 1 when its argument is true and to 0 * otherwise. */ -class CWeightedDegreeStringKernel: public CStringKernel +class WeightedDegreeStringKernel: public StringKernel { public: /** default constructor * */ - CWeightedDegreeStringKernel(); + WeightedDegreeStringKernel(); /** constructor @@ -62,13 +62,13 @@ class CWeightedDegreeStringKernel: public CStringKernel * @param degree degree * @param type weighted degree kernel type */ - CWeightedDegreeStringKernel(int32_t degree, EWDKernType type=E_WD); + WeightedDegreeStringKernel(int32_t degree, EWDKernType type=E_WD); /** constructor * * @param weights kernel's weights */ - CWeightedDegreeStringKernel(SGVector weights); + WeightedDegreeStringKernel(SGVector weights); /** constructor * @@ -76,10 +76,10 @@ class CWeightedDegreeStringKernel: public CStringKernel * @param r features of right-hand side * @param degree degree */ - CWeightedDegreeStringKernel( - CStringFeatures* l, CStringFeatures* r, int32_t degree); + WeightedDegreeStringKernel( + std::shared_ptr> l, std::shared_ptr> r, int32_t degree); - virtual ~CWeightedDegreeStringKernel(); + virtual ~WeightedDegreeStringKernel(); /** initialize kernel * @@ -87,7 +87,7 @@ class CWeightedDegreeStringKernel: public CStringKernel * @param r features of right-hand side * @return if initializing was successful */ - virtual bool init(CFeatures* l, CFeatures* r); + virtual bool init(std::shared_ptr l, std::shared_ptr r); /** clean up kernel */ virtual void cleanup(); @@ -227,7 +227,7 @@ class CWeightedDegreeStringKernel: public CStringKernel virtual int32_t get_num_subkernels() { if (normalizer && normalizer->get_normalizer_type()==N_MULTITASK) - return ((CMultitaskKernelMklNormalizer*)normalizer)->get_num_betas(); + return std::static_pointer_cast(normalizer)->get_num_betas(); if (position_weights!=NULL) return (int32_t) ceil(1.0*seq_length/mkl_stepsize) ; if (length==0) @@ -272,7 +272,7 @@ class CWeightedDegreeStringKernel: public CStringKernel if (normalizer && normalizer->get_normalizer_type()==N_MULTITASK) for (int32_t i=0; iget_beta(i); + weights_buffer[i] = std::static_pointer_cast(normalizer)->get_beta(i); else if (position_weights!=NULL) for (int32_t i=0; i if (normalizer && normalizer->get_normalizer_type()==N_MULTITASK) for (int32_t i=0; iset_beta(i, weights2[i]); + std::static_pointer_cast(normalizer)->set_beta(i, weights2[i]); else if (position_weights!=NULL) { for (int32_t i=0; i * * @return if successful */ - virtual bool set_normalizer(CKernelNormalizer* normalizer_) { + virtual bool set_normalizer(std::shared_ptr normalizer_) { if (normalizer_ && strcmp(normalizer_->get_name(),"MultitaskKernelTreeNormalizer")==0) { unset_property(KP_LINADD); @@ -351,7 +351,7 @@ class CWeightedDegreeStringKernel: public CStringKernel } - return CStringKernel::set_normalizer(normalizer_); + return StringKernel::set_normalizer(normalizer_); } @@ -747,13 +747,13 @@ class CWeightedDegreeStringKernel: public CStringKernel int32_t which_degree; /** tries */ - CTrie* tries; + std::shared_ptr> tries; /** if tree is initialized */ bool tree_initialized; /** alphabet of features */ - CAlphabet* alphabet; + std::shared_ptr alphabet; }; } diff --git a/src/shogun/labels/BinaryLabels.cpp b/src/shogun/labels/BinaryLabels.cpp index 8f2948e8c36..76386f6bf05 100644 --- a/src/shogun/labels/BinaryLabels.cpp +++ b/src/shogun/labels/BinaryLabels.cpp @@ -14,16 +14,16 @@ using namespace shogun; -CBinaryLabels::CBinaryLabels() : CDenseLabels() +BinaryLabels::BinaryLabels() : DenseLabels() { } -CBinaryLabels::CBinaryLabels(int32_t num_labels) : CDenseLabels(num_labels) +BinaryLabels::BinaryLabels(int32_t num_labels) : DenseLabels(num_labels) { } #if !defined(SWIGJAVA) && !defined(SWIGCSHARP) -CBinaryLabels::CBinaryLabels(SGVector src) : CDenseLabels() +BinaryLabels::BinaryLabels(SGVector src) : DenseLabels() { SGVector values(src.vlen); for (int32_t i = 0; i < values.vlen; i++) @@ -34,7 +34,7 @@ CBinaryLabels::CBinaryLabels(SGVector src) : CDenseLabels() set_values(values); } -CBinaryLabels::CBinaryLabels(SGVector src) : CDenseLabels() +BinaryLabels::BinaryLabels(SGVector src) : DenseLabels() { SGVector values(src.vlen); for (int32_t i = 0; i < values.vlen; i++) @@ -46,7 +46,7 @@ CBinaryLabels::CBinaryLabels(SGVector src) : CDenseLabels() } #endif -CBinaryLabels::CBinaryLabels(SGVector src, float64_t threshold) : CDenseLabels() +BinaryLabels::BinaryLabels(SGVector src, float64_t threshold) : DenseLabels() { SGVector labels(src.vlen); for (int32_t i = 0; i < labels.vlen; i++) @@ -57,13 +57,13 @@ CBinaryLabels::CBinaryLabels(SGVector src, float64_t threshold) : CDe set_values(src); } -CBinaryLabels::CBinaryLabels(CFile * loader) : CDenseLabels(loader) +BinaryLabels::BinaryLabels(std::shared_ptr loader) : DenseLabels(loader) { } -bool CBinaryLabels::is_valid() const +bool BinaryLabels::is_valid() const { - if (!CDenseLabels::is_valid()) + if (!DenseLabels::is_valid()) return false; int32_t subset_size = get_num_labels(); @@ -76,27 +76,27 @@ bool CBinaryLabels::is_valid() const return true; } -void CBinaryLabels::ensure_valid(const char* context) +void BinaryLabels::ensure_valid(const char* context) { require(is_valid(), "Binary Labels must be -1 or +1!"); } -ELabelType CBinaryLabels::get_label_type() const +ELabelType BinaryLabels::get_label_type() const { return LT_BINARY; } -void CBinaryLabels::scores_to_probabilities(float64_t a, float64_t b) +void BinaryLabels::scores_to_probabilities(float64_t a, float64_t b) { - SG_TRACE("entering CBinaryLabels::scores_to_probabilities()"); + SG_TRACE("entering BinaryLabels::scores_to_probabilities()"); require(m_current_values.vector, "{}::scores_to_probabilities() requires " "values vector!", get_name()); if (a == 0 && b == 0) { - CStatistics::SigmoidParamters params = - CStatistics::fit_sigmoid(m_current_values); + Statistics::SigmoidParamters params = + Statistics::fit_sigmoid(m_current_values); a = params.a; b = params.b; } @@ -112,36 +112,35 @@ void CBinaryLabels::scores_to_probabilities(float64_t a, float64_t b) : 1.0 / (1 + std::exp(fApB)); } - SG_TRACE("leaving CBinaryLabels::scores_to_probabilities()"); + SG_TRACE("leaving BinaryLabels::scores_to_probabilities()"); } -CLabels* CBinaryLabels::shallow_subset_copy() +std::shared_ptr BinaryLabels::shallow_subset_copy() { - CLabels* shallow_copy_labels=NULL; SGVector shallow_copy_vector(m_labels); - shallow_copy_labels=new CBinaryLabels(m_labels.size()); - SG_REF(shallow_copy_labels); + auto shallow_copy_labels=std::make_shared(m_labels.size()); - ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector); + + shallow_copy_labels->set_labels(shallow_copy_vector); if (m_subset_stack->has_subsets()) shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); return shallow_copy_labels; } -CBinaryLabels::CBinaryLabels(const CDenseLabels& dense) : CDenseLabels(dense) +BinaryLabels::BinaryLabels(const DenseLabels& dense) : DenseLabels(dense) { ensure_valid(); } -CLabels* CBinaryLabels::duplicate() const +std::shared_ptr BinaryLabels::duplicate() const { - return new CBinaryLabels(*this); + return std::make_shared(*this); } namespace shogun { - Some binary_labels(CLabels* orig) + std::shared_ptr binary_labels(std::shared_ptr orig) { require(orig, "No labels provided."); try @@ -149,7 +148,7 @@ namespace shogun switch (orig->get_label_type()) { case LT_BINARY: - return Some::from_raw((CBinaryLabels*)orig); + return std::static_pointer_cast(orig); default: not_implemented(SOURCE_LOCATION); } @@ -161,6 +160,6 @@ namespace shogun e.what()); } - return Some::from_raw(nullptr); + return nullptr; } } // namespace shogun diff --git a/src/shogun/labels/BinaryLabels.h b/src/shogun/labels/BinaryLabels.h index ae1996b79b2..757b3fd2ad2 100644 --- a/src/shogun/labels/BinaryLabels.h +++ b/src/shogun/labels/BinaryLabels.h @@ -18,7 +18,7 @@ namespace shogun { -class CFile; +class File; template class SGVector; /** @brief Binary Labels for binary classification @@ -31,17 +31,17 @@ template class SGVector; * A note on Platt's probabilistic outputs for support vector machines. * Should only be used in conjunction with SVM. */ -class CBinaryLabels : public CDenseLabels +class BinaryLabels : public DenseLabels { public: /** default constructor */ - CBinaryLabels(); + BinaryLabels(); /** constructor * * @param num_labels number of labels */ - CBinaryLabels(int32_t num_labels); + BinaryLabels(int32_t num_labels); #if !defined(SWIGJAVA) && !defined(SWIGCSHARP) /** constructor @@ -49,14 +49,14 @@ class CBinaryLabels : public CDenseLabels * * @param src labels to set */ - CBinaryLabels(SGVector src); + BinaryLabels(SGVector src); /** constructor * sets labels with src elements (int64 version) * * @param src labels to set */ - CBinaryLabels(SGVector src); + BinaryLabels(SGVector src); #endif /** constructor @@ -66,19 +66,19 @@ class CBinaryLabels : public CDenseLabels * @param src labels to set * @param threshold threshold */ - CBinaryLabels(SGVector src, float64_t threshold = 0.0); + BinaryLabels(SGVector src, float64_t threshold = 0.0); /** constructor * * @param loader File object via which to load data */ - CBinaryLabels(CFile * loader); + BinaryLabels(std::shared_ptr loader); /** Cast constructor * - * @param dense CDenseLabels containing the binary labels + * @param dense DenseLabels containing the binary labels */ - CBinaryLabels(const CDenseLabels& dense); + BinaryLabels(const DenseLabels& dense); virtual bool is_valid() const override; @@ -96,7 +96,7 @@ class CBinaryLabels : public CDenseLabels */ virtual ELabelType get_label_type() const override; - virtual CLabels* duplicate() const override; + virtual std::shared_ptr duplicate() const override; /** Converts all scores to calibrated probabilities by fitting a * sigmoid function using the method described in @@ -125,12 +125,12 @@ class CBinaryLabels : public CDenseLabels } #ifndef SWIG // SWIG should skip this part - virtual CLabels* shallow_subset_copy() override; + virtual std::shared_ptr shallow_subset_copy() override; #endif }; #ifndef SWIG -Some binary_labels(CLabels* orig); +std::shared_ptr binary_labels(std::shared_ptr orig); #endif // SWIG } #endif diff --git a/src/shogun/labels/DenseLabels.cpp b/src/shogun/labels/DenseLabels.cpp index 862e5828b80..0bca3d2ce2e 100644 --- a/src/shogun/labels/DenseLabels.cpp +++ b/src/shogun/labels/DenseLabels.cpp @@ -16,53 +16,53 @@ using namespace shogun; -CDenseLabels::CDenseLabels() -: CLabels() +DenseLabels::DenseLabels() +: Labels() { init(); } -CDenseLabels::CDenseLabels(int32_t num_lab) -: CLabels() +DenseLabels::DenseLabels(int32_t num_lab) +: Labels() { init(); m_labels = SGVector(num_lab); m_current_values=SGVector(num_lab); } -CDenseLabels::CDenseLabels(const CDenseLabels& orig) - : CLabels(orig), m_labels(orig.m_labels) +DenseLabels::DenseLabels(const DenseLabels& orig) + : Labels(orig), m_labels(orig.m_labels) { init(); } -CDenseLabels::CDenseLabels(CFile* loader) -: CLabels() +DenseLabels::DenseLabels(std::shared_ptr loader) +: Labels() { init(); load(loader); } -CDenseLabels::~CDenseLabels() +DenseLabels::~DenseLabels() { } -void CDenseLabels::init() +void DenseLabels::init() { SG_ADD(&m_labels, "labels", "The labels."); } -void CDenseLabels::set_to_one() +void DenseLabels::set_to_one() { set_to_const(1.0); } -void CDenseLabels::zero() +void DenseLabels::zero() { set_to_const(0.0); } -void CDenseLabels::set_to_const(float64_t c) +void DenseLabels::set_to_const(float64_t c) { ASSERT(m_labels.vector) index_t subset_size=get_num_labels(); @@ -73,7 +73,7 @@ void CDenseLabels::set_to_const(float64_t c) } } -void CDenseLabels::set_labels(SGVector v) +void DenseLabels::set_labels(SGVector v) { if (m_subset_stack->has_subsets()) error("A subset is set, cannot set labels"); @@ -81,7 +81,7 @@ void CDenseLabels::set_labels(SGVector v) m_labels = v; } -SGVector CDenseLabels::get_labels() const +SGVector DenseLabels::get_labels() const { if (m_subset_stack->has_subsets()) return get_labels_copy(); @@ -89,7 +89,7 @@ SGVector CDenseLabels::get_labels() const return m_labels; } -SGVector CDenseLabels::get_labels_copy() const +SGVector DenseLabels::get_labels_copy() const { if (!m_subset_stack->has_subsets()) return m_labels.clone(); @@ -104,7 +104,7 @@ SGVector CDenseLabels::get_labels_copy() const return result; } -SGVector CDenseLabels::get_int_labels() +SGVector DenseLabels::get_int_labels() const { SGVector intlab(get_num_labels()); @@ -114,7 +114,7 @@ SGVector CDenseLabels::get_int_labels() return intlab; } -void CDenseLabels::set_int_labels(SGVector lab) +void DenseLabels::set_int_labels(SGVector lab) { if (m_subset_stack->has_subsets()) error("set_int_labels() is not possible on subset"); @@ -126,7 +126,7 @@ void CDenseLabels::set_int_labels(SGVector lab) } #if !defined(SWIGJAVA) && !defined(SWIGCSHARP) -void CDenseLabels::set_int_labels(SGVector lab) +void DenseLabels::set_int_labels(SGVector lab) { if (m_subset_stack->has_subsets()) error("set_int_labels() is not possible on subset"); @@ -138,24 +138,24 @@ void CDenseLabels::set_int_labels(SGVector lab) } #endif -bool CDenseLabels::is_valid() const +bool DenseLabels::is_valid() const { return (m_labels.data() != nullptr) && (m_labels.size() > 0); } -void CDenseLabels::ensure_valid(const char* context) +void DenseLabels::ensure_valid(const char* context) { require(is_valid(), "Labels cannot be empty!"); } -void CDenseLabels::load(CFile* loader) +void DenseLabels::load(std::shared_ptr loader) { remove_subset(); m_labels = SGVector(); m_labels.load(loader); } -void CDenseLabels::save(CFile* writer) +void DenseLabels::save(std::shared_ptr writer) { if (m_subset_stack->has_subsets()) error("save() is not possible on subset"); @@ -163,7 +163,7 @@ void CDenseLabels::save(CFile* writer) m_labels.save(writer); } -bool CDenseLabels::set_label(int32_t idx, float64_t label) +bool DenseLabels::set_label(int32_t idx, float64_t label) { int32_t real_num=m_subset_stack->subset_idx_conversion(idx); if (m_labels.vector && real_numsubset_idx_conversion(idx); if (m_labels.vector && real_numsubset_idx_conversion(idx); ASSERT(m_labels.vector && idxsubset_idx_conversion(idx); ASSERT(m_labels.vector && idxhas_subsets() ? m_subset_stack->get_size() : m_labels.vlen; diff --git a/src/shogun/labels/DenseLabels.h b/src/shogun/labels/DenseLabels.h index 939d48c9712..477711f5f01 100644 --- a/src/shogun/labels/DenseLabels.h +++ b/src/shogun/labels/DenseLabels.h @@ -18,8 +18,8 @@ namespace shogun { - class CFile; - class CBinaryLabels; + class File; + class BinaryLabels; /** @brief Dense integer or floating point labels * @@ -31,32 +31,32 @@ namespace shogun * label * (vectors). */ - class CDenseLabels : public CLabels + class DenseLabels : public Labels { public: /** default constructor */ - CDenseLabels(); + DenseLabels(); /** constructor * * @param num_labels number of labels */ - CDenseLabels(int32_t num_labels); + DenseLabels(int32_t num_labels); /** copy constructor * * @param orig The dense labels to copy */ - CDenseLabels(const CDenseLabels& orig); + DenseLabels(const DenseLabels& orig); /** constructor * * @param loader File object via which to load data */ - CDenseLabels(CFile* loader); + DenseLabels(std::shared_ptr loader); /** destructor */ - virtual ~CDenseLabels() override; + virtual ~DenseLabels() override; virtual bool is_valid() const override; @@ -74,7 +74,7 @@ namespace shogun * * @param loader File object via which to load data */ - virtual void load(CFile* loader); + virtual void load(std::shared_ptr loader); /** save labels to file * @@ -82,7 +82,7 @@ namespace shogun * * @param writer File object via which to save data */ - virtual void save(CFile* writer); + virtual void save(std::shared_ptr writer); /** set label * @@ -144,7 +144,7 @@ namespace shogun * @param idx index of label to get * @return INT value of label */ - int32_t get_int_label(int32_t idx); + int32_t get_int_label(int32_t idx) const; /** Gets a copy of the labels. * @@ -237,7 +237,7 @@ namespace shogun * * @return INT labels */ - SGVector get_int_labels(); + SGVector get_int_labels() const; /** set INT labels * @@ -283,7 +283,7 @@ namespace shogun * is set */ template <> -inline SGVector CDenseLabels::get_labels_t() +inline SGVector DenseLabels::get_labels_t() { if (m_subset_stack->has_subsets()) return get_labels_copy_t(); diff --git a/src/shogun/labels/FactorGraphLabels.cpp b/src/shogun/labels/FactorGraphLabels.cpp index 46cee504770..d30cddbf969 100644 --- a/src/shogun/labels/FactorGraphLabels.cpp +++ b/src/shogun/labels/FactorGraphLabels.cpp @@ -2,9 +2,9 @@ using namespace shogun; -CFactorGraphObservation::CFactorGraphObservation(SGVector observed_state, +FactorGraphObservation::FactorGraphObservation(SGVector observed_state, SGVector loss_weights) - : CStructuredData(), m_observed_state(observed_state) + : StructuredData(), m_observed_state(observed_state) { if (loss_weights.size() == 0) { @@ -15,17 +15,17 @@ CFactorGraphObservation::CFactorGraphObservation(SGVector observed_stat set_loss_weights(loss_weights); } -SGVector CFactorGraphObservation::get_data() const +SGVector FactorGraphObservation::get_data() const { return m_observed_state; } -SGVector CFactorGraphObservation::get_loss_weights() const +SGVector FactorGraphObservation::get_loss_weights() const { return m_loss_weights; } -void CFactorGraphObservation::set_loss_weights(SGVector loss_weights) +void FactorGraphObservation::set_loss_weights(SGVector loss_weights) { require(loss_weights.size() == m_observed_state.size(), "{}::set_loss_weights(): \ loss_weights should be the same length as observed_states", get_name()); @@ -35,21 +35,21 @@ void CFactorGraphObservation::set_loss_weights(SGVector loss_weights) //------------------------------------------------------------------- -CFactorGraphLabels::CFactorGraphLabels() -: CStructuredLabels() +FactorGraphLabels::FactorGraphLabels() +: StructuredLabels() { } -CFactorGraphLabels::CFactorGraphLabels(int32_t num_labels) -: CStructuredLabels(num_labels) +FactorGraphLabels::FactorGraphLabels(int32_t num_labels) +: StructuredLabels(num_labels) { init(); } -CFactorGraphLabels::~CFactorGraphLabels() +FactorGraphLabels::~FactorGraphLabels() { } -void CFactorGraphLabels::init() +void FactorGraphLabels::init() { } diff --git a/src/shogun/labels/FactorGraphLabels.h b/src/shogun/labels/FactorGraphLabels.h index 046e7a214f4..e59017156b2 100644 --- a/src/shogun/labels/FactorGraphLabels.h +++ b/src/shogun/labels/FactorGraphLabels.h @@ -17,39 +17,39 @@ namespace shogun { -class CFactorGraphLabels; +class FactorGraphLabels; -/** @brief Class CFactorGraphObservation is used as +/** @brief Class FactorGraphObservation is used as * the structured output */ -class CFactorGraphObservation : public CStructuredData +class FactorGraphObservation : public StructuredData { public: /** data type */ STRUCTURED_DATA_TYPE(SDT_FACTOR_GRAPH); /** default constructor */ - CFactorGraphObservation() : CStructuredData() { } + FactorGraphObservation() : StructuredData() { } /** constructor discrete labeling observation * * @param observed_state Discrete labeling of a set of variables. * @param loss_weights weighted loss for each variable */ - CFactorGraphObservation(SGVector observed_state, + FactorGraphObservation(SGVector observed_state, SGVector loss_weights); - ~CFactorGraphObservation() { } + ~FactorGraphObservation() { } /** helper method used to specialize a base class instance * - * @param base_data its dynamic type must be CFactorGraphObservation + * @param base_data its dynamic type must be FactorGraphObservation */ - static CFactorGraphObservation* obtain_from_generic(CStructuredData* base_data) + static std::shared_ptr obtain_from_generic(std::shared_ptr base_data) { if ( base_data->get_structured_data_type() == SDT_FACTOR_GRAPH ) - return (CFactorGraphObservation*) base_data; + return std::static_pointer_cast(base_data); else - error("base_data must be of dynamic type CFactorGraphObservation"); + error("base_data must be of dynamic type FactorGraphObservation"); return NULL; } @@ -80,22 +80,22 @@ class CFactorGraphObservation : public CStructuredData /** @brief Class FactorGraphLabels used e.g. in the application of Structured Output * (SO) learning with the FactorGraphModel. Each of the labels is represented by a - * graph. Each label is of type CFactorGraphObservation and all of them are stored in - * a CDynamicObjectArray. */ -class CFactorGraphLabels : public CStructuredLabels + * graph. Each label is of type FactorGraphObservation and all of them are stored in + * a DynamicObjectArray. */ +class FactorGraphLabels : public StructuredLabels { public: /** default constructor */ - CFactorGraphLabels(); + FactorGraphLabels(); /** standard constructor * * @param num_labels number of labels */ - CFactorGraphLabels(int32_t num_labels); + FactorGraphLabels(int32_t num_labels); /** destructor */ - virtual ~CFactorGraphLabels(); + virtual ~FactorGraphLabels(); /** @return object name */ virtual const char* get_name() const { return "FactorGraphLabels"; } @@ -104,7 +104,7 @@ class CFactorGraphLabels : public CStructuredLabels /** internal initialization */ void init(); -}; /* CFactorGraphLabels */ +}; /* FactorGraphLabels */ } /* namespace shogun */ diff --git a/src/shogun/labels/Labels.cpp b/src/shogun/labels/Labels.cpp index 78e61f71ddc..e83fb21971c 100644 --- a/src/shogun/labels/Labels.cpp +++ b/src/shogun/labels/Labels.cpp @@ -14,74 +14,74 @@ using namespace shogun; -CLabels::CLabels() - : CSGObject() +Labels::Labels() + : SGObject() { init(); } -CLabels::CLabels(const CLabels& orig) - : CSGObject(orig), m_current_values(orig.m_current_values) +Labels::Labels(const Labels& orig) + : SGObject(orig), m_current_values(orig.m_current_values) { init(); if (orig.m_subset_stack != NULL) { - SG_UNREF(m_subset_stack); - m_subset_stack = new CSubsetStack(*orig.m_subset_stack); - SG_REF(m_subset_stack); + + m_subset_stack = std::make_shared(*orig.m_subset_stack); + } } -CLabels::~CLabels() +Labels::~Labels() { - SG_UNREF(m_subset_stack); + } -void CLabels::init() +void Labels::init() { - SG_ADD((CSGObject **)&m_subset_stack, "subset_stack", + SG_ADD((std::shared_ptr*)&m_subset_stack, "subset_stack", "Current subset stack"); SG_ADD( &m_current_values, "current_values", "current active value vector"); - m_subset_stack = new CSubsetStack(); - SG_REF(m_subset_stack); + m_subset_stack = std::make_shared(); + } -void CLabels::add_subset(SGVector subset) +void Labels::add_subset(SGVector subset) { m_subset_stack->add_subset(subset); } -void CLabels::add_subset_in_place(SGVector subset) +void Labels::add_subset_in_place(SGVector subset) { m_subset_stack->add_subset_in_place(subset); } -void CLabels::remove_subset() +void Labels::remove_subset() { m_subset_stack->remove_subset(); } -void CLabels::remove_all_subsets() +void Labels::remove_all_subsets() { m_subset_stack->remove_all_subsets(); } -CSubsetStack* CLabels::get_subset_stack() +std::shared_ptr Labels::get_subset_stack() { - SG_REF(m_subset_stack); + return m_subset_stack; } -float64_t CLabels::get_value(int32_t idx) +float64_t Labels::get_value(int32_t idx) { ASSERT(m_current_values.vector && idx < get_num_labels()) int32_t real_num = m_subset_stack->subset_idx_conversion(idx); return m_current_values.vector[real_num]; } -void CLabels::set_value(float64_t value, int32_t idx) +void Labels::set_value(float64_t value, int32_t idx) { require(m_current_values.vector, "{}::set_value({}, {}): No values vector" @@ -93,7 +93,7 @@ void CLabels::set_value(float64_t value, int32_t idx) m_current_values.vector[real_num] = value; } -void CLabels::set_values(SGVector values) +void Labels::set_values(SGVector values) { if (m_current_values.vlen != 0 && m_current_values.vlen != get_num_labels()) { @@ -105,7 +105,7 @@ void CLabels::set_values(SGVector values) m_current_values = values; } -SGVector CLabels::get_values() const +SGVector Labels::get_values() const { return m_current_values; } diff --git a/src/shogun/labels/Labels.h b/src/shogun/labels/Labels.h index dad19e83ad4..72e6efcbcec 100644 --- a/src/shogun/labels/Labels.h +++ b/src/shogun/labels/Labels.h @@ -12,7 +12,6 @@ #include #include -#include #include #include #include @@ -20,7 +19,7 @@ namespace shogun { - class CBinaryLabels; + class BinaryLabels; /** @brief The class Labels models labels, i.e. class assignments of * objects. @@ -45,17 +44,17 @@ namespace shogun * add_subset_in_place(). * The latter does not allow to remove such modifications one-by-one. */ - class CLabels : public CSGObject + class Labels : public SGObject { public: /** default constructor */ - CLabels(); + Labels(); /** copy constructor */ - CLabels(const CLabels& orig); + Labels(const Labels& orig); /** destructor */ - virtual ~CLabels(); + virtual ~Labels(); virtual bool is_valid() const = 0; @@ -110,7 +109,7 @@ namespace shogun /** * @return subset stack */ - virtual CSubsetStack* get_subset_stack(); + virtual std::shared_ptr get_subset_stack(); /** set the confidence value for a particular label * @@ -143,14 +142,14 @@ namespace shogun * * @return labels object */ - virtual CLabels* duplicate() const + virtual std::shared_ptr duplicate() const { not_implemented(SOURCE_LOCATION);; return nullptr; } #ifndef SWIG // SWIG should skip this part - virtual CLabels* shallow_subset_copy() + virtual std::shared_ptr shallow_subset_copy() { not_implemented(SOURCE_LOCATION);; return NULL; @@ -162,7 +161,7 @@ namespace shogun protected: /** subset class to enable subset support for this class */ - CSubsetStack* m_subset_stack; + std::shared_ptr m_subset_stack; /** current active value vector */ SGVector m_current_values; diff --git a/src/shogun/labels/LatentLabels.cpp b/src/shogun/labels/LatentLabels.cpp index ad3be2db208..fdb2e2b5698 100644 --- a/src/shogun/labels/LatentLabels.cpp +++ b/src/shogun/labels/LatentLabels.cpp @@ -9,22 +9,22 @@ using namespace shogun; -CLatentLabels::CLatentLabels() - : CLabels() +LatentLabels::LatentLabels() + : Labels() { init(); } -CLatentLabels::CLatentLabels(int32_t num_samples) - : CLabels() +LatentLabels::LatentLabels(int32_t num_samples) + : Labels() { init(); - m_latent_labels = new CDynamicObjectArray(num_samples); - SG_REF(m_latent_labels); + m_latent_labels = std::make_shared(num_samples); + } -CLatentLabels::CLatentLabels(CLabels* labels) - : CLabels() +LatentLabels::LatentLabels(std::shared_ptr labels) + : Labels() { init(); set_labels(labels); @@ -33,46 +33,46 @@ CLatentLabels::CLatentLabels(CLabels* labels) if (m_labels) num_labels = m_labels->get_num_labels(); - m_latent_labels = new CDynamicObjectArray(num_labels); - SG_REF(m_latent_labels); + m_latent_labels = std::make_shared(num_labels); + } -CLatentLabels::~CLatentLabels() +LatentLabels::~LatentLabels() { - SG_UNREF(m_latent_labels); - SG_UNREF(m_labels); + + } -void CLatentLabels::init() +void LatentLabels::init() { - SG_ADD((CSGObject**) &m_latent_labels, "m_latent_labels", "The latent labels"); - SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels"); + SG_ADD((std::shared_ptr*) &m_latent_labels, "m_latent_labels", "The latent labels"); + SG_ADD((std::shared_ptr*) &m_labels, "m_labels", "The labels"); m_latent_labels = NULL; m_labels = NULL; } -CDynamicObjectArray* CLatentLabels::get_latent_labels() const +std::shared_ptr LatentLabels::get_latent_labels() const { - SG_REF(m_latent_labels); + return m_latent_labels; } -CData* CLatentLabels::get_latent_label(int32_t idx) +std::shared_ptr LatentLabels::get_latent_label(int32_t idx) { ASSERT(m_latent_labels != NULL) if (idx < 0 || idx >= get_num_labels()) error("Out of index!"); - return (CData*) m_latent_labels->get_element(idx); + return std::static_pointer_cast( m_latent_labels->get_element(idx)); } -void CLatentLabels::add_latent_label(CData* label) +void LatentLabels::add_latent_label(std::shared_ptr label) { ASSERT(m_latent_labels != NULL) m_latent_labels->push_back(label); } -bool CLatentLabels::set_latent_label(int32_t idx, CData* label) +bool LatentLabels::set_latent_label(int32_t idx, std::shared_ptr label) { if (idx < get_num_labels()) { @@ -84,17 +84,17 @@ bool CLatentLabels::set_latent_label(int32_t idx, CData* label) } } -bool CLatentLabels::is_valid() const +bool LatentLabels::is_valid() const { return m_latent_labels != nullptr; } -void CLatentLabels::ensure_valid(const char* context) +void LatentLabels::ensure_valid(const char* context) { require(is_valid(), "Empty labels provided!"); } -int32_t CLatentLabels::get_num_labels() const +int32_t LatentLabels::get_num_labels() const { if (!m_latent_labels || !m_labels) return 0; @@ -105,16 +105,16 @@ int32_t CLatentLabels::get_num_labels() const return num_labels; } -void CLatentLabels::set_labels(CLabels* labels) +void LatentLabels::set_labels(std::shared_ptr labels) { - SG_REF(labels); - SG_UNREF(m_labels); + + m_labels = labels; } -CLabels* CLatentLabels::get_labels() const +std::shared_ptr LatentLabels::get_labels() const { - SG_REF(m_labels); + return m_labels; } diff --git a/src/shogun/labels/LatentLabels.h b/src/shogun/labels/LatentLabels.h index 113a9696341..0db5793631c 100644 --- a/src/shogun/labels/LatentLabels.h +++ b/src/shogun/labels/LatentLabels.h @@ -20,45 +20,45 @@ namespace shogun * As latent labels always depends on the given application, this class * only defines the API that the user has to implement for latent labels. */ - class CLatentLabels : public CLabels + class LatentLabels : public Labels { public: /** default ctor */ - CLatentLabels(); + LatentLabels(); /** constructor * * @param num_samples the number of labels */ - CLatentLabels(int32_t num_samples); + LatentLabels(int32_t num_samples); /** constructor * * @param labels the (y_i) labels */ - CLatentLabels(CLabels* labels); + LatentLabels(std::shared_ptr labels); /** destructor */ - virtual ~CLatentLabels() override; + virtual ~LatentLabels() override; /** get all the stored latent labels * - * @return the CDynamicObjectArray with the latent labels in it + * @return the DynamicObjectArray with the latent labels in it */ - CDynamicObjectArray* get_latent_labels() const; + std::shared_ptr get_latent_labels() const; /** get the latent label of a given example * * @param idx index of the label * @return the user defined latent label */ - CData* get_latent_label(int32_t idx); + std::shared_ptr get_latent_label(int32_t idx); /** append the latent label * * @param label latent label */ - void add_latent_label(CData* label); + void add_latent_label(std::shared_ptr label); /** set latent label at a given index * @@ -66,7 +66,7 @@ namespace shogun * @param label the latent label * @return TRUE if success, FALSE otherwise */ - bool set_latent_label(int32_t idx, CData* label); + bool set_latent_label(int32_t idx, std::shared_ptr label); virtual bool is_valid() const override; @@ -100,19 +100,19 @@ namespace shogun * * @param labels the labels (y_i) */ - void set_labels(CLabels* labels); + void set_labels(std::shared_ptr labels); /** get the labels (y_i) * * @return the labels (y_i) */ - CLabels* get_labels() const; + std::shared_ptr get_labels() const; protected: - /** the of CData, the latent labels (h_i) */ - CDynamicObjectArray* m_latent_labels; + /** the of Data, the latent labels (h_i) */ + std::shared_ptr m_latent_labels; /** the labels (y_i) */ - CLabels* m_labels; + std::shared_ptr m_labels; private: /** initalize the values to default values */ diff --git a/src/shogun/labels/MulticlassLabels.cpp b/src/shogun/labels/MulticlassLabels.cpp index ee2eb5157dc..33cfb208c94 100644 --- a/src/shogun/labels/MulticlassLabels.cpp +++ b/src/shogun/labels/MulticlassLabels.cpp @@ -5,29 +5,29 @@ using namespace shogun; -CMulticlassLabels::CMulticlassLabels() : CDenseLabels() +MulticlassLabels::MulticlassLabels() : DenseLabels() { init(); } -CMulticlassLabels::CMulticlassLabels(int32_t num_labels) : CDenseLabels(num_labels) +MulticlassLabels::MulticlassLabels(int32_t num_labels) : DenseLabels(num_labels) { init(); } -CMulticlassLabels::CMulticlassLabels(const SGVector src) : CDenseLabels() +MulticlassLabels::MulticlassLabels(const SGVector src) : DenseLabels() { init(); set_labels(src); } -CMulticlassLabels::CMulticlassLabels(CFile* loader) : CDenseLabels(loader) +MulticlassLabels::MulticlassLabels(std::shared_ptr loader) : DenseLabels(loader) { init(); } -CMulticlassLabels::CMulticlassLabels(CBinaryLabels* labels) - : CDenseLabels(labels->get_num_labels()) +MulticlassLabels::MulticlassLabels(std::shared_ptr labels) + : DenseLabels(labels->get_num_labels()) { init(); @@ -35,23 +35,23 @@ CMulticlassLabels::CMulticlassLabels(CBinaryLabels* labels) m_labels[i] = (labels->get_label(i) == 1 ? 1 : 0); } -CMulticlassLabels::CMulticlassLabels(const CMulticlassLabels& orig) - : CDenseLabels(orig) +MulticlassLabels::MulticlassLabels(const MulticlassLabels& orig) + : DenseLabels(orig) { init(); m_multiclass_confidences = orig.m_multiclass_confidences; } -CMulticlassLabels::~CMulticlassLabels() +MulticlassLabels::~MulticlassLabels() { } -void CMulticlassLabels::init() +void MulticlassLabels::init() { m_multiclass_confidences=SGMatrix(); } -void CMulticlassLabels::set_multiclass_confidences(int32_t i, +void MulticlassLabels::set_multiclass_confidences(int32_t i, SGVector confidences) { require(confidences.size()==m_multiclass_confidences.num_rows, @@ -61,7 +61,7 @@ void CMulticlassLabels::set_multiclass_confidences(int32_t i, m_multiclass_confidences.set_column(i, confidences); } -SGVector CMulticlassLabels::get_multiclass_confidences(int32_t i) +SGVector MulticlassLabels::get_multiclass_confidences(int32_t i) { SGVector confs(m_multiclass_confidences.num_rows); for (index_t j=0; j CMulticlassLabels::get_multiclass_confidences(int32_t i) return confs; } -void CMulticlassLabels::allocate_confidences_for(int32_t n_classes) +void MulticlassLabels::allocate_confidences_for(int32_t n_classes) { int32_t n_labels = m_labels.size(); require(n_labels!=0,"{}::allocate_confidences_for(): There should be " @@ -79,7 +79,7 @@ void CMulticlassLabels::allocate_confidences_for(int32_t n_classes) m_multiclass_confidences = SGMatrix(n_classes,n_labels); } -SGVector CMulticlassLabels::get_confidences_for_class(int32_t i) +SGVector MulticlassLabels::get_confidences_for_class(int32_t i) { require( (m_multiclass_confidences.num_rows != 0) && @@ -93,9 +93,9 @@ SGVector CMulticlassLabels::get_confidences_for_class(int32_t i) return confs; } -bool CMulticlassLabels::is_valid() const +bool MulticlassLabels::is_valid() const { - if (!CDenseLabels::is_valid()) + if (!DenseLabels::is_valid()) return false; int32_t subset_size=get_num_labels(); @@ -112,18 +112,18 @@ bool CMulticlassLabels::is_valid() const return true; } -void CMulticlassLabels::ensure_valid(const char* context) +void MulticlassLabels::ensure_valid(const char* context) { require(is_valid(), "Multiclass Labels must be in range " "[0,...,num_classes] and integers!"); } -ELabelType CMulticlassLabels::get_label_type() const +ELabelType MulticlassLabels::get_label_type() const { return LT_MULTICLASS; } -CBinaryLabels* CMulticlassLabels::get_binary_for_class(int32_t i) +std::shared_ptr MulticlassLabels::get_binary_for_class(int32_t i) { SGVector binary_labels(get_num_labels()); @@ -149,10 +149,10 @@ CBinaryLabels* CMulticlassLabels::get_binary_for_class(int32_t i) binary_labels[k] = label == i ? +1.0 : -1.0; } } - return new CBinaryLabels(binary_labels); + return std::make_shared(binary_labels); } -SGVector CMulticlassLabels::get_unique_labels() +SGVector MulticlassLabels::get_unique_labels() { /* extract all labels (copy because of possible subset) */ SGVector unique_labels=get_labels_copy(); @@ -166,49 +166,46 @@ SGVector CMulticlassLabels::get_unique_labels() } -int32_t CMulticlassLabels::get_num_classes() +int32_t MulticlassLabels::get_num_classes() { SGVector unique=get_unique_labels(); return unique.vlen; } -CLabels* CMulticlassLabels::shallow_subset_copy() +std::shared_ptr MulticlassLabels::shallow_subset_copy() { - CLabels* shallow_copy_labels=NULL; SGVector shallow_copy_vector(m_labels); - shallow_copy_labels=new CMulticlassLabels(m_labels.size()); - SG_REF(shallow_copy_labels); - ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector); + auto shallow_copy_labels=std::make_shared(m_labels.size()); + + shallow_copy_labels->set_labels(shallow_copy_vector); if (m_subset_stack->has_subsets()) shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); return shallow_copy_labels; } -CMulticlassLabels* CMulticlassLabels::obtain_from_generic(CLabels* labels) +std::shared_ptr MulticlassLabels::obtain_from_generic(std::shared_ptr labels) { if (labels == NULL) return NULL; if (labels->get_label_type() != LT_MULTICLASS) { - error("The Labels passed cannot be casted to CMulticlassLabels!"); + error("The Labels passed cannot be casted to MulticlassLabels!"); return NULL; } - CMulticlassLabels* casted = dynamic_cast(labels); - SG_REF(casted) - return casted; + return std::dynamic_pointer_cast(labels); } -CLabels* CMulticlassLabels::duplicate() const +std::shared_ptr MulticlassLabels::duplicate() const { - return new CMulticlassLabels(*this); + return std::make_shared(*this); } namespace shogun { - SG_FORCED_INLINE Some to_multiclass(CDenseLabels* orig) + SG_FORCED_INLINE std::shared_ptr to_multiclass(std::shared_ptr orig) { auto result_vector = orig->get_labels(); std::set unique(result_vector.begin(), result_vector.end()); @@ -239,10 +236,10 @@ namespace shogun }); result_vector = converted; } - return some(result_vector); + return std::make_shared(result_vector); } - Some multiclass_labels(CLabels* orig) + std::shared_ptr multiclass_labels(std::shared_ptr orig) { require(orig, "No labels provided."); try @@ -250,10 +247,9 @@ namespace shogun switch (orig->get_label_type()) { case LT_MULTICLASS: - return Some::from_raw( - (CMulticlassLabels*)orig); + return std::static_pointer_cast(orig); case LT_BINARY: - return to_multiclass((CBinaryLabels*)orig); + return to_multiclass(std::static_pointer_cast(orig)); default: not_implemented(SOURCE_LOCATION); } @@ -265,6 +261,6 @@ namespace shogun orig->get_name(), e.what()); } - return Some::from_raw(nullptr); + return nullptr; } } // namespace shogun diff --git a/src/shogun/labels/MulticlassLabels.h b/src/shogun/labels/MulticlassLabels.h index e41d71fbc9b..74e7892a03e 100644 --- a/src/shogun/labels/MulticlassLabels.h +++ b/src/shogun/labels/MulticlassLabels.h @@ -1,9 +1,9 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Soeren Sonnenburg, Sergey Lisitsyn, - * Saurabh Mahindre, Olivier NGuyen, Thoralf Klein, Giovanni De Toni, - * Heiko Strathmann, Michele Mazzoni, Evgeniy Andreev, Yuyu Zhang, + * Authors: Fernando Iglesias, Soeren Sonnenburg, Sergey Lisitsyn, + * Saurabh Mahindre, Olivier NGuyen, Thoralf Klein, Giovanni De Toni, + * Heiko Strathmann, Michele Mazzoni, Evgeniy Andreev, Yuyu Zhang, * Chiyuan Zhang, Viktor Gal, Bjoern Esser */ @@ -21,39 +21,39 @@ namespace shogun { - class CFile; - class CBinaryLabels; - class CMulticlassLabels; - class CDenseLabels; + class File; + class BinaryLabels; + class MulticlassLabels; + class DenseLabels; /** @brief Multiclass Labels for multi-class classification * * valid values for labels are 0...nr_classes-1 */ -class CMulticlassLabels : public CDenseLabels +class MulticlassLabels : public DenseLabels { public: /** default constructor */ - CMulticlassLabels(); + MulticlassLabels(); /** constructor * * @param num_labels number of labels */ - CMulticlassLabels(int32_t num_labels); + MulticlassLabels(int32_t num_labels); /** constructor * * @param src labels to set */ - CMulticlassLabels(SGVector src); + MulticlassLabels(SGVector src); /** constructor * * @param loader File object via which to load data */ - CMulticlassLabels(CFile* loader); + MulticlassLabels(std::shared_ptr loader); /** * Convert binary labels to multiclass labels, @@ -61,13 +61,13 @@ class CMulticlassLabels : public CDenseLabels * * @param labels Binary labels */ - CMulticlassLabels(CBinaryLabels* labels); + MulticlassLabels(std::shared_ptr labels); /** copy constructor */ - CMulticlassLabels(const CMulticlassLabels& orig); + MulticlassLabels(const MulticlassLabels& orig); /** destructor */ - ~CMulticlassLabels(); + ~MulticlassLabels(); /** Make sure the label is valid, otherwise raise SG_ERROR. * @@ -85,14 +85,14 @@ class CMulticlassLabels : public CDenseLabels */ virtual ELabelType get_label_type() const; - virtual CLabels* duplicate() const; + virtual std::shared_ptr duplicate() const; /** returns labels containing +1 at positions with ith class * and -1 at other positions * @param i index of class * @return new binary labels */ - CBinaryLabels* get_binary_for_class(int32_t i); + std::shared_ptr get_binary_for_class(int32_t i); /** get unique labels (new SGVector) * @@ -143,14 +143,14 @@ class CMulticlassLabels : public CDenseLabels /** @return object name */ virtual const char* get_name() const { return "MulticlassLabels"; } #ifndef SWIG // SWIG should skip this part - virtual CLabels* shallow_subset_copy(); + virtual std::shared_ptr shallow_subset_copy(); #endif /** * Cast a generic label object to a multiclass one - * @param labels generic CLabels instance + * @param labels generic Labels instance * @return the casted pointer (already SG_REF'ed) */ - static CMulticlassLabels* obtain_from_generic(CLabels* labels); + static std::shared_ptr obtain_from_generic(std::shared_ptr labels); private: /** initialises and register parameters */ @@ -163,7 +163,7 @@ class CMulticlassLabels : public CDenseLabels }; #ifndef SWIG -Some multiclass_labels(CLabels* orig); +std::shared_ptr multiclass_labels(std::shared_ptr orig); #endif // SWIG } #endif diff --git a/src/shogun/labels/MultilabelLabels.cpp b/src/shogun/labels/MultilabelLabels.cpp index a0a5ef886fb..4227708a57a 100644 --- a/src/shogun/labels/MultilabelLabels.cpp +++ b/src/shogun/labels/MultilabelLabels.cpp @@ -34,35 +34,35 @@ using namespace shogun; -CMultilabelLabels::CMultilabelLabels() - : CLabels() +MultilabelLabels::MultilabelLabels() + : Labels() { init(0, 1); } -CMultilabelLabels::CMultilabelLabels(int32_t num_classes) - : CLabels() +MultilabelLabels::MultilabelLabels(int32_t num_classes) + : Labels() { init(0, num_classes); } -CMultilabelLabels::CMultilabelLabels(int32_t num_labels, int32_t num_classes) - : CLabels() +MultilabelLabels::MultilabelLabels(int32_t num_labels, int32_t num_classes) + : Labels() { init(num_labels, num_classes); } -CMultilabelLabels::~CMultilabelLabels() +MultilabelLabels::~MultilabelLabels() { delete[] m_labels; } void -CMultilabelLabels::init(int32_t num_labels, int32_t num_classes) +MultilabelLabels::init(int32_t num_labels, int32_t num_classes) { require(num_labels >= 0, "num_labels={} should be >= 0", num_labels); require(num_classes > 0, "num_classes={} should be > 0", num_classes); @@ -71,7 +71,7 @@ CMultilabelLabels::init(int32_t num_labels, int32_t num_classes) // Can be disabled as SG_ADD(&m_num_labels, "m_num_labels", "number of labels"); SG_ADD(&m_num_classes, "m_num_classes", "number of classes"); - // SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels"); + // SG_ADD((std::shared_ptr*) &m_labels, "m_labels", "The labels"); // Can only be enabled after this issue has been solved: @@ -91,11 +91,11 @@ CMultilabelLabels::init(int32_t num_labels, int32_t num_classes) m_labels = new SGVector [m_num_labels]; } -bool CMultilabelLabels::is_valid() const +bool MultilabelLabels::is_valid() const { for (int32_t label_j = 0; label_j < get_num_labels(); label_j++) { - if (!CMath::is_sorted(m_labels[label_j])) + if (!Math::is_sorted(m_labels[label_j])) return false; int32_t c_len = m_labels[label_j].vlen; @@ -113,7 +113,7 @@ bool CMultilabelLabels::is_valid() const return true; } -void CMultilabelLabels::ensure_valid(const char* context) +void MultilabelLabels::ensure_valid(const char* context) { require( is_valid(), @@ -122,21 +122,21 @@ void CMultilabelLabels::ensure_valid(const char* context) int32_t -CMultilabelLabels::get_num_labels() const +MultilabelLabels::get_num_labels() const { return m_num_labels; } int32_t -CMultilabelLabels::get_num_classes() const +MultilabelLabels::get_num_classes() const { return m_num_classes; } void -CMultilabelLabels::set_labels(SGVector * labels) +MultilabelLabels::set_labels(SGVector * labels) { for (int32_t label_j = 0; label_j < m_num_labels; label_j++) { @@ -146,7 +146,7 @@ CMultilabelLabels::set_labels(SGVector * labels) } -SGVector ** CMultilabelLabels::get_class_labels() const +SGVector ** MultilabelLabels::get_class_labels() const { SGVector ** labels_list = SG_MALLOC(SGVector *, get_num_classes()); @@ -204,7 +204,7 @@ SGVector ** CMultilabelLabels::get_class_labels() const return labels_list; } -SGMatrix CMultilabelLabels::get_labels() const +SGMatrix MultilabelLabels::get_labels() const { if (m_num_labels==0) return SGMatrix(); @@ -223,7 +223,7 @@ SGMatrix CMultilabelLabels::get_labels() const return labels; } -SGVector CMultilabelLabels::get_label(int32_t j) +SGVector MultilabelLabels::get_label(int32_t j) { require(j < get_num_labels(), "label index j={} should be within [{},{}[", @@ -233,7 +233,7 @@ SGVector CMultilabelLabels::get_label(int32_t j) template -SGVector CMultilabelLabels::to_dense +SGVector MultilabelLabels::to_dense (SGVector * sparse, int32_t dense_len, D d_true, D d_false) { SGVector dense(dense_len); @@ -250,15 +250,15 @@ SGVector CMultilabelLabels::to_dense template -SGVector CMultilabelLabels::to_dense +SGVector MultilabelLabels::to_dense (SGVector *, int32_t, int32_t, int32_t); template -SGVector CMultilabelLabels::to_dense +SGVector MultilabelLabels::to_dense (SGVector *, int32_t, float64_t, float64_t); void -CMultilabelLabels::set_label(int32_t j, SGVector label) +MultilabelLabels::set_label(int32_t j, SGVector label) { require(j < get_num_labels(), "label index j={} should be within [{},{}[", @@ -268,7 +268,7 @@ CMultilabelLabels::set_label(int32_t j, SGVector label) void -CMultilabelLabels::set_class_labels(SGVector ** labels_list) +MultilabelLabels::set_class_labels(SGVector ** labels_list) { int32_t * num_class_idx = SG_MALLOC(int32_t , get_num_labels()); for (int32_t label_j = 0; label_j < get_num_labels(); label_j++) @@ -329,7 +329,7 @@ CMultilabelLabels::set_class_labels(SGVector ** labels_list) void -CMultilabelLabels::display() const +MultilabelLabels::display() const { SGVector ** labels_list = get_class_labels(); io::print("printing {} binary label vectors for {} multilabels:\n", diff --git a/src/shogun/labels/MultilabelLabels.h b/src/shogun/labels/MultilabelLabels.h index f66657c24dc..1045f604a7b 100644 --- a/src/shogun/labels/MultilabelLabels.h +++ b/src/shogun/labels/MultilabelLabels.h @@ -48,27 +48,27 @@ namespace shogun * Labels are subsets of {0, ..., num_classes-1} */ -class CMultilabelLabels : public CLabels +class MultilabelLabels : public Labels { public: /** default constructor */ - CMultilabelLabels(); + MultilabelLabels(); /** constructor * * @param num_classes number of (binary) class assignments per label */ - CMultilabelLabels(int32_t num_classes); + MultilabelLabels(int32_t num_classes); /** constructor * * @param num_labels number of labels * @param num_classes number of (binary) class assignments per label */ - CMultilabelLabels(int32_t num_labels, int32_t num_classes); + MultilabelLabels(int32_t num_labels, int32_t num_classes); /** destructor */ - ~CMultilabelLabels(); + ~MultilabelLabels(); virtual bool is_valid() const override; diff --git a/src/shogun/labels/RegressionLabels.cpp b/src/shogun/labels/RegressionLabels.cpp index 245e0de6ff5..ec8234c2d3f 100644 --- a/src/shogun/labels/RegressionLabels.cpp +++ b/src/shogun/labels/RegressionLabels.cpp @@ -5,50 +5,48 @@ using namespace shogun; -CRegressionLabels::CRegressionLabels() : CDenseLabels() +RegressionLabels::RegressionLabels() : DenseLabels() { } -CRegressionLabels::CRegressionLabels(int32_t num_labels) : CDenseLabels(num_labels) +RegressionLabels::RegressionLabels(int32_t num_labels) : DenseLabels(num_labels) { } -CRegressionLabels::CRegressionLabels(const SGVector src) : CDenseLabels() +RegressionLabels::RegressionLabels(const SGVector src) : DenseLabels() { set_labels(src); } -CRegressionLabels::CRegressionLabels(CFile* loader) : CDenseLabels(loader) +RegressionLabels::RegressionLabels(std::shared_ptr loader) : DenseLabels(loader) { } -ELabelType CRegressionLabels::get_label_type() const +ELabelType RegressionLabels::get_label_type() const { return LT_REGRESSION; } -CLabels* CRegressionLabels::shallow_subset_copy() +std::shared_ptr RegressionLabels::shallow_subset_copy() { - CLabels* shallow_copy_labels=NULL; SGVector shallow_copy_vector(m_labels); - shallow_copy_labels=new CRegressionLabels(m_labels.size()); - SG_REF(shallow_copy_labels); + auto shallow_copy_labels=std::make_shared(m_labels.size()); - ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector); + shallow_copy_labels->set_labels(shallow_copy_vector); if (m_subset_stack->has_subsets()) shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx()); return shallow_copy_labels; } -CLabels* CRegressionLabels::duplicate() const +std::shared_ptr RegressionLabels::duplicate() const { - return new CRegressionLabels(*this); + return std::make_shared(*this); } namespace shogun { - Some regression_labels(CLabels* orig) + std::shared_ptr regression_labels(std::shared_ptr orig) { require(orig, "No labels provided."); try @@ -56,11 +54,10 @@ namespace shogun switch (orig->get_label_type()) { case LT_REGRESSION: - return Some::from_raw( - orig->as()); + return std::static_pointer_cast(orig); case LT_BINARY: - return some( - orig->as()->get_labels()); + return std::make_shared( + (std::static_pointer_cast(orig))->get_labels()); default: not_implemented(SOURCE_LOCATION); } @@ -72,6 +69,6 @@ namespace shogun orig->get_name()); } - return Some::from_raw(nullptr); + return nullptr; } } // namespace shogun diff --git a/src/shogun/labels/RegressionLabels.h b/src/shogun/labels/RegressionLabels.h index ff5d468b345..27726cc6621 100644 --- a/src/shogun/labels/RegressionLabels.h +++ b/src/shogun/labels/RegressionLabels.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Saurabh Mahindre, Soeren Sonnenburg, Evgeniy Andreev, Yuyu Zhang, + * Authors: Saurabh Mahindre, Soeren Sonnenburg, Evgeniy Andreev, Yuyu Zhang, * Chiyuan Zhang, Fernando Iglesias, Sergey Lisitsyn */ @@ -18,9 +18,9 @@ namespace shogun { - class CFile; - class CRegressionLabels; - class CDenseLabels; + class File; + class RegressionLabels; + class DenseLabels; /** @brief Real Labels are real-valued labels * @@ -28,29 +28,29 @@ namespace shogun * * valid values for labels are all real-valued numbers. */ -class CRegressionLabels : public CDenseLabels +class RegressionLabels : public DenseLabels { public: /** default constructor */ - CRegressionLabels(); + RegressionLabels(); /** constructor * * @param num_labels number of labels */ - CRegressionLabels(int32_t num_labels); + RegressionLabels(int32_t num_labels); /** constructor * * @param src labels to set */ - CRegressionLabels(const SGVector src); + RegressionLabels(const SGVector src); /** constructor * * @param loader File object via which to load data */ - CRegressionLabels(CFile* loader); + RegressionLabels(std::shared_ptr loader); /** get label type * @@ -62,17 +62,17 @@ class CRegressionLabels : public CDenseLabels virtual const char* get_name() const { return "RegressionLabels"; } /** shallow-copy of the labels object - * @see CLabels::duplicate + * @see Labels::duplicate */ - virtual CLabels* duplicate() const; + virtual std::shared_ptr duplicate() const; #ifndef SWIG // SWIG should skip this part - virtual CLabels* shallow_subset_copy(); + virtual std::shared_ptr shallow_subset_copy(); #endif }; #ifndef SWIG -Some regression_labels(CLabels* orig); +std::shared_ptr regression_labels(std::shared_ptr orig); #endif // SWIG } #endif diff --git a/src/shogun/labels/StructuredLabels.cpp b/src/shogun/labels/StructuredLabels.cpp index 0668211c436..0657b5fc019 100644 --- a/src/shogun/labels/StructuredLabels.cpp +++ b/src/shogun/labels/StructuredLabels.cpp @@ -9,57 +9,57 @@ using namespace shogun; -CStructuredLabels::CStructuredLabels() -: CLabels() +StructuredLabels::StructuredLabels() +: Labels() { init(); } -CStructuredLabels::CStructuredLabels(int32_t num_labels) -: CLabels() +StructuredLabels::StructuredLabels(int32_t num_labels) +: Labels() { init(); - m_labels = new CDynamicObjectArray(num_labels); - SG_REF(m_labels); + m_labels = std::make_shared(num_labels); + } -CStructuredLabels::~CStructuredLabels() +StructuredLabels::~StructuredLabels() { - SG_UNREF(m_labels); + } -bool CStructuredLabels::is_valid() const +bool StructuredLabels::is_valid() const { return m_labels != nullptr; } -void CStructuredLabels::ensure_valid(const char* context) +void StructuredLabels::ensure_valid(const char* context) { require(is_valid(), "Non-valid StructuredLabels in {}", context); } -CDynamicObjectArray* CStructuredLabels::get_labels() const +std::shared_ptr StructuredLabels::get_labels() const { - SG_REF(m_labels); + return m_labels; } -CStructuredData* CStructuredLabels::get_label(int32_t idx) +std::shared_ptr StructuredLabels::get_label(int32_t idx) { - ensure_valid("CStructuredLabels::get_label(int32_t)"); + ensure_valid("StructuredLabels::get_label(int32_t)"); if ( idx < 0 || idx >= get_num_labels() ) error("Index must be inside [0, num_labels-1]"); - return (CStructuredData*) m_labels->get_element(idx); + return std::static_pointer_cast( m_labels->get_element(idx)); } -void CStructuredLabels::add_label(CStructuredData* label) +void StructuredLabels::add_label(std::shared_ptr label) { ensure_valid_sdt(label); m_labels->push_back(label); } -bool CStructuredLabels::set_label(int32_t idx, CStructuredData* label) +bool StructuredLabels::set_label(int32_t idx, std::shared_ptr label) { ensure_valid_sdt(label); int32_t real_idx = m_subset_stack->subset_idx_conversion(idx); @@ -74,7 +74,7 @@ bool CStructuredLabels::set_label(int32_t idx, CStructuredData* label) } } -int32_t CStructuredLabels::get_num_labels() const +int32_t StructuredLabels::get_num_labels() const { if ( m_labels == NULL ) return 0; @@ -82,15 +82,15 @@ int32_t CStructuredLabels::get_num_labels() const return m_labels->get_num_elements(); } -void CStructuredLabels::init() +void StructuredLabels::init() { - SG_ADD((CSGObject**) &m_labels, "m_labels", "The labels"); + SG_ADD((std::shared_ptr*) &m_labels, "m_labels", "The labels"); m_labels = NULL; m_sdt = SDT_UNKNOWN; } -void CStructuredLabels::ensure_valid_sdt(CStructuredData* label) +void StructuredLabels::ensure_valid_sdt(std::shared_ptr label) { if ( m_sdt == SDT_UNKNOWN ) { @@ -99,6 +99,6 @@ void CStructuredLabels::ensure_valid_sdt(CStructuredData* label) else { require(label->get_structured_data_type() == m_sdt, "All the labels must " - "belong to the same CStructuredData child class"); + "belong to the same StructuredData child class"); } } diff --git a/src/shogun/labels/StructuredLabels.h b/src/shogun/labels/StructuredLabels.h index 6f4b0abd86c..21edc7ff9a7 100644 --- a/src/shogun/labels/StructuredLabels.h +++ b/src/shogun/labels/StructuredLabels.h @@ -19,12 +19,12 @@ namespace shogun { /** @brief Base class of the labels used in Structured Output (SO) problems */ -class CStructuredLabels : public CLabels +class StructuredLabels : public Labels { public: /** default constructor */ - CStructuredLabels(); + StructuredLabels(); /** constructor * @@ -34,10 +34,10 @@ class CStructuredLabels : public CLabels * * @param num_labels number of labels to pre-allocate */ - CStructuredLabels(int32_t num_labels); + StructuredLabels(int32_t num_labels); /** destructor */ - virtual ~CStructuredLabels() override; + virtual ~StructuredLabels() override; virtual bool is_valid() const override; @@ -57,7 +57,7 @@ class CStructuredLabels : public CLabels * * @param label label to add */ - virtual void add_label(CStructuredData* label); + virtual void add_label(std::shared_ptr label); /** get labels * @@ -65,7 +65,7 @@ class CStructuredLabels : public CLabels * * @return labels */ - CDynamicObjectArray* get_labels() const; + std::shared_ptr get_labels() const; /** get label object for specified index * @@ -73,7 +73,7 @@ class CStructuredLabels : public CLabels * * @return label object */ - virtual CStructuredData* get_label(int32_t idx); + virtual std::shared_ptr get_label(int32_t idx); /** * set label, possible with subset. This method should be used @@ -85,7 +85,7 @@ class CStructuredLabels : public CLabels * * @return if setting was successful */ - virtual bool set_label(int32_t idx, CStructuredData* label); + virtual bool set_label(int32_t idx, std::shared_ptr label); /** get number of labels, depending on wheter a subset is set * @@ -113,16 +113,16 @@ class CStructuredLabels : public CLabels void init(); /** ensure that the correct structured data type is used */ - void ensure_valid_sdt(CStructuredData* label); + void ensure_valid_sdt(std::shared_ptr label); protected: /** the vector of labels */ - CDynamicObjectArray* m_labels; + std::shared_ptr m_labels; /** the structured data type the labels are composed of */ EStructuredDataType m_sdt; -}; /* class CStructuredLabels */ +}; /* class StructuredLabels */ } /* namespace shogun */ diff --git a/src/shogun/latent/DirectorLatentModel.cpp b/src/shogun/latent/DirectorLatentModel.cpp index e2c4b6d37fc..a81857e648b 100644 --- a/src/shogun/latent/DirectorLatentModel.cpp +++ b/src/shogun/latent/DirectorLatentModel.cpp @@ -4,35 +4,35 @@ using namespace shogun; -CDirectorLatentModel::CDirectorLatentModel() : CLatentModel() +DirectorLatentModel::DirectorLatentModel() : LatentModel() { } -CDirectorLatentModel::~CDirectorLatentModel() +DirectorLatentModel::~DirectorLatentModel() { } -int32_t CDirectorLatentModel::get_dim() const +int32_t DirectorLatentModel::get_dim() const { error("Please implemement get_dim() in your target language before use"); return 0; } -CDotFeatures* CDirectorLatentModel::get_psi_feature_vectors() +std::shared_ptr DirectorLatentModel::get_psi_feature_vectors() { error("Please implemement get_psi_feature_vectors() in your target language before use"); return NULL; } -CData* CDirectorLatentModel::infer_latent_variable(const SGVector& w, index_t idx) +std::shared_ptr DirectorLatentModel::infer_latent_variable(const SGVector& w, index_t idx) { error("Please implemement infer_latent_variable(w, idx) in your target language before use"); return NULL; } -void CDirectorLatentModel::argmax_h(const SGVector& w) +void DirectorLatentModel::argmax_h(const SGVector& w) { error("Please implemement argmax_h(w) in your target language before use"); } diff --git a/src/shogun/latent/DirectorLatentModel.h b/src/shogun/latent/DirectorLatentModel.h index aefc1e0b5c1..542fa91b097 100644 --- a/src/shogun/latent/DirectorLatentModel.h +++ b/src/shogun/latent/DirectorLatentModel.h @@ -13,7 +13,7 @@ namespace shogun { -class CLatentModel; +class LatentModel; #define IGNORE_IN_CLASSLIST /** @@ -21,16 +21,16 @@ class CLatentModel; * with latent variable svm in target interface language. It is a base class * that needs to be extended with real implementations before using. * - * @see CLatentModel + * @see LatentModel */ -IGNORE_IN_CLASSLIST class CDirectorLatentModel : public CLatentModel +IGNORE_IN_CLASSLIST class DirectorLatentModel : public LatentModel { public: /** default constructor */ - CDirectorLatentModel(); + DirectorLatentModel(); /** destructor */ - virtual ~CDirectorLatentModel(); + virtual ~DirectorLatentModel(); /** * return the dimensionality of the joint feature space, i.e. @@ -42,7 +42,7 @@ IGNORE_IN_CLASSLIST class CDirectorLatentModel : public CLatentModel * * @return PSI vectors */ - virtual CDotFeatures* get_psi_feature_vectors(); + virtual std::shared_ptr get_psi_feature_vectors(); /** User defined \f$h^{*} = argmax_{h} \langle \bold{w},\Psi(\bold{x},\bold{h}) \rangle\f$ * This function has to be defined the user as it is applications specific, since @@ -52,7 +52,7 @@ IGNORE_IN_CLASSLIST class CDirectorLatentModel : public CLatentModel * @param idx index of the example * @return returns \f$h^{*}\f$ for the given example */ - virtual CData* infer_latent_variable(const SGVector& w, index_t idx); + virtual std::shared_ptr infer_latent_variable(const SGVector& w, index_t idx); /** Calculates \f$argmax_{h} \langle \bold{w},\Psi(\bold{x},\bold{h}) \rangle\f$ * The default implementaiton calculates the argmax_h only on the positive examples. diff --git a/src/shogun/latent/LatentModel.cpp b/src/shogun/latent/LatentModel.cpp index b07410b26ea..f34107d671d 100644 --- a/src/shogun/latent/LatentModel.cpp +++ b/src/shogun/latent/LatentModel.cpp @@ -9,7 +9,7 @@ using namespace shogun; -CLatentModel::CLatentModel() +LatentModel::LatentModel() : m_features(NULL), m_labels(NULL), m_do_caching(false), @@ -18,53 +18,53 @@ CLatentModel::CLatentModel() register_parameters(); } -CLatentModel::CLatentModel(CLatentFeatures* feats, CLatentLabels* labels, bool do_caching) +LatentModel::LatentModel(std::shared_ptr feats, std::shared_ptr labels, bool do_caching) : m_features(feats), m_labels(labels), m_do_caching(do_caching), m_cached_psi(NULL) { register_parameters(); - SG_REF(m_features); - SG_REF(m_labels); + + } -CLatentModel::~CLatentModel() +LatentModel::~LatentModel() { - SG_UNREF(m_labels); - SG_UNREF(m_features); - SG_UNREF(m_cached_psi); + + + } -int32_t CLatentModel::get_num_vectors() const +int32_t LatentModel::get_num_vectors() const { return m_features->get_num_vectors(); } -void CLatentModel::set_labels(CLatentLabels* labs) +void LatentModel::set_labels(std::shared_ptr labs) { - SG_REF(labs); - SG_UNREF(m_labels); + + m_labels = labs; } -CLatentLabels* CLatentModel::get_labels() const +std::shared_ptr LatentModel::get_labels() const { - SG_REF(m_labels); + return m_labels; } -void CLatentModel::set_features(CLatentFeatures* feats) +void LatentModel::set_features(std::shared_ptr feats) { - SG_REF(feats); - SG_UNREF(m_features); + + m_features = feats; } -void CLatentModel::argmax_h(const SGVector& w) +void LatentModel::argmax_h(const SGVector& w) { int32_t num = get_num_vectors(); - CBinaryLabels* y = binary_labels(m_labels->get_labels()); + auto y = binary_labels(m_labels->get_labels()); ASSERT(num > 0) ASSERT(num == m_labels->get_num_labels()) @@ -74,13 +74,13 @@ void CLatentModel::argmax_h(const SGVector& w) if (y->get_label(i) == 1) { // infer h and set it for the argmax_h - CData* latent_data = infer_latent_variable(w, i); + auto latent_data = infer_latent_variable(w, i); m_labels->set_latent_label(i, latent_data); } } } -void CLatentModel::register_parameters() +void LatentModel::register_parameters() { SG_ADD(&m_features, "features", "Latent features"); SG_ADD(&m_labels, "labels", "Latent labels"); @@ -92,28 +92,28 @@ void CLatentModel::register_parameters() } -CLatentFeatures* CLatentModel::get_features() const +std::shared_ptr LatentModel::get_features() const { - SG_REF(m_features); + return m_features; } -void CLatentModel::cache_psi_features() +void LatentModel::cache_psi_features() { if (m_do_caching) { if (m_cached_psi) - SG_UNREF(m_cached_psi); + m_cached_psi = this->get_psi_feature_vectors(); - SG_REF(m_cached_psi); + } } -CDotFeatures* CLatentModel::get_cached_psi_features() const +std::shared_ptr LatentModel::get_cached_psi_features() const { if (m_do_caching) { - SG_REF(m_cached_psi); + return m_cached_psi; } return NULL; diff --git a/src/shogun/latent/LatentModel.h b/src/shogun/latent/LatentModel.h index dad4e8cc64c..15a73d116c3 100644 --- a/src/shogun/latent/LatentModel.h +++ b/src/shogun/latent/LatentModel.h @@ -16,21 +16,21 @@ namespace shogun { - /** @brief Abstract class CLatentModel + /** @brief Abstract class LatentModel * It represents the application specific model and contains most of the * application dependent logic to solve latent variable based problems. * * The idea is that the user have to define and implement her own model, which - * is derived from CLatentModel and implement all the pure virtual functions + * is derived from LatentModel and implement all the pure virtual functions * which depends on the given problem she wants to solve, like the combined * feature representation: \f$\Psi(\bold{x_i},\bold{h_i})\f$ and the inference * of the latent variable \f$argmax_{h} \langle \bold{w},\Psi(\bold{x},\bold{h}) \rangle\f$ */ - class CLatentModel: public CSGObject + class LatentModel: public SGObject { public: /** default ctor */ - CLatentModel(); + LatentModel(); /** constructor * @@ -38,10 +38,10 @@ namespace shogun * @param labels Latent labels * @param do_caching whether caching of PSI vectors is enabled or not. Enabled by default. */ - CLatentModel(CLatentFeatures* feats, CLatentLabels* labels, bool do_caching = true); + LatentModel(std::shared_ptr feats, std::shared_ptr labels, bool do_caching = true); /** destructor */ - virtual ~CLatentModel(); + virtual ~LatentModel(); /** get the number of examples * @@ -59,31 +59,31 @@ namespace shogun * * @param labs latent labels */ - void set_labels(CLatentLabels* labs); + void set_labels(std::shared_ptr labs); /** get latent labels * * @return latent labels */ - CLatentLabels* get_labels() const; + std::shared_ptr get_labels() const; /** set latent features * * @param feats the latent features of the problem */ - void set_features(CLatentFeatures* feats); + void set_features(std::shared_ptr feats); /** get latent features * * @return latent features */ - CLatentFeatures* get_features() const; + std::shared_ptr get_features() const; /** Calculate the PSI vectors for all features * * @return PSI vectors */ - virtual CDotFeatures* get_psi_feature_vectors()=0; + virtual std::shared_ptr get_psi_feature_vectors()=0; /** User defined \f$h^{*} = argmax_{h} \langle \bold{w},\Psi(\bold{x},\bold{h}) \rangle\f$ * This function has to be defined the user as it is applications specific, since @@ -93,7 +93,7 @@ namespace shogun * @param idx index of the example * @return returns \f$h^{*}\f$ for the given example */ - virtual CData* infer_latent_variable(const SGVector& w, index_t idx)=0; + virtual std::shared_ptr infer_latent_variable(const SGVector& w, index_t idx)=0; /** Calculates \f$argmax_{h} \langle \bold{w},\Psi(\bold{x},\bold{h}) \rangle\f$ * The default implementaiton calculates the argmax_h only on the positive examples. @@ -111,7 +111,7 @@ namespace shogun * * @return the cached PSI vectors */ - CDotFeatures* get_cached_psi_features() const; + std::shared_ptr get_cached_psi_features() const; /** get caching * @@ -139,13 +139,13 @@ namespace shogun protected: /** latent features for training */ - CLatentFeatures* m_features; + std::shared_ptr m_features; /** corresponding labels for the train set */ - CLatentLabels* m_labels; + std::shared_ptr m_labels; /** boolean that indicates whether caching of PSI vectors is enabled or not */ bool m_do_caching; /** cached PSI feature vectors after argmax_h */ - CDotFeatures* m_cached_psi; + std::shared_ptr m_cached_psi; private: /** register the parameters */ diff --git a/src/shogun/latent/LatentSVM.cpp b/src/shogun/latent/LatentSVM.cpp index dd764f5b101..7d93b305340 100644 --- a/src/shogun/latent/LatentSVM.cpp +++ b/src/shogun/latent/LatentSVM.cpp @@ -12,21 +12,21 @@ using namespace shogun; -CLatentSVM::CLatentSVM() - : CLinearLatentMachine() +LatentSVM::LatentSVM() + : LinearLatentMachine() { } -CLatentSVM::CLatentSVM(CLatentModel* model, float64_t C) - : CLinearLatentMachine(model, C) +LatentSVM::LatentSVM(std::shared_ptr model, float64_t C) + : LinearLatentMachine(model, C) { } -CLatentSVM::~CLatentSVM() +LatentSVM::~LatentSVM() { } -CLatentLabels* CLatentSVM::apply_latent() +std::shared_ptr LatentSVM::apply_latent() { if (!m_model) error("LatentModel is not set!"); @@ -36,36 +36,35 @@ CLatentLabels* CLatentSVM::apply_latent() SGVector w = get_w(); index_t num_examples = m_model->get_num_vectors(); - CLatentLabels* hs = new CLatentLabels(num_examples); - CBinaryLabels* ys = new CBinaryLabels(num_examples); + auto hs = std::make_shared(num_examples); + auto ys = std::make_shared(num_examples); hs->set_labels(ys); m_model->set_labels(hs); for (index_t i = 0; i < num_examples; ++i) { /* find h for the example */ - CData* h = m_model->infer_latent_variable(w, i); + auto h = m_model->infer_latent_variable(w, i); hs->add_latent_label(h); } /* compute the y labels */ - CDotFeatures* x = m_model->get_psi_feature_vectors(); + auto x = m_model->get_psi_feature_vectors(); x->dense_dot_range(ys->get_labels().vector, 0, num_examples, NULL, w.vector, w.vlen, 0.0); return hs; } -float64_t CLatentSVM::do_inner_loop(float64_t cooling_eps) +float64_t LatentSVM::do_inner_loop(float64_t cooling_eps) { - CLabels* ys = m_model->get_labels()->get_labels(); - CDotFeatures* feats = (m_model->get_caching() ? + auto ys = m_model->get_labels()->get_labels(); + auto feats = (m_model->get_caching() ? m_model->get_cached_psi_features() : m_model->get_psi_feature_vectors()); - CSVMOcas svm(m_C, feats, ys); + SVMOcas svm(m_C, feats, ys); svm.set_epsilon(cooling_eps); svm.train(); - SG_UNREF(ys); - SG_UNREF(feats); + /* copy the resulting w */ set_w(svm.get_w().clone()); diff --git a/src/shogun/latent/LatentSVM.h b/src/shogun/latent/LatentSVM.h index 4783435dc09..78f125ac5a2 100644 --- a/src/shogun/latent/LatentSVM.h +++ b/src/shogun/latent/LatentSVM.h @@ -19,7 +19,7 @@ namespace shogun * Latent SVM implementation based on [1]. * For optimization this implementation uses SVMOcas. * - * User must provide a her own CLatentModel which implements the PSI(x_i,h_i) + * User must provide a her own LatentModel which implements the PSI(x_i,h_i) * function for the given problem. * * [1] P. F. Felzenszwalb, R. B. Girshick, D. McAllester, and D. Ramanan, @@ -28,28 +28,28 @@ namespace shogun * IEEE Transactions on, vol. 32, no. 9, pp. 1627-1645, 2010. * */ - class CLatentSVM: public CLinearLatentMachine + class LatentSVM: public LinearLatentMachine { public: /** default contstructor */ - CLatentSVM(); + LatentSVM(); /** constructor * - * @param model the user defined CLatentModel object. + * @param model the user defined LatentModel object. * @param C regularization constant */ - CLatentSVM(CLatentModel* model, float64_t C); + LatentSVM(std::shared_ptr model, float64_t C); - virtual ~CLatentSVM(); + virtual ~LatentSVM(); /** apply linear machine to all examples * * @return resulting labels */ - virtual CLatentLabels* apply_latent(); + virtual std::shared_ptr apply_latent(); - using CLinearLatentMachine::apply_latent; + using LinearLatentMachine::apply_latent; /** Returns the name of the SGSerializable instance. * diff --git a/src/shogun/lib/BitString.h b/src/shogun/lib/BitString.h index 1d210d519fb..c947b3d0c52 100644 --- a/src/shogun/lib/BitString.h +++ b/src/shogun/lib/BitString.h @@ -23,11 +23,11 @@ namespace shogun * (or any other string of small alphabet size) * */ -class CBitString : public CSGObject +class BitString : public SGObject { public: /** default constructor */ - CBitString() { + BitString() { unstable(SOURCE_LOCATION); alphabet = NULL; @@ -45,9 +45,9 @@ class CBitString : public CSGObject * @param alpha Alphabet * @param width return this many bits upon str[idx] access operations */ - CBitString(EAlphabet alpha, int32_t width=1) : CSGObject(), string(NULL), length(0) + BitString(EAlphabet alpha, int32_t width=1) : SGObject(), string(NULL), length(0) { - alphabet=new CAlphabet(alpha); + alphabet=std::make_shared(alpha); int32_t nbits=alphabet->get_num_bits(); word_len = width*nbits; @@ -62,10 +62,9 @@ class CBitString : public CSGObject } /** destructor */ - ~CBitString() + ~BitString() { cleanup(); - SG_UNREF(alphabet); } /** free up memory */ @@ -125,7 +124,7 @@ class CBitString : public CSGObject uint64_t len=0; uint64_t offs=0; - CMemoryMappedFile f(fname); + MemoryMappedFile f(fname); uint64_t id_len=0; char* id=f.get_line(id_len, offs); @@ -331,7 +330,7 @@ class CBitString : public CSGObject private: /** alphabet the bit string is based on */ - CAlphabet* alphabet; + std::shared_ptr alphabet; /** the bit string */ uint64_t* string; /** the length of the bit string */ diff --git a/src/shogun/lib/Cache.h b/src/shogun/lib/Cache.h index f434cfe27a9..acea41d1f06 100644 --- a/src/shogun/lib/Cache.h +++ b/src/shogun/lib/Cache.h @@ -26,7 +26,7 @@ namespace shogun * the maximal number of entries in cache) * */ -template class CCache : public CSGObject +template class Cache : public SGObject { /** cache entry */ struct TEntry @@ -41,7 +41,7 @@ template class CCache : public CSGObject public: /** default constructor */ - CCache() :CSGObject() + Cache() :SGObject() { unstable(SOURCE_LOCATION); @@ -65,8 +65,8 @@ template class CCache : public CSGObject * @param obj_size object size * @param num_entries number of cached objects */ - CCache(int64_t cache_size, int64_t obj_size, int64_t num_entries) - : CSGObject() + Cache(int64_t cache_size, int64_t obj_size, int64_t num_entries) + : SGObject() { if (cache_size==0 || obj_size==0 || num_entries==0) { @@ -81,7 +81,7 @@ template class CCache : public CSGObject } entry_size=obj_size; - nr_cache_lines=CMath::min((int64_t) (cache_size*1024*1024/obj_size/sizeof(T)), num_entries+1); + nr_cache_lines=Math::min((int64_t) (cache_size*1024*1024/obj_size/sizeof(T)), num_entries+1); io::info("creating {} cache lines (total size: {} byte)", nr_cache_lines, nr_cache_lines*obj_size*sizeof(T)); cache_block=SG_MALLOC(T, obj_size*nr_cache_lines); @@ -111,7 +111,7 @@ template class CCache : public CSGObject set_generic(); } - virtual ~CCache() + virtual ~Cache() { SG_FREE(cache_block); SG_FREE(lookup_table); diff --git a/src/shogun/lib/CircularBuffer.cpp b/src/shogun/lib/CircularBuffer.cpp index ec2374dd69c..c1455f7185d 100644 --- a/src/shogun/lib/CircularBuffer.cpp +++ b/src/shogun/lib/CircularBuffer.cpp @@ -14,12 +14,12 @@ using namespace shogun; -CCircularBuffer::CCircularBuffer() +CircularBuffer::CircularBuffer() { init(); } -CCircularBuffer::CCircularBuffer(int32_t buffer_size) +CircularBuffer::CircularBuffer(int32_t buffer_size) { init(); @@ -32,23 +32,23 @@ CCircularBuffer::CCircularBuffer(int32_t buffer_size) m_bytes_available=m_buffer.vlen; } -CCircularBuffer::~CCircularBuffer() +CircularBuffer::~CircularBuffer() { - SG_UNREF(m_tokenizer); + } -void CCircularBuffer::set_tokenizer(CTokenizer* tokenizer) +void CircularBuffer::set_tokenizer(std::shared_ptr tokenizer) { - SG_REF(tokenizer); - SG_UNREF(m_tokenizer); + + m_tokenizer=tokenizer; } -int32_t CCircularBuffer::push(SGVector source) +int32_t CircularBuffer::push(SGVector source) { if (source.vector==NULL || source.vlen==0) { - error("CCircularBuffer::push(SGVector):: Invalid parameters! Source shouldn't be NULL or zero sized"); + error("CircularBuffer::push(SGVector):: Invalid parameters! Source shouldn't be NULL or zero sized"); return -1; } @@ -88,11 +88,11 @@ int32_t CCircularBuffer::push(SGVector source) return bytes_to_write; } -int32_t CCircularBuffer::push(FILE* source, int32_t source_size) +int32_t CircularBuffer::push(FILE* source, int32_t source_size) { if (source==NULL || source_size==0) { - error("CCircularBuffer::push(FILE*, int32_t):: Invalid parameters! Source shouldn't be NULL or zero sized"); + error("CircularBuffer::push(FILE*, int32_t):: Invalid parameters! Source shouldn't be NULL or zero sized"); return -1; } @@ -132,7 +132,7 @@ int32_t CCircularBuffer::push(FILE* source, int32_t source_size) return bytes_to_write; } -SGVector CCircularBuffer::pop(int32_t num_bytes) +SGVector CircularBuffer::pop(int32_t num_bytes) { SGVector result; @@ -171,11 +171,11 @@ SGVector CCircularBuffer::pop(int32_t num_bytes) return result; } -bool CCircularBuffer::has_next() +bool CircularBuffer::has_next() { if (m_tokenizer==NULL) { - error("CCircularBuffer::has_next():: Tokenizer is not initialized"); + error("CircularBuffer::has_next():: Tokenizer is not initialized"); return false; } @@ -208,13 +208,13 @@ bool CCircularBuffer::has_next() return false; } -index_t CCircularBuffer::next_token_idx(index_t &start) +index_t CircularBuffer::next_token_idx(index_t &start) { index_t end; if (m_tokenizer==NULL) { - error("CCircularBuffer::next_token_idx(index_t&):: Tokenizer is not initialized"); + error("CircularBuffer::next_token_idx(index_t&):: Tokenizer is not initialized"); return 0; } @@ -263,7 +263,7 @@ index_t CCircularBuffer::next_token_idx(index_t &start) return start; } -void CCircularBuffer::skip_characters(int32_t num_chars) +void CircularBuffer::skip_characters(int32_t num_chars) { auto head_length = std::distance(m_begin_pos, m_buffer_end); if (head_length >= num_chars) @@ -279,7 +279,7 @@ void CCircularBuffer::skip_characters(int32_t num_chars) m_bytes_count-=num_chars; } -void CCircularBuffer::clear() +void CircularBuffer::clear() { m_begin_pos=m_buffer.vector; m_end_pos=m_begin_pos; @@ -289,7 +289,7 @@ void CCircularBuffer::clear() m_bytes_count=0; } -void CCircularBuffer::init() +void CircularBuffer::init() { m_buffer=SGVector(); m_buffer_end=NULL; @@ -303,12 +303,12 @@ void CCircularBuffer::init() m_bytes_count=0; } -int32_t CCircularBuffer::append_chunk(const char* source, int32_t source_size, +int32_t CircularBuffer::append_chunk(const char* source, int32_t source_size, bool from_buffer_begin) { if (source==NULL || source_size==0) { - error("CCircularBuffer::append_chunk(const char*, int32_t, bool):: Invalid parameters!\ + error("CircularBuffer::append_chunk(const char*, int32_t, bool):: Invalid parameters!\ Source shouldn't be NULL or zero sized"); return -1; } @@ -325,7 +325,7 @@ int32_t CCircularBuffer::append_chunk(const char* source, int32_t source_size, return source_size; } -int32_t CCircularBuffer::append_chunk(FILE* source, int32_t source_size, +int32_t CircularBuffer::append_chunk(FILE* source, int32_t source_size, bool from_buffer_begin) { int32_t actually_read=fread(m_end_pos, sizeof(char), source_size, source); @@ -340,12 +340,12 @@ int32_t CCircularBuffer::append_chunk(FILE* source, int32_t source_size, return actually_read; } -void CCircularBuffer::detach_chunk(char** dest, int32_t* dest_size, int32_t dest_offset, int32_t num_bytes, +void CircularBuffer::detach_chunk(char** dest, int32_t* dest_size, int32_t dest_offset, int32_t num_bytes, bool from_buffer_begin) { if (dest==NULL || dest_size==NULL) { - error("CCircularBuffer::detach_chunk(...):: Invalid parameters! Pointers are NULL"); + error("CircularBuffer::detach_chunk(...):: Invalid parameters! Pointers are NULL"); return; } @@ -375,7 +375,7 @@ void CCircularBuffer::detach_chunk(char** dest, int32_t* dest_size, int32_t dest m_bytes_count-=num_bytes; } -bool CCircularBuffer::has_next_locally(char* part_begin, char* part_end) +bool CircularBuffer::has_next_locally(char* part_begin, char* part_end) { auto num_bytes_to_search=std::distance(part_begin, part_end); @@ -385,7 +385,7 @@ bool CCircularBuffer::has_next_locally(char* part_begin, char* part_end) return m_tokenizer->has_next(); } -index_t CCircularBuffer::next_token_idx_locally(index_t &start, char* part_begin, char* part_end) +index_t CircularBuffer::next_token_idx_locally(index_t &start, char* part_begin, char* part_end) { index_t end=0; auto num_bytes_to_search=std::distance(part_begin, part_end); @@ -409,7 +409,7 @@ index_t CCircularBuffer::next_token_idx_locally(index_t &start, char* part_begin return m_last_idx++; } -void CCircularBuffer::move_pointer(char** pointer, char* new_position) +void CircularBuffer::move_pointer(char** pointer, char* new_position) { *pointer = (new_position >= m_buffer_end) ? m_buffer.vector diff --git a/src/shogun/lib/CircularBuffer.h b/src/shogun/lib/CircularBuffer.h index 5a593a666ad..8a6eb0fb95b 100644 --- a/src/shogun/lib/CircularBuffer.h +++ b/src/shogun/lib/CircularBuffer.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Heiko Strathmann, Yuyu Zhang, Thoralf Klein, + * Authors: Evgeniy Andreev, Heiko Strathmann, Yuyu Zhang, Thoralf Klein, * Fernando Iglesias, Bjoern Esser */ @@ -16,7 +16,7 @@ namespace shogun { -class CTokenizer; +class Tokenizer; /** @brief Implementation of circular buffer * This buffer has logical structure such as queue (FIFO). @@ -27,26 +27,26 @@ class CTokenizer; * * w: http://en.wikipedia.org/wiki/Circular_buffer */ -class CCircularBuffer : public CSGObject +class CircularBuffer : public SGObject { public: /** default constructor */ - CCircularBuffer(); + CircularBuffer(); /** constructor * * @param buffer_size size of buffer */ - CCircularBuffer(int32_t buffer_size); + CircularBuffer(int32_t buffer_size); /** destructor */ - ~CCircularBuffer(); + ~CircularBuffer(); /** set tokenizer * * @param tokenizer tokenizer */ - void set_tokenizer(CTokenizer* tokenizer); + void set_tokenizer(std::shared_ptr tokenizer); /** push data into buffer from memory block * @@ -152,7 +152,7 @@ class CCircularBuffer : public CSGObject char* m_end_pos; /** tokenizer */ - CTokenizer* m_tokenizer; + std::shared_ptr m_tokenizer; /** position at which the search starts */ index_t m_last_idx; diff --git a/src/shogun/lib/Compressor.cpp b/src/shogun/lib/Compressor.cpp index 28eae40a06a..1e0bbf38e41 100644 --- a/src/shogun/lib/Compressor.cpp +++ b/src/shogun/lib/Compressor.cpp @@ -31,13 +31,13 @@ using namespace shogun; -CCompressor::CCompressor() - :CSGObject(), compression_type(UNCOMPRESSED) +Compressor::Compressor() + :SGObject(), compression_type(UNCOMPRESSED) { unstable(SOURCE_LOCATION); } -void CCompressor::compress(uint8_t* uncompressed, uint64_t uncompressed_size, +void Compressor::compress(uint8_t* uncompressed, uint64_t uncompressed_size, uint8_t* &compressed, uint64_t &compressed_size, int32_t level) { uint64_t initial_buffer_size=0; @@ -202,7 +202,7 @@ void CCompressor::compress(uint8_t* uncompressed, uint64_t uncompressed_size, compressed = SG_REALLOC(uint8_t, compressed, initial_buffer_size, compressed_size); } -void CCompressor::decompress(uint8_t* compressed, uint64_t compressed_size, +void Compressor::decompress(uint8_t* compressed, uint64_t compressed_size, uint8_t* uncompressed, uint64_t& uncompressed_size) { if (compressed_size==0) diff --git a/src/shogun/lib/Compressor.h b/src/shogun/lib/Compressor.h index 890d3db13fc..05e3e343e26 100644 --- a/src/shogun/lib/Compressor.h +++ b/src/shogun/lib/Compressor.h @@ -40,22 +40,22 @@ namespace shogun * Note that besides lzo compression, this library is thread safe. * */ - class CCompressor : public CSGObject + class Compressor : public SGObject { public: /** default constructor */ - CCompressor(); + Compressor(); /** default constructor * * @param ct compression to use: one of UNCOMPRESSED, LZO, GZIP, BZIP2 or LZMA */ - CCompressor(E_COMPRESSION_TYPE ct) : CSGObject(), compression_type(ct) + Compressor(E_COMPRESSION_TYPE ct) : SGObject(), compression_type(ct) { } /** default destructor */ - virtual ~CCompressor() + virtual ~Compressor() { } diff --git a/src/shogun/lib/Data.cpp b/src/shogun/lib/Data.cpp index 0dceaa8f3b0..a2a8c47875c 100644 --- a/src/shogun/lib/Data.cpp +++ b/src/shogun/lib/Data.cpp @@ -8,12 +8,12 @@ using namespace shogun; -CData::CData() +Data::Data() { } -CData::~CData() +Data::~Data() { } diff --git a/src/shogun/lib/Data.h b/src/shogun/lib/Data.h index 2f2d9d521bc..08fef6959dd 100644 --- a/src/shogun/lib/Data.h +++ b/src/shogun/lib/Data.h @@ -18,13 +18,13 @@ namespace shogun * An abstract class for storing data in any kind of format for * LatentLabels, LatentFeatures and for StructuredLabels. */ - class CData : public CSGObject + class Data : public SGObject { public: /** default ctor */ - CData(); + Data(); - virtual ~CData(); + virtual ~Data(); /** Returns the name of the SGSerializable instance. * diff --git a/src/shogun/lib/DataType.cpp b/src/shogun/lib/DataType.cpp index 102a405e9c3..d415b2d6bcd 100644 --- a/src/shogun/lib/DataType.cpp +++ b/src/shogun/lib/DataType.cpp @@ -199,7 +199,7 @@ TSGDataType::sizeof_ptype(EPrimitiveType ptype) case PT_FLOAT64: return sizeof (float64_t); case PT_FLOATMAX: return sizeof (floatmax_t); case PT_COMPLEX128: return sizeof (complex128_t); - case PT_SGOBJECT: return sizeof (CSGObject*); + case PT_SGOBJECT: return sizeof (SGObject*); case PT_UNDEFINED: default: error("Implementation error: undefined primitive type"); break; diff --git a/src/shogun/lib/DelimiterTokenizer.cpp b/src/shogun/lib/DelimiterTokenizer.cpp index d381faea62a..e1006e7a48c 100644 --- a/src/shogun/lib/DelimiterTokenizer.cpp +++ b/src/shogun/lib/DelimiterTokenizer.cpp @@ -15,21 +15,21 @@ namespace shogun { -CDelimiterTokenizer::CDelimiterTokenizer(bool skip_delimiters) : delimiters(256) +DelimiterTokenizer::DelimiterTokenizer(bool skip_delimiters) : delimiters(256) { last_idx = 0; skip_consecutive_delimiters = skip_delimiters; init(); } -CDelimiterTokenizer::CDelimiterTokenizer(const CDelimiterTokenizer& orig) +DelimiterTokenizer::DelimiterTokenizer(const DelimiterTokenizer& orig) { - CTokenizer::set_text(orig.text); + Tokenizer::set_text(orig.text); delimiters = orig.delimiters; init(); } -void CDelimiterTokenizer::init() +void DelimiterTokenizer::init() { SG_ADD(&last_idx, "last_idx", "Index of last token"); SG_ADD(&skip_consecutive_delimiters, "skip_consecutive_delimiters", @@ -37,18 +37,18 @@ void CDelimiterTokenizer::init() SGVector::fill_vector(delimiters, 256, 0); } -void CDelimiterTokenizer::set_text(SGVector txt) +void DelimiterTokenizer::set_text(SGVector txt) { last_idx = 0; - CTokenizer::set_text(txt); + Tokenizer::set_text(txt); } -const char* CDelimiterTokenizer::get_name() const +const char* DelimiterTokenizer::get_name() const { return "DelimiterTokenizer"; } -bool CDelimiterTokenizer::has_next() +bool DelimiterTokenizer::has_next() { if (skip_consecutive_delimiters) { @@ -63,19 +63,19 @@ bool CDelimiterTokenizer::has_next() return last_idxdelimiters = delimiters; t->skip_consecutive_delimiters = skip_consecutive_delimiters; return t; } -void CDelimiterTokenizer::set_skip_delimiters(bool skip_delimiters) +void DelimiterTokenizer::set_skip_delimiters(bool skip_delimiters) { skip_consecutive_delimiters = skip_delimiters; } -bool CDelimiterTokenizer::get_skip_delimiters() const +bool DelimiterTokenizer::get_skip_delimiters() const { return skip_consecutive_delimiters; } diff --git a/src/shogun/lib/DelimiterTokenizer.h b/src/shogun/lib/DelimiterTokenizer.h index b63c00c2061..3091506c179 100644 --- a/src/shogun/lib/DelimiterTokenizer.h +++ b/src/shogun/lib/DelimiterTokenizer.h @@ -17,29 +17,29 @@ namespace shogun { -/** @brief The class CDelimiterTokenizer is used to tokenize +/** @brief The class DelimiterTokenizer is used to tokenize * a SGVector into tokens using custom chars as delimiters. * One can set the delimiters to use by setting to 1 the appropriate * index of the public field delimiters. Eg. to set as delimiter the * character ':', one should do: tokenizer->delimiters[':'] = 1; */ -class CDelimiterTokenizer: public CTokenizer +class DelimiterTokenizer: public Tokenizer { public: /** default constructor * * @param skip_delimiters whether to skip consecutive delimiters or not */ - CDelimiterTokenizer(bool skip_delimiters = false); + DelimiterTokenizer(bool skip_delimiters = false); /** copy constructor * * @param orig the original DelimiterTokenizer */ - CDelimiterTokenizer(const CDelimiterTokenizer& orig); + DelimiterTokenizer(const DelimiterTokenizer& orig); /** destructor */ - virtual ~CDelimiterTokenizer() {} + virtual ~DelimiterTokenizer() {} /** Set the char array that requires tokenization * @@ -76,7 +76,7 @@ class CDelimiterTokenizer: public CTokenizer */ void init_for_whitespace(); - CDelimiterTokenizer* get_copy(); + DelimiterTokenizer* get_copy(); /** Resets the delimiters */ void clear_delimiters(); diff --git a/src/shogun/lib/DynamicArray.h b/src/shogun/lib/DynamicArray.h index 2be57ec9e6f..e49147d08bc 100644 --- a/src/shogun/lib/DynamicArray.h +++ b/src/shogun/lib/DynamicArray.h @@ -15,6 +15,9 @@ #include #include +#include +#include + namespace shogun { /** @brief Template Dynamic array class that creates an array that can @@ -25,12 +28,15 @@ namespace shogun * etc. and for hi-level objects only stores pointers, which are not * automagically SG_REF'd/deleted. */ -template class CDynamicArray :public CSGObject +template class DynamicArray :public SGObject { + using DiffType = typename std::vector::difference_type; + using ReferenceType = typename std::vector::reference; + using ConstReferenceType = typename std::vector::const_reference; public: /** default constructor */ - CDynamicArray() - : CSGObject(), m_array() + DynamicArray() + : SGObject(), m_array() { dim1_size=1; dim2_size=1; @@ -45,8 +51,8 @@ template class CDynamicArray :public CSGObject * @param p_dim2_size dimension 2 * @param p_dim3_size dimension 3 */ - CDynamicArray(int32_t p_dim1_size, int32_t p_dim2_size=1, int32_t p_dim3_size=1) - : CSGObject(), m_array(p_dim1_size*p_dim2_size*p_dim3_size) + DynamicArray(int32_t p_dim1_size, int32_t p_dim2_size=1, int32_t p_dim3_size=1) + : SGObject(), m_array(p_dim1_size*p_dim2_size*p_dim3_size) { dim1_size=p_dim1_size; dim2_size=p_dim2_size; @@ -62,13 +68,14 @@ template class CDynamicArray :public CSGObject * @param p_free_array if array must be freed * @param p_copy_array if array must be copied */ - CDynamicArray(T* p_array, int32_t p_dim1_size, bool p_free_array, bool p_copy_array) - : CSGObject(), m_array(p_array, p_dim1_size, p_free_array, p_copy_array) + DynamicArray(T* p_array, int32_t p_dim1_size, bool p_free_array, bool p_copy_array) + : SGObject() { dim1_size=p_dim1_size; dim2_size=1; dim3_size=1; + set_array(p_array, p_dim1_size, p_free_array, p_copy_array); init(); } @@ -80,14 +87,15 @@ template class CDynamicArray :public CSGObject * @param p_free_array if array must be freed * @param p_copy_array if array must be copied */ - CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size, + DynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size, bool p_free_array, bool p_copy_array) - : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size, p_free_array, p_copy_array) + : SGObject() { dim1_size=p_dim1_size; dim2_size=p_dim2_size; dim3_size=1; + set_array(p_array, p_dim1_size, p_dim2_size, p_free_array, p_copy_array); init(); } @@ -100,14 +108,16 @@ template class CDynamicArray :public CSGObject * @param p_free_array if array must be freed * @param p_copy_array if array must be copied */ - CDynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size, + DynamicArray(T* p_array, int32_t p_dim1_size, int32_t p_dim2_size, int32_t p_dim3_size, bool p_free_array, bool p_copy_array) - : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size, p_free_array, p_copy_array) + : SGObject() { dim1_size=p_dim1_size; dim2_size=p_dim2_size; dim3_size=p_dim3_size; + set_array(p_array, p_dim1_size, p_dim2_size, p_dim3_size, p_free_array, p_copy_array); + init(); } @@ -118,8 +128,8 @@ template class CDynamicArray :public CSGObject * @param p_dim2_size dimension 2 * @param p_dim3_size dimension 3 */ - CDynamicArray(const T* p_array, int32_t p_dim1_size=1, int32_t p_dim2_size=1, int32_t p_dim3_size=1) - : CSGObject(), m_array(p_array, p_dim1_size*p_dim2_size*p_dim3_size) + DynamicArray(const T* p_array, int32_t p_dim1_size=1, int32_t p_dim2_size=1, int32_t p_dim3_size=1) + : SGObject(), m_array(p_array, p_array+(p_dim1_size*p_dim2_size*p_dim3_size)) { dim1_size=p_dim1_size; dim2_size=p_dim2_size; @@ -128,17 +138,7 @@ template class CDynamicArray :public CSGObject init(); } - virtual ~CDynamicArray() {} - - /** set the resize granularity - * - * @param g new granularity - * @return what has been set (minimum is 128) - */ - inline int32_t set_granularity(int32_t g) - { - return m_array.set_granularity(g); - } + virtual ~DynamicArray() { m_array.clear(); } /** get array size (including granularity buffer) * @@ -146,7 +146,7 @@ template class CDynamicArray :public CSGObject */ inline int32_t get_array_size() { - return m_array.get_array_size(); + return m_array.capacity(); } /** return 2d array size @@ -197,7 +197,7 @@ template class CDynamicArray :public CSGObject */ inline int32_t get_num_elements() const { - return m_array.get_num_elements(); + return m_array.size(); } /** get array element at index @@ -207,9 +207,9 @@ template class CDynamicArray :public CSGObject * @param idx3 index 3 * @return array element at index */ - inline const T& get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const + inline ConstReferenceType get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const { - return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)]; + return m_array[idx1+dim1_size*(idx2+dim2_size*idx3)]; } /** get array element at index @@ -219,7 +219,7 @@ template class CDynamicArray :public CSGObject * @param idx3 index 3 * @return array element at index */ - inline const T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const + inline ConstReferenceType element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const { return get_element(idx1, idx2, idx3); } @@ -231,9 +231,9 @@ template class CDynamicArray :public CSGObject * @param idx3 index 3 * @return array element at index */ - inline T& element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) + inline ReferenceType element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) { - return m_array.get_array()[idx1+dim1_size*(idx2+dim2_size*idx3)]; + return m_array[idx1+dim1_size*(idx2+dim2_size*idx3)]; } /** get element of given array at given index @@ -276,9 +276,9 @@ template class CDynamicArray :public CSGObject * * @return array element at last index */ - inline T get_last_element() const + inline ReferenceType get_last_element() const { - return m_array.get_last_element(); + return m_array.back(); } /** get array element at index @@ -288,9 +288,14 @@ template class CDynamicArray :public CSGObject * @param index index * @return array element at index */ - inline T get_element_safe(int32_t index) const + inline ReferenceType get_element_safe(int32_t index) const { - return m_array.get_element_safe(index); + if (index>=get_num_elements()) + { + SG_SERROR("array index out of bounds (%d >= %d)\n", + index, get_num_elements()); + } + return m_array[index]; } /** set array element at index @@ -303,7 +308,11 @@ template class CDynamicArray :public CSGObject */ inline bool set_element(T e, int32_t idx1, int32_t idx2=0, int32_t idx3=0) { - return m_array.set_element(e, idx1+dim1_size*(idx2+dim2_size*idx3)); + auto index = idx1+dim1_size*(idx2+dim2_size*idx3); + if (index >= m_array.size()) + m_array.resize(index); + m_array[index] = e; + return true; } /** insert array element at index @@ -314,7 +323,8 @@ template class CDynamicArray :public CSGObject */ inline bool insert_element(T e, int32_t index) { - return m_array.insert_element(e, index); + m_array.insert(m_array.begin()+index, e); + return true; } /** append array element to the end of array @@ -324,7 +334,8 @@ template class CDynamicArray :public CSGObject */ inline bool append_element(T e) { - return m_array.append_element(e); + m_array.push_back(e); + return true; } /** STD VECTOR compatible. Append array element to the end @@ -361,7 +372,10 @@ template class CDynamicArray :public CSGObject */ inline int32_t find_element(T e) { - return m_array.find_element(e); + auto it = std::find(m_array.begin(), m_array.end(), e); + if (it != m_array.end()) + return std::distance(m_array.begin(), it); + return -1L; } /** delete array element at idx @@ -372,7 +386,10 @@ template class CDynamicArray :public CSGObject */ inline bool delete_element(int32_t idx) { - return m_array.delete_element(idx); + auto e=m_array[idx]; + m_array.erase(std::remove(m_array.begin(), m_array.end(), e), + m_array.end()); + return true; } /** resize array @@ -387,13 +404,21 @@ template class CDynamicArray :public CSGObject dim1_size=ndim1; dim2_size=ndim2; dim3_size=ndim3; - return m_array.resize_array(ndim1*ndim2*ndim3); + try + { + m_array.reserve(ndim1*ndim2*ndim3); + return true; + } + catch (std::exception& e) + { + return false; + } } /** set array with a constant */ void set_const(const T& const_element) { - m_array.set_const(const_element); + m_array.assign(m_array.capacity(), const_element); } /** get the array @@ -403,9 +428,9 @@ template class CDynamicArray :public CSGObject * * @return the array */ - inline T* get_array() const + inline T* get_array() { - return m_array.get_array(); + return m_array.data(); } /** set the array pointer and free previously allocated memory @@ -417,7 +442,8 @@ template class CDynamicArray :public CSGObject inline void set_array(T* p_array, int32_t p_num_elements, int32_t array_size) { - m_array.set_array(p_array, p_num_elements, array_size); + m_array.resize(array_size); + std::copy(p_array, p_array+p_num_elements, m_array.begin()); } /** set the array pointer and free previously allocated memory @@ -430,10 +456,14 @@ template class CDynamicArray :public CSGObject inline void set_array(T* p_array, int32_t dim1, bool p_free_array, bool copy_array) { + if (!copy_array) + throw std::runtime_error("not copying array is not supported"); dim1_size=dim1; dim2_size=1; dim3_size=1; - m_array.set_array(p_array, dim1, dim1, p_free_array, copy_array); + + m_array.resize(dim1); + std::copy(p_array, p_array+dim1, m_array.begin()); } /** set the 2d array pointer and free previously allocated memory @@ -447,11 +477,14 @@ template class CDynamicArray :public CSGObject inline void set_array(T* p_array, int32_t dim1, int32_t dim2, bool p_free_array, bool copy_array) { + if (!copy_array) + throw std::runtime_error("not copying array is not supported"); dim1_size=dim1; dim2_size=dim2; dim3_size=1; - m_array.set_array(p_array, dim1*dim2, dim1*dim2, p_free_array, copy_array); + m_array.reserve(dim1+dim2); + std::copy(p_array, p_array+dim1+dim2, m_array.begin()); } /** set the 3d array pointer and free previously allocated memory @@ -466,10 +499,15 @@ template class CDynamicArray :public CSGObject inline void set_array(T* p_array, int32_t dim1, int32_t dim2, int32_t dim3, bool p_free_array, bool copy_array) { + if (!copy_array) + throw std::runtime_error("not copying array is not supported"); dim1_size=dim1; dim2_size=dim2; dim3_size=dim3; - m_array.set_array(p_array, dim1*dim2*dim3, dim1*dim2*dim3, p_free_array, copy_array); + + m_array.clear(); + m_array.reserve(dim1+dim2+dim3); + std::copy(p_array, p_array+dim1+dim2+dim3, m_array.begin()); } /** set the array pointer and free previously allocated memory @@ -479,7 +517,8 @@ template class CDynamicArray :public CSGObject */ inline void set_array(const T* p_array, int32_t p_size) { - m_array.set_array(p_array, p_size, p_size); + m_array.resize(p_size); + std::copy(p_array, p_array+p_size, m_array.begin()); } /** clear the array (with e.g. zeros) @@ -487,13 +526,13 @@ template class CDynamicArray :public CSGObject */ inline void clear_array(T value) { - m_array.clear_array(value); + m_array.assign(m_array.capacity(), value); } /** resets the array */ inline void reset_array() { - m_array.reset((T) 0); + m_array.clear(); } /** operator overload for array read only access @@ -505,9 +544,9 @@ template class CDynamicArray :public CSGObject * @param index index * @return element at index */ - inline const T& operator[](int32_t index) const + inline ConstReferenceType operator[](int32_t index) const { - return get_element(index); + return m_array[index]; } /** operator overload for array read-write access @@ -517,9 +556,9 @@ template class CDynamicArray :public CSGObject * @param index index * @return element at index */ - inline T& operator[](int32_t index) + inline ReferenceType operator[](int32_t index) { - return element(index); + return m_array[index]; } /** operator overload for array assignment @@ -527,7 +566,7 @@ template class CDynamicArray :public CSGObject * @param orig original array * @return new array */ - inline CDynamicArray& operator=(CDynamicArray& orig) + inline DynamicArray& operator=(DynamicArray& orig) { m_array=orig.m_array; dim1_size=orig.dim1_size; @@ -579,21 +618,6 @@ template class CDynamicArray :public CSGObject return "DynamicArray"; } - /** Can (optionally) be overridden to pre-initialize some member - * variables which are not PARAMETER::ADD'ed. Make sure that at - * first the overridden method BASE_CLASS::LOAD_SERIALIZABLE_PRE - * is called. - * - * @exception ShogunException Will be thrown if an error - * occurres. - */ - virtual void load_serializable_pre() noexcept(false) - { - CSGObject::load_serializable_pre(); - - m_array.resize_array(m_array.get_num_elements(), true); - } - /** Can (optionally) be overridden to pre-initialize some member * variables which are not PARAMETER::ADD'ed. Make sure that at * first the overridden method BASE_CLASS::SAVE_SERIALIZABLE_PRE @@ -604,19 +628,13 @@ template class CDynamicArray :public CSGObject */ virtual void save_serializable_pre() noexcept(false) { - CSGObject::save_serializable_pre(); - m_array.resize_array(m_array.get_num_elements(), true); + SGObject::save_serializable_pre(); + m_array.shrink_to_fit(); } - virtual CSGObject* clone(ParameterProperties pp) const override + virtual std::shared_ptr clone(ParameterProperties pp) const override { - CDynamicArray * cloned = (CDynamicArray*) CSGObject::clone(pp); - // Since the array vector is registered with - // current_num_elements as size (see parameter - // registration) the cloned version has less memory - // allocated than known to dynarray. We fix this here. - cloned->m_array.num_elements = cloned->m_array.current_num_elements; - return cloned; + return SGObject::clone(pp)->template as(); } private: @@ -625,21 +643,7 @@ template class CDynamicArray :public CSGObject virtual void init() { set_generic(); - - m_parameters->add_vector(&m_array.array, - &m_array.current_num_elements, "array", - "Memory for dynamic array."); - watch_param("array", &m_array.array, &m_array.current_num_elements); - - SG_ADD(&m_array.resize_granularity, - "resize_granularity", - "shrink/grow step size."); - SG_ADD(&m_array.use_sg_mallocs, - "use_sg_malloc", - "whether SG_MALLOC or malloc should be used"); - SG_ADD(&m_array.free_array, - "free_array", - "whether array must be freed"); + watch_param("array", &m_array); SG_ADD(&dim1_size, "dim1_size", "Dimension 1"); SG_ADD(&dim2_size, "dim2_size", "Dimension 2"); SG_ADD(&dim3_size, "dim3_size", "Dimension 3"); @@ -648,7 +652,7 @@ template class CDynamicArray :public CSGObject protected: /** underlying array */ - DynArray m_array; + std::vector m_array; /** dimension 1 */ int32_t dim1_size; diff --git a/src/shogun/lib/DynamicObjectArray.h b/src/shogun/lib/DynamicObjectArray.h index c6e489d70d5..dda36320032 100644 --- a/src/shogun/lib/DynamicObjectArray.h +++ b/src/shogun/lib/DynamicObjectArray.h @@ -17,29 +17,27 @@ #include #include -#include #include #include #include #include - namespace shogun { -/** @brief Dynamic array class for CSGObject pointers that creates an array +/** @brief Dynamic array class for SGObject pointers that creates an array * that can be used like a list or an array. * * It grows and shrinks dynamically, while elements can be accessed - * via index. It only stores CSGObject pointers, which ARE automagically + * via index. It only stores SGObject pointers, which ARE automagically * SG_REF'd/deleted. * */ -class CDynamicObjectArray : public CSGObject +class DynamicObjectArray : public SGObject { public: /** default constructor */ - CDynamicObjectArray() - : CSGObject(), m_array() + DynamicObjectArray() + : SGObject(), m_array() { dim1_size = 1; dim2_size = 1; @@ -51,8 +49,8 @@ class CDynamicObjectArray : public CSGObject * @param dim1 dimension 1 * @param dim2 dimension 2 */ - CDynamicObjectArray(size_t dim1, size_t dim2 = 1) - : CSGObject() + DynamicObjectArray(size_t dim1, size_t dim2 = 1) + : SGObject() { dim1_size = dim1; dim2_size = dim2; @@ -60,17 +58,16 @@ class CDynamicObjectArray : public CSGObject init(); } - CDynamicObjectArray(CSGObject** p_array, size_t dim1, size_t dim2, bool p_free_array=true, bool p_copy_array=false) - : CSGObject(), m_array(dim1*dim2) + DynamicObjectArray(std::shared_ptr* p_array, size_t dim1, size_t dim2, bool p_free_array=true, bool p_copy_array=false) + : SGObject(), m_array(p_array, p_array + dim1*dim2) { - m_array.assign(p_array, p_array+(dim1*dim2)); dim1_size = dim1; dim2_size = dim2; init(); } - virtual ~CDynamicObjectArray() { unref_all(); } + virtual ~DynamicObjectArray() { } /** get array size (including granularity buffer) * @@ -97,21 +94,25 @@ class CDynamicObjectArray : public CSGObject * @param index index * @return array element at index */ - inline CSGObject* get_element(size_t index) const + inline std::shared_ptr get_element(size_t index) const + { + return m_array[index]; + } + + template + inline std::shared_ptr get_element(size_t index) const { - auto elem = m_array[index]; - SG_REF(elem); - return elem; + return std::dynamic_pointer_cast(m_array[index]); } /** get array element at index - * - * @param idx1 index 1 - * @param idx2 index 2 - * @param idx3 index 3 - * @return array element at index - */ - inline CSGObject* element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const + * + * @param idx1 index 1 + * @param idx2 index 2 + * @param idx3 index 3 + * @return array element at index + */ + inline std::shared_ptr element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) { return get_element(idx1+dim1_size*(idx2+dim2_size*idx3)); } @@ -120,11 +121,19 @@ class CDynamicObjectArray : public CSGObject * * @return last array element */ - inline CSGObject* get_last_element() const + inline std::shared_ptr get_last_element() const { - auto e = m_array.back(); - SG_REF(e); - return e; + return m_array.back(); + } + + /** get last array element + * + * @return last array element + */ + template + inline std::shared_ptr get_last_element() const + { + return std::dynamic_pointer_cast(m_array.back()); } /** get array element at index @@ -134,9 +143,9 @@ class CDynamicObjectArray : public CSGObject * @param index index * @return array element at index */ - inline CSGObject* get_element_safe(size_t index) const + inline std::shared_ptr get_element_safe(int32_t index) const { - if (index >= m_array.size()) + if (index >= utils::safe_convert(m_array.size())) { error("array index out of bounds ({} >= {})", index, m_array.size()); @@ -144,10 +153,18 @@ class CDynamicObjectArray : public CSGObject return get_element(index); } - inline CSGObject* at(int32_t index) const + template + inline std::shared_ptr get_element_safe(int32_t index) const + { + return std::dynamic_pointer_cast(get_element_safe(index)); + } + +#ifndef SWIG + SG_FORCED_INLINE std::shared_ptr at(int32_t index) const { return get_element_safe(index); } +#endif /** set array element at index * @@ -157,17 +174,11 @@ class CDynamicObjectArray : public CSGObject * @param idx3 index 2 * @return if setting was successful */ - inline bool set_element(CSGObject* e, size_t index) + inline bool set_element(std::shared_ptr e, size_t index) { - CSGObject* old = nullptr; - if (index < m_array.size()) - old = m_array[index]; - else + if (index >= m_array.size()) m_array.resize(index); - - SG_REF(e); m_array[index] = e; - SG_UNREF(old); return true; } @@ -177,9 +188,8 @@ class CDynamicObjectArray : public CSGObject * @param index index * @return if setting was successful */ - inline bool insert_element(CSGObject* e, size_t index) + inline bool insert_element(std::shared_ptr e, int32_t index) { - SG_REF(e); m_array.insert(m_array.begin()+index, e); return true; } @@ -187,29 +197,25 @@ class CDynamicObjectArray : public CSGObject template ::value>> inline bool append_element(T e, const char* name="") { - auto serialized_element = new CSerializable(e, name); - return append_element(serialized_element); + return append_element(std::make_shared>(e, name)); } template inline bool append_element(SGVector e, const char* name="") { - auto serialized_element = new CVectorSerializable(e, name); - return append_element(serialized_element); + return append_element(std::make_shared>(e, name)); } template inline bool append_element(SGMatrix e, const char* name="") { - auto serialized_element = new CMatrixSerializable(e, name); - return append_element(serialized_element); + return append_element(std::make_shared>(e, name)); } template inline bool append_element(const std::vector>& e, const char* name="") { - auto serialized_element = new CVectorListSerializable(e, name); - return append_element(serialized_element); + return append_element(std::make_shared>(e, name)); } /** append array element to the end of array @@ -217,10 +223,9 @@ class CDynamicObjectArray : public CSGObject * @param e element to append * @return if setting was successful */ - inline bool append_element(CSGObject* e) + inline bool append_element(std::shared_ptr e) { m_array.push_back(e); - SG_REF(e); return true; } @@ -229,10 +234,9 @@ class CDynamicObjectArray : public CSGObject * * @param e element to append */ - inline void push_back(CSGObject* e) + inline void push_back(std::shared_ptr e) { m_array.push_back(e); - SG_REF(e); } /** STD VECTOR compatible. Delete array element at the end @@ -240,9 +244,6 @@ class CDynamicObjectArray : public CSGObject */ inline void pop_back() { - auto e = m_array.back(); - SG_UNREF(e); - m_array.pop_back(); } @@ -251,11 +252,9 @@ class CDynamicObjectArray : public CSGObject * * @return element at the end of array */ - inline CSGObject* back() const + inline std::shared_ptr back() const { - auto e=m_array.back(); - SG_REF(e); - return e; + return m_array.back(); } /** find first occurence of array element and return its index @@ -264,7 +263,7 @@ class CDynamicObjectArray : public CSGObject * @param elem element to search for * @return index of element or -1 */ - inline index_t find_element(CSGObject* elem) const + inline int32_t find_element(std::shared_ptr elem) const { auto it = std::find(m_array.begin(), m_array.end(), elem); if (it != m_array.end()) @@ -283,20 +282,17 @@ class CDynamicObjectArray : public CSGObject auto e=m_array[idx]; m_array.erase(std::remove(m_array.begin(), m_array.end(), e), m_array.end()); - SG_UNREF(e); return true; } inline void clear_array() { - unref_all(); m_array.assign(m_array.size(), nullptr); } /** resets the array */ inline void reset_array() { - unref_all(); m_array.clear(); } @@ -305,22 +301,15 @@ class CDynamicObjectArray : public CSGObject * @param orig original array * @return new array */ - inline CDynamicObjectArray& operator=(CDynamicObjectArray& orig) + inline DynamicObjectArray& operator=(DynamicObjectArray& orig) { - /* SG_REF all new elements (implicitly) */ - for (auto& v: orig.m_array) - SG_REF(v); - - /* unref after adding to avoid possible deletion */ - unref_all(); - /* copy pointer DynArray */ m_array=orig.m_array; return *this; } /** @return underlying array of pointers */ - inline CSGObject** get_array() { return m_array.data(); } + inline std::shared_ptr* get_array() { return m_array.data(); } #ifndef SWIG // SWIG should skip this part inline auto begin() @@ -348,7 +337,7 @@ class CDynamicObjectArray : public CSGObject */ virtual void save_serializable_pre() noexcept(false) { - CSGObject::save_serializable_pre(); + SGObject::save_serializable_pre(); m_array.shrink_to_fit(); } @@ -357,21 +346,14 @@ class CDynamicObjectArray : public CSGObject virtual void init() { watch_param("array", &m_array); + SG_ADD(&dim1_size, "dim1_size", "Dimension 1"); SG_ADD(&dim2_size, "dim2_size", "Dimension 2"); } - /** de-reference all elements of this array once */ - inline void unref_all() - { - /* SG_UNREF all my elements */ - for (auto& o: m_array) - SG_UNREF(o); - } - private: /** underlying array */ - std::vector m_array; + std::vector> m_array; /** dimension 1 */ index_t dim1_size; diff --git a/src/shogun/lib/Hash.cpp b/src/shogun/lib/Hash.cpp index 6924f194e80..5f4a3d2478b 100644 --- a/src/shogun/lib/Hash.cpp +++ b/src/shogun/lib/Hash.cpp @@ -12,7 +12,7 @@ using namespace shogun; -uint32_t CHash::crc32(uint8_t *data, int32_t len) +uint32_t Hash::crc32(uint8_t *data, int32_t len) { uint32_t result; int32_t i,j; @@ -39,7 +39,7 @@ uint32_t CHash::crc32(uint8_t *data, int32_t len) return ~result; } -void CHash::MD5(unsigned char *x, unsigned l, unsigned char *buf) +void Hash::MD5(unsigned char *x, unsigned l, unsigned char *buf) { struct MD5Context ctx; @@ -70,7 +70,7 @@ void byteReverse(unsigned char *buf, unsigned uint32_t longs) #endif #endif -void CHash::MD5Init(struct MD5Context *ctx) +void Hash::MD5Init(struct MD5Context *ctx) { ctx->buf[0] = 0x67452301; ctx->buf[1] = 0xefcdab89; @@ -81,7 +81,7 @@ void CHash::MD5Init(struct MD5Context *ctx) ctx->bits[1] = 0; } -void CHash::MD5Update(struct MD5Context *ctx, unsigned char const *buf, +void Hash::MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) { uint32_t t; @@ -126,7 +126,7 @@ void CHash::MD5Update(struct MD5Context *ctx, unsigned char const *buf, sg_memcpy(ctx->in, buf, len); } -void CHash::MD5Final(unsigned char digest[16], struct MD5Context *ctx) +void Hash::MD5Final(unsigned char digest[16], struct MD5Context *ctx) { unsigned count; unsigned char *p; @@ -186,7 +186,7 @@ void CHash::MD5Final(unsigned char digest[16], struct MD5Context *ctx) ( w += f(x, y, z) + data, w = w<>(32-s), w += x ) #endif -void CHash::MD5Transform(uint32_t buf[4], uint32_t const in[16]) +void Hash::MD5Transform(uint32_t buf[4], uint32_t const in[16]) { uint32_t a, b, c, d; @@ -340,22 +340,22 @@ void CHash::MD5Transform(uint32_t buf[4], uint32_t const in[16]) } #endif -uint32_t CHash::MurmurHash3(uint8_t* data, int32_t len, uint32_t seed) +uint32_t Hash::MurmurHash3(uint8_t* data, int32_t len, uint32_t seed) { return PMurHash32(seed, data, len); } -void CHash::IncrementalMurmurHash3(uint32_t *ph1, uint32_t *pcarry, uint8_t* data, int32_t len) +void Hash::IncrementalMurmurHash3(uint32_t *ph1, uint32_t *pcarry, uint8_t* data, int32_t len) { PMurHash32_Process(ph1, pcarry, data, len); } -uint32_t CHash::FinalizeIncrementalMurmurHash3(uint32_t h, uint32_t carry, uint32_t total_length) +uint32_t Hash::FinalizeIncrementalMurmurHash3(uint32_t h, uint32_t carry, uint32_t total_length) { return PMurHash32_Result(h, carry, total_length); } -uint32_t CHash::MurmurHashString(substring s, uint32_t h) +uint32_t Hash::MurmurHashString(substring s, uint32_t h) { uint32_t ret = 0; diff --git a/src/shogun/lib/Hash.h b/src/shogun/lib/Hash.h index bb1d68d1c16..772171a8fdf 100644 --- a/src/shogun/lib/Hash.h +++ b/src/shogun/lib/Hash.h @@ -23,13 +23,13 @@ struct substring; * crc32, md5 and murmur. * */ -class CHash : public CSGObject +class Hash : public SGObject { public: /** default constructor */ - CHash() {} + Hash() {} /** default destructor */ - virtual ~CHash() {} + virtual ~Hash() {} /** crc32 checksumming * diff --git a/src/shogun/lib/IndexBlock.cpp b/src/shogun/lib/IndexBlock.cpp index a58babf1b77..4a7a5a2ad55 100644 --- a/src/shogun/lib/IndexBlock.cpp +++ b/src/shogun/lib/IndexBlock.cpp @@ -9,42 +9,42 @@ using namespace shogun; -CIndexBlock::CIndexBlock() : CSGObject(), +IndexBlock::IndexBlock() : SGObject(), m_min_index(0), m_max_index(0), m_weight(1.0), m_sub_blocks(NULL) { - m_sub_blocks = new CList(true); - SG_REF(m_sub_blocks); + m_sub_blocks = std::make_shared(true); + } -CIndexBlock::CIndexBlock(index_t min_index, index_t max_index, +IndexBlock::IndexBlock(index_t min_index, index_t max_index, float64_t weight, const char* name) : - CSGObject(), m_min_index(min_index), m_max_index(max_index), + SGObject(), m_min_index(min_index), m_max_index(max_index), m_weight(weight), m_sub_blocks(NULL) { - m_sub_blocks = new CList(true); - SG_REF(m_sub_blocks); + m_sub_blocks = std::make_shared(true); + } -CIndexBlock::~CIndexBlock() +IndexBlock::~IndexBlock() { - SG_UNREF(m_sub_blocks); + } -void CIndexBlock::add_sub_block(CIndexBlock* sub_block) +void IndexBlock::add_sub_block(std::shared_ptr sub_block) { ASSERT(sub_block->get_min_index()>=m_min_index) ASSERT(sub_block->get_max_index()<=m_max_index) m_sub_blocks->append_element(sub_block); } -CList* CIndexBlock::get_sub_blocks() +std::shared_ptr IndexBlock::get_sub_blocks() { - SG_REF(m_sub_blocks); + return m_sub_blocks; } -int32_t CIndexBlock::get_num_sub_blocks() +int32_t IndexBlock::get_num_sub_blocks() { return m_sub_blocks->get_num_elements(); } diff --git a/src/shogun/lib/IndexBlock.h b/src/shogun/lib/IndexBlock.h index 23905c17937..b7c16485212 100644 --- a/src/shogun/lib/IndexBlock.h +++ b/src/shogun/lib/IndexBlock.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Heiko Strathmann, Yuyu Zhang, Thoralf Klein, + * Authors: Sergey Lisitsyn, Heiko Strathmann, Yuyu Zhang, Thoralf Klein, * Bjoern Esser, Soeren Sonnenburg */ @@ -15,17 +15,17 @@ namespace shogun { -class CList; +class List; /** @brief class IndexBlock used to represent * contiguous indices of one group (e.g. block of related features) */ -class CIndexBlock : public CSGObject +class IndexBlock : public SGObject { public: /** default constructor */ - CIndexBlock(); + IndexBlock(); /** constructor * @param min_index smallest index of the index block @@ -33,11 +33,11 @@ class CIndexBlock : public CSGObject * @param weight weight (optional) * @param name name of task (optional) */ - CIndexBlock(index_t min_index, index_t max_index, + IndexBlock(index_t min_index, index_t max_index, float64_t weight=1.0, const char* name="task"); /** destructor */ - ~CIndexBlock(); + ~IndexBlock(); /** get min index */ index_t get_min_index() const { return m_min_index; } @@ -56,7 +56,7 @@ class CIndexBlock : public CSGObject virtual const char* get_name() const { return "IndexBlock"; }; /** get subtasks */ - CList* get_sub_blocks(); + std::shared_ptr get_sub_blocks(); /** get num subtasks */ int32_t get_num_sub_blocks(); @@ -64,7 +64,7 @@ class CIndexBlock : public CSGObject /** adds sub-block * @param sub_block subtask to add */ - void add_sub_block(CIndexBlock* sub_block); + void add_sub_block(std::shared_ptr sub_block); private: /** min index */ @@ -77,7 +77,7 @@ class CIndexBlock : public CSGObject float64_t m_weight; /** subtasks */ - CList* m_sub_blocks; + std::shared_ptr m_sub_blocks; }; diff --git a/src/shogun/lib/IndexBlockGroup.cpp b/src/shogun/lib/IndexBlockGroup.cpp index 28606339dc1..5fe9716db8f 100644 --- a/src/shogun/lib/IndexBlockGroup.cpp +++ b/src/shogun/lib/IndexBlockGroup.cpp @@ -10,43 +10,42 @@ using namespace shogun; -CIndexBlockGroup::CIndexBlockGroup() : CIndexBlockRelation() +IndexBlockGroup::IndexBlockGroup() : IndexBlockRelation() { - m_blocks = new CList(true); + m_blocks = std::make_shared(true); } -CIndexBlockGroup::~CIndexBlockGroup() +IndexBlockGroup::~IndexBlockGroup() { - SG_UNREF(m_blocks); + } -void CIndexBlockGroup::add_block(CIndexBlock* block) +void IndexBlockGroup::add_block(std::shared_ptr block) { m_blocks->push(block); } -void CIndexBlockGroup::remove_block(CIndexBlock* block) +void IndexBlockGroup::remove_block(std::shared_ptr block) { not_implemented(SOURCE_LOCATION); } -SGVector CIndexBlockGroup::get_SLEP_ind() +SGVector IndexBlockGroup::get_SLEP_ind() { check_blocks_list(m_blocks); int32_t n_sub_blocks = m_blocks->get_num_elements(); SG_DEBUG("Number of sub-blocks = {}", n_sub_blocks) SGVector ind(n_sub_blocks+1); - CIndexBlock* iterator = (CIndexBlock*)(m_blocks->get_first_element()); + auto iterator = std::dynamic_pointer_cast(m_blocks->get_first_element()); ind[0] = 0; int32_t i = 0; do { ind[i+1] = iterator->get_max_index(); - SG_UNREF(iterator); i++; } - while ((iterator = (CIndexBlock*)m_blocks->get_next_element()) != NULL); + while ((iterator = std::dynamic_pointer_cast(m_blocks->get_next_element())) != NULL); //ind.display_vector("ind"); return ind; diff --git a/src/shogun/lib/IndexBlockGroup.h b/src/shogun/lib/IndexBlockGroup.h index 8ef1fe9775d..c12f44a516d 100644 --- a/src/shogun/lib/IndexBlockGroup.h +++ b/src/shogun/lib/IndexBlockGroup.h @@ -15,8 +15,8 @@ namespace shogun { -class CIndexBlock; -class CList; +class IndexBlock; +class List; /** @brief class IndexBlockGroup used to represent * group-based feature relation. @@ -24,25 +24,25 @@ class CList; * Currently can be constructed with a few CIndexBlock * instances. */ -class CIndexBlockGroup : public CIndexBlockRelation +class IndexBlockGroup : public IndexBlockRelation { public: /** default constructor */ - CIndexBlockGroup(); + IndexBlockGroup(); /** destructor */ - virtual ~CIndexBlockGroup(); + virtual ~IndexBlockGroup(); /** add IndexBlock to the group * @param block IndexBlock to add */ - void add_block(CIndexBlock* block); + void add_block(std::shared_ptr block); /** remove IndexBlock from the group * @param block IndexBlock to remove */ - void remove_block(CIndexBlock* block); + void remove_block(std::shared_ptr block); /** returns information about IndexBlocks in * SLEP "ind" format @@ -57,7 +57,7 @@ class CIndexBlockGroup : public CIndexBlockRelation protected: /** blocks in group */ - CList* m_blocks; + std::shared_ptr m_blocks; }; diff --git a/src/shogun/lib/IndexBlockRelation.cpp b/src/shogun/lib/IndexBlockRelation.cpp index 6312f641db6..bd79f62afce 100644 --- a/src/shogun/lib/IndexBlockRelation.cpp +++ b/src/shogun/lib/IndexBlockRelation.cpp @@ -10,25 +10,24 @@ using namespace shogun; -bool CIndexBlockRelation::check_blocks_list(CList* blocks) +bool IndexBlockRelation::check_blocks_list(std::shared_ptr blocks) { int32_t n_sub_blocks = blocks->get_num_elements(); index_t* min_idxs = SG_MALLOC(index_t, n_sub_blocks); index_t* max_idxs = SG_MALLOC(index_t, n_sub_blocks); index_t* block_idxs_min = SG_MALLOC(index_t, n_sub_blocks); index_t* block_idxs_max = SG_MALLOC(index_t, n_sub_blocks); - CIndexBlock* iter_block = (CIndexBlock*)(blocks->get_first_element()); + auto iter_block = std::static_pointer_cast(blocks->get_first_element()); for (int32_t i=0; iget_min_index(); max_idxs[i] = iter_block->get_max_index(); block_idxs_min[i] = i; block_idxs_max[i] = i; - SG_UNREF(iter_block); - iter_block = (CIndexBlock*)(blocks->get_next_element()); + iter_block = std::static_pointer_cast(blocks->get_next_element()); } - CMath::qsort_index(min_idxs, block_idxs_min, n_sub_blocks); - CMath::qsort_index(max_idxs, block_idxs_max, n_sub_blocks); + Math::qsort_index(min_idxs, block_idxs_min, n_sub_blocks); + Math::qsort_index(max_idxs, block_idxs_max, n_sub_blocks); for (int32_t i=0; i blocks); }; diff --git a/src/shogun/lib/IndexBlockTree.cpp b/src/shogun/lib/IndexBlockTree.cpp index b6fb8ecb84c..970751bdfe8 100644 --- a/src/shogun/lib/IndexBlockTree.cpp +++ b/src/shogun/lib/IndexBlockTree.cpp @@ -87,40 +87,38 @@ void fill_ind_recursive(tree_node_t* node, vector* tree_nodes } } -void collect_tree_nodes_recursive(CIndexBlock* subtree_root_block, vector* tree_nodes) +void collect_tree_nodes_recursive(std::shared_ptr subtree_root_block, vector* tree_nodes) { - CList* sub_blocks = subtree_root_block->get_sub_blocks(); + auto sub_blocks = subtree_root_block->get_sub_blocks(); if (sub_blocks->get_num_elements()>0) { - CIndexBlock* iterator = (CIndexBlock*)sub_blocks->get_first_element(); + auto iterator = std::static_pointer_cast(sub_blocks->get_first_element()); do { SG_DEBUG("Block [{} {}] ",iterator->get_min_index(), iterator->get_max_index()) tree_nodes->push_back(block_tree_node_t(iterator->get_min_index(),iterator->get_max_index(),iterator->get_weight())); if (iterator->get_num_sub_blocks()>0) collect_tree_nodes_recursive(iterator, tree_nodes); - SG_UNREF(iterator); } - while ((iterator = (CIndexBlock*)sub_blocks->get_next_element()) != NULL); + while ((iterator = std::static_pointer_cast(sub_blocks->get_next_element())) != NULL); } - SG_UNREF(sub_blocks); } -CIndexBlockTree::CIndexBlockTree() : - CIndexBlockRelation(), m_root_block(NULL), +IndexBlockTree::IndexBlockTree() : + IndexBlockRelation(), m_root_block(NULL), m_general(false) { } -CIndexBlockTree::CIndexBlockTree(CIndexBlock* root_block) : CIndexBlockRelation(), +IndexBlockTree::IndexBlockTree(std::shared_ptr root_block) : IndexBlockRelation(), m_root_block(NULL), m_general(false) { set_root_block(root_block); } -CIndexBlockTree::CIndexBlockTree(SGMatrix adjacency_matrix, bool include_supernode) : - CIndexBlockRelation(), +IndexBlockTree::IndexBlockTree(SGMatrix adjacency_matrix, bool include_supernode) : + IndexBlockRelation(), m_root_block(NULL), m_general(true) { ASSERT(adjacency_matrix.num_rows == adjacency_matrix.num_cols) @@ -215,56 +213,56 @@ CIndexBlockTree::CIndexBlockTree(SGMatrix adjacency_matrix, bool incl SG_FREE(nodes); } -CIndexBlockTree::CIndexBlockTree(SGVector G, SGVector ind_t) : - CIndexBlockRelation(), +IndexBlockTree::IndexBlockTree(SGVector G, SGVector ind_t) : + IndexBlockRelation(), m_root_block(NULL), m_general(true) { m_precomputed_G = G; m_precomputed_ind_t = ind_t; } -CIndexBlockTree::CIndexBlockTree(SGVector ind_t) : - CIndexBlockRelation(), +IndexBlockTree::IndexBlockTree(SGVector ind_t) : + IndexBlockRelation(), m_root_block(NULL), m_general(false) { m_precomputed_ind_t = ind_t; } -CIndexBlockTree::~CIndexBlockTree() +IndexBlockTree::~IndexBlockTree() { - SG_UNREF(m_root_block); + } -CIndexBlock* CIndexBlockTree::get_root_block() const +std::shared_ptr IndexBlockTree::get_root_block() const { - SG_REF(m_root_block); + return m_root_block; } -void CIndexBlockTree::set_root_block(CIndexBlock* root_block) +void IndexBlockTree::set_root_block(std::shared_ptr root_block) { - SG_REF(root_block); - SG_UNREF(m_root_block); + + m_root_block = root_block; } -SGVector CIndexBlockTree::get_SLEP_ind() +SGVector IndexBlockTree::get_SLEP_ind() { not_implemented(SOURCE_LOCATION); return SGVector(); } -SGVector CIndexBlockTree::get_SLEP_G() +SGVector IndexBlockTree::get_SLEP_G() { return m_precomputed_G; } -bool CIndexBlockTree::is_general() const +bool IndexBlockTree::is_general() const { return m_general; } -SGVector CIndexBlockTree::get_SLEP_ind_t() const +SGVector IndexBlockTree::get_SLEP_ind_t() const { if (m_precomputed_ind_t.vlen) return m_precomputed_ind_t; @@ -272,7 +270,7 @@ SGVector CIndexBlockTree::get_SLEP_ind_t() const else { ASSERT(m_root_block) - CList* blocks = new CList(true); + auto blocks = std::make_shared(true); vector tree_nodes = vector(); @@ -291,7 +289,7 @@ SGVector CIndexBlockTree::get_SLEP_ind_t() const ind_t[3+i*3+2] = tree_nodes[i].weight; } - SG_UNREF(blocks); + return ind_t; } diff --git a/src/shogun/lib/IndexBlockTree.h b/src/shogun/lib/IndexBlockTree.h index 6f9aca44f32..62108121704 100644 --- a/src/shogun/lib/IndexBlockTree.h +++ b/src/shogun/lib/IndexBlockTree.h @@ -16,7 +16,7 @@ namespace shogun { -class CIndexBlock; +class IndexBlock; /** @brief class IndexBlockTree used to represent * tree guided feature relation. @@ -24,23 +24,23 @@ class CIndexBlock; * Can be constructed via CIndexBlock instance having * sub blocks, adjacency matrix or precomputed indices. */ -class CIndexBlockTree : public CIndexBlockRelation +class IndexBlockTree : public IndexBlockRelation { public: /** default constructor */ - CIndexBlockTree(); + IndexBlockTree(); /** constructor from index block * @param root_block root block of the tree */ - CIndexBlockTree(CIndexBlock* root_block); + IndexBlockTree(std::shared_ptr root_block); /** constructor from adjacency matrix * @param adjacency_matrix adjacency matrix * @param include_supernode whether to include supernode */ - CIndexBlockTree(SGMatrix adjacency_matrix, bool include_supernode); + IndexBlockTree(SGMatrix adjacency_matrix, bool include_supernode); #ifndef SWIG /** constructor from general precomputed indices @@ -49,7 +49,7 @@ class CIndexBlockTree : public CIndexBlockRelation * @param G custom G containing mapping indices * @param ind_t custom ind_t containing flatten parameters of each node [min,max,weight] */ - CIndexBlockTree(SGVector G, SGVector ind_t); + IndexBlockTree(SGVector G, SGVector ind_t); #endif /** constructor from basic precomputed indices @@ -57,16 +57,16 @@ class CIndexBlockTree : public CIndexBlockRelation * and weight ind_t.weight * @param ind_t custom ind_t containing flatten parameters of each node [min,max,weight] */ - CIndexBlockTree(SGVector ind_t); + IndexBlockTree(SGVector ind_t); /** destructor */ - virtual ~CIndexBlockTree(); + virtual ~IndexBlockTree(); /** get root IndexBlock */ - CIndexBlock* get_root_block() const; + std::shared_ptr get_root_block() const; /** set root block */ - void set_root_block(CIndexBlock* root_block); + void set_root_block(std::shared_ptr root_block); /** returns information about blocks in * SLEP "ind" format @@ -95,7 +95,7 @@ class CIndexBlockTree : public CIndexBlockRelation protected: /** root block */ - CIndexBlock* m_root_block; + std::shared_ptr m_root_block; /** general */ bool m_general; diff --git a/src/shogun/lib/List.h b/src/shogun/lib/List.h index 5c9b1cfe677..50859c3c4ff 100644 --- a/src/shogun/lib/List.h +++ b/src/shogun/lib/List.h @@ -17,11 +17,11 @@ namespace shogun { /** @brief Class ListElement, defines how an element of the the list looks like */ -class CListElement :public CSGObject +class ListElement :public SGObject { public: /** default constructor */ - CListElement() + ListElement() : next(NULL), prev(NULL), data(NULL) { init(); @@ -33,9 +33,9 @@ class CListElement :public CSGObject * @param p_prev previous element * @param p_next next element */ - CListElement(CSGObject* p_data, - CListElement* p_prev = NULL, - CListElement* p_next = NULL) + ListElement(std::shared_ptr p_data, + std::shared_ptr p_prev = NULL, + std::shared_ptr p_next = NULL) { init(); @@ -45,7 +45,7 @@ class CListElement :public CSGObject } /// destructor - virtual ~CListElement() { data = NULL; } + virtual ~ListElement() { data = NULL; } /** @return object name */ virtual const char* get_name() const { return "ListElement"; } @@ -53,17 +53,17 @@ class CListElement :public CSGObject private: void init() { - SG_ADD(&data, "data", "Data of this element."); - SG_ADD(&next, "next", "Next element in list."); + /*SG_ADD(&data, "data", "Data of this element.")*/; + /*SG_ADD(&next, "next", "Next element in list.")*/; } public: /** next element in list */ - CListElement* next; + std::shared_ptr next; /** previous element in list */ - CListElement* prev; + std::shared_ptr prev; /** data of this element */ - CSGObject* data; + std::shared_ptr data; }; @@ -72,23 +72,23 @@ class CListElement :public CSGObject * For higher level objects pointers should be used. The list supports calling * delete() of an object that is to be removed from the list. */ -class CList : public CSGObject +class List : public SGObject { public: /** constructor * * @param p_delete_data if data shall be deleted */ - CList(bool p_delete_data=false) : CSGObject() + List(bool p_delete_data=false) : SGObject() { - m_parameters->add(&delete_data, "delete_data", - "Delete data on destruction?"); - m_parameters->add(&num_elements, "num_elements", - "Number of elements."); - m_parameters->add((CSGObject**) &first, "first", - "First element in list."); - m_model_selection_parameters->add((CSGObject**) &first, "first", - "First element in list."); + /*m_parameters->add(&delete_data, "delete_data", + "Delete data on destruction?")*/; + /*m_parameters->add(&num_elements, "num_elements", + "Number of elements.")*/; + /*m_parameters->add((SGObject**) &first, "first", + "First element in list.")*/; + /*m_model_selection_parameters->add((SGObject**) &first, "first", + "First element in list.")*/; first = NULL; current = NULL; @@ -98,7 +98,7 @@ class CList : public CSGObject this->delete_data=p_delete_data; } - virtual ~CList() + virtual ~List() { SG_TRACE("Destroying List {}", fmt::ptr(this)); @@ -109,11 +109,7 @@ class CList : public CSGObject inline void delete_all_elements() { // move to the first element and then delete sequentially - CSGObject* d=get_first_element(); - - // important to unref because get_first_elements() SG_REFs it - if (delete_data) - SG_UNREF(d); + auto d=get_first_element(); while (get_num_elements()) { @@ -139,84 +135,71 @@ class CList : public CSGObject * * @return first element in list or NULL if list is empty */ - inline CSGObject* get_first_element() + inline std::shared_ptr get_first_element() { if (first != NULL) { current = first; if (delete_data) - SG_REF(current->data); + return current->data; } - else - return NULL; + return NULL; } /** go to last element in list and return it * * @return last element in list or NULL if list is empty */ - inline CSGObject* get_last_element() + inline std::shared_ptr get_last_element() { if (last != NULL) { current = last; - if (delete_data) - SG_REF(current->data); return current->data; } - else - return NULL; + return NULL; } /** go to next element in list and return it * * @return next element in list or NULL if list is empty */ - inline CSGObject* get_next_element() + inline std::shared_ptr get_next_element() { if ((current != NULL) && (current->next != NULL)) { current = current->next; - if (delete_data) - SG_REF(current->data); return current->data; } - else - return NULL; + return NULL; } /** go to previous element in list and return it * * @return previous element in list or NULL if list is empty */ - inline CSGObject* get_previous_element() + inline std::shared_ptr get_previous_element() { if ((current != NULL) && (current->prev != NULL)) { current = current->prev; - if (delete_data) - SG_REF(current->data); return current->data; } - else - return NULL; + return NULL; } /** get current element in list * * @return current element in list or NULL if not available */ - inline CSGObject* get_current_element() + inline std::shared_ptr get_current_element() { if (current != NULL) { - if (delete_data) - SG_REF(current->data); return current->data; } - else - return NULL; + return NULL; } @@ -228,17 +211,14 @@ class CList : public CSGObject * @param p_current current list element * @return first element in list or NULL if list is empty */ - inline CSGObject* get_first_element(CListElement*& p_current) + inline std::shared_ptr get_first_element(std::shared_ptr& p_current) { if (first != NULL) { p_current = first; - if (delete_data) - SG_REF(p_current->data); return p_current->data; } - else - return NULL; + return NULL; } /** go to last element in list and return it @@ -246,17 +226,14 @@ class CList : public CSGObject * @param p_current current list element * @return last element in list or NULL if list is empty */ - inline CSGObject* get_last_element(CListElement*& p_current) + inline std::shared_ptr get_last_element(std::shared_ptr& p_current) { if (last != NULL) { p_current = last; - if (delete_data) - SG_REF(p_current->data); return p_current->data; } - else - return NULL; + return NULL; } /** go to next element in list and return it @@ -264,17 +241,14 @@ class CList : public CSGObject * @param p_current current list element * @return next element in list or NULL if list is empty */ - inline CSGObject* get_next_element(CListElement*& p_current) + inline std::shared_ptr get_next_element(std::shared_ptr& p_current) { if ((p_current != NULL) && (p_current->next != NULL)) { p_current = p_current->next; - if (delete_data) - SG_REF(p_current->data); return p_current->data; } - else - return NULL; + return NULL; } /** go to previous element in list and return it @@ -282,17 +256,14 @@ class CList : public CSGObject * @param p_current current list element * @return previous element in list or NULL if list is empty */ - inline CSGObject* get_previous_element(CListElement*& p_current) + inline std::shared_ptr get_previous_element(std::shared_ptr& p_current) { if ((p_current != NULL) && (p_current->prev != NULL)) { p_current = p_current->prev; - if (delete_data) - SG_REF(p_current->data); return p_current->data; } - else - return NULL; + return NULL; } /** get current element in list @@ -300,16 +271,13 @@ class CList : public CSGObject * @param p_current current list element * @return current element in list or NULL if not available */ - inline CSGObject* get_current_element(CListElement*& p_current) + inline std::shared_ptr get_current_element(std::shared_ptr& p_current) { if (p_current != NULL) { - if (delete_data) - SG_REF(p_current->data); return p_current->data; } - else - return NULL; + return NULL; } //@} @@ -319,18 +287,16 @@ class CList : public CSGObject * @param data data element to append * @return if appending was successful */ - inline bool append_element(CSGObject* data) + inline bool append_element(std::shared_ptr data) { SG_TRACE("Entering"); // none available, case is shattered in insert_element() if (current != NULL) { - CSGObject* e=get_next_element(); + auto e=get_next_element(); if (e) { - if (delete_data) - SG_UNREF(e); // if successor exists use insert_element() SG_TRACE("Leaving"); return insert_element(data); @@ -338,9 +304,9 @@ class CList : public CSGObject else { // case with no successor but nonempty - CListElement* element; + std::shared_ptr element; - if ((element = new CListElement(data, current)) != NULL) + if ((element = std::make_shared(data, current)) != NULL) { current->next = element; current = element; @@ -348,9 +314,6 @@ class CList : public CSGObject num_elements++; - if (delete_data) - SG_REF(data); - SG_TRACE("Leaving"); return true; } @@ -374,12 +337,9 @@ class CList : public CSGObject * @param data data element to append * @return if appending was successful */ - inline bool append_element_at_listend(CSGObject* data) + inline bool append_element_at_listend(std::shared_ptr data) { - CSGObject* p = get_last_element(); - if (delete_data) - SG_UNREF(p); - + auto p = get_last_element(); return append_element(data); } @@ -388,7 +348,7 @@ class CList : public CSGObject * @param data data element to append * @return if appending was successful */ - inline bool push(CSGObject* data) + inline bool push(std::shared_ptr data) { return append_element_at_listend(data); } @@ -412,12 +372,10 @@ class CList : public CSGObject current=current->prev; } - if (delete_data) - SG_UNREF(last->data); - CListElement* temp=last; + auto temp=last; last=last->prev; - SG_UNREF(temp); + if (last) last->next=NULL; @@ -435,16 +393,13 @@ class CList : public CSGObject * @param data data element to insert * @return if inserting was successful */ - inline bool insert_element(CSGObject* data) + inline bool insert_element(std::shared_ptr data) { - CListElement* element; - - if (delete_data) - SG_REF(data); + std::shared_ptr element; if (current == NULL) { - if ((element = new CListElement(data)) != NULL) + if ((element = std::make_shared(data)) != NULL) { current = element; first = element; @@ -462,7 +417,7 @@ class CList : public CSGObject } else { - if ((element = new CListElement(data, current->prev, current)) != NULL) + if ((element = std::make_shared(data, current->prev, current)) != NULL) { if (current->prev != NULL) current->prev->next = element; @@ -490,24 +445,17 @@ class CList : public CSGObject * * @return the elements data - if available - otherwise NULL */ - inline CSGObject* delete_element() + inline std::shared_ptr delete_element() { SG_TRACE("Entering"); - CSGObject* data = current ? current->data : NULL; + auto data = current ? current->data : NULL; if (num_elements>0) num_elements--; if (data) { - if (delete_data) - { - SG_TRACE("Decreasing refcount of {}({})!", - data->get_name(), fmt::ptr(data)); - SG_UNREF(data); - } - - CListElement *element = current; + auto element = current; if (element->prev) element->prev->next = element->next; @@ -526,7 +474,7 @@ class CList : public CSGObject if (element == last) last = element->prev; - delete element; + element.reset(); SG_TRACE("Leaving"); return data; @@ -538,11 +486,11 @@ class CList : public CSGObject virtual void load_serializable_post() noexcept(false) { - CSGObject::load_serializable_post(); + SGObject::load_serializable_post(); current = first; - CListElement* prev = NULL; - for (CListElement* cur=first; cur!=NULL; cur=cur->next) + std::shared_ptr prev = NULL; + for (auto cur=first; cur!=NULL; cur=cur->next) { cur->prev = prev; prev = cur; @@ -553,11 +501,11 @@ class CList : public CSGObject /** print all elements of the list */ void print_list() { - CListElement* c=first; + auto c=first; while (c) { - io::print("\"{}\" at {}\n", c->data ? c->data->get_name() : "", fmt::ptr(c->data)); + io::print("\"{}\" at {}\n", c->data ? c->data->get_name() : "", fmt::ptr(c->data.get())); c=c->next; } } @@ -572,11 +520,11 @@ class CList : public CSGObject /** if data is to be deleted on object destruction */ bool delete_data; /** first element in list */ - CListElement* first; + std::shared_ptr first; /** current element in list */ - CListElement* current; + std::shared_ptr current; /** last element in list */ - CListElement* last; + std::shared_ptr last; /** number of elements */ int32_t num_elements; }; diff --git a/src/shogun/lib/Map.h b/src/shogun/lib/Map.h index 3524e1f8820..14949ca16bb 100644 --- a/src/shogun/lib/Map.h +++ b/src/shogun/lib/Map.h @@ -55,7 +55,7 @@ IGNORE_IN_CLASSLIST template struct CMapNode /** @brief the class CMap, a map based on the hash-table. * w: http://en.wikipedia.org/wiki/Hash_table */ -IGNORE_IN_CLASSLIST template class CMap: public CSGObject +IGNORE_IN_CLASSLIST template class CMap: public SGObject { public: /** Custom constructor */ @@ -442,7 +442,7 @@ IGNORE_IN_CLASSLIST template class CMap: public CSGObject */ virtual uint32_t get_hash_value(const K& key) { - return CHash::MurmurHash3((uint8_t*)(&key), sizeof(key), 0xDEADBEEF); + return Hash::MurmurHash3((uint8_t*)(&key), sizeof(key), 0xDEADBEEF); } /** whether SG_MALLOC or just malloc etc shall be used */ diff --git a/src/shogun/lib/NGramTokenizer.cpp b/src/shogun/lib/NGramTokenizer.cpp index fd74540fbcf..8a51705dbbf 100644 --- a/src/shogun/lib/NGramTokenizer.cpp +++ b/src/shogun/lib/NGramTokenizer.cpp @@ -11,52 +11,52 @@ namespace shogun { -CNGramTokenizer::CNGramTokenizer(int32_t ns) : CTokenizer() +NGramTokenizer::NGramTokenizer(int32_t ns) : Tokenizer() { n = ns; last_idx = 0; init(); } -CNGramTokenizer::CNGramTokenizer(const CNGramTokenizer& orig) -: CTokenizer(orig) +NGramTokenizer::NGramTokenizer(const NGramTokenizer& orig) +: Tokenizer(orig) { - CTokenizer::set_text(orig.text); + Tokenizer::set_text(orig.text); n = orig.n; init(); } -void CNGramTokenizer::init() +void NGramTokenizer::init() { SG_ADD(&n, "n", "Size of n-grams"); SG_ADD(&last_idx, "last_idx", "Index of last token"); } -void CNGramTokenizer::set_text(SGVector txt) +void NGramTokenizer::set_text(SGVector txt) { last_idx = 0; - CTokenizer::set_text(txt); + Tokenizer::set_text(txt); } -const char* CNGramTokenizer::get_name() const +const char* NGramTokenizer::get_name() const { return "NGramTokenizer"; } -bool CNGramTokenizer::has_next() +bool NGramTokenizer::has_next() { return last_idx<=text.size()-n; } -index_t CNGramTokenizer::next_token_idx(index_t& start) +index_t NGramTokenizer::next_token_idx(index_t& start) { start = last_idx++; return start + n; } -CNGramTokenizer* CNGramTokenizer::get_copy() +NGramTokenizer* NGramTokenizer::get_copy() { - CNGramTokenizer* t = new CNGramTokenizer(n); + NGramTokenizer* t = new NGramTokenizer(n); return t; } } diff --git a/src/shogun/lib/NGramTokenizer.h b/src/shogun/lib/NGramTokenizer.h index 073ef8fbc3c..a414f58b3c4 100644 --- a/src/shogun/lib/NGramTokenizer.h +++ b/src/shogun/lib/NGramTokenizer.h @@ -15,10 +15,10 @@ namespace shogun { template class SGVector; -/** @brief The class CNGramTokenizer is used to tokenize +/** @brief The class NGramTokenizer is used to tokenize * a SGVector into n-grams */ -class CNGramTokenizer: public CTokenizer +class NGramTokenizer: public Tokenizer { public: @@ -26,16 +26,16 @@ class CNGramTokenizer: public CTokenizer * * @param ns N-grams' size */ - CNGramTokenizer(int32_t ns=3); + NGramTokenizer(int32_t ns=3); /** copy constructor * * @param orig the original NGramTokenizer */ - CNGramTokenizer(const CNGramTokenizer& orig); + NGramTokenizer(const NGramTokenizer& orig); /** destructor */ - virtual ~CNGramTokenizer() {} + virtual ~NGramTokenizer() {} /** Set the char array that requires tokenization * @@ -65,7 +65,7 @@ class CNGramTokenizer: public CTokenizer */ virtual const char* get_name() const; - virtual CNGramTokenizer* get_copy(); + virtual NGramTokenizer* get_copy(); private: void init(); diff --git a/src/shogun/lib/OpenCV/CV2SGFactory.h b/src/shogun/lib/OpenCV/CV2SGFactory.h index c98f5feaa86..7de54a3b1af 100644 --- a/src/shogun/lib/OpenCV/CV2SGFactory.h +++ b/src/shogun/lib/OpenCV/CV2SGFactory.h @@ -65,14 +65,14 @@ class CV2SGFactory * @param cv::Mat to be converted * @return DenseFeatures pointer of the specified data type */ - template static CDenseFeatures* get_dense_features(cv::Mat); + template static DenseFeatures* get_dense_features(cv::Mat); }; -template CDenseFeatures* CV2SGFactory::get_dense_features +template DenseFeatures* CV2SGFactory::get_dense_features (cv::Mat cvMat) { SGMatrix sgMat=CV2SGFactory::get_sgmatrix(cvMat); - CDenseFeatures* features=new CDenseFeatures(sgMat); + DenseFeatures* features=new DenseFeatures(sgMat); return features; } diff --git a/src/shogun/lib/OpenCV/SG2CVFactory.h b/src/shogun/lib/OpenCV/SG2CVFactory.h index 116bf92d066..aab49eedaf4 100644 --- a/src/shogun/lib/OpenCV/SG2CVFactory.h +++ b/src/shogun/lib/OpenCV/SG2CVFactory.h @@ -66,7 +66,7 @@ class SG2CVFactory * @return Mat object of the specified data type */ template static cv::Mat get_cvMat_from_features - (CDenseFeatures* sgDense, int cv_type); + (DenseFeatures* sgDense, int cv_type); private: template static cv::Mat get_cvMat @@ -124,7 +124,7 @@ template cv::Mat SG2CVFactory::get_cvMat } template cv::Mat SG2CVFactory::get_cvMat_from_features - (CDenseFeatures* sgDense, int cv_type) + (DenseFeatures* sgDense, int cv_type) { SGMatrix sgMat=sgDense->get_feature_matrix(); cv::Mat cvMat=SG2CVFactory::get_cvMat(sgMat, cv_type); diff --git a/src/shogun/lib/SGMatrix.cpp b/src/shogun/lib/SGMatrix.cpp index 322ece1b0bf..5264db91a4b 100644 --- a/src/shogun/lib/SGMatrix.cpp +++ b/src/shogun/lib/SGMatrix.cpp @@ -195,7 +195,7 @@ bool SGMatrix::equals(const SGMatrix& other) const return std::equal( \ matrix, matrix + size(), other.matrix, \ [](const real_t& a, const real_t& b) { \ - return CMath::fequals( \ + return Math::fequals( \ a, b, std::numeric_limits::epsilon()); \ }); \ } @@ -221,8 +221,8 @@ bool SGMatrix::equals(const SGMatrix& other) const return std::equal(matrix, matrix+size(), other.matrix, [](const complex128_t& a, const complex128_t& b) { - return CMath::fequals(a.real(), b.real(), LDBL_EPSILON) && - CMath::fequals(a.imag(), b.imag(), LDBL_EPSILON); + return Math::fequals(a.real(), b.real(), LDBL_EPSILON) && + Math::fequals(a.imag(), b.imag(), LDBL_EPSILON); }); } @@ -286,7 +286,7 @@ bool SGMatrix::is_symmetric() const \ { \ for (index_t j=i+1; j(matrix[j*num_rows+i], \ + if (!Math::fequals(matrix[j*num_rows+i], \ matrix[i*num_rows+j], std::numeric_limits::epsilon())) \ return false; \ } \ @@ -317,9 +317,9 @@ bool SGMatrix::is_symmetric() const { for (index_t j=i+1; j(matrix[j*num_rows+i].real(), + if (!(Math::fequals(matrix[j*num_rows+i].real(), matrix[i*num_rows+j].real(), DBL_EPSILON) && - CMath::fequals(matrix[j*num_rows+i].imag(), + Math::fequals(matrix[j*num_rows+i].imag(), matrix[i*num_rows+j].imag(), DBL_EPSILON))) return false; } @@ -913,7 +913,7 @@ void SGMatrix::free_data() } template -void SGMatrix::load(CFile* loader) +void SGMatrix::load(std::shared_ptr loader) { ASSERT(loader) unref(); @@ -929,13 +929,13 @@ void SGMatrix::load(CFile* loader) } template<> -void SGMatrix::load(CFile* loader) +void SGMatrix::load(std::shared_ptr loader) { error("SGMatrix::load():: Not supported for complex128_t"); } template -void SGMatrix::save(CFile* writer) +void SGMatrix::save(std::shared_ptr writer) { assert_on_cpu(); ASSERT(writer) @@ -945,7 +945,7 @@ void SGMatrix::save(CFile* writer) } template<> -void SGMatrix::save(CFile* saver) +void SGMatrix::save(std::shared_ptr saver) { error("SGMatrix::save():: Not supported for complex128_t"); } @@ -966,7 +966,7 @@ template SGVector SGMatrix::get_diagonal_vector() const { assert_on_cpu(); - index_t diag_vlen=CMath::min(num_cols, num_rows); + index_t diag_vlen=Math::min(num_cols, num_rows); SGVector diag(diag_vlen); for (int64_t i=0; i class SGVector; template struct GPUMemoryBase; - class CFile; + class File; /** @brief shogun matrix */ template class SGMatrix : public SGReferencedData @@ -325,7 +325,7 @@ template class SGMatrix : public SGReferencedData /** * Checks whether the matrix is symmetric or not. The equality check * is performed using '==' operators for discrete types (int, char, - * bool) and using CMath::fequals method for floating types (float, + * bool) and using Math::fequals method for floating types (float, * double, long double, std::complex) with default espilon * values from std::numeric_limits * @@ -461,13 +461,13 @@ template class SGMatrix : public SGReferencedData * * @param loader File object via which to load data */ - void load(CFile* loader); + void load(std::shared_ptr loader); /** Save matrix to file * * @param saver File object via which to save data */ - void save(CFile* saver); + void save(std::shared_ptr saver); #endif // #ifndef SWIG // SWIG should skip this part protected: diff --git a/src/shogun/lib/SGSparseMatrix.cpp b/src/shogun/lib/SGSparseMatrix.cpp index d590f10c284..1c2dfff0ff5 100644 --- a/src/shogun/lib/SGSparseMatrix.cpp +++ b/src/shogun/lib/SGSparseMatrix.cpp @@ -89,7 +89,7 @@ const SGVector SGSparseMatrix::operator*( } template -void SGSparseMatrix::load(CFile* loader) +void SGSparseMatrix::load(std::shared_ptr loader) { ASSERT(loader) unref(); @@ -100,12 +100,12 @@ void SGSparseMatrix::load(CFile* loader) } template<> -void SGSparseMatrix::load(CFile* loader) +void SGSparseMatrix::load(std::shared_ptr loader) { error("SGSparseMatrix::load():: Not supported for complex128_t"); } -template SGVector SGSparseMatrix::load_with_labels(CLibSVMFile* file, bool do_sort_features) +template SGVector SGSparseMatrix::load_with_labels(std::shared_ptr file, bool do_sort_features) { ASSERT(file) @@ -121,11 +121,11 @@ template SGVector SGSparseMatrix::load_with_labels(CLibSV return labels; } -template<> SGVector SGSparseMatrix::load_with_labels(CLibSVMFile* file, bool do_sort_features) { return SGVector(); } +template<> SGVector SGSparseMatrix::load_with_labels(std::shared_ptr file, bool do_sort_features) { return SGVector(); } template -void SGSparseMatrix::save(CFile* saver) +void SGSparseMatrix::save(std::shared_ptr saver) { ASSERT(saver) @@ -135,12 +135,12 @@ void SGSparseMatrix::save(CFile* saver) } template<> -void SGSparseMatrix::save(CFile* saver) +void SGSparseMatrix::save(std::shared_ptr saver) { error("SGSparseMatrix::save():: Not supported for complex128_t"); } -template void SGSparseMatrix::save_with_labels(CLibSVMFile* file, +template void SGSparseMatrix::save_with_labels(std::shared_ptr file, SGVector labels) { ASSERT(file) @@ -153,7 +153,7 @@ template void SGSparseMatrix::save_with_labels(CLibSVMFile* file, raw_labels); } -template <> void SGSparseMatrix::save_with_labels(CLibSVMFile* saver, SGVector labels) { } +template <> void SGSparseMatrix::save_with_labels(std::shared_ptr saver, SGVector labels) { } template diff --git a/src/shogun/lib/SGSparseMatrix.h b/src/shogun/lib/SGSparseMatrix.h index ecef4752c81..72147216465 100644 --- a/src/shogun/lib/SGSparseMatrix.h +++ b/src/shogun/lib/SGSparseMatrix.h @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Soumyajit De, Sergey Lisitsyn, Yingrui Chang, - * Evgeniy Andreev, Yuyu Zhang, Viktor Gal, Thoralf Klein, + * Authors: Soeren Sonnenburg, Soumyajit De, Sergey Lisitsyn, Yingrui Chang, + * Evgeniy Andreev, Yuyu Zhang, Viktor Gal, Thoralf Klein, * Fernando Iglesias, Bjoern Esser */ @@ -22,8 +22,8 @@ namespace shogun template class SGSparseVector; template struct SGSparseVectorEntry; template class SGMatrix; -class CFile; -class CLibSVMFile; +class File; +class LibSVMFile; /** @brief template class SGSparseMatrix */ template class SGSparseMatrix : public SGReferencedData @@ -147,7 +147,7 @@ template class SGSparseMatrix : public SGReferencedData * * @param loader File object via which to load data */ - void load(CFile* loader); + void load(std::shared_ptr loader); /** load sparse matrix from libsvm file together with labels * @@ -156,20 +156,20 @@ template class SGSparseMatrix : public SGReferencedData * ascending order) after loading * @return label vector */ - SGVector load_with_labels(CLibSVMFile* libsvm_file, bool do_sort_features=true); + SGVector load_with_labels(std::shared_ptr libsvm_file, bool do_sort_features=true); /** save sparse matrix to file * * @param saver File object via which to save data */ - void save(CFile* saver); + void save(std::shared_ptr saver); /** save sparse matrix together with labels to file * * @param saver File object via which to save data * @param labels label vector */ - void save_with_labels(CLibSVMFile* saver, SGVector labels); + void save_with_labels(std::shared_ptr saver, SGVector labels); /** return the transposed of the sparse matrix */ SGSparseMatrix get_transposed(); diff --git a/src/shogun/lib/SGSparseVector.cpp b/src/shogun/lib/SGSparseVector.cpp index 693a54a4dca..9e453e57321 100644 --- a/src/shogun/lib/SGSparseVector.cpp +++ b/src/shogun/lib/SGSparseVector.cpp @@ -77,7 +77,7 @@ void SGSparseVector::add_to_dense(T alpha, T * vec, int32_t dim, bool abs_val { for (int32_t i = 0; i < num_feat_entries; i++) { - vec[features[i].feat_index] += alpha*CMath::abs(features[i].entry); + vec[features[i].feat_index] += alpha*Math::abs(features[i].entry); } } else @@ -196,7 +196,7 @@ void SGSparseVector::sort_features(bool stable_pointer) feat_idx[j] = features[j].feat_index; } - CMath::qsort_index(feat_idx, features, num_feat_entries); + Math::qsort_index(feat_idx, features, num_feat_entries); SG_FREE(feat_idx); for (size_type j = 1; j < num_feat_entries; j++) @@ -387,7 +387,7 @@ bool SGSparseVector::equals(const SGSparseVector& other) const return std::equal(features, features + num_feat_entries, other.features); } -template void SGSparseVector::load(CFile * loader) +template void SGSparseVector::load(File * loader) { ASSERT(loader) unref(); @@ -397,7 +397,7 @@ template void SGSparseVector::load(CFile * loader) SG_RESET_LOCALE; } -template void SGSparseVector::save(CFile * saver) +template void SGSparseVector::save(File * saver) { ASSERT(saver) @@ -407,13 +407,13 @@ template void SGSparseVector::save(CFile * saver) } template <> -void SGSparseVector::load(CFile * loader) +void SGSparseVector::load(File * loader) { error("SGSparseVector::load():: Not supported for complex128_t"); } template <> -void SGSparseVector::save(CFile * saver) +void SGSparseVector::save(File * saver) { error("SGSparseVector::save():: Not supported for complex128_t"); } @@ -505,7 +505,7 @@ operator==(const SGSparseVectorEntry& other) const if (feat_index != other.feat_index) \ return false; \ \ - return CMath::fequals( \ + return Math::fequals( \ entry, other.entry, std::numeric_limits::epsilon()); \ } @@ -522,9 +522,9 @@ operator==(const SGSparseVectorEntry& other) const if (feat_index != other.feat_index) return false; - return CMath::fequals( + return Math::fequals( entry.real(), other.entry.real(), LDBL_EPSILON) && - CMath::fequals( + Math::fequals( entry.imag(), other.entry.imag(), LDBL_EPSILON); } diff --git a/src/shogun/lib/SGSparseVector.h b/src/shogun/lib/SGSparseVector.h index 8f4a540a4c0..4b3504045e8 100644 --- a/src/shogun/lib/SGSparseVector.h +++ b/src/shogun/lib/SGSparseVector.h @@ -16,7 +16,7 @@ namespace shogun { - class CFile; + class File; /** @brief template class SGSparseVectorEntry */ template struct SGSparseVectorEntry @@ -169,13 +169,13 @@ template class SGSparseVector : public SGReferencedData * * @param loader File object via which to load data */ - void load(CFile* loader); + void load(File* loader); /** save vector to file * * @param saver File object via which to save data */ - void save(CFile* saver); + void save(File* saver); /** add a sparse feature vector onto a dense one * dense += alpha*sparse diff --git a/src/shogun/lib/SGString.cpp b/src/shogun/lib/SGString.cpp new file mode 100644 index 00000000000..24386765be0 --- /dev/null +++ b/src/shogun/lib/SGString.cpp @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include + +namespace shogun +{ + +template +SGString::SGString() : string(NULL), slen(0), do_free(false) { } + +template +SGString::SGString(T* s, index_t l, bool free_s) + : string(s), slen(l), do_free(free_s) { } + +template +SGString::SGString(SGVector v) + : string(v.vector), slen(v.vlen), do_free(false) { } + +template +SGString::SGString(index_t len, bool free_s) : + slen(len), do_free(free_s) +{ + string=SG_CALLOC(T, len); +} + +template +SGString::SGString(const SGString &orig) + : string(orig.string), slen(orig.slen), do_free(orig.do_free) { } + +template +bool SGString::operator==(const SGString & other) const +{ + if (other.slen != slen) + return false; + + if (string != other.string) + return false; + + return true; +} + +template +bool SGString::equals(const SGString& other) const +{ + // avoid comparing elements when both are same. + // the case where both matrices are uninitialized is handled here as well. + if (*this == other) + return true; + + // both empty + if (!(slen || other.slen)) + return true; + + // only one empty + if (!string || !other.string) + return false; + + // different size + if (slen != other.slen) + return false; + + // content + return std::equal(string, string + slen, other.string); + + return true; +} + +template +SGString SGString::clone() const +{ + SGString result(slen); + sg_memcpy(result.string, string, sizeof(T)*slen); + return result; +} + +template +void SGString::free_string() +{ + if (do_free) + SG_FREE(string); + + string=NULL; + do_free=false; + slen=0; +} + +template +void SGString::destroy_string() +{ + do_free=true; + free_string(); +} + +template void SGString::load(File* loader) +{ + ASSERT(loader) + free_string(); + + SG_SET_LOCALE_C; + loader->get_vector(string, slen); + do_free=true; + SG_RESET_LOCALE; +} + +template void SGString::save(File* saver) +{ + ASSERT(saver) + + SG_SET_LOCALE_C; + saver->set_vector(string, slen); + SG_RESET_LOCALE; +} + +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +template class SGString; +} diff --git a/src/shogun/lib/SGString.h b/src/shogun/lib/SGString.h new file mode 100644 index 00000000000..8a56e5eeb7d --- /dev/null +++ b/src/shogun/lib/SGString.h @@ -0,0 +1,84 @@ +/* + * This software is distributed under BSD 3-clause license (see LICENSE file). + * + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Jacob Walker, Thoralf Klein, + * Bjoern Esser + */ +#ifndef __SGSTRING_H__ +#define __SGSTRING_H__ + +#include +#include + +namespace shogun +{ + +template class SGVector; +class File; + +/** @brief shogun string */ +template class SGString +{ +public: + /** default constructor */ + SGString(); + + /** constructor for setting params */ + SGString(T* s, index_t l, bool free_s=false); + + /** constructor for setting params from a SGVector*/ + SGString(SGVector v); + + /** constructor to create new string in memory */ + SGString(index_t len, bool free_s=false); + + /** copy constructor */ + SGString(const SGString &orig); + + /** @return true iff pointer and size are equal */ + bool operator==(const SGString & other) const; + + /** @return true iff content is equal */ + bool equals(const SGString& other) const; + + /** Clone string */ + SGString clone() const; + + /** free string */ + void free_string(); + + /** destroy string */ + void destroy_string(); + + /** + * get the string (no copying is done here) + * + * @return the refcount increased string + */ + inline SGString get() + { + return *this; + } + + /** load string from file + * + * @param loader File object via which to load data + */ + void load(File* loader); + + /** save string to file + * + * @param saver File object via which to save data + */ + void save(File* saver); + +public: + /** string */ + T* string; + /** length of string */ + index_t slen; + /** whether string needs to be freed */ + bool do_free; +}; +} +#endif // __SGSTRING_H__ diff --git a/src/shogun/lib/SGVector.cpp b/src/shogun/lib/SGVector.cpp index 810fdae027f..9f2378fd1eb 100644 --- a/src/shogun/lib/SGVector.cpp +++ b/src/shogun/lib/SGVector.cpp @@ -426,7 +426,7 @@ bool SGVector::equals(const SGVector& other) const \ for (index_t i = 0; i < vlen; ++i) \ { \ - if (!CMath::fequals( \ + if (!Math::fequals( \ vector[i], other.vector[i], \ std::numeric_limits::epsilon())) \ return false; \ @@ -657,7 +657,7 @@ float64_t SGVector::onenorm(T* x, int32_t len) { float64_t result=0; for (int32_t i=0;i::qsq(T* x, int32_t len, float64_t q) { float64_t result=0; for (int32_t i=0; i T SGVector::qnorm(T* x, int32_t len, float64_t q) { require(q!=0, "Q should be non-zero for calculating qnorm"); - return CMath::pow((float64_t) qsq(x, len, q), 1.0/q); + return Math::pow((float64_t) qsq(x, len, q), 1.0/q); } template <> @@ -701,7 +701,7 @@ T SGVector::sum_abs(T* vec, int32_t len) { T result=0; for (int32_t i=0; i::scale(T alpha) scale_vector(alpha, vector, vlen); } -template void SGVector::load(CFile* loader) +template void SGVector::load(std::shared_ptr loader) { require(loader, "No file provided."); unref(); @@ -813,12 +813,12 @@ template void SGVector::load(CFile* loader) } template<> -void SGVector::load(CFile* loader) +void SGVector::load(std::shared_ptr loader) { error("SGVector::load():: Not supported for complex128_t"); } -template void SGVector::save(CFile* saver) +template void SGVector::save(std::shared_ptr saver) { require(saver, "Requires a valid 'c FILE pointer'"); @@ -829,7 +829,7 @@ template void SGVector::save(CFile* saver) } template<> -void SGVector::save(CFile* saver) +void SGVector::save(std::shared_ptr saver) { error("SGVector::save():: Not supported for complex128_t"); } @@ -839,7 +839,7 @@ template SGVector SGVector::get_real() assert_on_cpu(); SGVector real(vlen); for (int32_t i=0; i SGVector SGVector::get_imag() assert_on_cpu(); SGVector imag(vlen); for (int32_t i=0; i class SGSparseVector; template class SGMatrix; - class CFile; + class File; /** @brief shogun vector */ template class SGVector : public SGReferencedData @@ -536,13 +536,13 @@ template class SGVector : public SGReferencedData * * @param loader File object via which to load data */ - void load(CFile* loader); + void load(std::shared_ptr loader); /** Save vector to file * * @param saver File object via which to save data */ - void save(CFile* saver); + void save(std::shared_ptr saver); /** Real part of a complex128_t vector */ SGVector get_real(); diff --git a/src/shogun/lib/SGVector_benchmark.cc b/src/shogun/lib/SGVector_benchmark.cc new file mode 100644 index 00000000000..cdc9f682269 --- /dev/null +++ b/src/shogun/lib/SGVector_benchmark.cc @@ -0,0 +1,28 @@ +/* + * This software is distributed under BSD 3-clause license (see LICENSE file). + * + * Authors: Eleftherios Avramidis + */ + +#include "shogun/mathematics/Math.h" +#include +#include "shogun/lib/SGVector.h" + + +namespace shogun +{ + +void BM_SGVector_calculation(benchmark::State& state) +{ + for (auto _ : state) + { + SGVector a(state.range(0)), b(state.range(0)); + a.random(0.0, 1E10); + for (index_t i = 0; i < a.size(); ++i) + b[i] = a[i] *2; + } +} + +BENCHMARK(BM_SGVector_calculation)->Range(8, 2048); + +} diff --git a/src/shogun/lib/Set.h b/src/shogun/lib/Set.h index 22afab75495..8218390064b 100644 --- a/src/shogun/lib/Set.h +++ b/src/shogun/lib/Set.h @@ -44,11 +44,11 @@ template struct CSetNode /** @brief the class CSet, a set based on the hash-table. * w: http://en.wikipedia.org/wiki/Hash_table */ -template class CSet: public CSGObject +template class Set: public SGObject { public: /** Custom constructor */ - CSet(int32_t size=41, int32_t reserved=128, bool tracable=true) + Set(int32_t size=41, int32_t reserved=128, bool tracable=true) { hash_size=size; free_index=0; @@ -69,7 +69,7 @@ template class CSet: public CSGObject } /** Default destructor */ - virtual ~CSet() + virtual ~Set() { if (array!=NULL) { @@ -213,7 +213,7 @@ template class CSet: public CSGObject */ int32_t hash_element(const T& element) { - return CHash::MurmurHash3((uint8_t*)(&element), sizeof(element), 0xDEADBEEF) % hash_size; + return Hash::MurmurHash3((uint8_t*)(&element), sizeof(element), 0xDEADBEEF) % hash_size; } /** is free? */ diff --git a/src/shogun/lib/Signal.cpp b/src/shogun/lib/Signal.cpp index 06bbc678799..a23ef17e880 100644 --- a/src/shogun/lib/Signal.cpp +++ b/src/shogun/lib/Signal.cpp @@ -14,23 +14,23 @@ using namespace shogun; using namespace rxcpp; -bool CSignal::m_active = true; -CSignal::SGSubjectS* CSignal::m_subject = new rxcpp::subjects::subject(); +bool Signal::m_active = true; +Signal::SGSubjectS* Signal::m_subject = new rxcpp::subjects::subject(); -CSignal::SGObservableS* CSignal::m_observable = - new CSignal::SGObservableS(CSignal::m_subject->get_observable()); -CSignal::SGSubscriberS* CSignal::m_subscriber = - new CSignal::SGSubscriberS(CSignal::m_subject->get_subscriber()); +Signal::SGObservableS* Signal::m_observable = + new Signal::SGObservableS(Signal::m_subject->get_observable()); +Signal::SGSubscriberS* Signal::m_subscriber = + new Signal::SGSubscriberS(Signal::m_subject->get_subscriber()); -CSignal::CSignal() +Signal::Signal() { } -CSignal::~CSignal() +Signal::~Signal() { } -void CSignal::handler(int signal) +void Signal::handler(int signal) { /* If the handler is not enabled exit */ if (!m_active) @@ -74,13 +74,13 @@ void CSignal::handler(int signal) } } -void CSignal::reset_handler() +void Signal::reset_handler() { delete m_subject; delete m_observable; delete m_subscriber; m_subject = new rxcpp::subjects::subject(); - m_observable = new CSignal::SGObservableS(m_subject->get_observable()); - m_subscriber = new CSignal::SGSubscriberS(m_subject->get_subscriber()); + m_observable = new Signal::SGObservableS(m_subject->get_observable()); + m_subscriber = new Signal::SGSubscriberS(m_subject->get_subscriber()); } diff --git a/src/shogun/lib/Signal.h b/src/shogun/lib/Signal.h index 817bc19881e..f39791e2ae9 100644 --- a/src/shogun/lib/Signal.h +++ b/src/shogun/lib/Signal.h @@ -30,7 +30,7 @@ namespace shogun * option bewteen: immediately exit the running method and fall back to * the command line, prematurely stop the current algoritmh and do nothing. */ - class CSignal + class Signal { public: typedef rxcpp::subjects::subject SGSubjectS; @@ -40,8 +40,8 @@ namespace shogun rxcpp::observer> SGSubscriberS; - CSignal(); - virtual ~CSignal(); + Signal(); + virtual ~Signal(); /** Signal handler. Need to be registered with std::signal. * diff --git a/src/shogun/lib/StoppableSGObject.cpp b/src/shogun/lib/StoppableSGObject.cpp index 218b49b4ad1..12b2db1e108 100644 --- a/src/shogun/lib/StoppableSGObject.cpp +++ b/src/shogun/lib/StoppableSGObject.cpp @@ -11,7 +11,7 @@ using namespace shogun; -CStoppableSGObject::CStoppableSGObject() : CSGObject() +StoppableSGObject::StoppableSGObject() : SGObject() { m_cancel_computation = false; m_pause_computation_flag = false; @@ -19,9 +19,9 @@ CStoppableSGObject::CStoppableSGObject() : CSGObject() m_callback = nullptr; }; -CStoppableSGObject::~CStoppableSGObject(){}; +StoppableSGObject::~StoppableSGObject(){}; -rxcpp::subscription CStoppableSGObject::connect_to_signal_handler() +rxcpp::subscription StoppableSGObject::connect_to_signal_handler() { // Subscribe this algorithm to the signal handler auto subscriber = rxcpp::make_subscriber( @@ -35,40 +35,40 @@ rxcpp::subscription CStoppableSGObject::connect_to_signal_handler() return env()->signal()->get_observable()->subscribe(subscriber); } -void CStoppableSGObject::set_callback(std::function callback) +void StoppableSGObject::set_callback(std::function callback) { m_callback = callback; } -void CStoppableSGObject::reset_computation_variables() +void StoppableSGObject::reset_computation_variables() { m_cancel_computation = false; m_pause_computation_flag = false; } -void CStoppableSGObject::on_next() +void StoppableSGObject::on_next() { m_cancel_computation.store(true); on_next_impl(); } -void CStoppableSGObject::on_pause() +void StoppableSGObject::on_pause() { m_pause_computation_flag.store(true); on_pause_impl(); resume_computation(); } -void CStoppableSGObject::on_complete() +void StoppableSGObject::on_complete() { on_complete_impl(); } -void CStoppableSGObject::on_next_impl() +void StoppableSGObject::on_next_impl() { } -void CStoppableSGObject::on_pause_impl() +void StoppableSGObject::on_pause_impl() { } -void CStoppableSGObject::on_complete_impl() +void StoppableSGObject::on_complete_impl() { } diff --git a/src/shogun/lib/StoppableSGObject.h b/src/shogun/lib/StoppableSGObject.h index db66d5617cc..1fd82a36458 100644 --- a/src/shogun/lib/StoppableSGObject.h +++ b/src/shogun/lib/StoppableSGObject.h @@ -22,14 +22,14 @@ namespace shogun /** * Class that abstracts all premature stopping code */ - class CStoppableSGObject : public CSGObject + class StoppableSGObject : public SGObject { public: /** constructor */ - CStoppableSGObject(); + StoppableSGObject(); /** destructor */ - virtual ~CStoppableSGObject(); + virtual ~StoppableSGObject(); #ifndef SWIG /** @return whether the algorithm needs to be stopped */ @@ -87,14 +87,14 @@ namespace shogun void on_next(); /** The action which will be done when the user decides to - * premature stop the CMachine execution */ + * premature stop the Machine execution */ virtual void on_next_impl(); /** sets pause computation flag and resumes after action is complete */ void on_pause(); /** The action which will be done when the user decides to - * pause the CMachine execution */ + * pause the Machine execution */ virtual void on_pause_impl(); /** These actions which will be done when the user decides to diff --git a/src/shogun/lib/StringMap.h b/src/shogun/lib/StringMap.h index 8a98fe0c72c..d469426e97c 100644 --- a/src/shogun/lib/StringMap.h +++ b/src/shogun/lib/StringMap.h @@ -62,7 +62,7 @@ IGNORE_IN_CLASSLIST template class CStringMap: public CMap txt) +void Tokenizer::set_text(SGVector txt) { text = txt; } -void CTokenizer::init() +void Tokenizer::init() { SG_ADD(&text, "text", "The text"); } diff --git a/src/shogun/lib/Tokenizer.h b/src/shogun/lib/Tokenizer.h index 3209c832b07..75fa88b7bce 100644 --- a/src/shogun/lib/Tokenizer.h +++ b/src/shogun/lib/Tokenizer.h @@ -14,24 +14,24 @@ namespace shogun { -class CSGObject; +class SGObject; template class SGVector; -/** @brief The class CTokenizer acts as a base class in order +/** @brief The class Tokenizer acts as a base class in order * to implement tokenizers. Sub-classes must implement * the methods has_next(), next_token_idx() and get_copy(). */ -class CTokenizer: public CSGObject +class Tokenizer: public SGObject { public: /** Constructor */ - CTokenizer(); + Tokenizer(); /** Copy constructor */ - CTokenizer(const CTokenizer& orig); + Tokenizer(const Tokenizer& orig); /** Destructor */ - virtual ~CTokenizer() { }; + virtual ~Tokenizer() { }; /** Set the char array that requires tokenization * @@ -55,10 +55,10 @@ class CTokenizer: public CSGObject virtual index_t next_token_idx(index_t& start)=0; /** Creates a copy of the appropriate runtime - * instance of a CTokenizer subclass + * instance of a Tokenizer subclass * Needs to be overriden */ - virtual CTokenizer* get_copy()=0; + virtual Tokenizer* get_copy()=0; private: void init(); diff --git a/src/shogun/lib/Trie.h b/src/shogun/lib/Trie.h index 89edff29898..af1da1149cb 100644 --- a/src/shogun/lib/Trie.h +++ b/src/shogun/lib/Trie.h @@ -150,7 +150,7 @@ template class CTrie; * may save a lot of memory on higher degree tries. * */ -IGNORE_IN_CLASSLIST template class CTrie : public CSGObject +IGNORE_IN_CLASSLIST template class CTrie : public SGObject { public: /** default constructor */ @@ -656,7 +656,7 @@ IGNORE_IN_CLASSLIST template class CTrie : public CSGObject }; template CTrie::CTrie() - : CSGObject(), degree(0), position_weights(NULL), + : SGObject(), degree(0), position_weights(NULL), use_compact_terminal_nodes(false), weights_in_tree(true) { @@ -673,7 +673,7 @@ IGNORE_IN_CLASSLIST template class CTrie : public CSGObject template CTrie::CTrie(int32_t d, bool p_use_compact_terminal_nodes) - : CSGObject(), degree(d), position_weights(NULL), + : SGObject(), degree(d), position_weights(NULL), use_compact_terminal_nodes(p_use_compact_terminal_nodes), weights_in_tree(true) { @@ -689,7 +689,7 @@ IGNORE_IN_CLASSLIST template class CTrie : public CSGObject template CTrie::CTrie(const CTrie & to_copy) - : CSGObject(to_copy), degree(to_copy.degree), position_weights(NULL), + : SGObject(to_copy), degree(to_copy.degree), position_weights(NULL), use_compact_terminal_nodes(to_copy.use_compact_terminal_nodes) { if (to_copy.position_weights!=NULL) @@ -1506,7 +1506,7 @@ void CTrie::add_to_trie( if (weights_in_tree) { for (int32_t j=0; (j0) + if (Math::abs(weights_column[j]*alpha)>0) max_depth = j+1 ; } else diff --git a/src/shogun/lib/View.h b/src/shogun/lib/View.h index 292b367ce60..5c891e8dfdd 100644 --- a/src/shogun/lib/View.h +++ b/src/shogun/lib/View.h @@ -22,28 +22,15 @@ namespace shogun * @return new viewable instance */ template - T* view(const T* viewable, const SGVector& subset) + std::shared_ptr view(std::shared_ptr viewable, const SGVector& subset) { static_assert( - std::is_base_of::value || - std::is_base_of::value, + std::is_base_of::value || + std::is_base_of::value, "Class is not viewable."); auto result = viewable->duplicate(); result->add_subset(subset); - return static_cast(result); - } - - /** Creates a subset view of the viewable object containing the elements - * whose indices are listed in the passed vector - * - * @param viewable pointer to the viewable object - * @param subset subset of indices - * @return new viewable instance - */ - template - Some view(const Some viewable, const SGVector& subset) - { - return wrap(view(viewable.get(), subset)); + return result->template as(); } } // namespace shogun diff --git a/src/shogun/lib/any.cpp b/src/shogun/lib/any.cpp index a46911ce033..5bd6010e2ae 100644 --- a/src/shogun/lib/any.cpp +++ b/src/shogun/lib/any.cpp @@ -36,7 +36,7 @@ namespace shogun bool compare_impl_eq(const real_t& lhs, const real_t& rhs) \ { \ SG_DEBUG("Comparing using fequals<" #real_t ">(lhs, rhs)."); \ - return CMath::fequals( \ + return Math::fequals( \ lhs, rhs, std::numeric_limits::epsilon()); \ } @@ -50,13 +50,14 @@ namespace shogun bool compare_impl_eq(const complex128_t& lhs, const complex128_t& rhs) { SG_DEBUG("Comparing using fequals(lhs, rhs)."); - return CMath::fequals(lhs.real(), rhs.real(), LDBL_EPSILON) && - CMath::fequals(lhs.imag(), rhs.imag(), LDBL_EPSILON); + return Math::fequals(lhs.real(), rhs.real(), LDBL_EPSILON) && + Math::fequals(lhs.imag(), rhs.imag(), LDBL_EPSILON); } - void free_object(CSGObject* obj) + void free_object(SGObject* obj) { - SG_UNREF(obj); + //FIXME + //SG_UNREF(obj); } } diff --git a/src/shogun/lib/any.h b/src/shogun/lib/any.h index 6ef670a9304..559e32da360 100644 --- a/src/shogun/lib/any.h +++ b/src/shogun/lib/any.h @@ -52,7 +52,7 @@ namespace shogun return any_detail::demangled_type_helper(name); } - class CSGObject; + class SGObject; template class SGVector; template @@ -184,7 +184,7 @@ namespace shogun virtual void on(float64_t*) = 0; virtual void on(floatmax_t*) = 0; virtual void on(complex128_t*) = 0; - virtual void on(CSGObject**) = 0; + virtual void on(std::shared_ptr*) = 0; virtual void on(std::string*) = 0; virtual void enter_matrix(index_t* rows, index_t* cols) = 0; virtual void enter_vector(index_t* size) = 0; @@ -309,7 +309,7 @@ namespace shogun enter_std_vector(std::addressof(size)); if (size != _v->size()) _v->resize(size); - for (auto& _value : *_v) + for (auto&& _value: *_v) on(std::addressof(_value)); exit_std_vector(std::addressof(size)); } @@ -343,12 +343,10 @@ namespace shogun exit_map(std::addressof(size)); } - template < - class T, - std::enable_if_t, T>* = nullptr> - void on(T** v) + template, T>* = nullptr> + void on(std::shared_ptr* v) { - on((CSGObject**)v); + on((std::shared_ptr*)v); } void on(Empty*) @@ -422,6 +420,21 @@ namespace shogun }; #endif // DOXYGEN_SHOULD_SKIP_THIS + template + struct is_shared_ptr : std::false_type {}; + + template + struct is_shared_ptr> : std::true_type {}; + + template + constexpr T cast(const U& value) + { + if constexpr (is_shared_ptr::value) + return std::dynamic_pointer_cast(value); + else + return dynamic_cast(value); + } + template constexpr T& mutable_value_of(void** ptr) { @@ -537,7 +550,11 @@ namespace shogun if (!value) return nullptr; - return dynamic_cast(value->clone()); + auto cloned = value->clone(); + if constexpr(std::is_same_v) + return cloned; + else + return cast(cloned); } else if constexpr (traits::has_clone::value) { @@ -626,7 +643,7 @@ namespace shogun return 0; } - void free_object(CSGObject* obj); + void free_object(SGObject* obj); template inline auto free_array(T** ptr, S size) -> decltype(ptr[0]->unref()) @@ -1035,8 +1052,8 @@ namespace shogun /** @brief Allows to store objects of arbitrary types * by using a BaseAnyPolicy and provides a type agnostic API. - * See its usage in CSGObject::Self, CSGObject::set(), CSGObject::get() - * and CSGObject::has(). + * See its usage in SGObject::Self, SGObject::set(), SGObject::get() + * and SGObject::has(). * . */ class Any @@ -1274,10 +1291,10 @@ namespace shogun inline void register_casts() { using Derived = std::remove_pointer_t; - if constexpr (std::is_base_of_v) + if constexpr (std::is_base_of_v) { - Any::register_caster( - [](T value) { return dynamic_cast(value); }); + Any::register_caster( + [](T value) { return dynamic_cast(value); }); if constexpr (!std::is_same_v>) Any::register_caster*>([](T value) { return dynamic_cast*>(value); diff --git a/src/shogun/lib/auto_initialiser.cpp b/src/shogun/lib/auto_initialiser.cpp index 7de7f13b805..a22d62f29d1 100644 --- a/src/shogun/lib/auto_initialiser.cpp +++ b/src/shogun/lib/auto_initialiser.cpp @@ -18,4 +18,4 @@ namespace shogun "calculated then gamma = 1 / (n_features * std(features)), else " "gamma = 1 / n_features."; } // namespace factory -} // namespace shogun \ No newline at end of file +} // namespace shogun diff --git a/src/shogun/lib/auto_initialiser.h b/src/shogun/lib/auto_initialiser.h index 0eb83c72025..bff263b7c9c 100644 --- a/src/shogun/lib/auto_initialiser.h +++ b/src/shogun/lib/auto_initialiser.h @@ -22,7 +22,7 @@ namespace shogun static const char* const kDescription; public: - explicit GammaFeatureNumberInit(CKernel* kernel) + explicit GammaFeatureNumberInit(Kernel* kernel) : AutoInit(kName, kDescription), m_kernel(kernel) { } @@ -31,7 +31,7 @@ namespace shogun Any operator()() override { - require(m_kernel != nullptr, "m_kernel is not pointing to a CKernel object"); + require(m_kernel != nullptr, "m_kernel is not pointing to a Kernel object"); auto lhs = m_kernel->get_lhs(); switch (lhs->get_feature_class()) @@ -39,7 +39,7 @@ namespace shogun case EFeatureClass::C_DENSE: case EFeatureClass::C_SPARSE: { - auto dot_features = (CDotFeatures*)lhs; + auto dot_features = lhs->as(); return make_any( 1.0 / (static_cast( @@ -47,15 +47,18 @@ namespace shogun dot_features->get_std(false)[0])); } default: + { + auto dot_features = lhs->as(); return make_any( 1.0 / static_cast( - ((CDotFeatures*)lhs)->get_dim_feature_space())); + dot_features->get_dim_feature_space())); + } } } private: - CKernel* m_kernel; + Kernel* m_kernel; SG_DELETE_COPY_AND_ASSIGN(GammaFeatureNumberInit); }; } // namespace factory diff --git a/src/shogun/lib/basetag.h b/src/shogun/lib/basetag.h index e41ba086752..e500fad2a63 100644 --- a/src/shogun/lib/basetag.h +++ b/src/shogun/lib/basetag.h @@ -43,7 +43,7 @@ namespace shogun * This class stores name and not the type information for * a shogun object. It can be used as an identifier for a shogun object * where type information is not known. - * One application of this can be found in CSGObject::set_param_with_btag(). + * One application of this can be found in SGObject::set_param_with_btag(). */ class BaseTag { diff --git a/src/shogun/lib/exception/ShogunException.cpp b/src/shogun/lib/exception/ShogunException.cpp index ebc1596d8cd..2b0499a3f21 100644 --- a/src/shogun/lib/exception/ShogunException.cpp +++ b/src/shogun/lib/exception/ShogunException.cpp @@ -21,4 +21,4 @@ ShogunException::ShogunException(const std::string& what_arg) ShogunException::ShogunException(const char* what_arg) : std::runtime_error(what_arg) { -} \ No newline at end of file +} diff --git a/src/shogun/lib/external/libocas.cpp b/src/shogun/lib/external/libocas.cpp index 4612ca03306..0f64ae5efcf 100644 --- a/src/shogun/lib/external/libocas.cpp +++ b/src/shogun/lib/external/libocas.cpp @@ -689,9 +689,9 @@ ocas_return_value_T svm_ocas_solver( for(i=0; i < nData; i++) new_cut[i] = i; - gap=(ocas.Q_P-ocas.Q_D)/CMath::abs(ocas.Q_P); + gap=(ocas.Q_P-ocas.Q_D)/Math::abs(ocas.Q_P); pb.print_absolute( - gap, -CMath::log10(gap), -CMath::log10(1), -CMath::log10(TolRel)); + gap, -Math::log10(gap), -Math::log10(1), -Math::log10(TolRel)); ocas.trn_err = nData; ocas.ocas_time = get_time() - ocas_start_time; @@ -773,10 +773,10 @@ ocas_return_value_T svm_ocas_solver( goto cleanup; } ocas.output_time += get_time() - start_time; - gap = (ocas.Q_P - ocas.Q_D) / CMath::abs(ocas.Q_P); + gap = (ocas.Q_P - ocas.Q_D) / Math::abs(ocas.Q_P); pb.print_absolute( - gap, -CMath::log10(gap), -CMath::log10(1), - -CMath::log10(TolRel)); + gap, -Math::log10(gap), -Math::log10(1), + -Math::log10(TolRel)); xi = 0; cut_length = 0; diff --git a/src/shogun/lib/external/libocas_common.h b/src/shogun/lib/external/libocas_common.h index e7b9933b830..f91e468214a 100644 --- a/src/shogun/lib/external/libocas_common.h +++ b/src/shogun/lib/external/libocas_common.h @@ -10,7 +10,7 @@ namespace shogun { -#define OCAS_PLUS_INF CMath::INFTY +#define OCAS_PLUS_INF Math::INFTY #define OCAS_CALLOC(...) calloc(__VA_ARGS__) #define OCAS_FREE(...) SG_FREE(__VA_ARGS__) diff --git a/src/shogun/lib/external/shogun_libsvm.cpp b/src/shogun/lib/external/shogun_libsvm.cpp index 6bfda59e5d3..10cb4abd096 100644 --- a/src/shogun/lib/external/shogun_libsvm.cpp +++ b/src/shogun/lib/external/shogun_libsvm.cpp @@ -110,7 +110,7 @@ Cache::Cache(int32_t l_, int64_t size_):l(l_),size(size_) head = (head_t *)SG_CALLOC(head_t, l); // initialized to 0 size /= sizeof(Qfloat); size -= l * sizeof(head_t) / sizeof(Qfloat); - size = CMath::max(size, (int64_t) 2*l); // cache must be large enough for two columns + size = Math::max(size, (int64_t) 2*l); // cache must be large enough for two columns lru_head.next = lru_head.prev = &lru_head; } @@ -159,7 +159,7 @@ int32_t Cache::get_data(const int32_t index, Qfloat **data, int32_t len) // allocate new space h->data = SG_REALLOC(Qfloat, h->data, h->len, len); size -= more; - CMath::swap(h->len,len); + Math::swap(h->len,len); } lru_insert(h); @@ -173,18 +173,18 @@ void Cache::swap_index(int32_t i, int32_t j) if(head[i].len) lru_delete(&head[i]); if(head[j].len) lru_delete(&head[j]); - CMath::swap(head[i].data,head[j].data); - CMath::swap(head[i].len,head[j].len); + Math::swap(head[i].data,head[j].data); + Math::swap(head[i].len,head[j].len); if(head[i].len) lru_insert(&head[i]); if(head[j].len) lru_insert(&head[j]); - if(i>j) CMath::swap(i,j); + if(i>j) Math::swap(i,j); for(head_t *h = lru_head.next; h!=&lru_head; h=h->next) { if(h->len > i) { if(h->len > j) - CMath::swap(h->data[i],h->data[j]); + Math::swap(h->data[i],h->data[j]); else { // give up @@ -226,8 +226,8 @@ class LibSVMKernel: public QMatrix { virtual Qfloat *get_QD() const = 0; virtual void swap_index(int32_t i, int32_t j) const // no so const... { - CMath::swap(x[i],x[j]); - if(x_square) CMath::swap(x_square[i],x_square[j]); + Math::swap(x[i],x[j]); + if(x_square) Math::swap(x_square[i],x_square[j]); } void compute_Q_parallel(Qfloat* data, float64_t* lab, int32_t i, int32_t start, int32_t len) const @@ -252,7 +252,7 @@ class LibSVMKernel: public QMatrix { } private: - CKernel* kernel; + Kernel* kernel; const svm_node **x; float64_t *x_square; }; @@ -393,13 +393,13 @@ void Solver::reset_computation_variables() void Solver::swap_index(int32_t i, int32_t j) { Q->swap_index(i,j); - CMath::swap(y[i],y[j]); - CMath::swap(G[i],G[j]); - CMath::swap(alpha_status[i],alpha_status[j]); - CMath::swap(alpha[i],alpha[j]); - CMath::swap(p[i],p[j]); - CMath::swap(active_set[i],active_set[j]); - CMath::swap(G_bar[i],G_bar[j]); + Math::swap(y[i],y[j]); + Math::swap(G[i],G[j]); + Math::swap(alpha_status[i],alpha_status[j]); + Math::swap(alpha[i],alpha[j]); + Math::swap(p[i],p[j]); + Math::swap(active_set[i],active_set[j]); + Math::swap(G_bar[i],G_bar[j]); } void Solver::reconstruct_gradient() @@ -475,7 +475,7 @@ void Solver::Solve( } // initialize gradient - CTime start_time; + Time start_time; { auto pb = SG_SPROGRESS(range(l)); G = SG_MALLOC(float64_t, l); @@ -487,7 +487,7 @@ void Solver::Solve( G_bar[i] = 0; } io::info("Computing gradient for initial set of non-zero alphas"); - //CMath::display_vector(alpha, l, "alphas"); + //Math::display_vector(alpha, l, "alphas"); for (i = 0; i < l && !cancel_computation(); i++) { if(!is_lower_bound(i)) @@ -509,7 +509,7 @@ void Solver::Solve( // optimization step int32_t iter = 0; - int32_t counter = CMath::min(l,1000)+1; + int32_t counter = Math::min(l,1000)+1; auto pb = SG_SPROGRESS(range(10)); while (!cancel_computation()) { @@ -520,7 +520,7 @@ void Solver::Solve( if(--counter == 0) { - counter = CMath::min(l,1000); + counter = Math::min(l,1000); if(shrinking) do_shrinking(); //io::info("."); } @@ -541,7 +541,7 @@ void Solver::Solve( } pb.print_absolute( - gap, -CMath::log10(gap), -CMath::log10(1), -CMath::log10(eps)); + gap, -Math::log10(gap), -Math::log10(1), -Math::log10(eps)); ++iter; @@ -562,14 +562,14 @@ void Solver::Solve( double pj=G[j]-Q_i[j]*alpha[i]-Q_j[j]*alpha[j]; double det=Q_i[i]*Q_j[j]-Q_i[j]*Q_i[j]; double alpha_i=-(Q_j[j]*pi-Q_i[j]*pj)/det; - alpha_i=CMath::min(C_i,CMath::max(0.0,alpha_i)); + alpha_i=Math::min(C_i,Math::max(0.0,alpha_i)); double alpha_j=-(-Q_i[j]*pi+Q_i[i]*pj)/det; - alpha_j=CMath::min(C_j,CMath::max(0.0,alpha_j)); + alpha_j=Math::min(C_j,Math::max(0.0,alpha_j)); if (alpha_i==0 || alpha_i == C_i) - alpha_j=CMath::min(C_j,CMath::max(0.0,-(pj+Q_i[j]*alpha_i)/Q_j[j])); + alpha_j=Math::min(C_j,Math::max(0.0,-(pj+Q_i[j]*alpha_i)/Q_j[j])); if (alpha_j==0 || alpha_j == C_j) - alpha_i=CMath::min(C_i,CMath::max(0.0,-(pi+Q_i[j]*alpha_j)/Q_i[i])); + alpha_i=Math::min(C_i,Math::max(0.0,-(pi+Q_i[j]*alpha_j)/Q_i[i])); alpha[i]=alpha_i; alpha[j]=alpha_j; } @@ -747,7 +747,7 @@ void Solver::Solve( p_si->upper_bound_p = Cp; p_si->upper_bound_n = Cn; - io::info("\noptimization finished, #iter = {}",iter); + io::info("optimization finished, #iter = {}",iter); SG_FREE(p); SG_FREE(y); @@ -956,16 +956,16 @@ float64_t Solver::calculate_rho() if(is_upper_bound(i)) { if(y[i]==-1) - ub = CMath::min(ub,yG); + ub = Math::min(ub,yG); else - lb = CMath::max(lb,yG); + lb = Math::max(lb,yG); } else if(is_lower_bound(i)) { if(y[i]==+1) - ub = CMath::min(ub,yG); + ub = Math::min(ub,yG); else - lb = CMath::max(lb,yG); + lb = Math::max(lb,yG); } else { @@ -1141,7 +1141,7 @@ int32_t Solver_NU::select_working_set( } } - gap=CMath::max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2); + gap=Math::max(Gmaxp+Gmaxp2,Gmaxn+Gmaxn2); if(gap < eps) return 1; @@ -1205,7 +1205,7 @@ void Solver_NU::do_shrinking() } } - if(unshrink == false && CMath::max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10) + if(unshrink == false && Math::max(Gmax1+Gmax2,Gmax3+Gmax4) <= eps*10) { unshrink = true; reconstruct_gradient(); @@ -1240,9 +1240,9 @@ float64_t Solver_NU::calculate_rho() if(y[i]==+1) { if(is_upper_bound(i)) - lb1 = CMath::max(lb1,G[i]); + lb1 = Math::max(lb1,G[i]); else if(is_lower_bound(i)) - ub1 = CMath::min(ub1,G[i]); + ub1 = Math::min(ub1,G[i]); else { ++nr_free1; @@ -1252,9 +1252,9 @@ float64_t Solver_NU::calculate_rho() else { if(is_upper_bound(i)) - lb2 = CMath::max(lb2,G[i]); + lb2 = Math::max(lb2,G[i]); else if(is_lower_bound(i)) - ub2 = CMath::min(ub2,G[i]); + ub2 = Math::min(ub2,G[i]); else { ++nr_free2; @@ -1331,8 +1331,8 @@ class SVC_QMC: public LibSVMKernel { cache->swap_index(i,j); LibSVMKernel::swap_index(i,j); - CMath::swap(y[i],y[j]); - CMath::swap(QD[i],QD[j]); + Math::swap(y[i],y[j]); + Math::swap(QD[i],QD[j]); } ~SVC_QMC() @@ -1412,7 +1412,7 @@ float64_t Solver_NUMC::compute_primal(const schar* p_y, float64_t* p_alpha, floa class_count[(int32_t) y[i]]++; } - //CMath::display_vector(class_count, nr_class, "class_count"); + //Math::display_vector(class_count, nr_class, "class_count"); float64_t mu=((float64_t) nr_class)/(nu*l); //io::print("nr_class={}, l={}, active_size={}, nu={}, mu={}\n", nr_class, l, active_size, nu, mu); @@ -1476,7 +1476,7 @@ float64_t Solver_NUMC::compute_primal(const schar* p_y, float64_t* p_alpha, floa if (class_count[i] == 0.0) rho+=zero_counts[i]; - normwcw[i]+=normwc_const/CMath::sq(nr_class); + normwcw[i]+=normwc_const/Math::sq(nr_class); normwcw[i] = std::sqrt(normwcw[i]); } @@ -1507,7 +1507,7 @@ float64_t Solver_NUMC::compute_primal(const schar* p_y, float64_t* p_alpha, floa biases[0]=rho; - //CMath::display_vector(outputs, l, "outputs"); + //Math::display_vector(outputs, l, "outputs"); float64_t xi=0; @@ -1692,8 +1692,8 @@ class SVC_Q: public LibSVMKernel { cache->swap_index(i,j); LibSVMKernel::swap_index(i,j); - CMath::swap(y[i],y[j]); - CMath::swap(QD[i],QD[j]); + Math::swap(y[i],y[j]); + Math::swap(QD[i],QD[j]); } ~SVC_Q() @@ -1740,7 +1740,7 @@ class ONE_CLASS_Q: public LibSVMKernel { cache->swap_index(i,j); LibSVMKernel::swap_index(i,j); - CMath::swap(QD[i],QD[j]); + Math::swap(QD[i],QD[j]); } ~ONE_CLASS_Q() @@ -1780,9 +1780,9 @@ class SVR_Q: public LibSVMKernel void swap_index(int32_t i, int32_t j) const { - CMath::swap(sign[i],sign[j]); - CMath::swap(index[i],index[j]); - CMath::swap(QD[i],QD[j]); + Math::swap(sign[i],sign[j]); + Math::swap(index[i],index[j]); + Math::swap(QD[i],QD[j]); } Qfloat *get_Q(int32_t i, int32_t len) const @@ -1920,12 +1920,12 @@ static void solve_nu_svc( for(i=0;ieps, si, param->shrinking, param->use_bias); @@ -2001,7 +2001,7 @@ static void solve_nu_multiclass_svc(const svm_problem *prob, for (int32_t i=0; i 0) + if (Math::abs(alpha[i]) > 0) class_sv_count[(int32_t) y[i]]++; } @@ -2135,7 +2135,7 @@ static void solve_nu_svr( float64_t sum = C * param->nu * l / 2; for(i=0;iy[i]; @@ -2652,7 +2652,7 @@ const char *svm_check_parameter( for(int32_t j=i+1;jnu*(n1+n2)/2 > CMath::min(n1,n2)) + if(param->nu*(n1+n2)/2 > Math::min(n1,n2)) { SG_FREE(label); SG_FREE(count); diff --git a/src/shogun/lib/external/shogun_libsvm.h b/src/shogun/lib/external/shogun_libsvm.h index 34b331624de..1e012ccba03 100644 --- a/src/shogun/lib/external/shogun_libsvm.h +++ b/src/shogun/lib/external/shogun_libsvm.h @@ -91,7 +91,7 @@ struct svm_parameter /** kernel type */ int32_t kernel_type; /** kernel */ - shogun::CKernel* kernel; + shogun::Kernel* kernel; /** for poly */ int32_t degree; /** for poly/rbf/sigmoid */ diff --git a/src/shogun/lib/memory.cpp b/src/shogun/lib/memory.cpp index d1b5dfcc85e..af0bf15f4b1 100644 --- a/src/shogun/lib/memory.cpp +++ b/src/shogun/lib/memory.cpp @@ -24,7 +24,6 @@ using namespace shogun; - SG_FORCED_INLINE bool allocation_error(void *p, size_t size, const char* op_str) { const size_t buf_len=128; diff --git a/src/shogun/lib/observers/ObservedValue.cpp b/src/shogun/lib/observers/ObservedValue.cpp index ae682fe43bc..e45cdccb4d1 100644 --- a/src/shogun/lib/observers/ObservedValue.cpp +++ b/src/shogun/lib/observers/ObservedValue.cpp @@ -10,7 +10,7 @@ using namespace shogun; ObservedValue::ObservedValue(const int64_t step, std::string_view name) - : CSGObject(), m_step(step), m_name(name), m_any_value(Any()) + : SGObject(), m_step(step), m_name(name), m_any_value(Any()) { SG_ADD(&m_step, "step", "Step", ParameterProperties::READONLY); this->watch_param( diff --git a/src/shogun/lib/observers/ObservedValue.h b/src/shogun/lib/observers/ObservedValue.h index 66620b832ee..8be56bd2c98 100644 --- a/src/shogun/lib/observers/ObservedValue.h +++ b/src/shogun/lib/observers/ObservedValue.h @@ -18,7 +18,7 @@ namespace shogun /** * Observed value which is emitted by algorithms. */ - class ObservedValue : public CSGObject + class ObservedValue : public SGObject { public: /** diff --git a/src/shogun/lib/observers/ParameterObserver.cpp b/src/shogun/lib/observers/ParameterObserver.cpp index 72512ace49a..8f1436a58d0 100644 --- a/src/shogun/lib/observers/ParameterObserver.cpp +++ b/src/shogun/lib/observers/ParameterObserver.cpp @@ -81,4 +81,4 @@ bool ParameterObserver::filter(const std::string& param) index_t ParameterObserver::get_num_observations() const { return utils::safe_convert(m_observations.size()); -}; \ No newline at end of file +}; diff --git a/src/shogun/lib/observers/ParameterObserver.h b/src/shogun/lib/observers/ParameterObserver.h index 428cb101a09..3efc8d0e687 100644 --- a/src/shogun/lib/observers/ParameterObserver.h +++ b/src/shogun/lib/observers/ParameterObserver.h @@ -49,7 +49,7 @@ namespace shogun /** * Interface for the parameter observer classes */ - class ParameterObserver : public CSGObject + class ParameterObserver : public SGObject { public: @@ -159,7 +159,7 @@ namespace shogun /** * Observations recorded each time we compute on_next() */ - std::vector> m_observations; + std::vector> m_observations; /** * Subscription id set when I subscribe to a machine diff --git a/src/shogun/lib/observers/ParameterObserverCV.cpp b/src/shogun/lib/observers/ParameterObserverCV.cpp index b96721d4f8c..346ec996530 100644 --- a/src/shogun/lib/observers/ParameterObserverCV.cpp +++ b/src/shogun/lib/observers/ParameterObserverCV.cpp @@ -43,23 +43,21 @@ using namespace shogun; -CParameterObserverCV::CParameterObserverCV(bool verbose) +ParameterObserverCV::ParameterObserverCV(bool verbose) : ParameterObserver(), m_verbose(verbose) { } -CParameterObserverCV::~CParameterObserverCV() +ParameterObserverCV::~ParameterObserverCV() { } -void CParameterObserverCV::on_next_impl(const shogun::TimedObservedValue& value) +void ParameterObserverCV::on_next_impl(const shogun::TimedObservedValue& value) { try { - CrossValidationStorage* recalled_value = - dynamic_cast( - value.first->get(value.first->get("name"))); - SG_REF(recalled_value); + auto recalled_value = + value.first->get(value.first->get("name"))->as(); /* Print information on screen if enabled*/ if (m_verbose) @@ -75,16 +73,16 @@ void CParameterObserverCV::on_next_impl(const shogun::TimedObservedValue& value) } } -void CParameterObserverCV::on_error(std::exception_ptr ptr) +void ParameterObserverCV::on_error(std::exception_ptr ptr) { } -void CParameterObserverCV::on_complete() +void ParameterObserverCV::on_complete() { } -void CParameterObserverCV::print_observed_value( - CrossValidationStorage* value) const +void ParameterObserverCV::print_observed_value( + std::shared_ptr value) const { for (index_t i = 0; i < value->get("num_folds"); i++) { @@ -98,64 +96,61 @@ void CParameterObserverCV::print_observed_value( .display_vector("Train Indices "); f->get>("test_indices") .display_vector("Test Indices "); - print_machine_information(f->get("trained_machine")); - f->get("test_result") + print_machine_information(f->get("trained_machine")); + f->get("test_result") ->get_values() .display_vector("Test Labels "); - f->get("test_true_result") + f->get("test_true_result") ->get_values() .display_vector("Test True Label "); io::print( "Evaluation result: {}\n", f->get("evaluation_result")); - SG_UNREF(f) } } -void CParameterObserverCV::print_machine_information(CMachine* machine) const +void ParameterObserverCV::print_machine_information(std::shared_ptr machine) const { - if (dynamic_cast(machine)) + if (std::dynamic_pointer_cast(machine)) { - CLinearMachine* linear_machine = (CLinearMachine*)machine; + auto linear_machine = std::static_pointer_cast(machine); linear_machine->get_w().display_vector("Learned Weights = "); io::print("Learned Bias = {}\n", linear_machine->get_bias()); } - if (dynamic_cast(machine)) + if (std::dynamic_pointer_cast(machine)) { - CKernelMachine* kernel_machine = (CKernelMachine*)machine; + auto kernel_machine = machine->as(); kernel_machine->get_alphas().display_vector("Learned alphas = "); io::print("Learned Bias = {}\n", kernel_machine->get_bias()); } - if (dynamic_cast(machine) || - dynamic_cast(machine)) + if (std::dynamic_pointer_cast(machine) || + std::dynamic_pointer_cast(machine)) { - CMulticlassMachine* mc_machine = (CMulticlassMachine*)machine; + auto mc_machine = machine->as(); for (int i = 0; i < mc_machine->get_num_machines(); i++) { - CMachine* sub_machine = mc_machine->get_machine(i); + auto sub_machine = mc_machine->get_machine(i); this->print_machine_information(sub_machine); - SG_UNREF(sub_machine); } } - if (dynamic_cast(machine)) + if (std::dynamic_pointer_cast(machine)) { - CMKL* mkl = (CMKL*)machine; - CCombinedKernel* kernel = - dynamic_cast(mkl->get_kernel()); + auto mkl = machine->as(); + auto kernel = mkl->get_kernel()->as(); kernel->get_subkernel_weights().display_vector( "MKL sub-kernel weights ="); - SG_UNREF(kernel); + } - if (dynamic_cast(machine)) + if (std::dynamic_pointer_cast(machine)) { - CMKLMulticlass* mkl = (CMKLMulticlass*)machine; - CCombinedKernel* kernel = - dynamic_cast(mkl->get_kernel()); + auto mkl = machine->as(); + auto kernel = mkl->get_kernel()->as(); kernel->get_subkernel_weights().display_vector( "MKL sub-kernel weights ="); - SG_UNREF(kernel); + } } + diff --git a/src/shogun/lib/observers/ParameterObserverCV.h b/src/shogun/lib/observers/ParameterObserverCV.h index bd166583407..a170222e807 100644 --- a/src/shogun/lib/observers/ParameterObserverCV.h +++ b/src/shogun/lib/observers/ParameterObserverCV.h @@ -46,12 +46,12 @@ namespace shogun /** * Base ParameterObserver class for CrossValidation. */ - class CParameterObserverCV : public ParameterObserver + class ParameterObserverCV : public ParameterObserver { public: - CParameterObserverCV(bool verbose = false); - virtual ~CParameterObserverCV(); + ParameterObserverCV(bool verbose = false); + virtual ~ParameterObserverCV(); virtual void on_error(std::exception_ptr ptr); virtual void on_complete(); @@ -69,13 +69,13 @@ namespace shogun * Print data contained into a CrossValidationStorage object. * @param value CrossValidationStorage object */ - void print_observed_value(CrossValidationStorage* value) const; + void print_observed_value(std::shared_ptr value) const; /** * Print information of a machine * @param machine given machine */ - void print_machine_information(CMachine* machine) const; + void print_machine_information(std::shared_ptr machine) const; protected: virtual void on_next_impl(const TimedObservedValue& value); diff --git a/src/shogun/lib/observers/ParameterObserverHistogram.cpp b/src/shogun/lib/observers/ParameterObserverHistogram.cpp index 29dd09aaa51..d22bd710600 100644 --- a/src/shogun/lib/observers/ParameterObserverHistogram.cpp +++ b/src/shogun/lib/observers/ParameterObserverHistogram.cpp @@ -41,28 +41,28 @@ using namespace shogun; -CParameterObserverHistogram::CParameterObserverHistogram() +ParameterObserverHistogram::ParameterObserverHistogram() : ParameterObserverTensorBoard() { } -CParameterObserverHistogram::CParameterObserverHistogram( +ParameterObserverHistogram::ParameterObserverHistogram( std::vector& parameters) : ParameterObserverTensorBoard(parameters) { } -CParameterObserverHistogram::CParameterObserverHistogram( +ParameterObserverHistogram::ParameterObserverHistogram( const std::string& filename, std::vector& parameters) : ParameterObserverTensorBoard(filename, parameters) { } -CParameterObserverHistogram::~CParameterObserverHistogram() +ParameterObserverHistogram::~ParameterObserverHistogram() { } -void CParameterObserverHistogram::on_next_impl(const TimedObservedValue& value) +void ParameterObserverHistogram::on_next_impl(const TimedObservedValue& value) { auto node_name = std::string("node"); auto format = TBOutputFormat(); @@ -70,11 +70,11 @@ void CParameterObserverHistogram::on_next_impl(const TimedObservedValue& value) m_writer.writeEvent(event_value); } -void CParameterObserverHistogram::on_error(std::exception_ptr) +void ParameterObserverHistogram::on_error(std::exception_ptr) { } -void CParameterObserverHistogram::on_complete() +void ParameterObserverHistogram::on_complete() { } diff --git a/src/shogun/lib/observers/ParameterObserverHistogram.h b/src/shogun/lib/observers/ParameterObserverHistogram.h index 476cfd94522..a8213ea57a9 100644 --- a/src/shogun/lib/observers/ParameterObserverHistogram.h +++ b/src/shogun/lib/observers/ParameterObserverHistogram.h @@ -47,15 +47,15 @@ namespace shogun * Implementation of a ParameterObserver which write to file * histograms, given object emitted from a parameter observable. */ - class CParameterObserverHistogram : public ParameterObserverTensorBoard + class ParameterObserverHistogram : public ParameterObserverTensorBoard { public: - CParameterObserverHistogram(); - CParameterObserverHistogram(std::vector& parameters); - CParameterObserverHistogram( + ParameterObserverHistogram(); + ParameterObserverHistogram(std::vector& parameters); + ParameterObserverHistogram( const std::string& filename, std::vector& parameters); - ~CParameterObserverHistogram(); + ~ParameterObserverHistogram(); virtual void on_error(std::exception_ptr); virtual void on_complete(); diff --git a/src/shogun/lib/observers/ParameterObserverLogger.cpp b/src/shogun/lib/observers/ParameterObserverLogger.cpp index 2d81f690859..1ae72264d2f 100644 --- a/src/shogun/lib/observers/ParameterObserverLogger.cpp +++ b/src/shogun/lib/observers/ParameterObserverLogger.cpp @@ -12,29 +12,29 @@ using namespace shogun; -CParameterObserverLogger::CParameterObserverLogger() +ParameterObserverLogger::ParameterObserverLogger() { } -CParameterObserverLogger::CParameterObserverLogger( +ParameterObserverLogger::ParameterObserverLogger( std::vector& parameters) : ParameterObserver(parameters) { } -CParameterObserverLogger::~CParameterObserverLogger() +ParameterObserverLogger::~ParameterObserverLogger() { } -void CParameterObserverLogger::on_error(std::exception_ptr ptr) +void ParameterObserverLogger::on_error(std::exception_ptr ptr) { } -void CParameterObserverLogger::on_complete() +void ParameterObserverLogger::on_complete() { } -void CParameterObserverLogger::on_next_impl(const TimedObservedValue& value) +void ParameterObserverLogger::on_next_impl(const TimedObservedValue& value) { auto name = value.first->get("name"); diff --git a/src/shogun/lib/observers/ParameterObserverLogger.h b/src/shogun/lib/observers/ParameterObserverLogger.h index 10a9173b51e..ef8ede42a64 100644 --- a/src/shogun/lib/observers/ParameterObserverLogger.h +++ b/src/shogun/lib/observers/ParameterObserverLogger.h @@ -15,15 +15,15 @@ namespace shogun /** * This class implements a logger which prints all observed updates. */ - class CParameterObserverLogger : public ParameterObserver + class ParameterObserverLogger : public ParameterObserver { public: - CParameterObserverLogger(); + ParameterObserverLogger(); - CParameterObserverLogger(std::vector& parameters); + ParameterObserverLogger(std::vector& parameters); - virtual ~CParameterObserverLogger(); + virtual ~ParameterObserverLogger(); virtual void on_error(std::exception_ptr ptr); diff --git a/src/shogun/lib/observers/ParameterObserverScalar.cpp b/src/shogun/lib/observers/ParameterObserverScalar.cpp index ff9baf58ec2..a4b3d5c5b0c 100644 --- a/src/shogun/lib/observers/ParameterObserverScalar.cpp +++ b/src/shogun/lib/observers/ParameterObserverScalar.cpp @@ -41,28 +41,28 @@ using namespace shogun; -CParameterObserverScalar::CParameterObserverScalar() +ParameterObserverScalar::ParameterObserverScalar() : ParameterObserverTensorBoard() { } -CParameterObserverScalar::CParameterObserverScalar( +ParameterObserverScalar::ParameterObserverScalar( std::vector& parameters) : ParameterObserverTensorBoard(parameters) { } -CParameterObserverScalar::CParameterObserverScalar( +ParameterObserverScalar::ParameterObserverScalar( const std::string& filename, std::vector& parameters) : ParameterObserverTensorBoard(filename, parameters) { } -CParameterObserverScalar::~CParameterObserverScalar() +ParameterObserverScalar::~ParameterObserverScalar() { } -void CParameterObserverScalar::on_next_impl(const TimedObservedValue& value) +void ParameterObserverScalar::on_next_impl(const TimedObservedValue& value) { auto node_name = std::string("node"); auto format = TBOutputFormat(); @@ -70,11 +70,11 @@ void CParameterObserverScalar::on_next_impl(const TimedObservedValue& value) m_writer.writeEvent(event_value); } -void CParameterObserverScalar::on_error(std::exception_ptr) +void ParameterObserverScalar::on_error(std::exception_ptr) { } -void CParameterObserverScalar::on_complete() +void ParameterObserverScalar::on_complete() { } diff --git a/src/shogun/lib/observers/ParameterObserverScalar.h b/src/shogun/lib/observers/ParameterObserverScalar.h index d56cb232670..70908755539 100644 --- a/src/shogun/lib/observers/ParameterObserverScalar.h +++ b/src/shogun/lib/observers/ParameterObserverScalar.h @@ -47,15 +47,15 @@ namespace shogun * Implementation of a ParameterObserver which write to file * scalar values, given object emitted from a parameter observable. */ - class CParameterObserverScalar : public ParameterObserverTensorBoard + class ParameterObserverScalar : public ParameterObserverTensorBoard { public: - CParameterObserverScalar(); - CParameterObserverScalar(std::vector& parameters); - CParameterObserverScalar( + ParameterObserverScalar(); + ParameterObserverScalar(std::vector& parameters); + ParameterObserverScalar( const std::string& filename, std::vector& parameters); - ~CParameterObserverScalar(); + ~ParameterObserverScalar(); virtual void on_error(std::exception_ptr); virtual void on_complete(); diff --git a/src/shogun/lib/observers/observers_utils.h b/src/shogun/lib/observers/observers_utils.h index a5f64344031..c286ab3d58f 100644 --- a/src/shogun/lib/observers/observers_utils.h +++ b/src/shogun/lib/observers/observers_utils.h @@ -12,7 +12,6 @@ #include #include -#include /** * Definitions of basic object with are needed by the Parameter @@ -26,7 +25,7 @@ namespace shogun /** * Observed value with a timestamp */ - typedef std::pair, time_point> TimedObservedValue; + typedef std::pair, time_point> TimedObservedValue; /** * Helper method to convert a time_point to milliseconds diff --git a/src/shogun/lib/tag.h b/src/shogun/lib/tag.h index 3e5b6bbd58b..39968dd8340 100644 --- a/src/shogun/lib/tag.h +++ b/src/shogun/lib/tag.h @@ -41,7 +41,7 @@ namespace shogun { /** @brief Acts as an identifier for a shogun object. * It contains type information and name of the object. - * Generally used to CSGObject::set() and CSGObject::get() parameters of a class. + * Generally used to SGObject::set() and SGObject::get() parameters of a class. */ template class Tag : public BaseTag diff --git a/src/shogun/lib/tapkee/tapkee_shogun.cpp b/src/shogun/lib/tapkee/tapkee_shogun.cpp index 7ac098ddd2f..d792c9dde2e 100644 --- a/src/shogun/lib/tapkee/tapkee_shogun.cpp +++ b/src/shogun/lib/tapkee/tapkee_shogun.cpp @@ -48,7 +48,7 @@ class ShogunLoggerImplementation : public tapkee::LoggerImplementation struct ShogunFeatureVectorCallback { - ShogunFeatureVectorCallback(CDotFeatures* f) : dim(0), features(f) { } + ShogunFeatureVectorCallback(DotFeatures* f) : dim(0), features(f) { } inline tapkee::IndexType dimension() const { if (features) @@ -62,18 +62,18 @@ struct ShogunFeatureVectorCallback features->add_to_dense_vec(1.0,i,v.data(),dim); } mutable int32_t dim; - CDotFeatures* features; + DotFeatures* features; }; -CDenseFeatures* shogun::tapkee_embed(const shogun::TAPKEE_PARAMETERS_FOR_SHOGUN& parameters) +std::shared_ptr> shogun::tapkee_embed(const shogun::TAPKEE_PARAMETERS_FOR_SHOGUN& parameters) { tapkee::LoggingSingleton::instance().set_logger_impl(new ShogunLoggerImplementation); tapkee::LoggingSingleton::instance().enable_benchmark(); tapkee::LoggingSingleton::instance().enable_info(); - pimpl_kernel_callback kernel_callback(parameters.kernel); - pimpl_distance_callback distance_callback(parameters.distance); + pimpl_kernel_callback kernel_callback(parameters.kernel); + pimpl_distance_callback distance_callback(parameters.distance); ShogunFeatureVectorCallback features_callback(parameters.features); tapkee::DimensionReductionMethod method = tapkee::PCA; @@ -197,6 +197,6 @@ CDenseFeatures* shogun::tapkee_embed(const shogun::TAPKEE_PARAMETERS_ feature_matrix(j,i) = result_embedding(i,j); } } - return new CDenseFeatures(feature_matrix); + return std::make_shared>(feature_matrix); } diff --git a/src/shogun/lib/tapkee/tapkee_shogun.hpp b/src/shogun/lib/tapkee/tapkee_shogun.hpp index 6df9d7f1ace..1839ae4e44b 100644 --- a/src/shogun/lib/tapkee/tapkee_shogun.hpp +++ b/src/shogun/lib/tapkee/tapkee_shogun.hpp @@ -69,12 +69,12 @@ struct TAPKEE_PARAMETERS_FOR_SHOGUN float64_t sne_theta; float64_t sne_perplexity; float64_t squishing_rate; - CKernel* kernel; - CDistance* distance; - CDotFeatures* features; + Kernel* kernel; + Distance* distance; + DotFeatures* features; }; -CDenseFeatures* tapkee_embed(const TAPKEE_PARAMETERS_FOR_SHOGUN& parameters); +std::shared_ptr> tapkee_embed(const TAPKEE_PARAMETERS_FOR_SHOGUN& parameters); } #endif diff --git a/src/shogun/lib/v_array.h b/src/shogun/lib/v_array.h index c3ba15c4c4a..849967d3a09 100644 --- a/src/shogun/lib/v_array.h +++ b/src/shogun/lib/v_array.h @@ -177,7 +177,7 @@ inline void v_array::push_many(const T* new_elem, size_t num) if(end+num >= end_array) { size_t length = end - begin; - size_t new_length = CMath::max(2 * (size_t)(end_array - begin) + 3, + size_t new_length = Math::max(2 * (size_t)(end_array - begin) + 3, end - begin + num); begin = SG_REALLOC(T, begin, length, new_length); end = begin + length; diff --git a/src/shogun/loss/AbsoluteDeviationLoss.cpp b/src/shogun/loss/AbsoluteDeviationLoss.cpp index 7b965db2cde..e3a0c4918cf 100644 --- a/src/shogun/loss/AbsoluteDeviationLoss.cpp +++ b/src/shogun/loss/AbsoluteDeviationLoss.cpp @@ -33,43 +33,43 @@ using namespace shogun; -float64_t CAbsoluteDeviationLoss::loss(float64_t prediction, float64_t label) +float64_t AbsoluteDeviationLoss::loss(float64_t prediction, float64_t label) { return loss(prediction-label); } -float64_t CAbsoluteDeviationLoss::loss(float64_t z) +float64_t AbsoluteDeviationLoss::loss(float64_t z) { - return CMath::abs(z); + return Math::abs(z); } -float64_t CAbsoluteDeviationLoss::first_derivative(float64_t prediction, float64_t label) +float64_t AbsoluteDeviationLoss::first_derivative(float64_t prediction, float64_t label) { return first_derivative(prediction-label); } -float64_t CAbsoluteDeviationLoss::first_derivative(float64_t z) +float64_t AbsoluteDeviationLoss::first_derivative(float64_t z) { return (z>0)?1:-1; } -float64_t CAbsoluteDeviationLoss::second_derivative(float64_t prediction, float64_t label) +float64_t AbsoluteDeviationLoss::second_derivative(float64_t prediction, float64_t label) { return 0; } -float64_t CAbsoluteDeviationLoss::second_derivative(float64_t z) +float64_t AbsoluteDeviationLoss::second_derivative(float64_t z) { return 0; } -float64_t CAbsoluteDeviationLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t AbsoluteDeviationLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { not_implemented(SOURCE_LOCATION);; return 0; } -float64_t CAbsoluteDeviationLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t AbsoluteDeviationLoss::get_square_grad(float64_t prediction, float64_t label) { not_implemented(SOURCE_LOCATION);; return 0; diff --git a/src/shogun/loss/AbsoluteDeviationLoss.h b/src/shogun/loss/AbsoluteDeviationLoss.h index 9526bde8e6f..ffdec636f12 100644 --- a/src/shogun/loss/AbsoluteDeviationLoss.h +++ b/src/shogun/loss/AbsoluteDeviationLoss.h @@ -40,14 +40,14 @@ namespace shogun /** @brief CAbsoluteDeviationLoss implements the absolute deviation loss function. \n * \f$L(y_i,f(x_i)) = \mod{y_i-f(x_i)}\f$ */ -class CAbsoluteDeviationLoss: public CLossFunction +class AbsoluteDeviationLoss: public LossFunction { public: /** default constructor */ - CAbsoluteDeviationLoss(): CLossFunction() {}; + AbsoluteDeviationLoss(): LossFunction() {}; /** Destructor */ - ~CAbsoluteDeviationLoss() {}; + ~AbsoluteDeviationLoss() {}; /** Get loss for an example * diff --git a/src/shogun/loss/ExponentialLoss.cpp b/src/shogun/loss/ExponentialLoss.cpp index e6e2fc93880..717d5895d6f 100644 --- a/src/shogun/loss/ExponentialLoss.cpp +++ b/src/shogun/loss/ExponentialLoss.cpp @@ -33,43 +33,43 @@ using namespace shogun; -float64_t CExponentialLoss::loss(float64_t prediction, float64_t label) +float64_t ExponentialLoss::loss(float64_t prediction, float64_t label) { return loss(prediction*label); } -float64_t CExponentialLoss::loss(float64_t z) +float64_t ExponentialLoss::loss(float64_t z) { return std::exp(-z); } -float64_t CExponentialLoss::first_derivative(float64_t prediction, float64_t label) +float64_t ExponentialLoss::first_derivative(float64_t prediction, float64_t label) { return -label*loss(prediction,label); } -float64_t CExponentialLoss::first_derivative(float64_t z) +float64_t ExponentialLoss::first_derivative(float64_t z) { return -loss(z); } -float64_t CExponentialLoss::second_derivative(float64_t prediction, float64_t label) +float64_t ExponentialLoss::second_derivative(float64_t prediction, float64_t label) { return label*label*loss(prediction,label); } -float64_t CExponentialLoss::second_derivative(float64_t z) +float64_t ExponentialLoss::second_derivative(float64_t z) { return loss(z); } -float64_t CExponentialLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t ExponentialLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { not_implemented(SOURCE_LOCATION);; return 0; } -float64_t CExponentialLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t ExponentialLoss::get_square_grad(float64_t prediction, float64_t label) { not_implemented(SOURCE_LOCATION);; return 0; diff --git a/src/shogun/loss/ExponentialLoss.h b/src/shogun/loss/ExponentialLoss.h index 9015f082861..c287fa10ada 100644 --- a/src/shogun/loss/ExponentialLoss.h +++ b/src/shogun/loss/ExponentialLoss.h @@ -40,14 +40,14 @@ namespace shogun /** @brief CExponentialLoss implements the exponential loss function. \n * \f$L(y_i,f(x_i)) = \exp^{-y_if(x_i)}\f$ */ -class CExponentialLoss: public CLossFunction +class ExponentialLoss: public LossFunction { public: /** default constructor */ - CExponentialLoss(): CLossFunction() {}; + ExponentialLoss(): LossFunction() {}; /** Destructor */ - ~CExponentialLoss() {}; + ~ExponentialLoss() {}; /** Get loss for an example * diff --git a/src/shogun/loss/HingeLoss.cpp b/src/shogun/loss/HingeLoss.cpp index 8ea9b236a93..6ed8d02b147 100644 --- a/src/shogun/loss/HingeLoss.cpp +++ b/src/shogun/loss/HingeLoss.cpp @@ -9,39 +9,39 @@ using namespace shogun; -float64_t CHingeLoss::loss(float64_t prediction, float64_t label) +float64_t HingeLoss::loss(float64_t prediction, float64_t label) { float64_t e = 1 - label * prediction; return (e > 0) ? e : 0; } -float64_t CHingeLoss::loss(float64_t z) +float64_t HingeLoss::loss(float64_t z) { - return CMath::max(0.0, z); + return Math::max(0.0, z); } -float64_t CHingeLoss::first_derivative(float64_t prediction, float64_t label) +float64_t HingeLoss::first_derivative(float64_t prediction, float64_t label) { return (label * prediction >= label * label) ? 0 : -label; } -float64_t CHingeLoss::first_derivative(float64_t z) +float64_t HingeLoss::first_derivative(float64_t z) { return z > 0.0 ? 1.0 : 0.0; } -float64_t CHingeLoss::second_derivative(float64_t prediction, float64_t label) +float64_t HingeLoss::second_derivative(float64_t prediction, float64_t label) { return 0.; } -float64_t CHingeLoss::second_derivative(float64_t z) +float64_t HingeLoss::second_derivative(float64_t z) { return 0; } -float64_t CHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t HingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { if (label * prediction >= label * label) return 0; @@ -50,7 +50,7 @@ float64_t CHingeLoss::get_update(float64_t prediction, float64_t label, float64_ return label * (normal < err ? normal : err)/norm; } -float64_t CHingeLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t HingeLoss::get_square_grad(float64_t prediction, float64_t label) { return first_derivative(prediction, label); } diff --git a/src/shogun/loss/HingeLoss.h b/src/shogun/loss/HingeLoss.h index bc9331ebc92..acaad972850 100644 --- a/src/shogun/loss/HingeLoss.h +++ b/src/shogun/loss/HingeLoss.h @@ -13,21 +13,21 @@ namespace shogun { -/** @brief CHingeLoss implements the hinge +/** @brief HingeLoss implements the hinge * loss function. */ -class CHingeLoss: public CLossFunction +class HingeLoss: public LossFunction { public: /** * Constructor */ - CHingeLoss(): CLossFunction() {}; + HingeLoss(): LossFunction() {}; /** * Destructor */ - ~CHingeLoss() {}; + ~HingeLoss() {}; /** * Get loss for an example diff --git a/src/shogun/loss/HuberLoss.cpp b/src/shogun/loss/HuberLoss.cpp index ef5511497b1..b052f4d9b0f 100644 --- a/src/shogun/loss/HuberLoss.cpp +++ b/src/shogun/loss/HuberLoss.cpp @@ -33,65 +33,65 @@ using namespace shogun; -CHuberLoss::CHuberLoss(float64_t delta) -: CLossFunction() +HuberLoss::HuberLoss(float64_t delta) +: LossFunction() { init(); m_delta=delta; } -float64_t CHuberLoss::loss(float64_t prediction, float64_t label) +float64_t HuberLoss::loss(float64_t prediction, float64_t label) { return loss(prediction-label); } -float64_t CHuberLoss::loss(float64_t z) +float64_t HuberLoss::loss(float64_t z) { - if (CMath::abs(z)0)?m_delta:-m_delta; } -float64_t CHuberLoss::second_derivative(float64_t prediction, float64_t label) +float64_t HuberLoss::second_derivative(float64_t prediction, float64_t label) { return second_derivative(prediction-label); } -float64_t CHuberLoss::second_derivative(float64_t z) +float64_t HuberLoss::second_derivative(float64_t z) { - if (CMath::abs(z)= 0) ? log(1 + exp(-z)) : -z + log(1 + exp(z)); } -float64_t CLogLoss::first_derivative(float64_t z) +float64_t LogLoss::first_derivative(float64_t z) { if (z < 0) return -1 / (exp(z) + 1); @@ -22,13 +22,13 @@ float64_t CLogLoss::first_derivative(float64_t z) return -ez / (ez + 1); } -float64_t CLogLoss::second_derivative(float64_t z) +float64_t LogLoss::second_derivative(float64_t z) { float64_t ez = exp(z); return ez / (ez*(ez + 2) + 1); } -float64_t CLogLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t LogLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { float64_t w,x; float64_t d = exp(label * prediction); @@ -54,9 +54,9 @@ float64_t CLogLoss::get_update(float64_t prediction, float64_t label, float64_t return -(label*w+prediction)/norm; } -float64_t CLogLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t LogLoss::get_square_grad(float64_t prediction, float64_t label) { - float64_t d = CLossFunction::first_derivative(prediction, label); + float64_t d = LossFunction::first_derivative(prediction, label); return d*d; } diff --git a/src/shogun/loss/LogLoss.h b/src/shogun/loss/LogLoss.h index d2e6f1c8a59..f14466dd403 100644 --- a/src/shogun/loss/LogLoss.h +++ b/src/shogun/loss/LogLoss.h @@ -16,18 +16,18 @@ namespace shogun /**@brief CLogLoss implements the logarithmic loss * function. */ -class CLogLoss: public CLossFunction +class LogLoss: public LossFunction { public: /** * Constructor */ - CLogLoss(): CLossFunction() {}; + LogLoss(): LossFunction() {}; /** * Destructor */ - ~CLogLoss() {}; + ~LogLoss() {}; /** * Get loss for an example diff --git a/src/shogun/loss/LogLossMargin.cpp b/src/shogun/loss/LogLossMargin.cpp index bcc87843a8f..8778deacbcc 100644 --- a/src/shogun/loss/LogLossMargin.cpp +++ b/src/shogun/loss/LogLossMargin.cpp @@ -10,7 +10,7 @@ using namespace shogun; -float64_t CLogLossMargin::loss(float64_t z) +float64_t LogLossMargin::loss(float64_t z) { if (z >= 1) return log(1+exp(1-z)); @@ -18,7 +18,7 @@ float64_t CLogLossMargin::loss(float64_t z) return 1-z + log(1+exp(z-1)); } -float64_t CLogLossMargin::first_derivative(float64_t z) +float64_t LogLossMargin::first_derivative(float64_t z) { if (z < 1) return -1 / (exp(z-1) + 1); @@ -27,20 +27,20 @@ float64_t CLogLossMargin::first_derivative(float64_t z) return -ez / (ez + 1); } -float64_t CLogLossMargin::second_derivative(float64_t z) +float64_t LogLossMargin::second_derivative(float64_t z) { float64_t ez = exp(z-1); return ez / (ez + 1)*(ez + 1); } -float64_t CLogLossMargin::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t LogLossMargin::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { not_implemented(SOURCE_LOCATION); return -1; } -float64_t CLogLossMargin::get_square_grad(float64_t prediction, float64_t label) +float64_t LogLossMargin::get_square_grad(float64_t prediction, float64_t label) { not_implemented(SOURCE_LOCATION); return -1; diff --git a/src/shogun/loss/LogLossMargin.h b/src/shogun/loss/LogLossMargin.h index f2d7d84615c..c2e53c07d99 100644 --- a/src/shogun/loss/LogLossMargin.h +++ b/src/shogun/loss/LogLossMargin.h @@ -16,18 +16,18 @@ namespace shogun /** @brief Class CLogLossMargin implements a * margin-based log-likelihood loss function. */ -class CLogLossMargin: public CLossFunction +class LogLossMargin: public LossFunction { public: /** * Constructor */ - CLogLossMargin(): CLossFunction() {}; + LogLossMargin(): LossFunction() {}; /** * Destructor */ - ~CLogLossMargin() {}; + ~LogLossMargin() {}; /** * Get loss for an example diff --git a/src/shogun/loss/LossFunction.h b/src/shogun/loss/LossFunction.h index a60ac15909b..ca7c53ae941 100644 --- a/src/shogun/loss/LossFunction.h +++ b/src/shogun/loss/LossFunction.h @@ -45,19 +45,19 @@ namespace shogun * Nikos Karampatziakis, John Langford * http://arxiv.org/abs/1011.1576 */ -class CLossFunction: public CSGObject +class LossFunction: public SGObject { public: /** * Constructor */ - CLossFunction(): CSGObject() {} + LossFunction(): SGObject() {} /** * Destructor */ - virtual ~CLossFunction() {}; + virtual ~LossFunction() {}; /** * Get loss for an example diff --git a/src/shogun/loss/SmoothHingeLoss.cpp b/src/shogun/loss/SmoothHingeLoss.cpp index f710106e64e..8443b84b5fd 100644 --- a/src/shogun/loss/SmoothHingeLoss.cpp +++ b/src/shogun/loss/SmoothHingeLoss.cpp @@ -10,7 +10,7 @@ using namespace shogun; -float64_t CSmoothHingeLoss::loss(float64_t z) +float64_t SmoothHingeLoss::loss(float64_t z) { if (z < 0) return 0.5 - z; @@ -19,7 +19,7 @@ float64_t CSmoothHingeLoss::loss(float64_t z) return 0; } -float64_t CSmoothHingeLoss::first_derivative(float64_t z) +float64_t SmoothHingeLoss::first_derivative(float64_t z) { if (z < 0) return -1; @@ -28,7 +28,7 @@ float64_t CSmoothHingeLoss::first_derivative(float64_t z) return 0; } -float64_t CSmoothHingeLoss::second_derivative(float64_t z) +float64_t SmoothHingeLoss::second_derivative(float64_t z) { if (z < 0) return 0; @@ -37,13 +37,13 @@ float64_t CSmoothHingeLoss::second_derivative(float64_t z) return 0; } -float64_t CSmoothHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t SmoothHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { not_implemented(SOURCE_LOCATION); return -1; } -float64_t CSmoothHingeLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t SmoothHingeLoss::get_square_grad(float64_t prediction, float64_t label) { not_implemented(SOURCE_LOCATION); return -1; diff --git a/src/shogun/loss/SmoothHingeLoss.h b/src/shogun/loss/SmoothHingeLoss.h index dc76ddb8f99..83d0bb5f05a 100644 --- a/src/shogun/loss/SmoothHingeLoss.h +++ b/src/shogun/loss/SmoothHingeLoss.h @@ -16,18 +16,18 @@ namespace shogun /** @brief CSmoothHingeLoss implements * the smooth hinge loss function. */ -class CSmoothHingeLoss: public CLossFunction +class SmoothHingeLoss: public LossFunction { public: /** * Constructor */ - CSmoothHingeLoss(): CLossFunction() {}; + SmoothHingeLoss(): LossFunction() {}; /** * Destructor */ - ~CSmoothHingeLoss() {}; + ~SmoothHingeLoss() {}; /** * Get loss for an example diff --git a/src/shogun/loss/SquaredHingeLoss.cpp b/src/shogun/loss/SquaredHingeLoss.cpp index 5bf41e3e966..0ef4dbce431 100644 --- a/src/shogun/loss/SquaredHingeLoss.cpp +++ b/src/shogun/loss/SquaredHingeLoss.cpp @@ -10,28 +10,28 @@ using namespace shogun; -float64_t CSquaredHingeLoss::loss(float64_t z) +float64_t SquaredHingeLoss::loss(float64_t z) { return (z < 1) ? 0.5 * (1-z) * (1-z) : 0; } -float64_t CSquaredHingeLoss::first_derivative(float64_t z) +float64_t SquaredHingeLoss::first_derivative(float64_t z) { return (z < 1) ? z-1 : 0; } -float64_t CSquaredHingeLoss::second_derivative(float64_t z) +float64_t SquaredHingeLoss::second_derivative(float64_t z) { return (z < 1) ? 1 : 0; } -float64_t CSquaredHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t SquaredHingeLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { not_implemented(SOURCE_LOCATION); return -1; } -float64_t CSquaredHingeLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t SquaredHingeLoss::get_square_grad(float64_t prediction, float64_t label) { not_implemented(SOURCE_LOCATION); return -1; diff --git a/src/shogun/loss/SquaredHingeLoss.h b/src/shogun/loss/SquaredHingeLoss.h index a6b21217e8f..a20d16cb1fe 100644 --- a/src/shogun/loss/SquaredHingeLoss.h +++ b/src/shogun/loss/SquaredHingeLoss.h @@ -17,18 +17,18 @@ namespace shogun /** @brief Class CSquaredHingeLoss implements a * squared hinge loss function. */ -class CSquaredHingeLoss: public CLossFunction +class SquaredHingeLoss: public LossFunction { public: /** * Constructor */ - CSquaredHingeLoss(): CLossFunction() {}; + SquaredHingeLoss(): LossFunction() {}; /** * Destructor */ - ~CSquaredHingeLoss() {}; + ~SquaredHingeLoss() {}; /** * Get loss for an example diff --git a/src/shogun/loss/SquaredLoss.cpp b/src/shogun/loss/SquaredLoss.cpp index 28ed62288d3..3722cc7c52a 100644 --- a/src/shogun/loss/SquaredLoss.cpp +++ b/src/shogun/loss/SquaredLoss.cpp @@ -9,37 +9,37 @@ using namespace shogun; -float64_t CSquaredLoss::loss(float64_t prediction, float64_t label) +float64_t SquaredLoss::loss(float64_t prediction, float64_t label) { return (prediction - label) * (prediction - label); } -float64_t CSquaredLoss::loss(float64_t z) +float64_t SquaredLoss::loss(float64_t z) { return z*z; } -float64_t CSquaredLoss::first_derivative(float64_t prediction, float64_t label) +float64_t SquaredLoss::first_derivative(float64_t prediction, float64_t label) { return 2. * (prediction - label); } -float64_t CSquaredLoss::first_derivative(float64_t z) +float64_t SquaredLoss::first_derivative(float64_t z) { return 2. * z; } -float64_t CSquaredLoss::second_derivative(float64_t prediction, float64_t label) +float64_t SquaredLoss::second_derivative(float64_t prediction, float64_t label) { return 2; } -float64_t CSquaredLoss::second_derivative(float64_t z) +float64_t SquaredLoss::second_derivative(float64_t z) { return 2; } -float64_t CSquaredLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) +float64_t SquaredLoss::get_update(float64_t prediction, float64_t label, float64_t eta_t, float64_t norm) { if (eta_t < 1e-6) { @@ -52,7 +52,7 @@ float64_t CSquaredLoss::get_update(float64_t prediction, float64_t label, float6 return (label - prediction)*(1-exp(-eta_t))/norm; } -float64_t CSquaredLoss::get_square_grad(float64_t prediction, float64_t label) +float64_t SquaredLoss::get_square_grad(float64_t prediction, float64_t label) { return (prediction - label) * (prediction - label); } diff --git a/src/shogun/loss/SquaredLoss.h b/src/shogun/loss/SquaredLoss.h index 0062bc75542..c1727c400b6 100644 --- a/src/shogun/loss/SquaredLoss.h +++ b/src/shogun/loss/SquaredLoss.h @@ -16,18 +16,18 @@ namespace shogun /** @brief CSquaredLoss implements the * squared loss function. */ -class CSquaredLoss: public CLossFunction +class SquaredLoss: public LossFunction { public: /** * Constructor */ - CSquaredLoss(): CLossFunction() {}; + SquaredLoss(): LossFunction() {}; /** * Destructor */ - ~CSquaredLoss() {}; + ~SquaredLoss() {}; /** * Get loss for an example diff --git a/src/shogun/machine/BaggingMachine.cpp b/src/shogun/machine/BaggingMachine.cpp index 43ddc19005e..5334900faa5 100644 --- a/src/shogun/machine/BaggingMachine.cpp +++ b/src/shogun/machine/BaggingMachine.cpp @@ -17,51 +17,33 @@ using namespace shogun; -CBaggingMachine::CBaggingMachine() : RandomMixin() +BaggingMachine::BaggingMachine() : RandomMixin() { init(); register_parameters(); } -CBaggingMachine::CBaggingMachine(CFeatures* features, CLabels* labels) - : RandomMixin() +BaggingMachine::BaggingMachine(std::shared_ptr features, std::shared_ptr labels) + : BaggingMachine() { - init(); - register_parameters(); - set_labels(labels); - - SG_REF(features); m_features = features; } -CBaggingMachine::~CBaggingMachine() -{ - SG_UNREF(m_machine); - SG_UNREF(m_features); - SG_UNREF(m_combination_rule); - SG_UNREF(m_bags); - SG_UNREF(m_oob_indices); -} - -CBinaryLabels* CBaggingMachine::apply_binary(CFeatures* data) +std::shared_ptr BaggingMachine::apply_binary(std::shared_ptr data) { SGMatrix output = apply_outputs_without_combination(data); - CMeanRule* mean_rule = new CMeanRule(); + auto mean_rule = std::make_shared(); SGVector labels = m_combination_rule->combine(output); SGVector probabilities = mean_rule->combine(output); float64_t threshold = 0.5; - CBinaryLabels* pred = new CBinaryLabels(probabilities, threshold); - - SG_UNREF(mean_rule); - - return pred; + return std::make_shared(probabilities, threshold); } -CMulticlassLabels* CBaggingMachine::apply_multiclass(CFeatures* data) +std::shared_ptr BaggingMachine::apply_multiclass(std::shared_ptr data) { SGMatrix bagged_outputs = apply_outputs_without_combination(data); @@ -72,11 +54,11 @@ CMulticlassLabels* CBaggingMachine::apply_multiclass(CFeatures* data) "Labels ({}) are not compatible with multiclass.", m_labels->get_name()); - auto labels_multiclass = dynamic_cast(m_labels); + auto labels_multiclass = std::dynamic_pointer_cast(m_labels); auto num_samples = bagged_outputs.size() / m_num_bags; auto num_classes = labels_multiclass->get_num_classes(); - CMulticlassLabels* pred = new CMulticlassLabels(num_samples); + auto pred = std::make_shared(num_samples); pred->allocate_confidences_for(num_classes); SGMatrix class_probabilities(num_classes, num_samples); @@ -102,26 +84,24 @@ CMulticlassLabels* CBaggingMachine::apply_multiclass(CFeatures* data) return pred; } -CRegressionLabels* CBaggingMachine::apply_regression(CFeatures* data) +std::shared_ptr BaggingMachine::apply_regression(std::shared_ptr data) { - return new CRegressionLabels(apply_get_outputs(data)); + return std::make_shared(apply_get_outputs(data)); } -SGVector CBaggingMachine::apply_get_outputs(CFeatures* data) +SGVector BaggingMachine::apply_get_outputs(std::shared_ptr data) { ASSERT(data != NULL); require(m_combination_rule != NULL, "Combination rule is not set!"); - SGMatrix output = apply_outputs_without_combination(data); - SGVector combined = m_combination_rule->combine(output); - - return combined; + auto output = apply_outputs_without_combination(data); + return m_combination_rule->combine(output); } SGMatrix -CBaggingMachine::apply_outputs_without_combination(CFeatures* data) +BaggingMachine::apply_outputs_without_combination(std::shared_ptr data) { - ASSERT(m_num_bags == m_bags->get_num_elements()); + ASSERT(m_num_bags == m_bags.size()); SGMatrix output(data->get_num_vectors(), m_num_bags); output.zero(); @@ -129,33 +109,28 @@ CBaggingMachine::apply_outputs_without_combination(CFeatures* data) #pragma omp parallel for for (int32_t i = 0; i < m_num_bags; ++i) { - CMachine* m = dynamic_cast(m_bags->get_element(i)); - CLabels* l = m->apply(data); + auto m = m_bags.at(i); + auto l = m->apply(data); SGVector lv; - if (l != NULL) - lv = dynamic_cast(l)->get_labels(); + if (l!=NULL) + lv = l->as()->get_labels(); else error("NULL returned by apply method"); float64_t* bag_results = output.get_column_vector(i); sg_memcpy(bag_results, lv.vector, lv.vlen * sizeof(float64_t)); - - SG_UNREF(l); - SG_UNREF(m); } return output; } -bool CBaggingMachine::train_machine(CFeatures* data) +bool BaggingMachine::train_machine(std::shared_ptr data) { require(m_machine != NULL, "Machine is not set!"); require(m_num_bags > 0, "Number of bag is not set!"); if (data) { - SG_REF(data); - SG_UNREF(m_features); m_features = data; ASSERT(m_features->get_num_vectors() == m_labels->get_num_labels()); @@ -166,14 +141,14 @@ bool CBaggingMachine::train_machine(CFeatures* data) m_bag_size = m_features->get_num_vectors(); // clear the array, if previously trained - m_bags->reset_array(); + m_bags.clear(); // reset the oob index vector m_all_oob_idx = SGVector(m_features->get_num_vectors()); m_all_oob_idx.zero(); - SG_UNREF(m_oob_indices); - m_oob_indices = new CDynamicObjectArray(); + + m_oob_indices = std::make_shared(); SGMatrix rnd_indicies(m_bag_size, m_num_bags); random::fill_array(rnd_indicies, 0, m_bag_size - 1, m_prng); @@ -182,13 +157,13 @@ bool CBaggingMachine::train_machine(CFeatures* data) #pragma omp parallel for for (int32_t i = 0; i < m_num_bags; ++i) { - CMachine* c = dynamic_cast(m_machine->clone()); + auto c=std::dynamic_pointer_cast(m_machine->clone()); ASSERT(c != NULL); SGVector idx( rnd_indicies.get_column_vector(i), m_bag_size, false); - CFeatures* features; - CLabels* labels; + std::shared_ptr features; + std::shared_ptr labels; if (env()->get_num_threads() == 1) { @@ -227,21 +202,14 @@ bool CBaggingMachine::train_machine(CFeatures* data) #pragma omp critical { - // get out of bag indexes - CDynamicArray* oob = get_oob_indices(idx); - m_oob_indices->push_back(oob); + // get out of bag indexes + auto oob = get_oob_indices(idx); + m_oob_indices->push_back(oob); - // add trained machine to bag array - m_bags->push_back(c); - } - - if (env()->get_num_threads() != 1) - { - SG_UNREF(features); - SG_UNREF(labels); + // add trained machine to bag array + m_bags.push_back(c); } - SG_UNREF(c); pb.print_progress(); } pb.complete(); @@ -249,11 +217,11 @@ bool CBaggingMachine::train_machine(CFeatures* data) return true; } -void CBaggingMachine::set_machine_parameters(CMachine* m, SGVector idx) +void BaggingMachine::set_machine_parameters(std::shared_ptr m, SGVector idx) { } -void CBaggingMachine::register_parameters() +void BaggingMachine::register_parameters() { SG_ADD(&m_features, kFeatures, "Train features for bagging"); SG_ADD( @@ -270,46 +238,41 @@ void CBaggingMachine::register_parameters() SG_ADD(&m_machine, kMachine, "machine to use for bagging"); SG_ADD(&m_oob_evaluation_metric, kOobEvaluationMetric, "metric to calculate the oob error"); - watch_method(kOobError, &CBaggingMachine::get_oob_error); + watch_method(kOobError, &BaggingMachine::get_oob_error); } -void CBaggingMachine::set_num_bags(int32_t num_bags) +void BaggingMachine::set_num_bags(int32_t num_bags) { m_num_bags = num_bags; } -int32_t CBaggingMachine::get_num_bags() const +int32_t BaggingMachine::get_num_bags() const { return m_num_bags; } -void CBaggingMachine::set_bag_size(int32_t bag_size) +void BaggingMachine::set_bag_size(int32_t bag_size) { m_bag_size = bag_size; } -int32_t CBaggingMachine::get_bag_size() const +int32_t BaggingMachine::get_bag_size() const { return m_bag_size; } -CMachine* CBaggingMachine::get_machine() const +std::shared_ptr BaggingMachine::get_machine() const { - SG_REF(m_machine); return m_machine; } -void CBaggingMachine::set_machine(CMachine* machine) +void BaggingMachine::set_machine(std::shared_ptr machine) { - SG_REF(machine); - SG_UNREF(m_machine); m_machine = machine; } -void CBaggingMachine::init() +void BaggingMachine::init() { - m_bags = new CDynamicObjectArray(); - SG_REF(m_bags); m_machine = nullptr; m_features = nullptr; m_combination_rule = nullptr; @@ -321,28 +284,25 @@ void CBaggingMachine::init() m_oob_evaluation_metric = nullptr; } -void CBaggingMachine::set_combination_rule(CCombinationRule* rule) +void BaggingMachine::set_combination_rule(std::shared_ptr rule) { - SG_REF(rule); - SG_UNREF(m_combination_rule); m_combination_rule = rule; } -CCombinationRule* CBaggingMachine::get_combination_rule() const +std::shared_ptr BaggingMachine::get_combination_rule() const { - SG_REF(m_combination_rule); return m_combination_rule; } -float64_t CBaggingMachine::get_oob_error() const +float64_t BaggingMachine::get_oob_error() const { require( m_oob_evaluation_metric, "Out of bag evaluation metric is not set!"); require(m_combination_rule, "Combination rule is not set!"); - require(m_bags->get_num_elements() > 0, "BaggingMachine is not trained!"); + require(m_bags.size() > 0, "BaggingMachine is not trained!"); SGMatrix output( - m_features->get_num_vectors(), m_bags->get_num_elements()); + m_features->get_num_vectors(), m_bags.size()); if (m_labels->get_label_type() == LT_REGRESSION) output.zero(); else @@ -352,21 +312,20 @@ float64_t CBaggingMachine::get_oob_error() const only possible when add_subset is thread-safe #pragma omp parallel for num_threads(env()->get_num_threads()) */ - for (index_t i = 0; i < m_bags->get_num_elements(); i++) + for (index_t i = 0; i < m_bags.size(); i++) { - CMachine* m = dynamic_cast(m_bags->get_element(i)); - CDynamicArray* current_oob = - dynamic_cast*>( - m_oob_indices->get_element(i)); + auto m = m_bags.at(i); + auto current_oob + = m_oob_indices->get_element>(i); SGVector oob( current_oob->get_array(), current_oob->get_num_elements(), false); m_features->add_subset(oob); - CLabels* l = m->apply(m_features); + auto l = m->apply(m_features); SGVector lv; - if (l != NULL) - lv = dynamic_cast(l)->get_labels(); + if (l!=NULL) + lv = std::dynamic_pointer_cast(l)->get_labels(); else error("NULL returned by apply method"); @@ -375,9 +334,9 @@ float64_t CBaggingMachine::get_oob_error() const output(oob[j], i) = lv[j]; m_features->remove_subset(); - SG_UNREF(current_oob); - SG_UNREF(m); - SG_UNREF(l); + + + } std::vector idx; @@ -392,36 +351,34 @@ float64_t CBaggingMachine::get_oob_error() const for (int32_t i = 0; i < lab.vlen; i++) lab[i] = combined[idx[i]]; - CLabels* predicted = NULL; + std::shared_ptr predicted = NULL; switch (m_labels->get_label_type()) { - case LT_BINARY: - predicted = new CBinaryLabels(lab); - break; + case LT_BINARY: + predicted = std::make_shared(lab); + break; - case LT_MULTICLASS: - predicted = new CMulticlassLabels(lab); - break; + case LT_MULTICLASS: + predicted = std::make_shared(lab); + break; - case LT_REGRESSION: - predicted = new CRegressionLabels(lab); - break; + case LT_REGRESSION: + predicted = std::make_shared(lab); + break; - default: - error("Unsupported label type"); + default: + error("Unsupported label type"); } - SG_REF(predicted); + m_labels->add_subset(SGVector(idx.data(), idx.size(), false)); float64_t res = m_oob_evaluation_metric->evaluate(predicted, m_labels); m_labels->remove_subset(); - SG_UNREF(predicted); return res; } -CDynamicArray* -CBaggingMachine::get_oob_indices(const SGVector& in_bag) +std::shared_ptr> BaggingMachine::get_oob_indices(const SGVector& in_bag) { SGVector out_of_bag(m_features->get_num_vectors()); out_of_bag.set_const(true); @@ -430,7 +387,7 @@ CBaggingMachine::get_oob_indices(const SGVector& in_bag) for (index_t i = 0; i < in_bag.vlen; i++) out_of_bag[in_bag[i]] &= false; - CDynamicArray* oob = new CDynamicArray(); + auto oob = std::make_shared>(); // store the indicies of vectors that are out of the bag for (index_t i = 0; i < out_of_bag.vlen; i++) { @@ -440,6 +397,5 @@ CBaggingMachine::get_oob_indices(const SGVector& in_bag) m_all_oob_idx[i] = true; } } - return oob; } diff --git a/src/shogun/machine/BaggingMachine.h b/src/shogun/machine/BaggingMachine.h index da13825dd39..fc6a5a3cde6 100644 --- a/src/shogun/machine/BaggingMachine.h +++ b/src/shogun/machine/BaggingMachine.h @@ -17,18 +17,18 @@ namespace shogun { - class CCombinationRule; - class CEvaluation; + class CombinationRule; + class Evaluation; /** * @brief: Bagging algorithm * i.e. bootstrap aggregating */ - class CBaggingMachine : public RandomMixin + class BaggingMachine : public RandomMixin { public: /** default ctor */ - CBaggingMachine(); + BaggingMachine(); /** * constructor @@ -36,13 +36,13 @@ namespace shogun * @param features training features * @param labels training labels */ - CBaggingMachine(CFeatures* features, CLabels* labels); + BaggingMachine(std::shared_ptr features, std::shared_ptr labels); - virtual ~CBaggingMachine(); + virtual ~BaggingMachine() = default; - virtual CBinaryLabels* apply_binary(CFeatures* data = NULL); - virtual CMulticlassLabels* apply_multiclass(CFeatures* data = NULL); - virtual CRegressionLabels* apply_regression(CFeatures* data = NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); /** * Set number of bags/machine to create @@ -79,14 +79,14 @@ namespace shogun * * @return machine that is being used in bagging */ - CMachine* get_machine() const; + std::shared_ptr get_machine() const; /** * Set machine to use in bagging * * @param machine the machine to use for bagging */ - virtual void set_machine(CMachine* machine); + virtual void set_machine(std::shared_ptr machine); /** * Set the combination rule to use for aggregating the classification @@ -94,14 +94,14 @@ namespace shogun * * @param rule combination rule */ - void set_combination_rule(CCombinationRule* rule); + void set_combination_rule(std::shared_ptr rule); /** * Get the combination rule that is used for aggregating the results * - * @return CCombinationRule + * @return CombinationRule */ - CCombinationRule* get_combination_rule() const; + std::shared_ptr get_combination_rule() const; /** get classifier type * @@ -127,15 +127,15 @@ namespace shogun } protected: - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** - * sets parameters of CMachine - useful in Random Forest + * sets parameters of Machine - useful in Random Forest * * @param m machine * @param idx indices of training vectors chosen in current bag */ - virtual void set_machine_parameters(CMachine* m, SGVector idx); + virtual void set_machine_parameters(std::shared_ptr m, SGVector idx); /** helper function for the apply_{regression,..} functions that * computes the output @@ -143,7 +143,7 @@ namespace shogun * @param data the data to compute the output for * @return predictions */ - SGVector apply_get_outputs(CFeatures* data); + SGVector apply_get_outputs(std::shared_ptr data); /** helper function for the apply_{binary,..} functions that * computes the output probabilities without combination rules @@ -151,7 +151,8 @@ namespace shogun * @param data the data to compute the output for * @return predictions */ - SGMatrix apply_outputs_without_combination(CFeatures* data); + SGMatrix + apply_outputs_without_combination(std::shared_ptr data); /** Register paramaters */ void register_parameters(); @@ -166,18 +167,18 @@ namespace shogun * NOTE: in_bag is a randomly generated with replacement * @return the vector of indices */ - CDynamicArray* - get_oob_indices(const SGVector& in_bag); + std::shared_ptr> + get_oob_indices(const SGVector& in_bag); protected: /** bags array */ - CDynamicObjectArray* m_bags; + std::vector> m_bags; /** features to train on */ - CFeatures* m_features; + std::shared_ptr m_features; /** machine to use for bagging */ - CMachine* m_machine; + std::shared_ptr m_machine; /** number of bags to create */ int32_t m_num_bags; @@ -186,16 +187,16 @@ namespace shogun int32_t m_bag_size; /** combination rule to use */ - CCombinationRule* m_combination_rule; + std::shared_ptr m_combination_rule; /** indices of all feature vectors that are out of bag */ SGVector m_all_oob_idx; /** array of oob indices */ - CDynamicObjectArray* m_oob_indices; + std::shared_ptr m_oob_indices; /** metric to calculate the oob error */ - CEvaluation* m_oob_evaluation_metric; + std::shared_ptr m_oob_evaluation_metric; #ifndef SWIG public: diff --git a/src/shogun/machine/BaseMulticlassMachine.cpp b/src/shogun/machine/BaseMulticlassMachine.cpp index d6278f96958..9cf0f5d5860 100644 --- a/src/shogun/machine/BaseMulticlassMachine.cpp +++ b/src/shogun/machine/BaseMulticlassMachine.cpp @@ -8,30 +8,27 @@ using namespace shogun; -CBaseMulticlassMachine::CBaseMulticlassMachine() +BaseMulticlassMachine::BaseMulticlassMachine() { - m_machines = new CDynamicObjectArray(); - SG_REF(m_machines); - SG_ADD(&m_machines, "machines", "Machines that jointly make up the multi-class machine."); } -CBaseMulticlassMachine::~CBaseMulticlassMachine() +BaseMulticlassMachine::~BaseMulticlassMachine() { - SG_UNREF(m_machines); + } -int32_t CBaseMulticlassMachine::get_num_machines() const +int32_t BaseMulticlassMachine::get_num_machines() const { - return m_machines->get_num_elements(); + return m_machines.size(); } -EProblemType CBaseMulticlassMachine::get_machine_problem_type() const +EProblemType BaseMulticlassMachine::get_machine_problem_type() const { return PT_MULTICLASS; } -bool CBaseMulticlassMachine::is_label_valid(CLabels *lab) const +bool BaseMulticlassMachine::is_label_valid(std::shared_ptrlab) const { return lab->get_label_type() == LT_MULTICLASS; } diff --git a/src/shogun/machine/BaseMulticlassMachine.h b/src/shogun/machine/BaseMulticlassMachine.h index 3c1ff4be3e3..95c3059d1ec 100644 --- a/src/shogun/machine/BaseMulticlassMachine.h +++ b/src/shogun/machine/BaseMulticlassMachine.h @@ -15,19 +15,18 @@ namespace shogun { -class CDynamicObjectArray; -class CLabels; +class Labels; /** Base class of all Multiclass Machines. */ -class CBaseMulticlassMachine: public CMachine +class BaseMulticlassMachine: public Machine { public: /** constructor */ - CBaseMulticlassMachine(); + BaseMulticlassMachine(); /** destructor */ - virtual ~CBaseMulticlassMachine(); + virtual ~BaseMulticlassMachine(); /** get name */ virtual const char* get_name() const { return "BaseMulticlassMachine"; } @@ -45,12 +44,12 @@ class CBaseMulticlassMachine: public CMachine * * @param lab the labels being checked, guaranteed to be non-NULL */ - virtual bool is_label_valid(CLabels *lab) const; + virtual bool is_label_valid(std::shared_ptr lab) const; protected: /** machines */ - CDynamicObjectArray *m_machines; + std::vector> m_machines; }; } /* shogun */ diff --git a/src/shogun/machine/DirectorKernelMachine.h b/src/shogun/machine/DirectorKernelMachine.h index df61f5994e0..ee353f69c32 100644 --- a/src/shogun/machine/DirectorKernelMachine.h +++ b/src/shogun/machine/DirectorKernelMachine.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Tejas Jogi, Evgeniy Andreev, Soeren Sonnenburg, Yuyu Zhang, + * Authors: Tejas Jogi, Evgeniy Andreev, Soeren Sonnenburg, Yuyu Zhang, * Bjoern Esser */ @@ -20,12 +20,12 @@ namespace shogun { #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine +IGNORE_IN_CLASSLIST class DirectorKernelMachine : public KernelMachine { public: /* default constructor */ - CDirectorKernelMachine() - : CKernelMachine() + DirectorKernelMachine() + : KernelMachine() { } @@ -38,13 +38,13 @@ IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine * @param svs indices of examples, i.e. i's for x_i * @param b bias term */ - CDirectorKernelMachine(CKernel* k, const SGVector alphas, const SGVector svs, float64_t b) - : CKernelMachine(k, alphas, svs, b) + DirectorKernelMachine(std::shared_ptr k, const SGVector alphas, const SGVector svs, float64_t b) + : KernelMachine(k, alphas, svs, b) { } /* destructor */ - virtual ~CDirectorKernelMachine() + virtual ~DirectorKernelMachine() { } @@ -57,12 +57,12 @@ IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL) + virtual bool train(std::shared_ptr data=NULL) { - return CKernelMachine::train(data); + return KernelMachine::train(data); } - virtual bool train_function(CFeatures* data=NULL) + virtual bool train_function(std::shared_ptr data=NULL) { error("Train function of Director Kernel Machine needs to be overridden."); return false; @@ -74,25 +74,25 @@ IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine * @param data (test)data to be classified * @return classified labels */ - virtual CLabels* apply(CFeatures* data=NULL) + virtual ::std::shared_ptr apply(std::shared_ptr data=NULL) { - return CKernelMachine::apply(data); + return KernelMachine::apply(data); } /** apply machine to data in means of binary classification problem */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL) + virtual ::std::shared_ptr apply_binary(std::shared_ptr data=NULL) { - return CKernelMachine::apply_binary(data); + return KernelMachine::apply_binary(data); } /** apply machine to data in means of regression problem */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL) + virtual ::std::shared_ptr apply_regression(std::shared_ptr data=NULL) { - return CKernelMachine::apply_regression(data); + return KernelMachine::apply_regression(data); } /** apply machine to data in means of multiclass classification problem */ - using CKernelMachine::apply_multiclass; + using KernelMachine::apply_multiclass; /** apply kernel machine to one example * @@ -101,25 +101,25 @@ IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine */ virtual float64_t apply_one(int32_t num) { - return CKernelMachine::apply_one(num); + return KernelMachine::apply_one(num); } /** set labels * * @param lab labels */ - virtual void set_labels(CLabels* lab) + virtual void set_labels(std::shared_ptr lab) { - CKernelMachine::set_labels(lab); + KernelMachine::set_labels(lab); } /** get labels * * @return labels */ - virtual CLabels* get_labels() + virtual std::shared_ptr get_labels() { - return CKernelMachine::get_labels(); + return KernelMachine::get_labels(); } /** get classifier type @@ -131,7 +131,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine //TODO change to pure virtual virtual EProblemType get_machine_problem_type() const { - return CKernelMachine::get_machine_problem_type(); + return KernelMachine::get_machine_problem_type(); } virtual const char* get_name() const { return "DirectorKernelMachine"; } @@ -147,7 +147,7 @@ IGNORE_IN_CLASSLIST class CDirectorKernelMachine : public CKernelMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL) + virtual bool train_machine(std::shared_ptr data=NULL) { return train_function(data); } diff --git a/src/shogun/machine/DirectorLinearMachine.h b/src/shogun/machine/DirectorLinearMachine.h index 90e54669cb3..a702e4da564 100644 --- a/src/shogun/machine/DirectorLinearMachine.h +++ b/src/shogun/machine/DirectorLinearMachine.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Evgeniy Andreev, Tejas Jogi, Soeren Sonnenburg, Yuyu Zhang, + * Authors: Evgeniy Andreev, Tejas Jogi, Soeren Sonnenburg, Yuyu Zhang, * Viktor Gal, Bjoern Esser */ @@ -20,18 +20,18 @@ namespace shogun { #define IGNORE_IN_CLASSLIST -IGNORE_IN_CLASSLIST class CDirectorLinearMachine : public CLinearMachine +IGNORE_IN_CLASSLIST class DirectorLinearMachine : public LinearMachine { public: /* default constructor */ - CDirectorLinearMachine() - : CLinearMachine() + DirectorLinearMachine() + : LinearMachine() { } /* destructor */ - virtual ~CDirectorLinearMachine() + virtual ~DirectorLinearMachine() { } @@ -44,12 +44,12 @@ IGNORE_IN_CLASSLIST class CDirectorLinearMachine : public CLinearMachine * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL) + virtual bool train(std::shared_ptr data=NULL) { - return CLinearMachine::train(data); + return LinearMachine::train(data); } - virtual bool train_function(CFeatures* data=NULL) + virtual bool train_function(std::shared_ptr data=NULL) { error("Train function of Director Linear Machine needs to be overridden."); return false; @@ -59,18 +59,18 @@ IGNORE_IN_CLASSLIST class CDirectorLinearMachine : public CLinearMachine * * @param feat features to set */ - virtual void set_features(CDotFeatures* feat) + virtual void set_features(std::shared_ptr feat) { - CLinearMachine::set_features(feat); + LinearMachine::set_features(feat); } /** get features * * @return features */ - virtual CDotFeatures* get_features() + virtual std::shared_ptr get_features() { - return CLinearMachine::get_features(); + return LinearMachine::get_features(); } /** apply machine to data @@ -79,47 +79,47 @@ IGNORE_IN_CLASSLIST class CDirectorLinearMachine : public CLinearMachine * @param data (test)data to be classified * @return classified labels */ - virtual CLabels* apply(CFeatures* data=NULL) + virtual std::shared_ptr apply(std::shared_ptr data=NULL) { - return CLinearMachine::apply(data); + return LinearMachine::apply(data); } /** apply machine to data in means of binary classification problem */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL) + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL) { - return CLinearMachine::apply_binary(data); + return LinearMachine::apply_binary(data); } /** apply machine to data in means of regression problem */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL) + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL) { - return CLinearMachine::apply_regression(data); + return LinearMachine::apply_regression(data); } /** apply machine to data in means of multiclass classification problem */ - using CLinearMachine::apply_multiclass; + using LinearMachine::apply_multiclass; virtual float64_t apply_one(int32_t vec_idx) { - return CLinearMachine::apply_one(vec_idx); + return LinearMachine::apply_one(vec_idx); } /** set labels * * @param lab labels */ - virtual void set_labels(CLabels* lab) + virtual void set_labels(std::shared_ptr lab) { - CLinearMachine::set_labels(lab); + LinearMachine::set_labels(lab); } /** get labels * * @return labels */ - virtual CLabels* get_labels() + virtual std::shared_ptr get_labels() { - return CLinearMachine::get_labels(); + return LinearMachine::get_labels(); } /** get classifier type @@ -131,7 +131,7 @@ IGNORE_IN_CLASSLIST class CDirectorLinearMachine : public CLinearMachine //TODO change to pure virtual virtual EProblemType get_machine_problem_type() const { - return CLinearMachine::get_machine_problem_type(); + return LinearMachine::get_machine_problem_type(); } virtual const char* get_name() const { return "DirectorLinearMachine"; } @@ -147,7 +147,7 @@ IGNORE_IN_CLASSLIST class CDirectorLinearMachine : public CLinearMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL) + virtual bool train_machine(std::shared_ptr data=NULL) { return train_function(data); } diff --git a/src/shogun/machine/DistanceMachine.cpp b/src/shogun/machine/DistanceMachine.cpp index cd15950751f..d3df1e89526 100644 --- a/src/shogun/machine/DistanceMachine.cpp +++ b/src/shogun/machine/DistanceMachine.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn, Thoralf Klein, + * Authors: Heiko Strathmann, Soeren Sonnenburg, Sergey Lisitsyn, Thoralf Klein, * Viktor Gal, Evan Shelhamer */ @@ -15,24 +15,24 @@ using namespace shogun; -CDistanceMachine::CDistanceMachine() -: CMachine() +DistanceMachine::DistanceMachine() +: Machine() { init(); } -CDistanceMachine::~CDistanceMachine() +DistanceMachine::~DistanceMachine() { - SG_UNREF(distance); + } -void CDistanceMachine::init() +void DistanceMachine::init() { distance=NULL; SG_ADD(&distance, "distance", "Distance to use", ParameterProperties::HYPER); } -void CDistanceMachine::distances_lhs(SGVector& result, index_t idx_a1, index_t idx_a2, index_t idx_b) +void DistanceMachine::distances_lhs(SGVector& result, index_t idx_a1, index_t idx_a2, index_t idx_b) { int32_t num_threads; int32_t num_vec; @@ -64,7 +64,7 @@ void CDistanceMachine::distances_lhs(SGVector& result, index_t idx_a1 } } -void CDistanceMachine::distances_rhs(SGVector& result, index_t idx_b1, index_t idx_b2, index_t idx_a) +void DistanceMachine::distances_rhs(SGVector& result, index_t idx_b1, index_t idx_b2, index_t idx_a) { int32_t num_threads; int32_t num_vec; @@ -96,17 +96,16 @@ void CDistanceMachine::distances_rhs(SGVector& result, index_t idx_b1 } } -CMulticlassLabels* CDistanceMachine::apply_multiclass(CFeatures* data) +std::shared_ptr DistanceMachine::apply_multiclass(std::shared_ptr data) { if (data) { /* set distance features to given ones and apply to all */ - CFeatures* lhs=distance->get_lhs(); + auto lhs=distance->get_lhs(); distance->init(lhs, data); - SG_UNREF(lhs); /* build result labels and classify all elements of procedure */ - CMulticlassLabels* result=new CMulticlassLabels(data->get_num_vectors()); + auto result=std::make_shared(data->get_num_vectors()); for (index_t i=0; iget_num_vectors(); ++i) result->set_label(i, apply_one(i)); return result; @@ -114,20 +113,17 @@ CMulticlassLabels* CDistanceMachine::apply_multiclass(CFeatures* data) else { /* call apply on complete right hand side */ - CFeatures* all=distance->get_rhs(); - CMulticlassLabels* result = apply_multiclass(all); - SG_UNREF(all); - return result; + auto all=distance->get_rhs(); + return apply_multiclass(all); } return NULL; } -float64_t CDistanceMachine::apply_one(int32_t num) +float64_t DistanceMachine::apply_one(int32_t num) { /* number of clusters */ - CFeatures* lhs=distance->get_lhs(); + auto lhs=distance->get_lhs(); int32_t num_clusters=lhs->get_num_vectors(); - SG_UNREF(lhs); /* (multiple threads) calculate distances to all cluster centers */ SGVector dists(num_clusters); @@ -149,16 +145,16 @@ float64_t CDistanceMachine::apply_one(int32_t num) return best_index; } -void CDistanceMachine::set_distance(CDistance* d) +void DistanceMachine::set_distance(std::shared_ptr d) { - SG_REF(d); - SG_UNREF(distance); + + distance=d; } -CDistance* CDistanceMachine::get_distance() const +std::shared_ptr DistanceMachine::get_distance() const { - SG_REF(distance); + return distance; } diff --git a/src/shogun/machine/DistanceMachine.h b/src/shogun/machine/DistanceMachine.h index 0d848fa5e79..86f3c7fcfec 100644 --- a/src/shogun/machine/DistanceMachine.h +++ b/src/shogun/machine/DistanceMachine.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann, Yuyu Zhang, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Heiko Strathmann, Yuyu Zhang, * Thoralf Klein, Evan Shelhamer, Saurabh Goyal */ @@ -16,34 +16,34 @@ namespace shogun { - class CDistance; - class CFeatures; - class CMulticlassLabels; + class Distance; + class Features; + class MulticlassLabels; /** @brief A generic DistanceMachine interface. * * A distance machine is based on a a-priori choosen distance. */ -class CDistanceMachine : public CMachine +class DistanceMachine : public Machine { public: /** default constructor */ - CDistanceMachine(); + DistanceMachine(); /** destructor */ - virtual ~CDistanceMachine(); + virtual ~DistanceMachine(); /** set distance * * @param d distance to set */ - void set_distance(CDistance* d); + void set_distance(std::shared_ptr d); /** get distance * * @return distance */ - CDistance* get_distance() const; + std::shared_ptr get_distance() const; /** * get distance functions for lhs feature vectors @@ -81,7 +81,7 @@ class CDistanceMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /** Apply machine to one example. * Cluster index with smallest distance to to be classified element is @@ -97,7 +97,7 @@ class CDistanceMachine : public CMachine protected: /** the distance */ - CDistance* distance; + std::shared_ptr distance; }; } #endif diff --git a/src/shogun/machine/FeatureDispatchCRTP.h b/src/shogun/machine/FeatureDispatchCRTP.h index 96e9b39684d..8672fdf9d20 100644 --- a/src/shogun/machine/FeatureDispatchCRTP.h +++ b/src/shogun/machine/FeatureDispatchCRTP.h @@ -15,39 +15,39 @@ namespace shogun { - class CFeatures; - class CLabels; + class Features; + class Labels; #define IGNORE_IN_CLASSLIST template - IGNORE_IN_CLASSLIST class CDenseRealDispatch : public T + IGNORE_IN_CLASSLIST class DenseRealDispatch : public T { public: /** Default constructor */ - CDenseRealDispatch() : T() + DenseRealDispatch() : T() { } - virtual ~CDenseRealDispatch() + virtual ~DenseRealDispatch() { } protected: - virtual bool train_dense(CFeatures* data) + virtual bool train_dense(std::shared_ptr data) { auto this_casted = this->template as

(); switch (data->get_feature_type()) { case F_DREAL: return this_casted->template train_machine_templated( - data->as>()); + std::dynamic_pointer_cast>(data)); case F_SHORTREAL: return this_casted->template train_machine_templated( - data->as>()); + std::dynamic_pointer_cast>(data)); case F_LONGREAL: return this_casted ->template train_machine_templated( - data->as>()); + std::dynamic_pointer_cast>(data)); default: error( "Training with {} of provided type {} is not " @@ -70,33 +70,33 @@ namespace shogun }; template - IGNORE_IN_CLASSLIST class CStringFeaturesDispatch : public T + IGNORE_IN_CLASSLIST class StringFeaturesDispatch : public T { public: /** Default constructor */ - CStringFeaturesDispatch() : T() + StringFeaturesDispatch() : T() { } - virtual ~CStringFeaturesDispatch() + virtual ~StringFeaturesDispatch() { } protected: - virtual bool train_string(CFeatures* data) + virtual bool train_string(std::shared_ptr data) { auto this_casted = this->template as

(); switch (data->get_feature_type()) { case F_BYTE: return this_casted->template train_machine_templated( - data->as>()); + data->as>()); case F_CHAR: return this_casted->template train_machine_templated( - data->as>()); + data->as>()); case F_WORD: return this_casted->template train_machine_templated( - data->as>()); + data->as>()); default: error( "Training with {} of provided type {} is " diff --git a/src/shogun/machine/GaussianProcessMachine.cpp b/src/shogun/machine/GaussianProcessMachine.cpp index 069e9563577..448e859ea14 100644 --- a/src/shogun/machine/GaussianProcessMachine.cpp +++ b/src/shogun/machine/GaussianProcessMachine.cpp @@ -46,18 +46,18 @@ using namespace shogun; using namespace Eigen; -CGaussianProcessMachine::CGaussianProcessMachine() +GaussianProcessMachine::GaussianProcessMachine() { init(); } -CGaussianProcessMachine::CGaussianProcessMachine(CInference* method) +GaussianProcessMachine::GaussianProcessMachine(std::shared_ptr method) { init(); set_inference_method(method); } -void CGaussianProcessMachine::init() +void GaussianProcessMachine::init() { m_method=NULL; m_compute_variance = false; @@ -67,19 +67,19 @@ void CGaussianProcessMachine::init() SG_ADD(&m_compute_variance, "compute_variance", "Whether predictive variance is computed in predictions"); } -CGaussianProcessMachine::~CGaussianProcessMachine() +GaussianProcessMachine::~GaussianProcessMachine() { - SG_UNREF(m_method); + } -SGVector CGaussianProcessMachine::get_posterior_means(CFeatures* data) +SGVector GaussianProcessMachine::get_posterior_means(std::shared_ptr data) { require(m_method, "Inference method should not be NULL"); - CFeatures* feat; + std::shared_ptr feat; - CSingleSparseInference* sparse_method= - dynamic_cast(m_method); + auto sparse_method= + std::dynamic_pointer_cast(m_method); // use inducing features for sparse inference method if (sparse_method) { @@ -90,9 +90,9 @@ SGVector CGaussianProcessMachine::get_posterior_means(CFeatures* data feat=m_method->get_features(); // get kernel and compute kernel matrix: K(feat, data)*scale^2 - CKernel* training_kernel=m_method->get_kernel(); - CKernel* kernel = training_kernel->clone()->as(); - SG_UNREF(training_kernel); + auto training_kernel=m_method->get_kernel(); + auto kernel = std::dynamic_pointer_cast(training_kernel->clone()); + kernel->init(feat, data); @@ -101,21 +101,21 @@ SGVector CGaussianProcessMachine::get_posterior_means(CFeatures* data Map eigen_Ks(k_trts.matrix, k_trts.num_rows, k_trts.num_cols); // compute Ks=Ks*scale^2 - eigen_Ks*=CMath::sq(m_method->get_scale()); + eigen_Ks*=Math::sq(m_method->get_scale()); // cleanup - SG_UNREF(feat); - SG_UNREF(kernel); + + // get alpha and create eigen representation of it SGVector alpha=m_method->get_alpha(); Map eigen_alpha(alpha.vector, alpha.vlen); // get mean and create eigen representation of it - CMeanFunction* mean_function=m_method->get_mean(); + auto mean_function=m_method->get_mean(); SGVector mean=mean_function->get_mean_vector(data); Map eigen_mean(mean.vector, mean.vlen); - SG_UNREF(mean_function); + const index_t C=alpha.vlen/k_trts.num_rows; const index_t n=k_trts.num_rows; @@ -131,16 +131,16 @@ SGVector CGaussianProcessMachine::get_posterior_means(CFeatures* data return mu; } -SGVector CGaussianProcessMachine::get_posterior_variances( - CFeatures* data) +SGVector GaussianProcessMachine::get_posterior_variances( + std::shared_ptr data) { require(m_method, "Inference method should not be NULL"); - CFeatures* feat; + std::shared_ptr feat; bool is_sparse=false; - CSingleSparseInference* sparse_method= - dynamic_cast(m_method); + auto sparse_method= + std::dynamic_pointer_cast(m_method); // use inducing features for sparse inference method if (sparse_method) { @@ -152,9 +152,9 @@ SGVector CGaussianProcessMachine::get_posterior_variances( feat=m_method->get_features(); // get kernel and compute kernel matrix: K(data, data)*scale^2 - CKernel* training_kernel=m_method->get_kernel(); - CKernel* kernel = training_kernel->clone()->as(); - SG_UNREF(training_kernel); + auto training_kernel=m_method->get_kernel(); + auto kernel = std::dynamic_pointer_cast(training_kernel->clone()); + kernel->init(data, data); // get kernel matrix and create eigen representation of it @@ -162,7 +162,7 @@ SGVector CGaussianProcessMachine::get_posterior_variances( Map eigen_Kss_diag(k_tsts.vector, k_tsts.vlen); // compute Kss=Kss*scale^2 - eigen_Kss_diag*=CMath::sq(m_method->get_scale()); + eigen_Kss_diag*=Math::sq(m_method->get_scale()); // compute kernel matrix: K(feat, data)*scale^2 kernel->init(feat, data); @@ -172,11 +172,11 @@ SGVector CGaussianProcessMachine::get_posterior_variances( Map eigen_Ks(k_trts.matrix, k_trts.num_rows, k_trts.num_cols); // compute Ks=Ks*scale^2 - eigen_Ks*=CMath::sq(m_method->get_scale()); + eigen_Ks*=Math::sq(m_method->get_scale()); // cleanup - SG_UNREF(kernel); - SG_UNREF(feat); + + // get shogun representation of cholesky and create eigen representation SGMatrix L=m_method->get_cholesky(); diff --git a/src/shogun/machine/GaussianProcessMachine.h b/src/shogun/machine/GaussianProcessMachine.h index dfe8f8dd060..cfa6383a71b 100644 --- a/src/shogun/machine/GaussianProcessMachine.h +++ b/src/shogun/machine/GaussianProcessMachine.h @@ -57,19 +57,19 @@ namespace shogun * * where \f$m(x)\f$ - mean function, \f$k(x, x')\f$ - covariance function. */ -class CGaussianProcessMachine : public CMachine +class GaussianProcessMachine : public Machine { public: /** default constructor */ - CGaussianProcessMachine(); + GaussianProcessMachine(); /** constructor * * @param method inference method */ - CGaussianProcessMachine(CInference* method); + GaussianProcessMachine(std::shared_ptr method); - virtual ~CGaussianProcessMachine(); + virtual ~GaussianProcessMachine(); /** returns name of the machine * @@ -85,7 +85,7 @@ class CGaussianProcessMachine : public CMachine * * @return posterior means */ - SGVector get_posterior_means(CFeatures* data); + SGVector get_posterior_means(std::shared_ptr data); /** returns a variance \f$\sigma^2\f$ of a Gaussian distribution * \f$\mathcal{N}(\mu,\sigma^2)\f$, which is an approximation to the @@ -95,15 +95,15 @@ class CGaussianProcessMachine : public CMachine * * @return posterior variances */ - SGVector get_posterior_variances(CFeatures* data); + SGVector get_posterior_variances(std::shared_ptr data); /** get inference method * * @return inference method, which is used by Gaussian process machine */ - CInference* get_inference_method() const + std::shared_ptr get_inference_method() const { - SG_REF(m_method); + return m_method; } @@ -111,10 +111,10 @@ class CGaussianProcessMachine : public CMachine * * @param method inference method */ - void set_inference_method(CInference* method) + void set_inference_method(std::shared_ptr method) { - SG_REF(method); - SG_UNREF(m_method); + + m_method=method; } @@ -122,9 +122,9 @@ class CGaussianProcessMachine : public CMachine * * @param lab labels to set */ - virtual void set_labels(CLabels* lab) + virtual void set_labels(std::shared_ptr lab) { - CMachine::set_labels(lab); + Machine::set_labels(lab); m_method->set_labels(lab); } private: @@ -132,7 +132,7 @@ class CGaussianProcessMachine : public CMachine protected: /** inference method */ - CInference* m_method; + std::shared_ptr m_method; /** Whether predictive variance is computed in predictions. If true, the * values are stored in the current_values vector of the predicted labels */ diff --git a/src/shogun/machine/IterativeMachine.h b/src/shogun/machine/IterativeMachine.h index c657eb92878..813f58a8540 100644 --- a/src/shogun/machine/IterativeMachine.h +++ b/src/shogun/machine/IterativeMachine.h @@ -16,19 +16,19 @@ namespace shogun { - class CFeatures; - class CLabels; + class Features; + class Labels; /** @brief Mix-in class that implements an iterative model * whose training can be prematurely stopped, and in particular be * resumed, anytime. */ template - class CIterativeMachine : public T + class IterativeMachine : public T { public: /** Default constructor */ - CIterativeMachine() : T() + IterativeMachine() : T() { m_current_iteration = 0; m_complete = false; @@ -45,9 +45,9 @@ namespace shogun SG_ADD( &m_continue_features, "continue_features", "Continue Features"); } - virtual ~CIterativeMachine() + virtual ~IterativeMachine() { - SG_UNREF(m_continue_features); + } /** Returns convergence status */ @@ -59,7 +59,7 @@ namespace shogun virtual bool continue_train() { this->reset_computation_variables(); - this->put("features", m_continue_features); + //this->put("features", m_continue_features); auto pb = SG_PROGRESS(range(m_max_iterations)); while (m_current_iteration < m_max_iterations && !m_complete) @@ -92,12 +92,12 @@ namespace shogun } protected: - virtual bool train_machine(CFeatures* data = NULL) + virtual bool train_machine(std::shared_ptr data = NULL) { - if (data) + if (data) { - SG_REF(data); - SG_UNREF(m_continue_features); + + m_continue_features = data; } m_current_iteration = 0; @@ -113,7 +113,7 @@ namespace shogun /** To be overloaded in subclasses to initialize the model for training */ - virtual void init_model(CFeatures* data = NULL) = 0; + virtual void init_model(std::shared_ptr data = NULL) = 0; /** Can be overloaded in subclasses to show more information * and/or clean up states @@ -123,7 +123,7 @@ namespace shogun } /** Stores features to continue training */ - CFeatures* m_continue_features; + std::shared_ptr m_continue_features; /** Maximum Iterations */ int32_t m_max_iterations; /** Current iteration of training loop */ @@ -132,4 +132,4 @@ namespace shogun bool m_complete; }; } -#endif \ No newline at end of file +#endif diff --git a/src/shogun/machine/KernelMachine.cpp b/src/shogun/machine/KernelMachine.cpp index 10d67087d57..b3a01298a33 100644 --- a/src/shogun/machine/KernelMachine.cpp +++ b/src/shogun/machine/KernelMachine.cpp @@ -25,7 +25,7 @@ using namespace shogun; #ifndef DOXYGEN_SHOULD_SKIP_THIS struct S_THREAD_PARAM_KERNEL_MACHINE { - CKernelMachine* kernel_machine; + KernelMachine* kernel_machine; float64_t* result; int32_t start; int32_t end; @@ -37,13 +37,13 @@ struct S_THREAD_PARAM_KERNEL_MACHINE }; #endif // DOXYGEN_SHOULD_SKIP_THIS -CKernelMachine::CKernelMachine() : CMachine() +KernelMachine::KernelMachine() : Machine() { init(); } -CKernelMachine::CKernelMachine(CKernel* k, SGVector alphas, - SGVector svs, float64_t b) : CMachine() +KernelMachine::KernelMachine(std::shared_ptr k, SGVector alphas, + SGVector svs, float64_t b) : Machine() { init(); @@ -56,14 +56,14 @@ CKernelMachine::CKernelMachine(CKernel* k, SGVector alphas, set_bias(b); } -CKernelMachine::CKernelMachine(CKernelMachine* machine) : CMachine() +KernelMachine::KernelMachine(std::shared_ptr machine) : Machine() { init(); SGVector alphas = machine->get_alphas().clone(); SGVector svs = machine->get_support_vectors().clone(); float64_t bias = machine->get_bias(); - CKernel* ker = machine->get_kernel(); + auto ker = machine->get_kernel(); int32_t num_sv = svs.vlen; create_new_model(num_sv); @@ -73,71 +73,68 @@ CKernelMachine::CKernelMachine(CKernelMachine* machine) : CMachine() set_kernel(ker); } -CKernelMachine::~CKernelMachine() +KernelMachine::~KernelMachine() { - SG_UNREF(kernel); } -void CKernelMachine::set_kernel(CKernel* k) +void KernelMachine::set_kernel(std::shared_ptr k) { - SG_REF(k); - SG_UNREF(kernel); kernel=k; } -CKernel* CKernelMachine::get_kernel() +std::shared_ptr KernelMachine::get_kernel() { - SG_REF(kernel); + return kernel; } -void CKernelMachine::set_batch_computation_enabled(bool enable) +void KernelMachine::set_batch_computation_enabled(bool enable) { use_batch_computation=enable; } -bool CKernelMachine::get_batch_computation_enabled() +bool KernelMachine::get_batch_computation_enabled() { return use_batch_computation; } -void CKernelMachine::set_linadd_enabled(bool enable) +void KernelMachine::set_linadd_enabled(bool enable) { use_linadd=enable; } -bool CKernelMachine::get_linadd_enabled() +bool KernelMachine::get_linadd_enabled() { return use_linadd; } -void CKernelMachine::set_bias_enabled(bool enable_bias) +void KernelMachine::set_bias_enabled(bool enable_bias) { use_bias=enable_bias; } -bool CKernelMachine::get_bias_enabled() +bool KernelMachine::get_bias_enabled() { return use_bias; } -float64_t CKernelMachine::get_bias() +float64_t KernelMachine::get_bias() { return m_bias; } -void CKernelMachine::set_bias(float64_t bias) +void KernelMachine::set_bias(float64_t bias) { m_bias=bias; } -int32_t CKernelMachine::get_support_vector(int32_t idx) +int32_t KernelMachine::get_support_vector(int32_t idx) { ASSERT(m_svs.vector && idx alphas) +void KernelMachine::set_alphas(SGVector alphas) { m_alpha = alphas; } -void CKernelMachine::set_support_vectors(SGVector svs) +void KernelMachine::set_support_vectors(SGVector svs) { m_svs = svs; } -SGVector CKernelMachine::get_support_vectors() +SGVector KernelMachine::get_support_vectors() { return m_svs; } -SGVector CKernelMachine::get_alphas() +SGVector KernelMachine::get_alphas() { return m_alpha; } -bool CKernelMachine::create_new_model(int32_t num) +bool KernelMachine::create_new_model(int32_t num) { m_alpha=SGVector(); m_svs=SGVector(); @@ -208,7 +205,7 @@ bool CKernelMachine::create_new_model(int32_t num) return true; } -bool CKernelMachine::init_kernel_optimization() +bool KernelMachine::init_kernel_optimization() { int32_t num_sv=get_num_support_vectors(); @@ -239,22 +236,22 @@ bool CKernelMachine::init_kernel_optimization() return false; } -CRegressionLabels* CKernelMachine::apply_regression(CFeatures* data) +std::shared_ptr KernelMachine::apply_regression(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CRegressionLabels(outputs); + return std::make_shared(outputs); } -CBinaryLabels* CKernelMachine::apply_binary(CFeatures* data) +std::shared_ptr KernelMachine::apply_binary(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CBinaryLabels(outputs); + return std::make_shared(outputs); } -SGVector CKernelMachine::apply_get_outputs(CFeatures* data) +SGVector KernelMachine::apply_get_outputs(std::shared_ptr data) { SG_TRACE("entering {}::apply_get_outputs({} at {})", - get_name(), data ? data->get_name() : "NULL", fmt::ptr(data)); + get_name(), data ? data->get_name() : "NULL", fmt::ptr(data.get())); require(kernel, "{}::apply_get_outputs(): No kernel assigned!"); @@ -268,11 +265,11 @@ SGVector CKernelMachine::apply_get_outputs(CFeatures* data) if (data) { - CFeatures* lhs=kernel->get_lhs(); + auto lhs=kernel->get_lhs(); require(lhs, "{}::apply_get_outputs(): No left hand side specified", get_name()); kernel->init(lhs, data); - SG_UNREF(lhs); + } /* using the features to get num vectors is far safer than using the kernel @@ -282,9 +279,9 @@ SGVector CKernelMachine::apply_get_outputs(CFeatures* data) * However, the below version works * TODO Heiko Strathmann */ - CFeatures* rhs=kernel->get_rhs(); + auto rhs=kernel->get_rhs(); int32_t num_vectors=rhs ? rhs->get_num_vectors() : kernel->get_num_vec_rhs(); - SG_UNREF(rhs) + SGVector output(num_vectors); @@ -383,41 +380,41 @@ SGVector CKernelMachine::apply_get_outputs(CFeatures* data) } SG_TRACE("leaving {}::apply_get_outputs({} at {})", - get_name(), data ? data->get_name() : "NULL", fmt::ptr(data)); + get_name(), data ? data->get_name() : "NULL", fmt::ptr(data.get())); return output; } -void CKernelMachine::store_model_features() +void KernelMachine::store_model_features() { if (!kernel) error("kernel is needed to store SV features."); - CFeatures* lhs=kernel->get_lhs(); - CFeatures* rhs=kernel->get_rhs(); + auto lhs=kernel->get_lhs(); + auto rhs=kernel->get_rhs(); if (!lhs) error("kernel lhs is needed to store SV features."); /* copy sv feature data */ - CFeatures* sv_features=lhs->copy_subset(m_svs); - SG_UNREF(lhs); + auto sv_features=lhs->copy_subset(m_svs); + /* set new lhs to kernel */ kernel->init(sv_features, rhs); /* unref rhs */ - SG_UNREF(rhs); + /* was SG_REF'ed by copy_subset */ - SG_UNREF(sv_features); + /* now sv indices are just the identity */ m_svs.range_fill(); } -float64_t CKernelMachine::apply_one(int32_t num) +float64_t KernelMachine::apply_one(int32_t num) { ASSERT(kernel) @@ -436,7 +433,7 @@ float64_t CKernelMachine::apply_one(int32_t num) } } -void CKernelMachine::init() +void KernelMachine::init() { m_bias=0.0; kernel=NULL; diff --git a/src/shogun/machine/KernelMachine.h b/src/shogun/machine/KernelMachine.h index 8d804791c55..681223b910f 100644 --- a/src/shogun/machine/KernelMachine.h +++ b/src/shogun/machine/KernelMachine.h @@ -1,9 +1,9 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, Tejas Jogi, - * Evgeniy Andreev, Evan Shelhamer, Yuyu Zhang, Chiyuan Zhang, - * Weijie Lin, Fernando Iglesias, Bjoern Esser, Thoralf Klein, + * Authors: Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, Tejas Jogi, + * Evgeniy Andreev, Evan Shelhamer, Yuyu Zhang, Chiyuan Zhang, + * Weijie Lin, Fernando Iglesias, Bjoern Esser, Thoralf Klein, * Saurabh Goyal */ @@ -19,12 +19,12 @@ namespace shogun { -class CLabels; -class CBinaryLabels; -class CRegressionLabels; -class CKernel; -class CCustomKernel; -class CFeatures; +class Labels; +class BinaryLabels; +class RegressionLabels; +class Kernel; +class CustomKernel; +class Features; /** @brief A generic KernelMachine interface. * @@ -41,11 +41,11 @@ class CFeatures; * Using an a-priori choosen kernel, the \f$\alpha_i\f$ and bias are determined * in a training procedure. */ -class CKernelMachine : public CMachine +class KernelMachine : public Machine { public: /** default constructor */ - CKernelMachine(); + KernelMachine(); /** Convenience constructor to initialize a trained kernel * machine @@ -55,15 +55,15 @@ class CKernelMachine : public CMachine * @param svs indices of examples, i.e. i's for x_i * @param b bias term */ - CKernelMachine(CKernel* k, const SGVector alphas, const SGVector svs, float64_t b); + KernelMachine(std::shared_ptr k, const SGVector alphas, const SGVector svs, float64_t b); /** copy constructor * @param machine machine having parameters to copy */ - CKernelMachine(CKernelMachine* machine); + KernelMachine(std::shared_ptr machine); /** destructor */ - virtual ~CKernelMachine(); + virtual ~KernelMachine(); /** Returns the name of the SGSerializable instance. It MUST BE * the CLASS NAME without the prefixed `C'. @@ -76,13 +76,13 @@ class CKernelMachine : public CMachine * * @param k kernel */ - void set_kernel(CKernel* k); + void set_kernel(std::shared_ptr k); /** get kernel * * @return kernel */ - CKernel* get_kernel(); + std::shared_ptr get_kernel(); /** set batch computation enabled * @@ -204,7 +204,7 @@ class CKernelMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); /** apply kernel machine to data * for binary classification task @@ -212,7 +212,7 @@ class CKernelMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /** apply kernel machine to one example * @@ -236,7 +236,7 @@ class CKernelMachine : public CMachine * @param data features to compute outputs * @return outputs */ - SGVector apply_get_outputs(CFeatures* data); + SGVector apply_get_outputs(std::shared_ptr data); private: @@ -245,7 +245,7 @@ class CKernelMachine : public CMachine protected: /** kernel */ - CKernel* kernel; + std::shared_ptr kernel; /** if batch computation is enabled */ bool use_batch_computation; diff --git a/src/shogun/machine/KernelMulticlassMachine.cpp b/src/shogun/machine/KernelMulticlassMachine.cpp index 361d56da01f..890d118e43e 100644 --- a/src/shogun/machine/KernelMulticlassMachine.cpp +++ b/src/shogun/machine/KernelMulticlassMachine.cpp @@ -13,15 +13,15 @@ using namespace shogun; -void CKernelMulticlassMachine::store_model_features() +void KernelMulticlassMachine::store_model_features() { - CKernel *kernel= m_kernel; + auto kernel= m_kernel; if (!kernel) error("{}::store_model_features(): kernel is needed to store SV " "features.", get_name()); - CFeatures* lhs=kernel->get_lhs(); - CFeatures* rhs=kernel->get_rhs(); + auto lhs=kernel->get_lhs(); + auto rhs=kernel->get_rhs(); if (!lhs) { error("{}::store_model_features(): kernel lhs is needed to store " @@ -29,14 +29,14 @@ void CKernelMulticlassMachine::store_model_features() } /* this map will be abused as a map */ - CSet all_sv; - for (index_t i=0; iget_num_elements(); ++i) + Set all_sv; + for (auto m: m_machines) { - auto machine=get_machine(i)->as(); + auto machine=m->as(); for (index_t j=0; jget_num_support_vectors(); ++j) all_sv.add(machine->get_support_vector(j)); - SG_UNREF(machine); + } /* convert map to vector of SV */ @@ -44,20 +44,20 @@ void CKernelMulticlassMachine::store_model_features() for (index_t i=0; icopy_subset(sv_idx); + auto sv_features=lhs->copy_subset(sv_idx); /* now, features are replaced by concatenated SV features */ kernel->init(sv_features, rhs); /* was SG_REF'ed by copy_subset */ - SG_UNREF(sv_features); + /* now the old SV indices have to be mapped to the new features */ /* update SV of all machines */ - for (int32_t i=0; iget_num_elements(); ++i) + for (auto m: m_machines) { - auto machine=get_machine(i)->as(); + auto machine=m->as(); /* for each machine, replace SV by index in sv_idx array */ for (int32_t j=0; jget_num_support_vectors(); ++j) @@ -72,14 +72,14 @@ void CKernelMulticlassMachine::store_model_features() machine->set_support_vector(j, new_sv_idx); } - SG_UNREF(machine); + } - SG_UNREF(lhs); - SG_UNREF(rhs); + + } -CKernelMulticlassMachine::CKernelMulticlassMachine() : CMulticlassMachine(), m_kernel(NULL) +KernelMulticlassMachine::KernelMulticlassMachine() : MulticlassMachine(), m_kernel(NULL) { SG_ADD(&m_kernel,"kernel", "The kernel to be used", ParameterProperties::HYPER); } @@ -90,69 +90,68 @@ CKernelMulticlassMachine::CKernelMulticlassMachine() : CMulticlassMachine(), m_k * @param machine kernel machine * @param labs labels */ -CKernelMulticlassMachine::CKernelMulticlassMachine(CMulticlassStrategy *strategy, CKernel* kernel, CMachine* machine, CLabels* labs) : - CMulticlassMachine(strategy,machine,labs), m_kernel(NULL) +KernelMulticlassMachine::KernelMulticlassMachine(std::shared_ptrstrategy, std::shared_ptr kernel, std::shared_ptr machine, std::shared_ptr labs) : + MulticlassMachine(strategy,machine,labs), m_kernel(NULL) { set_kernel(kernel); SG_ADD(&m_kernel,"kernel", "The kernel to be used", ParameterProperties::HYPER); } /** destructor */ -CKernelMulticlassMachine::~CKernelMulticlassMachine() +KernelMulticlassMachine::~KernelMulticlassMachine() { - SG_UNREF(m_kernel); + } /** set kernel * * @param k kernel */ -void CKernelMulticlassMachine::set_kernel(CKernel* k) +void KernelMulticlassMachine::set_kernel(std::shared_ptr k) { - m_machine->as()->set_kernel(k); - SG_REF(k); - SG_UNREF(m_kernel); + m_machine->as()->set_kernel(k); + + m_kernel=k; } -CKernel* CKernelMulticlassMachine::get_kernel() const +std::shared_ptr KernelMulticlassMachine::get_kernel() const { - SG_REF(m_kernel); + return m_kernel; } -bool CKernelMulticlassMachine::init_machine_for_train(CFeatures* data) +bool KernelMulticlassMachine::init_machine_for_train(std::shared_ptr data) { if (data) m_kernel->init(data,data); - m_machine->as()->set_kernel(m_kernel); + m_machine->as()->set_kernel(m_kernel); return true; } -bool CKernelMulticlassMachine::init_machines_for_apply(CFeatures* data) +bool KernelMulticlassMachine::init_machines_for_apply(std::shared_ptr data) { if (data) { /* set data to rhs for this kernel */ - CFeatures* lhs=m_kernel->get_lhs(); + auto lhs=m_kernel->get_lhs(); m_kernel->init(lhs, data); - SG_UNREF(lhs); + } /* set kernel to all sub-machines */ - for (int32_t i=0; iget_num_elements(); i++) + for (auto m: m_machines) { - auto machine=m_machines->get_element(i)->as(); + auto machine=m->as(); machine->set_kernel(m_kernel); - SG_UNREF(machine); } return true; } -bool CKernelMulticlassMachine::is_ready() +bool KernelMulticlassMachine::is_ready() { if (m_kernel && m_kernel->get_num_vec_lhs() && m_kernel->get_num_vec_rhs()) return true; @@ -160,22 +159,22 @@ bool CKernelMulticlassMachine::is_ready() return false; } -CMachine* CKernelMulticlassMachine::get_machine_from_trained(CMachine* machine) const +std::shared_ptr KernelMulticlassMachine::get_machine_from_trained(std::shared_ptr machine) const { - return new CKernelMachine(machine->as()); + return std::make_shared(machine->as()); } -int32_t CKernelMulticlassMachine::get_num_rhs_vectors() const +int32_t KernelMulticlassMachine::get_num_rhs_vectors() const { return m_kernel->get_num_vec_rhs(); } -void CKernelMulticlassMachine::add_machine_subset(SGVector subset) +void KernelMulticlassMachine::add_machine_subset(SGVector subset) { not_implemented(SOURCE_LOCATION); } -void CKernelMulticlassMachine::remove_machine_subset() +void KernelMulticlassMachine::remove_machine_subset() { not_implemented(SOURCE_LOCATION); } diff --git a/src/shogun/machine/KernelMulticlassMachine.h b/src/shogun/machine/KernelMulticlassMachine.h index 5e8ef79a3c8..70cf3bc010d 100644 --- a/src/shogun/machine/KernelMulticlassMachine.h +++ b/src/shogun/machine/KernelMulticlassMachine.h @@ -16,15 +16,15 @@ namespace shogun { -class CFeatures; -class CKernel; +class Features; +class Kernel; /** @brief generic kernel multiclass */ -class CKernelMulticlassMachine : public CMulticlassMachine +class KernelMulticlassMachine : public MulticlassMachine { public: /** default constructor */ - CKernelMulticlassMachine(); + KernelMulticlassMachine(); /** standard constructor * @param strategy multiclass strategy @@ -32,10 +32,10 @@ class CKernelMulticlassMachine : public CMulticlassMachine * @param machine kernel machine * @param labs labels */ - CKernelMulticlassMachine(CMulticlassStrategy *strategy, CKernel* kernel, CMachine* machine, CLabels* labs); + KernelMulticlassMachine(std::shared_ptrstrategy, std::shared_ptr kernel, std::shared_ptr machine, std::shared_ptr labs); /** destructor */ - virtual ~CKernelMulticlassMachine(); + virtual ~KernelMulticlassMachine(); /** get name */ virtual const char* get_name() const @@ -47,13 +47,13 @@ class CKernelMulticlassMachine : public CMulticlassMachine * * @param k kernel */ - void set_kernel(CKernel* k); + void set_kernel(std::shared_ptr k); /** get kernel * * @return kernel */ - CKernel* get_kernel() const; + std::shared_ptr get_kernel() const; /** Stores feature data of underlying model. * @@ -66,16 +66,16 @@ class CKernelMulticlassMachine : public CMulticlassMachine protected: /** init machine for training with kernel init */ - virtual bool init_machine_for_train(CFeatures* data); + virtual bool init_machine_for_train(std::shared_ptr data); /** init machines for applying with kernel init */ - virtual bool init_machines_for_apply(CFeatures* data); + virtual bool init_machines_for_apply(std::shared_ptr data); /** check kernel availability */ virtual bool is_ready(); /** construct kernel machine from given kernel machine */ - virtual CMachine* get_machine_from_trained(CMachine* machine) const; + virtual std::shared_ptr get_machine_from_trained(std::shared_ptr machine) const; /** return number of rhs feature vectors */ virtual int32_t get_num_rhs_vectors() const; @@ -92,7 +92,7 @@ class CKernelMulticlassMachine : public CMulticlassMachine protected: /** kernel */ - CKernel* m_kernel; + std::shared_ptr m_kernel; }; } diff --git a/src/shogun/machine/KernelStructuredOutputMachine.cpp b/src/shogun/machine/KernelStructuredOutputMachine.cpp index a307e908402..fa940d46a52 100644 --- a/src/shogun/machine/KernelStructuredOutputMachine.cpp +++ b/src/shogun/machine/KernelStructuredOutputMachine.cpp @@ -9,41 +9,41 @@ using namespace shogun; -CKernelStructuredOutputMachine::CKernelStructuredOutputMachine() -: CStructuredOutputMachine(), m_kernel(NULL) +KernelStructuredOutputMachine::KernelStructuredOutputMachine() +: StructuredOutputMachine(), m_kernel(NULL) { register_parameters(); } -CKernelStructuredOutputMachine::CKernelStructuredOutputMachine( - CStructuredModel* model, - CStructuredLabels* labs, - CKernel* kernel) -: CStructuredOutputMachine(model, labs), m_kernel(NULL) +KernelStructuredOutputMachine::KernelStructuredOutputMachine( + std::shared_ptr model, + std::shared_ptr labs, + std::shared_ptr kernel) +: StructuredOutputMachine(model, labs), m_kernel(NULL) { set_kernel(kernel); register_parameters(); } -CKernelStructuredOutputMachine::~CKernelStructuredOutputMachine() +KernelStructuredOutputMachine::~KernelStructuredOutputMachine() { - SG_UNREF(m_kernel) + } -void CKernelStructuredOutputMachine::set_kernel(CKernel* k) +void KernelStructuredOutputMachine::set_kernel(std::shared_ptr k) { - SG_REF(k); - SG_UNREF(m_kernel); + + m_kernel = k; } -CKernel* CKernelStructuredOutputMachine::get_kernel() const +std::shared_ptr KernelStructuredOutputMachine::get_kernel() const { - SG_REF(m_kernel); + return m_kernel; } -void CKernelStructuredOutputMachine::register_parameters() +void KernelStructuredOutputMachine::register_parameters() { - SG_ADD((CSGObject**)&m_kernel, "m_kernel", "The kernel", ParameterProperties::HYPER); + SG_ADD((std::shared_ptr*)&m_kernel, "m_kernel", "The kernel", ParameterProperties::HYPER); } diff --git a/src/shogun/machine/KernelStructuredOutputMachine.h b/src/shogun/machine/KernelStructuredOutputMachine.h index b747725bec7..03290924b12 100644 --- a/src/shogun/machine/KernelStructuredOutputMachine.h +++ b/src/shogun/machine/KernelStructuredOutputMachine.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Yuyu Zhang, Shell Hu, Thoralf Klein, + * Authors: Fernando Iglesias, Yuyu Zhang, Shell Hu, Thoralf Klein, * Bjoern Esser, Soeren Sonnenburg */ @@ -15,14 +15,14 @@ namespace shogun { -class CKernel; +class Kernel; /** TODO doc */ -class CKernelStructuredOutputMachine : public CStructuredOutputMachine +class KernelStructuredOutputMachine : public StructuredOutputMachine { public: /** default constructor */ - CKernelStructuredOutputMachine(); + KernelStructuredOutputMachine(); /** standard constructor * @@ -30,22 +30,22 @@ class CKernelStructuredOutputMachine : public CStructuredOutputMachine * @param labs structured labels * @param kernel kernel */ - CKernelStructuredOutputMachine(CStructuredModel* model, CStructuredLabels* labs, CKernel* kernel); + KernelStructuredOutputMachine(std::shared_ptr model, std::shared_ptr labs, std::shared_ptr kernel); /** destructor */ - virtual ~CKernelStructuredOutputMachine(); + virtual ~KernelStructuredOutputMachine(); /** set kernel * * @param f kernel */ - void set_kernel(CKernel* f); + void set_kernel(std::shared_ptr f); /** get kernel * * @return kernel */ - CKernel* get_kernel() const; + std::shared_ptr get_kernel() const; /** @return object name */ virtual const char* get_name() const @@ -59,9 +59,9 @@ class CKernelStructuredOutputMachine : public CStructuredOutputMachine protected: /** kernel */ - CKernel* m_kernel; + std::shared_ptr m_kernel; -}; /* class CKernelStructuredOutputMachine */ +}; /* class KernelStructuredOutputMachine */ } /* namespace shogun */ diff --git a/src/shogun/machine/LinearLatentMachine.cpp b/src/shogun/machine/LinearLatentMachine.cpp index a4e1434b6b7..7126d9716f5 100644 --- a/src/shogun/machine/LinearLatentMachine.cpp +++ b/src/shogun/machine/LinearLatentMachine.cpp @@ -11,14 +11,14 @@ using namespace shogun; -CLinearLatentMachine::CLinearLatentMachine() - : CLinearMachine() +LinearLatentMachine::LinearLatentMachine() + : LinearMachine() { init(); } -CLinearLatentMachine::CLinearLatentMachine(CLatentModel* model, float64_t C) - : CLinearMachine() +LinearLatentMachine::LinearLatentMachine(std::shared_ptr model, float64_t C) + : LinearMachine() { init(); m_C= C; @@ -31,31 +31,31 @@ CLinearLatentMachine::CLinearLatentMachine(CLatentModel* model, float64_t C) set_w(w); } -CLinearLatentMachine::~CLinearLatentMachine() +LinearLatentMachine::~LinearLatentMachine() { - SG_UNREF(m_model); + } -CLatentLabels* CLinearLatentMachine::apply_latent(CFeatures* data) +std::shared_ptr LinearLatentMachine::apply_latent(std::shared_ptr data) { if (m_model == NULL) error("LatentModel is not set!"); - CLatentFeatures* lf = data->as(); + auto lf = std::dynamic_pointer_cast(data); m_model->set_features(lf); return apply_latent(); } -void CLinearLatentMachine::set_model(CLatentModel* latent_model) +void LinearLatentMachine::set_model(std::shared_ptr latent_model) { ASSERT(latent_model != NULL) - SG_REF(latent_model); - SG_UNREF(m_model); + + m_model = latent_model; } -bool CLinearLatentMachine::train_machine(CFeatures* data) +bool LinearLatentMachine::train_machine(std::shared_ptr data) { if (m_model == NULL) error("LatentModel is not set!"); @@ -93,7 +93,7 @@ bool CLinearLatentMachine::train_machine(CFeatures* data) stop = (inner_eps < (0.5*m_C*m_epsilon+1E-8)) && (decrement < m_C*m_epsilon); inner_eps = -decrement*0.01; - inner_eps = CMath::max(inner_eps, 0.5*m_C*m_epsilon); + inner_eps = Math::max(inner_eps, 0.5*m_C*m_epsilon); SG_DEBUG("inner epsilon: {}", inner_eps) /* find argmaxH */ @@ -111,7 +111,7 @@ bool CLinearLatentMachine::train_machine(CFeatures* data) return true; } -void CLinearLatentMachine::init() +void LinearLatentMachine::init() { m_C = 10.0; m_epsilon = 1E-3; diff --git a/src/shogun/machine/LinearLatentMachine.h b/src/shogun/machine/LinearLatentMachine.h index 86246ab5000..30bf7afc0ef 100644 --- a/src/shogun/machine/LinearLatentMachine.h +++ b/src/shogun/machine/LinearLatentMachine.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Viktor Gal, Yuyu Zhang, Thoralf Klein, Sergey Lisitsyn, + * Authors: Viktor Gal, Yuyu Zhang, Thoralf Klein, Sergey Lisitsyn, * Bjoern Esser, Soeren Sonnenburg */ @@ -15,14 +15,14 @@ namespace shogun { - class CFeatures; - class CLatentLabels; - class CLatentModel; + class Features; + class LatentLabels; + class LatentModel; /** @brief abstract implementaion of Linear Machine with latent variable * This is the base implementation of all linear machines with latent variable. */ - class CLinearLatentMachine: public CLinearMachine + class LinearLatentMachine: public LinearMachine { public: @@ -31,29 +31,29 @@ namespace shogun MACHINE_PROBLEM_TYPE(PT_LATENT); /** default contstructor */ - CLinearLatentMachine(); + LinearLatentMachine(); /** constructor * - * @param model the user defined CLatentModel + * @param model the user defined LatentModel * @param C regularisation constant */ - CLinearLatentMachine(CLatentModel* model, float64_t C); + LinearLatentMachine(std::shared_ptr model, float64_t C); - virtual ~CLinearLatentMachine(); + virtual ~LinearLatentMachine(); /** apply linear machine to data set before * * @return classified labels */ - virtual CLatentLabels* apply_latent() = 0; + virtual std::shared_ptr apply_latent() = 0; /** apply linear machine to data * * @param data (test)data to be classified * @return classified labels */ - virtual CLatentLabels* apply_latent(CFeatures* data); + virtual std::shared_ptr apply_latent(std::shared_ptr data); /** Returns the name of the SGSerializable instance. * @@ -104,7 +104,7 @@ namespace shogun * * @param latent_model user defined latent model */ - void set_model(CLatentModel* latent_model); + void set_model(std::shared_ptr latent_model); virtual bool train_require_labels() const { @@ -112,7 +112,7 @@ namespace shogun } protected: - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** inner loop of the latent machine * @@ -124,7 +124,7 @@ namespace shogun protected: /** user supplied latent model */ - CLatentModel* m_model; + std::shared_ptr m_model; /** C */ float64_t m_C; /** epsilon */ diff --git a/src/shogun/machine/LinearMachine.cpp b/src/shogun/machine/LinearMachine.cpp index d50aa6bfb6a..d9402774243 100644 --- a/src/shogun/machine/LinearMachine.cpp +++ b/src/shogun/machine/LinearMachine.cpp @@ -14,12 +14,12 @@ using namespace shogun; -CLinearMachine::CLinearMachine(): CMachine() +LinearMachine::LinearMachine(): Machine() { init(); } -CLinearMachine::CLinearMachine(CLinearMachine* machine) : CMachine() +LinearMachine::LinearMachine(std::shared_ptr machine) : Machine() { init(); require(machine, "No machine provided."); @@ -30,7 +30,7 @@ CLinearMachine::CLinearMachine(CLinearMachine* machine) : CMachine() set_bias(machine->get_bias()); } -void CLinearMachine::init() +void LinearMachine::init() { bias = 0; features = NULL; @@ -38,40 +38,40 @@ void CLinearMachine::init() SG_ADD(&m_w, "w", "Parameter vector w.", ParameterProperties::MODEL); SG_ADD(&bias, "bias", "Bias b.", ParameterProperties::MODEL); SG_ADD( - (CFeatures**)&features, "features", "Feature object."); + (std::shared_ptr*)&features, "features", "Feature object."); } -CLinearMachine::~CLinearMachine() +LinearMachine::~LinearMachine() { - SG_UNREF(features); + } -float64_t CLinearMachine::apply_one(int32_t vec_idx) +float64_t LinearMachine::apply_one(int32_t vec_idx) { return features->dot(vec_idx, m_w) + bias; } -CRegressionLabels* CLinearMachine::apply_regression(CFeatures* data) +std::shared_ptr LinearMachine::apply_regression(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CRegressionLabels(outputs); + return std::make_shared(outputs); } -CBinaryLabels* CLinearMachine::apply_binary(CFeatures* data) +std::shared_ptr LinearMachine::apply_binary(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CBinaryLabels(outputs); + return std::make_shared(outputs); } -SGVector CLinearMachine::apply_get_outputs(CFeatures* data) +SGVector LinearMachine::apply_get_outputs(std::shared_ptr data) { if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(std::static_pointer_cast(data)); } if (!features) @@ -86,36 +86,36 @@ SGVector CLinearMachine::apply_get_outputs(CFeatures* data) return SGVector(out,num); } -SGVector CLinearMachine::get_w() const +SGVector LinearMachine::get_w() const { return m_w; } -void CLinearMachine::set_w(const SGVector w) +void LinearMachine::set_w(const SGVector w) { m_w = w; } -void CLinearMachine::set_bias(float64_t b) +void LinearMachine::set_bias(float64_t b) { bias=b; } -float64_t CLinearMachine::get_bias() const +float64_t LinearMachine::get_bias() const { return bias; } -void CLinearMachine::set_features(CDotFeatures* feat) +void LinearMachine::set_features(std::shared_ptr feat) { - SG_REF(feat); - SG_UNREF(features); + + features=feat; } -CDotFeatures* CLinearMachine::get_features() +std::shared_ptr LinearMachine::get_features() { - SG_REF(features); + return features; } diff --git a/src/shogun/machine/LinearMachine.h b/src/shogun/machine/LinearMachine.h index 38039277fb3..a0781df326a 100644 --- a/src/shogun/machine/LinearMachine.h +++ b/src/shogun/machine/LinearMachine.h @@ -20,10 +20,10 @@ namespace shogun { -class CBinaryLabels; -class CDotFeatures; -class CFeatures; -class CRegressionLabels; +class BinaryLabels; +class DotFeatures; +class Features; +class RegressionLabels; /** @brief Class LinearMachine is a generic interface for all kinds of linear * machines like classifiers. @@ -42,7 +42,7 @@ class CRegressionLabels; * * Note that this framework works with linear classifiers of arbitraty feature * type, e.g. dense and sparse and even string based features. This is - * implemented by using CDotFeatures that may provide a mapping function + * implemented by using DotFeatures that may provide a mapping function * \f$\Phi({\bf x})\mapsto {\cal R^D}\f$ encapsulating all the required * operations (like the dot product). The decision function is thus * @@ -53,23 +53,23 @@ class CRegressionLabels; * The following linear classifiers are implemented * \li Linear Descriminant Analysis (CLDA) * \li Linear Programming Machines (CLPM, CLPBoost) - * \li Perceptron (CPerceptron) - * \li Linear SVMs (CSVMSGD, CLibLinear, CSVMOcas, CSVMLin, CSubgradientSVM) + * \li Perceptron (Perceptron) + * \li Linear SVMs (SVMSGD, LibLinear, SVMOcas, SVMLin, CSubgradientSVM) * - * \sa CDotFeatures + * \sa DotFeatures * * */ -class CLinearMachine : public CMachine +class LinearMachine : public Machine { public: /** default constructor */ - CLinearMachine(); + LinearMachine(); /** destructor */ - virtual ~CLinearMachine(); + virtual ~LinearMachine(); /** copy constructor */ - CLinearMachine(CLinearMachine* machine); + LinearMachine(std::shared_ptr machine); /** get w * @@ -99,7 +99,7 @@ class CLinearMachine : public CMachine * * @param feat features to set */ - virtual void set_features(CDotFeatures* feat); + virtual void set_features(std::shared_ptr feat); /** apply linear machine to data * for binary classification problem @@ -107,7 +107,7 @@ class CLinearMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /** apply linear machine to data * for regression problem @@ -115,7 +115,7 @@ class CLinearMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); /** applies to one vector */ virtual float64_t apply_one(int32_t vec_idx); @@ -124,7 +124,7 @@ class CLinearMachine : public CMachine * * @return features */ - virtual CDotFeatures* get_features(); + virtual std::shared_ptr get_features(); /** Returns the name of the SGSerializable instance. It MUST BE * the CLASS NAME without the prefixed `C'. @@ -140,7 +140,7 @@ class CLinearMachine : public CMachine * @param data features to compute outputs * @return outputs */ - virtual SGVector apply_get_outputs(CFeatures* data); + virtual SGVector apply_get_outputs(std::shared_ptr data); private: @@ -154,7 +154,7 @@ class CLinearMachine : public CMachine float64_t bias; /** features */ - CDotFeatures* features; + std::shared_ptr features; }; } #endif diff --git a/src/shogun/machine/LinearMulticlassMachine.h b/src/shogun/machine/LinearMulticlassMachine.h index 2ad093df0a0..b7270da4124 100644 --- a/src/shogun/machine/LinearMulticlassMachine.h +++ b/src/shogun/machine/LinearMulticlassMachine.h @@ -19,18 +19,18 @@ namespace shogun { -class CDotFeatures; -class CLinearMachine; -class CMulticlassStrategy; +class DotFeatures; +class LinearMachine; +class MulticlassStrategy; /** @brief generic linear multiclass machine */ -class CLinearMulticlassMachine : public CMulticlassMachine +class LinearMulticlassMachine : public MulticlassMachine { public: /** default constructor */ - CLinearMulticlassMachine() : CMulticlassMachine(), m_features(NULL) + LinearMulticlassMachine() : MulticlassMachine(), m_features(NULL) { - SG_ADD((CSGObject**)&m_features, "m_features", "Feature object."); + SG_ADD(&m_features, "m_features", "Feature object."); } /** standard constructor @@ -39,17 +39,17 @@ class CLinearMulticlassMachine : public CMulticlassMachine * @param machine linear machine * @param labs labels */ - CLinearMulticlassMachine(CMulticlassStrategy *strategy, CFeatures* features, CMachine* machine, CLabels* labs) : - CMulticlassMachine(strategy, machine,labs), m_features(NULL) + LinearMulticlassMachine(std::shared_ptr strategy, std::shared_ptr features, std::shared_ptr machine, std::shared_ptr labs) : + MulticlassMachine(strategy, machine,labs), m_features(NULL) { - set_features(features->as()); + set_features(features->as()); SG_ADD(&m_features, "m_features", "Feature object."); } /** destructor */ - virtual ~CLinearMulticlassMachine() + virtual ~LinearMulticlassMachine() { - SG_UNREF(m_features); + } /** get name */ @@ -62,17 +62,13 @@ class CLinearMulticlassMachine : public CMulticlassMachine * * @param f features */ - void set_features(CDotFeatures* f) + void set_features(std::shared_ptr f) { - SG_REF(f); - SG_UNREF(m_features); m_features = f; - - for (index_t i=0; iget_num_elements(); i++) + for (auto m: m_machines) { - auto machine = m_machines->get_element(i)->as(); + auto machine = m->as(); machine->set_features(f); - SG_UNREF(machine); } } @@ -80,41 +76,39 @@ class CLinearMulticlassMachine : public CMulticlassMachine * * @return features */ - CDotFeatures* get_features() const + std::shared_ptr get_features() const { - SG_REF(m_features); return m_features; } protected: /** init machine for train with setting features */ - virtual bool init_machine_for_train(CFeatures* data) + virtual bool init_machine_for_train(std::shared_ptr data) { if (!m_machine) error("No machine given in Multiclass constructor"); if (data) - set_features((CDotFeatures*)data); + set_features(data->as()); - m_machine->as()->set_features(m_features); + m_machine->as()->set_features(m_features); return true; } /** init machines for applying with setting features */ - virtual bool init_machines_for_apply(CFeatures* data) + virtual bool init_machines_for_apply(std::shared_ptr data) { if (data) - set_features((CDotFeatures*)data); + set_features(data->as()); - for (int32_t i=0; iget_num_elements(); i++) + for (auto m: m_machines) { - auto machine = m_machines->get_element(i)->as(); + auto machine = m->as(); ASSERT(m_features) ASSERT(machine) machine->set_features(m_features); - SG_UNREF(machine); } return true; @@ -130,9 +124,9 @@ class CLinearMulticlassMachine : public CMulticlassMachine } /** construct linear machine from given linear machine */ - virtual CMachine* get_machine_from_trained(CMachine* machine) const + virtual std::shared_ptr get_machine_from_trained(std::shared_ptr machine) const { - return new CLinearMachine(machine->as()); + return std::make_shared(machine->as()); } /** get number of rhs feature vectors */ @@ -163,7 +157,7 @@ class CLinearMulticlassMachine : public CMulticlassMachine protected: /** features */ - CDotFeatures* m_features; + std::shared_ptr m_features; }; } #endif diff --git a/src/shogun/machine/LinearStructuredOutputMachine.cpp b/src/shogun/machine/LinearStructuredOutputMachine.cpp index 6f54f9697e6..290d18e4510 100644 --- a/src/shogun/machine/LinearStructuredOutputMachine.cpp +++ b/src/shogun/machine/LinearStructuredOutputMachine.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Shell Hu, Thoralf Klein, Sergey Lisitsyn, + * Authors: Fernando Iglesias, Shell Hu, Thoralf Klein, Sergey Lisitsyn, * Bjoern Esser, Soeren Sonnenburg */ @@ -10,63 +10,63 @@ using namespace shogun; -CLinearStructuredOutputMachine::CLinearStructuredOutputMachine() -: CStructuredOutputMachine() +LinearStructuredOutputMachine::LinearStructuredOutputMachine() +: StructuredOutputMachine() { register_parameters(); } -CLinearStructuredOutputMachine::CLinearStructuredOutputMachine( - CStructuredModel* model, - CStructuredLabels* labs) -: CStructuredOutputMachine(model, labs) +LinearStructuredOutputMachine::LinearStructuredOutputMachine( + std::shared_ptr model, + std::shared_ptr labs) +: StructuredOutputMachine(model, labs) { register_parameters(); } -CLinearStructuredOutputMachine::~CLinearStructuredOutputMachine() +LinearStructuredOutputMachine::~LinearStructuredOutputMachine() { } -void CLinearStructuredOutputMachine::set_w(SGVector< float64_t > w) +void LinearStructuredOutputMachine::set_w(SGVector< float64_t > w) { m_w = w; } -SGVector< float64_t > CLinearStructuredOutputMachine::get_w() const +SGVector< float64_t > LinearStructuredOutputMachine::get_w() const { return m_w; } -CStructuredLabels* CLinearStructuredOutputMachine::apply_structured(CFeatures* data) +std::shared_ptr LinearStructuredOutputMachine::apply_structured(std::shared_ptr data) { if (data) { set_features(data); } - CFeatures* model_features = this->get_features(); + auto model_features = this->get_features(); if (!model_features) { return m_model->structured_labels_factory(); } int num_input_vectors = model_features->get_num_vectors(); - CStructuredLabels* out; + std::shared_ptr out; out = m_model->structured_labels_factory(num_input_vectors); for ( int32_t i = 0 ; i < num_input_vectors ; ++i ) { - CResultSet* result = m_model->argmax(m_w, i, false); + auto result = m_model->argmax(m_w, i, false); out->add_label(result->argmax); - SG_UNREF(result); + } - SG_UNREF(model_features); + return out; } -void CLinearStructuredOutputMachine::register_parameters() +void LinearStructuredOutputMachine::register_parameters() { SG_ADD(&m_w, "m_w", "Weight vector", ParameterProperties::MODEL); } diff --git a/src/shogun/machine/LinearStructuredOutputMachine.h b/src/shogun/machine/LinearStructuredOutputMachine.h index ba14e2c5a0c..8aeb0d4cc5a 100644 --- a/src/shogun/machine/LinearStructuredOutputMachine.h +++ b/src/shogun/machine/LinearStructuredOutputMachine.h @@ -16,24 +16,24 @@ namespace shogun { -class CFeatures; +class Features; /** TODO doc */ -class CLinearStructuredOutputMachine : public CStructuredOutputMachine +class LinearStructuredOutputMachine : public StructuredOutputMachine { public: /** default constructor */ - CLinearStructuredOutputMachine(); + LinearStructuredOutputMachine(); /** standard constructor * * @param model structured model with application specific functions * @param labs structured labels */ - CLinearStructuredOutputMachine(CStructuredModel* model, CStructuredLabels* labs); + LinearStructuredOutputMachine(std::shared_ptr model, std::shared_ptr labs); /** destructor */ - virtual ~CLinearStructuredOutputMachine(); + virtual ~LinearStructuredOutputMachine(); /** set w (useful for modular interfaces) * @@ -55,7 +55,7 @@ class CLinearStructuredOutputMachine : public CStructuredOutputMachine * * @return classified 'labels' */ - virtual CStructuredLabels* apply_structured(CFeatures* data = NULL); + virtual std::shared_ptr apply_structured(std::shared_ptr data = NULL); /** @return object name */ virtual const char* get_name() const @@ -71,7 +71,7 @@ class CLinearStructuredOutputMachine : public CStructuredOutputMachine /** weight vector */ SGVector< float64_t > m_w; -}; /* class CLinearStructuredOutputMachine */ +}; /* class LinearStructuredOutputMachine */ } /* namespace shogun */ diff --git a/src/shogun/machine/Machine.cpp b/src/shogun/machine/Machine.cpp index ca2fdee0d4d..09bce852c85 100644 --- a/src/shogun/machine/Machine.cpp +++ b/src/shogun/machine/Machine.cpp @@ -12,8 +12,8 @@ using namespace shogun; -CMachine::CMachine() - : CStoppableSGObject(), m_max_train_time(0), m_labels(NULL), +Machine::Machine() + : StoppableSGObject(), m_max_train_time(0), m_labels(NULL), m_solver_type(ST_AUTO) { SG_ADD(&m_max_train_time, "max_train_time", "Maximum training time."); @@ -26,12 +26,12 @@ CMachine::CMachine() ST_BLOCK_NORM)); } -CMachine::~CMachine() +Machine::~Machine() { - SG_UNREF(m_labels); + } -bool CMachine::train(CFeatures* data) +bool Machine::train(std::shared_ptr data) { if (train_require_labels()) { @@ -71,54 +71,53 @@ bool CMachine::train(CFeatures* data) return result; } -void CMachine::set_labels(CLabels* lab) +void Machine::set_labels(std::shared_ptr lab) { if (lab != NULL) + { if (!is_label_valid(lab)) error("Invalid label for {}", get_name()); - SG_REF(lab); - SG_UNREF(m_labels); m_labels = lab; + } } -CLabels* CMachine::get_labels() +std::shared_ptr Machine::get_labels() { - SG_REF(m_labels); return m_labels; } -void CMachine::set_max_train_time(float64_t t) +void Machine::set_max_train_time(float64_t t) { m_max_train_time = t; } -float64_t CMachine::get_max_train_time() +float64_t Machine::get_max_train_time() { return m_max_train_time; } -EMachineType CMachine::get_classifier_type() +EMachineType Machine::get_classifier_type() { return CT_NONE; } -void CMachine::set_solver_type(ESolverType st) +void Machine::set_solver_type(ESolverType st) { m_solver_type = st; } -ESolverType CMachine::get_solver_type() +ESolverType Machine::get_solver_type() { return m_solver_type; } -CLabels* CMachine::apply(CFeatures* data) +std::shared_ptr Machine::apply(std::shared_ptr data) { SG_TRACE("entering {}::apply({} at {})", - get_name(), data ? data->get_name() : "NULL", fmt::ptr(data)); + get_name(), data ? data->get_name() : "NULL", fmt::ptr(data.get())); - CLabels* result=NULL; + std::shared_ptr result=NULL; switch (get_machine_problem_type()) { @@ -143,36 +142,36 @@ CLabels* CMachine::apply(CFeatures* data) } SG_TRACE("leaving {}::apply({} at {})", - get_name(), data ? data->get_name() : "NULL", fmt::ptr(data)); + get_name(), data ? data->get_name() : "NULL", fmt::ptr(data.get())); return result; } -CBinaryLabels* CMachine::apply_binary(CFeatures* data) +std::shared_ptr Machine::apply_binary(std::shared_ptr data) { error("This machine does not support apply_binary()"); return NULL; } -CRegressionLabels* CMachine::apply_regression(CFeatures* data) +std::shared_ptr Machine::apply_regression(std::shared_ptr data) { error("This machine does not support apply_regression()"); return NULL; } -CMulticlassLabels* CMachine::apply_multiclass(CFeatures* data) +std::shared_ptr Machine::apply_multiclass(std::shared_ptr data) { error("This machine does not support apply_multiclass()"); return NULL; } -CStructuredLabels* CMachine::apply_structured(CFeatures* data) +std::shared_ptr Machine::apply_structured(std::shared_ptr data) { error("This machine does not support apply_structured()"); return NULL; } -CLatentLabels* CMachine::apply_latent(CFeatures* data) +std::shared_ptr Machine::apply_latent(std::shared_ptr data) { error("This machine does not support apply_latent()"); return NULL; diff --git a/src/shogun/machine/Machine.h b/src/shogun/machine/Machine.h index 3b8dec545ad..ad9d7c5cc0d 100644 --- a/src/shogun/machine/Machine.h +++ b/src/shogun/machine/Machine.h @@ -25,8 +25,8 @@ namespace shogun { -class CFeatures; -class CLabels; +class Features; +class Labels; /** classifier type */ enum EMachineType @@ -124,7 +124,7 @@ enum EProblemType /** @brief A generic learning machine interface. * - * A machine takes as input CFeatures and CLabels (by default). + * A machine takes as input Features and Labels (by default). * Later subclasses may specialize the machine to e.g. require labels * and a kernel or labels and (real-valued) features. * @@ -132,16 +132,16 @@ enum EProblemType * the functions apply(idx) (optionally apply() to predict on the * whole set of examples) and the load and save routines. */ -class CMachine : public CStoppableSGObject +class Machine : public StoppableSGObject { - friend class CPipeline; + friend class Pipeline; public: /** constructor */ - CMachine(); + Machine(); /** destructor */ - virtual ~CMachine(); + virtual ~Machine(); /** train machine * @@ -152,7 +152,7 @@ class CMachine : public CStoppableSGObject * * @return whether training was successful */ - virtual bool train(CFeatures* data=NULL); + virtual bool train(std::shared_ptr data=NULL); /** apply machine to data * if data is not specified apply to the current features @@ -160,30 +160,30 @@ class CMachine : public CStoppableSGObject * @param data (test)data to be classified * @return classified labels */ - virtual CLabels* apply(CFeatures* data=NULL); + virtual std::shared_ptr apply(std::shared_ptr data=NULL); /** apply machine to data in means of binary classification problem */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /** apply machine to data in means of regression problem */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); /** apply machine to data in means of multiclass classification problem */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /** apply machine to data in means of SO classification problem */ - virtual CStructuredLabels* apply_structured(CFeatures* data=NULL); + virtual std::shared_ptr apply_structured(std::shared_ptr data=NULL); /** apply machine to data in means of latent problem */ - virtual CLatentLabels* apply_latent(CFeatures* data=NULL); + virtual std::shared_ptr apply_latent(std::shared_ptr data=NULL); /** set labels * * @param lab labels */ - virtual void set_labels(CLabels* lab); + virtual void set_labels(std::shared_ptr lab); /** get labels * * @return labels */ - virtual CLabels* get_labels(); + virtual std::shared_ptr get_labels(); /** set maximum training time * @@ -248,20 +248,20 @@ class CMachine : public CStoppableSGObject * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL) + virtual bool train_machine(std::shared_ptr data=NULL) { error( "train_machine is not yet implemented for {}!", get_name()); return false; } - virtual bool train_dense(CFeatures* data) + virtual bool train_dense(std::shared_ptr data) { not_implemented(SOURCE_LOCATION); return false; } - virtual bool train_string(CFeatures* data) + virtual bool train_string(std::shared_ptr data) { not_implemented(SOURCE_LOCATION); return false; @@ -285,7 +285,7 @@ class CMachine : public CStoppableSGObject /** Continue Training * * This method can be used to continue a prematurely stopped - * call to CMachine::train. + * call to Machine::train. * This is available for Iterative models and throws an error * if the feature is not supported. * @@ -303,7 +303,7 @@ class CMachine : public CStoppableSGObject * * @param lab the labels being checked, guaranteed to be non-NULL */ - virtual bool is_label_valid(CLabels *lab) const + virtual bool is_label_valid(std::shared_ptrlab) const { return true; } @@ -313,7 +313,7 @@ class CMachine : public CStoppableSGObject float64_t m_max_train_time; /** labels */ - CLabels* m_labels; + std::shared_ptr m_labels; /** solver type */ ESolverType m_solver_type; diff --git a/src/shogun/machine/MulticlassMachine.cpp b/src/shogun/machine/MulticlassMachine.cpp index 5b06dedb020..a2c8f5ce226 100644 --- a/src/shogun/machine/MulticlassMachine.cpp +++ b/src/shogun/machine/MulticlassMachine.cpp @@ -16,78 +16,70 @@ using namespace shogun; -CMulticlassMachine::CMulticlassMachine() -: CBaseMulticlassMachine(), m_multiclass_strategy(new CMulticlassOneVsRestStrategy()), +MulticlassMachine::MulticlassMachine() +: BaseMulticlassMachine(), m_multiclass_strategy(std::make_shared()), m_machine(NULL) { - SG_REF(m_multiclass_strategy); + register_parameters(); } -CMulticlassMachine::CMulticlassMachine( - CMulticlassStrategy *strategy, - CMachine* machine, CLabels* labs) -: CBaseMulticlassMachine(), m_multiclass_strategy(strategy) +MulticlassMachine::MulticlassMachine( + std::shared_ptrstrategy, + std::shared_ptr machine, std::shared_ptr labs) +: BaseMulticlassMachine(), m_multiclass_strategy(strategy) { - SG_REF(strategy); + set_labels(labs); - SG_REF(machine); + m_machine = machine; register_parameters(); } -CMulticlassMachine::~CMulticlassMachine() +MulticlassMachine::~MulticlassMachine() { - SG_UNREF(m_multiclass_strategy); - SG_UNREF(m_machine); + + } -void CMulticlassMachine::set_labels(CLabels* lab) +void MulticlassMachine::set_labels(std::shared_ptr lab) { - CMachine::set_labels(lab); + Machine::set_labels(lab); } -void CMulticlassMachine::register_parameters() +void MulticlassMachine::register_parameters() { SG_ADD(&m_multiclass_strategy,"multiclass_strategy", "Multiclass strategy"); SG_ADD(&m_machine, "machine", "The base machine"); } -void CMulticlassMachine::init_strategy() +void MulticlassMachine::init_strategy() { - int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes(); + int32_t num_classes = m_labels->as()->get_num_classes(); m_multiclass_strategy->set_num_classes(num_classes); } -CBinaryLabels* CMulticlassMachine::get_submachine_outputs(int32_t i) +std::shared_ptr MulticlassMachine::get_submachine_outputs(int32_t i) { - CMachine *machine = (CMachine*)m_machines->get_element(i); + auto machine = m_machines.at(i); ASSERT(machine) - CBinaryLabels* output = machine->apply_binary(); - SG_UNREF(machine); - return output; + return machine->apply_binary(); } -float64_t CMulticlassMachine::get_submachine_output(int32_t i, int32_t num) +float64_t MulticlassMachine::get_submachine_output(int32_t i, int32_t num) { - CMachine *machine = get_machine(i); + auto machine = get_machine(i); float64_t output = 0.0; - // dirty hack - if (dynamic_cast(machine)) - output = ((CLinearMachine*)machine)->apply_one(num); - if (dynamic_cast(machine)) - output = ((CKernelMachine*)machine)->apply_one(num); - SG_UNREF(machine); - return output; + return machine->apply_one(num); } -CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data) +std::shared_ptr MulticlassMachine::apply_multiclass(std::shared_ptr data) { SG_TRACE("entering {}::apply_multiclass({} at {})", - get_name(), data ? data->get_name() : "NULL", fmt::ptr(data)); + get_name(), data ? data->get_name() : "NULL", fmt::ptr(data.get())); - CMulticlassLabels* return_labels=NULL; + std::shared_ptr return_labels=NULL; if (data) init_machines_for_apply(data); @@ -100,11 +92,11 @@ CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data) int32_t num_vectors=data ? data->get_num_vectors() : get_num_rhs_vectors(); - int32_t num_machines=m_machines->get_num_elements(); + int32_t num_machines=m_machines.size(); if (num_machines <= 0) error("num_machines = {}, did you train your machine?", num_machines); - CMulticlassLabels* result=new CMulticlassLabels(num_vectors); + auto result=std::make_shared(num_vectors); // if outputs are prob, only one confidence for each class int32_t num_classes=m_multiclass_strategy->get_num_classes(); @@ -115,17 +107,17 @@ CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data) else result->allocate_confidences_for(num_machines); - CBinaryLabels** outputs=SG_MALLOC(CBinaryLabels*, num_machines); + std::vector> outputs(num_machines); SGVector As(num_machines); SGVector Bs(num_machines); for (int32_t i=0; iget_values()); + Statistics::SigmoidParamters params = Statistics::fit_sigmoid(outputs[i]->get_values()); As[i] = params.a; Bs[i] = params.b; } @@ -167,11 +159,7 @@ CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data) result->set_label(i, m_multiclass_strategy->decide_label(r_output_for_i)); result->set_multiclass_confidences(i, r_output_for_i); } - - for (int32_t i=0; i < num_machines; ++i) - SG_UNREF(outputs[i]); - - SG_FREE(outputs); + outputs.clear(); return_labels=result; } @@ -180,13 +168,13 @@ CMulticlassLabels* CMulticlassMachine::apply_multiclass(CFeatures* data) SG_TRACE("leaving {}::apply_multiclass({} at {})", - get_name(), data ? data->get_name() : "NULL", fmt::ptr(data)); + get_name(), data ? data->get_name() : "NULL", fmt::ptr(data.get())); return return_labels; } -CMultilabelLabels* CMulticlassMachine::apply_multilabel_output(CFeatures* data, int32_t n_outputs) +std::shared_ptr MulticlassMachine::apply_multilabel_output(std::shared_ptr data, int32_t n_outputs) { - CMultilabelLabels* return_labels=NULL; + std::shared_ptr return_labels=NULL; if (data) init_machines_for_apply(data); @@ -199,16 +187,16 @@ CMultilabelLabels* CMulticlassMachine::apply_multilabel_output(CFeatures* data, int32_t num_vectors=data ? data->get_num_vectors() : get_num_rhs_vectors(); - int32_t num_machines=m_machines->get_num_elements(); + int32_t num_machines=m_machines.size(); if (num_machines <= 0) error("num_machines = {}, did you train your machine?", num_machines); require(n_outputs<=num_machines,"You request more outputs than machines available"); - CMultilabelLabels* result=new CMultilabelLabels(num_vectors, n_outputs); - CBinaryLabels** outputs=SG_MALLOC(CBinaryLabels*, num_machines); + auto result=std::make_shared(num_vectors, n_outputs); + std::vector> outputs(num_machines); for (int32_t i=0; i < num_machines; ++i) - outputs[i] = (CBinaryLabels*) get_submachine_outputs(i); + outputs[i] = get_submachine_outputs(i); SGVector output_for_i(num_machines); for (int32_t i=0; iset_label(i, m_multiclass_strategy->decide_label_multiple_output(output_for_i, n_outputs)); } - for (int32_t i=0; i < num_machines; ++i) - SG_UNREF(outputs[i]); - - SG_FREE(outputs); + outputs[i].reset(); return_labels=result; } @@ -232,7 +217,7 @@ CMultilabelLabels* CMulticlassMachine::apply_multilabel_output(CFeatures* data, return return_labels; } -bool CMulticlassMachine::train_machine(CFeatures* data) +bool MulticlassMachine::train_machine(std::shared_ptr data) { ASSERT(m_multiclass_strategy) init_strategy(); @@ -242,9 +227,9 @@ bool CMulticlassMachine::train_machine(CFeatures* data) else init_machine_for_train(data); - m_machines->reset_array(); - CBinaryLabels* train_labels = new CBinaryLabels(get_num_rhs_vectors()); - SG_REF(train_labels); + m_machines.clear(); + auto train_labels = std::make_shared(get_num_rhs_vectors()); + m_machine->set_labels(train_labels); m_multiclass_strategy->train_start( @@ -259,7 +244,7 @@ bool CMulticlassMachine::train_machine(CFeatures* data) } m_machine->train(); - m_machines->push_back(get_machine_from_trained(m_machine)); + m_machines.push_back(get_machine_from_trained(m_machine)); if (subset.vlen) { @@ -269,19 +254,19 @@ bool CMulticlassMachine::train_machine(CFeatures* data) } m_multiclass_strategy->train_stop(); - SG_UNREF(train_labels); + return true; } -float64_t CMulticlassMachine::apply_one(int32_t vec_idx) +float64_t MulticlassMachine::apply_one(int32_t vec_idx) { init_machines_for_apply(NULL); - ASSERT(m_machines->get_num_elements()>0) - SGVector outputs(m_machines->get_num_elements()); + ASSERT(m_machines.size()>0) + SGVector outputs(m_machines.size()); - for (int32_t i=0; iget_num_elements(); i++) + for (int32_t i=0; idecide_label(outputs); diff --git a/src/shogun/machine/MulticlassMachine.h b/src/shogun/machine/MulticlassMachine.h index 4343fa124d7..a10686552be 100644 --- a/src/shogun/machine/MulticlassMachine.h +++ b/src/shogun/machine/MulticlassMachine.h @@ -15,36 +15,38 @@ #include #include +#include + namespace shogun { -class CFeatures; -class CLabels; -class CMulticlassLabels; -class CMultilabelLabels; +class Features; +class Labels; +class MulticlassLabels; +class MultilabelLabels; /** @brief experimental abstract generic multiclass machine class */ -class CMulticlassMachine : public CBaseMulticlassMachine +class MulticlassMachine : public BaseMulticlassMachine { public: /** default constructor */ - CMulticlassMachine(); + MulticlassMachine(); /** standard constructor * @param strategy multiclass strategy * @param machine machine * @param labels labels */ - CMulticlassMachine(CMulticlassStrategy* strategy, CMachine* machine, CLabels* labels); + MulticlassMachine(std::shared_ptr strategy, std::shared_ptr machine, std::shared_ptr labels); /** destructor */ - virtual ~CMulticlassMachine(); + virtual ~MulticlassMachine(); /** set labels * * @param lab labels */ - virtual void set_labels(CLabels* lab); + virtual void set_labels(std::shared_ptr lab); /** set machine * @@ -52,13 +54,13 @@ class CMulticlassMachine : public CBaseMulticlassMachine * @param machine machine to set * @return if setting was successful */ - inline bool set_machine(int32_t num, CMachine* machine) + inline bool set_machine(int32_t num, std::shared_ptr machine) { - ASSERT(numget_num_elements() && num>=0) + ASSERT(num(m_machines.size()) && num>=0) if (machine != NULL && !is_acceptable_machine(machine)) error("Machine {} is not acceptable by {}", machine->get_name(), this->get_name()); - m_machines->set_element(machine, num); + m_machines.insert(m_machines.begin()+num, machine); return true; } @@ -67,16 +69,16 @@ class CMulticlassMachine : public CBaseMulticlassMachine * @param num index of machine to get * @return SVM at number num */ - inline CMachine* get_machine(int32_t num) const + inline std::shared_ptr get_machine(int32_t num) const { - return (CMachine*) m_machines->get_element_safe(num); + return m_machines.at(num); } /** get outputs of i-th submachine * @param i number of submachine * @return outputs */ - virtual CBinaryLabels* get_submachine_outputs(int32_t i); + virtual std::shared_ptr get_submachine_outputs(int32_t i); /** get output of i-th submachine for num-th vector * @param i number of submachine @@ -89,13 +91,13 @@ class CMulticlassMachine : public CBaseMulticlassMachine * * @return resulting labels */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /** classify all examples with multiple output * * @return resulting labels */ - virtual CMultilabelLabels* apply_multilabel_output(CFeatures* data=NULL, int32_t n_outputs=5); + virtual std::shared_ptr apply_multilabel_output(std::shared_ptr data=NULL, int32_t n_outputs=5); /** classify one example * @param vec_idx @@ -107,9 +109,9 @@ class CMulticlassMachine : public CBaseMulticlassMachine * * @return multiclass type one vs one etc */ - inline CMulticlassStrategy* get_multiclass_strategy() const + inline std::shared_ptr get_multiclass_strategy() const { - SG_REF(m_multiclass_strategy); + return m_multiclass_strategy; } @@ -117,7 +119,7 @@ class CMulticlassMachine : public CBaseMulticlassMachine * * @return rejection strategy */ - inline CRejectionStrategy* get_rejection_strategy() const + inline std::shared_ptr get_rejection_strategy() const { return m_multiclass_strategy->get_rejection_strategy(); } @@ -126,7 +128,7 @@ class CMulticlassMachine : public CBaseMulticlassMachine * * @param rejection_strategy rejection strategy to be set */ - inline void set_rejection_strategy(CRejectionStrategy* rejection_strategy) + inline void set_rejection_strategy(std::shared_ptr rejection_strategy) { m_multiclass_strategy->set_rejection_strategy(rejection_strategy); } @@ -159,19 +161,19 @@ class CMulticlassMachine : public CBaseMulticlassMachine void clear_machines(); /** train machine */ - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data = NULL); /** abstract init machine for training method */ - virtual bool init_machine_for_train(CFeatures* data) = 0; + virtual bool init_machine_for_train(std::shared_ptr data) = 0; /** abstract init machines for applying method */ - virtual bool init_machines_for_apply(CFeatures* data) = 0; + virtual bool init_machines_for_apply(std::shared_ptr data) = 0; /** check whether machine is ready */ virtual bool is_ready() = 0; /** obtain machine from trained one */ - virtual CMachine* get_machine_from_trained(CMachine* machine) const = 0; + virtual std::shared_ptr get_machine_from_trained(std::shared_ptr machine) const = 0; /** get num rhs vectors */ virtual int32_t get_num_rhs_vectors() const = 0; @@ -186,7 +188,7 @@ class CMulticlassMachine : public CBaseMulticlassMachine virtual void remove_machine_subset() = 0; /** whether the machine is acceptable in set_machine */ - virtual bool is_acceptable_machine(CMachine *machine) + virtual bool is_acceptable_machine(std::shared_ptrmachine) { return true; } @@ -198,10 +200,10 @@ class CMulticlassMachine : public CBaseMulticlassMachine protected: /** type of multiclass strategy */ - CMulticlassStrategy *m_multiclass_strategy; + std::shared_ptrm_multiclass_strategy; /** machine */ - CMachine* m_machine; + std::shared_ptr m_machine; }; } #endif diff --git a/src/shogun/machine/NativeMulticlassMachine.h b/src/shogun/machine/NativeMulticlassMachine.h index 2c42a99833a..7f03e999d15 100644 --- a/src/shogun/machine/NativeMulticlassMachine.h +++ b/src/shogun/machine/NativeMulticlassMachine.h @@ -15,16 +15,16 @@ namespace shogun { /** @brief experimental abstract native multiclass machine class */ -class CNativeMulticlassMachine : public CMulticlassMachine +class NativeMulticlassMachine : public MulticlassMachine { public: /** default constructor */ - CNativeMulticlassMachine() + NativeMulticlassMachine() { } /** destructor */ - virtual ~CNativeMulticlassMachine() + virtual ~NativeMulticlassMachine() { } @@ -42,16 +42,16 @@ class CNativeMulticlassMachine : public CMulticlassMachine void clear_machines() { } /** abstract init machine for training method */ - virtual bool init_machine_for_train(CFeatures* data) { return true; } + virtual bool init_machine_for_train(std::shared_ptr data) { return true; } /** abstract init machines for applying method */ - virtual bool init_machines_for_apply(CFeatures* data) { return true; } + virtual bool init_machines_for_apply(std::shared_ptr data) { return true; } /** check whether machine is ready */ virtual bool is_ready() { return true; } /** obtain machine from trained one */ - virtual CMachine* get_machine_from_trained(CMachine* machine) const { return NULL; } + virtual std::shared_ptr get_machine_from_trained(std::shared_ptr machine) const { return NULL; } /** get num rhs vectors */ virtual int32_t get_num_rhs_vectors() const { return 0; } @@ -66,7 +66,7 @@ class CNativeMulticlassMachine : public CMulticlassMachine virtual void remove_machine_subset() { } /** whether the machine is acceptable in set_machine */ - virtual bool is_acceptable_machine(CMachine *machine) { return true; } + virtual bool is_acceptable_machine(std::shared_ptrmachine) { return true; } }; } diff --git a/src/shogun/machine/OnlineLinearMachine.cpp b/src/shogun/machine/OnlineLinearMachine.cpp index 7f5f5493443..bf835f76a4c 100644 --- a/src/shogun/machine/OnlineLinearMachine.cpp +++ b/src/shogun/machine/OnlineLinearMachine.cpp @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Shashwat Lal Das, Sergey Lisitsyn, Thoralf Klein, - * Evgeniy Andreev, Chiyuan Zhang, Viktor Gal, Evan Shelhamer, + * Authors: Soeren Sonnenburg, Shashwat Lal Das, Sergey Lisitsyn, Thoralf Klein, + * Evgeniy Andreev, Chiyuan Zhang, Viktor Gal, Evan Shelhamer, * Sanuj Sharma */ @@ -16,40 +16,40 @@ using namespace shogun; -COnlineLinearMachine::COnlineLinearMachine() -: CMachine(), bias(0), features(NULL) +OnlineLinearMachine::OnlineLinearMachine() +: Machine(), bias(0), features(NULL) { SG_ADD(&m_w, "m_w", "Parameter vector w.", ParameterProperties::MODEL); SG_ADD(&bias, "bias", "Bias b.", ParameterProperties::MODEL); - SG_ADD((CSGObject**) &features, "features", + SG_ADD((std::shared_ptr*) &features, "features", "Feature object."); } -COnlineLinearMachine::~COnlineLinearMachine() +OnlineLinearMachine::~OnlineLinearMachine() { - SG_UNREF(features); + } -CBinaryLabels* COnlineLinearMachine::apply_binary(CFeatures* data) +std::shared_ptr OnlineLinearMachine::apply_binary(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CBinaryLabels(outputs); + return std::make_shared(outputs); } -CRegressionLabels* COnlineLinearMachine::apply_regression(CFeatures* data) +std::shared_ptr OnlineLinearMachine::apply_regression(std::shared_ptr data) { SGVector outputs = apply_get_outputs(data); - return new CRegressionLabels(outputs); + return std::make_shared(outputs); } -SGVector COnlineLinearMachine::apply_get_outputs(CFeatures* data) +SGVector OnlineLinearMachine::apply_get_outputs(std::shared_ptr data) { if (data) { if (!data->has_property(FP_STREAMING_DOT)) error("Specified features are not of type CStreamingDotFeatures"); - set_features((CStreamingDotFeatures*) data); + set_features(data->as()); } ASSERT(features) @@ -72,24 +72,24 @@ SGVector COnlineLinearMachine::apply_get_outputs(CFeatures* data) return labels_array; } -float32_t COnlineLinearMachine::apply_one(float32_t* vec, int32_t len) +float32_t OnlineLinearMachine::apply_one(float32_t* vec, int32_t len) { SGVector wrap(vec, len, false); return linalg::dot(wrap, m_w)+bias; } -float32_t COnlineLinearMachine::apply_to_current_example() +float32_t OnlineLinearMachine::apply_to_current_example() { return features->dense_dot(m_w.vector, m_w.vlen)+bias; } -bool COnlineLinearMachine::train_machine(CFeatures *data) +bool OnlineLinearMachine::train_machine(std::shared_ptrdata) { if (data) { if (!data->has_property(FP_STREAMING_DOT)) error("Specified features are not of type CStreamingDotFeatures"); - set_features((CStreamingDotFeatures*) data); + set_features(data->as()); } start_train(); features->start_parser(); diff --git a/src/shogun/machine/OnlineLinearMachine.h b/src/shogun/machine/OnlineLinearMachine.h index a7f5e0607e0..a3a6ea16fa0 100644 --- a/src/shogun/machine/OnlineLinearMachine.h +++ b/src/shogun/machine/OnlineLinearMachine.h @@ -18,9 +18,9 @@ namespace shogun { -class CBinaryLabels; -class CFeatures; -class CRegressionLabels; +class BinaryLabels; +class Features; +class RegressionLabels; /** @brief Class OnlineLinearMachine is a generic interface for linear * machines like classifiers which work through online algorithms. @@ -48,12 +48,12 @@ class CRegressionLabels; * \f] * * */ -class COnlineLinearMachine : public CMachine +class OnlineLinearMachine : public Machine { public: /** default constructor */ - COnlineLinearMachine(); - virtual ~COnlineLinearMachine(); + OnlineLinearMachine(); + virtual ~OnlineLinearMachine(); /** * Get w as a _new_ float64_t array @@ -124,10 +124,10 @@ class COnlineLinearMachine : public CMachine * * @param feat features to set */ - virtual void set_features(CStreamingDotFeatures* feat) + virtual void set_features(std::shared_ptr feat) { - SG_REF(feat); - SG_UNREF(features); + + features=feat; } @@ -137,7 +137,7 @@ class COnlineLinearMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); /** apply linear machine to data * for binary classification problems @@ -145,13 +145,13 @@ class COnlineLinearMachine : public CMachine * @param data (test)data to be classified * @return classified labels */ - virtual CBinaryLabels* apply_binary(CFeatures* data=NULL); + virtual std::shared_ptr apply_binary(std::shared_ptr data=NULL); /// get output for example "vec_idx" virtual float64_t apply_one(int32_t vec_idx) { not_implemented(SOURCE_LOCATION); - return CMath::INFTY; + return Math::INFTY; } /** @@ -175,7 +175,7 @@ class COnlineLinearMachine : public CMachine * * @return features */ - virtual CStreamingDotFeatures* get_features() { SG_REF(features); return features; } + virtual std::shared_ptr get_features() { return features; } /** Returns the name of the SGSerializable instance. It MUST BE * the CLASS NAME without the prefixed `C'. @@ -203,7 +203,7 @@ class COnlineLinearMachine : public CMachine * labels or the caller might want to provide some other labels. * @param label label of this example */ - virtual void train_example(CStreamingDotFeatures *feature, float64_t label) { not_implemented(SOURCE_LOCATION); } + virtual void train_example(std::shared_ptrfeature, float64_t label) { not_implemented(SOURCE_LOCATION); } /** whether train require labels */ virtual bool train_require_labels() const @@ -220,14 +220,14 @@ class COnlineLinearMachine : public CMachine * * @return Whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** get real outputs * * @param data features to compute outputs * @return outputs */ - SGVector apply_get_outputs(CFeatures* data); + SGVector apply_get_outputs(std::shared_ptr data); protected: /** w */ @@ -235,7 +235,7 @@ class COnlineLinearMachine : public CMachine /** bias */ float32_t bias; /** features */ - CStreamingDotFeatures* features; + std::shared_ptr features; }; } #endif diff --git a/src/shogun/machine/Pipeline.cpp b/src/shogun/machine/Pipeline.cpp index 9e71bc24b45..9992813bcda 100644 --- a/src/shogun/machine/Pipeline.cpp +++ b/src/shogun/machine/Pipeline.cpp @@ -13,136 +13,127 @@ namespace shogun { - CPipelineBuilder::~CPipelineBuilder() + PipelineBuilder::~PipelineBuilder() { - for (auto&& stage : m_stages) - { - visit([](auto&& object) { SG_UNREF(object) }, stage.second); - } } - CPipelineBuilder* CPipelineBuilder::over(CTransformer* transformer) + std::shared_ptr PipelineBuilder::over(std::shared_ptr transformer) { return over(transformer->get_name(), transformer); } - CPipelineBuilder* - CPipelineBuilder::over(const std::string& name, CTransformer* transformer) + std::shared_ptr + PipelineBuilder::over(const std::string& name, std::shared_ptr transformer) { require( m_stages.empty() || - holds_alternative(m_stages.back().second), + holds_alternative>(m_stages.back().second), "Transformers can not be placed after machines. Last element is " "{}", m_stages.back().first); - SG_REF(transformer); + m_stages.emplace_back(name, transformer); - return this; + return shared_from_this()->as(); } - CPipeline* CPipelineBuilder::then(CMachine* machine) + std::shared_ptr PipelineBuilder::then(std::shared_ptr machine) { return then(machine->get_name(), machine); } - CPipeline* - CPipelineBuilder::then(const std::string& name, CMachine* machine) + std::shared_ptr + PipelineBuilder::then(const std::string& name, std::shared_ptr machine) { require( m_stages.empty() || - holds_alternative(m_stages.back().second), + holds_alternative>(m_stages.back().second), "Multiple machines are added to pipeline. Last element is {}", m_stages.back().first); - SG_REF(machine); m_stages.emplace_back(name, machine); return build(); } - CPipelineBuilder* - CPipelineBuilder::add_stages(std::vector stages) + std::shared_ptr + PipelineBuilder::add_stages(std::vector> stages) { for (auto stage : stages) { - auto transformer = dynamic_cast(stage); + auto transformer = stage->as(); if (transformer) { over(transformer); } else { - auto machine = dynamic_cast(stage); + auto machine = stage->as(); require( machine, "Stage must be either a " "transformer or a machine. " "Provided {}", stage->get_name()); - SG_REF(machine); + m_stages.emplace_back(machine->get_name(), machine); } } - return this; + return shared_from_this()->as(); } - CPipeline* CPipelineBuilder::build() + std::shared_ptr PipelineBuilder::build() { check_pipeline(); - auto pipeline = new CPipeline(); + auto pipeline = std::make_shared(); pipeline->m_stages = std::move(m_stages); m_stages.clear(); return pipeline; } - void CPipelineBuilder::check_pipeline() const + void PipelineBuilder::check_pipeline() const { require( !m_stages.empty(), "Pipeline is empty"); require( - holds_alternative(m_stages.back().second), + holds_alternative>(m_stages.back().second), "Pipline cannot be trained without an " "added machine. Last element " "is {}.", m_stages.back().first); } - CPipeline::CPipeline() : CMachine() + Pipeline::Pipeline() : Machine() { } - CPipeline::~CPipeline() + Pipeline::~Pipeline() { - for (auto&& stage : m_stages) - { - visit([](auto&& object) { SG_UNREF(object) }, stage.second); - } } - bool CPipeline::train_machine(CFeatures* data) + bool Pipeline::train_machine(std::shared_ptr data) { if (train_require_labels()) { require(m_labels, "No labels given."); } - auto current_data = wrap(data); + auto current_data = data; for (auto&& stage : m_stages) { - if (holds_alternative(stage.second)) + if (holds_alternative>(stage.second)) { - auto transformer = shogun::get(stage.second); + auto transformer = shogun::get>(stage.second); transformer->train_require_labels() ? transformer->fit(current_data, m_labels) : transformer->fit(current_data); - current_data = wrap(transformer->transform(current_data)); + current_data = transformer->transform(current_data); } else { - auto machine = shogun::get(stage.second); + auto machine = shogun::get>(stage.second); if (machine->train_require_labels()) machine->set_labels(m_labels); machine->train(current_data); @@ -151,19 +142,19 @@ namespace shogun return true; } - CLabels* CPipeline::apply(CFeatures* data) + std::shared_ptr Pipeline::apply(std::shared_ptr data) { - auto current_data = wrap(data); + auto current_data = data; for (auto&& stage : m_stages) { - if (holds_alternative(stage.second)) + if (holds_alternative>(stage.second)) { - auto transformer = shogun::get(stage.second); - current_data = wrap(transformer->transform(current_data)); + auto transformer = shogun::get>(stage.second); + current_data = transformer->transform(current_data); } else { - auto machine = shogun::get(stage.second); + auto machine = shogun::get>(stage.second); return machine->apply(current_data); } } @@ -171,7 +162,7 @@ namespace shogun return nullptr; // unreachable } - bool CPipeline::train_require_labels() const + bool Pipeline::train_require_labels() const { bool require_labels = false; @@ -190,7 +181,7 @@ namespace shogun return require_labels; } - std::string CPipeline::to_string() const + std::string Pipeline::to_string() const { std::stringstream ss; @@ -202,13 +193,13 @@ namespace shogun return ss.str(); } - CTransformer* CPipeline::get_transformer(const std::string& name) const + std::shared_ptr Pipeline::get_transformer(const std::string& name) const { for (auto&& stage : m_stages) { if (stage.first == name && - holds_alternative(stage.second)) - return shogun::get(stage.second); + holds_alternative>(stage.second)) + return shogun::get>(stage.second); } error( @@ -218,14 +209,14 @@ namespace shogun return nullptr; } - CMachine* CPipeline::get_machine() const + std::shared_ptr Pipeline::get_machine() const { - return shogun::get(m_stages.back().second); + return shogun::get>(m_stages.back().second); } - CSGObject* CPipeline::clone(ParameterProperties pp) const + std::shared_ptr Pipeline::clone(ParameterProperties pp) const { - auto result = CMachine::clone()->as(); + auto result = Machine::clone()->as(); for (auto&& stage : m_stages) { visit( @@ -233,15 +224,15 @@ namespace shogun result->m_stages.emplace_back( stage.first, object->clone(pp) - ->template as::type>()); + ->template as>() + ); }, stage.second); } return result; } - EProblemType CPipeline::get_machine_problem_type() const + EProblemType Pipeline::get_machine_problem_type() const { return get_machine()->get_machine_problem_type(); } diff --git a/src/shogun/machine/Pipeline.h b/src/shogun/machine/Pipeline.h index facc82053a1..6f70e6cf04f 100644 --- a/src/shogun/machine/Pipeline.h +++ b/src/shogun/machine/Pipeline.h @@ -14,28 +14,28 @@ namespace shogun { - class CPipeline; + class Pipeline; /** @brief Builder of pipeline. */ - class CPipelineBuilder : public CSGObject + class PipelineBuilder : public SGObject { public: - virtual ~CPipelineBuilder(); + virtual ~PipelineBuilder(); /** Add a transformer with default name to pipeline. The name is * obtained by transformer->get_name(). * @param transformer the transformer * @return the current pipeline builder */ - CPipelineBuilder* over(CTransformer* transformer); + std::shared_ptr over(std::shared_ptr transformer); /** Add a transformer with given name to pipeline * @param name the name of the transformer * @param transformer the transformer * @return the current pipeline builder */ - CPipelineBuilder* - over(const std::string& name, CTransformer* transformer); + std::shared_ptr + over(const std::string& name, std::shared_ptr transformer); /** Add a machine with default name to pipeline. Pipeline may have only * one machine. build() will be called to create a new pipeline @@ -43,7 +43,7 @@ namespace shogun * @param machine the machine * @return the current pipeline */ - CPipeline* then(CMachine* machine); + std::shared_ptr then(std::shared_ptr machine); /** Add a machine with given name to pipeline. Pipeline may have only * one machine. build() will be called to create a new pipeline @@ -52,20 +52,20 @@ namespace shogun * @param machine the machine * @return the current pipeline */ - CPipeline* then(const std::string& name, CMachine* machine); + std::shared_ptr then(const std::string& name, std::shared_ptr machine); /** Add a list of stages to pipeline. Stages in the list must be * transformers or machines. * @param stages vector of stages to add * @return the current pipeline */ - CPipelineBuilder* add_stages(std::vector stages); + std::shared_ptr add_stages(std::vector> stages); /* Build pipeline. A new pipeline instance will be created, current * pipeline builder will become invalid after calling this method. * @return the new pipeline instance */ - CPipeline* build(); + std::shared_ptr build(); virtual const char* get_name() const override { @@ -76,7 +76,7 @@ namespace shogun /** Check pipeline is not empty and machine has been added. */ void check_pipeline() const; - std::vector>> + std::vector, std::shared_ptr>>> m_stages; }; @@ -85,15 +85,15 @@ namespace shogun * or testing and a machine as the final stage. Features are transformed by * transformers and fed into the next stage sequentially. */ - class CPipeline : public CMachine + class Pipeline : public Machine { - friend class CPipelineBuilder; + friend class PipelineBuilder; public: - CPipeline(); - virtual ~CPipeline(); + Pipeline(); + virtual ~Pipeline(); - virtual CLabels* apply(CFeatures* data = NULL) override; + virtual std::shared_ptr apply(std::shared_ptr data = NULL) override; virtual const char* get_name() const override { @@ -107,21 +107,21 @@ namespace shogun * @param name name of the transformer * @return the index-th transformer */ - CTransformer* get_transformer(const std::string& name) const; + std::shared_ptr get_transformer(const std::string& name) const; /** Get machine in the pipeline * @return the machine */ - CMachine* get_machine() const; + std::shared_ptr get_machine() const; - virtual CSGObject* clone(ParameterProperties pp = ParameterProperties::ALL) const override; + virtual std::shared_ptr clone(ParameterProperties pp = ParameterProperties::ALL) const override; virtual EProblemType get_machine_problem_type() const override; protected: - virtual bool train_machine(CFeatures* data = NULL) override; + virtual bool train_machine(std::shared_ptr data = NULL) override; - std::vector>> + std::vector, std::shared_ptr>>> m_stages; virtual bool train_require_labels() const override; }; diff --git a/src/shogun/machine/RandomForest.cpp b/src/shogun/machine/RandomForest.cpp index 24adbe33853..0e60d1626c8 100644 --- a/src/shogun/machine/RandomForest.cpp +++ b/src/shogun/machine/RandomForest.cpp @@ -33,14 +33,14 @@ using namespace shogun; -CRandomForest::CRandomForest() -: CBaggingMachine() +RandomForest::RandomForest() +: BaggingMachine() { init(); } -CRandomForest::CRandomForest(int32_t rand_numfeats, int32_t num_bags) -: CBaggingMachine() +RandomForest::RandomForest(int32_t rand_numfeats, int32_t num_bags) +: BaggingMachine() { init(); @@ -48,30 +48,30 @@ CRandomForest::CRandomForest(int32_t rand_numfeats, int32_t num_bags) set_num_bags(num_bags); if (rand_numfeats>0) - dynamic_cast(m_machine)->set_feature_subset_size(rand_numfeats); + m_machine->as()->set_feature_subset_size(rand_numfeats); } -CRandomForest::CRandomForest(CFeatures* features, CLabels* labels, int32_t num_bags, int32_t rand_numfeats) -: CBaggingMachine() +RandomForest::RandomForest(std::shared_ptr features, std::shared_ptr labels, int32_t num_bags, int32_t rand_numfeats) +: BaggingMachine() { init(); - SG_REF(features); + m_features=features; set_labels(labels); set_num_bags(num_bags); if (rand_numfeats>0) - dynamic_cast(m_machine)->set_feature_subset_size(rand_numfeats); + m_machine->as()->set_feature_subset_size(rand_numfeats); } -CRandomForest::CRandomForest(CFeatures* features, CLabels* labels, SGVector weights, int32_t num_bags, int32_t rand_numfeats) -: CBaggingMachine() +RandomForest::RandomForest(std::shared_ptr features, std::shared_ptr labels, SGVector weights, int32_t num_bags, int32_t rand_numfeats) +: BaggingMachine() { init(); - SG_REF(features); + m_features=features; set_labels(labels); m_weights=weights; @@ -79,72 +79,72 @@ CRandomForest::CRandomForest(CFeatures* features, CLabels* labels, SGVector0) - dynamic_cast(m_machine)->set_feature_subset_size(rand_numfeats); + m_machine->as()->set_feature_subset_size(rand_numfeats); } -CRandomForest::~CRandomForest() +RandomForest::~RandomForest() { } -void CRandomForest::set_machine(CMachine* machine) +void RandomForest::set_machine(std::shared_ptr machine) { error("Machine is set as CRandomCART and cannot be changed"); } -void CRandomForest::set_weights(SGVector weights) +void RandomForest::set_weights(SGVector weights) { m_weights=weights; } -SGVector CRandomForest::get_weights() const +SGVector RandomForest::get_weights() const { return m_weights; } -void CRandomForest::set_feature_types(SGVector ft) +void RandomForest::set_feature_types(SGVector ft) { require(m_machine,"m_machine is NULL. It is expected to be RandomCARTree"); - dynamic_cast(m_machine)->set_feature_types(ft); + m_machine->as()->set_feature_types(ft); } -SGVector CRandomForest::get_feature_types() const +SGVector RandomForest::get_feature_types() const { require(m_machine,"m_machine is NULL. It is expected to be RandomCARTree"); - return dynamic_cast(m_machine)->get_feature_types(); + return m_machine->as()->get_feature_types(); } -EProblemType CRandomForest::get_machine_problem_type() const +EProblemType RandomForest::get_machine_problem_type() const { require(m_machine,"m_machine is NULL. It is expected to be RandomCARTree"); - return dynamic_cast(m_machine)->get_machine_problem_type(); + return m_machine->as()->get_machine_problem_type(); } -void CRandomForest::set_machine_problem_type(EProblemType mode) +void RandomForest::set_machine_problem_type(EProblemType mode) { require(m_machine,"m_machine is NULL. It is expected to be RandomCARTree"); - dynamic_cast(m_machine)->set_machine_problem_type(mode); + m_machine->as()->set_machine_problem_type(mode); } -void CRandomForest::set_num_random_features(int32_t rand_featsize) +void RandomForest::set_num_random_features(int32_t rand_featsize) { require(m_machine,"m_machine is NULL. It is expected to be RandomCARTree"); require(rand_featsize>0,"feature subset size should be greater than 0"); - dynamic_cast(m_machine)->set_feature_subset_size(rand_featsize); + m_machine->as()->set_feature_subset_size(rand_featsize); } -int32_t CRandomForest::get_num_random_features() const +int32_t RandomForest::get_num_random_features() const { require(m_machine,"m_machine is NULL. It is expected to be RandomCARTree"); - return dynamic_cast(m_machine)->get_feature_subset_size(); + return m_machine->as()->get_feature_subset_size(); } -void CRandomForest::set_machine_parameters(CMachine* m, SGVector idx) +void RandomForest::set_machine_parameters(std::shared_ptr m, SGVector idx) { require(m,"Machine supplied is NULL"); require(m_machine,"Reference Machine is NULL"); - CRandomCARTree* tree=dynamic_cast(m); + auto tree=m->as(); SGVector weights(idx.vlen); @@ -161,29 +161,28 @@ void CRandomForest::set_machine_parameters(CMachine* m, SGVector idx) tree->set_weights(weights); tree->set_sorted_features(m_sorted_transposed_feats, m_sorted_indices); // equate the machine problem types - cloning does not do this - tree->set_machine_problem_type(dynamic_cast(m_machine)->get_machine_problem_type()); + tree->set_machine_problem_type(m_machine->as()->get_machine_problem_type()); } -bool CRandomForest::train_machine(CFeatures* data) +bool RandomForest::train_machine(std::shared_ptr data) { if (data) { - SG_REF(data); - SG_UNREF(m_features); + + m_features = data; } require(m_features, "Training features not set!"); - - dynamic_cast(m_machine)->pre_sort_features(m_features, m_sorted_transposed_feats, m_sorted_indices); - return CBaggingMachine::train_machine(); + m_machine->as()->pre_sort_features(m_features, m_sorted_transposed_feats, m_sorted_indices); + + return BaggingMachine::train_machine(); } -void CRandomForest::init() +void RandomForest::init() { - m_machine=new CRandomCARTree(); - SG_REF(m_machine); + m_machine=std::make_shared(); m_weights=SGVector(); SG_ADD(&m_weights, kWeights, "weights"); diff --git a/src/shogun/machine/RandomForest.h b/src/shogun/machine/RandomForest.h index 7a8fd004379..3859bc6c279 100644 --- a/src/shogun/machine/RandomForest.h +++ b/src/shogun/machine/RandomForest.h @@ -40,21 +40,21 @@ namespace shogun /** @brief This class implements the Random Forests algorithm. In Random Forests algorithm, we train a number of randomized CART trees * (see class CRandomCARTree) using the supplied training data. The number of trees to be trained is a parameter (called number of bags) * controlled by the user. Test feature vectors are classified/regressed by combining the outputs of all these trained candidate trees using a - * combination rule (see class CCombinationRule). The feature for calculating out-of-box error is also provided to help determine the + * combination rule (see class CombinationRule). The feature for calculating out-of-box error is also provided to help determine the * appropriate number of bags. The evaluatin criteria for calculating this out-of-box error is specified by the user (see class CEvaluation). */ -class CRandomForest : public CBaggingMachine +class RandomForest : public BaggingMachine { public: /** constructor */ - CRandomForest(); + RandomForest(); /** constructor * * @param num_rand_feats number of attributes chosen randomly during node split in candidate trees * @param num_bags number of trees in forest */ - CRandomForest(int32_t num_rand_feats, int32_t num_bags=10); + RandomForest(int32_t num_rand_feats, int32_t num_bags=10); /** constructor * @@ -63,7 +63,7 @@ class CRandomForest : public CBaggingMachine * @param num_bags number of trees in forest * @param num_rand_feats number of attributes chosen randomly during node split in candidate trees */ - CRandomForest(CFeatures* features, CLabels* labels, int32_t num_bags=10, int32_t num_rand_feats=0); + RandomForest(std::shared_ptr features, std::shared_ptr labels, int32_t num_bags=10, int32_t num_rand_feats=0); /** constructor * @@ -73,10 +73,10 @@ class CRandomForest : public CBaggingMachine * @param num_bags number of trees in forest * @param num_rand_feats number of attributes chosen randomly during node split in candidate trees */ - CRandomForest(CFeatures* features, CLabels* labels, SGVector weights, int32_t num_bags=10, int32_t num_rand_feats=0); + RandomForest(std::shared_ptr features, std::shared_ptr labels, SGVector weights, int32_t num_bags=10, int32_t num_rand_feats=0); /** destructor */ - virtual ~CRandomForest(); + virtual ~RandomForest(); /** get name * @@ -88,7 +88,7 @@ class CRandomForest : public CBaggingMachine * * @param machine the machine to use for bagging */ - virtual void set_machine(CMachine* machine); + virtual void set_machine(std::shared_ptr machine); /** set weights * @@ -140,13 +140,13 @@ class CRandomForest : public CBaggingMachine protected: - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** sets parameters of CARTree - sets machine labels and weights here * * @param m machine * @param idx indices of training vectors chosen in current bag */ - virtual void set_machine_parameters(CMachine* m, SGVector idx); + virtual void set_machine_parameters(std::shared_ptr m, SGVector idx); private: /** initialize parameters */ diff --git a/src/shogun/machine/StochasticGBMachine.cpp b/src/shogun/machine/StochasticGBMachine.cpp index 67eda104e37..b747b8415f6 100644 --- a/src/shogun/machine/StochasticGBMachine.cpp +++ b/src/shogun/machine/StochasticGBMachine.cpp @@ -29,7 +29,6 @@ */ #include -#include #include #include #include @@ -37,21 +36,21 @@ using namespace shogun; -CStochasticGBMachine::CStochasticGBMachine(CMachine* machine, CLossFunction* loss, int32_t num_iterations, +StochasticGBMachine::StochasticGBMachine(std::shared_ptr machine, std::shared_ptr loss, int32_t num_iterations, float64_t learning_rate, float64_t subset_fraction) -: RandomMixin() +: RandomMixin() { init(); if (machine!=NULL) { - SG_REF(machine); + m_machine=machine; } if (loss!=NULL) { - SG_REF(loss); + m_loss=loss; } @@ -60,92 +59,84 @@ CStochasticGBMachine::CStochasticGBMachine(CMachine* machine, CLossFunction* los m_learning_rate=learning_rate; } -CStochasticGBMachine::~CStochasticGBMachine() +StochasticGBMachine::~StochasticGBMachine() { - SG_UNREF(m_machine); - SG_UNREF(m_loss); - SG_UNREF(m_weak_learners); - SG_UNREF(m_gamma); + + + + } -void CStochasticGBMachine::set_machine(CMachine* machine) +void StochasticGBMachine::set_machine(std::shared_ptr machine) { require(machine,"Supplied machine is NULL"); - if (m_machine!=NULL) - SG_UNREF(m_machine); - - SG_REF(machine); m_machine=machine; } -CMachine* CStochasticGBMachine::get_machine() const +std::shared_ptr StochasticGBMachine::get_machine() const { if (m_machine==NULL) error("machine not set yet!"); - SG_REF(m_machine); + return m_machine; } -void CStochasticGBMachine::set_loss_function(CLossFunction* f) +void StochasticGBMachine::set_loss_function(std::shared_ptr f) { require(f,"Supplied loss function is NULL"); - if (m_loss!=NULL) - SG_UNREF(m_loss); - - SG_REF(f); m_loss=f; } -CLossFunction* CStochasticGBMachine::get_loss_function() const +std::shared_ptr StochasticGBMachine::get_loss_function() const { if (m_loss==NULL) error("Loss function not set yet!"); - SG_REF(m_loss) + return m_loss; } -void CStochasticGBMachine::set_num_iterations(int32_t iter) +void StochasticGBMachine::set_num_iterations(int32_t iter) { require(iter,"Number of iterations"); m_num_iter=iter; } -int32_t CStochasticGBMachine::get_num_iterations() const +int32_t StochasticGBMachine::get_num_iterations() const { return m_num_iter; } -void CStochasticGBMachine::set_subset_fraction(float64_t frac) +void StochasticGBMachine::set_subset_fraction(float64_t frac) { require((frac>0)&&(frac<=1),"subset fraction should lie between 0 and 1. Supplied value is {}",frac); m_subset_frac=frac; } -float64_t CStochasticGBMachine::get_subset_fraction() const +float64_t StochasticGBMachine::get_subset_fraction() const { return m_subset_frac; } -void CStochasticGBMachine::set_learning_rate(float64_t lr) +void StochasticGBMachine::set_learning_rate(float64_t lr) { require((lr>0)&&(lr<=1),"learning rate should lie between 0 and 1. Supplied value is {}",lr); m_learning_rate=lr; } -float64_t CStochasticGBMachine::get_learning_rate() const +float64_t StochasticGBMachine::get_learning_rate() const { return m_learning_rate; } -CRegressionLabels* CStochasticGBMachine::apply_regression(CFeatures* data) +std::shared_ptr StochasticGBMachine::apply_regression(std::shared_ptr data) { require(data,"test data supplied is NULL"); - CDenseFeatures* feats=data->as>(); + auto feats=data->as>(); SGVector retlabs(feats->get_num_vectors()); retlabs.fill_vector(retlabs.vector,retlabs.vlen,0); @@ -153,38 +144,37 @@ CRegressionLabels* CStochasticGBMachine::apply_regression(CFeatures* data) { float64_t gamma=m_gamma->get_element(i); - CSGObject* element=m_weak_learners->get_element(i); - require(element,"{} element of the array of weak learners is NULL. This is not expected",i); - CMachine* machine=dynamic_cast(element); + auto machine=m_weak_learners->get_element(i); + require(machine, "{} element of the array of weak learners is NULL. This is not expected",i); - CRegressionLabels* dlabels=machine->apply_regression(feats); + auto dlabels=machine->apply_regression(feats); SGVector delta=dlabels->get_labels(); for (int32_t j=0;j(retlabs); } -bool CStochasticGBMachine::train_machine(CFeatures* data) +bool StochasticGBMachine::train_machine(std::shared_ptr data) { require(data,"training data not supplied!"); require(m_machine,"machine not set!"); require(m_loss,"loss function not specified"); require(m_labels, "labels not specified"); - CDenseFeatures* feats=data->as>(); + auto feats=data->as>(); // initialize weak learners array and gamma array initialize_learners(); // cache predicted labels for intermediate models - CRegressionLabels* interf=new CRegressionLabels(feats->get_num_vectors()); - SG_REF(interf); + auto interf=std::make_shared(feats->get_num_vectors()); + for (int32_t i=0;iget_num_labels();i++) interf->set_label(i,0); @@ -196,61 +186,55 @@ bool CStochasticGBMachine::train_machine(CFeatures* data) const auto& labels_iter = std::get<2>(result); // compute pseudo-residuals - CRegressionLabels* pres = + auto pres = compute_pseudo_residuals(interf_iter, labels_iter); // fit learner - CMachine* wlearner = fit_model(feats_iter, pres); + auto wlearner = fit_model(feats_iter, pres); m_weak_learners->push_back(wlearner); // compute multiplier - CRegressionLabels* hm = wlearner->apply_regression(feats_iter); - SG_REF(hm); + auto hm = wlearner->apply_regression(feats_iter); + float64_t gamma = compute_multiplier(interf_iter, hm, labels_iter); m_gamma->push_back(gamma); // update intermediate function value - CRegressionLabels* dlabels=wlearner->apply_regression(feats); + auto dlabels=wlearner->apply_regression(feats); SGVector delta=dlabels->get_labels(); for (int32_t j=0;jget_num_labels();j++) interf->set_label(j,interf->get_label(j)+delta[j]*gamma*m_learning_rate); - SG_UNREF(dlabels); - SG_UNREF(hm); - SG_UNREF(wlearner); + + + } - SG_UNREF(interf); + return true; } -float64_t CStochasticGBMachine::compute_multiplier( - CRegressionLabels* f, CRegressionLabels* hm, CLabels* labs) +float64_t StochasticGBMachine::compute_multiplier( + std::shared_ptr f, std::shared_ptr hm, std::shared_ptr labs) { require(f->get_num_labels()==hm->get_num_labels(),"The number of labels in both input parameters should be equal"); - CDynamicObjectArray* instance=new CDynamicObjectArray(); + auto instance=std::make_shared(); instance->push_back(labs); instance->push_back(f); instance->push_back(hm); instance->push_back(m_loss); - float64_t ret=get_gamma(instance); + float64_t ret=get_gamma(instance.get()); + - SG_UNREF(instance); return ret; } -CMachine* CStochasticGBMachine::fit_model(CDenseFeatures* feats, CRegressionLabels* labels) +std::shared_ptr StochasticGBMachine::fit_model(std::shared_ptr> feats, std::shared_ptr labels) { // clone base machine - CSGObject* obj=m_machine->clone(); - CMachine* c=NULL; - if (obj) - c=dynamic_cast(obj); - else - error("Machine could not be cloned!"); - + auto c=m_machine->clone()->as(); // train cloned machine c->set_labels(labels); c->train(feats); @@ -258,26 +242,26 @@ CMachine* CStochasticGBMachine::fit_model(CDenseFeatures* feats, CReg return c; } -CRegressionLabels* CStochasticGBMachine::compute_pseudo_residuals( - CRegressionLabels* inter_f, CLabels* labs) +std::shared_ptr StochasticGBMachine::compute_pseudo_residuals( + std::shared_ptr inter_f, std::shared_ptr labs) { - auto labels = labs->as()->get_labels(); + auto labels = labs->as()->get_labels(); SGVector f=inter_f->get_labels(); SGVector residuals(f.vlen); for (int32_t i=0;ifirst_derivative(f[i],labels[i]); - return new CRegressionLabels(residuals); + return std::make_shared(residuals); } -std::tuple>, Some, - Some> -CStochasticGBMachine::get_subset( - CDenseFeatures* f, CRegressionLabels* interf) +std::tuple>, std::shared_ptr, + std::shared_ptr> +StochasticGBMachine::get_subset( + std::shared_ptr> f, std::shared_ptr interf) { if (m_subset_frac == 1.0) - return std::make_tuple(wrap(f), wrap(interf), wrap(m_labels)); + return std::make_tuple(f, interf, m_labels); int32_t subset_size=m_subset_frac*(f->get_num_vectors()); SGVector idx(f->get_num_vectors()); @@ -288,53 +272,51 @@ CStochasticGBMachine::get_subset( sg_memcpy(subset.vector,idx.vector,subset.vlen*sizeof(index_t)); return std::make_tuple( - wrap(view(f, subset)), wrap(view(interf, subset)), - wrap(view(m_labels, subset))); + view(f, subset), view(interf, subset), + view(m_labels, subset)); } -void CStochasticGBMachine::initialize_learners() +void StochasticGBMachine::initialize_learners() { - SG_UNREF(m_weak_learners); - m_weak_learners=new CDynamicObjectArray(); - SG_REF(m_weak_learners); - SG_UNREF(m_gamma); - m_gamma=new CDynamicArray(); - SG_REF(m_gamma); + m_weak_learners=std::make_shared(); + + + + m_gamma=std::make_shared>(); + } -float64_t CStochasticGBMachine::get_gamma(void* instance) +float64_t StochasticGBMachine::get_gamma(void* instance) { lbfgs_parameter_t lbfgs_param; lbfgs_parameter_init(&lbfgs_param); lbfgs_param.linesearch=2; float64_t gamma=0; - lbfgs(1,&gamma,NULL,CStochasticGBMachine::lbfgs_evaluate,NULL,instance,&lbfgs_param); + lbfgs(1,&gamma,NULL,StochasticGBMachine::lbfgs_evaluate,NULL,instance,&lbfgs_param); return gamma; } -float64_t CStochasticGBMachine::lbfgs_evaluate(void *obj, const float64_t *parameters, float64_t *gradient, const int dim, +float64_t StochasticGBMachine::lbfgs_evaluate(void *obj, const float64_t *parameters, float64_t *gradient, const int dim, const float64_t step) { require(obj,"object cannot be NULL"); - CDynamicObjectArray* objects=static_cast(obj); + auto objects=(DynamicObjectArray*)obj; require((objects->get_num_elements()==2) || (objects->get_num_elements()==4),"Number of elements in obj array" " ({}) does not match expectations(2 or 4)",objects->get_num_elements()); if (objects->get_num_elements()==2) { // extract labels - CSGObject* element=objects->get_element(0); - require(element,"0 index element of objects is NULL"); - CDenseLabels* lab=dynamic_cast(element); + auto lab=objects->get_element(0); + require(lab,"0 index element of objects is NULL"); SGVector labels=lab->get_labels(); // extract loss function - element=objects->get_element(1); - require(element,"1 index element of objects is NULL"); - CLossFunction* lossf=dynamic_cast(element); + auto lossf =objects->get_element(1); + require(lossf,"1 index element of objects is NULL"); *gradient=0; float64_t ret=0; @@ -344,33 +326,29 @@ float64_t CStochasticGBMachine::lbfgs_evaluate(void *obj, const float64_t *param ret+=lossf->loss((*parameters),labels[i]); } - SG_UNREF(lab); - SG_UNREF(lossf); + + return ret; } // extract labels - CSGObject* element=objects->get_element(0); - require(element,"0 index element of objects is NULL"); - CDenseLabels* lab=dynamic_cast(element); + auto lab=objects->get_element(0); + require(lab,"0 index element of objects is NULL"); SGVector labels=lab->get_labels(); // extract f - element=objects->get_element(1); - require(element,"1 index element of objects is NULL"); - CDenseLabels* func=dynamic_cast(element); + auto func=objects->get_element(1); + require(func,"1 index element of objects is NULL"); SGVector f=func->get_labels(); // extract hm - element=objects->get_element(2); - require(element,"2 index element of objects is NULL"); - CDenseLabels* delta=dynamic_cast(element); + auto delta=objects->get_element(2); + require(delta,"2 index element of objects is NULL"); SGVector hm=delta->get_labels(); // extract loss function - element=objects->get_element(3); - require(element,"3 index element of objects is NULL"); - CLossFunction* lossf=dynamic_cast(element); + auto lossf=objects->get_element(3); + require(lossf,"3 index element of objects is NULL"); *gradient=0; float64_t ret=0; @@ -380,14 +358,14 @@ float64_t CStochasticGBMachine::lbfgs_evaluate(void *obj, const float64_t *param ret+=lossf->loss((*parameters)*hm[i]+f[i],labels[i]); } - SG_UNREF(lab); - SG_UNREF(delta); - SG_UNREF(func); - SG_UNREF(lossf) + + + + return ret; } -void CStochasticGBMachine::init() +void StochasticGBMachine::init() { m_machine=nullptr; m_loss=nullptr; @@ -395,17 +373,14 @@ void CStochasticGBMachine::init() m_subset_frac=0; m_learning_rate=0; - m_weak_learners=new CDynamicObjectArray(); - SG_REF(m_weak_learners); - - m_gamma=new CDynamicArray(); - SG_REF(m_gamma); + m_weak_learners=std::make_shared(); + m_gamma=std::make_shared>(); - SG_ADD(&m_machine, kMachine, "machine"); - SG_ADD(&m_loss, kLoss, "loss function"); - SG_ADD(&m_num_iter, kNumIterations, "number of iterations"); - SG_ADD(&m_subset_frac, kSubsetFrac, "subset fraction"); - SG_ADD(&m_learning_rate, kLearningRate, "learning rate"); - SG_ADD(&m_weak_learners, kWeakLearners, "array of weak learners"); - SG_ADD((CSGObject**)&m_gamma, kGamma, "array of learner weights"); + SG_ADD((std::shared_ptr*)&m_machine,"m_machine","machine"); + SG_ADD((std::shared_ptr*)&m_loss,"m_loss","loss function"); + SG_ADD(&m_num_iter,"m_num_iter","number of iterations"); + SG_ADD(&m_subset_frac,"m_subset_frac","subset fraction"); + SG_ADD(&m_learning_rate,"m_learning_rate","learning rate"); + SG_ADD((std::shared_ptr*)&m_weak_learners,"m_weak_learners","array of weak learners"); + SG_ADD((std::shared_ptr*)&m_gamma,"m_gamma","array of learner weights"); } diff --git a/src/shogun/machine/StochasticGBMachine.h b/src/shogun/machine/StochasticGBMachine.h index a187a5122de..0dc004c10de 100644 --- a/src/shogun/machine/StochasticGBMachine.h +++ b/src/shogun/machine/StochasticGBMachine.h @@ -34,7 +34,6 @@ #include -#include #include #include #include @@ -48,12 +47,12 @@ namespace shogun /** @brief This class implements the stochastic gradient boosting algorithm for ensemble learning invented by Jerome H. Friedman. This class * works with a variety of loss functions like squared loss, exponential loss, Huber loss etc which can be accessed through Shogun's * CLossFunction interface (cf. http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CLossFunction.html). Additionally, it can create - * an ensemble of any regressor class derived from the CMachine class (cf. http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1CMachine.html). + * an ensemble of any regressor class derived from the Machine class (cf. http://www.shogun-toolbox.org/doc/en/latest/classshogun_1_1Machine.html). * For one dimensional optimization, this class uses the backtracking linesearch accessed via Shogun's L-BFGS class. * A concise description of the algorithm implemented can be found in the following link : * http://en.wikipedia.org/wiki/Gradient_boosting#Algorithm */ -class CStochasticGBMachine : public RandomMixin +class StochasticGBMachine : public RandomMixin { public: /** Constructor @@ -64,11 +63,11 @@ class CStochasticGBMachine : public RandomMixin * @param subset_fraction fraction of trainining vectors to be chosen randomly w/o replacement * @param learning_rate shrinkage factor */ - CStochasticGBMachine(CMachine* machine=NULL, CLossFunction* loss=NULL, int32_t num_iterations=100, + StochasticGBMachine(std::shared_ptr machine=NULL, std::shared_ptr loss=NULL, int32_t num_iterations=100, float64_t learning_rate=1.0, float64_t subset_fraction=0.6); /** Destructor */ - virtual ~CStochasticGBMachine(); + virtual ~StochasticGBMachine(); /** get name * @@ -80,25 +79,25 @@ class CStochasticGBMachine : public RandomMixin * * @param machine machine */ - void set_machine(CMachine* machine); + void set_machine(std::shared_ptr machine); /** get machine * * @return machine */ - CMachine* get_machine() const; + std::shared_ptr get_machine() const; /** set loss function * * @param f loss function */ - virtual void set_loss_function(CLossFunction* f); + virtual void set_loss_function(std::shared_ptr f); /** get loss function * * @return loss function */ - virtual CLossFunction* get_loss_function() const; + virtual std::shared_ptr get_loss_function() const; /** set number of iterations * @@ -141,7 +140,7 @@ class CStochasticGBMachine : public RandomMixin * @param data test data * @return Regression labels */ - virtual CRegressionLabels* apply_regression(CFeatures* data=NULL); + virtual std::shared_ptr apply_regression(std::shared_ptr data=NULL); protected: /** train machine @@ -149,7 +148,7 @@ class CStochasticGBMachine : public RandomMixin * @param data training data * @return true */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); /** compute gamma values * @@ -160,7 +159,7 @@ class CStochasticGBMachine : public RandomMixin * the ensemble model */ float64_t compute_multiplier( - CRegressionLabels* f, CRegressionLabels* hm, CLabels* labs); + std::shared_ptr f, std::shared_ptr hm, std::shared_ptr labs); /** train base model * @@ -168,7 +167,7 @@ class CStochasticGBMachine : public RandomMixin * @param labels training labels * @return trained base model */ - CMachine* fit_model(CDenseFeatures* feats, CRegressionLabels* labels); + std::shared_ptr fit_model(std::shared_ptr> feats, std::shared_ptr labels); /** compute pseudo_residuals * @@ -176,17 +175,17 @@ class CStochasticGBMachine : public RandomMixin * @param labs training labels * @return pseudo_residuals */ - CRegressionLabels* - compute_pseudo_residuals(CRegressionLabels* inter_f, CLabels* labs); + std::shared_ptr + compute_pseudo_residuals(std::shared_ptr inter_f, std::shared_ptr labs); /** add randomized subset to relevant parameters * * @param f training data * @param interf intermediate boosted model labels for training data */ - std::tuple>, Some, - Some> - get_subset(CDenseFeatures* f, CRegressionLabels* interf); + std::tuple>, std::shared_ptr, + std::shared_ptr> + get_subset(std::shared_ptr> f, std::shared_ptr interf); /** reset arrays of weak learners and gamma values */ void initialize_learners(); @@ -213,10 +212,10 @@ class CStochasticGBMachine : public RandomMixin protected: /** machine to be used for GBoosting */ - CMachine* m_machine; + std::shared_ptr m_machine; /** loss function */ - CLossFunction* m_loss; + std::shared_ptr m_loss; /** num of iterations */ int32_t m_num_iter; @@ -228,10 +227,10 @@ class CStochasticGBMachine : public RandomMixin float64_t m_learning_rate; /** array of weak learners */ - CDynamicObjectArray* m_weak_learners; + std::shared_ptr m_weak_learners; /** gamma - weak learner weights */ - CDynamicArray* m_gamma; + std::shared_ptr> m_gamma; #ifndef SWIG public: static constexpr std::string_view kMachine = "machine"; diff --git a/src/shogun/machine/StructuredOutputMachine.cpp b/src/shogun/machine/StructuredOutputMachine.cpp index f45f77764d3..806ab05fef0 100644 --- a/src/shogun/machine/StructuredOutputMachine.cpp +++ b/src/shogun/machine/StructuredOutputMachine.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Thoralf Klein, Shell Hu, Bjoern Esser, + * Authors: Fernando Iglesias, Thoralf Klein, Shell Hu, Bjoern Esser, * Viktor Gal */ @@ -14,89 +14,89 @@ using namespace shogun; -CStructuredOutputMachine::CStructuredOutputMachine() -: CMachine(), m_model(NULL), m_surrogate_loss(NULL) +StructuredOutputMachine::StructuredOutputMachine() +: Machine(), m_model(NULL), m_surrogate_loss(NULL) { register_parameters(); } -CStructuredOutputMachine::CStructuredOutputMachine( - CStructuredModel* model, - CStructuredLabels* labs) -: CMachine(), m_model(model), m_surrogate_loss(NULL) +StructuredOutputMachine::StructuredOutputMachine( + std::shared_ptr model, + std::shared_ptr labs) +: Machine(), m_model(model), m_surrogate_loss(NULL) { - SG_REF(m_model); + set_labels(labs); register_parameters(); } -CStructuredOutputMachine::~CStructuredOutputMachine() +StructuredOutputMachine::~StructuredOutputMachine() { - SG_UNREF(m_model); - SG_UNREF(m_surrogate_loss); - SG_UNREF(m_helper); + + + } -void CStructuredOutputMachine::set_model(CStructuredModel* model) +void StructuredOutputMachine::set_model(std::shared_ptr model) { - SG_REF(model); - SG_UNREF(m_model); + + m_model = model; } -CStructuredModel* CStructuredOutputMachine::get_model() const +std::shared_ptr StructuredOutputMachine::get_model() const { - SG_REF(m_model); + return m_model; } -void CStructuredOutputMachine::register_parameters() +void StructuredOutputMachine::register_parameters() { - SG_ADD((CSGObject**)&m_model, "m_model", "Structured model"); - SG_ADD((CSGObject**)&m_surrogate_loss, "m_surrogate_loss", "Surrogate loss"); + SG_ADD((std::shared_ptr*)&m_model, "m_model", "Structured model"); + SG_ADD((std::shared_ptr*)&m_surrogate_loss, "m_surrogate_loss", "Surrogate loss"); SG_ADD(&m_verbose, "verbose", "Verbosity flag"); - SG_ADD((CSGObject**)&m_helper, "helper", "Training helper"); + SG_ADD((std::shared_ptr*)&m_helper, "helper", "Training helper"); m_verbose = false; m_helper = NULL; } -void CStructuredOutputMachine::set_labels(CLabels* lab) +void StructuredOutputMachine::set_labels(std::shared_ptr lab) { - CMachine::set_labels(lab); + Machine::set_labels(lab); require(m_model != NULL, "please call set_model() before set_labels()"); - m_model->set_labels(lab->as()); + m_model->set_labels(lab->as()); } -void CStructuredOutputMachine::set_features(CFeatures* f) +void StructuredOutputMachine::set_features(std::shared_ptr f) { m_model->set_features(f); } -CFeatures* CStructuredOutputMachine::get_features() const +std::shared_ptr StructuredOutputMachine::get_features() const { return m_model->get_features(); } -void CStructuredOutputMachine::set_surrogate_loss(CLossFunction* loss) +void StructuredOutputMachine::set_surrogate_loss(std::shared_ptr loss) { - SG_REF(loss); - SG_UNREF(m_surrogate_loss); + + m_surrogate_loss = loss; } -CLossFunction* CStructuredOutputMachine::get_surrogate_loss() const +std::shared_ptr StructuredOutputMachine::get_surrogate_loss() const { - SG_REF(m_surrogate_loss); + return m_surrogate_loss; } -float64_t CStructuredOutputMachine::risk_nslack_margin_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) +float64_t StructuredOutputMachine::risk_nslack_margin_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) { int32_t dim = m_model->get_dim(); int32_t from=0, to=0; - CFeatures* features = get_features(); + auto features = get_features(); if (info) { from = info->m_from; @@ -107,50 +107,50 @@ float64_t CStructuredOutputMachine::risk_nslack_margin_rescale(SGVectorget_num_vectors(); } - SG_UNREF(features); + float64_t R = 0.0; linalg::zero(subgrad); for (int32_t i=from; iargmax(SGVector(W.vector,dim,false), i, true); + auto result = m_model->argmax(SGVector(W.vector,dim,false), i, true); SGVector psi_pred = result->psi_pred; SGVector psi_truth = result->psi_truth; SGVector::vec1_plus_scalar_times_vec2(subgrad.vector, 1.0, psi_pred.vector, dim); SGVector::vec1_plus_scalar_times_vec2(subgrad.vector, -1.0, psi_truth.vector, dim); R += result->score; - SG_UNREF(result); + } return R; } -float64_t CStructuredOutputMachine::risk_nslack_slack_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) +float64_t StructuredOutputMachine::risk_nslack_slack_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) { error("{}::risk_nslack_slack_rescale() has not been implemented!", get_name()); return 0.0; } -float64_t CStructuredOutputMachine::risk_1slack_margin_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) +float64_t StructuredOutputMachine::risk_1slack_margin_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) { error("{}::risk_1slack_margin_rescale() has not been implemented!", get_name()); return 0.0; } -float64_t CStructuredOutputMachine::risk_1slack_slack_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) +float64_t StructuredOutputMachine::risk_1slack_slack_rescale(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) { error("{}::risk_1slack_slack_rescale() has not been implemented!", get_name()); return 0.0; } -float64_t CStructuredOutputMachine::risk_customized_formulation(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) +float64_t StructuredOutputMachine::risk_customized_formulation(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info) { error("{}::risk_customized_formulation() has not been implemented!", get_name()); return 0.0; } -float64_t CStructuredOutputMachine::risk(SGVector& subgrad, SGVector& W, +float64_t StructuredOutputMachine::risk(SGVector& subgrad, SGVector& W, TMultipleCPinfo* info, EStructRiskType rtype) { float64_t ret = 0.0; @@ -179,7 +179,7 @@ float64_t CStructuredOutputMachine::risk(SGVector& subgrad, SGVector< return ret; } -CSOSVMHelper* CStructuredOutputMachine::get_helper() const +std::shared_ptr StructuredOutputMachine::get_helper() const { if (m_helper == NULL) { @@ -187,16 +187,16 @@ CSOSVMHelper* CStructuredOutputMachine::get_helper() const "Please set verbose before training!", get_name()); } - SG_REF(m_helper); + return m_helper; } -void CStructuredOutputMachine::set_verbose(bool verbose) +void StructuredOutputMachine::set_verbose(bool verbose) { m_verbose = verbose; } -bool CStructuredOutputMachine::get_verbose() const +bool StructuredOutputMachine::get_verbose() const { return m_verbose; } diff --git a/src/shogun/machine/StructuredOutputMachine.h b/src/shogun/machine/StructuredOutputMachine.h index d048f2949f1..891b2e9b032 100644 --- a/src/shogun/machine/StructuredOutputMachine.h +++ b/src/shogun/machine/StructuredOutputMachine.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Fernando Iglesias, Shell Hu, Yuyu Zhang, Thoralf Klein, + * Authors: Fernando Iglesias, Shell Hu, Yuyu Zhang, Thoralf Klein, * Bjoern Esser, Sergey Lisitsyn, Viktor Gal */ @@ -17,10 +17,10 @@ namespace shogun { -class CFeatures; -class CLabels; -class CLossFunction; -class CStructuredLabels; +class Features; +class Labels; +class LossFunction; +class StructuredLabels; struct TMultipleCPinfo; /** The structured empirical risk types, corresponding to different training objectives [1]. @@ -37,39 +37,39 @@ enum EStructRiskType CUSTOMIZED_RISK = 4 }; -class CStructuredModel; +class StructuredModel; /** TODO doc */ -class CStructuredOutputMachine : public CMachine +class StructuredOutputMachine : public Machine { public: /** problem type */ MACHINE_PROBLEM_TYPE(PT_STRUCTURED); /** deafult constructor */ - CStructuredOutputMachine(); + StructuredOutputMachine(); /** standard constructor * * @param model structured model with application specific functions * @param labs structured labels */ - CStructuredOutputMachine(CStructuredModel* model, CStructuredLabels* labs); + StructuredOutputMachine(std::shared_ptr model, std::shared_ptr labs); /** destructor */ - virtual ~CStructuredOutputMachine(); + virtual ~StructuredOutputMachine(); /** set structured model * * @param model structured model to set */ - void set_model(CStructuredModel* model); + void set_model(std::shared_ptr model); /** get structured model * * @return structured model */ - CStructuredModel* get_model() const; + std::shared_ptr get_model() const; /** @return object name */ virtual const char* get_name() const @@ -81,31 +81,31 @@ class CStructuredOutputMachine : public CMachine * * @param lab labels */ - virtual void set_labels(CLabels* lab); + virtual void set_labels(std::shared_ptr lab); /** set features * * @param f features */ - void set_features(CFeatures* f); + void set_features(std::shared_ptr f); /** get features * * @return features */ - CFeatures* get_features() const; + std::shared_ptr get_features() const; /** set surrogate loss function * * @param loss loss function to set */ - void set_surrogate_loss(CLossFunction* loss); + void set_surrogate_loss(std::shared_ptr loss); /** get surrogate loss function * * @return loss function */ - CLossFunction* get_surrogate_loss() const; + std::shared_ptr get_surrogate_loss() const; /** computes the value of the risk function and sub-gradient at given point * @@ -119,7 +119,7 @@ class CStructuredOutputMachine : public CMachine TMultipleCPinfo* info=0, EStructRiskType rtype = N_SLACK_MARGIN_RESCALING); /** @return training progress helper */ - CSOSVMHelper* get_helper() const; + std::shared_ptr get_helper() const; /** set verbose * NOTE that track verbose information including primal objectives, @@ -207,21 +207,21 @@ class CStructuredOutputMachine : public CMachine protected: /** the model that contains the application dependent modules */ - CStructuredModel* m_model; + std::shared_ptr m_model; /** the surrogate loss, for SOSVM, fixed to Hinge loss, * other non-convex losses such as Ramp loss are also applicable, * will be extended in the future */ - CLossFunction* m_surrogate_loss; + std::shared_ptr m_surrogate_loss; /** the helper that records primal objectives, duality gaps etc */ - CSOSVMHelper* m_helper; + std::shared_ptr m_helper; /** verbose outputs and statistics */ bool m_verbose; -}; /* class CStructuredOutputMachine */ +}; /* class StructuredOutputMachine */ } /* namespace shogun */ diff --git a/src/shogun/machine/gp/ConstMean.cpp b/src/shogun/machine/gp/ConstMean.cpp index baa8741acc9..34187804f5d 100644 --- a/src/shogun/machine/gp/ConstMean.cpp +++ b/src/shogun/machine/gp/ConstMean.cpp @@ -35,36 +35,36 @@ using namespace shogun; -CConstMean::CConstMean() : CMeanFunction() +ConstMean::ConstMean() : MeanFunction() { init(); } -CConstMean::~CConstMean() +ConstMean::~ConstMean() { } -CConstMean::CConstMean(float64_t mean) - : CMeanFunction() +ConstMean::ConstMean(float64_t mean) + : MeanFunction() { init(); m_mean=mean; } -void CConstMean::init() +void ConstMean::init() { m_mean=0.0; SG_ADD(&m_mean, "mean", "const value of mean function", ParameterProperties::HYPER | ParameterProperties::GRADIENT); } -SGVector CConstMean::get_mean_vector(const CFeatures* features) const +SGVector ConstMean::get_mean_vector(std::shared_ptr features) const { SGVector result(features->get_num_vectors()); result.set_const(m_mean); return result; } -SGVector CConstMean::get_parameter_derivative(const CFeatures* features, +SGVector ConstMean::get_parameter_derivative(std::shared_ptr features, const TParameter* param, index_t index) { require(features,"The features should NOT be NULL"); diff --git a/src/shogun/machine/gp/ConstMean.h b/src/shogun/machine/gp/ConstMean.h index e8d0ed2506f..1a25e1d2563 100644 --- a/src/shogun/machine/gp/ConstMean.h +++ b/src/shogun/machine/gp/ConstMean.h @@ -45,20 +45,20 @@ namespace shogun * * Simple mean function that assumes a mean of Const value. */ -class CConstMean : public CMeanFunction +class ConstMean : public MeanFunction { public: /** default constructor * the default value of mean is 0 */ - CConstMean(); + ConstMean(); /** constructor * @param mean const value for mean function */ - CConstMean(float64_t mean); + ConstMean(float64_t mean); - virtual ~CConstMean(); + virtual ~ConstMean(); /** set the const_value of mean function * @@ -79,7 +79,7 @@ class CConstMean : public CMeanFunction * * @return mean of feature vectors */ - virtual SGVector get_mean_vector(const CFeatures* features) const; + virtual SGVector get_mean_vector(std::shared_ptr features) const; /** returns the derivative of the mean function * @@ -89,7 +89,7 @@ class CConstMean : public CMeanFunction * * @return derivative of mean function with respect to parameter */ - virtual SGVector get_parameter_derivative(const CFeatures* features, + virtual SGVector get_parameter_derivative(std::shared_ptr features, const TParameter* param, index_t index=-1); private: diff --git a/src/shogun/machine/gp/DualVariationalGaussianLikelihood.cpp b/src/shogun/machine/gp/DualVariationalGaussianLikelihood.cpp index 947911c0a8f..ca3a5d42f7b 100644 --- a/src/shogun/machine/gp/DualVariationalGaussianLikelihood.cpp +++ b/src/shogun/machine/gp/DualVariationalGaussianLikelihood.cpp @@ -41,20 +41,20 @@ using namespace Eigen; namespace shogun { -CDualVariationalGaussianLikelihood::CDualVariationalGaussianLikelihood() - : CVariationalGaussianLikelihood() +DualVariationalGaussianLikelihood::DualVariationalGaussianLikelihood() + : VariationalGaussianLikelihood() { init(); } -CDualVariationalGaussianLikelihood::~CDualVariationalGaussianLikelihood() +DualVariationalGaussianLikelihood::~DualVariationalGaussianLikelihood() { } -CVariationalGaussianLikelihood* CDualVariationalGaussianLikelihood::get_variational_likelihood() const +std::shared_ptr DualVariationalGaussianLikelihood::get_variational_likelihood() const { require(m_likelihood, "The likelihood model must not be NULL"); - CVariationalGaussianLikelihood* var_lik=dynamic_cast(m_likelihood); + auto var_lik=std::dynamic_pointer_cast(m_likelihood); require(var_lik, "The likelihood model ({}) does NOT support variational guassian inference", m_likelihood->get_name()); @@ -62,44 +62,44 @@ CVariationalGaussianLikelihood* CDualVariationalGaussianLikelihood::get_variatio return var_lik; } -SGVector CDualVariationalGaussianLikelihood::get_variational_expection() +SGVector DualVariationalGaussianLikelihood::get_variational_expection() { - CVariationalLikelihood * var_lik=get_variational_likelihood(); + auto var_lik=get_variational_likelihood(); return var_lik->get_variational_expection(); } -void CDualVariationalGaussianLikelihood::set_noise_factor(float64_t noise_factor) +void DualVariationalGaussianLikelihood::set_noise_factor(float64_t noise_factor) { - CVariationalGaussianLikelihood * var_lik=get_variational_likelihood(); + auto var_lik=get_variational_likelihood(); var_lik->set_noise_factor(noise_factor); } -SGVector CDualVariationalGaussianLikelihood::get_variational_first_derivative(const TParameter* param) const +SGVector DualVariationalGaussianLikelihood::get_variational_first_derivative(const TParameter* param) const { - CVariationalLikelihood * var_lik=get_variational_likelihood(); + auto var_lik=get_variational_likelihood(); return var_lik->get_variational_first_derivative(param); } -bool CDualVariationalGaussianLikelihood::supports_derivative_wrt_hyperparameter() const +bool DualVariationalGaussianLikelihood::supports_derivative_wrt_hyperparameter() const { - CVariationalLikelihood * var_lik=get_variational_likelihood(); + auto var_lik=get_variational_likelihood(); return var_lik->supports_derivative_wrt_hyperparameter(); } -SGVector CDualVariationalGaussianLikelihood::get_first_derivative_wrt_hyperparameter(const TParameter* param) const +SGVector DualVariationalGaussianLikelihood::get_first_derivative_wrt_hyperparameter(const TParameter* param) const { - CVariationalLikelihood * var_lik=get_variational_likelihood(); + auto var_lik=get_variational_likelihood(); return var_lik->get_first_derivative_wrt_hyperparameter(param); } -bool CDualVariationalGaussianLikelihood::set_variational_distribution( - SGVector mu, SGVector s2, const CLabels* lab) +bool DualVariationalGaussianLikelihood::set_variational_distribution( + SGVector mu, SGVector s2, std::shared_ptr lab) { - CVariationalGaussianLikelihood* var_lik=get_variational_likelihood(); + auto var_lik=get_variational_likelihood(); return var_lik->set_variational_distribution(mu, s2, lab); } -void CDualVariationalGaussianLikelihood::set_strict_scale(float64_t strict_scale) +void DualVariationalGaussianLikelihood::set_strict_scale(float64_t strict_scale) { require((strict_scale>0 && strict_scale<1), "The strict_scale ({}) should be between 0 and 1 exclusively.", @@ -107,7 +107,7 @@ void CDualVariationalGaussianLikelihood::set_strict_scale(float64_t strict_scale m_strict_scale=strict_scale; } -float64_t CDualVariationalGaussianLikelihood::adjust_step_wrt_dual_parameter(SGVector direction, const float64_t step) const +float64_t DualVariationalGaussianLikelihood::adjust_step_wrt_dual_parameter(SGVector direction, const float64_t step) const { require(direction.vlen==m_lambda.vlen, "The length ({}) of direction should be same as the length ({}) of dual parameters", @@ -131,18 +131,18 @@ float64_t CDualVariationalGaussianLikelihood::adjust_step_wrt_dual_parameter(SGV if (direction[i]==0.0) continue; - if (lower_bound!=-CMath::INFTY && attemptupper_bound) + if (upper_bound!=Math::INFTY && attempt>upper_bound) { - adjust=(upper_bound-m_lambda[i])/CMath::abs(direction[i]); + adjust=(upper_bound-m_lambda[i])/Math::abs(direction[i]); if (dual_upper_bound_strict()) adjust*=(1-m_strict_scale); if (adjust lambda, const CLabels* lab) +void DualVariationalGaussianLikelihood::set_dual_parameters(SGVector lambda, std::shared_ptr lab) { require(lab, "Labels are required (lab should not be NULL)"); @@ -162,23 +162,23 @@ void CDualVariationalGaussianLikelihood::set_dual_parameters(SGVector "and number of labels ({}) should be the same", lambda.vlen, lab->get_num_labels()); require(lab->get_label_type()==LT_BINARY, - "Labels ({}) must be type of CBinaryLabels", + "Labels ({}) must be type of BinaryLabels", lab->get_name()); - m_lab=(((CBinaryLabels*)lab)->get_labels()).clone(); + m_lab=lab->as()->get_labels().clone(); //Convert the input label to standard label used in the class //Note that Shogun uses -1 and 1 as labels and this class internally uses //0 and 1 repectively. for(index_t i = 0; i < m_lab.size(); ++i) - m_lab[i]=CMath::max(m_lab[i], 0.0); + m_lab[i]=Math::max(m_lab[i], 0.0); m_lambda=lambda; precompute(); } -bool CDualVariationalGaussianLikelihood::dual_parameters_valid() const +bool DualVariationalGaussianLikelihood::dual_parameters_valid() const { float64_t lower_bound=get_dual_lower_bound(); float64_t upper_bound=get_dual_upper_bound(); @@ -209,12 +209,12 @@ bool CDualVariationalGaussianLikelihood::dual_parameters_valid() const return true; } -void CDualVariationalGaussianLikelihood::precompute() +void DualVariationalGaussianLikelihood::precompute() { m_is_valid=dual_parameters_valid(); } -void CDualVariationalGaussianLikelihood::init() +void DualVariationalGaussianLikelihood::init() { SG_ADD(&m_lambda, "lambda", "Dual parameter for variational s2"); diff --git a/src/shogun/machine/gp/DualVariationalGaussianLikelihood.h b/src/shogun/machine/gp/DualVariationalGaussianLikelihood.h index 1b4e5783d8b..3c5f028f219 100644 --- a/src/shogun/machine/gp/DualVariationalGaussianLikelihood.h +++ b/src/shogun/machine/gp/DualVariationalGaussianLikelihood.h @@ -59,13 +59,13 @@ namespace shogun * In this setting, \f$\alpha\f$ and \f$\lambda\f$ are called dual parameters for \f$\mu\f$ and \f$\sigma^2\f$ respectively. * */ -class CDualVariationalGaussianLikelihood : public CVariationalGaussianLikelihood +class DualVariationalGaussianLikelihood : public VariationalGaussianLikelihood { public: /** default constructor */ - CDualVariationalGaussianLikelihood(); + DualVariationalGaussianLikelihood(); - virtual ~CDualVariationalGaussianLikelihood(); + virtual ~DualVariationalGaussianLikelihood(); /** returns the name of the likelihood model * @@ -118,7 +118,7 @@ class CDualVariationalGaussianLikelihood : public CVariationalGaussianLikelihood * */ virtual bool set_variational_distribution(SGVector mu, - SGVector s2, const CLabels* lab); + SGVector s2, std::shared_ptr lab); /** check whether the dual parameters are valid or not. * @@ -146,7 +146,7 @@ class CDualVariationalGaussianLikelihood : public CVariationalGaussianLikelihood * Note that dual parameter (alpha) for the variational variance * is implicitly set based on lambda */ - virtual void set_dual_parameters(SGVector the_lambda, const CLabels* lab); + virtual void set_dual_parameters(SGVector the_lambda, std::shared_ptr lab); /** get the dual parameter (alpha) for variational mu * @@ -245,7 +245,7 @@ class CDualVariationalGaussianLikelihood : public CVariationalGaussianLikelihood /** this method is used to dynamic-cast the likelihood model, m_likelihood, * to variational likelihood model. */ - virtual CVariationalGaussianLikelihood* get_variational_likelihood() const; + virtual std::shared_ptr get_variational_likelihood() const; private: /** initialize private data members for this class */ void init(); diff --git a/src/shogun/machine/gp/EPInferenceMethod.cpp b/src/shogun/machine/gp/EPInferenceMethod.cpp index f94b177b1d9..80727d2759e 100644 --- a/src/shogun/machine/gp/EPInferenceMethod.cpp +++ b/src/shogun/machine/gp/EPInferenceMethod.cpp @@ -60,28 +60,28 @@ using namespace Eigen; mat=SGMatrix(rows, cols); \ } -CEPInferenceMethod::CEPInferenceMethod() +EPInferenceMethod::EPInferenceMethod() { init(); } -CEPInferenceMethod::CEPInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model) - : RandomMixin(kernel, features, mean, labels, model) +EPInferenceMethod::EPInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model) + : RandomMixin(kernel, features, mean, labels, model) { init(); } -CEPInferenceMethod::~CEPInferenceMethod() +EPInferenceMethod::~EPInferenceMethod() { } -void CEPInferenceMethod::register_minimizer(Minimizer* minimizer) +void EPInferenceMethod::register_minimizer(std::shared_ptr minimizer) { io::warn("The method does not require a minimizer. The provided minimizer will not be used."); } -void CEPInferenceMethod::init() +void EPInferenceMethod::init() { m_max_sweep=15; m_min_sweep=2; @@ -89,20 +89,19 @@ void CEPInferenceMethod::init() m_fail_on_non_convergence=true; } -CEPInferenceMethod* CEPInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr EPInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_EP) - error("Provided inference is not of type CEPInferenceMethod!"); + error("Provided inference is not of type EPInferenceMethod!"); - SG_REF(inference); - return (CEPInferenceMethod*)inference; + return inference->as(); } -float64_t CEPInferenceMethod::get_negative_log_marginal_likelihood() +float64_t EPInferenceMethod::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); @@ -110,7 +109,7 @@ float64_t CEPInferenceMethod::get_negative_log_marginal_likelihood() return m_nlZ; } -SGVector CEPInferenceMethod::get_alpha() +SGVector EPInferenceMethod::get_alpha() { if (parameter_hash_changed()) update(); @@ -118,7 +117,7 @@ SGVector CEPInferenceMethod::get_alpha() return SGVector(m_alpha); } -SGMatrix CEPInferenceMethod::get_cholesky() +SGMatrix EPInferenceMethod::get_cholesky() { if (parameter_hash_changed()) update(); @@ -126,7 +125,7 @@ SGMatrix CEPInferenceMethod::get_cholesky() return SGMatrix(m_L); } -SGVector CEPInferenceMethod::get_diagonal_vector() +SGVector EPInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); @@ -134,23 +133,23 @@ SGVector CEPInferenceMethod::get_diagonal_vector() return SGVector(m_sttau); } -SGVector CEPInferenceMethod::get_posterior_mean() +SGVector EPInferenceMethod::get_posterior_mean() { compute_gradient(); return SGVector(m_mu); } -SGMatrix CEPInferenceMethod::get_posterior_covariance() +SGMatrix EPInferenceMethod::get_posterior_covariance() { compute_gradient(); return SGMatrix(m_Sigma); } -void CEPInferenceMethod::compute_gradient() +void EPInferenceMethod::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -161,12 +160,12 @@ void CEPInferenceMethod::compute_gradient() } } -void CEPInferenceMethod::update() +void EPInferenceMethod::update() { SG_TRACE("entering"); // update kernel and feature matrix - CInference::update(); + Inference::update(); // get number of labels (trainig examples) index_t n=m_labels->get_num_labels(); @@ -229,10 +228,10 @@ void CEPInferenceMethod::update() SGVector mu_n(n); SGVector s2_n(n); - float64_t nlZ_old=CMath::INFTY; + float64_t nlZ_old=Math::INFTY; uint32_t sweep=0; - while ((CMath::abs(m_nlZ-nlZ_old)>m_tol && sweepm_tol && sweepm_tol) + if (sweep==m_max_sweep && Math::abs(m_nlZ-nlZ_old)>m_tol) { io::warn("Maximum number ({}) of sweeps reached, but tolerance ({}) was " "not yet reached. You can increase or decrease both.", @@ -316,7 +315,7 @@ void CEPInferenceMethod::update() SG_TRACE("leaving"); } -void CEPInferenceMethod::update_alpha() +void EPInferenceMethod::update_alpha() { // create eigen representations kernel matrix, L^T, sqrt(ttau) and tnu Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -340,7 +339,7 @@ void CEPInferenceMethod::update_alpha() eigen_alpha=eigen_tnu-eigen_sttau.cwiseProduct(eigen_v); } -void CEPInferenceMethod::update_chol() +void EPInferenceMethod::update_chol() { // create eigen representations of kernel matrix and sqrt(ttau) Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -361,7 +360,7 @@ void CEPInferenceMethod::update_chol() eigen_L=eigen_chol.matrixU(); } -void CEPInferenceMethod::update_approx_cov() +void EPInferenceMethod::update_approx_cov() { // create eigen representations of kernel matrix, L^T matrix and sqrt(ttau) Map eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols); @@ -385,7 +384,7 @@ void CEPInferenceMethod::update_approx_cov() eigen_K * std::exp(m_log_scale * 2.0) - eigen_V.adjoint() * eigen_V; } -void CEPInferenceMethod::update_approx_mean() +void EPInferenceMethod::update_approx_mean() { // create eigen representation of posterior covariance matrix and tnu Map eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols); @@ -399,7 +398,7 @@ void CEPInferenceMethod::update_approx_mean() eigen_mu=eigen_Sigma*eigen_tnu; } -void CEPInferenceMethod::update_negative_ml() +void EPInferenceMethod::update_negative_ml() { // create eigen representation of Sigma, L, mu, tnu, ttau Map eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols); @@ -455,7 +454,7 @@ void CEPInferenceMethod::update_negative_ml() m_nlZ=nlZ_part1+nlZ_part2+nlZ_part3; } -void CEPInferenceMethod::update_deriv() +void EPInferenceMethod::update_deriv() { // create eigen representation of L, sstau, alpha Map eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols); @@ -475,7 +474,7 @@ void CEPInferenceMethod::update_deriv() eigen_F=eigen_alpha*eigen_alpha.adjoint()-eigen_sttau.asDiagonal()*V; } -SGVector CEPInferenceMethod::get_derivative_wrt_inference_method( +SGVector EPInferenceMethod::get_derivative_wrt_inference_method( const TParameter* param) { require(!strcmp(param->m_name, "log_scale"), "Can't compute derivative of " @@ -494,14 +493,14 @@ SGVector CEPInferenceMethod::get_derivative_wrt_inference_method( return result; } -SGVector CEPInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector EPInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { not_implemented(SOURCE_LOCATION); return SGVector(); } -SGVector CEPInferenceMethod::get_derivative_wrt_kernel( +SGVector EPInferenceMethod::get_derivative_wrt_kernel( const TParameter* param) { // create eigen representation of the matrix Q @@ -531,7 +530,7 @@ SGVector CEPInferenceMethod::get_derivative_wrt_kernel( return result; } -SGVector CEPInferenceMethod::get_derivative_wrt_mean( +SGVector EPInferenceMethod::get_derivative_wrt_mean( const TParameter* param) { not_implemented(SOURCE_LOCATION); diff --git a/src/shogun/machine/gp/EPInferenceMethod.h b/src/shogun/machine/gp/EPInferenceMethod.h index 95eb9f338a4..13c4b44dcf0 100644 --- a/src/shogun/machine/gp/EPInferenceMethod.h +++ b/src/shogun/machine/gp/EPInferenceMethod.h @@ -50,11 +50,11 @@ namespace shogun * Approximate Bayesian Inference. PhD thesis, Massachusetts Institute of * Technology */ -class CEPInferenceMethod : public RandomMixin +class EPInferenceMethod : public RandomMixin { public: /** default constructor */ - CEPInferenceMethod(); + EPInferenceMethod(); /** constructor * @@ -64,10 +64,10 @@ class CEPInferenceMethod : public RandomMixin * @param labels labels of the features * @param model likelihood model to use */ - CEPInferenceMethod(CKernel* kernel, CFeatures* features, CMeanFunction* mean, - CLabels* labels, CLikelihoodModel* model); + EPInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, std::shared_ptr mean, + std::shared_ptr labels, std::shared_ptr model); - virtual ~CEPInferenceMethod(); + virtual ~EPInferenceMethod(); /** return what type of inference we are * @@ -84,9 +84,9 @@ class CEPInferenceMethod : public RandomMixin /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CEPInferenceMethod object + * @return casted EPInferenceMethod object */ - static CEPInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** returns the negative logarithm of the marginal likelihood function: * @@ -250,7 +250,7 @@ class CEPInferenceMethod : public RandomMixin * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); /** Specify behavious when EP does not converge: failure or warning * @param fail_on_non_convergence If True, throws error, otherwise prints warning diff --git a/src/shogun/machine/gp/ExactInferenceMethod.cpp b/src/shogun/machine/gp/ExactInferenceMethod.cpp index 0ce3be53d71..b7fe5465f59 100644 --- a/src/shogun/machine/gp/ExactInferenceMethod.cpp +++ b/src/shogun/machine/gp/ExactInferenceMethod.cpp @@ -42,28 +42,28 @@ using namespace shogun; using namespace Eigen; -CExactInferenceMethod::CExactInferenceMethod() : CInference() +ExactInferenceMethod::ExactInferenceMethod() : Inference() { } -CExactInferenceMethod::CExactInferenceMethod(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) : - CInference(kern, feat, m, lab, mod) +ExactInferenceMethod::ExactInferenceMethod(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) : + Inference(kern, feat, m, lab, mod) { } -CExactInferenceMethod::~CExactInferenceMethod() +ExactInferenceMethod::~ExactInferenceMethod() { } -void CExactInferenceMethod::register_minimizer(Minimizer* minimizer) +void ExactInferenceMethod::register_minimizer(std::shared_ptr minimizer) { io::warn("The method does not require a minimizer. The provided minimizer will not be used."); } -void CExactInferenceMethod::compute_gradient() +void ExactInferenceMethod::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -75,11 +75,11 @@ void CExactInferenceMethod::compute_gradient() } } -void CExactInferenceMethod::update() +void ExactInferenceMethod::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_chol(); update_alpha(); m_gradient_update=false; @@ -88,9 +88,9 @@ void CExactInferenceMethod::update() SG_TRACE("leaving"); } -void CExactInferenceMethod::check_members() const +void ExactInferenceMethod::check_members() const { - CInference::check_members(); + Inference::check_members(); require(m_model->get_model_type()==LT_GAUSSIAN, "Exact inference method can only use Gaussian likelihood function"); @@ -98,13 +98,13 @@ void CExactInferenceMethod::check_members() const "Labels must be type of CRegressionLabels"); } -SGVector CExactInferenceMethod::get_diagonal_vector() +SGVector ExactInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // compute diagonal vector: sW=1/sigma @@ -114,13 +114,13 @@ SGVector CExactInferenceMethod::get_diagonal_vector() return result; } -float64_t CExactInferenceMethod::get_negative_log_marginal_likelihood() +float64_t ExactInferenceMethod::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // create eigen representation of alpha and L @@ -128,7 +128,7 @@ float64_t CExactInferenceMethod::get_negative_log_marginal_likelihood() Map eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols); // get labels and mean vectors and create eigen representation - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=regression_labels(m_labels)->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); @@ -138,12 +138,12 @@ float64_t CExactInferenceMethod::get_negative_log_marginal_likelihood() float64_t result = (eigen_y - eigen_m).dot(eigen_alpha) / 2.0 + eigen_L.diagonal().array().log().sum() + - m_L.num_rows * std::log(2 * CMath::PI * CMath::sq(sigma)) / 2.0; + m_L.num_rows * std::log(2 * Math::PI * Math::sq(sigma)) / 2.0; return result; } -SGVector CExactInferenceMethod::get_alpha() +SGVector ExactInferenceMethod::get_alpha() { if (parameter_hash_changed()) update(); @@ -151,7 +151,7 @@ SGVector CExactInferenceMethod::get_alpha() return SGVector(m_alpha); } -SGMatrix CExactInferenceMethod::get_cholesky() +SGMatrix ExactInferenceMethod::get_cholesky() { if (parameter_hash_changed()) update(); @@ -159,24 +159,24 @@ SGMatrix CExactInferenceMethod::get_cholesky() return SGMatrix(m_L); } -SGVector CExactInferenceMethod::get_posterior_mean() +SGVector ExactInferenceMethod::get_posterior_mean() { compute_gradient(); return SGVector(m_mu); } -SGMatrix CExactInferenceMethod::get_posterior_covariance() +SGMatrix ExactInferenceMethod::get_posterior_covariance() { compute_gradient(); return SGMatrix(m_Sigma); } -void CExactInferenceMethod::update_chol() +void ExactInferenceMethod::update_chol() { // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); /* check whether to allocate cholesky memory */ @@ -187,19 +187,19 @@ void CExactInferenceMethod::update_chol() Map K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); Map L(m_L.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); LLT llt( - K * (std::exp(m_log_scale * 2.0) / CMath::sq(sigma)) + + K * (std::exp(m_log_scale * 2.0) / Math::sq(sigma)) + MatrixXd::Identity(m_ktrtr.num_rows, m_ktrtr.num_cols)); L=llt.matrixU(); } -void CExactInferenceMethod::update_alpha() +void ExactInferenceMethod::update_alpha() { // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // get labels and mean vector and create eigen representation - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=regression_labels(m_labels)->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); @@ -214,10 +214,10 @@ void CExactInferenceMethod::update_alpha() a=L.triangularView().adjoint().solve(eigen_y-eigen_m); a=L.triangularView().solve(a); - a/=CMath::sq(sigma); + a/=Math::sq(sigma); } -void CExactInferenceMethod::update_mean() +void ExactInferenceMethod::update_mean() { // create eigen representataion of kernel matrix and alpha Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -233,7 +233,7 @@ void CExactInferenceMethod::update_mean() eigen_mu = eigen_K * std::exp(m_log_scale * 2.0) * eigen_alpha; } -void CExactInferenceMethod::update_cov() +void ExactInferenceMethod::update_cov() { // create eigen representataion of upper triangular factor L^T and kernel // matrix @@ -248,7 +248,7 @@ void CExactInferenceMethod::update_cov() MatrixXd eigen_V = eigen_L.triangularView().adjoint().solve( eigen_K * std::exp(m_log_scale * 2.0)); - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); eigen_V = eigen_V/sigma; @@ -257,10 +257,10 @@ void CExactInferenceMethod::update_cov() eigen_K * std::exp(m_log_scale * 2.0) - eigen_V.adjoint() * eigen_V; } -void CExactInferenceMethod::update_deriv() +void ExactInferenceMethod::update_deriv() { // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // create eigen representation of derivative matrix and cholesky @@ -276,13 +276,13 @@ void CExactInferenceMethod::update_deriv() eigen_Q=eigen_L.triangularView().solve(eigen_Q); // divide Q by sigma^2 - eigen_Q/=CMath::sq(sigma); + eigen_Q/=Math::sq(sigma); // create eigen representation of alpha and compute Q=Q-alpha*alpha' eigen_Q-=eigen_alpha*eigen_alpha.transpose(); } -SGVector CExactInferenceMethod::get_derivative_wrt_inference_method( +SGVector ExactInferenceMethod::get_derivative_wrt_inference_method( const TParameter* param) { require(!strcmp(param->m_name, "log_scale"), "Can't compute derivative of " @@ -301,20 +301,19 @@ SGVector CExactInferenceMethod::get_derivative_wrt_inference_method( return result; } -CExactInferenceMethod* CExactInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr ExactInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_EXACT) - error("Provided inference is not of type CExactInferenceMethod!"); + error("Provided inference is not of type ExactInferenceMethod!"); - SG_REF(inference); - return (CExactInferenceMethod*)inference; + return inference->as(); } -SGVector CExactInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector ExactInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { require(!strcmp(param->m_name, "log_sigma"), "Can't compute derivative of " @@ -322,7 +321,7 @@ SGVector CExactInferenceMethod::get_derivative_wrt_likelihood_model( m_model->get_name(), param->m_name); // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // create eigen representation of the matrix Q @@ -332,12 +331,12 @@ SGVector CExactInferenceMethod::get_derivative_wrt_likelihood_model( // compute derivative wrt likelihood model parameter sigma: // dnlZ=sigma^2*trace(Q) - result[0]=CMath::sq(sigma)*eigen_Q.trace(); + result[0]=Math::sq(sigma)*eigen_Q.trace(); return result; } -SGVector CExactInferenceMethod::get_derivative_wrt_kernel( +SGVector ExactInferenceMethod::get_derivative_wrt_kernel( const TParameter* param) { // create eigen representation of the matrix Q @@ -367,7 +366,7 @@ SGVector CExactInferenceMethod::get_derivative_wrt_kernel( return result; } -SGVector CExactInferenceMethod::get_derivative_wrt_mean( +SGVector ExactInferenceMethod::get_derivative_wrt_mean( const TParameter* param) { // create eigen representation of alpha vector diff --git a/src/shogun/machine/gp/ExactInferenceMethod.h b/src/shogun/machine/gp/ExactInferenceMethod.h index c3417dfcee8..7e6774f275a 100644 --- a/src/shogun/machine/gp/ExactInferenceMethod.h +++ b/src/shogun/machine/gp/ExactInferenceMethod.h @@ -63,11 +63,11 @@ namespace shogun * NOTE: The Gaussian Likelihood Function must be used for this inference * method. */ -class CExactInferenceMethod: public CInference +class ExactInferenceMethod: public Inference { public: /** default constructor */ - CExactInferenceMethod(); + ExactInferenceMethod(); /** constructor * @@ -77,10 +77,10 @@ class CExactInferenceMethod: public CInference * @param labels labels of the features * @param model likelihood model to use */ - CExactInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + ExactInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CExactInferenceMethod(); + virtual ~ExactInferenceMethod(); /** return what type of inference we are * @@ -97,9 +97,9 @@ class CExactInferenceMethod: public CInference /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CExactInferenceMethod object + * @return casted ExactInferenceMethod object */ - static CExactInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get negative log marginal likelihood * @@ -191,7 +191,7 @@ class CExactInferenceMethod: public CInference * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** check if members of object are valid for inference */ virtual void check_members() const; diff --git a/src/shogun/machine/gp/FITCInferenceMethod.cpp b/src/shogun/machine/gp/FITCInferenceMethod.cpp index cc8a59643db..b9790f1e768 100644 --- a/src/shogun/machine/gp/FITCInferenceMethod.cpp +++ b/src/shogun/machine/gp/FITCInferenceMethod.cpp @@ -39,28 +39,28 @@ using namespace shogun; using namespace Eigen; -CFITCInferenceMethod::CFITCInferenceMethod() : CSingleFITCInference() +FITCInferenceMethod::FITCInferenceMethod() : SingleFITCInference() { init(); } -CFITCInferenceMethod::CFITCInferenceMethod(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat) - : CSingleFITCInference(kern, feat, m, lab, mod, lat) +FITCInferenceMethod::FITCInferenceMethod(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod, std::shared_ptr lat) + : SingleFITCInference(kern, feat, m, lab, mod, lat) { init(); } -void CFITCInferenceMethod::init() +void FITCInferenceMethod::init() { } -CFITCInferenceMethod::~CFITCInferenceMethod() +FITCInferenceMethod::~FITCInferenceMethod() { } -void CFITCInferenceMethod::compute_gradient() +void FITCInferenceMethod::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -70,11 +70,11 @@ void CFITCInferenceMethod::compute_gradient() } } -void CFITCInferenceMethod::update() +void FITCInferenceMethod::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_chol(); update_alpha(); m_gradient_update=false; @@ -83,36 +83,35 @@ void CFITCInferenceMethod::update() SG_TRACE("leaving"); } -CFITCInferenceMethod* CFITCInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr FITCInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_FITC_REGRESSION) - error("Provided inference is not of type CFITCInferenceMethod!"); + error("Provided inference is not of type FITCInferenceMethod!"); - SG_REF(inference); - return (CFITCInferenceMethod*)inference; + return inference->as(); } -void CFITCInferenceMethod::check_members() const +void FITCInferenceMethod::check_members() const { - CSingleFITCInference::check_members(); + SingleFITCInference::check_members(); require(m_model->get_model_type()==LT_GAUSSIAN, "FITC inference method can only use Gaussian likelihood function"); require(m_labels->get_label_type()==LT_REGRESSION, "Labels must be type " - "of CRegressionLabels"); + "of RegressionLabels"); } -SGVector CFITCInferenceMethod::get_diagonal_vector() +SGVector FITCInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // compute diagonal vector: sW=1/sigma @@ -122,7 +121,7 @@ SGVector CFITCInferenceMethod::get_diagonal_vector() return result; } -float64_t CFITCInferenceMethod::get_negative_log_marginal_likelihood() +float64_t FITCInferenceMethod::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); @@ -141,18 +140,18 @@ float64_t CFITCInferenceMethod::get_negative_log_marginal_likelihood() float64_t result = eigen_chol_utr.diagonal().array().log().sum() + (-eigen_t.array().log().sum() + eigen_r.dot(eigen_r) - - eigen_be.dot(eigen_be) + m_ktrtr_diag.vlen * std::log(2 * CMath::PI)) / + eigen_be.dot(eigen_be) + m_ktrtr_diag.vlen * std::log(2 * Math::PI)) / 2.0; return result; } -void CFITCInferenceMethod::update_chol() +void FITCInferenceMethod::update_chol() { //time complexits O(m^2*n) // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); // eigen3 representation of covariance matrix of inducing features (m_kuu) @@ -192,7 +191,7 @@ void CFITCInferenceMethod::update_chol() //g_sn2 = diagK + sn2 - sum(V.*V,1)'; % g + sn2 = diag(K) + sn2 - diag(Q) eigen_t = eigen_ktrtr_diag * std::exp(m_log_scale * 2.0) + - CMath::sq(sigma) * VectorXd::Ones(m_t.vlen) - + Math::sq(sigma) * VectorXd::Ones(m_t.vlen) - (V.cwiseProduct(V)).colwise().sum().adjoint(); eigen_t=MatrixXd::Ones(eigen_t.rows(),1).cwiseQuotient(eigen_t); @@ -209,7 +208,7 @@ void CFITCInferenceMethod::update_chol() eigen_chol_utr=Lu.matrixU(); // create eigen representation of labels and mean vectors - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=regression_labels(m_labels)->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); @@ -246,7 +245,7 @@ void CFITCInferenceMethod::update_chol() eigen_chol=eigen_prod.triangularView().solve(eigen_chol)-iKuu; } -void CFITCInferenceMethod::update_alpha() +void FITCInferenceMethod::update_alpha() { //time complexity O(m^2) since triangular.solve is O(m^2) Map eigen_chol_uu(m_chol_uu.matrix, m_chol_uu.num_rows, @@ -265,7 +264,7 @@ void CFITCInferenceMethod::update_alpha() eigen_alpha=eigen_chol_uu.triangularView().solve(eigen_alpha); } -void CFITCInferenceMethod::update_deriv() +void FITCInferenceMethod::update_deriv() { //time complexits O(m^2*n) @@ -279,7 +278,7 @@ void CFITCInferenceMethod::update_deriv() Map eigen_be(m_be.vector, m_be.vlen); // get and create eigen representation of labels - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=regression_labels(m_labels)->get_labels(); Map eigen_y(y.vector, y.vlen); // get and create eigen representation of mean vector @@ -328,7 +327,7 @@ void CFITCInferenceMethod::update_deriv() } -SGVector CFITCInferenceMethod::get_posterior_mean() +SGVector FITCInferenceMethod::get_posterior_mean() { compute_gradient(); @@ -339,14 +338,14 @@ SGVector CFITCInferenceMethod::get_posterior_mean() //true posterior mean with equivalent FITC prior //time complexity of the following operations is O(n) Map eigen_al(m_al.vector, m_al.vlen); - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=((RegressionLabels*) m_labels)->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); - CGaussianLikelihood* lik=CGaussianLikelihood::obtain_from_generic(m_model); + auto lik=GaussianLikelihood::obtain_from_generic(m_model); float64_t sigma=lik->get_sigma(); SG_UNREF(lik); - eigen_mu=(eigen_y-eigen_m)-eigen_al*CMath::sq(sigma); + eigen_mu=(eigen_y-eigen_m)-eigen_al*Math::sq(sigma); */ //FITC approximated posterior mean @@ -358,7 +357,7 @@ SGVector CFITCInferenceMethod::get_posterior_mean() return SGVector(m_mu); } -SGMatrix CFITCInferenceMethod::get_posterior_covariance() +SGMatrix FITCInferenceMethod::get_posterior_covariance() { compute_gradient(); @@ -373,17 +372,17 @@ SGMatrix CFITCInferenceMethod::get_posterior_covariance() /* //true posterior mean with equivalent FITC prior - CGaussianLikelihood* lik=CGaussianLikelihood::obtain_from_generic(m_model); + auto lik=GaussianLikelihood::obtain_from_generic(m_model); float64_t sigma=lik->get_sigma(); SG_UNREF(lik); Map eigen_t(m_t.vector, m_t.vlen); - VectorXd diag_part=CMath::sq(sigma)*eigen_t; + VectorXd diag_part=Math::sq(sigma)*eigen_t; // diag(sigma2/dg)*V'*(Lu\eye(n)) MatrixXd part1=diag_part.asDiagonal()*eigen_V.adjoint()* (eigen_Lu.triangularView().solve(MatrixXd::Identity( m_kuu.num_rows, m_kuu.num_cols))); eigen_Sigma=part1*part1.adjoint(); - VectorXd part2=(VectorXd::Ones(m_t.vlen)-diag_part)*CMath::sq(sigma); + VectorXd part2=(VectorXd::Ones(m_t.vlen)-diag_part)*Math::sq(sigma); eigen_Sigma+=part2.asDiagonal(); */ @@ -400,7 +399,7 @@ SGMatrix CFITCInferenceMethod::get_posterior_covariance() return SGMatrix(m_Sigma); } -SGVector CFITCInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector FITCInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { //time complexity O(m*n) @@ -416,20 +415,20 @@ SGVector CFITCInferenceMethod::get_derivative_wrt_likelihood_model( Map eigen_B(m_B.matrix, m_B.num_rows, m_B.num_cols); // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); SGVector result(1); //diag_dK = 1./g_sn2 - sum(W.*W,1)' - al.*al; % diag(dnlZ/dK) //dnlZ.lik = sn2*sum(diag_dK) without noise term - result[0]=CMath::sq(sigma)*(VectorXd::Ones(m_t.vlen).cwiseProduct( + result[0]=Math::sq(sigma)*(VectorXd::Ones(m_t.vlen).cwiseProduct( eigen_t).sum()-eigen_W.cwiseProduct(eigen_W).sum()-eigen_al.dot(eigen_al)); return result; } -void CFITCInferenceMethod::register_minimizer(Minimizer* minimizer) +void FITCInferenceMethod::register_minimizer(std::shared_ptr minimizer) { io::warn("The method does not require a minimizer. The provided minimizer will not be used."); } diff --git a/src/shogun/machine/gp/FITCInferenceMethod.h b/src/shogun/machine/gp/FITCInferenceMethod.h index e861c57d370..7465bfd300f 100644 --- a/src/shogun/machine/gp/FITCInferenceMethod.h +++ b/src/shogun/machine/gp/FITCInferenceMethod.h @@ -31,19 +31,19 @@ namespace shogun * (the time complexity is computed based on the assumption m < n) * * Warning: the time complexity of method, - * CSingleFITCInference::get_derivative_wrt_kernel(const TParameter* param), + * SingleFITCInference::get_derivative_wrt_kernel(const TParameter* param), * depends on the implementation of virtual kernel method, - * CKernel::get_parameter_gradient_diagonal(param, i). + * Kernel::get_parameter_gradient_diagonal(param, i). * The default time complexity of the kernel method can be O(n^2) * * Warning: the the time complexity increases from O(m^2*n) to O(n^2*m) if method - * CFITCInferenceMethod::get_posterior_covariance() is called + * FITCInferenceMethod::get_posterior_covariance() is called */ -class CFITCInferenceMethod: public CSingleFITCInference +class FITCInferenceMethod: public SingleFITCInference { public: /** default constructor */ - CFITCInferenceMethod(); + FITCInferenceMethod(); /** constructor * @@ -54,11 +54,11 @@ class CFITCInferenceMethod: public CSingleFITCInference * @param model likelihood model to use * @param inducing_features features to use */ - CFITCInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model, - CFeatures* inducing_features); + FITCInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model, + std::shared_ptr inducing_features); - virtual ~CFITCInferenceMethod(); + virtual ~FITCInferenceMethod(); /** returns the name of the inference method * @@ -75,9 +75,9 @@ class CFITCInferenceMethod: public CSingleFITCInference /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CFITCInferenceMethod object + * @return casted FITCInferenceMethod object */ - static CFITCInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get negative log marginal likelihood * @@ -158,7 +158,7 @@ class CFITCInferenceMethod: public CSingleFITCInference * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** check if members of object are valid for inference */ virtual void check_members() const; diff --git a/src/shogun/machine/gp/GaussianARDSparseKernel.cpp b/src/shogun/machine/gp/GaussianARDSparseKernel.cpp index 9578e0d98e4..b0942875be7 100644 --- a/src/shogun/machine/gp/GaussianARDSparseKernel.cpp +++ b/src/shogun/machine/gp/GaussianARDSparseKernel.cpp @@ -34,56 +34,56 @@ using namespace shogun; -CGaussianARDSparseKernel::CGaussianARDSparseKernel() : CGaussianARDKernel() +GaussianARDSparseKernel::GaussianARDSparseKernel() : GaussianARDKernel() { initialize_sparse_kernel(); } -void CGaussianARDSparseKernel::initialize_sparse_kernel() +void GaussianARDSparseKernel::initialize_sparse_kernel() { } -CGaussianARDSparseKernel::~CGaussianARDSparseKernel() +GaussianARDSparseKernel::~GaussianARDSparseKernel() { } using namespace Eigen; -CGaussianARDSparseKernel::CGaussianARDSparseKernel(int32_t size) - : CGaussianARDKernel(size) +GaussianARDSparseKernel::GaussianARDSparseKernel(int32_t size) + : GaussianARDKernel(size) { initialize_sparse_kernel(); } -CGaussianARDSparseKernel::CGaussianARDSparseKernel(CDotFeatures* l, - CDotFeatures* r, int32_t size) - : CGaussianARDKernel(l, r, size) +GaussianARDSparseKernel::GaussianARDSparseKernel(std::shared_ptr l, + std::shared_ptr r, int32_t size) + : GaussianARDKernel(l, r, size) { initialize_sparse_kernel(); } -CGaussianARDSparseKernel* CGaussianARDSparseKernel::obtain_from_generic(CKernel* kernel) +std::shared_ptr GaussianARDSparseKernel::obtain_from_generic(std::shared_ptr kernel) { if (kernel->get_kernel_type()!=K_GAUSSIANARDSPARSE) { - error("Provided kernel is not of type CGaussianARDSparseKernel!"); + error("Provided kernel is not of type GaussianARDSparseKernel!"); } /* since an additional reference is returned */ - SG_REF(kernel); - return (CGaussianARDSparseKernel*)kernel; + + return kernel->as(); } -SGVector CGaussianARDSparseKernel::get_parameter_gradient_diagonal( +SGVector GaussianARDSparseKernel::get_parameter_gradient_diagonal( const TParameter* param, index_t index) { require(param, "Param not set"); if (!strcmp(param->m_name, "inducing_features")) - return CKernel::get_parameter_gradient_diagonal(param, index); + return Kernel::get_parameter_gradient_diagonal(param, index); else - return CGaussianARDKernel::get_parameter_gradient_diagonal(param, index); + return GaussianARDKernel::get_parameter_gradient_diagonal(param, index); } -SGMatrix CGaussianARDSparseKernel::get_parameter_gradient( +SGMatrix GaussianARDSparseKernel::get_parameter_gradient( const TParameter* param, index_t index) { require(param, "Param not set"); @@ -94,8 +94,8 @@ SGMatrix CGaussianARDSparseKernel::get_parameter_gradient( require(index>=0 && index left_vec=get_feature_vector(idx_l, lhs); SGMatrix res(left_vec.vlen, num_rhs); @@ -142,6 +142,6 @@ SGMatrix CGaussianARDSparseKernel::get_parameter_gradient( } else { - return CGaussianARDKernel::get_parameter_gradient(param, index); + return GaussianARDKernel::get_parameter_gradient(param, index); } } diff --git a/src/shogun/machine/gp/GaussianARDSparseKernel.h b/src/shogun/machine/gp/GaussianARDSparseKernel.h index c01f485abe1..b06dd444ec9 100644 --- a/src/shogun/machine/gp/GaussianARDSparseKernel.h +++ b/src/shogun/machine/gp/GaussianARDSparseKernel.h @@ -47,11 +47,11 @@ namespace shogun * which are not hyper-parameters of the kernel. * * */ -class CGaussianARDSparseKernel: public CGaussianARDKernel +class GaussianARDSparseKernel: public GaussianARDKernel { public: /** default constructor */ - CGaussianARDSparseKernel(); + GaussianARDSparseKernel(); /** return what type of kernel we are * @@ -66,7 +66,7 @@ class CGaussianARDSparseKernel: public CGaussianARDKernel virtual const char* get_name() const { return "GaussianARDSparseKernel"; } /** destructor */ - virtual ~CGaussianARDSparseKernel(); + virtual ~GaussianARDSparseKernel(); private: void initialize_sparse_kernel(); @@ -76,7 +76,7 @@ class CGaussianARDSparseKernel: public CGaussianARDKernel * * @param size cache size */ - CGaussianARDSparseKernel(int32_t size); + GaussianARDSparseKernel(int32_t size); /** constructor * @@ -84,14 +84,14 @@ class CGaussianARDSparseKernel: public CGaussianARDKernel * @param r features of right-hand side * @param size cache size */ - CGaussianARDSparseKernel(CDotFeatures* l, CDotFeatures* r, + GaussianARDSparseKernel(std::shared_ptr l, std::shared_ptr r, int32_t size=10); - /** @param kernel is casted to CGaussianARDSparseKernel, error if not possible + /** @param kernel is casted to GaussianARDSparseKernel, error if not possible * is SG_REF'ed - * @return casted CGaussianARDSparseKernel object + * @return casted GaussianARDSparseKernel object */ - static CGaussianARDSparseKernel* obtain_from_generic(CKernel* kernel); + static std::shared_ptr obtain_from_generic(std::shared_ptr kernel); /** return derivative with respect to specified parameter * diff --git a/src/shogun/machine/gp/GaussianLikelihood.cpp b/src/shogun/machine/gp/GaussianLikelihood.cpp index 836eb2361b1..a78fcde1771 100644 --- a/src/shogun/machine/gp/GaussianLikelihood.cpp +++ b/src/shogun/machine/gp/GaussianLikelihood.cpp @@ -38,47 +38,46 @@ using namespace shogun; using namespace Eigen; -CGaussianLikelihood::CGaussianLikelihood() : CLikelihoodModel() +GaussianLikelihood::GaussianLikelihood() : LikelihoodModel() { init(); } -CGaussianLikelihood::CGaussianLikelihood(float64_t sigma) : CLikelihoodModel() +GaussianLikelihood::GaussianLikelihood(float64_t sigma) : LikelihoodModel() { init(); set_sigma(sigma); } -void CGaussianLikelihood::init() +void GaussianLikelihood::init() { m_log_sigma=0.0; SG_ADD(&m_log_sigma, "log_sigma", "Observation noise in log domain", ParameterProperties::HYPER | ParameterProperties::GRADIENT); } -CGaussianLikelihood::~CGaussianLikelihood() +GaussianLikelihood::~GaussianLikelihood() { } -CGaussianLikelihood* CGaussianLikelihood::obtain_from_generic( - CLikelihoodModel* lik) +std::shared_ptr GaussianLikelihood::obtain_from_generic( + std::shared_ptr lik) { ASSERT(lik!=NULL); if (lik->get_model_type()!=LT_GAUSSIAN) - error("Provided likelihood is not of type CGaussianLikelihood!"); + error("Provided likelihood is not of type GaussianLikelihood!"); - SG_REF(lik); - return (CGaussianLikelihood*)lik; + return lik->as(); } -SGVector CGaussianLikelihood::get_predictive_means( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector GaussianLikelihood::get_predictive_means( + SGVector mu, SGVector s2, std::shared_ptr lab) const { return SGVector(mu); } -SGVector CGaussianLikelihood::get_predictive_variances( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector GaussianLikelihood::get_predictive_variances( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector result(s2); Map eigen_result(result.vector, result.vlen); @@ -88,13 +87,13 @@ SGVector CGaussianLikelihood::get_predictive_variances( return result; } -SGVector CGaussianLikelihood::get_log_probability_f(const CLabels* lab, +SGVector GaussianLikelihood::get_log_probability_f(std::shared_ptr lab, SGVector func) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -103,7 +102,7 @@ SGVector CGaussianLikelihood::get_log_probability_f(const CLabels* la SGVector result(func.vlen); Map eigen_result(result.vector, result.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute log probability: lp=-(y-f).^2./sigma^2/2-log(2*pi*sigma^2)/2 @@ -111,18 +110,18 @@ SGVector CGaussianLikelihood::get_log_probability_f(const CLabels* la eigen_result = -eigen_result.cwiseProduct(eigen_result) / (2.0 * std::exp(m_log_sigma * 2.0)) - VectorXd::Ones(result.vlen) * - log(2.0 * CMath::PI * std::exp(m_log_sigma * 2.0)) / 2.0; + log(2.0 * Math::PI * std::exp(m_log_sigma * 2.0)) / 2.0; return result; } -SGVector CGaussianLikelihood::get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const +SGVector GaussianLikelihood::get_log_probability_derivative_f( + std::shared_ptr lab, SGVector func, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); require(i>=1 && i<=3, "Index for derivative should be 1, 2 or 3"); @@ -132,7 +131,7 @@ SGVector CGaussianLikelihood::get_log_probability_derivative_f( SGVector result(func.vlen); Map eigen_result(result.vector, result.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // set result=y-f @@ -150,12 +149,12 @@ SGVector CGaussianLikelihood::get_log_probability_derivative_f( return result; } -SGVector CGaussianLikelihood::get_first_derivative(const CLabels* lab, +SGVector GaussianLikelihood::get_first_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -167,7 +166,7 @@ SGVector CGaussianLikelihood::get_first_derivative(const CLabels* lab if (strcmp(param->m_name, "log_sigma")) return SGVector(); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute derivative of log probability wrt log_sigma: @@ -181,12 +180,12 @@ SGVector CGaussianLikelihood::get_first_derivative(const CLabels* lab return result; } -SGVector CGaussianLikelihood::get_second_derivative(const CLabels* lab, +SGVector GaussianLikelihood::get_second_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -198,7 +197,7 @@ SGVector CGaussianLikelihood::get_second_derivative(const CLabels* la SGVector result(func.vlen); Map eigen_result(result.vector, result.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute derivative of (the first log_sigma derivative of log probability) wrt f: @@ -209,12 +208,12 @@ SGVector CGaussianLikelihood::get_second_derivative(const CLabels* la return result; } -SGVector CGaussianLikelihood::get_third_derivative(const CLabels* lab, +SGVector GaussianLikelihood::get_third_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -235,8 +234,8 @@ SGVector CGaussianLikelihood::get_third_derivative(const CLabels* lab return result; } -SGVector CGaussianLikelihood::get_log_zeroth_moments( - SGVector mu, SGVector s2, const CLabels *lab) const +SGVector GaussianLikelihood::get_log_zeroth_moments( + SGVector mu, SGVector s2, std::shared_ptrlab) const { SGVector y; @@ -247,9 +246,9 @@ SGVector CGaussianLikelihood::get_log_zeroth_moments( "variances ({}) and number of labels ({}) should be the same", mu.vlen, s2.vlen, lab->get_num_labels()); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); - y=((CRegressionLabels*)lab)->get_labels(); + y=lab->as()->get_labels(); } else { @@ -272,13 +271,13 @@ SGVector CGaussianLikelihood::get_log_zeroth_moments( // compule lZ=-(y-mu).^2./(sn2+s2)/2-log(2*pi*(sn2+s2))/2 eigen_s2 = eigen_s2.array() + std::exp(m_log_sigma * 2.0); eigen_result=-(eigen_y-eigen_mu).array().square()/(2.0*eigen_s2.array())- - (2.0*CMath::PI*eigen_s2.array()).log()/2.0; + (2.0*Math::PI*eigen_s2.array()).log()/2.0; return result; } -float64_t CGaussianLikelihood::get_first_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t GaussianLikelihood::get_first_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -288,9 +287,9 @@ float64_t CGaussianLikelihood::get_first_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); // compute 1st moment float64_t Ex = @@ -299,8 +298,8 @@ float64_t CGaussianLikelihood::get_first_moment(SGVector mu, return Ex; } -float64_t CGaussianLikelihood::get_second_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t GaussianLikelihood::get_second_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -310,11 +309,11 @@ float64_t CGaussianLikelihood::get_second_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); // compute 2nd moment float64_t Var = - s2[i] - CMath::sq(s2[i]) / (std::exp(m_log_sigma * 2.0) + s2[i]); + s2[i] - Math::sq(s2[i]) / (std::exp(m_log_sigma * 2.0) + s2[i]); return Var; } diff --git a/src/shogun/machine/gp/GaussianLikelihood.h b/src/shogun/machine/gp/GaussianLikelihood.h index 2ec21c714c4..419e149d38a 100644 --- a/src/shogun/machine/gp/GaussianLikelihood.h +++ b/src/shogun/machine/gp/GaussianLikelihood.h @@ -51,19 +51,19 @@ namespace shogun * The hyperparameter of the Gaussian likelihood model is standard deviation: * \f$\sigma\f$. */ -class CGaussianLikelihood: public CLikelihoodModel +class GaussianLikelihood: public LikelihoodModel { public: /** default constructor */ - CGaussianLikelihood(); + GaussianLikelihood(); /** constructor * * @param sigma observation noise */ - CGaussianLikelihood(float64_t sigma); + GaussianLikelihood(float64_t sigma); - virtual ~CGaussianLikelihood(); + virtual ~GaussianLikelihood(); /** returns the name of the likelihood model * @@ -94,9 +94,9 @@ class CGaussianLikelihood: public CLikelihoodModel /** helper method used to specialize a base class instance * * @param lik likelihood model - * @return casted CGaussianLikelihood object + * @return casted GaussianLikelihood object */ - static CGaussianLikelihood* obtain_from_generic(CLikelihoodModel* lik); + static std::shared_ptr obtain_from_generic(std::shared_ptr lik); /** returns mean of the predictive marginal \f$p(y_*|X,y,x_*)\f$. * @@ -114,7 +114,7 @@ class CGaussianLikelihood: public CLikelihoodModel * @return final means evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$. * @@ -131,7 +131,7 @@ class CGaussianLikelihood: public CLikelihoodModel * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** get model type * @@ -150,7 +150,7 @@ class CGaussianLikelihood: public CLikelihoodModel * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const; /** get derivative of log likelihood \f$log(P(y|f))\f$ with respect to @@ -164,7 +164,7 @@ class CGaussianLikelihood: public CLikelihoodModel * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const; + std::shared_ptr lab, SGVector func, index_t i) const; /** get derivative of log likelihood \f$log(P(y|f))\f$ with respect to given * parameter @@ -175,7 +175,7 @@ class CGaussianLikelihood: public CLikelihoodModel * * @return derivative */ - virtual SGVector get_first_derivative(const CLabels* lab, + virtual SGVector get_first_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** get derivative of the first derivative of log likelihood with respect to @@ -188,7 +188,7 @@ class CGaussianLikelihood: public CLikelihoodModel * * @return derivative */ - virtual SGVector get_second_derivative(const CLabels* lab, + virtual SGVector get_second_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** get derivative of the second derivative of log likelihood with respect @@ -201,7 +201,7 @@ class CGaussianLikelihood: public CLikelihoodModel * * @return derivative */ - virtual SGVector get_third_derivative(const CLabels* lab, + virtual SGVector get_third_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** returns the zeroth moment of a given (unnormalized) probability @@ -221,7 +221,7 @@ class CGaussianLikelihood: public CLikelihoodModel * @return log zeroth moments \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -238,7 +238,7 @@ class CGaussianLikelihood: public CLikelihoodModel * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** returns the second central moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -255,7 +255,7 @@ class CGaussianLikelihood: public CLikelihoodModel * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** return whether Gaussian likelihood function supports regression * diff --git a/src/shogun/machine/gp/Inference.cpp b/src/shogun/machine/gp/Inference.cpp index f37f57ceb51..1d22134df02 100644 --- a/src/shogun/machine/gp/Inference.cpp +++ b/src/shogun/machine/gp/Inference.cpp @@ -40,23 +40,23 @@ using namespace shogun; -CInference::CInference() +Inference::Inference() { init(); } -float64_t CInference::get_scale() const +float64_t Inference::get_scale() const { return std::exp(m_log_scale); } -void CInference::set_scale(float64_t scale) +void Inference::set_scale(float64_t scale) { require(scale>0, "Scale ({}) must be positive", scale); m_log_scale = std::log(scale); } -SGMatrix CInference::get_multiclass_E() +SGMatrix Inference::get_multiclass_E() { if (parameter_hash_changed()) update(); @@ -64,8 +64,8 @@ SGMatrix CInference::get_multiclass_E() return SGMatrix(m_E); } -CInference::CInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model) +Inference::Inference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model) { init(); @@ -76,17 +76,17 @@ CInference::CInference(CKernel* kernel, CFeatures* features, set_mean(mean); } -CInference::~CInference() +Inference::~Inference() { - SG_UNREF(m_kernel); - SG_UNREF(m_features); - SG_UNREF(m_labels); - SG_UNREF(m_model); - SG_UNREF(m_mean); - SG_UNREF(m_minimizer); + + + + + + } -void CInference::init() +void Inference::init() { SG_ADD(&m_kernel, "kernel", "Kernel", ParameterProperties::HYPER); SG_ADD(&m_log_scale, "log_scale", "Kernel log scale", ParameterProperties::HYPER | ParameterProperties::GRADIENT); @@ -95,7 +95,7 @@ void CInference::init() SG_ADD(&m_labels, "labels", "Labels"); SG_ADD(&m_features, "features", "Features"); SG_ADD(&m_gradient_update, "gradient_update", "Whether gradients are updated"); - + m_kernel=NULL; m_model=NULL; @@ -106,24 +106,24 @@ void CInference::init() m_gradient_update=false; m_minimizer=NULL; - SG_ADD((CSGObject**)&m_minimizer, "Inference__m_minimizer", "minimizer in Inference"); + SG_ADD((std::shared_ptr*)&m_minimizer, "Inference__m_minimizer", "minimizer in Inference"); SG_ADD(&m_alpha, "alpha", "alpha vector used in process mean calculation"); SG_ADD(&m_L, "L", "upper triangular factor of Cholesky decomposition"); SG_ADD(&m_E, "E", "the matrix used for multi classification"); } -void CInference::register_minimizer(Minimizer* minimizer) +void Inference::register_minimizer(std::shared_ptr minimizer) { require(minimizer, "Minimizer must set"); if(minimizer!=m_minimizer) { - SG_REF(minimizer); - SG_UNREF(m_minimizer); + + m_minimizer=minimizer; } } -float64_t CInference::get_marginal_likelihood_estimate( +float64_t Inference::get_marginal_likelihood_estimate( int32_t num_importance_samples, float64_t ridge_size) { /* sample from Gaussian approximation to q(f|y) */ @@ -135,7 +135,7 @@ float64_t CInference::get_marginal_likelihood_estimate( SGVector mean=get_posterior_mean(); - CGaussianDistribution* post_approx=new CGaussianDistribution(mean, cov); + auto post_approx=std::make_shared(mean, cov); SGMatrix samples=post_approx->sample(num_importance_samples); /* evaluate q(f^i|y), p(f^i|\theta), p(y|f^i), i.e., @@ -145,7 +145,7 @@ float64_t CInference::get_marginal_likelihood_estimate( SGVector log_pdf_post_approx=post_approx->log_pdf_multiple(samples); /* dont need gaussian anymore, free memory */ - SG_UNREF(post_approx); + post_approx=NULL; /* log pdf p(f^i|\theta) and free memory afterwise. Scale kernel before */ @@ -159,10 +159,10 @@ float64_t CInference::get_marginal_likelihood_estimate( for (index_t i=0; i( m_mean->get_mean_vector(m_features), scaled_kernel); SGVector log_pdf_prior=prior->log_pdf_multiple(samples); - SG_UNREF(prior); + prior=NULL; /* p(y|f^i) */ @@ -178,11 +178,11 @@ float64_t CInference::get_marginal_likelihood_estimate( sum[i]=log_likelihood[i]+log_pdf_prior[i]-log_pdf_post_approx[i]; /* use log-sum-exp (in particular, log-mean-exp) trick to combine values */ - return CMath::log_mean_exp(sum); + return Math::log_mean_exp(sum); } -CMap >* CInference:: -get_negative_log_marginal_likelihood_derivatives(CMap* params) +std::shared_ptr >> Inference:: +get_negative_log_marginal_likelihood_derivatives(std::shared_ptr> params) { require(params->get_num_elements(), "Number of parameters should be greater " "than zero"); @@ -193,15 +193,15 @@ get_negative_log_marginal_likelihood_derivatives(CMap* const index_t num_deriv=params->get_num_elements(); // create map of derivatives - CMap >* result= - new CMap >(num_deriv, num_deriv); + auto result= + std::make_shared>>(num_deriv, num_deriv); + - SG_REF(result); #pragma omp parallel for for (index_t i=0; i* node=params->get_node_ptr(i); + CMapNode* node=params->get_node_ptr(i); SGVector gradient; if(node->data == this) @@ -209,17 +209,17 @@ get_negative_log_marginal_likelihood_derivatives(CMap* // try to find dervative wrt InferenceMethod.parameter gradient=this->get_derivative_wrt_inference_method(node->key); } - else if (node->data == this->m_model) + else if (node->data == this->m_model.get()) { // try to find derivative wrt LikelihoodModel.parameter gradient=this->get_derivative_wrt_likelihood_model(node->key); } - else if (node->data ==this->m_kernel) + else if (node->data ==this->m_kernel.get()) { // try to find derivative wrt Kernel.parameter gradient=this->get_derivative_wrt_kernel(node->key); } - else if (node->data ==this->m_mean) + else if (node->data ==this->m_mean.get()) { // try to find derivative wrt MeanFunction.parameter gradient=this->get_derivative_wrt_mean(node->key); @@ -239,13 +239,13 @@ get_negative_log_marginal_likelihood_derivatives(CMap* return result; } -void CInference::update() +void Inference::update() { check_members(); update_train_kernel(); } -void CInference::check_members() const +void Inference::check_members() const { require(m_features, "Training features should not be NULL"); require(m_features->get_num_vectors(), @@ -260,13 +260,13 @@ void CInference::check_members() const require(m_mean, "Mean function should not be NULL"); } -void CInference::update_train_kernel() +void Inference::update_train_kernel() { m_kernel->init(m_features, m_features); m_ktrtr=m_kernel->get_kernel_matrix(); } -void CInference::compute_gradient() +void Inference::compute_gradient() { if (parameter_hash_changed()) update(); diff --git a/src/shogun/machine/gp/Inference.h b/src/shogun/machine/gp/Inference.h index 6b691738843..912d1f45041 100644 --- a/src/shogun/machine/gp/Inference.h +++ b/src/shogun/machine/gp/Inference.h @@ -78,11 +78,11 @@ enum EInferenceType * any implemented approximation. See * CInference::get_marginal_likelihood_estimate. */ -class CInference : public CDifferentiableFunction +class Inference : public DifferentiableFunction { public: /** default constructor */ - CInference(); + Inference(); /** constructor * @@ -92,10 +92,10 @@ class CInference : public CDifferentiableFunction * @param labels labels of the features * @param model likelihood model to use */ - CInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + Inference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CInference(); + virtual ~Inference(); /** return what type of inference we are, e.g. exact, FITC, Laplace, etc. * @@ -168,9 +168,9 @@ class CInference : public CDifferentiableFunction * where \f$y\f$ are the labels, \f$X\f$ are the features, and \f$\theta\f$ * represent hyperparameters. */ - virtual CMap >* - get_negative_log_marginal_likelihood_derivatives(CMap* parameters); + virtual std::shared_ptr >> + get_negative_log_marginal_likelihood_derivatives( + std::shared_ptr> parameters); /** get alpha vector * @@ -242,8 +242,8 @@ class CInference : public CDifferentiableFunction * @return map of gradient. Keys are names of parameters, values are values * of derivative with respect to that parameter. */ - virtual CMap >* get_gradient( - CMap* parameters) + virtual std::shared_ptr >> get_gradient( + std::shared_ptr> parameters) { return get_negative_log_marginal_likelihood_derivatives(parameters); } @@ -263,16 +263,16 @@ class CInference : public CDifferentiableFunction * * @return features */ - virtual CFeatures* get_features() { SG_REF(m_features); return m_features; } + virtual std::shared_ptr get_features() { return m_features; } /** set features * * @param feat features to set */ - virtual void set_features(CFeatures* feat) + virtual void set_features(std::shared_ptr feat) { - SG_REF(feat); - SG_UNREF(m_features); + + m_features=feat; } @@ -280,16 +280,16 @@ class CInference : public CDifferentiableFunction * * @return kernel */ - virtual CKernel* get_kernel() { SG_REF(m_kernel); return m_kernel; } + virtual std::shared_ptr get_kernel() { return m_kernel; } /** set kernel * * @param kern kernel to set */ - virtual void set_kernel(CKernel* kern) + virtual void set_kernel(std::shared_ptr kern) { - SG_REF(kern); - SG_UNREF(m_kernel); + + m_kernel=kern; } @@ -297,16 +297,16 @@ class CInference : public CDifferentiableFunction * * @return mean */ - virtual CMeanFunction* get_mean() { SG_REF(m_mean); return m_mean; } + virtual std::shared_ptr get_mean() { return m_mean; } /** set mean * * @param m mean function to set */ - virtual void set_mean(CMeanFunction* m) + virtual void set_mean(std::shared_ptr m) { - SG_REF(m); - SG_UNREF(m_mean); + + m_mean=m; } @@ -314,16 +314,16 @@ class CInference : public CDifferentiableFunction * * @return labels */ - virtual CLabels* get_labels() { SG_REF(m_labels); return m_labels; } + virtual std::shared_ptr get_labels() { return m_labels; } /** set labels * * @param lab label to set */ - virtual void set_labels(CLabels* lab) + virtual void set_labels(std::shared_ptr lab) { - SG_REF(lab); - SG_UNREF(m_labels); + + m_labels=lab; } @@ -331,16 +331,16 @@ class CInference : public CDifferentiableFunction * * @return likelihood */ - CLikelihoodModel* get_model() { SG_REF(m_model); return m_model; } + std::shared_ptr get_model() { return m_model; } /** set likelihood model * * @param mod model to set */ - virtual void set_model(CLikelihoodModel* mod) + virtual void set_model(std::shared_ptr mod) { - SG_REF(mod); - SG_UNREF(m_model); + + m_model=mod; } @@ -391,7 +391,7 @@ class CInference : public CDifferentiableFunction * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** check if members of object are valid for inference */ virtual void check_members() const; @@ -458,22 +458,22 @@ class CInference : public CDifferentiableFunction protected: /** minimizer */ - Minimizer* m_minimizer; + std::shared_ptr m_minimizer; /** covariance function */ - CKernel* m_kernel; + std::shared_ptr m_kernel; /** mean function */ - CMeanFunction* m_mean; + std::shared_ptr m_mean; /** likelihood function to use */ - CLikelihoodModel* m_model; + std::shared_ptr m_model; /** features to use */ - CFeatures* m_features; + std::shared_ptr m_features; /** labels of features */ - CLabels* m_labels; + std::shared_ptr m_labels; /** alpha vector used in process mean calculation */ SGVector m_alpha; diff --git a/src/shogun/machine/gp/KLCholeskyInferenceMethod.cpp b/src/shogun/machine/gp/KLCholeskyInferenceMethod.cpp index 99e8f7bbc91..451a819a78f 100644 --- a/src/shogun/machine/gp/KLCholeskyInferenceMethod.cpp +++ b/src/shogun/machine/gp/KLCholeskyInferenceMethod.cpp @@ -51,19 +51,19 @@ using namespace Eigen; namespace shogun { -CKLCholeskyInferenceMethod::CKLCholeskyInferenceMethod() : CKLLowerTriangularInference() +KLCholeskyInferenceMethod::KLCholeskyInferenceMethod() : KLLowerTriangularInference() { init(); } -CKLCholeskyInferenceMethod::CKLCholeskyInferenceMethod(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CKLLowerTriangularInference(kern, feat, m, lab, mod) +KLCholeskyInferenceMethod::KLCholeskyInferenceMethod(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : KLLowerTriangularInference(kern, feat, m, lab, mod) { init(); } -void CKLCholeskyInferenceMethod::init() +void KLCholeskyInferenceMethod::init() { SG_ADD(&m_C, "C", "The Cholesky represention of the variational co-variance matrix"); @@ -71,20 +71,19 @@ void CKLCholeskyInferenceMethod::init() " The K^{-1}C matrix"); } -CKLCholeskyInferenceMethod* CKLCholeskyInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr KLCholeskyInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_KL_CHOLESKY) - error("Provided inference is not of type CKLCholeskyInferenceMethod!"); + error("Provided inference is not of type KLCholeskyInferenceMethod!"); - SG_REF(inference); - return (CKLCholeskyInferenceMethod*)inference; + return inference->as(); } -SGVector CKLCholeskyInferenceMethod::get_alpha() +SGVector KLCholeskyInferenceMethod::get_alpha() { /** Note that m_alpha contains not only the alpha vector defined in the reference * but also a vector corresponding to the lower triangular of C @@ -107,11 +106,11 @@ SGVector CKLCholeskyInferenceMethod::get_alpha() return result; } -CKLCholeskyInferenceMethod::~CKLCholeskyInferenceMethod() +KLCholeskyInferenceMethod::~KLCholeskyInferenceMethod() { } -bool CKLCholeskyInferenceMethod::precompute() +bool KLCholeskyInferenceMethod::precompute() { index_t len=m_mean_vec.vlen; Map eigen_mean(m_mean_vec.vector, m_mean_vec.vlen); @@ -128,7 +127,7 @@ bool CKLCholeskyInferenceMethod::precompute() //s2=sum(C.*C,2); eigen_s2=(eigen_C.array()*eigen_C.array()).rowwise().sum().matrix(); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); bool status = lik->set_variational_distribution(m_mu, m_s2, m_labels); if (status) { @@ -138,7 +137,7 @@ bool CKLCholeskyInferenceMethod::precompute() return status; } -void CKLCholeskyInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector gradient) +void KLCholeskyInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector gradient) { require(gradient.vlen==m_alpha.vlen, "The length of gradients ({}) should the same as the length of parameters ({})", @@ -151,12 +150,12 @@ void CKLCholeskyInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector eigen_alpha(m_alpha.vector, len); Map eigen_C_seq(m_alpha.vector+len, m_alpha.vlen-len); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); //[a,df,dV] = a_related2(mu,s2,y,lik); TParameter* s2_param=lik->m_parameters->get_parameter("sigma2"); SGVector dv=lik->get_variational_first_derivative(s2_param); Map eigen_dv(dv.vector, dv.vlen); - + //FIXME TParameter* mu_param=lik->m_parameters->get_parameter("mu"); SGVector df=lik->get_variational_first_derivative(mu_param); Map eigen_df(df.vector, df.vlen); @@ -197,7 +196,7 @@ void CKLCholeskyInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector eigen_alpha(m_alpha.vector, m_mu.vlen); Map eigen_mu(m_mu.vector, m_mu.vlen); @@ -208,7 +207,7 @@ float64_t CKLCholeskyInferenceMethod::get_negative_log_marginal_likelihood_helpe Map eigen_InvK_C(m_InvK_C.matrix, m_InvK_C.num_rows, m_InvK_C.num_cols); Map eigen_C(m_C.matrix, m_C.num_rows, m_C.num_cols); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); float64_t a=SGVector::sum(lik->get_variational_expection()); //float64_t log_det=2.0*log_det(eigen_C)-m_log_det_Kernel; @@ -220,7 +219,7 @@ float64_t CKLCholeskyInferenceMethod::get_negative_log_marginal_likelihood_helpe return result; } -void CKLCholeskyInferenceMethod::update_alpha() +void KLCholeskyInferenceMethod::update_alpha() { Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -237,7 +236,7 @@ void CKLCholeskyInferenceMethod::update_alpha() SGVector s2_tmp(m_s2.vlen); Map eigen_s2(s2_tmp.vector, s2_tmp.vlen); eigen_s2.fill(1.0); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); lik->set_variational_distribution(m_mean_vec, s2_tmp, m_labels); float64_t a=SGVector::sum(lik->get_variational_expection()); MatrixXd inv_K=solve_inverse(MatrixXd::Identity(m_ktrtr.num_rows, m_ktrtr.num_cols)); @@ -275,7 +274,7 @@ void CKLCholeskyInferenceMethod::update_alpha() nlml_new=optimization(); } -void CKLCholeskyInferenceMethod::update_C() +void KLCholeskyInferenceMethod::update_C() { ASSERT(m_C.num_rows == m_C.num_cols); index_t len=m_C.num_rows; @@ -292,7 +291,7 @@ void CKLCholeskyInferenceMethod::update_C() } } -void CKLCholeskyInferenceMethod::get_lower_triangular_vector(SGMatrix square_matrix, +void KLCholeskyInferenceMethod::get_lower_triangular_vector(SGMatrix square_matrix, SGVector target) { ASSERT(square_matrix.num_rows == square_matrix.num_cols); @@ -310,7 +309,7 @@ void CKLCholeskyInferenceMethod::get_lower_triangular_vector(SGMatrix } } -void CKLCholeskyInferenceMethod::update_Sigma() +void KLCholeskyInferenceMethod::update_Sigma() { m_Sigma=SGMatrix(m_mu.vlen, m_mu.vlen); Map eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols); @@ -318,7 +317,7 @@ void CKLCholeskyInferenceMethod::update_Sigma() eigen_Sigma=eigen_C*(eigen_C.transpose()); } -void CKLCholeskyInferenceMethod::update_InvK_Sigma() +void KLCholeskyInferenceMethod::update_InvK_Sigma() { m_InvK_Sigma=SGMatrix(m_ktrtr.num_rows, m_ktrtr.num_cols); Map eigen_InvK_Sigma(m_InvK_Sigma.matrix, m_InvK_Sigma.num_rows, m_InvK_Sigma.num_cols); diff --git a/src/shogun/machine/gp/KLCholeskyInferenceMethod.h b/src/shogun/machine/gp/KLCholeskyInferenceMethod.h index 018cf3b81c0..4ec2b0489d4 100644 --- a/src/shogun/machine/gp/KLCholeskyInferenceMethod.h +++ b/src/shogun/machine/gp/KLCholeskyInferenceMethod.h @@ -69,11 +69,11 @@ namespace shogun * Note that "Cholesky" means Cholesky represention of the variational co-variance matrix * is explicitly used in inference */ -class CKLCholeskyInferenceMethod: public CKLLowerTriangularInference +class KLCholeskyInferenceMethod: public KLLowerTriangularInference { public: /** default constructor */ - CKLCholeskyInferenceMethod(); + KLCholeskyInferenceMethod(); /** constructor * @@ -83,10 +83,10 @@ class CKLCholeskyInferenceMethod: public CKLLowerTriangularInference * @param labels labels of the features * @param model Likelihood model to use */ - CKLCholeskyInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + KLCholeskyInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CKLCholeskyInferenceMethod(); + virtual ~KLCholeskyInferenceMethod(); /** returns the name of the inference method * @@ -103,9 +103,9 @@ class CKLCholeskyInferenceMethod: public CKLLowerTriangularInference /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CKLCholeskyInferenceMethod object + * @return casted KLCholeskyInferenceMethod object */ - static CKLCholeskyInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get alpha vector * diff --git a/src/shogun/machine/gp/KLCovarianceInferenceMethod.cpp b/src/shogun/machine/gp/KLCovarianceInferenceMethod.cpp index 28f5eaba6bd..8735bbb7200 100644 --- a/src/shogun/machine/gp/KLCovarianceInferenceMethod.cpp +++ b/src/shogun/machine/gp/KLCovarianceInferenceMethod.cpp @@ -51,19 +51,19 @@ using namespace Eigen; namespace shogun { -CKLCovarianceInferenceMethod::CKLCovarianceInferenceMethod() : CKLInference() +KLCovarianceInferenceMethod::KLCovarianceInferenceMethod() : KLInference() { init(); } -CKLCovarianceInferenceMethod::CKLCovarianceInferenceMethod(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CKLInference(kern, feat, m, lab, mod) +KLCovarianceInferenceMethod::KLCovarianceInferenceMethod(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : KLInference(kern, feat, m, lab, mod) { init(); } -void CKLCovarianceInferenceMethod::init() +void KLCovarianceInferenceMethod::init() { SG_ADD(&m_V, "V", "V is L'*V=diag(sW)*K"); @@ -80,7 +80,7 @@ void CKLCovarianceInferenceMethod::init() } -SGVector CKLCovarianceInferenceMethod::get_alpha() +SGVector KLCovarianceInferenceMethod::get_alpha() { /** Note that m_alpha contains not only the alpha vector defined in the reference * but also a vector corresponding to the diagonal part of W @@ -103,24 +103,23 @@ SGVector CKLCovarianceInferenceMethod::get_alpha() return result; } -CKLCovarianceInferenceMethod::~CKLCovarianceInferenceMethod() +KLCovarianceInferenceMethod::~KLCovarianceInferenceMethod() { } -CKLCovarianceInferenceMethod* CKLCovarianceInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr KLCovarianceInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_KL_COVARIANCE) - error("Provided inference is not of type CKLCovarianceInferenceMethod!"); + error("Provided inference is not of type KLCovarianceInferenceMethod!"); - SG_REF(inference); - return (CKLCovarianceInferenceMethod*)inference; + return inference->as(); } -bool CKLCovarianceInferenceMethod::precompute() +bool KLCovarianceInferenceMethod::precompute() { SGVector mean=m_mean->get_mean_vector(m_features); Map eigen_mean(mean.vector, mean.vlen); @@ -160,12 +159,12 @@ bool CKLCovarianceInferenceMethod::precompute() .abs() .matrix(); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); bool status = lik->set_variational_distribution(m_mu, m_s2, m_labels); return status; } -void CKLCovarianceInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector gradient) +void KLCovarianceInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector gradient) { require(gradient.vlen==m_alpha.vlen, "The length of gradients ({}) should the same as the length of parameters ({})", @@ -181,7 +180,7 @@ void CKLCovarianceInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector< Map eigen_alpha(m_alpha.vector, len); Map eigen_log_neg_lambda(m_alpha.vector+len, len); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); lik->set_variational_distribution(m_mu, m_s2, m_labels); //[a,df,dV] = a_related2(mu,s2,y,lik); @@ -218,7 +217,7 @@ void CKLCovarianceInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector< } -float64_t CKLCovarianceInferenceMethod::get_negative_log_marginal_likelihood_helper() +float64_t KLCovarianceInferenceMethod::get_negative_log_marginal_likelihood_helper() { Map eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols); Map eigen_alpha(m_alpha.vector, m_alpha.vlen/2); @@ -228,7 +227,7 @@ float64_t CKLCovarianceInferenceMethod::get_negative_log_marginal_likelihood_hel SGVector mean=m_mean->get_mean_vector(m_features); Map eigen_mean(mean.vector, mean.vlen); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); float64_t a=SGVector::sum(lik->get_variational_expection()); float64_t trace=0; @@ -245,7 +244,7 @@ float64_t CKLCovarianceInferenceMethod::get_negative_log_marginal_likelihood_hel return result; } -float64_t CKLCovarianceInferenceMethod::get_derivative_related_cov(SGMatrix dK) +float64_t KLCovarianceInferenceMethod::get_derivative_related_cov(SGMatrix dK) { Map eigen_dK(dK.matrix, dK.num_rows, dK.num_cols); Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -270,7 +269,7 @@ float64_t CKLCovarianceInferenceMethod::get_derivative_related_cov(SGMatrix mean=m_mean->get_mean_vector(m_features); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); lik->set_variational_distribution(mean, s2_tmp, m_labels); float64_t a=SGVector::sum(lik->get_variational_expection()); @@ -339,7 +338,7 @@ void CKLCovarianceInferenceMethod::update_alpha() nlml_new=optimization(); } -SGVector CKLCovarianceInferenceMethod::get_diagonal_vector() +SGVector KLCovarianceInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); @@ -347,21 +346,21 @@ SGVector CKLCovarianceInferenceMethod::get_diagonal_vector() return SGVector(m_sW); } -void CKLCovarianceInferenceMethod::update_deriv() +void KLCovarianceInferenceMethod::update_deriv() { /** get_derivative_related_cov() does the similar job * Therefore, this function body is empty */ } -void CKLCovarianceInferenceMethod::update_chol() +void KLCovarianceInferenceMethod::update_chol() { /** L is automatically updated when update_alpha is called * Therefore, this function body is empty */ } -void CKLCovarianceInferenceMethod::update_approx_cov() +void KLCovarianceInferenceMethod::update_approx_cov() { /** The variational co-variational matrix, * which is automatically computed when update_alpha is called, diff --git a/src/shogun/machine/gp/KLCovarianceInferenceMethod.h b/src/shogun/machine/gp/KLCovarianceInferenceMethod.h index f5d15def4a6..69f79e05356 100644 --- a/src/shogun/machine/gp/KLCovarianceInferenceMethod.h +++ b/src/shogun/machine/gp/KLCovarianceInferenceMethod.h @@ -69,11 +69,11 @@ namespace shogun * https://gist.github.com/yorkerlin/b64a015491833562d11a * */ -class CKLCovarianceInferenceMethod: public CKLInference +class KLCovarianceInferenceMethod: public KLInference { public: /** default constructor */ - CKLCovarianceInferenceMethod(); + KLCovarianceInferenceMethod(); /** constructor * @@ -83,10 +83,10 @@ class CKLCovarianceInferenceMethod: public CKLInference * @param labels labels of the features * @param model Likelihood model to use */ - CKLCovarianceInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + KLCovarianceInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CKLCovarianceInferenceMethod(); + virtual ~KLCovarianceInferenceMethod(); /** returns the name of the inference method * @@ -103,9 +103,9 @@ class CKLCovarianceInferenceMethod: public CKLInference /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CKLCovarianceInferenceMethod object + * @return casted KLCovarianceInferenceMethod object */ - static CKLCovarianceInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get alpha vector * diff --git a/src/shogun/machine/gp/KLDiagonalInferenceMethod.cpp b/src/shogun/machine/gp/KLDiagonalInferenceMethod.cpp index 19423f41c3c..75fc9d3219d 100644 --- a/src/shogun/machine/gp/KLDiagonalInferenceMethod.cpp +++ b/src/shogun/machine/gp/KLDiagonalInferenceMethod.cpp @@ -51,38 +51,37 @@ using namespace Eigen; namespace shogun { -CKLDiagonalInferenceMethod::CKLDiagonalInferenceMethod() : CKLLowerTriangularInference() +KLDiagonalInferenceMethod::KLDiagonalInferenceMethod() : KLLowerTriangularInference() { init(); } -CKLDiagonalInferenceMethod::CKLDiagonalInferenceMethod(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CKLLowerTriangularInference(kern, feat, m, lab, mod) +KLDiagonalInferenceMethod::KLDiagonalInferenceMethod(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : KLLowerTriangularInference(kern, feat, m, lab, mod) { init(); } -void CKLDiagonalInferenceMethod::init() +void KLDiagonalInferenceMethod::init() { SG_ADD(&m_InvK, "invK", "The K^{-1} matrix"); } -CKLDiagonalInferenceMethod* CKLDiagonalInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr KLDiagonalInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_KL_DIAGONAL) - error("Provided inference is not of type CKLDiagonalInferenceMethod!"); + error("Provided inference is not of type KLDiagonalInferenceMethod!"); - SG_REF(inference); - return (CKLDiagonalInferenceMethod*)inference; + return inference->as(); } -SGVector CKLDiagonalInferenceMethod::get_alpha() +SGVector KLDiagonalInferenceMethod::get_alpha() { /** Note that m_alpha contains not only the alpha vector defined in the reference * but also a vector corresponding to the lower triangular of C @@ -104,11 +103,11 @@ SGVector CKLDiagonalInferenceMethod::get_alpha() return result; } -CKLDiagonalInferenceMethod::~CKLDiagonalInferenceMethod() +KLDiagonalInferenceMethod::~KLDiagonalInferenceMethod() { } -bool CKLDiagonalInferenceMethod::precompute() +bool KLDiagonalInferenceMethod::precompute() { index_t len=m_mean_vec.vlen; Map eigen_mean(m_mean_vec.vector, m_mean_vec.vlen); @@ -124,12 +123,12 @@ bool CKLDiagonalInferenceMethod::precompute() //s2=sum(C.*C,2); eigen_s2=eigen_log_v.array().exp(); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); bool status=lik->set_variational_distribution(m_mu, m_s2, m_labels); return status; } -void CKLDiagonalInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector gradient) +void KLDiagonalInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector gradient) { require(gradient.vlen==m_alpha.vlen, "The length of gradients ({}) should the same as the length of parameters ({})", @@ -142,7 +141,7 @@ void CKLDiagonalInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector eigen_alpha(m_alpha.vector, len); Map eigen_s2(m_s2.vector, m_s2.vlen); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); //[a,df,dV] = a_related2(mu,s2,y,lik); TParameter* s2_param=lik->m_parameters->get_parameter("sigma2"); SGVector dv=lik->get_variational_first_derivative(s2_param); @@ -165,7 +164,7 @@ void CKLDiagonalInferenceMethod::get_gradient_of_nlml_wrt_parameters(SGVector eigen_alpha(m_alpha.vector, m_mu.vlen); Map eigen_mu(m_mu.vector, m_mu.vlen); @@ -176,7 +175,7 @@ float64_t CKLDiagonalInferenceMethod::get_negative_log_marginal_likelihood_helpe Map eigen_s2(m_s2.vector, m_s2.vlen); Map eigen_InvK(m_InvK.matrix, m_InvK.num_rows, m_InvK.num_cols); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); float64_t a=SGVector::sum(lik->get_variational_expection()); float64_t log_det=eigen_log_v.array().sum()-m_log_det_Kernel; float64_t trace=(eigen_s2.array()*eigen_InvK.diagonal().array()).sum(); @@ -187,7 +186,7 @@ float64_t CKLDiagonalInferenceMethod::get_negative_log_marginal_likelihood_helpe return result; } -void CKLDiagonalInferenceMethod::update_alpha() +void KLDiagonalInferenceMethod::update_alpha() { Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); m_InvK=SGMatrix(m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -207,7 +206,7 @@ void CKLDiagonalInferenceMethod::update_alpha() SGVector s2_tmp(m_s2.vlen); Map eigen_s2(s2_tmp.vector, s2_tmp.vlen); eigen_s2.fill(1.0); - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); lik->set_variational_distribution(m_mean_vec, s2_tmp, m_labels); float64_t a=SGVector::sum(lik->get_variational_expection()); float64_t trace=eigen_InvK.diagonal().array().sum(); @@ -230,7 +229,7 @@ void CKLDiagonalInferenceMethod::update_alpha() nlml_new=optimization(); } -void CKLDiagonalInferenceMethod::update_Sigma() +void KLDiagonalInferenceMethod::update_Sigma() { m_Sigma=SGMatrix(m_mu.vlen, m_mu.vlen); Map eigen_Sigma(m_Sigma.matrix, m_Sigma.num_rows, m_Sigma.num_cols); @@ -238,7 +237,7 @@ void CKLDiagonalInferenceMethod::update_Sigma() eigen_Sigma=eigen_s2.asDiagonal(); } -void CKLDiagonalInferenceMethod::update_InvK_Sigma() +void KLDiagonalInferenceMethod::update_InvK_Sigma() { m_InvK_Sigma=SGMatrix(m_ktrtr.num_rows, m_ktrtr.num_cols); Map eigen_InvK_Sigma(m_InvK_Sigma.matrix, m_InvK_Sigma.num_rows, m_InvK_Sigma.num_cols); diff --git a/src/shogun/machine/gp/KLDiagonalInferenceMethod.h b/src/shogun/machine/gp/KLDiagonalInferenceMethod.h index 13a86b3c397..c09e029c392 100644 --- a/src/shogun/machine/gp/KLDiagonalInferenceMethod.h +++ b/src/shogun/machine/gp/KLDiagonalInferenceMethod.h @@ -70,11 +70,11 @@ namespace shogun * Note that "Diagonal" means a variational diagonal co-variance matrix * is used in inference. */ -class CKLDiagonalInferenceMethod: public CKLLowerTriangularInference +class KLDiagonalInferenceMethod: public KLLowerTriangularInference { public: /** default constructor */ - CKLDiagonalInferenceMethod(); + KLDiagonalInferenceMethod(); /** constructor * @@ -84,10 +84,10 @@ class CKLDiagonalInferenceMethod: public CKLLowerTriangularInference * @param labels labels of the features * @param model Likelihood model to use */ - CKLDiagonalInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + KLDiagonalInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CKLDiagonalInferenceMethod(); + virtual ~KLDiagonalInferenceMethod(); /** returns the name of the inference method * @@ -104,9 +104,9 @@ class CKLDiagonalInferenceMethod: public CKLLowerTriangularInference /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CKLDiagonalInferenceMethod object + * @return casted KLDiagonalInferenceMethod object */ - static CKLDiagonalInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get alpha vector * diff --git a/src/shogun/machine/gp/KLDualInferenceMethod.cpp b/src/shogun/machine/gp/KLDualInferenceMethod.cpp index 1da05201d19..9c8fb59dfd5 100644 --- a/src/shogun/machine/gp/KLDualInferenceMethod.cpp +++ b/src/shogun/machine/gp/KLDualInferenceMethod.cpp @@ -53,17 +53,17 @@ namespace shogun #ifndef DOXYGEN_SHOULD_SKIP_THIS class KLDualInferenceMethodCostFunction: public FirstOrderCostFunction { -friend class CKLDualInferenceMethodMinimizer; +friend class KLDualInferenceMethodMinimizer; public: KLDualInferenceMethodCostFunction():FirstOrderCostFunction() { init(); } - virtual ~KLDualInferenceMethodCostFunction() { SG_UNREF(m_obj); } - void set_target(CKLDualInferenceMethod *obj) + virtual ~KLDualInferenceMethodCostFunction() { } + void set_target(std::shared_ptrobj) { require(obj, "Obj must set"); if(m_obj != obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; } } @@ -71,7 +71,7 @@ friend class CKLDualInferenceMethodMinimizer; { if(is_unref) { - SG_UNREF(m_obj); + } m_obj=NULL; } @@ -84,7 +84,7 @@ friend class CKLDualInferenceMethodMinimizer; float64_t nlml=m_obj->get_dual_objective_wrt_parameters(); return nlml; } - return CMath::NOT_A_NUMBER; + return Math::NOT_A_NUMBER; } virtual SGVector obtain_variable_reference() { @@ -107,11 +107,11 @@ friend class CKLDualInferenceMethodMinimizer; m_derivatives = SGVector(); SG_ADD(&m_derivatives, "KLDualInferenceMethodCostFunction__m_derivatives", "derivatives in KLDualInferenceMethodCostFunction"); - SG_ADD((CSGObject **)&m_obj, "KLDualInferenceMethodCostFunction__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "KLDualInferenceMethodCostFunction__m_obj", "obj in KLDualInferenceMethodCostFunction"); } - CKLDualInferenceMethod *m_obj; - CDualVariationalGaussianLikelihood* get_dual_variational_likelihood() const + std::shared_ptrm_obj; + std::shared_ptr get_dual_variational_likelihood() const { require(m_obj,"Object not set"); return m_obj->get_dual_variational_likelihood(); @@ -119,17 +119,17 @@ friend class CKLDualInferenceMethodMinimizer; }; #endif //DOXYGEN_SHOULD_SKIP_THIS -void CKLDualInferenceMethodMinimizer::init_minimization() +void KLDualInferenceMethodMinimizer::init_minimization() { ELBFGSLineSearch linesearch=LBFGSLineSearchHelper::get_lbfgs_linear_search(m_linesearch_id); require((linesearch == BACKTRACKING_ARMIJO) || (linesearch == BACKTRACKING_WOLFE) || (linesearch == BACKTRACKING_STRONG_WOLFE), "The provided line search method is not supported. Please use backtracking line search methods"); - CLBFGSMinimizer::init_minimization(); + LBFGSMinimizer::init_minimization(); } -float64_t CKLDualInferenceMethodMinimizer::minimize() +float64_t KLDualInferenceMethodMinimizer::minimize() { lbfgs_parameter_t lbfgs_param; lbfgs_param.m = m_m; @@ -153,8 +153,8 @@ float64_t CKLDualInferenceMethodMinimizer::minimize() float64_t cost=0.0; int error_code=lbfgs(m_target_variable.vlen, m_target_variable.vector, - &cost, CKLDualInferenceMethodMinimizer::evaluate, - NULL, this, &lbfgs_param, CKLDualInferenceMethodMinimizer::adjust_step); + &cost, KLDualInferenceMethodMinimizer::evaluate, + NULL, this, &lbfgs_param, KLDualInferenceMethodMinimizer::adjust_step); if(error_code!=0 && error_code!=LBFGS_ALREADY_MINIMIZED) { @@ -164,17 +164,17 @@ float64_t CKLDualInferenceMethodMinimizer::minimize() return cost; } -float64_t CKLDualInferenceMethodMinimizer::evaluate(void *obj, const float64_t *variable, +float64_t KLDualInferenceMethodMinimizer::evaluate(void *obj, const float64_t *variable, float64_t *gradient, const int dim, const float64_t step) { /* Note that parameters = parameters_pre_iter - step * gradient_pre_iter */ - CKLDualInferenceMethodMinimizer * obj_prt - = static_cast(obj); + auto obj_prt + = (KLDualInferenceMethodMinimizer*)obj; require(obj_prt, "The instance object passed to L-BFGS optimizer should not be NULL"); float64_t cost=obj_prt->m_fun->get_cost(); - if (CMath::is_nan(cost) || std::isinf(cost)) + if (Math::is_nan(cost) || std::isinf(cost)) return cost; //get the gradient wrt variable_new SGVector grad=obj_prt->m_fun->get_gradient(); @@ -186,41 +186,41 @@ float64_t CKLDualInferenceMethodMinimizer::evaluate(void *obj, const float64_t * return cost; } -float64_t CKLDualInferenceMethodMinimizer::adjust_step(void *obj, const float64_t *parameters, +float64_t KLDualInferenceMethodMinimizer::adjust_step(void *obj, const float64_t *parameters, const float64_t *direction, const int dim, const float64_t step) { /* Note that parameters = parameters_pre_iter - step * gradient_pre_iter */ - CKLDualInferenceMethodMinimizer * obj_prt - = static_cast(obj); + auto obj_prt + = (KLDualInferenceMethodMinimizer*)obj; require(obj_prt, "The instance object passed to L-BFGS optimizer should not be NULL"); float64_t *non_const_direction=const_cast(direction); SGVector sg_direction(non_const_direction, dim, false); - KLDualInferenceMethodCostFunction* fun=dynamic_cast(obj_prt->m_fun); + auto fun=std::dynamic_pointer_cast(obj_prt->m_fun); require(fun, "The cost function must be KLDualInferenceMethodCostFunction"); - CDualVariationalGaussianLikelihood* lik=fun->get_dual_variational_likelihood(); + auto lik=fun->get_dual_variational_likelihood(); float64_t adjust_stp=lik->adjust_step_wrt_dual_parameter(sg_direction, step); return adjust_stp; } -CKLDualInferenceMethod::CKLDualInferenceMethod() : CKLInference() +KLDualInferenceMethod::KLDualInferenceMethod() : KLInference() { init(); } -CKLDualInferenceMethod::CKLDualInferenceMethod(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CKLInference(kern, feat, m, lab, mod) +KLDualInferenceMethod::KLDualInferenceMethod(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : KLInference(kern, feat, m, lab, mod) { init(); } -CKLDualInferenceMethod* CKLDualInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr KLDualInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; @@ -230,11 +230,11 @@ CKLDualInferenceMethod* CKLDualInferenceMethod::obtain_from_generic( error("Provided inference is not of type CKLDualInferenceMethod!"); } - SG_REF(inference); - return (CKLDualInferenceMethod*)inference; + + return inference->as(); } -SGVector CKLDualInferenceMethod::get_alpha() +SGVector KLDualInferenceMethod::get_alpha() { if (parameter_hash_changed()) update(); @@ -243,39 +243,38 @@ SGVector CKLDualInferenceMethod::get_alpha() return result; } -CKLDualInferenceMethod::~CKLDualInferenceMethod() +KLDualInferenceMethod::~KLDualInferenceMethod() { } -void CKLDualInferenceMethod::check_dual_inference(CLikelihoodModel* mod) const +void KLDualInferenceMethod::check_dual_inference(std::shared_ptr mod) const { - CDualVariationalGaussianLikelihood * lik=dynamic_cast(mod); + auto lik=mod->as(); require(lik, "The provided likelihood model is not a variational dual Likelihood model."); } -void CKLDualInferenceMethod::set_model(CLikelihoodModel* mod) +void KLDualInferenceMethod::set_model(std::shared_ptr mod) { check_dual_inference(mod); - CKLInference::set_model(mod); + KLInference::set_model(mod); } -CDualVariationalGaussianLikelihood* CKLDualInferenceMethod::get_dual_variational_likelihood() const +std::shared_ptr KLDualInferenceMethod::get_dual_variational_likelihood() const { check_dual_inference(m_model); - CDualVariationalGaussianLikelihood * lik=dynamic_cast(m_model); - return lik; + return m_model->as(); } -void CKLDualInferenceMethod::register_minimizer(Minimizer* minimizer) +void KLDualInferenceMethod::register_minimizer(std::shared_ptr minimizer) { - CKLDualInferenceMethodMinimizer* opt=dynamic_cast(minimizer); + auto opt=minimizer->as(); require(opt,"The minimizer must be an instance of CKLDualInferenceMethodMinimizer"); - CInference::register_minimizer(minimizer); + Inference::register_minimizer(minimizer); } -void CKLDualInferenceMethod::init() +void KLDualInferenceMethod::init() { SG_ADD(&m_W, "W", "noise matrix W"); @@ -289,13 +288,13 @@ void CKLDualInferenceMethod::init() "whether the lambda (m_W) is valid or not"); m_is_dual_valid=false; - register_minimizer(new CKLDualInferenceMethodMinimizer()); + register_minimizer(std::make_shared()); } -bool CKLDualInferenceMethod::precompute() +bool KLDualInferenceMethod::precompute() { Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); - CDualVariationalGaussianLikelihood *lik= get_dual_variational_likelihood(); + auto lik= get_dual_variational_likelihood(); Map eigen_W(m_W.vector, m_W.vlen); lik->set_dual_parameters(m_W, m_labels); @@ -337,10 +336,10 @@ bool CKLDualInferenceMethod::precompute() return true; } -float64_t CKLDualInferenceMethod::get_dual_objective_wrt_parameters() +float64_t KLDualInferenceMethod::get_dual_objective_wrt_parameters() { if (!m_is_dual_valid) - return CMath::INFTY; + return Math::INFTY; SGVector mean=m_mean->get_mean_vector(m_features); Map eigen_mean(mean.vector, mean.vlen); @@ -348,7 +347,7 @@ float64_t CKLDualInferenceMethod::get_dual_objective_wrt_parameters() Map eigen_alpha(m_alpha.vector, m_alpha.vlen); Map eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols); - CDualVariationalGaussianLikelihood *lik= get_dual_variational_likelihood(); + auto lik= get_dual_variational_likelihood(); float64_t a=SGVector::sum(lik->get_dual_objective_value()); float64_t result=0.5*eigen_alpha.dot(eigen_mu-eigen_mean)+a; @@ -358,7 +357,7 @@ float64_t CKLDualInferenceMethod::get_dual_objective_wrt_parameters() return result; } -void CKLDualInferenceMethod::get_gradient_of_dual_objective_wrt_parameters(SGVector gradient) +void KLDualInferenceMethod::get_gradient_of_dual_objective_wrt_parameters(SGVector gradient) { require(gradient.vlen==m_alpha.vlen, "The length of gradients ({}) should the same as the length of parameters ({})", @@ -369,7 +368,7 @@ void CKLDualInferenceMethod::get_gradient_of_dual_objective_wrt_parameters(SGVec Map eigen_gradient(gradient.vector, gradient.vlen); - CDualVariationalGaussianLikelihood *lik= get_dual_variational_likelihood(); + auto lik= get_dual_variational_likelihood(); TParameter* lambda_param=lik->m_parameters->get_parameter("lambda"); SGVectord_lambda=lik->get_dual_first_derivative(lambda_param); @@ -377,10 +376,10 @@ void CKLDualInferenceMethod::get_gradient_of_dual_objective_wrt_parameters(SGVec Map eigen_mu(m_mu.vector, m_mu.vlen); Map eigen_s2(m_s2.vector, m_s2.vlen); -eigen_gradient=-eigen_mu-0.5*eigen_s2+eigen_d_lambda; + eigen_gradient=-eigen_mu-0.5*eigen_s2+eigen_d_lambda; } -float64_t CKLDualInferenceMethod::get_nlml_wrapper(SGVector alpha, SGVector mu, SGMatrix L) +float64_t KLDualInferenceMethod::get_nlml_wrapper(SGVector alpha, SGVector mu, SGMatrix L) { Map eigen_L(L.matrix, L.num_rows, L.num_cols); Map eigen_alpha(alpha.vector, alpha.vlen); @@ -390,9 +389,9 @@ float64_t CKLDualInferenceMethod::get_nlml_wrapper(SGVector alpha, SG Map eigen_mu(mu.vector, mu.vlen); Map eigen_mean(mean.vector, mean.vlen); - CDualVariationalGaussianLikelihood *lik=get_dual_variational_likelihood(); + auto lik=get_dual_variational_likelihood(); - SGVectorlab=((CBinaryLabels*)m_labels)->get_labels(); + SGVectorlab=m_labels->as()->get_labels(); Map eigen_lab(lab.vector, lab.vlen); float64_t a=SGVector::sum(lik->get_variational_expection()); @@ -412,16 +411,16 @@ float64_t CKLDualInferenceMethod::get_nlml_wrapper(SGVector alpha, SG return result; } -float64_t CKLDualInferenceMethod::get_negative_log_marginal_likelihood_helper() +float64_t KLDualInferenceMethod::get_negative_log_marginal_likelihood_helper() { - CDualVariationalGaussianLikelihood *lik=get_dual_variational_likelihood(); + auto lik=get_dual_variational_likelihood(); bool status = lik->set_variational_distribution(m_mu, m_s2, m_labels); if (status) return get_nlml_wrapper(m_alpha, m_mu, m_L); - return CMath::NOT_A_NUMBER; + return Math::NOT_A_NUMBER; } -float64_t CKLDualInferenceMethod::get_derivative_related_cov(SGMatrix dK) +float64_t KLDualInferenceMethod::get_derivative_related_cov(SGMatrix dK) { Map eigen_dK(dK.matrix, dK.num_rows, dK.num_cols); Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -453,13 +452,13 @@ float64_t CKLDualInferenceMethod::get_derivative_related_cov(SGMatrix return result; } -void CKLDualInferenceMethod::update_alpha() +void KLDualInferenceMethod::update_alpha() { float64_t nlml_new=0; float64_t nlml_def=0; Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); - CDualVariationalGaussianLikelihood *lik= get_dual_variational_likelihood(); + auto lik= get_dual_variational_likelihood(); if (m_alpha.vlen == m_labels->get_num_labels()) { @@ -538,26 +537,23 @@ void CKLDualInferenceMethod::update_alpha() m_df=lik->get_variational_first_derivative(mu_param); } -float64_t CKLDualInferenceMethod::optimization() +float64_t KLDualInferenceMethod::optimization() { - CKLDualInferenceMethodMinimizer *minimizer=dynamic_cast(m_minimizer); + auto minimizer=m_minimizer->as(); require(minimizer,"The minimizer must be an instance of KLDualInferenceMethodMinimizer"); - KLDualInferenceMethodCostFunction* cost_fun=new KLDualInferenceMethodCostFunction(); - cost_fun->set_target(this); + auto cost_fun=std::make_shared(); + cost_fun->set_target(shared_from_this()->as()); bool cleanup=false; - if(this->ref_count()>1) - cleanup=true; - minimizer->set_cost_function(cost_fun); float64_t nlml_opt = minimizer->minimize(); minimizer->unset_cost_function(false); - cost_fun->unset_target(cleanup); - SG_UNREF(cost_fun); + //cost_fun->unset_target(cleanup); + return nlml_opt; } -SGVector CKLDualInferenceMethod::get_diagonal_vector() +SGVector KLDualInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); @@ -565,21 +561,21 @@ SGVector CKLDualInferenceMethod::get_diagonal_vector() return SGVector(m_sW); } -void CKLDualInferenceMethod::update_deriv() +void KLDualInferenceMethod::update_deriv() { /* get_derivative_related_cov(MatrixXd eigen_dK) does the similar job * Therefore, this function body is empty */ } -void CKLDualInferenceMethod::update_chol() +void KLDualInferenceMethod::update_chol() { /* L is automatically updated when update_alpha is called * Therefore, this function body is empty */ } -void CKLDualInferenceMethod::update_approx_cov() +void KLDualInferenceMethod::update_approx_cov() { m_Sigma = CMatrixOperations::get_inverse( m_L, m_ktrtr, m_sW, m_V, std::exp(m_log_scale)); diff --git a/src/shogun/machine/gp/KLDualInferenceMethod.h b/src/shogun/machine/gp/KLDualInferenceMethod.h index 8fbc22d015f..845714a7650 100644 --- a/src/shogun/machine/gp/KLDualInferenceMethod.h +++ b/src/shogun/machine/gp/KLDualInferenceMethod.h @@ -45,17 +45,17 @@ namespace shogun { /** @brief Build-in minimizer for KLDualInference */ -class CKLDualInferenceMethodMinimizer: public CLBFGSMinimizer +class KLDualInferenceMethodMinimizer: public LBFGSMinimizer { public: - CKLDualInferenceMethodMinimizer(): CLBFGSMinimizer() { init(); } + KLDualInferenceMethodMinimizer(): LBFGSMinimizer() { init(); } /** Constructor * @param fun a cost function */ - CKLDualInferenceMethodMinimizer(FirstOrderCostFunction *fun): CLBFGSMinimizer(fun) { init(); } + KLDualInferenceMethodMinimizer(std::shared_ptrfun): LBFGSMinimizer(fun) { init(); } - virtual ~CKLDualInferenceMethodMinimizer() {} + virtual ~KLDualInferenceMethodMinimizer() {} /** Do minimization and get the optimal value * @@ -105,12 +105,12 @@ class CKLDualInferenceMethodMinimizer: public CLBFGSMinimizer * * For detailed information, please refer to the paper. */ -class CKLDualInferenceMethod: public CKLInference +class KLDualInferenceMethod: public KLInference { friend class KLDualInferenceMethodCostFunction; public: /** default constructor */ - CKLDualInferenceMethod(); + KLDualInferenceMethod(); /** constructor * @@ -120,10 +120,10 @@ friend class KLDualInferenceMethodCostFunction; * @param labels labels of the features * @param model Likelihood model to use */ - CKLDualInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + KLDualInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CKLDualInferenceMethod(); + virtual ~KLDualInferenceMethod(); /** returns the name of the inference method * @@ -142,7 +142,7 @@ friend class KLDualInferenceMethodCostFunction; * @param inference inference method * @return casted CKLDualInferenceMethod object */ - static CKLDualInferenceMethod * obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get alpha vector * @@ -173,13 +173,13 @@ friend class KLDualInferenceMethodCostFunction; * * @param mod model to set */ - void set_model(CLikelihoodModel* mod); + void set_model(std::shared_ptr mod); /** Set a minimizer * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** compute the gradient wrt variational parameters @@ -192,14 +192,14 @@ friend class KLDualInferenceMethodCostFunction; /** this method is used to dynamic-cast the likelihood model, m_model, * to dual variational likelihood model. */ - virtual CDualVariationalGaussianLikelihood* get_dual_variational_likelihood() const; + virtual std::shared_ptr get_dual_variational_likelihood() const; /** check the provided likelihood model supports dual variational inference or not * @param mod the provided likelihood model * * @return whether the provided likelihood model supports dual variational inference or not */ - virtual void check_dual_inference(CLikelihoodModel* mod) const; + virtual void check_dual_inference(std::shared_ptr mod) const; /** update covariance matrix of the approximation to the posterior */ virtual void update_approx_cov(); diff --git a/src/shogun/machine/gp/KLInference.cpp b/src/shogun/machine/gp/KLInference.cpp index 008affd5ccd..0abc467b4bc 100644 --- a/src/shogun/machine/gp/KLInference.cpp +++ b/src/shogun/machine/gp/KLInference.cpp @@ -48,32 +48,19 @@ class KLInferenceCostFunction: public FirstOrderCostFunction { public: KLInferenceCostFunction():FirstOrderCostFunction() { init(); } - virtual ~KLInferenceCostFunction() { SG_UNREF(m_obj); } - void set_target(CKLInference *obj) + virtual ~KLInferenceCostFunction() { } + void set_target(std::shared_ptrobj) { - require(obj,"Obj must set"); - if(m_obj!=obj) - { - SG_REF(obj); - SG_UNREF(m_obj); + require(obj,"Obj must set"); m_obj=obj; - } - } - void unset_target(bool is_unref) - { - if(is_unref) - { - SG_UNREF(m_obj); - } - m_obj=NULL; - } + } virtual float64_t get_cost() { require(m_obj,"Object not set"); bool status = m_obj->precompute(); if (!status) - return CMath::NOT_A_NUMBER; + return Math::NOT_A_NUMBER; float64_t nlml=m_obj->get_nlml_wrt_parameters(); return nlml; @@ -100,43 +87,43 @@ class KLInferenceCostFunction: public FirstOrderCostFunction m_derivatives = SGVector(); SG_ADD(&m_derivatives, "KLInferenceCostFunction__m_derivatives", "derivatives in KLInferenceCostFunction"); - SG_ADD((CSGObject **)&m_obj, "KLInferenceCostFunction__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "KLInferenceCostFunction__m_obj", "obj in KLInferenceCostFunction"); } - CKLInference *m_obj; + std::shared_ptr m_obj; }; #endif //DOXYGEN_SHOULD_SKIP_THIS -CKLInference::CKLInference() : CInference() +KLInference::KLInference() : Inference() { init(); } -CKLInference::CKLInference(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CInference(kern, feat, m, lab, mod) +KLInference::KLInference(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : Inference(kern, feat, m, lab, mod) { init(); check_variational_likelihood(m_model); } -void CKLInference::check_variational_likelihood(CLikelihoodModel* mod) const +void KLInference::check_variational_likelihood(std::shared_ptr mod) const { require(mod, "the likelihood model must not be NULL"); - CVariationalGaussianLikelihood* lik= dynamic_cast(mod); + auto lik= std::dynamic_pointer_cast(mod); require(lik, "The provided likelihood model ({}) must support variational Gaussian inference. ", "Please use a Variational Gaussian Likelihood model", mod->get_name()); } -void CKLInference::set_model(CLikelihoodModel* mod) +void KLInference::set_model(std::shared_ptr mod) { check_variational_likelihood(mod); - CInference::set_model(mod); + Inference::set_model(mod); } -void CKLInference::init() +void KLInference::init() { m_noise_factor=1e-10; m_max_attempt=0; @@ -156,16 +143,16 @@ void CKLInference::init() "Variational parameter mu and posterior mean"); SG_ADD(&m_Sigma, "Sigma", "Posterior covariance matrix Sigma"); - register_minimizer(new CLBFGSMinimizer()); + register_minimizer(std::make_shared()); } -CKLInference::~CKLInference() +KLInference::~KLInference() { } -void CKLInference::compute_gradient() +void KLInference::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -176,11 +163,11 @@ void CKLInference::compute_gradient() } } -void CKLInference::update() +void KLInference::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_init(); update_alpha(); update_chol(); @@ -190,36 +177,36 @@ void CKLInference::update() SG_TRACE("leaving"); } -void CKLInference::set_noise_factor(float64_t noise_factor) +void KLInference::set_noise_factor(float64_t noise_factor) { require(noise_factor>=0, "The noise_factor {:.20f} should be non-negative", noise_factor); m_noise_factor=noise_factor; } -void CKLInference::set_min_coeff_kernel(float64_t min_coeff_kernel) +void KLInference::set_min_coeff_kernel(float64_t min_coeff_kernel) { require(min_coeff_kernel>=0, "The min_coeff_kernel {:.20f} should be non-negative", min_coeff_kernel); m_min_coeff_kernel=min_coeff_kernel; } -void CKLInference::set_max_attempt(index_t max_attempt) +void KLInference::set_max_attempt(index_t max_attempt) { require(max_attempt>=0, "The max_attempt {} should be non-negative. 0 means inifity attempts", max_attempt); m_max_attempt=max_attempt; } -void CKLInference::set_exp_factor(float64_t exp_factor) +void KLInference::set_exp_factor(float64_t exp_factor) { require(exp_factor>1.0, "The exp_factor {} should be greater than 1.0.", exp_factor); m_exp_factor=exp_factor; } -void CKLInference::update_init() +void KLInference::update_init() { update_init_helper(); } -Eigen::LDLT CKLInference::update_init_helper() +Eigen::LDLT KLInference::update_init_helper() { Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -251,35 +238,35 @@ Eigen::LDLT CKLInference::update_init_helper() } -SGVector CKLInference::get_posterior_mean() +SGVector KLInference::get_posterior_mean() { compute_gradient(); return SGVector(m_mu); } -SGMatrix CKLInference::get_posterior_covariance() +SGMatrix KLInference::get_posterior_covariance() { compute_gradient(); return SGMatrix(m_Sigma); } -CVariationalGaussianLikelihood* CKLInference::get_variational_likelihood() const +std::shared_ptr KLInference::get_variational_likelihood() const { check_variational_likelihood(m_model); - CVariationalGaussianLikelihood* lik= dynamic_cast(m_model); + auto lik= std::dynamic_pointer_cast(m_model); return lik; } -float64_t CKLInference::get_nlml_wrt_parameters() +float64_t KLInference::get_nlml_wrt_parameters() { - CVariationalGaussianLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); lik->set_variational_distribution(m_mu, m_s2, m_labels); return get_negative_log_marginal_likelihood_helper(); } -float64_t CKLInference::get_negative_log_marginal_likelihood() +float64_t KLInference::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); @@ -287,9 +274,9 @@ float64_t CKLInference::get_negative_log_marginal_likelihood() return get_negative_log_marginal_likelihood_helper(); } -SGVector CKLInference::get_derivative_wrt_likelihood_model(const TParameter* param) +SGVector KLInference::get_derivative_wrt_likelihood_model(const TParameter* param) { - CVariationalLikelihood * lik=get_variational_likelihood(); + auto lik=get_variational_likelihood(); if (!lik->supports_derivative_wrt_hyperparameter()) return SGVector (); @@ -303,7 +290,7 @@ SGVector CKLInference::get_derivative_wrt_likelihood_model(const TPar return result; } -SGVector CKLInference::get_derivative_wrt_mean(const TParameter* param) +SGVector KLInference::get_derivative_wrt_mean(const TParameter* param) { // create eigen representation of K, Z, dfhat and alpha Map eigen_alpha(m_alpha.vector, m_alpha.vlen/2); @@ -332,37 +319,32 @@ SGVector CKLInference::get_derivative_wrt_mean(const TParameter* para return result; } -float64_t CKLInference::optimization() +float64_t KLInference::optimization() { - KLInferenceCostFunction *cost_fun=new KLInferenceCostFunction(); - cost_fun->set_target(this); - bool cleanup=false; - if(this->ref_count()>1) - cleanup=true; + auto cost_fun=std::make_shared(); + cost_fun->set_target(shared_from_this()->as()); - FirstOrderMinimizer* opt= dynamic_cast(m_minimizer); + auto opt=m_minimizer->as(); require(opt, "FirstOrderMinimizer is required"); opt->set_cost_function(cost_fun); - float64_t nlml_opt = opt->minimize(); + float64_t nlml_opt = opt->minimize(); opt->unset_cost_function(false); - cost_fun->unset_target(cleanup); - SG_UNREF(cost_fun); return nlml_opt; } -void CKLInference::register_minimizer(Minimizer* minimizer) +void KLInference::register_minimizer(std::shared_ptr minimizer) { require(minimizer, "Minimizer must set"); - FirstOrderMinimizer* opt= dynamic_cast(minimizer); + auto opt= std::dynamic_pointer_cast(minimizer); require(opt, "FirstOrderMinimizer is required"); - CInference::register_minimizer(minimizer); + Inference::register_minimizer(minimizer); } -SGVector CKLInference::get_derivative_wrt_inference_method(const TParameter* param) +SGVector KLInference::get_derivative_wrt_inference_method(const TParameter* param) { require(!strcmp(param->m_name, "log_scale"), "Can't compute derivative of " "the nagative log marginal likelihood wrt {}.{} parameter", @@ -378,7 +360,7 @@ SGVector CKLInference::get_derivative_wrt_inference_method(const TPar return result; } -SGVector CKLInference::get_derivative_wrt_kernel(const TParameter* param) +SGVector KLInference::get_derivative_wrt_kernel(const TParameter* param) { require(param, "Param not set"); SGVector result; @@ -402,7 +384,7 @@ SGVector CKLInference::get_derivative_wrt_kernel(const TParameter* pa return result; } -SGMatrix CKLInference::get_cholesky() +SGMatrix KLInference::get_cholesky() { if (parameter_hash_changed()) update(); diff --git a/src/shogun/machine/gp/KLInference.h b/src/shogun/machine/gp/KLInference.h index 786cbd76b75..ecf79c403d9 100644 --- a/src/shogun/machine/gp/KLInference.h +++ b/src/shogun/machine/gp/KLInference.h @@ -72,12 +72,12 @@ namespace shogun * "Approximations for Binary Gaussian Process Classification." * Journal of Machine Learning Research 9.10 (2008). */ -class CKLInference: public CInference +class KLInference: public Inference { friend class KLInferenceCostFunction; public: /** default constructor */ - CKLInference(); + KLInference(); /** constructor * @@ -87,10 +87,10 @@ friend class KLInferenceCostFunction; * @param labels labels of the features * @param model Likelihood model to use */ - CKLInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + KLInference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CKLInference(); + virtual ~KLInference(); /** return what type of inference we are */ @@ -172,7 +172,7 @@ friend class KLInferenceCostFunction; * * @param mod model to set */ - virtual void set_model(CLikelihoodModel* mod); + virtual void set_model(std::shared_ptr mod); /** update all matrices except gradients */ virtual void update(); @@ -190,7 +190,7 @@ friend class KLInferenceCostFunction; * * Note that in some sub class L is not the Cholesky decomposition * In this case, L will still be used to compute required matrix for prediction - * see CGaussianProcessMachine::get_posterior_variances() + * see GaussianProcessMachine::get_posterior_variances() */ virtual SGMatrix get_cholesky(); @@ -231,7 +231,7 @@ friend class KLInferenceCostFunction; * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** update gradients */ @@ -263,14 +263,14 @@ friend class KLInferenceCostFunction; /** this method is used to dynamic-cast the likelihood model, m_model, * to variational likelihood model. */ - virtual CVariationalGaussianLikelihood* get_variational_likelihood() const; + virtual std::shared_ptr get_variational_likelihood() const; /**check the provided likelihood model supports variational inference * @param mod the provided likelihood model * * @return whether the provided likelihood model supports variational inference or not */ - virtual void check_variational_likelihood(CLikelihoodModel* mod) const; + virtual void check_variational_likelihood(std::shared_ptr mod) const; /** update covariance matrix of the approximation to the posterior */ virtual void update_approx_cov()=0; diff --git a/src/shogun/machine/gp/KLLowerTriangularInference.cpp b/src/shogun/machine/gp/KLLowerTriangularInference.cpp index 722d26d475f..aa34b413a3c 100644 --- a/src/shogun/machine/gp/KLLowerTriangularInference.cpp +++ b/src/shogun/machine/gp/KLLowerTriangularInference.cpp @@ -50,19 +50,19 @@ using namespace Eigen; namespace shogun { -CKLLowerTriangularInference::CKLLowerTriangularInference() : CKLInference() +KLLowerTriangularInference::KLLowerTriangularInference() : KLInference() { init(); } -CKLLowerTriangularInference::CKLLowerTriangularInference(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CKLInference(kern, feat, m, lab, mod) +KLLowerTriangularInference::KLLowerTriangularInference(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : KLInference(kern, feat, m, lab, mod) { init(); } -void CKLLowerTriangularInference::init() +void KLLowerTriangularInference::init() { SG_ADD(&m_InvK_Sigma, "invk_Sigma", "K^{-1}Sigma'"); @@ -78,11 +78,11 @@ void CKLLowerTriangularInference::init() m_log_det_Kernel=0; } -CKLLowerTriangularInference::~CKLLowerTriangularInference() +KLLowerTriangularInference::~KLLowerTriangularInference() { } -SGVector CKLLowerTriangularInference::get_diagonal_vector() +SGVector KLLowerTriangularInference::get_diagonal_vector() { /** The diagonal vector W is NOT used in this KL method * Therefore, return empty vector @@ -90,14 +90,14 @@ SGVector CKLLowerTriangularInference::get_diagonal_vector() return SGVector(); } -void CKLLowerTriangularInference::update_deriv() +void KLLowerTriangularInference::update_deriv() { /** get_derivative_related_cov() does the similar job * Therefore, this function body is empty */ } -void CKLLowerTriangularInference::update_init() +void KLLowerTriangularInference::update_init() { Eigen::LDLT ldlt=update_init_helper(); MatrixXd Kernel_D=ldlt.vectorD(); @@ -117,7 +117,7 @@ void CKLLowerTriangularInference::update_init() m_mean_vec=m_mean->get_mean_vector(m_features); } -MatrixXd CKLLowerTriangularInference::solve_inverse(MatrixXd eigen_A) +MatrixXd KLLowerTriangularInference::solve_inverse(MatrixXd eigen_A) { Map eigen_Kernel_P(m_Kernel_P.vector, m_Kernel_P.vlen); Map eigen_Kernel_LsD(m_Kernel_LsD.matrix, m_Kernel_LsD.num_rows, m_Kernel_LsD.num_cols); @@ -144,7 +144,7 @@ MatrixXd CKLLowerTriangularInference::solve_inverse(MatrixXd eigen_A) return P.transpose()*tmp3; } -float64_t CKLLowerTriangularInference::get_derivative_related_cov(SGMatrix dK) +float64_t KLLowerTriangularInference::get_derivative_related_cov(SGMatrix dK) { Map eigen_dK(dK.matrix, dK.num_rows, dK.num_cols); Map eigen_alpha(m_alpha.vector, m_mu.vlen); @@ -158,14 +158,14 @@ float64_t CKLLowerTriangularInference::get_derivative_related_cov(SGMatrix kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CKLLowerTriangularInference(); + virtual ~KLLowerTriangularInference(); /** returns the name of the inference method * diff --git a/src/shogun/machine/gp/LaplaceInference.cpp b/src/shogun/machine/gp/LaplaceInference.cpp index 81d67918b3e..f34fac667fc 100644 --- a/src/shogun/machine/gp/LaplaceInference.cpp +++ b/src/shogun/machine/gp/LaplaceInference.cpp @@ -42,19 +42,19 @@ using namespace Eigen; namespace shogun { -CLaplaceInference::CLaplaceInference() : CInference() +LaplaceInference::LaplaceInference() : Inference() { init(); } -CLaplaceInference::CLaplaceInference(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CInference(kern, feat, m, lab, mod) +LaplaceInference::LaplaceInference(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : Inference(kern, feat, m, lab, mod) { init(); } -void CLaplaceInference::init() +void LaplaceInference::init() { SG_ADD(&m_dlp, "dlp", "derivative of log likelihood with respect to function location"); SG_ADD(&m_mu, "mu", "mean vector of the approximation to the posterior"); @@ -62,13 +62,13 @@ void CLaplaceInference::init() SG_ADD(&m_W, "W", "the noise matrix"); } -CLaplaceInference::~CLaplaceInference() +LaplaceInference::~LaplaceInference() { } -void CLaplaceInference::compute_gradient() +void LaplaceInference::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -78,11 +78,11 @@ void CLaplaceInference::compute_gradient() update_parameter_hash(); } } -void CLaplaceInference::update() +void LaplaceInference::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_alpha(); update_chol(); m_gradient_update=false; @@ -91,7 +91,7 @@ void CLaplaceInference::update() SG_TRACE("leaving"); } -SGVector CLaplaceInference::get_alpha() +SGVector LaplaceInference::get_alpha() { if (parameter_hash_changed()) update(); @@ -101,7 +101,7 @@ SGVector CLaplaceInference::get_alpha() } -SGMatrix CLaplaceInference::get_cholesky() +SGMatrix LaplaceInference::get_cholesky() { if (parameter_hash_changed()) update(); @@ -110,7 +110,7 @@ SGMatrix CLaplaceInference::get_cholesky() } -SGMatrix CLaplaceInference::get_posterior_covariance() +SGMatrix LaplaceInference::get_posterior_covariance() { compute_gradient(); diff --git a/src/shogun/machine/gp/LaplaceInference.h b/src/shogun/machine/gp/LaplaceInference.h index 1bf866949a0..8450dcaf66f 100644 --- a/src/shogun/machine/gp/LaplaceInference.h +++ b/src/shogun/machine/gp/LaplaceInference.h @@ -48,11 +48,11 @@ namespace shogun * function. * */ -class CLaplaceInference: public CInference +class LaplaceInference: public Inference { public: /** default constructor */ - CLaplaceInference(); + LaplaceInference(); /** constructor * @@ -62,10 +62,10 @@ class CLaplaceInference: public CInference * @param labels labels of the features * @param model Likelihood model to use */ - CLaplaceInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + LaplaceInference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CLaplaceInference(); + virtual ~LaplaceInference(); /** return what type of inference we are * diff --git a/src/shogun/machine/gp/LikelihoodModel.cpp b/src/shogun/machine/gp/LikelihoodModel.cpp index 67b1f49774f..bb473f1b063 100644 --- a/src/shogun/machine/gp/LikelihoodModel.cpp +++ b/src/shogun/machine/gp/LikelihoodModel.cpp @@ -34,22 +34,22 @@ using namespace shogun; -CLikelihoodModel::CLikelihoodModel() +LikelihoodModel::LikelihoodModel() { } -CLikelihoodModel::~CLikelihoodModel() +LikelihoodModel::~LikelihoodModel() { } -SGVector CLikelihoodModel::get_predictive_log_probabilities( - SGVector mu, SGVector s2, const CLabels *lab) +SGVector LikelihoodModel::get_predictive_log_probabilities( + SGVector mu, SGVector s2, std::shared_ptrlab) { return get_log_zeroth_moments(mu, s2, lab); } -SGVector CLikelihoodModel::get_log_probability_fmatrix( - const CLabels* lab, SGMatrix F) const +SGVector LikelihoodModel::get_log_probability_fmatrix( + std::shared_ptr lab, SGMatrix F) const { require(lab, "Given labels are NULL!"); require(lab->get_num_labels()==F.num_rows, "Number of labels ({}) does " @@ -69,8 +69,8 @@ SGVector CLikelihoodModel::get_log_probability_fmatrix( return result; } -SGVector CLikelihoodModel::get_first_moments(SGVector mu, - SGVector s2, const CLabels* lab) const +SGVector LikelihoodModel::get_first_moments(SGVector mu, + SGVector s2, std::shared_ptr lab) const { require(lab, "Labels are required (lab should not be NULL)"); require((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()), @@ -86,8 +86,8 @@ SGVector CLikelihoodModel::get_first_moments(SGVector mu, return result; } -SGVector CLikelihoodModel::get_second_moments(SGVector mu, - SGVector s2, const CLabels* lab) const +SGVector LikelihoodModel::get_second_moments(SGVector mu, + SGVector s2, std::shared_ptr lab) const { require(lab, "Labels are required (lab should not be NULL)"); require((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()), diff --git a/src/shogun/machine/gp/LikelihoodModel.h b/src/shogun/machine/gp/LikelihoodModel.h index 3e30b67b7cd..c30b5d425d0 100644 --- a/src/shogun/machine/gp/LikelihoodModel.h +++ b/src/shogun/machine/gp/LikelihoodModel.h @@ -59,13 +59,13 @@ enum ELikelihoodModelType * The Likelihood model computes approximately the distribution \f$p(y|f)\f$, * where \f$y\f$ are the labels, and \f$f\f$ is the prediction function. */ -class CLikelihoodModel : public CSGObject +class LikelihoodModel : public SGObject { public: /** default constructor */ - CLikelihoodModel(); + LikelihoodModel(); - virtual ~CLikelihoodModel(); + virtual ~LikelihoodModel(); /** returns the logarithm of the predictive density of \f$y_*\f$: * @@ -96,7 +96,7 @@ class CLikelihoodModel : public CSGObject */ virtual SGVector get_predictive_log_probabilities( SGVector mu, SGVector s2, - const CLabels* lab=NULL); + std::shared_ptr lab=NULL); /** returns mean of the predictive marginal \f$p(y_*|X,y,x_*)\f$ * @@ -113,7 +113,7 @@ class CLikelihoodModel : public CSGObject * @return final means evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const=0; + SGVector s2, std::shared_ptr lab=NULL) const=0; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$ * @@ -130,7 +130,7 @@ class CLikelihoodModel : public CSGObject * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const=0; + SGVector s2, std::shared_ptr lab=NULL) const=0; /** get model type * @@ -149,7 +149,7 @@ class CLikelihoodModel : public CSGObject * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const=0; /** Returns the log-likelihood \f$log(p(y|f)) = \sum_{i=1}^{n} @@ -164,7 +164,7 @@ class CLikelihoodModel : public CSGObject * * @return log-likelihood for every provided function */ - virtual SGVector get_log_probability_fmatrix(const CLabels* lab, + virtual SGVector get_log_probability_fmatrix(std::shared_ptr lab, SGMatrix F) const; /** get derivative of log likelihood \f$log(p(y|f))\f$ with respect to @@ -178,7 +178,7 @@ class CLikelihoodModel : public CSGObject * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const=0; + std::shared_ptr lab, SGVector func, index_t i) const=0; /** get derivative of log likelihood \f$log(p(y|f))\f$ with respect to given * parameter @@ -189,7 +189,7 @@ class CLikelihoodModel : public CSGObject * * @return derivative */ - virtual SGVector get_first_derivative(const CLabels* lab, + virtual SGVector get_first_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { error("Can't compute derivative wrt {} parameter", param->m_name); @@ -207,7 +207,7 @@ class CLikelihoodModel : public CSGObject * * @return derivative */ - virtual SGVector get_second_derivative(const CLabels* lab, + virtual SGVector get_second_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { error("Can't compute derivative wrt {} parameter", param->m_name); @@ -224,7 +224,7 @@ class CLikelihoodModel : public CSGObject * * @return derivative */ - virtual SGVector get_third_derivative(const CLabels* lab, + virtual SGVector get_third_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { error("Can't compute derivative wrt {} parameter", param->m_name); @@ -248,7 +248,7 @@ class CLikelihoodModel : public CSGObject * @return log zeroth moment \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const=0; + SGVector s2, std::shared_ptr lab) const=0; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -265,7 +265,7 @@ class CLikelihoodModel : public CSGObject * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const=0; + SGVector s2, std::shared_ptr lab, index_t i) const=0; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -281,7 +281,7 @@ class CLikelihoodModel : public CSGObject * @return the first moment of \f$q(f_i)\f$ for each \f$f_i\f$ */ virtual SGVector get_first_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** returns the second moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -298,7 +298,7 @@ class CLikelihoodModel : public CSGObject * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const=0; + SGVector s2, std::shared_ptr lab, index_t i) const=0; /** returns the second moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -314,7 +314,7 @@ class CLikelihoodModel : public CSGObject * @return the second moment of \f$q(f_i)\f$ for each \f$f_i\f$ */ virtual SGVector get_second_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** return whether likelihood function supports regression * diff --git a/src/shogun/machine/gp/LogitDVGLikelihood.cpp b/src/shogun/machine/gp/LogitDVGLikelihood.cpp index d5a478a81a4..f4d56667a21 100644 --- a/src/shogun/machine/gp/LogitDVGLikelihood.cpp +++ b/src/shogun/machine/gp/LogitDVGLikelihood.cpp @@ -43,24 +43,24 @@ using namespace Eigen; namespace shogun { -CLogitDVGLikelihood::CLogitDVGLikelihood() - : CDualVariationalGaussianLikelihood() +LogitDVGLikelihood::LogitDVGLikelihood() + : DualVariationalGaussianLikelihood() { init(); } -CLogitDVGLikelihood::~CLogitDVGLikelihood() +LogitDVGLikelihood::~LogitDVGLikelihood() { } -SGVector CLogitDVGLikelihood::get_variance_dual_parameter() const +SGVector LogitDVGLikelihood::get_variance_dual_parameter() const { SGVector lambda=m_lambda.clone(); return lambda; } -SGVector CLogitDVGLikelihood::get_mu_dual_parameter() const +SGVector LogitDVGLikelihood::get_mu_dual_parameter() const { SGVector alpha=m_lambda.clone(); @@ -70,14 +70,14 @@ SGVector CLogitDVGLikelihood::get_mu_dual_parameter() const return alpha; } -SGVector CLogitDVGLikelihood::get_dual_objective_value() +SGVector LogitDVGLikelihood::get_dual_objective_value() { SGVector result(m_lambda.vlen); if (!m_is_valid) { Map eigen_reslut(result.vector, result.vlen); - eigen_reslut.fill(CMath::INFTY); + eigen_reslut.fill(Math::INFTY); return result; } @@ -90,7 +90,7 @@ SGVector CLogitDVGLikelihood::get_dual_objective_value() return result; } -SGVector CLogitDVGLikelihood::get_dual_first_derivative( +SGVector LogitDVGLikelihood::get_dual_first_derivative( const TParameter* param) const { require(param, "Param is required (param should not be NULL)"); @@ -106,7 +106,7 @@ SGVector CLogitDVGLikelihood::get_dual_first_derivative( if (!m_is_valid) { Map eigen_reslut(result.vector, result.vlen); - eigen_reslut.fill(CMath::INFTY); + eigen_reslut.fill(Math::INFTY); return result; } @@ -118,12 +118,12 @@ SGVector CLogitDVGLikelihood::get_dual_first_derivative( return result; } -void CLogitDVGLikelihood::init_likelihood() +void LogitDVGLikelihood::init_likelihood() { - set_likelihood(new CLogitVGLikelihood()); + set_likelihood(std::make_shared()); } -void CLogitDVGLikelihood::init() +void LogitDVGLikelihood::init() { init_likelihood(); } diff --git a/src/shogun/machine/gp/LogitDVGLikelihood.h b/src/shogun/machine/gp/LogitDVGLikelihood.h index c4359355937..4c4fe9fca3d 100644 --- a/src/shogun/machine/gp/LogitDVGLikelihood.h +++ b/src/shogun/machine/gp/LogitDVGLikelihood.h @@ -71,13 +71,13 @@ namespace shogun * \f], where t is a local variable and the inequality holds for every t>0. * See Bernoulli-logit in Table 2 of the paper for detailed information */ -class CLogitDVGLikelihood : public CDualVariationalGaussianLikelihood +class LogitDVGLikelihood : public DualVariationalGaussianLikelihood { public: /** default constructor */ - CLogitDVGLikelihood(); + LogitDVGLikelihood(); - virtual ~CLogitDVGLikelihood(); + virtual ~LogitDVGLikelihood(); /** returns the name of the likelihood model * diff --git a/src/shogun/machine/gp/LogitLikelihood.cpp b/src/shogun/machine/gp/LogitLikelihood.cpp index 272a91d40c6..4b191beb2ad 100644 --- a/src/shogun/machine/gp/LogitLikelihood.cpp +++ b/src/shogun/machine/gp/LogitLikelihood.cpp @@ -47,11 +47,11 @@ namespace shogun #ifndef DOXYGEN_SHOULD_SKIP_THIS /** Class of the probability density function of the normal distribution */ -class CNormalPDF : public CFunction +class NormalPDF : public Function { public: /** default constructor */ - CNormalPDF() + NormalPDF() { m_mu=0.0; m_sigma=1.0; @@ -62,7 +62,7 @@ class CNormalPDF : public CFunction * @param mu mean * @param sigma standard deviation */ - CNormalPDF(float64_t mu, float64_t sigma) + NormalPDF(float64_t mu, float64_t sigma) { m_mu=mu; m_sigma=sigma; @@ -88,8 +88,8 @@ class CNormalPDF : public CFunction */ virtual float64_t operator() (float64_t x) { - return (1.0 / (std::sqrt(2 * CMath::PI) * m_sigma)) * - std::exp(-CMath::sq(x - m_mu) / (2.0 * CMath::sq(m_sigma))); + return (1.0 / (std::sqrt(2 * Math::PI) * m_sigma)) * + std::exp(-Math::sq(x - m_mu) / (2.0 * Math::sq(m_sigma))); } private: @@ -101,11 +101,11 @@ class CNormalPDF : public CFunction }; /** Class of the sigmoid function */ -class CSigmoidFunction : public CFunction +class SigmoidFunction : public Function { public: /** default constructor */ - CSigmoidFunction() + SigmoidFunction() { m_a=0.0; } @@ -114,7 +114,7 @@ class CSigmoidFunction : public CFunction * * @param a slope parameter */ - CSigmoidFunction(float64_t a) + SigmoidFunction(float64_t a) { m_a=a; } @@ -142,7 +142,7 @@ class CSigmoidFunction : public CFunction }; /** Class of the function, which is a product of two given functions */ -class CProductFunction : public CFunction +class ProductFunction : public Function { public: /** constructor @@ -150,18 +150,18 @@ class CProductFunction : public CFunction * @param f f(x) * @param g g(x) */ - CProductFunction(CFunction* f, CFunction* g) + ProductFunction(std::shared_ptr f, std::shared_ptr g) { - SG_REF(f); - SG_REF(g); + + m_f=f; m_g=g; } - virtual ~CProductFunction() + virtual ~ProductFunction() { - SG_UNREF(m_f); - SG_UNREF(m_g); + + } /** returns value of the function at given point @@ -177,19 +177,19 @@ class CProductFunction : public CFunction private: /** function f(x) */ - CFunction* m_f; + std::shared_ptr m_f; /** function g(x) */ - CFunction* m_g; + std::shared_ptr m_g; }; /** Class of the f(x)=x */ -class CLinearFunction : public CFunction +class LinearFunction : public Function { public: /** default constructor */ - CLinearFunction() { } + LinearFunction() { } - virtual ~CLinearFunction() { } + virtual ~LinearFunction() { } /** returns value of the function at given point * @@ -204,13 +204,13 @@ class CLinearFunction : public CFunction }; /** Class of the f(x)=x^2 */ -class CQuadraticFunction : public CFunction +class QuadraticFunction : public Function { public: /** default constructor */ - CQuadraticFunction() { } + QuadraticFunction() { } - virtual ~CQuadraticFunction() { } + virtual ~QuadraticFunction() { } /** returns value of the function at given point * @@ -220,22 +220,22 @@ class CQuadraticFunction : public CFunction */ virtual float64_t operator() (float64_t x) { - return CMath::sq(x); + return Math::sq(x); } }; #endif /* DOXYGEN_SHOULD_SKIP_THIS */ -CLogitLikelihood::CLogitLikelihood() : CLikelihoodModel() +LogitLikelihood::LogitLikelihood() : LikelihoodModel() { } -CLogitLikelihood::~CLogitLikelihood() +LogitLikelihood::~LogitLikelihood() { } -SGVector CLogitLikelihood::get_predictive_means( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector LogitLikelihood::get_predictive_means( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector lp=get_log_zeroth_moments(mu, s2, lab); Map eigen_lp(lp.vector, lp.vlen); @@ -252,8 +252,8 @@ SGVector CLogitLikelihood::get_predictive_means( return r; } -SGVector CLogitLikelihood::get_predictive_variances( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector LogitLikelihood::get_predictive_variances( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector lp=get_log_zeroth_moments(mu, s2, lab); Map eigen_lp(lp.vector, lp.vlen); @@ -270,17 +270,17 @@ SGVector CLogitLikelihood::get_predictive_variances( return r; } -SGVector CLogitLikelihood::get_log_probability_f(const CLabels* lab, +SGVector LogitLikelihood::get_log_probability_f(std::shared_ptr lab, SGVector func) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); Map eigen_f(func.vector, func.vlen); @@ -294,18 +294,18 @@ SGVector CLogitLikelihood::get_log_probability_f(const CLabels* lab, return r; } -SGVector CLogitLikelihood::get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const +SGVector LogitLikelihood::get_log_probability_derivative_f( + std::shared_ptr lab, SGVector func, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); require(i>=1 && i<=3, "Index for derivative should be 1, 2 or 3"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); Map eigen_f(func.vector, func.vlen); @@ -341,8 +341,8 @@ SGVector CLogitLikelihood::get_log_probability_derivative_f( return r; } -SGVector CLogitLikelihood::get_log_zeroth_moments( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector LogitLikelihood::get_log_zeroth_moments( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector y; @@ -353,9 +353,8 @@ SGVector CLogitLikelihood::get_log_zeroth_moments( "variances ({}) and number of labels ({}) should be the same", mu.vlen, s2.vlen, lab->get_num_labels()); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); - - y=((CBinaryLabels*)lab)->get_labels(); + "Labels must be type of BinaryLabels"); + y=lab->as()->get_labels(); } else { @@ -368,14 +367,14 @@ SGVector CLogitLikelihood::get_log_zeroth_moments( } // create an object of normal pdf function - CNormalPDF* f=new CNormalPDF(); + auto f=std::make_shared(); // create an object of sigmoid function - CSigmoidFunction* g=new CSigmoidFunction(); + auto g=std::make_shared(); // create an object of product of sigmoid and normal pdf functions - CProductFunction* h=new CProductFunction(f, g); - SG_REF(h); + auto h=std::make_shared(f, g); + // compute probabilities using numerical integration SGVector r(mu.vlen); @@ -391,14 +390,14 @@ SGVector CLogitLikelihood::get_log_zeroth_moments( // evaluate integral on (-inf, inf) #ifdef USE_GPL_SHOGUN - r[i]=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); + r[i]=Integration::integrate_quadgk(h, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(h, mu[i], Math::INFTY); #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN } - SG_UNREF(h); + for (index_t i=0; i CLogitLikelihood::get_log_zeroth_moments( return r; } -float64_t CLogitLikelihood::get_first_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t LogitLikelihood::get_first_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -417,44 +416,44 @@ float64_t CLogitLikelihood::get_first_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); // create an object of f(x)=N(x|mu,sigma^2) - CNormalPDF* f = new CNormalPDF(mu[i], std::sqrt(s2[i])); + auto f = std::make_shared(mu[i], std::sqrt(s2[i])); // create an object of g(x)=sigmoid(x) - CSigmoidFunction* g=new CSigmoidFunction(y[i]); + auto g=std::make_shared(y[i]); // create an object of h(x)=N(x|mu,sigma^2)*sigmoid(x) - CProductFunction* h=new CProductFunction(f, g); + auto h=std::make_shared(f, g); // create an object of l(x)=x - CLinearFunction* l=new CLinearFunction(); + auto l=std::make_shared(); // create an object of k(x)=x*N(x|mu,sigma^2)*sigmoid(x) - CProductFunction* k=new CProductFunction(l, h); - SG_REF(k); + auto k=std::make_shared(l, h); + float64_t Ex=0; #ifdef USE_GPL_SHOGUN // compute Z = \int N(x|mu,sigma)*sigmoid(a*x) dx - float64_t Z=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); + float64_t Z=Integration::integrate_quadgk(h, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(h, mu[i], Math::INFTY); // compute 1st moment: E[x] = Z^-1 * \int x*N(x|mu,sigma)*sigmoid(a*x)dx - Ex=(CIntegration::integrate_quadgk(k, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(k, mu[i], CMath::INFTY))/Z; + Ex=(Integration::integrate_quadgk(k, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(k, mu[i], Math::INFTY))/Z; #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN - SG_UNREF(k); + return Ex; } -float64_t CLogitLikelihood::get_second_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t LogitLikelihood::get_second_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -464,55 +463,55 @@ float64_t CLogitLikelihood::get_second_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); // create an object of f(x)=N(x|mu,sigma^2) - CNormalPDF* f = new CNormalPDF(mu[i], std::sqrt(s2[i])); + auto f = std::make_shared(mu[i], std::sqrt(s2[i])); // create an object of g(x)=sigmoid(a*x) - CSigmoidFunction* g=new CSigmoidFunction(y[i]); + auto g=std::make_shared(y[i]); // create an object of h(x)=N(x|mu,sigma^2)*sigmoid(a*x) - CProductFunction* h=new CProductFunction(f, g); + auto h=std::make_shared(f, g); // create an object of l(x)=x - CLinearFunction* l=new CLinearFunction(); + auto l=std::make_shared(); // create an object of k(x)=x*N(x|mu,sigma^2)*sigmoid(a*x) - CProductFunction* k=new CProductFunction(l, h); - SG_REF(k); + auto k=std::make_shared(l, h); + // create an object of q(x)=x^2 - CQuadraticFunction* q=new CQuadraticFunction(); + auto q=std::make_shared(); // create an object of p(x)=x^2*N(x|mu,sigma^2)*sigmoid(x) - CProductFunction* p=new CProductFunction(q, h); - SG_REF(p); + auto p=std::make_shared(q, h); + float64_t Ex=0; float64_t Ex2=0; #ifdef USE_GPL_SHOGUN // compute Z = \int N(x|mu,sigma)*sigmoid(a*x) dx - float64_t Z=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); + float64_t Z=Integration::integrate_quadgk(h, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(h, mu[i], Math::INFTY); // compute 1st moment: E[x] = Z^-1 * \int x*N(x|mu,sigma)*sigmoid(a*x)dx - Ex=(CIntegration::integrate_quadgk(k, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(k, mu[i], CMath::INFTY))/Z; + Ex=(Integration::integrate_quadgk(k, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(k, mu[i], Math::INFTY))/Z; // compute E[x^2] = Z^-1 * \int x^2*N(x|mu,sigma)*sigmoid(a*x)dx - Ex2=(CIntegration::integrate_quadgk(p, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(p, mu[i], CMath::INFTY))/Z; + Ex2=(Integration::integrate_quadgk(p, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(p, mu[i], Math::INFTY))/Z; #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN - SG_UNREF(k); - SG_UNREF(p); + + // return 2nd moment: Var[x]=E[x^2]-E[x]^2 - return Ex2-CMath::sq(Ex);; + return Ex2-Math::sq(Ex);; } } diff --git a/src/shogun/machine/gp/LogitLikelihood.h b/src/shogun/machine/gp/LogitLikelihood.h index 57251547a3d..9396c45fedd 100644 --- a/src/shogun/machine/gp/LogitLikelihood.h +++ b/src/shogun/machine/gp/LogitLikelihood.h @@ -46,13 +46,13 @@ namespace shogun * p(y|f) = \prod_{i=1}^n \frac{1}{1+exp(-y_i*f_i)} * \f] */ -class CLogitLikelihood : public CLikelihoodModel +class LogitLikelihood : public LikelihoodModel { public: /** default constructor */ - CLogitLikelihood(); + LogitLikelihood(); - virtual ~CLogitLikelihood(); + virtual ~LogitLikelihood(); /** returns the name of the likelihood model * @@ -75,7 +75,7 @@ class CLogitLikelihood : public CLikelihoodModel * @return final means evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$. * @@ -92,7 +92,7 @@ class CLogitLikelihood : public CLikelihoodModel * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** get model type * @@ -111,7 +111,7 @@ class CLogitLikelihood : public CLikelihoodModel * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const; /** get derivative of log likelihood \f$log(P(y|f))\f$ with respect to @@ -125,7 +125,7 @@ class CLogitLikelihood : public CLikelihoodModel * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const; + std::shared_ptr lab, SGVector func, index_t i) const; /** returns the zeroth moment of a given (unnormalized) probability * distribution: @@ -144,7 +144,7 @@ class CLogitLikelihood : public CLikelihoodModel * @return log zeroth moments \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -161,7 +161,7 @@ class CLogitLikelihood : public CLikelihoodModel * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** returns the second moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -178,7 +178,7 @@ class CLogitLikelihood : public CLikelihoodModel * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** return whether logit likelihood function supports binary classification * * @return true diff --git a/src/shogun/machine/gp/LogitVGLikelihood.cpp b/src/shogun/machine/gp/LogitVGLikelihood.cpp index 63fe2716fbe..fc50b80bb8a 100644 --- a/src/shogun/machine/gp/LogitVGLikelihood.cpp +++ b/src/shogun/machine/gp/LogitVGLikelihood.cpp @@ -46,22 +46,22 @@ using namespace Eigen; namespace shogun { -CLogitVGLikelihood::CLogitVGLikelihood() - : CNumericalVGLikelihood() +LogitVGLikelihood::LogitVGLikelihood() + : NumericalVGLikelihood() { init(); } -CLogitVGLikelihood::~CLogitVGLikelihood() +LogitVGLikelihood::~LogitVGLikelihood() { } -void CLogitVGLikelihood::init_likelihood() +void LogitVGLikelihood::init_likelihood() { - set_likelihood(new CLogitLikelihood()); + set_likelihood(std::make_shared()); } -void CLogitVGLikelihood::init() +void LogitVGLikelihood::init() { init_likelihood(); } diff --git a/src/shogun/machine/gp/LogitVGLikelihood.h b/src/shogun/machine/gp/LogitVGLikelihood.h index 3ca642c6f5b..0d829317792 100644 --- a/src/shogun/machine/gp/LogitVGLikelihood.h +++ b/src/shogun/machine/gp/LogitVGLikelihood.h @@ -54,12 +54,12 @@ template class SGMatrix; * \f] * */ -class CLogitVGLikelihood : public CNumericalVGLikelihood +class LogitVGLikelihood : public NumericalVGLikelihood { public: - CLogitVGLikelihood(); + LogitVGLikelihood(); - virtual ~CLogitVGLikelihood(); + virtual ~LogitVGLikelihood(); /** returns the name of the likelihood model * diff --git a/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.cpp b/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.cpp index e97d7670d15..dd2b1783df7 100644 --- a/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.cpp +++ b/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.cpp @@ -51,22 +51,22 @@ using namespace Eigen; namespace shogun { -CLogitVGPiecewiseBoundLikelihood::CLogitVGPiecewiseBoundLikelihood() - : CVariationalGaussianLikelihood() +LogitVGPiecewiseBoundLikelihood::LogitVGPiecewiseBoundLikelihood() + : VariationalGaussianLikelihood() { init(); } -CLogitVGPiecewiseBoundLikelihood::~CLogitVGPiecewiseBoundLikelihood() +LogitVGPiecewiseBoundLikelihood::~LogitVGPiecewiseBoundLikelihood() { } -void CLogitVGPiecewiseBoundLikelihood::set_variational_bound(SGMatrix bound) +void LogitVGPiecewiseBoundLikelihood::set_variational_bound(SGMatrix bound) { m_bound = bound; } -void CLogitVGPiecewiseBoundLikelihood::set_default_variational_bound() +void LogitVGPiecewiseBoundLikelihood::set_default_variational_bound() { index_t row = 20; index_t col = 5; @@ -135,7 +135,7 @@ void CLogitVGPiecewiseBoundLikelihood::set_default_variational_bound() m_bound(18,2)=0.000397791059000; m_bound(19,2)=0; - m_bound(0,3)=-CMath::INFTY; + m_bound(0,3)=-Math::INFTY; m_bound(1,3)=-8.575194939999999; m_bound(2,3)=-5.933689180000000; m_bound(3,3)=-4.525933600000000; @@ -175,10 +175,10 @@ void CLogitVGPiecewiseBoundLikelihood::set_default_variational_bound() m_bound(16,4)=4.525933600000000; m_bound(17,4)=5.933689180000000; m_bound(18,4)=8.575194939999999; - m_bound(19,4)= CMath::INFTY; + m_bound(19,4)= Math::INFTY; } -SGVector CLogitVGPiecewiseBoundLikelihood::get_variational_expection() +SGVector LogitVGPiecewiseBoundLikelihood::get_variational_expection() { //This function is based on the Matlab code, //function [f, gm, gv] = Ellp(m, v, bound, ind), to compute f @@ -245,7 +245,7 @@ SGVector CLogitVGPiecewiseBoundLikelihood::get_variational_expection( } -SGVector CLogitVGPiecewiseBoundLikelihood::get_variational_first_derivative( +SGVector LogitVGPiecewiseBoundLikelihood::get_variational_first_derivative( const TParameter* param) const { //This function is based on the Matlab code @@ -355,23 +355,23 @@ SGVector CLogitVGPiecewiseBoundLikelihood::get_variational_first_deri return result; } -bool CLogitVGPiecewiseBoundLikelihood::set_variational_distribution(SGVector mu, - SGVector s2, const CLabels* lab) +bool LogitVGPiecewiseBoundLikelihood::set_variational_distribution(SGVector mu, + SGVector s2, std::shared_ptr lab) { bool status = true; - status=CVariationalGaussianLikelihood::set_variational_distribution(mu, s2, lab); + status=VariationalGaussianLikelihood::set_variational_distribution(mu, s2, lab); if (status) { require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); - m_lab = (((CBinaryLabels*)lab)->get_labels()).clone(); + m_lab = lab->as()->get_labels().clone(); //Convert the input label to standard label used in the class //Note that Shogun uses -1 and 1 as labels and this class internally uses //0 and 1 repectively. for(index_t i = 0; i < m_lab.size(); ++i) - m_lab[i] = CMath::max(m_lab[i], 0.0); + m_lab[i] = std::max(m_lab[i], 0.0); precompute(); @@ -379,12 +379,12 @@ bool CLogitVGPiecewiseBoundLikelihood::set_variational_distribution(SGVector()); } -void CLogitVGPiecewiseBoundLikelihood::init() +void LogitVGPiecewiseBoundLikelihood::init() { SG_ADD(&m_bound, "bound", "Variational piecewise bound for logit likelihood"); @@ -404,7 +404,7 @@ void CLogitVGPiecewiseBoundLikelihood::init() init_likelihood(); } -void CLogitVGPiecewiseBoundLikelihood::precompute() +void LogitVGPiecewiseBoundLikelihood::precompute() { //This function is based on the Matlab code //function [f, gm, gv] = Ellp(m, v, bound, ind), to compute common variables later @@ -459,17 +459,17 @@ void CLogitVGPiecewiseBoundLikelihood::precompute() { for (index_t c = 0; c < zl.num_cols; c++) { - if (CMath::abs(zl(r, c)) == CMath::INFTY) + if (Math::abs(zl(r, c)) == Math::INFTY) m_pl(r, c) = 0; else m_pl(r, c) = std::exp( - CGaussianDistribution::univariate_log_pdf(zl(r, c))); + GaussianDistribution::univariate_log_pdf(zl(r, c))); - if (CMath::abs(zh(r, c)) == CMath::INFTY) + if (Math::abs(zh(r, c)) == Math::INFTY) m_ph(r, c) = 0; else m_ph(r, c) = std::exp( - CGaussianDistribution::univariate_log_pdf(zh(r, c))); + GaussianDistribution::univariate_log_pdf(zh(r, c))); } } @@ -486,9 +486,9 @@ void CLogitVGPiecewiseBoundLikelihood::precompute() for (index_t c = 0; c < zl.num_cols; c++) { //cl = 0.5*erf(zl/sqrt(2)); %normal cdf -const - cl(r, c) = CStatistics::normal_cdf(zl(r, c)) - 0.5; + cl(r, c) = Statistics::normal_cdf(zl(r, c)) - 0.5; //ch = 0.5*erf(zl/sqrt(2)); %normal cdf -const - ch(r, c) = CStatistics::normal_cdf(zh(r, c)) - 0.5; + ch(r, c) = Statistics::normal_cdf(zh(r, c)) - 0.5; } } @@ -515,7 +515,7 @@ void CLogitVGPiecewiseBoundLikelihood::precompute() eigen_h(eigen_h.size()-1) = h_bak; } -SGVector CLogitVGPiecewiseBoundLikelihood::get_first_derivative_wrt_hyperparameter( +SGVector LogitVGPiecewiseBoundLikelihood::get_first_derivative_wrt_hyperparameter( const TParameter* param) const { return SGVector(); diff --git a/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.h b/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.h index 66e6f61ca5d..20938aab9e1 100644 --- a/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.h +++ b/src/shogun/machine/gp/LogitVGPiecewiseBoundLikelihood.h @@ -60,12 +60,12 @@ namespace shogun * m is the size of the pre-defined bound, which is the num_rows of bound passed in set_bound * (In the reference Matlab code, m is 20) */ -class CLogitVGPiecewiseBoundLikelihood : public CVariationalGaussianLikelihood +class LogitVGPiecewiseBoundLikelihood : public VariationalGaussianLikelihood { public: - CLogitVGPiecewiseBoundLikelihood(); + LogitVGPiecewiseBoundLikelihood(); - virtual ~CLogitVGPiecewiseBoundLikelihood(); + virtual ~LogitVGPiecewiseBoundLikelihood(); /** returns the name of the likelihood model * @@ -87,7 +87,7 @@ class CLogitVGPiecewiseBoundLikelihood : public CVariationalGaussianLikelihood * @return true if variational parameters are valid * */ - virtual bool set_variational_distribution(SGVector mu, SGVector s2, const CLabels* lab); + virtual bool set_variational_distribution(SGVector mu, SGVector s2, std::shared_ptr lab); /** returns the expection of the logarithm of a logit distribution * wrt the variational distribution using piecewise bound diff --git a/src/shogun/machine/gp/MatrixOperations.cpp b/src/shogun/machine/gp/MatrixOperations.cpp index f212891817b..2e0e91144a4 100644 --- a/src/shogun/machine/gp/MatrixOperations.cpp +++ b/src/shogun/machine/gp/MatrixOperations.cpp @@ -70,7 +70,7 @@ SGMatrix CMatrixOperations::get_choleksy(SGVector W, VectorXd eigen_iW=(VectorXd::Ones(eigen_W.rows())).cwiseQuotient(eigen_W); FullPivLU lu( - eigen_kernel*CMath::sq(scale)+MatrixXd(eigen_iW.asDiagonal())); + eigen_kernel*Math::sq(scale)+MatrixXd(eigen_iW.asDiagonal())); // compute cholesky: L = -(K + iW)^-1 eigen_L=-lu.inverse(); @@ -79,7 +79,7 @@ SGMatrix CMatrixOperations::get_choleksy(SGVector W, { // compute cholesky: L = chol(sW * sW' .* K + I) LLT llt( - (eigen_sW*eigen_sW.transpose()).cwiseProduct(eigen_kernel*CMath::sq(scale))+ + (eigen_sW*eigen_sW.transpose()).cwiseProduct(eigen_kernel*Math::sq(scale))+ MatrixXd::Identity(eigen_kernel.rows(), eigen_kernel.cols())); eigen_L=llt.matrixU(); @@ -99,7 +99,7 @@ SGMatrix CMatrixOperations::get_inverse(SGMatrix L, SGMatr // compute V = L^(-1) * W^(1/2) * K, using upper triangular factor L^T eigen_V=eigen_L.triangularView().adjoint().solve( - eigen_sW.asDiagonal()*eigen_kernel*CMath::sq(scale)); + eigen_sW.asDiagonal()*eigen_kernel*Math::sq(scale)); return get_inverse(L, kernel, sW, V, scale); } @@ -134,7 +134,7 @@ SGMatrix CMatrixOperations::get_inverse(SGMatrix L, // Sigma = K - K * W^(1/2) * (L * L^T)^(-1) * W^(1/2) * K = // K - (K * W^(1/2)) * (L^T)^(-1) * L^(-1) * W^(1/2) * K = // K - (W^(1/2) * K)^T * (L^(-1))^T * L^(-1) * W^(1/2) * K = K - V^T * V - eigen_tmp=eigen_kernel*CMath::sq(scale)-eigen_V.adjoint()*eigen_V; + eigen_tmp=eigen_kernel*Math::sq(scale)-eigen_V.adjoint()*eigen_V; return tmp; } diff --git a/src/shogun/machine/gp/MeanFunction.h b/src/shogun/machine/gp/MeanFunction.h index 4f8dab93ddc..bfe89e0c628 100644 --- a/src/shogun/machine/gp/MeanFunction.h +++ b/src/shogun/machine/gp/MeanFunction.h @@ -46,13 +46,13 @@ namespace shogun * This class takes the mean of data used for Gaussian Process Regression. It * also includes the derivatives of the specified function. */ -class CMeanFunction : public CSGObject +class MeanFunction : public SGObject { public: /** constructor */ - CMeanFunction() { } + MeanFunction() { } - virtual ~CMeanFunction() { } + virtual ~MeanFunction() { } /** returns the mean of the specified data * @@ -60,7 +60,7 @@ class CMeanFunction : public CSGObject * * @return mean of feature vectors */ - virtual SGVector get_mean_vector(const CFeatures* features) const=0; + virtual SGVector get_mean_vector(std::shared_ptr features) const=0; /** returns the derivative of the mean function * @@ -70,7 +70,7 @@ class CMeanFunction : public CSGObject * * @return derivative of mean function with respect to parameter */ - virtual SGVector get_parameter_derivative(const CFeatures* features, + virtual SGVector get_parameter_derivative(std::shared_ptr features, const TParameter* param, index_t index=-1) { error("Can't compute derivative wrt {} parameter", param->m_name); diff --git a/src/shogun/machine/gp/MultiLaplaceInferenceMethod.cpp b/src/shogun/machine/gp/MultiLaplaceInferenceMethod.cpp index 346dba7dec1..ad8020a0dfb 100644 --- a/src/shogun/machine/gp/MultiLaplaceInferenceMethod.cpp +++ b/src/shogun/machine/gp/MultiLaplaceInferenceMethod.cpp @@ -68,13 +68,13 @@ class CMultiPsiLine : public func_base SGVector* dlp; SGVector* f; SGVector* m; - CLikelihoodModel* lik; - CLabels* lab; + std::shared_ptr lik; + std::shared_ptr lab; virtual double operator() (double x) { - const index_t C=((CMulticlassLabels*)lab)->get_num_classes(); - const index_t n=((CMulticlassLabels*)lab)->get_num_labels(); + const index_t C=multiclass_labels(lab)->get_num_classes(); + const index_t n=multiclass_labels(lab)->get_num_labels(); Map eigen_f(f->vector, f->vlen); Map eigen_m(m->vector, m->vlen); @@ -102,19 +102,19 @@ class CMultiPsiLine : public func_base #endif /* DOXYGEN_SHOULD_SKIP_THIS */ -CMultiLaplaceInferenceMethod::CMultiLaplaceInferenceMethod() : CLaplaceInference() +MultiLaplaceInferenceMethod::MultiLaplaceInferenceMethod() : LaplaceInference() { init(); } -CMultiLaplaceInferenceMethod::CMultiLaplaceInferenceMethod(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CLaplaceInference(kern, feat, m, lab, mod) +MultiLaplaceInferenceMethod::MultiLaplaceInferenceMethod(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : LaplaceInference(kern, feat, m, lab, mod) { init(); } -void CMultiLaplaceInferenceMethod::init() +void MultiLaplaceInferenceMethod::init() { m_iter=20; m_tolerance=1e-6; @@ -131,21 +131,21 @@ void CMultiLaplaceInferenceMethod::init() SG_ADD(&m_opt_max, "opt_max", "max iterations for Brent's minimization method"); } -CMultiLaplaceInferenceMethod::~CMultiLaplaceInferenceMethod() +MultiLaplaceInferenceMethod::~MultiLaplaceInferenceMethod() { } -void CMultiLaplaceInferenceMethod::check_members() const +void MultiLaplaceInferenceMethod::check_members() const { - CInference::check_members(); + Inference::check_members(); require(m_labels->get_label_type()==LT_MULTICLASS, - "Labels must be type of CMulticlassLabels"); + "Labels must be type of MulticlassLabels"); require(m_model->supports_multiclass(), "likelihood model should support multi-classification"); } -SGVector CMultiLaplaceInferenceMethod::get_diagonal_vector() +SGVector MultiLaplaceInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); @@ -155,7 +155,7 @@ SGVector CMultiLaplaceInferenceMethod::get_diagonal_vector() return SGVector(m_W); } -float64_t CMultiLaplaceInferenceMethod::get_negative_log_marginal_likelihood() +float64_t MultiLaplaceInferenceMethod::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); @@ -163,7 +163,7 @@ float64_t CMultiLaplaceInferenceMethod::get_negative_log_marginal_likelihood() return m_nlz; } -SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector MultiLaplaceInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { //SoftMax likelihood does not have this kind of derivative @@ -171,24 +171,23 @@ SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_likelihood_ return SGVector (); } -CMultiLaplaceInferenceMethod* CMultiLaplaceInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr MultiLaplaceInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_LAPLACE_MULTIPLE) - error("Provided inference is not of type CMultiLaplaceInferenceMethod!"); + error("Provided inference is not of type MultiLaplaceInferenceMethod!"); - SG_REF(inference); - return (CMultiLaplaceInferenceMethod*)inference; + return inference->as(); } -void CMultiLaplaceInferenceMethod::update_approx_cov() +void MultiLaplaceInferenceMethod::update_approx_cov() { //Sigma=K-K*(E-E*R(M*M')^{-1}*R'*E)*K - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); const index_t n=m_labels->get_num_labels(); Map eigen_M(m_L.matrix, m_L.num_rows, m_L.num_cols); Map eigen_E(m_E.matrix, m_E.num_rows, m_E.num_cols); @@ -211,13 +210,13 @@ void CMultiLaplaceInferenceMethod::update_approx_cov() eigen_Sigma+=eigen_V.transpose()*eigen_V; } -void CMultiLaplaceInferenceMethod::update_chol() +void MultiLaplaceInferenceMethod::update_chol() { } -void CMultiLaplaceInferenceMethod::get_dpi_helper() +void MultiLaplaceInferenceMethod::get_dpi_helper() { - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); const index_t n=m_labels->get_num_labels(); Map eigen_dpi(m_W.vector, m_W.vlen); Map eigen_dpi_matrix(eigen_dpi.data(),n,C); @@ -236,12 +235,12 @@ void CMultiLaplaceInferenceMethod::get_dpi_helper() //eigen_dpi_matrix=eigen_dpi_matrix.array().colwise()/tmp_for_dpi.array(); } -void CMultiLaplaceInferenceMethod::update_alpha() +void MultiLaplaceInferenceMethod::update_alpha() { - float64_t Psi_Old = CMath::INFTY; + float64_t Psi_Old = Math::INFTY; float64_t Psi_New; float64_t Psi_Def; - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); const index_t n=m_labels->get_num_labels(); // get mean vector and create eigen representation of it @@ -394,9 +393,9 @@ void CMultiLaplaceInferenceMethod::update_alpha() } } -void CMultiLaplaceInferenceMethod::update_deriv() +void MultiLaplaceInferenceMethod::update_deriv() { - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); const index_t n=m_labels->get_num_labels(); m_U=SGMatrix(n, n*C); Map eigen_U(m_U.matrix, m_U.num_rows, m_U.num_cols); @@ -405,11 +404,11 @@ void CMultiLaplaceInferenceMethod::update_deriv() eigen_U=eigen_M.triangularView().adjoint().solve(eigen_E); } -float64_t CMultiLaplaceInferenceMethod::get_derivative_helper(SGMatrix dK) +float64_t MultiLaplaceInferenceMethod::get_derivative_helper(SGMatrix dK) { Map eigen_dK(dK.matrix, dK.num_rows, dK.num_cols); //currently only explicit term is computed - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); const index_t n=m_labels->get_num_labels(); Map eigen_U(m_U.matrix, m_U.num_rows, m_U.num_cols); Map eigen_E(m_E.matrix, m_E.num_rows, m_E.num_cols); @@ -426,7 +425,7 @@ float64_t CMultiLaplaceInferenceMethod::get_derivative_helper(SGMatrix CMultiLaplaceInferenceMethod::get_derivative_wrt_inference_method( +SGVector MultiLaplaceInferenceMethod::get_derivative_wrt_inference_method( const TParameter* param) { require(!strcmp(param->m_name, "log_scale"), "Can't compute derivative of " @@ -445,7 +444,7 @@ SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_inference_m return result; } -SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_kernel( +SGVector MultiLaplaceInferenceMethod::get_derivative_wrt_kernel( const TParameter* param) { // create eigen representation of K, Z, dfhat, dlp and alpha @@ -472,13 +471,13 @@ SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_kernel( return result; } -SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_mean( +SGVector MultiLaplaceInferenceMethod::get_derivative_wrt_mean( const TParameter* param) { // create eigen representation of K, Z, dfhat and alpha Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); Map eigen_alpha(m_alpha.vector, m_alpha.vlen); - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); const index_t n=m_labels->get_num_labels(); require(param, "Param not set"); @@ -506,13 +505,13 @@ SGVector CMultiLaplaceInferenceMethod::get_derivative_wrt_mean( return result; } -SGVector CMultiLaplaceInferenceMethod::get_posterior_mean() +SGVector MultiLaplaceInferenceMethod::get_posterior_mean() { compute_gradient(); SGVector res(m_mu.vlen); Map eigen_res(res.vector, res.vlen); - const index_t C=((CMulticlassLabels*)m_labels)->get_num_classes(); + const index_t C=multiclass_labels(m_labels)->get_num_classes(); SGVector mean=m_mean->get_mean_vector(m_features); Map eigen_mean_bl(mean.vector, mean.vlen); diff --git a/src/shogun/machine/gp/MultiLaplaceInferenceMethod.h b/src/shogun/machine/gp/MultiLaplaceInferenceMethod.h index 57e26bdecfa..e87e8a30596 100644 --- a/src/shogun/machine/gp/MultiLaplaceInferenceMethod.h +++ b/src/shogun/machine/gp/MultiLaplaceInferenceMethod.h @@ -66,11 +66,11 @@ namespace shogun * * The reference pseudo code is the algorithm 3.3 of the GPML textbook */ -class CMultiLaplaceInferenceMethod: public CLaplaceInference +class MultiLaplaceInferenceMethod: public LaplaceInference { public: /** default constructor */ - CMultiLaplaceInferenceMethod(); + MultiLaplaceInferenceMethod(); /** constructor * @@ -80,10 +80,10 @@ class CMultiLaplaceInferenceMethod: public CLaplaceInference * @param labels labels of the features * @param model Likelihood model to use */ - CMultiLaplaceInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + MultiLaplaceInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CMultiLaplaceInferenceMethod(); + virtual ~MultiLaplaceInferenceMethod(); /** returns the name of the inference method * @@ -102,9 +102,9 @@ class CMultiLaplaceInferenceMethod: public CLaplaceInference /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CMultiLaplaceInferenceMethod object + * @return casted MultiLaplaceInferenceMethod object */ - static CMultiLaplaceInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get negative log marginal likelihood * diff --git a/src/shogun/machine/gp/NumericalVGLikelihood.cpp b/src/shogun/machine/gp/NumericalVGLikelihood.cpp index f58a79f260f..334d0d487f4 100644 --- a/src/shogun/machine/gp/NumericalVGLikelihood.cpp +++ b/src/shogun/machine/gp/NumericalVGLikelihood.cpp @@ -53,17 +53,17 @@ using namespace Eigen; namespace shogun { -CNumericalVGLikelihood::CNumericalVGLikelihood() - : CVariationalGaussianLikelihood() +NumericalVGLikelihood::NumericalVGLikelihood() + : VariationalGaussianLikelihood() { init(); } -CNumericalVGLikelihood::~CNumericalVGLikelihood() +NumericalVGLikelihood::~NumericalVGLikelihood() { } -void CNumericalVGLikelihood::init() +void NumericalVGLikelihood::init() { SG_ADD(&m_log_lam, "log_lam", "The result of used for computing variational expection\n"); @@ -84,7 +84,7 @@ void CNumericalVGLikelihood::init() } -void CNumericalVGLikelihood::set_GHQ_number(index_t n) +void NumericalVGLikelihood::set_GHQ_number(index_t n) { require(n>0, "The number ({}) of Gaussian Hermite point should be positive",n); if (m_GHQ_N!=n) @@ -94,7 +94,7 @@ void CNumericalVGLikelihood::set_GHQ_number(index_t n) } } -SGVector CNumericalVGLikelihood::get_first_derivative_wrt_hyperparameter( +SGVector NumericalVGLikelihood::get_first_derivative_wrt_hyperparameter( const TParameter* param) const { require(param, "Param is required (param should not be NULL)"); @@ -107,12 +107,12 @@ SGVector CNumericalVGLikelihood::get_first_derivative_wrt_hyperparame Map eigen_res(res.vector, res.vlen); //ll = ll + w(i)*lp; - CLabels* lab=NULL; + std::shared_ptr lab=NULL; if (supports_binary()) - lab=new CBinaryLabels(m_lab); + lab=std::make_shared(m_lab); else if (supports_regression()) - lab=new CRegressionLabels(m_lab); + lab=std::make_shared(m_lab); for (index_t cidx = 0; cidx < m_log_lam.num_cols; cidx++) { @@ -122,24 +122,24 @@ SGVector CNumericalVGLikelihood::get_first_derivative_wrt_hyperparame eigen_res+=eigen_lp*m_wgh[cidx]; } - SG_UNREF(lab); + return res; } -SGVector CNumericalVGLikelihood::get_variational_expection() +SGVector NumericalVGLikelihood::get_variational_expection() { SGVector res(m_lab.vlen); res.zero(); Map eigen_res(res.vector, res.vlen); //ll = ll + w(i)*lp; - CLabels* lab=NULL; + std::shared_ptr lab=NULL; if (supports_binary()) - lab=new CBinaryLabels(m_lab); + lab=std::make_shared(m_lab); else if (supports_regression()) - lab=new CRegressionLabels(m_lab); + lab=std::make_shared(m_lab); for (index_t cidx = 0; cidx < m_log_lam.num_cols; cidx++) { @@ -149,12 +149,12 @@ SGVector CNumericalVGLikelihood::get_variational_expection() eigen_res+=eigen_lp*m_wgh[cidx]; } - SG_UNREF(lab); + return res; } -SGVector CNumericalVGLikelihood::get_variational_first_derivative( +SGVector NumericalVGLikelihood::get_variational_first_derivative( const TParameter* param) const { //based on the likKL(v, lik, varargin) function in infKL.m @@ -175,12 +175,12 @@ SGVector CNumericalVGLikelihood::get_variational_first_derivative( Map eigen_v(m_s2.vector, m_s2.vlen); - CLabels* lab=NULL; + std::shared_ptr lab=NULL; if (supports_binary()) - lab=new CBinaryLabels(m_lab); + lab=std::make_shared(m_lab); else if (supports_regression()) - lab=new CRegressionLabels(m_lab); + lab=std::make_shared(m_lab); if (strcmp(param->m_name, "mu")==0) { @@ -212,47 +212,47 @@ SGVector CNumericalVGLikelihood::get_variational_first_derivative( } } - SG_UNREF(lab); + return res; } -bool CNumericalVGLikelihood::set_variational_distribution(SGVector mu, - SGVector s2, const CLabels* lab) +bool NumericalVGLikelihood::set_variational_distribution(SGVector mu, + SGVector s2, std::shared_ptr lab) { bool status = true; - status = CVariationalGaussianLikelihood::set_variational_distribution(mu, s2, lab); + status = VariationalGaussianLikelihood::set_variational_distribution(mu, s2, lab); if (status) { if (supports_binary()) { require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); } else { if (supports_regression()) { require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); } else error("Unsupported Label type"); } if (supports_binary()) - m_lab=((CBinaryLabels*)lab)->get_labels(); + m_lab=lab->as()->get_labels(); else - m_lab=((CRegressionLabels*)lab)->get_labels(); + m_lab=lab->as()->get_labels(); if (!m_is_init_GHQ) { m_xgh=SGVector(m_GHQ_N); m_wgh=SGVector(m_GHQ_N); #ifdef USE_GPL_SHOGUN - CIntegration::generate_gauher(m_xgh, m_wgh); + Integration::generate_gauher(m_xgh, m_wgh); #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN @@ -266,7 +266,7 @@ bool CNumericalVGLikelihood::set_variational_distribution(SGVector mu return status; } -void CNumericalVGLikelihood::precompute() +void NumericalVGLikelihood::precompute() { //samples-by-abscissas m_log_lam=SGMatrix(m_s2.vlen, m_xgh.vlen); diff --git a/src/shogun/machine/gp/NumericalVGLikelihood.h b/src/shogun/machine/gp/NumericalVGLikelihood.h index 58c522796a1..af9e3eec4be 100644 --- a/src/shogun/machine/gp/NumericalVGLikelihood.h +++ b/src/shogun/machine/gp/NumericalVGLikelihood.h @@ -57,12 +57,12 @@ template class SGMatrix; * \sum_{{i=1}^n}{E_{q(f_i|{\mu}_i,{\sigma}^2_i)}[logP(y_i|f_i)]} * \f] */ -class CNumericalVGLikelihood : public CVariationalGaussianLikelihood +class NumericalVGLikelihood : public VariationalGaussianLikelihood { public: - CNumericalVGLikelihood(); + NumericalVGLikelihood(); - virtual ~CNumericalVGLikelihood(); + virtual ~NumericalVGLikelihood(); /** returns the name of the likelihood model * @@ -79,7 +79,7 @@ class CNumericalVGLikelihood : public CVariationalGaussianLikelihood * */ virtual bool set_variational_distribution(SGVector mu, - SGVector s2, const CLabels* lab); + SGVector s2, std::shared_ptr lab); /** returns the expection of the logarithm of a logit distribution * wrt the variational distribution using numerical integration @@ -126,7 +126,7 @@ class CNumericalVGLikelihood : public CVariationalGaussianLikelihood protected: - /** The function used to initialize m_likelihood defined in CVariationalLikelihood + /** The function used to initialize m_likelihood defined in VariationalLikelihood * Note that for some compiler removing this line will issue an error * */ virtual void init_likelihood()=0; diff --git a/src/shogun/machine/gp/ProbitLikelihood.cpp b/src/shogun/machine/gp/ProbitLikelihood.cpp index f52760ee024..4e14af8e790 100644 --- a/src/shogun/machine/gp/ProbitLikelihood.cpp +++ b/src/shogun/machine/gp/ProbitLikelihood.cpp @@ -38,16 +38,16 @@ using namespace shogun; using namespace Eigen; -CProbitLikelihood::CProbitLikelihood() +ProbitLikelihood::ProbitLikelihood() { } -CProbitLikelihood::~CProbitLikelihood() +ProbitLikelihood::~ProbitLikelihood() { } -SGVector CProbitLikelihood::get_predictive_means( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector ProbitLikelihood::get_predictive_means( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector lp=get_log_zeroth_moments(mu, s2, lab); Map eigen_lp(lp.vector, lp.vlen); @@ -61,8 +61,8 @@ SGVector CProbitLikelihood::get_predictive_means( return r; } -SGVector CProbitLikelihood::get_predictive_variances( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector ProbitLikelihood::get_predictive_variances( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector lp=get_log_zeroth_moments(mu, s2, lab); Map eigen_lp(lp.vector, lp.vlen); @@ -76,17 +76,17 @@ SGVector CProbitLikelihood::get_predictive_variances( return r; } -SGVector CProbitLikelihood::get_log_probability_f(const CLabels* lab, +SGVector ProbitLikelihood::get_log_probability_f(std::shared_ptr lab, SGVector func) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); Map eigen_f(func.vector, func.vlen); @@ -98,23 +98,23 @@ SGVector CProbitLikelihood::get_log_probability_f(const CLabels* lab, eigen_r=eigen_y.cwiseProduct(eigen_f); for (index_t i=0; i CProbitLikelihood::get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const +SGVector ProbitLikelihood::get_log_probability_derivative_f( + std::shared_ptr lab, SGVector func, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); require(i>=1 && i<=3, "Index for derivative should be 1, 2 or 3"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); Map eigen_f(func.vector, func.vlen); @@ -127,18 +127,18 @@ SGVector CProbitLikelihood::get_log_probability_derivative_f( for (index_t j=0; j CProbitLikelihood::get_log_probability_derivative_f( return r; } -SGVector CProbitLikelihood::get_log_zeroth_moments( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector ProbitLikelihood::get_log_zeroth_moments( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector y; @@ -180,9 +180,8 @@ SGVector CProbitLikelihood::get_log_zeroth_moments( "variances ({}) and number of labels ({}) should be the same", mu.vlen, s2.vlen, lab->get_num_labels()); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); - - y=((CBinaryLabels*)lab)->get_labels(); + "Labels must be type of BinaryLabels"); + y=lab->as()->get_labels(); } else { @@ -205,13 +204,13 @@ SGVector CProbitLikelihood::get_log_zeroth_moments( eigen_r=eigen_mu.array()*eigen_y.array()/((1.0+eigen_s2.array()).sqrt()); for (index_t i=0; i mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t ProbitLikelihood::get_first_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -221,18 +220,18 @@ float64_t CProbitLikelihood::get_first_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); float64_t z = y[i] * mu[i] / std::sqrt(1.0 + s2[i]); // compute ncdf=normal_cdf(z) - float64_t ncdf=CStatistics::normal_cdf(z); + float64_t ncdf=Statistics::normal_cdf(z); // compute npdf=normal_pdf(z)=(1/sqrt(2*pi))*exp(-z.^2/2) float64_t npdf = - (1.0 / std::sqrt(2.0 * CMath::PI)) * std::exp(-0.5 * CMath::sq(z)); + (1.0 / std::sqrt(2.0 * Math::PI)) * std::exp(-0.5 * Math::sq(z)); // compute the 1st moment: E[x] = mu + (y*s2*N(z))/(Phi(z)*sqrt(1+s2)) float64_t Ex = @@ -241,8 +240,8 @@ float64_t CProbitLikelihood::get_first_moment(SGVector mu, return Ex; } -float64_t CProbitLikelihood::get_second_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t ProbitLikelihood::get_second_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -252,25 +251,25 @@ float64_t CProbitLikelihood::get_second_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_BINARY, - "Labels must be type of CBinaryLabels"); + "Labels must be type of BinaryLabels"); - SGVector y=((CBinaryLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); float64_t z = y[i] * mu[i] / std::sqrt(1.0 + s2[i]); // compute ncdf=normal_cdf(z) - float64_t ncdf=CStatistics::normal_cdf(z); + float64_t ncdf=Statistics::normal_cdf(z); // compute npdf=normal_pdf(z)=(1/sqrt(2*pi))*exp(-z.^2/2) float64_t npdf = - (1.0 / std::sqrt(2.0 * CMath::PI)) * std::exp(-0.5 * CMath::sq(z)); + (1.0 / std::sqrt(2.0 * Math::PI)) * std::exp(-0.5 * Math::sq(z)); SGVector r(y.vlen); Map eigen_r(r.vector, r.vlen); // compute the 2nd moment: // Var[x] = s2 - (s2^2*N(z))/((1+s2)*Phi(z))*(z+N(z)/Phi(z)) - float64_t Var=s2[i]-(CMath::sq(s2[i])/(1.0+s2[i]))*(npdf/ncdf)*(z+(npdf/ncdf)); + float64_t Var=s2[i]-(Math::sq(s2[i])/(1.0+s2[i]))*(npdf/ncdf)*(z+(npdf/ncdf)); return Var; } diff --git a/src/shogun/machine/gp/ProbitLikelihood.h b/src/shogun/machine/gp/ProbitLikelihood.h index d4d5f92fd16..e73765346b0 100644 --- a/src/shogun/machine/gp/ProbitLikelihood.h +++ b/src/shogun/machine/gp/ProbitLikelihood.h @@ -48,13 +48,13 @@ namespace shogun * where \f$\Phi: \mathbb{R} \mapsto [0, 1]\f$ is the cumulative distribution function (CDF) of * the normal distribution \f$\mathcal{N}(0, 1)\f$. */ -class CProbitLikelihood : public CLikelihoodModel +class ProbitLikelihood : public LikelihoodModel { public: /** default constructor */ - CProbitLikelihood(); + ProbitLikelihood(); - virtual ~CProbitLikelihood(); + virtual ~ProbitLikelihood(); /** returns the name of the likelihood model * @@ -77,7 +77,7 @@ class CProbitLikelihood : public CLikelihoodModel * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$. * @@ -94,7 +94,7 @@ class CProbitLikelihood : public CLikelihoodModel * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** get model type * @@ -113,7 +113,7 @@ class CProbitLikelihood : public CLikelihoodModel * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const; /** get derivative of log likelihood \f$log(P(y|f))\f$ with respect to @@ -127,7 +127,7 @@ class CProbitLikelihood : public CLikelihoodModel * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const; + std::shared_ptr lab, SGVector func, index_t i) const; /** returns the zeroth moment of a given (unnormalized) probability * distribution: @@ -146,7 +146,7 @@ class CProbitLikelihood : public CLikelihoodModel * @return log zeroth moments \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -163,7 +163,7 @@ class CProbitLikelihood : public CLikelihoodModel * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** returns the second moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -180,7 +180,7 @@ class CProbitLikelihood : public CLikelihoodModel * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** return whether logit likelihood function supports binary classification * diff --git a/src/shogun/machine/gp/ProbitVGLikelihood.cpp b/src/shogun/machine/gp/ProbitVGLikelihood.cpp index 2f1d68ac9e9..822c039bbbe 100644 --- a/src/shogun/machine/gp/ProbitVGLikelihood.cpp +++ b/src/shogun/machine/gp/ProbitVGLikelihood.cpp @@ -46,22 +46,22 @@ using namespace Eigen; namespace shogun { -CProbitVGLikelihood::CProbitVGLikelihood() - : CNumericalVGLikelihood() +ProbitVGLikelihood::ProbitVGLikelihood() + : NumericalVGLikelihood() { init(); } -CProbitVGLikelihood::~CProbitVGLikelihood() +ProbitVGLikelihood::~ProbitVGLikelihood() { } -void CProbitVGLikelihood::init_likelihood() +void ProbitVGLikelihood::init_likelihood() { - set_likelihood(new CProbitLikelihood()); + set_likelihood(std::make_shared()); } -void CProbitVGLikelihood::init() +void ProbitVGLikelihood::init() { init_likelihood(); } diff --git a/src/shogun/machine/gp/ProbitVGLikelihood.h b/src/shogun/machine/gp/ProbitVGLikelihood.h index a6f6636453a..66684934919 100644 --- a/src/shogun/machine/gp/ProbitVGLikelihood.h +++ b/src/shogun/machine/gp/ProbitVGLikelihood.h @@ -54,12 +54,12 @@ template class SGMatrix; * \f] * */ -class CProbitVGLikelihood : public CNumericalVGLikelihood +class ProbitVGLikelihood : public NumericalVGLikelihood { public: - CProbitVGLikelihood(); + ProbitVGLikelihood(); - virtual ~CProbitVGLikelihood(); + virtual ~ProbitVGLikelihood(); /** returns the name of the likelihood model * diff --git a/src/shogun/machine/gp/SingleFITCInference.cpp b/src/shogun/machine/gp/SingleFITCInference.cpp index 10a015bc1ca..607a2eed063 100644 --- a/src/shogun/machine/gp/SingleFITCInference.cpp +++ b/src/shogun/machine/gp/SingleFITCInference.cpp @@ -39,19 +39,19 @@ using namespace shogun; using namespace Eigen; -CSingleFITCInference::CSingleFITCInference() : CSingleSparseInference() +SingleFITCInference::SingleFITCInference() : SingleSparseInference() { init(); } -CSingleFITCInference::CSingleFITCInference(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat) - : CSingleSparseInference(kern, feat, m, lab, mod, lat) +SingleFITCInference::SingleFITCInference(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod, std::shared_ptr lat) + : SingleSparseInference(kern, feat, m, lab, mod, lat) { init(); } -void CSingleFITCInference::init() +void SingleFITCInference::init() { SG_ADD(&m_al, "al", "alpha"); SG_ADD(&m_t, "t", "noise"); @@ -61,11 +61,11 @@ void CSingleFITCInference::init() SG_ADD(&m_V, "V", "V"); } -CSingleFITCInference::~CSingleFITCInference() +SingleFITCInference::~SingleFITCInference() { } -SGVector CSingleFITCInference::get_derivative_related_cov_diagonal() +SGVector SingleFITCInference::get_derivative_related_cov_diagonal() { //time complexity O(m*n) Map eigen_W(m_Rvdd.matrix, m_Rvdd.num_rows, m_Rvdd.num_cols); @@ -78,7 +78,7 @@ SGVector CSingleFITCInference::get_derivative_related_cov_diagonal() return res; } -float64_t CSingleFITCInference::get_derivative_related_cov_helper( +float64_t SingleFITCInference::get_derivative_related_cov_helper( SGMatrix dKuui, SGVector v, SGMatrix R) { //time complexity O(m^2*n) @@ -101,7 +101,7 @@ float64_t CSingleFITCInference::get_derivative_related_cov_helper( return result; } -float64_t CSingleFITCInference::get_derivative_related_cov(SGVector ddiagKi, +float64_t SingleFITCInference::get_derivative_related_cov(SGVector ddiagKi, SGMatrix dKuui, SGMatrix dKui) { //time complexity O(m^2*n) @@ -123,7 +123,7 @@ float64_t CSingleFITCInference::get_derivative_related_cov(SGVector d return get_derivative_related_cov(ddiagKi, dKuui, dKui, v, R); } -float64_t CSingleFITCInference::get_derivative_related_cov(SGVector ddiagKi, +float64_t SingleFITCInference::get_derivative_related_cov(SGVector ddiagKi, SGMatrix dKuui, SGMatrix dKui, SGVector v, SGMatrix R) { @@ -146,7 +146,7 @@ float64_t CSingleFITCInference::get_derivative_related_cov(SGVector d return result; } -float64_t CSingleFITCInference::get_derivative_related_mean(SGVector dmu) +float64_t SingleFITCInference::get_derivative_related_mean(SGVector dmu) { //time complexity O(n) Map eigen_al(m_al.vector, m_al.vlen); @@ -154,7 +154,7 @@ float64_t CSingleFITCInference::get_derivative_related_mean(SGVector return -eigen_dmu.dot(eigen_al); } -SGVector CSingleFITCInference::get_derivative_wrt_mean( +SGVector SingleFITCInference::get_derivative_wrt_mean( const TParameter* param) { //time complexity O(n) @@ -176,7 +176,7 @@ SGVector CSingleFITCInference::get_derivative_wrt_mean( return result; } -SGVector CSingleFITCInference::get_derivative_wrt_inducing_noise( +SGVector SingleFITCInference::get_derivative_wrt_inducing_noise( const TParameter* param) { //time complexity O(m^2*n) @@ -207,7 +207,7 @@ SGVector CSingleFITCInference::get_derivative_wrt_inducing_noise( return result; } -SGVector CSingleFITCInference::get_derivative_related_inducing_features( +SGVector SingleFITCInference::get_derivative_related_inducing_features( SGMatrix BdK, const TParameter* param) { //time complexity depends on the implementation of the provided kernel @@ -224,7 +224,7 @@ SGVector CSingleFITCInference::get_derivative_related_inducing_featur deriv_lat.zero(); m_lock->lock(); - CFeatures *inducing_features=get_inducing_features(); + auto inducing_features=get_inducing_features(); //asymtric part (related to xu and x) m_kernel->init(inducing_features, m_features); //A = (Kpu.*BdK)*diag(e); @@ -251,12 +251,12 @@ SGVector CSingleFITCInference::get_derivative_related_inducing_featur Map eigen_deriv_mat(deriv_mat.matrix, deriv_mat.num_rows, deriv_mat.num_cols); deriv_lat_col_vec+=eigen_deriv_mat*(C.row(lat_lidx).transpose()); } - SG_UNREF(inducing_features); + m_lock->unlock(); return deriv_lat; } -SGVector CSingleFITCInference::get_derivative_wrt_inducing_features(const TParameter* param) +SGVector SingleFITCInference::get_derivative_wrt_inducing_features(const TParameter* param) { //time complexity depends on the implementation of the provided kernel //time complexity is at least O(max((p*n*m),(m^2*n))), where p is the dimension (#) of features diff --git a/src/shogun/machine/gp/SingleFITCInference.h b/src/shogun/machine/gp/SingleFITCInference.h index 62ab9d47fea..3fb6b94e99a 100644 --- a/src/shogun/machine/gp/SingleFITCInference.h +++ b/src/shogun/machine/gp/SingleFITCInference.h @@ -59,17 +59,17 @@ namespace shogun * in the GPML toolbox. * * Warning: the time complexity of method, - * CSingleFITCInference::get_derivative_wrt_kernel(const TParameter* param), + * SingleFITCInference::get_derivative_wrt_kernel(const TParameter* param), * depends on the implementation of virtual kernel method, - * CKernel::get_parameter_gradient_diagonal(param, i). + * Kernel::get_parameter_gradient_diagonal(param, i). * The default time complexity of the kernel method can be O(n^2) * */ -class CSingleFITCInference: public CSingleSparseInference +class SingleFITCInference: public SingleSparseInference { public: /** default constructor */ - CSingleFITCInference(); + SingleFITCInference(); /** constructor * @@ -80,11 +80,11 @@ class CSingleFITCInference: public CSingleSparseInference * @param model likelihood model to use * @param inducing_features features to use */ - CSingleFITCInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model, - CFeatures* inducing_features); + SingleFITCInference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model, + std::shared_ptr inducing_features); - virtual ~CSingleFITCInference(); + virtual ~SingleFITCInference(); /** returns the name of the inference method * diff --git a/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.cpp b/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.cpp index d244a7ce780..ca3aa0cbd41 100644 --- a/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.cpp +++ b/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.cpp @@ -60,9 +60,9 @@ class CFITCPsiLine : public func_base SGVector* W; SGVector* f; SGVector* m; - CLikelihoodModel* lik; - CLabels* lab; - CSingleFITCLaplaceInferenceMethod *inf; + std::shared_ptr lik; + std::shared_ptr lab; + std::shared_ptrinf; virtual double operator() (double x) { @@ -98,20 +98,20 @@ class SingleFITCLaplaceInferenceMethodCostFunction: public FirstOrderCostFunctio public: SingleFITCLaplaceInferenceMethodCostFunction():FirstOrderCostFunction() { init(); } virtual ~SingleFITCLaplaceInferenceMethodCostFunction() { clean(); } - void set_target(CSingleFITCLaplaceInferenceMethod *obj) + void set_target(std::shared_ptrobj) { require(obj, "Obj must set"); if(m_obj != obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; } } void clean() { - SG_UNREF(m_obj); + } virtual float64_t get_cost() @@ -123,7 +123,7 @@ class SingleFITCLaplaceInferenceMethodCostFunction: public FirstOrderCostFunctio { if(is_unref) { - SG_UNREF(m_obj); + } m_obj=NULL; } @@ -147,38 +147,38 @@ class SingleFITCLaplaceInferenceMethodCostFunction: public FirstOrderCostFunctio m_derivatives = SGVector(); SG_ADD(&m_derivatives, "SingleFITCLaplaceInferenceMethodCostFunction__m_derivatives", "derivatives in SingleFITCLaplaceInferenceMethodCostFunction"); - SG_ADD((CSGObject **)&m_obj, "SingleFITCLaplaceInferenceMethodCostFunction__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "SingleFITCLaplaceInferenceMethodCostFunction__m_obj", "obj in SingleFITCLaplaceInferenceMethodCostFunction"); } SGVector m_derivatives; - CSingleFITCLaplaceInferenceMethod *m_obj; + std::shared_ptrm_obj; }; #endif /* DOXYGEN_SHOULD_SKIP_THIS */ -void CSingleFITCLaplaceNewtonOptimizer::set_target(CSingleFITCLaplaceInferenceMethod *obj) +void SingleFITCLaplaceNewtonOptimizer::set_target(std::shared_ptrobj) { require(obj, "Obj must set"); if(m_obj != obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; } } -void CSingleFITCLaplaceNewtonOptimizer::unset_target(bool is_unref) +void SingleFITCLaplaceNewtonOptimizer::unset_target(bool is_unref) { if(is_unref) { - SG_UNREF(m_obj); + } m_obj=NULL; } -void CSingleFITCLaplaceNewtonOptimizer::init() +void SingleFITCLaplaceNewtonOptimizer::init() { m_obj=NULL; m_iter=20; @@ -186,7 +186,7 @@ void CSingleFITCLaplaceNewtonOptimizer::init() m_opt_tolerance=1e-6; m_opt_max=10; - SG_ADD((CSGObject **)&m_obj, "CSingleFITCLaplaceNewtonOptimizer__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "CSingleFITCLaplaceNewtonOptimizer__m_obj", "obj in CSingleFITCLaplaceNewtonOptimizer"); SG_ADD(&m_iter, "CSingleFITCLaplaceNewtonOptimizer__m_iter", "iter in CSingleFITCLaplaceNewtonOptimizer"); @@ -198,7 +198,7 @@ void CSingleFITCLaplaceNewtonOptimizer::init() "opt_max in CSingleFITCLaplaceNewtonOptimizer"); } -float64_t CSingleFITCLaplaceNewtonOptimizer::minimize() +float64_t SingleFITCLaplaceNewtonOptimizer::minimize() { require(m_obj,"Object not set"); //time complexity O(m^2*n); @@ -211,7 +211,7 @@ float64_t CSingleFITCLaplaceNewtonOptimizer::minimize() SGVector mean=m_obj->m_mean->get_mean_vector(m_obj->m_features); Map eigen_mean(mean.vector, mean.vlen); - float64_t Psi_Old=CMath::INFTY; + float64_t Psi_Old=Math::INFTY; float64_t Psi_New=m_obj->m_Psi; // compute W = -d2lp @@ -245,7 +245,7 @@ float64_t CSingleFITCLaplaceNewtonOptimizer::minimize() if (m_obj->m_model->get_model_type()==LT_STUDENTST) { - CStudentsTLikelihood* lik = m_obj->m_model->as(); + auto lik = m_obj->m_model->as(); df=lik->get_degrees_freedom(); } else @@ -303,19 +303,19 @@ float64_t CSingleFITCLaplaceNewtonOptimizer::minimize() return Psi_New; } -CSingleFITCLaplaceInferenceMethod::CSingleFITCLaplaceInferenceMethod() : CSingleFITCInference() +SingleFITCLaplaceInferenceMethod::SingleFITCLaplaceInferenceMethod() : SingleFITCInference() { init(); } -CSingleFITCLaplaceInferenceMethod::CSingleFITCLaplaceInferenceMethod(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat) -: CSingleFITCInference(kern, feat, m, lab, mod, lat) +SingleFITCLaplaceInferenceMethod::SingleFITCLaplaceInferenceMethod(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod, std::shared_ptr lat) +: SingleFITCInference(kern, feat, m, lab, mod, lat) { init(); } -void CSingleFITCLaplaceInferenceMethod::init() +void SingleFITCLaplaceInferenceMethod::init() { m_Psi=0; m_Wneg=false; @@ -333,12 +333,12 @@ void CSingleFITCLaplaceInferenceMethod::init() SG_ADD(&m_Psi, "Psi", "the negative log likelihood without constant terms used in Newton's method"); SG_ADD(&m_Wneg, "Wneg", "whether W contains negative elements"); - register_minimizer(new CSingleFITCLaplaceNewtonOptimizer()); + register_minimizer(std::make_shared()); } -void CSingleFITCLaplaceInferenceMethod::compute_gradient() +void SingleFITCLaplaceInferenceMethod::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -349,11 +349,11 @@ void CSingleFITCLaplaceInferenceMethod::compute_gradient() } } -void CSingleFITCLaplaceInferenceMethod::update() +void SingleFITCLaplaceInferenceMethod::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_init(); update_alpha(); update_chol(); @@ -363,7 +363,7 @@ void CSingleFITCLaplaceInferenceMethod::update() SG_TRACE("leaving"); } -SGVector CSingleFITCLaplaceInferenceMethod::get_diagonal_vector() +SGVector SingleFITCLaplaceInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); @@ -371,11 +371,11 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_diagonal_vector() return SGVector(m_sW); } -CSingleFITCLaplaceInferenceMethod::~CSingleFITCLaplaceInferenceMethod() +SingleFITCLaplaceInferenceMethod::~SingleFITCLaplaceInferenceMethod() { } -SGVector CSingleFITCLaplaceInferenceMethod::compute_mvmZ(SGVector x) +SGVector SingleFITCLaplaceInferenceMethod::compute_mvmZ(SGVector x) { //time complexity O(m*n) Map eigen_Rvdd(m_Rvdd.matrix, m_Rvdd.num_rows, m_Rvdd.num_cols); @@ -390,7 +390,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::compute_mvmZ(SGVector CSingleFITCLaplaceInferenceMethod::compute_mvmK(SGVector al) +SGVector SingleFITCLaplaceInferenceMethod::compute_mvmK(SGVector al) { //time complexity O(m*n) Map eigen_V(m_V.matrix, m_V.num_rows, m_V.num_cols); @@ -405,19 +405,17 @@ SGVector CSingleFITCLaplaceInferenceMethod::compute_mvmK(SGVector SingleFITCLaplaceInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { require(inference!=NULL, "Inference should be not NULL"); if (inference->get_inference_type()!=INF_FITC_LAPLACE_SINGLE) - error("Provided inference is not of type CSingleFITCLaplaceInferenceMethod!"); - - SG_REF(inference); - return (CSingleFITCLaplaceInferenceMethod*)inference; + error("Provided inference is not of type SingleFITCLaplaceInferenceMethod!"); + return inference->as(); } -SGMatrix CSingleFITCLaplaceInferenceMethod::get_chol_inv(SGMatrix mtx) +SGMatrix SingleFITCLaplaceInferenceMethod::get_chol_inv(SGMatrix mtx) { //time complexity O(m^3), where mtx is a m-by-m matrix require(mtx.num_rows==mtx.num_cols, "Matrix must be square"); @@ -434,7 +432,7 @@ SGMatrix CSingleFITCLaplaceInferenceMethod::get_chol_inv(SGMatrix eigen_alpha(m_al.vector, m_al.vlen); @@ -470,11 +468,11 @@ float64_t CSingleFITCLaplaceInferenceMethod::get_negative_log_marginal_likelihoo return result; } -void CSingleFITCLaplaceInferenceMethod::update_approx_cov() +void SingleFITCLaplaceInferenceMethod::update_approx_cov() { } -void CSingleFITCLaplaceInferenceMethod::update_init() +void SingleFITCLaplaceInferenceMethod::update_init() { //time complexity O(m^2*n) //m-by-m matrix @@ -551,44 +549,38 @@ void CSingleFITCLaplaceInferenceMethod::update_init() m_Psi=Psi_New; } -void CSingleFITCLaplaceInferenceMethod::register_minimizer(Minimizer* minimizer) +void SingleFITCLaplaceInferenceMethod::register_minimizer(std::shared_ptr minimizer) { require(minimizer, "Minimizer must set"); - if (!dynamic_cast(minimizer)) + if (!std::dynamic_pointer_cast(minimizer)) { - FirstOrderMinimizer* opt= dynamic_cast(minimizer); + auto opt= std::dynamic_pointer_cast(minimizer); require(opt, "The provided minimizer is not supported"); } - CInference::register_minimizer(minimizer); + Inference::register_minimizer(minimizer); } -void CSingleFITCLaplaceInferenceMethod::update_alpha() +void SingleFITCLaplaceInferenceMethod::update_alpha() { - CSingleFITCLaplaceNewtonOptimizer *opt=dynamic_cast(m_minimizer); + auto opt=std::dynamic_pointer_cast(m_minimizer); bool cleanup=false; if (opt) { - opt->set_target(this); - if(this->ref_count()>1) - cleanup=true; + opt->set_target(shared_from_this()->as()); opt->minimize(); - opt->unset_target(cleanup); } else { - FirstOrderMinimizer* minimizer= dynamic_cast(m_minimizer); + auto minimizer= std::dynamic_pointer_cast(m_minimizer); require(minimizer, "The provided minimizer is not supported"); - SingleFITCLaplaceInferenceMethodCostFunction *cost_fun=new SingleFITCLaplaceInferenceMethodCostFunction(); - cost_fun->set_target(this); - if(this->ref_count()>1) - cleanup=true; + auto cost_fun=std::make_shared(); + cost_fun->set_target(shared_from_this()->as()); minimizer->set_cost_function(cost_fun); minimizer->minimize(); minimizer->unset_cost_function(false); - cost_fun->unset_target(cleanup); - SG_UNREF(cost_fun); + } Map eigen_mean(m_mean_f.vector, m_mean_f.vlen); @@ -610,7 +602,7 @@ void CSingleFITCLaplaceInferenceMethod::update_alpha() eigen_post_alpha=eigen_R0.transpose()*(eigen_V*eigen_al); } -void CSingleFITCLaplaceInferenceMethod::update_chol() +void SingleFITCLaplaceInferenceMethod::update_chol() { //time complexity O(m^2*n) Map eigen_dg(m_dg.vector, m_dg.vlen); @@ -713,7 +705,7 @@ void CSingleFITCLaplaceInferenceMethod::update_chol() 2; } -void CSingleFITCLaplaceInferenceMethod::update_deriv() +void SingleFITCLaplaceInferenceMethod::update_deriv() { //time complexity O(m^2*n) Map eigen_ktru(m_ktru.matrix, m_ktru.num_rows, m_ktru.num_cols); @@ -745,7 +737,7 @@ void CSingleFITCLaplaceInferenceMethod::update_deriv() eigen_dfhat=eigen_g.cwiseProduct(eigen_d3lp); } -float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_related_cov(SGVector ddiagKi, +float64_t SingleFITCLaplaceInferenceMethod::get_derivative_related_cov(SGVector ddiagKi, SGMatrix dKuui, SGMatrix dKui) { //time complexity O(m^2*n) @@ -771,7 +763,7 @@ float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_related_cov(SGVector eigen_v=eigen_ddiagKi-eigen_dA.cwiseProduct(eigen_R0tV).colwise().sum().transpose(); //explicit term - float64_t result=CSingleFITCInference::get_derivative_related_cov(ddiagKi, dKuui, dKui, v, dA); + float64_t result=SingleFITCInference::get_derivative_related_cov(ddiagKi, dKuui, dKui, v, dA); //implicit term Map eigen_dlp(m_dlp.vector, m_dlp.vlen); @@ -789,7 +781,7 @@ float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_related_cov(SGVector return result; } -SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_inference_method( +SGVector SingleFITCLaplaceInferenceMethod::get_derivative_wrt_inference_method( const TParameter* param) { require(param, "Param not set"); @@ -812,7 +804,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_infere len=dim*num_samples; } else if (!m_fully_sparse) - return CSingleFITCInference::get_derivative_wrt_inference_method(param); + return SingleFITCInference::get_derivative_wrt_inference_method(param); else return get_derivative_wrt_inducing_features(param); } @@ -848,7 +840,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_infere return result; } -SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector SingleFITCLaplaceInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { SGVector result(1); @@ -883,7 +875,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_likeli return result; } -SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_kernel( +SGVector SingleFITCLaplaceInferenceMethod::get_derivative_wrt_kernel( const TParameter* param) { require(param, "Param not set"); @@ -895,7 +887,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_kernel return derivative_helper_when_Wneg(result, param); m_lock->lock(); - CFeatures *inducing_features=get_inducing_features(); + auto inducing_features=get_inducing_features(); for (index_t i=0; i CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_kernel result[i]=get_derivative_related_cov(deriv_trtr, deriv_uu, deriv_tru); result[i] *= std::exp(m_log_scale * 2.0); } - SG_UNREF(inducing_features); + m_lock->unlock(); return result; } -float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_related_mean(SGVector dmu) +float64_t SingleFITCLaplaceInferenceMethod::get_derivative_related_mean(SGVector dmu) { //time complexity O(m*n) //explicit term - float64_t result=CSingleFITCInference::get_derivative_related_mean(dmu); + float64_t result=SingleFITCInference::get_derivative_related_mean(dmu); //implicit term //Zdm = mvmZ(dm,RVdd,t); @@ -943,7 +935,7 @@ float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_related_mean(SGVecto return result; } -float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_implicit_term_helper(SGVector d) +float64_t SingleFITCLaplaceInferenceMethod::get_derivative_implicit_term_helper(SGVector d) { //time complexity O(m*n) Map eigen_d(d.vector, d.vlen); @@ -953,7 +945,7 @@ float64_t CSingleFITCLaplaceInferenceMethod::get_derivative_implicit_term_helper return eigen_dfhat.dot(eigen_d-eigen_tmp); } -SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_mean( +SGVector SingleFITCLaplaceInferenceMethod::get_derivative_wrt_mean( const TParameter* param) { //time complexity O(m*n) @@ -975,7 +967,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_mean( return result; } -SGVector CSingleFITCLaplaceInferenceMethod::derivative_helper_when_Wneg( +SGVector SingleFITCLaplaceInferenceMethod::derivative_helper_when_Wneg( SGVector res, const TParameter *param) { require(param, "Param not set"); @@ -985,7 +977,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::derivative_helper_when_Wn return res; } -SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_inducing_features( +SGVector SingleFITCLaplaceInferenceMethod::get_derivative_wrt_inducing_features( const TParameter* param) { //time complexity depends on the implementation of the provided kernel @@ -1032,12 +1024,12 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_induci return get_derivative_related_inducing_features(BdK, param); } -SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_inducing_noise( +SGVector SingleFITCLaplaceInferenceMethod::get_derivative_wrt_inducing_noise( const TParameter* param) { //time complexity O(m^2*n) //explicit term - SGVector result=CSingleFITCInference::get_derivative_wrt_inducing_noise(param); + SGVector result=SingleFITCInference::get_derivative_wrt_inducing_noise(param); //implicit term Map eigen_B(m_B.matrix, m_B.num_rows, m_B.num_cols); @@ -1061,7 +1053,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_derivative_wrt_induci return result; } -SGVector CSingleFITCLaplaceInferenceMethod::get_posterior_mean() +SGVector SingleFITCLaplaceInferenceMethod::get_posterior_mean() { compute_gradient(); @@ -1087,7 +1079,7 @@ SGVector CSingleFITCLaplaceInferenceMethod::get_posterior_mean() return res; } -SGMatrix CSingleFITCLaplaceInferenceMethod::get_posterior_covariance() +SGMatrix SingleFITCLaplaceInferenceMethod::get_posterior_covariance() { compute_gradient(); //time complexity of the following operations is O(m*n^2) @@ -1127,7 +1119,7 @@ SGMatrix CSingleFITCLaplaceInferenceMethod::get_posterior_covariance( return SGMatrix(m_Sigma); } -float64_t CSingleFITCLaplaceInferenceMethod::get_psi_wrt_alpha() +float64_t SingleFITCLaplaceInferenceMethod::get_psi_wrt_alpha() { //time complexity O(m*n) Map eigen_alpha(m_al, m_al.vlen); @@ -1146,7 +1138,7 @@ float64_t CSingleFITCLaplaceInferenceMethod::get_psi_wrt_alpha() return psi; } -void CSingleFITCLaplaceInferenceMethod::get_gradient_wrt_alpha(SGVector gradient) +void SingleFITCLaplaceInferenceMethod::get_gradient_wrt_alpha(SGVector gradient) { //time complexity O(m*n) Map eigen_alpha(m_al, m_al.vlen); diff --git a/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.h b/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.h index 6cfe1a4cb15..818ced81035 100644 --- a/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.h +++ b/src/shogun/machine/gp/SingleFITCLaplaceInferenceMethod.h @@ -44,25 +44,25 @@ namespace shogun * (the time complexity is computed based on the assumption m < n) * * Warning: the time complexity of method, - * CSingleFITCInference::get_derivative_wrt_kernel(const TParameter* param), + * SingleFITCInference::get_derivative_wrt_kernel(const TParameter* param), * depends on the implementation of virtual kernel method, - * CKernel::get_parameter_gradient_diagonal(param, i). + * Kernel::get_parameter_gradient_diagonal(param, i). * The default time complexity of the kernel method can be O(n^2) * * Warning: the the time complexity increases from O(m^2*n) to O(n^2*m) if method - * CSingleFITCLaplaceInferenceMethod::get_posterior_covariance() is called + * SingleFITCLaplaceInferenceMethod::get_posterior_covariance() is called * * This specific implementation was adapted from the infFITC_Laplace.m file in the * GPML toolbox. */ -class CSingleFITCLaplaceInferenceMethod: public CSingleFITCInference +class SingleFITCLaplaceInferenceMethod: public SingleFITCInference { friend class CFITCPsiLine; -friend class CSingleFITCLaplaceNewtonOptimizer; +friend class SingleFITCLaplaceNewtonOptimizer; friend class SingleFITCLaplaceInferenceMethodCostFunction; public: /** default constructor */ - CSingleFITCLaplaceInferenceMethod(); + SingleFITCLaplaceInferenceMethod(); /** constructor * @@ -73,11 +73,11 @@ friend class SingleFITCLaplaceInferenceMethodCostFunction; * @param model Likelihood model to use * @param inducing_features features to use */ - CSingleFITCLaplaceInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model, - CFeatures* inducing_features); + SingleFITCLaplaceInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model, + std::shared_ptr inducing_features); - virtual ~CSingleFITCLaplaceInferenceMethod(); + virtual ~SingleFITCLaplaceInferenceMethod(); /** returns the name of the inference method * @@ -95,9 +95,9 @@ friend class SingleFITCLaplaceInferenceMethodCostFunction; /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CSingleFITCLaplaceInferenceMethod object + * @return casted SingleFITCLaplaceInferenceMethod object */ - static CSingleFITCLaplaceInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** * @return whether combination of Laplace approximation inference method and @@ -175,7 +175,7 @@ friend class SingleFITCLaplaceInferenceMethodCostFunction; * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** update gradients */ @@ -389,19 +389,19 @@ friend class SingleFITCLaplaceInferenceMethodCostFunction; }; /** @brief The build-in minimizer for SingleFITCLaplaceInference */ -class CSingleFITCLaplaceNewtonOptimizer: public Minimizer +class SingleFITCLaplaceNewtonOptimizer: public Minimizer { public: - CSingleFITCLaplaceNewtonOptimizer() :Minimizer() { init(); } + SingleFITCLaplaceNewtonOptimizer() :Minimizer() { init(); } virtual const char* get_name() const { return "SingleFITCLaplaceNewtonOptimizer"; } - virtual ~CSingleFITCLaplaceNewtonOptimizer() { SG_UNREF(m_obj); } + virtual ~SingleFITCLaplaceNewtonOptimizer() { } /** Set the inference method * @param obj the inference method */ - void set_target(CSingleFITCLaplaceInferenceMethod *obj); + void set_target(std::shared_ptrobj); /** Unset the inference method * @param is_unref do we SG_UNREF the method @@ -441,7 +441,7 @@ class CSingleFITCLaplaceNewtonOptimizer: public Minimizer void init(); /** the inference method */ - CSingleFITCLaplaceInferenceMethod *m_obj; + std::shared_ptrm_obj; /** amount of tolerance for Newton's iterations */ float64_t m_tolerance; diff --git a/src/shogun/machine/gp/SingleLaplaceInferenceMethod.cpp b/src/shogun/machine/gp/SingleLaplaceInferenceMethod.cpp index 928b2e0a6d0..6677b00a97a 100644 --- a/src/shogun/machine/gp/SingleLaplaceInferenceMethod.cpp +++ b/src/shogun/machine/gp/SingleLaplaceInferenceMethod.cpp @@ -36,8 +36,8 @@ class PsiLine : public func_base SGVector* W; SGVector* f; SGVector* m; - CLikelihoodModel* lik; - CLabels* lab; + std::shared_ptr lik; + std::shared_ptr lab; virtual double operator() (double x) { @@ -67,14 +67,14 @@ class SingleLaplaceInferenceMethodCostFunction: public FirstOrderCostFunction { public: SingleLaplaceInferenceMethodCostFunction():FirstOrderCostFunction() { init(); } - virtual ~SingleLaplaceInferenceMethodCostFunction() { SG_UNREF(m_obj); } - void set_target(CSingleLaplaceInferenceMethod *obj) + virtual ~SingleLaplaceInferenceMethodCostFunction() { } + void set_target(std::shared_ptrobj) { require(obj, "Obj must set"); if(m_obj != obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; } } @@ -83,14 +83,6 @@ class SingleLaplaceInferenceMethodCostFunction: public FirstOrderCostFunction require(m_obj,"Object not set"); return m_obj->get_psi_wrt_alpha(); } - void unset_target(bool is_unref) - { - if(is_unref) - { - SG_UNREF(m_obj); - } - m_obj=NULL; - } virtual SGVector obtain_variable_reference() { require(m_obj,"Object not set"); @@ -111,39 +103,39 @@ class SingleLaplaceInferenceMethodCostFunction: public FirstOrderCostFunction m_derivatives = SGVector(); SG_ADD(&m_derivatives, "SingleLaplaceInferenceMethodCostFunction__m_derivatives", "derivatives in SingleLaplaceInferenceMethodCostFunction"); - SG_ADD((CSGObject **)&m_obj, "SingleLaplaceInferenceMethodCostFunction__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "SingleLaplaceInferenceMethodCostFunction__m_obj", "obj in SingleLaplaceInferenceMethodCostFunction"); } SGVector m_derivatives; - CSingleLaplaceInferenceMethod *m_obj; + std::shared_ptrm_obj; }; #endif /* DOXYGEN_SHOULD_SKIP_THIS */ -void CSingleLaplaceNewtonOptimizer::set_target(CSingleLaplaceInferenceMethod *obj) +void SingleLaplaceNewtonOptimizer::set_target(std::shared_ptrobj) { require(obj, "Obj must set"); if(m_obj != obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; } } -void CSingleLaplaceNewtonOptimizer::unset_target(bool is_unref) +void SingleLaplaceNewtonOptimizer::unset_target(bool is_unref) { if(is_unref) { - SG_UNREF(m_obj); + } m_obj=NULL; } -void CSingleLaplaceNewtonOptimizer::init() +void SingleLaplaceNewtonOptimizer::init() { m_obj=NULL; m_iter=20; @@ -151,7 +143,7 @@ void CSingleLaplaceNewtonOptimizer::init() m_opt_tolerance=1e-6; m_opt_max=10; - SG_ADD((CSGObject **)&m_obj, "CSingleLaplaceNewtonOptimizer__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "CSingleLaplaceNewtonOptimizer__m_obj", "obj in CSingleLaplaceNewtonOptimizer"); SG_ADD(&m_iter, "CSingleLaplaceNewtonOptimizer__m_iter", "iter in CSingleLaplaceNewtonOptimizer"); @@ -163,10 +155,10 @@ void CSingleLaplaceNewtonOptimizer::init() "opt_max in CSingleLaplaceNewtonOptimizer"); } -float64_t CSingleLaplaceNewtonOptimizer::minimize() +float64_t SingleLaplaceNewtonOptimizer::minimize() { require(m_obj,"Object not set"); - float64_t Psi_Old=CMath::INFTY; + float64_t Psi_Old=Math::INFTY; float64_t Psi_New=m_obj->m_Psi; // get mean vector and create eigen representation of it @@ -209,7 +201,7 @@ float64_t CSingleLaplaceNewtonOptimizer::minimize() if (m_obj->m_model->get_model_type()==LT_STUDENTST) { - CStudentsTLikelihood* lik = m_obj->m_model->as(); + auto lik = m_obj->m_model->as(); df=lik->get_degrees_freedom(); } else @@ -268,29 +260,29 @@ float64_t CSingleLaplaceNewtonOptimizer::minimize() return Psi_New; } -CSingleLaplaceInferenceMethod::CSingleLaplaceInferenceMethod() : CLaplaceInference() +SingleLaplaceInferenceMethod::SingleLaplaceInferenceMethod() : LaplaceInference() { init(); } -CSingleLaplaceInferenceMethod::CSingleLaplaceInferenceMethod(CKernel* kern, - CFeatures* feat, CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod) - : CLaplaceInference(kern, feat, m, lab, mod) +SingleLaplaceInferenceMethod::SingleLaplaceInferenceMethod(std::shared_ptr kern, + std::shared_ptr feat, std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod) + : LaplaceInference(kern, feat, m, lab, mod) { init(); } -void CSingleLaplaceInferenceMethod::init() +void SingleLaplaceInferenceMethod::init() { m_Psi=0; SG_ADD(&m_Psi, "Psi", "posterior log likelihood without constant terms"); SG_ADD(&m_sW, "sW", "square root of W"); SG_ADD(&m_d2lp, "d2lp", "second derivative of log likelihood with respect to function location"); SG_ADD(&m_d3lp, "d3lp", "third derivative of log likelihood with respect to function location"); - register_minimizer(new CSingleLaplaceNewtonOptimizer()); + register_minimizer(std::make_shared()); } -SGVector CSingleLaplaceInferenceMethod::get_diagonal_vector() +SGVector SingleLaplaceInferenceMethod::get_diagonal_vector() { if (parameter_hash_changed()) update(); @@ -298,24 +290,23 @@ SGVector CSingleLaplaceInferenceMethod::get_diagonal_vector() return SGVector(m_sW); } -CSingleLaplaceInferenceMethod* CSingleLaplaceInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr SingleLaplaceInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; if (inference->get_inference_type()!=INF_LAPLACE_SINGLE) - error("Provided inference is not of type CSingleLaplaceInferenceMethod"); + error("Provided inference is not of type SingleLaplaceInferenceMethod"); - SG_REF(inference); - return (CSingleLaplaceInferenceMethod*)inference; + return inference->as(); } -CSingleLaplaceInferenceMethod::~CSingleLaplaceInferenceMethod() +SingleLaplaceInferenceMethod::~SingleLaplaceInferenceMethod() { } -float64_t CSingleLaplaceInferenceMethod::get_negative_log_marginal_likelihood() +float64_t SingleLaplaceInferenceMethod::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); @@ -357,7 +348,7 @@ float64_t CSingleLaplaceInferenceMethod::get_negative_log_marginal_likelihood() return result; } -void CSingleLaplaceInferenceMethod::update_approx_cov() +void SingleLaplaceInferenceMethod::update_approx_cov() { Map eigen_L(m_L.matrix, m_L.num_rows, m_L.num_cols); Map eigen_K(m_ktrtr.matrix, m_ktrtr.num_rows, m_ktrtr.num_cols); @@ -378,7 +369,7 @@ void CSingleLaplaceInferenceMethod::update_approx_cov() eigen_K * std::exp(m_log_scale * 2.0) - eigen_V.adjoint() * eigen_V; } -void CSingleLaplaceInferenceMethod::update_chol() +void SingleLaplaceInferenceMethod::update_chol() { // get log probability derivatives m_dlp=m_model->get_log_probability_derivative_f(m_labels, m_mu, 1); @@ -431,11 +422,11 @@ void CSingleLaplaceInferenceMethod::update_chol() } } -void CSingleLaplaceInferenceMethod::update() +void SingleLaplaceInferenceMethod::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_init(); update_alpha(); update_chol(); @@ -446,7 +437,7 @@ void CSingleLaplaceInferenceMethod::update() } -void CSingleLaplaceInferenceMethod::update_init() +void SingleLaplaceInferenceMethod::update_init() { float64_t Psi_New; float64_t Psi_Def; @@ -498,43 +489,37 @@ void CSingleLaplaceInferenceMethod::update_init() } -void CSingleLaplaceInferenceMethod::register_minimizer(Minimizer* minimizer) +void SingleLaplaceInferenceMethod::register_minimizer(std::shared_ptr minimizer) { require(minimizer, "Minimizer must set"); - if (!dynamic_cast(minimizer)) + if (!std::dynamic_pointer_cast(minimizer)) { - FirstOrderMinimizer* opt= dynamic_cast(minimizer); + auto opt= std::dynamic_pointer_cast(minimizer); require(opt, "The provided minimizer is not supported"); } - CInference::register_minimizer(minimizer); + Inference::register_minimizer(minimizer); } -void CSingleLaplaceInferenceMethod::update_alpha() +void SingleLaplaceInferenceMethod::update_alpha() { - CSingleLaplaceNewtonOptimizer *opt=dynamic_cast(m_minimizer); + auto opt=std::dynamic_pointer_cast(m_minimizer); bool cleanup=false; if (opt) { - opt->set_target(this); - if(this->ref_count()>1) - cleanup=true; + opt->set_target(shared_from_this()->as()); opt->minimize(); - opt->unset_target(cleanup); } else { - FirstOrderMinimizer* minimizer= dynamic_cast(m_minimizer); + auto minimizer= std::dynamic_pointer_cast(m_minimizer); require(minimizer, "The provided minimizer is not supported"); #ifdef USE_GPL_SHOGUN - SingleLaplaceInferenceMethodCostFunction *cost_fun=new SingleLaplaceInferenceMethodCostFunction(); - cost_fun->set_target(this); - if(this->ref_count()>1) - cleanup=true; + auto cost_fun=std::make_shared(); + cost_fun->set_target(shared_from_this()->as()); minimizer->set_cost_function(cost_fun); minimizer->minimize(); minimizer->unset_cost_function(false); - cost_fun->unset_target(cleanup); - SG_UNREF(cost_fun); + #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN @@ -554,7 +539,7 @@ void CSingleLaplaceInferenceMethod::update_alpha() eigen_ktrtr * std::exp(m_log_scale * 2.0) * eigen_alpha + eigen_mean; } -void CSingleLaplaceInferenceMethod::update_deriv() +void SingleLaplaceInferenceMethod::update_deriv() { // create eigen representation of W, sW, dlp, d3lp, K, alpha and L Map eigen_W(m_W.vector, m_W.vlen); @@ -615,7 +600,7 @@ void CSingleLaplaceInferenceMethod::update_deriv() eigen_dfhat=eigen_g.cwiseProduct(eigen_d3lp); } -SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_inference_method( +SGVector SingleLaplaceInferenceMethod::get_derivative_wrt_inference_method( const TParameter* param) { require(!strcmp(param->m_name, "log_scale"), "Can't compute derivative of " @@ -648,7 +633,7 @@ SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_inference_ return result; } -SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector SingleLaplaceInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { // create eigen representation of K, Z, g and dfhat @@ -683,7 +668,7 @@ SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_likelihood return result; } -SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_kernel( +SGVector SingleLaplaceInferenceMethod::get_derivative_wrt_kernel( const TParameter* param) { // create eigen representation of K, Z, dfhat, dlp and alpha @@ -727,7 +712,7 @@ SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_kernel( return result; } -SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_mean( +SGVector SingleLaplaceInferenceMethod::get_derivative_wrt_mean( const TParameter* param) { // create eigen representation of K, Z, dfhat and alpha @@ -763,7 +748,7 @@ SGVector CSingleLaplaceInferenceMethod::get_derivative_wrt_mean( return result; } -SGVector CSingleLaplaceInferenceMethod::get_posterior_mean() +SGVector SingleLaplaceInferenceMethod::get_posterior_mean() { compute_gradient(); @@ -779,7 +764,7 @@ SGVector CSingleLaplaceInferenceMethod::get_posterior_mean() } -float64_t CSingleLaplaceInferenceMethod::get_psi_wrt_alpha() +float64_t SingleLaplaceInferenceMethod::get_psi_wrt_alpha() { Eigen::Map eigen_alpha(m_alpha.vector, m_alpha.vlen); SGVector f(m_alpha.vlen); @@ -799,7 +784,7 @@ float64_t CSingleLaplaceInferenceMethod::get_psi_wrt_alpha() return psi; } -void CSingleLaplaceInferenceMethod::get_gradient_wrt_alpha(SGVector gradient) +void SingleLaplaceInferenceMethod::get_gradient_wrt_alpha(SGVector gradient) { require(gradient.vlen==m_alpha.vlen, "The length of gradients ({}) should the same as the length of parameters ({})", diff --git a/src/shogun/machine/gp/SingleLaplaceInferenceMethod.h b/src/shogun/machine/gp/SingleLaplaceInferenceMethod.h index 926a7e040dc..944032e0e0d 100644 --- a/src/shogun/machine/gp/SingleLaplaceInferenceMethod.h +++ b/src/shogun/machine/gp/SingleLaplaceInferenceMethod.h @@ -29,13 +29,13 @@ namespace shogun * This specific implementation was adapted from the infLaplace.m file in the * GPML toolbox. */ -class CSingleLaplaceInferenceMethod: public CLaplaceInference +class SingleLaplaceInferenceMethod: public LaplaceInference { -friend class CSingleLaplaceNewtonOptimizer; +friend class SingleLaplaceNewtonOptimizer; friend class SingleLaplaceInferenceMethodCostFunction; public: /** default constructor */ - CSingleLaplaceInferenceMethod(); + SingleLaplaceInferenceMethod(); /** constructor * @@ -45,10 +45,10 @@ friend class SingleLaplaceInferenceMethodCostFunction; * @param labels labels of the features * @param model Likelihood model to use */ - CSingleLaplaceInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model); + SingleLaplaceInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model); - virtual ~CSingleLaplaceInferenceMethod(); + virtual ~SingleLaplaceInferenceMethod(); /** returns the name of the inference method * @@ -65,9 +65,9 @@ friend class SingleLaplaceInferenceMethodCostFunction; /** helper method used to specialize a base class instance * * @param inference inference method - * @return casted CSingleLaplaceInferenceMethod object + * @return casted SingleLaplaceInferenceMethod object */ - static CSingleLaplaceInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get negative log marginal likelihood * @@ -137,7 +137,7 @@ friend class SingleLaplaceInferenceMethodCostFunction; * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** initialize the update */ @@ -240,19 +240,19 @@ friend class SingleLaplaceInferenceMethodCostFunction; /** @brief The build-in minimizer for SingleLaplaceInference */ -class CSingleLaplaceNewtonOptimizer: public Minimizer +class SingleLaplaceNewtonOptimizer: public Minimizer { public: - CSingleLaplaceNewtonOptimizer() :Minimizer() { init(); } + SingleLaplaceNewtonOptimizer() :Minimizer() { init(); } virtual const char* get_name() const { return "SingleLaplaceNewtonOptimizer"; } - virtual ~CSingleLaplaceNewtonOptimizer() { SG_UNREF(m_obj); } + virtual ~SingleLaplaceNewtonOptimizer() { } /** Set the inference method * @param obj the inference method */ - void set_target(CSingleLaplaceInferenceMethod *obj); + void set_target(std::shared_ptrobj); /** Unset the inference method * @param is_unref do we SG_UNREF the method @@ -293,7 +293,7 @@ class CSingleLaplaceNewtonOptimizer: public Minimizer void init(); /** the inference method */ - CSingleLaplaceInferenceMethod *m_obj; + std::shared_ptrm_obj; /** amount of tolerance for Newton's iterations */ float64_t m_tolerance; diff --git a/src/shogun/machine/gp/SingleSparseInference.cpp b/src/shogun/machine/gp/SingleSparseInference.cpp index 85f16eed13e..1c8dd272154 100644 --- a/src/shogun/machine/gp/SingleSparseInference.cpp +++ b/src/shogun/machine/gp/SingleSparseInference.cpp @@ -53,28 +53,20 @@ class SingleSparseInferenceCostFunction: public FirstOrderBoundConstraintsCostFu { public: SingleSparseInferenceCostFunction():FirstOrderBoundConstraintsCostFunction() { init(); } - virtual ~SingleSparseInferenceCostFunction() { SG_UNREF(m_obj); } + virtual ~SingleSparseInferenceCostFunction() { } virtual const char* get_name() const { return "SingleSparseInferenceCostFunction"; } - void set_target(CSingleSparseInference *obj) + void set_target(std::shared_ptrobj) { require(obj,"Object not set"); if(obj!=m_obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; m_obj->check_fully_sparse(); require(m_obj->m_fully_sparse,"Can not compute gradient"); } } - void unset_target(bool is_unref) - { - if(is_unref) - { - SG_UNREF(m_obj); - } - m_obj=NULL; - } virtual float64_t get_cost() { require(m_obj,"Object not set"); @@ -107,32 +99,32 @@ class SingleSparseInferenceCostFunction: public FirstOrderBoundConstraintsCostFu return m_obj->m_upper_bound; } private: - CSingleSparseInference *m_obj; + std::shared_ptrm_obj; void init() { m_obj=NULL; - //The existing implementation in CSGObject::get_parameter_incremental_hash() + //The existing implementation in SGObject::get_parameter_incremental_hash() //can NOT deal with circular reference when parameter_hash_changed() is called - //SG_ADD((CSGObject **)&m_obj, "CSigleSparseInference__m_obj", + //SG_ADD((std::shared_ptr*)&m_obj, "CSigleSparseInference__m_obj", //"m_obj in SingleSparseInferenceCostFunction"); } }; #endif //DOXYGEN_SHOULD_SKIP_THIS -CSingleSparseInference::CSingleSparseInference() : CSparseInference() +SingleSparseInference::SingleSparseInference() : SparseInference() { init(); } -CSingleSparseInference::CSingleSparseInference(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat) - : CSparseInference(kern, feat, m, lab, mod, lat) +SingleSparseInference::SingleSparseInference(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod, std::shared_ptr lat) + : SparseInference(kern, feat, m, lab, mod, lat) { init(); check_fully_sparse(); } -void CSingleSparseInference::init() +void SingleSparseInference::init() { m_fully_sparse=false; m_inducing_minimizer=NULL; @@ -151,7 +143,7 @@ void CSingleSparseInference::init() SG_ADD(&m_opt_inducing_features, "opt_inducing_features", "whether optimize inducing features"); - SG_ADD((CSGObject **)&m_inducing_minimizer, + SG_ADD((std::shared_ptr*)&m_inducing_minimizer, "inducing_minimizer", "Minimizer used in optimize inducing features"); m_max_ind_iterations=50; @@ -161,19 +153,19 @@ void CSingleSparseInference::init() m_upper_bound=SGVector(); } -void CSingleSparseInference::set_kernel(CKernel* kern) +void SingleSparseInference::set_kernel(std::shared_ptr kern) { - CInference::set_kernel(kern); + Inference::set_kernel(kern); check_fully_sparse(); } -CSingleSparseInference::~CSingleSparseInference() +SingleSparseInference::~SingleSparseInference() { - SG_UNREF(m_inducing_minimizer); + delete m_lock; } -void CSingleSparseInference::check_fully_sparse() +void SingleSparseInference::check_fully_sparse() { require(m_kernel, "Kernel must be set first"); if (strstr(m_kernel->get_name(), "SparseKernel")!=NULL) @@ -185,7 +177,7 @@ void CSingleSparseInference::check_fully_sparse() } } -SGVector CSingleSparseInference::get_derivative_wrt_inference_method( +SGVector SingleSparseInference::get_derivative_wrt_inference_method( const TParameter* param) { // the time complexity O(m^2*n) if the TO DO is done @@ -237,7 +229,7 @@ SGVector CSingleSparseInference::get_derivative_wrt_inference_method( return result; } -SGVector CSingleSparseInference::get_derivative_wrt_kernel( +SGVector SingleSparseInference::get_derivative_wrt_kernel( const TParameter* param) { require(param, "Param not set"); @@ -245,7 +237,7 @@ SGVector CSingleSparseInference::get_derivative_wrt_kernel( int64_t len=const_cast(param)->m_datatype.get_num_elements(); result=SGVector(len); - CFeatures *inducing_features=get_inducing_features(); + auto inducing_features=get_inducing_features(); for (index_t i=0; i deriv_trtr; @@ -275,11 +267,11 @@ SGVector CSingleSparseInference::get_derivative_wrt_kernel( result[i]=get_derivative_related_cov(deriv_trtr, deriv_uu, deriv_tru); result[i] *= std::exp(m_log_scale * 2.0); } - SG_UNREF(inducing_features); + return result; } -void CSingleSparseInference::check_bound(SGVector bound, const char* name) +void SingleSparseInference::check_bound(SGVector bound, const char* name) { if (bound.vlen>1) { @@ -296,29 +288,29 @@ void CSingleSparseInference::check_bound(SGVector bound, const char* } } -void CSingleSparseInference::set_lower_bound_of_inducing_features(SGVector bound) +void SingleSparseInference::set_lower_bound_of_inducing_features(SGVector bound) { check_bound(bound,"lower"); m_lower_bound=bound; } -void CSingleSparseInference::set_upper_bound_of_inducing_features(SGVector bound) +void SingleSparseInference::set_upper_bound_of_inducing_features(SGVector bound) { check_bound(bound, "upper"); m_upper_bound=bound; } -void CSingleSparseInference::set_max_iterations_for_inducing_features(int32_t it) +void SingleSparseInference::set_max_iterations_for_inducing_features(int32_t it) { require(it>0, "Iteration ({}) must be positive",it); m_max_ind_iterations=it; } -void CSingleSparseInference::set_tolearance_for_inducing_features(float64_t tol) +void SingleSparseInference::set_tolearance_for_inducing_features(float64_t tol) { require(tol>0, "Tolearance ({}) must be positive",tol); m_ind_tolerance=tol; } -void CSingleSparseInference::enable_optimizing_inducing_features(bool is_optmization, FirstOrderMinimizer* minimizer) +void SingleSparseInference::enable_optimizing_inducing_features(bool is_optmization, std::shared_ptr minimizer) { m_opt_inducing_features=is_optmization; if (m_opt_inducing_features) @@ -330,45 +322,42 @@ void CSingleSparseInference::enable_optimizing_inducing_features(bool is_optmiza { if (minimizer!=m_inducing_minimizer) { - SG_REF(minimizer); - SG_UNREF(m_inducing_minimizer); + + m_inducing_minimizer=minimizer; } } else { - SG_UNREF(m_inducing_minimizer); + #ifdef USE_GPL_SHOGUN #ifdef HAVE_NLOPT - m_inducing_minimizer=new CNLOPTMinimizer(); - SG_REF(m_inducing_minimizer); + m_inducing_minimizer=std::make_shared(); + #else m_inducing_minimizer=NULL; io::warn("We require NLOPT library for using default minimizer.\nYou can use other minimizer. (eg, LBFGSMinimier)"); #endif //HAVE_NLOPT -#else +#else m_inducing_minimizer=NULL; io::warn("We require NLOPT (GPL License) library for using default minimizer.\nYou can use other minimizer. (eg, LBFGSMinimier)"); #endif //USE_GPL_SHOGUN } } -void CSingleSparseInference::optimize_inducing_features() +void SingleSparseInference::optimize_inducing_features() { if (!m_opt_inducing_features) return; require(m_inducing_minimizer, "Please call enable_optimizing_inducing_features() first"); - SingleSparseInferenceCostFunction *cost_fun=new SingleSparseInferenceCostFunction(); - cost_fun->set_target(this); - bool cleanup=false; - if(this->ref_count()>1) - cleanup=true; + auto cost_fun=std::make_shared(); + cost_fun->set_target(shared_from_this()->as()); #ifdef USE_GPL_SHOGUN #ifdef HAVE_NLOPT - CNLOPTMinimizer* opt=dynamic_cast(m_inducing_minimizer); + auto opt=std::dynamic_pointer_cast(m_inducing_minimizer); if (opt) opt->set_nlopt_parameters(LD_LBFGS, m_max_ind_iterations, m_ind_tolerance, m_ind_tolerance); #endif //HAVE_NLOPT @@ -377,8 +366,7 @@ void CSingleSparseInference::optimize_inducing_features() m_inducing_minimizer->set_cost_function(cost_fun); m_inducing_minimizer->minimize(); m_inducing_minimizer->unset_cost_function(false); - cost_fun->unset_target(cleanup); - SG_UNREF(cost_fun); + } } diff --git a/src/shogun/machine/gp/SingleSparseInference.h b/src/shogun/machine/gp/SingleSparseInference.h index 6db7e872525..12d9d580a08 100644 --- a/src/shogun/machine/gp/SingleSparseInference.h +++ b/src/shogun/machine/gp/SingleSparseInference.h @@ -45,13 +45,13 @@ class SingleSparseInferenceCostFunction; /** @brief The sparse inference base class * for classification and regression for 1-D labels (1D regression and binary classification) */ -class CSingleSparseInference: public CSparseInference +class SingleSparseInference: public SparseInference { friend class SingleSparseInferenceCostFunction; public: /** default constructor */ - CSingleSparseInference(); + SingleSparseInference(); /** constructor * @@ -62,11 +62,11 @@ friend class SingleSparseInferenceCostFunction; * @param model likelihood model to use * @param inducing_features features to use */ - CSingleSparseInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model, - CFeatures* inducing_features); + SingleSparseInference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model, + std::shared_ptr inducing_features); - virtual ~CSingleSparseInference(); + virtual ~SingleSparseInference(); /** returns the name of the inference method * @@ -78,7 +78,7 @@ friend class SingleSparseInferenceCostFunction; * * @param kern kernel to set */ - virtual void set_kernel(CKernel* kern); + virtual void set_kernel(std::shared_ptr kern); /** opitmize inducing features * @@ -126,7 +126,7 @@ friend class SingleSparseInferenceCostFunction; * @param is_optmization enable optimization * @param minimizer minimizer used in optimization */ - virtual void enable_optimizing_inducing_features(bool is_optmization, FirstOrderMinimizer* minimizer=NULL); + virtual void enable_optimizing_inducing_features(bool is_optmization, std::shared_ptr minimizer=NULL); protected: @@ -227,7 +227,7 @@ friend class SingleSparseInferenceCostFunction; CLock* m_lock; /** minimizer used in finding optimal inducing features*/ - FirstOrderMinimizer* m_inducing_minimizer; + std::shared_ptr m_inducing_minimizer; private: /* init */ void init(); diff --git a/src/shogun/machine/gp/SoftMaxLikelihood.cpp b/src/shogun/machine/gp/SoftMaxLikelihood.cpp index 16c7a5e0d3e..0104e5ca948 100644 --- a/src/shogun/machine/gp/SoftMaxLikelihood.cpp +++ b/src/shogun/machine/gp/SoftMaxLikelihood.cpp @@ -47,30 +47,30 @@ using namespace shogun; using namespace Eigen; -CSoftMaxLikelihood::CSoftMaxLikelihood() : RandomMixin() +SoftMaxLikelihood::SoftMaxLikelihood() : RandomMixin() { init(); } -CSoftMaxLikelihood::~CSoftMaxLikelihood() +SoftMaxLikelihood::~SoftMaxLikelihood() { } -void CSoftMaxLikelihood::init() +void SoftMaxLikelihood::init() { m_num_samples=10000; SG_ADD(&m_num_samples, "num_samples", "Number of samples to be generated"); } -SGVector CSoftMaxLikelihood::get_log_probability_f(const CLabels* lab, +SGVector SoftMaxLikelihood::get_log_probability_f(std::shared_ptr lab, SGVector func) const { require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_MULTICLASS, - "Labels must be type of CMulticlassLabels"); + "Labels must be type of MulticlassLabels"); - SGVector labels=((CMulticlassLabels*) lab)->get_int_labels(); + SGVector labels=lab->as()->get_int_labels(); for (int32_t i=0;i-1)&&(labels[i] CSoftMaxLikelihood::get_log_probability_f(const CLabels* lab return ret; } -SGVector CSoftMaxLikelihood::get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const +SGVector SoftMaxLikelihood::get_log_probability_derivative_f( + std::shared_ptr lab, SGVector func, index_t i) const { int32_t num_rows=lab->get_num_labels(); int32_t num_cols=func.vlen/num_rows; @@ -114,16 +114,16 @@ SGVector CSoftMaxLikelihood::get_log_probability_derivative_f( return get_log_probability_derivative3_f(f); } -SGVector CSoftMaxLikelihood::get_log_probability_derivative1_f( - const CLabels* lab, SGMatrix func) const +SGVector SoftMaxLikelihood::get_log_probability_derivative1_f( + std::shared_ptr lab, SGMatrix func) const { require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_MULTICLASS, - "Labels must be type of CMulticlassLabels"); + "Labels must be type of MulticlassLabels"); require(lab->get_num_labels()==func.num_rows, "Number of labels must match " "number of vectors in function"); - SGVector labels=((CMulticlassLabels*) lab)->get_int_labels(); + SGVector labels=lab->as()->get_int_labels(); for (int32_t i=0;i-1)&&(labels[i] CSoftMaxLikelihood::get_log_probability_derivative1_f( return ret; } -SGVector CSoftMaxLikelihood::get_log_probability_derivative2_f(SGMatrix func) const +SGVector SoftMaxLikelihood::get_log_probability_derivative2_f(SGMatrix func) const { SGVector ret=SGVector(func.num_rows*func.num_cols*func.num_cols); Map eigen_ret(ret.vector,func.num_rows*func.num_cols,func.num_cols); @@ -180,10 +180,10 @@ SGVector CSoftMaxLikelihood::get_log_probability_derivative2_f(SGMatr return ret; } -SGVector CSoftMaxLikelihood::get_log_probability_derivative3_f(SGMatrix +SGVector SoftMaxLikelihood::get_log_probability_derivative3_f(SGMatrix func) const { - SGVector ret=SGVector(CMath::pow(func.num_cols,3)*func.num_rows); + SGVector ret=SGVector(Math::pow(func.num_cols,3)*func.num_rows); Map eigen_f(func.matrix,func.num_rows,func.num_cols); @@ -210,8 +210,8 @@ SGVector CSoftMaxLikelihood::get_log_probability_derivative3_f(SGMatr sum_temp=sum_temp-f1(i,c1)*f1(i,c2); sum_temp+=2.0*f1(i,c1)*f1(i,c2)*f1(i,c3); - ret[i*CMath::pow(func.num_cols,3)+ - c1*CMath::pow(func.num_cols,2)+c2*func.num_cols+c3]=sum_temp; + ret[i*Math::pow(func.num_cols,3)+ + c1*Math::pow(func.num_cols,2)+c2*func.num_cols+c3]=sum_temp; } } } @@ -220,15 +220,15 @@ SGVector CSoftMaxLikelihood::get_log_probability_derivative3_f(SGMatr return ret; } -void CSoftMaxLikelihood::set_num_samples(index_t num_samples) +void SoftMaxLikelihood::set_num_samples(index_t num_samples) { require(num_samples>0, "Numer of samples ({}) should be positive", num_samples); m_num_samples=num_samples; } -SGVector CSoftMaxLikelihood::predictive_helper(SGVector mu, - SGVector s2, const CLabels *lab, EMCSamplerType option) const +SGVector SoftMaxLikelihood::predictive_helper(SGVector mu, + SGVector s2, std::shared_ptrlab, EMCSamplerType option) const { const index_t C=s2.vlen/mu.vlen; const index_t n=mu.vlen/C; @@ -244,13 +244,13 @@ SGVector CSoftMaxLikelihood::predictive_helper(SGVector mu if (lab) { require(lab->get_label_type()==LT_MULTICLASS, - "Labels must be type of CMulticlassLabels"); + "Labels must be type of MulticlassLabels"); const index_t n1=lab->get_num_labels(); require(n==n1, "Number of samples ({}) learned from mu and s2 must match " "number of labels({}) in lab",n,n1); - y=((CMulticlassLabels*) lab)->get_int_labels(); + y=lab->as()->get_int_labels(); for (index_t i=0;i CSoftMaxLikelihood::predictive_helper(SGVector mu return ret; } -SGVector CSoftMaxLikelihood::get_predictive_log_probabilities( - SGVector mu, SGVector s2, const CLabels *lab) +SGVector SoftMaxLikelihood::get_predictive_log_probabilities( + SGVector mu, SGVector s2, std::shared_ptrlab) { return predictive_helper(mu, s2, lab, MC_Probability); } -SGVector CSoftMaxLikelihood::mc_sampler(index_t num_samples, SGVector mean, +SGVector SoftMaxLikelihood::mc_sampler(index_t num_samples, SGVector mean, SGMatrix Sigma, SGVector y) const { - CGaussianDistribution *gen=new CGaussianDistribution (mean, Sigma); + auto gen=std::make_shared (mean, Sigma); seed(gen); //category by samples @@ -327,20 +327,20 @@ SGVector CSoftMaxLikelihood::mc_sampler(index_t num_samples, SGVector Map eigen_y(y.vector, y.vlen); eigen_est=(mean_samples.array()*eigen_y.array())+(1-mean_samples.array())*(1-eigen_y.array()); - SG_UNREF(gen); + return est; } -SGVector CSoftMaxLikelihood::get_predictive_means( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector SoftMaxLikelihood::get_predictive_means( + SGVector mu, SGVector s2, std::shared_ptr lab) const { return predictive_helper(mu, s2, lab, MC_Mean); } -SGVector CSoftMaxLikelihood::get_predictive_variances( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector SoftMaxLikelihood::get_predictive_variances( + SGVector mu, SGVector s2, std::shared_ptr lab) const { return predictive_helper(mu, s2, lab, MC_Variance); } diff --git a/src/shogun/machine/gp/SoftMaxLikelihood.h b/src/shogun/machine/gp/SoftMaxLikelihood.h index c361220dc3e..e80c8beba42 100644 --- a/src/shogun/machine/gp/SoftMaxLikelihood.h +++ b/src/shogun/machine/gp/SoftMaxLikelihood.h @@ -78,14 +78,14 @@ enum EMCSamplerType * and then using the samplers to estimate the predictive marginal distribution. * */ -class CSoftMaxLikelihood : public RandomMixin +class SoftMaxLikelihood : public RandomMixin { public: /** default constructor */ - CSoftMaxLikelihood(); + SoftMaxLikelihood(); /** destructor */ - virtual ~CSoftMaxLikelihood(); + virtual ~SoftMaxLikelihood(); /** returns the name of the likelihood model * @@ -110,7 +110,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return final means (based on 0 and 1 bernoulli-encoding) evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$ * The implementation is based on a simple Monte Carlo sampler from the pseudo code. @@ -129,7 +129,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return final variances (based on 0 and 1 bernoulli-encoding) evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns the logarithm of the predictive density of \f$y_*\f$: * The implementation is based on a simple Monte Carlo sampler from the pseudo code. @@ -163,7 +163,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return \f$log(p(y_*|X, y, x*))\f$ for each label \f$y_*\f$ (based on 0 and 1 bernoulli-encoding) */ virtual SGVector get_predictive_log_probabilities(SGVector mu, - SGVector s2, const CLabels *lab=NULL); + SGVector s2, std::shared_ptrlab=NULL); /** returns the logarithm of the point-wise likelihood \f$log(p(y_i|f_i))\f$ * for each label \f$y_i\f$, an integer between 1 and C (ie. number of classes). @@ -176,7 +176,7 @@ class CSoftMaxLikelihood : public RandomMixin * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const; /** get derivative of log likelihood \f$log(p(y|f))\f$ with respect to @@ -190,7 +190,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const; + std::shared_ptr lab, SGVector func, index_t i) const; /** returns the zeroth moment of a given (unnormalized) probability * distribution: @@ -204,7 +204,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return log zeroth moment \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const + SGVector s2, std::shared_ptr lab) const { error("Not Implemented"); return SGVector(); @@ -225,7 +225,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const + SGVector s2, std::shared_ptr lab, index_t i) const { error("Not Implemented"); return -1.0; @@ -246,7 +246,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const + SGVector s2, std::shared_ptr lab, index_t i) const { error("Not Implemented"); return -1.0; @@ -285,7 +285,7 @@ class CSoftMaxLikelihood : public RandomMixin * @return the statistics based on mc sampler */ SGVector predictive_helper(SGVector mu, - SGVector s2, const CLabels *lab, EMCSamplerType option) const; + SGVector s2, std::shared_ptrlab, EMCSamplerType option) const; /** the Monte method sampler * @@ -313,7 +313,7 @@ class CSoftMaxLikelihood : public RandomMixin * * @return derivative (NxC matrix linearized in column major format) */ - SGVector get_log_probability_derivative1_f(const CLabels* lab, SGMatrix func) const; + SGVector get_log_probability_derivative1_f(std::shared_ptr lab, SGMatrix func) const; /** get 2nd derivative of log likelihood \f$log(p(y|f))\f$ with respect to * location function \f$f\f$ diff --git a/src/shogun/machine/gp/SparseInference.cpp b/src/shogun/machine/gp/SparseInference.cpp index 7347f711ff5..9fe5da65124 100644 --- a/src/shogun/machine/gp/SparseInference.cpp +++ b/src/shogun/machine/gp/SparseInference.cpp @@ -40,23 +40,23 @@ using namespace shogun; using namespace Eigen; -CSparseInference::CSparseInference() : CInference() +SparseInference::SparseInference() : Inference() { init(); } -void CSparseInference::check_features() +void SparseInference::check_features() { require(m_features, "Input features not set"); } -void CSparseInference::convert_features() +void SparseInference::convert_features() { - CDotFeatures *feat_type=dynamic_cast(m_features); + auto feat_type=m_features->as(); SGMatrixlat_m(m_inducing_features.matrix, m_inducing_features.num_rows,m_inducing_features.num_cols,false); - CDotFeatures *lat_type=new CDenseFeatures(lat_m); + auto lat_type=std::make_shared>(lat_m); require(feat_type, "Input features ({}) must be DotFeatures" " or one of its subclasses", m_features->get_name()); @@ -80,22 +80,22 @@ void CSparseInference::convert_features() } io::warn("Input features may be deleted"); SGMatrix feat_m=feat_type->get_computed_dot_feature_matrix(); - SG_UNREF(m_features); - m_features=new CDenseFeatures(feat_m); - SG_REF(m_features); + + m_features=std::make_shared>(feat_m); + } - SG_UNREF(lat_type); + } -CSparseInference::CSparseInference(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat) - : CInference(kern, feat, m, lab, mod) +SparseInference::SparseInference(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod, std::shared_ptr lat) + : Inference(kern, feat, m, lab, mod) { init(); set_inducing_features(lat); } -void CSparseInference::init() +void SparseInference::init() { SG_ADD(&m_inducing_features, "inducing_features", "inducing features", ParameterProperties::HYPER | ParameterProperties::GRADIENT); @@ -109,30 +109,30 @@ void CSparseInference::init() m_inducing_features=SGMatrix(); } -void CSparseInference::set_inducing_noise(float64_t noise) +void SparseInference::set_inducing_noise(float64_t noise) { require(noise>0, "Noise ({}) for inducing points must be postive",noise); m_log_ind_noise = std::log(noise); } -float64_t CSparseInference::get_inducing_noise() +float64_t SparseInference::get_inducing_noise() { return std::exp(m_log_ind_noise); } -CSparseInference::~CSparseInference() +SparseInference::~SparseInference() { } -void CSparseInference::check_members() const +void SparseInference::check_members() const { - CInference::check_members(); + Inference::check_members(); require(m_inducing_features.num_rows, "Inducing features should not be empty"); require(m_inducing_features.num_cols, "Inducing features should not be empty"); } -SGVector CSparseInference::get_alpha() +SGVector SparseInference::get_alpha() { if (parameter_hash_changed()) update(); @@ -141,7 +141,7 @@ SGVector CSparseInference::get_alpha() return result; } -SGMatrix CSparseInference::get_cholesky() +SGMatrix SparseInference::get_cholesky() { if (parameter_hash_changed()) update(); @@ -150,7 +150,7 @@ SGMatrix CSparseInference::get_cholesky() return result; } -void CSparseInference::update_train_kernel() +void SparseInference::update_train_kernel() { //time complexity can be O(m*n) if the TO DO is done check_features(); @@ -159,7 +159,7 @@ void CSparseInference::update_train_kernel() m_kernel->init(m_features, m_features); m_ktrtr_diag=m_kernel->get_kernel_diagonal(); - CFeatures* inducing_features=get_inducing_features(); + auto inducing_features=get_inducing_features(); // create kernel matrix for inducing features m_kernel->init(inducing_features, inducing_features); @@ -169,6 +169,6 @@ void CSparseInference::update_train_kernel() m_kernel->init(inducing_features, m_features); m_ktru=m_kernel->get_kernel_matrix(); - SG_UNREF(inducing_features); + } diff --git a/src/shogun/machine/gp/SparseInference.h b/src/shogun/machine/gp/SparseInference.h index 8957189b9d2..07f8fb74965 100644 --- a/src/shogun/machine/gp/SparseInference.h +++ b/src/shogun/machine/gp/SparseInference.h @@ -68,11 +68,11 @@ namespace shogun * the (approximated) negative log marginal likelihood are computed based on \f$\Sigma_{Sparse}\f$. * */ -class CSparseInference: public CInference +class SparseInference: public Inference { public: /** default constructor */ - CSparseInference(); + SparseInference(); /** constructor * @@ -83,11 +83,11 @@ class CSparseInference: public CInference * @param model likelihood model to use * @param inducing_features features to use */ - CSparseInference(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model, - CFeatures* inducing_features); + SparseInference(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model, + std::shared_ptr inducing_features); - virtual ~CSparseInference(); + virtual ~SparseInference(); /** return what type of inference we are * @@ -105,10 +105,10 @@ class CSparseInference: public CInference * * @param feat features to set */ - virtual void set_inducing_features(CFeatures* feat) + virtual void set_inducing_features(std::shared_ptr feat) { require(feat,"Input inducing features must be not empty"); - CDotFeatures *lat_type=dynamic_cast(feat); + auto lat_type=std::dynamic_pointer_cast(feat); require(lat_type, "Inducing features ({}) must be" " DotFeatures or one of its subclasses", feat->get_name()); m_inducing_features=lat_type->get_computed_dot_feature_matrix(); @@ -118,13 +118,11 @@ class CSparseInference: public CInference * * @return features */ - virtual CFeatures* get_inducing_features() + virtual std::shared_ptr get_inducing_features() { SGMatrix out(m_inducing_features.matrix, m_inducing_features.num_rows,m_inducing_features.num_cols,false); - CFeatures* inducing_features=new CDenseFeatures(out); - SG_REF(inducing_features); - return inducing_features; + return std::make_shared>(out); } /** get alpha vector @@ -219,7 +217,7 @@ class CSparseInference: public CInference * 1. The type of the gradient wrt inducing features is float64_t, which is used to update inducing features * 2. Reason 1 implies that the type of inducing features can be float64_t while the type of features does not required * as float64_t - * 3. Reason 2 implies that the type of features must be a subclass of CDotFeatures, which can represent features as + * 3. Reason 2 implies that the type of features must be a subclass of DotFeatures, which can represent features as * float64_t */ virtual void convert_features(); diff --git a/src/shogun/machine/gp/StudentsTLikelihood.cpp b/src/shogun/machine/gp/StudentsTLikelihood.cpp index 6a4b11526df..0a16930e961 100644 --- a/src/shogun/machine/gp/StudentsTLikelihood.cpp +++ b/src/shogun/machine/gp/StudentsTLikelihood.cpp @@ -52,11 +52,11 @@ namespace shogun #ifndef DOXYGEN_SHOULD_SKIP_THIS /** Class of the probability density function of the normal distribution */ -class CNormalPDF : public CFunction +class NormalPDF : public Function { public: /** default constructor */ - CNormalPDF() + NormalPDF() { m_mu=0.0; m_sigma=1.0; @@ -67,7 +67,7 @@ class CNormalPDF : public CFunction * @param mu mean * @param sigma standard deviation */ - CNormalPDF(float64_t mu, float64_t sigma) + NormalPDF(float64_t mu, float64_t sigma) { m_mu=mu; m_sigma=sigma; @@ -93,8 +93,8 @@ class CNormalPDF : public CFunction */ virtual float64_t operator() (float64_t x) { - return (1.0 / (std::sqrt(2 * CMath::PI) * m_sigma)) * - std::exp(-CMath::sq(x - m_mu) / (2.0 * CMath::sq(m_sigma))); + return (1.0 / (std::sqrt(2 * Math::PI) * m_sigma)) * + std::exp(-Math::sq(x - m_mu) / (2.0 * Math::sq(m_sigma))); } private: @@ -108,11 +108,11 @@ class CNormalPDF : public CFunction /** Class of the probability density function of the non-standardized Student's * t-distribution */ -class CStudentsTPDF : public CFunction +class StudentsTPDF : public Function { public: /** default constructor */ - CStudentsTPDF() + StudentsTPDF() { m_sigma=1.0; m_mu=0.0; @@ -125,7 +125,7 @@ class CStudentsTPDF : public CFunction * @param nu degrees of freedom * @param mu location parameter */ - CStudentsTPDF(float64_t sigma, float64_t nu, float64_t mu) + StudentsTPDF(float64_t sigma, float64_t nu, float64_t mu) { m_sigma=sigma; m_mu=mu; @@ -159,14 +159,14 @@ class CStudentsTPDF : public CFunction */ virtual float64_t operator() (float64_t x) { - float64_t lZ = CStatistics::lgamma((m_nu + 1.0) / 2.0) - - CStatistics::lgamma(m_nu / 2.0) - - std::log(m_nu * CMath::PI * CMath::sq(m_sigma)) / 2.0; + float64_t lZ = Statistics::lgamma((m_nu + 1.0) / 2.0) - + Statistics::lgamma(m_nu / 2.0) - + std::log(m_nu * Math::PI * Math::sq(m_sigma)) / 2.0; return std::exp( lZ - ((m_nu + 1.0) / 2.0) * std::log( - 1.0 + CMath::sq(x - m_mu) / (m_nu * CMath::sq(m_sigma)))); + 1.0 + Math::sq(x - m_mu) / (m_nu * Math::sq(m_sigma)))); } private: @@ -181,7 +181,7 @@ class CStudentsTPDF : public CFunction }; /** Class of the function, which is a product of two given functions */ -class CProductFunction : public CFunction +class ProductFunction : public Function { public: /** constructor @@ -189,18 +189,18 @@ class CProductFunction : public CFunction * @param f f(x) * @param g g(x) */ - CProductFunction(CFunction* f, CFunction* g) + ProductFunction(std::shared_ptr f, std::shared_ptr g) { - SG_REF(f); - SG_REF(g); + + m_f=f; m_g=g; } - virtual ~CProductFunction() + virtual ~ProductFunction() { - SG_UNREF(m_f); - SG_UNREF(m_g); + + } /** returns value of the function at given point @@ -216,19 +216,19 @@ class CProductFunction : public CFunction private: /** function f(x) */ - CFunction* m_f; + std::shared_ptr m_f; /** function g(x) */ - CFunction* m_g; + std::shared_ptr m_g; }; /** Class of the f(x)=x */ -class CLinearFunction : public CFunction +class LinearFunction : public Function { public: /** default constructor */ - CLinearFunction() { } + LinearFunction() { } - virtual ~CLinearFunction() { } + virtual ~LinearFunction() { } /** returns value of the function at given point * @@ -243,13 +243,13 @@ class CLinearFunction : public CFunction }; /** Class of the f(x)=x^2 */ -class CQuadraticFunction : public CFunction +class QuadraticFunction : public Function { public: /** default constructor */ - CQuadraticFunction() { } + QuadraticFunction() { } - virtual ~CQuadraticFunction() { } + virtual ~QuadraticFunction() { } /** returns value of the function at given point * @@ -259,26 +259,26 @@ class CQuadraticFunction : public CFunction */ virtual float64_t operator() (float64_t x) { - return CMath::sq(x); + return Math::sq(x); } }; #endif /* DOXYGEN_SHOULD_SKIP_THIS */ -CStudentsTLikelihood::CStudentsTLikelihood() : CLikelihoodModel() +StudentsTLikelihood::StudentsTLikelihood() : LikelihoodModel() { init(); } -CStudentsTLikelihood::CStudentsTLikelihood(float64_t sigma, float64_t df) - : CLikelihoodModel() +StudentsTLikelihood::StudentsTLikelihood(float64_t sigma, float64_t df) + : LikelihoodModel() { init(); set_sigma(sigma); set_degrees_freedom(df); } -void CStudentsTLikelihood::init() +void StudentsTLikelihood::init() { m_log_sigma=0.0; m_log_df = std::log(2.0); @@ -286,36 +286,35 @@ void CStudentsTLikelihood::init() SG_ADD(&m_log_sigma, "log_sigma", "Scale parameter in log domain", ParameterProperties::HYPER | ParameterProperties::GRADIENT); } -CStudentsTLikelihood::~CStudentsTLikelihood() +StudentsTLikelihood::~StudentsTLikelihood() { } -CStudentsTLikelihood* CStudentsTLikelihood::obtain_from_generic( - CLikelihoodModel* lik) +std::shared_ptr StudentsTLikelihood::obtain_from_generic( + std::shared_ptr lik) { ASSERT(lik!=NULL); if (lik->get_model_type()!=LT_STUDENTST) - error("Provided likelihood is not of type CStudentsTLikelihood!"); + error("Provided likelihood is not of type StudentsTLikelihood!"); - SG_REF(lik); - return (CStudentsTLikelihood*)lik; + return lik->as(); } -SGVector CStudentsTLikelihood::get_predictive_means( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector StudentsTLikelihood::get_predictive_means( + SGVector mu, SGVector s2, std::shared_ptr lab) const { return SGVector(mu); } -SGVector CStudentsTLikelihood::get_predictive_variances( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector StudentsTLikelihood::get_predictive_variances( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector result(s2); Map eigen_result(result.vector, result.vlen); float64_t df=get_degrees_freedom(); if (df<2.0) - eigen_result=CMath::INFTY*VectorXd::Ones(result.vlen); + eigen_result=Math::INFTY*VectorXd::Ones(result.vlen); else { eigen_result += std::exp(m_log_sigma * 2.0) * df / (df - 2.0) * @@ -325,13 +324,13 @@ SGVector CStudentsTLikelihood::get_predictive_variances( return result; } -SGVector CStudentsTLikelihood::get_log_probability_f(const CLabels* lab, +SGVector StudentsTLikelihood::get_log_probability_f(std::shared_ptr lab, SGVector func) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -340,14 +339,14 @@ SGVector CStudentsTLikelihood::get_log_probability_f(const CLabels* l SGVector r(func.vlen); Map eigen_r(r.vector, r.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); float64_t df=get_degrees_freedom(); // compute lZ=log(gamma(df/2+1/2))-log(gamma(df/2))-log(df*pi*sigma^2)/2 VectorXd eigen_lZ = - (CStatistics::lgamma(df / 2.0 + 0.5) - CStatistics::lgamma(df / 2.0) - - log(df * CMath::PI * std::exp(m_log_sigma * 2.0)) / 2.0) * + (Statistics::lgamma(df / 2.0 + 0.5) - Statistics::lgamma(df / 2.0) - + log(df * Math::PI * std::exp(m_log_sigma * 2.0)) / 2.0) * VectorXd::Ones(r.vlen); // compute log probability: lp=lZ-(df+1)*log(1+(y-f).^2./(df*sigma^2))/2 @@ -360,13 +359,13 @@ SGVector CStudentsTLikelihood::get_log_probability_f(const CLabels* l return r; } -SGVector CStudentsTLikelihood::get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const +SGVector StudentsTLikelihood::get_log_probability_derivative_f( + std::shared_ptr lab, SGVector func, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); require(i>=1 && i<=3, "Index for derivative should be 1, 2 or 3"); @@ -376,7 +375,7 @@ SGVector CStudentsTLikelihood::get_log_probability_derivative_f( SGVector r(func.vlen); Map eigen_r(r.vector, r.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute r=y-f, r2=r.^2 @@ -419,13 +418,13 @@ SGVector CStudentsTLikelihood::get_log_probability_derivative_f( return r; } -SGVector CStudentsTLikelihood::get_first_derivative(const CLabels* lab, +SGVector StudentsTLikelihood::get_first_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -434,7 +433,7 @@ SGVector CStudentsTLikelihood::get_first_derivative(const CLabels* la Map eigen_f(func.vector, func.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute r=y-f and r2=(y-f).^2 @@ -447,8 +446,8 @@ SGVector CStudentsTLikelihood::get_first_derivative(const CLabels* la // compute derivative of log probability wrt df: // lp_ddf=df*(dloggamma(df/2+1/2)-dloggamma(df/2))/2-1/2- // df*log(1+r2/(df*sigma^2))/2 +(df/2+1/2)*r2./(df*sigma^2+r2) - eigen_r=(df*(CStatistics::dlgamma(df*0.5+0.5)- - CStatistics::dlgamma(df*0.5))*0.5-0.5)*VectorXd::Ones(r.vlen); + eigen_r=(df*(Statistics::dlgamma(df*0.5+0.5)- + Statistics::dlgamma(df*0.5))*0.5-0.5)*VectorXd::Ones(r.vlen); eigen_r -= df * (VectorXd::Ones(r.vlen) + @@ -485,13 +484,13 @@ SGVector CStudentsTLikelihood::get_first_derivative(const CLabels* la return SGVector(); } -SGVector CStudentsTLikelihood::get_second_derivative(const CLabels* lab, +SGVector StudentsTLikelihood::get_second_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -500,7 +499,7 @@ SGVector CStudentsTLikelihood::get_second_derivative(const CLabels* l Map eigen_f(func.vector, func.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute r=y-f and r2=(y-f).^2 @@ -541,13 +540,13 @@ SGVector CStudentsTLikelihood::get_second_derivative(const CLabels* l return SGVector(); } -SGVector CStudentsTLikelihood::get_third_derivative(const CLabels* lab, +SGVector StudentsTLikelihood::get_third_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); require(lab->get_num_labels()==func.vlen, "Number of labels must match " "length of the function vector"); @@ -556,7 +555,7 @@ SGVector CStudentsTLikelihood::get_third_derivative(const CLabels* la Map eigen_f(func.vector, func.vlen); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); Map eigen_y(y.vector, y.vlen); // compute r=y-f and r2=(y-f).^2 @@ -576,7 +575,7 @@ SGVector CStudentsTLikelihood::get_third_derivative(const CLabels* la float64_t sigma2 = std::exp(m_log_sigma * 2.0); eigen_r=df*(eigen_r2.cwiseProduct(eigen_r2-3*sigma2*(1.0+df)* - VectorXd::Ones(r.vlen))+(df*CMath::sq(sigma2))*VectorXd::Ones(r.vlen)); + VectorXd::Ones(r.vlen))+(df*Math::sq(sigma2))*VectorXd::Ones(r.vlen)); eigen_r=eigen_r.cwiseQuotient(a3); eigen_r*=(1.0-1.0/df); @@ -596,8 +595,8 @@ SGVector CStudentsTLikelihood::get_third_derivative(const CLabels* la return SGVector(); } -SGVector CStudentsTLikelihood::get_log_zeroth_moments( - SGVector mu, SGVector s2, const CLabels* lab) const +SGVector StudentsTLikelihood::get_log_zeroth_moments( + SGVector mu, SGVector s2, std::shared_ptr lab) const { SGVector y; @@ -608,9 +607,9 @@ SGVector CStudentsTLikelihood::get_log_zeroth_moments( "variances ({}) and number of labels ({}) should be the same", mu.vlen, s2.vlen, lab->get_num_labels()); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); - y=((CRegressionLabels*)lab)->get_labels(); + y=lab->as()->get_labels(); } else { @@ -623,17 +622,17 @@ SGVector CStudentsTLikelihood::get_log_zeroth_moments( } // create an object of normal pdf - CNormalPDF* f=new CNormalPDF(); + auto f=std::make_shared(); // create an object of Student's t pdf - CStudentsTPDF* g=new CStudentsTPDF(); + auto g=std::make_shared(); g->set_nu(get_degrees_freedom()); g->set_sigma(std::exp(m_log_sigma)); // create an object of product of Student's-t pdf and normal pdf - CProductFunction* h=new CProductFunction(f, g); - SG_REF(h); + auto h=std::make_shared(f, g); + // compute probabilities using numerical integration SGVector r(mu.vlen); @@ -649,14 +648,14 @@ SGVector CStudentsTLikelihood::get_log_zeroth_moments( #ifdef USE_GPL_SHOGUN // evaluate integral on (-inf, inf) - r[i]=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); + r[i]=Integration::integrate_quadgk(h, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(h, mu[i], Math::INFTY); #else error("StudentsT likelihood moments only supported under GPL."); #endif //USE_GPL_SHOGUN } - SG_UNREF(h); + for (index_t i=0; i CStudentsTLikelihood::get_log_zeroth_moments( return r; } -float64_t CStudentsTLikelihood::get_first_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t StudentsTLikelihood::get_first_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -675,44 +674,44 @@ float64_t CStudentsTLikelihood::get_first_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); // create an object of normal pdf - CNormalPDF* f = new CNormalPDF(mu[i], std::sqrt(s2[i])); + auto f = std::make_shared(mu[i], std::sqrt(s2[i])); // create an object of Student's t pdf - CStudentsTPDF* g = - new CStudentsTPDF(std::exp(m_log_sigma), get_degrees_freedom(), y[i]); + auto g = + std::make_shared(std::exp(m_log_sigma), get_degrees_freedom(), y[i]); // create an object of h(x)=N(x|mu,sigma)*t(x|mu,sigma,nu) - CProductFunction* h=new CProductFunction(f, g); + auto h=std::make_shared(f, g); // create an object of k(x)=x*N(x|mu,sigma)*t(x|mu,sigma,nu) - CProductFunction* k=new CProductFunction(new CLinearFunction(), h); - SG_REF(k); + auto k=std::make_shared(std::make_shared(), h); + float64_t Ex=0; #ifdef USE_GPL_SHOGUN // compute Z = \int N(x|mu,sigma)*t(x|mu,sigma,nu) dx - float64_t Z=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); + float64_t Z=Integration::integrate_quadgk(h, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(h, mu[i], Math::INFTY); // compute 1st moment: // E[x] = Z^-1 * \int x*N(x|mu,sigma)*t(x|mu,sigma,nu)dx - Ex=(CIntegration::integrate_quadgk(k, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(k, mu[i], CMath::INFTY))/Z; + Ex=(Integration::integrate_quadgk(k, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(k, mu[i], Math::INFTY))/Z; #else error("StudentsT likelihood moments only supported under GPL."); #endif //USE_GPL_SHOGUN - SG_UNREF(k); + return Ex; } -float64_t CStudentsTLikelihood::get_second_moment(SGVector mu, - SGVector s2, const CLabels *lab, index_t i) const +float64_t StudentsTLikelihood::get_second_moment(SGVector mu, + SGVector s2, std::shared_ptrlab, index_t i) const { // check the parameters require(lab, "Labels are required (lab should not be NULL)"); @@ -722,51 +721,51 @@ float64_t CStudentsTLikelihood::get_second_moment(SGVector mu, mu.vlen, s2.vlen, lab->get_num_labels()); require(i>=0 && i<=mu.vlen, "Index ({}) out of bounds!", i); require(lab->get_label_type()==LT_REGRESSION, - "Labels must be type of CRegressionLabels"); + "Labels must be type of RegressionLabels"); - SGVector y=((CRegressionLabels*)lab)->get_labels(); + SGVector y=lab->as()->get_labels(); // create an object of normal pdf - CNormalPDF* f = new CNormalPDF(mu[i], std::sqrt(s2[i])); + auto f = std::make_shared(mu[i], std::sqrt(s2[i])); // create an object of Student's t pdf - CStudentsTPDF* g = - new CStudentsTPDF(std::exp(m_log_sigma), get_degrees_freedom(), y[i]); + auto g = + std::make_shared(std::exp(m_log_sigma), get_degrees_freedom(), y[i]); // create an object of h(x)=N(x|mu,sigma)*t(x|mu,sigma,nu) - CProductFunction* h=new CProductFunction(f, g); + auto h=std::make_shared(f, g); // create an object of k(x)=x*N(x|mu,sigma)*t(x|mu,sigma,nu) - CProductFunction* k=new CProductFunction(new CLinearFunction(), h); - SG_REF(k); + auto k=std::make_shared(std::make_shared(), h); + // create an object of p(x)=x^2*N(x|mu,sigma^2)*t(x|mu,sigma,nu) - CProductFunction* p=new CProductFunction(new CQuadraticFunction(), h); - SG_REF(p); + auto p=std::make_shared(std::make_shared(), h); + float64_t Ex=0; float64_t Ex2=0; #ifdef USE_GPL_SHOGUN // compute Z = \int N(x|mu,sigma)*t(x|mu,sigma,nu) dx - float64_t Z=CIntegration::integrate_quadgk(h, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(h, mu[i], CMath::INFTY); + float64_t Z=Integration::integrate_quadgk(h, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(h, mu[i], Math::INFTY); // compute 1st moment: // E[x] = Z^-1 * \int x*N(x|mu,sigma)*t(x|mu,sigma,nu)dx - Ex=(CIntegration::integrate_quadgk(k, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(k, mu[i], CMath::INFTY))/Z; + Ex=(Integration::integrate_quadgk(k, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(k, mu[i], Math::INFTY))/Z; // compute E[x^2] = Z^-1 * \int x^2*N(x|mu,sigma)*t(x|mu,sigma,nu)dx - Ex2=(CIntegration::integrate_quadgk(p, -CMath::INFTY, mu[i])+ - CIntegration::integrate_quadgk(p, mu[i], CMath::INFTY))/Z; + Ex2=(Integration::integrate_quadgk(p, -Math::INFTY, mu[i])+ + Integration::integrate_quadgk(p, mu[i], Math::INFTY))/Z; #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN - SG_UNREF(k); - SG_UNREF(p); + + // return 2nd moment: Var[x]=E[x^2]-E[x]^2 - return Ex2-CMath::sq(Ex); + return Ex2-Math::sq(Ex); } } diff --git a/src/shogun/machine/gp/StudentsTLikelihood.h b/src/shogun/machine/gp/StudentsTLikelihood.h index d316ced0776..242f326d29d 100644 --- a/src/shogun/machine/gp/StudentsTLikelihood.h +++ b/src/shogun/machine/gp/StudentsTLikelihood.h @@ -54,20 +54,20 @@ namespace shogun * The hyperparameters of the Student's t-likelihood model are \f$\sigma\f$ - * scale parameter, and \f$\nu\f$ - degrees of freedom. */ -class CStudentsTLikelihood: public CLikelihoodModel +class StudentsTLikelihood: public LikelihoodModel { public: /** default constructor */ - CStudentsTLikelihood(); + StudentsTLikelihood(); /** constructor * * @param sigma noise variance * @param df degrees of freedom */ - CStudentsTLikelihood(float64_t sigma, float64_t df); + StudentsTLikelihood(float64_t sigma, float64_t df); - virtual ~CStudentsTLikelihood(); + virtual ~StudentsTLikelihood(); /** returns the name of the likelihood model * @@ -116,9 +116,9 @@ class CStudentsTLikelihood: public CLikelihoodModel /** helper method used to specialize a base class instance * * @param likelihood likelihood model - * @return casted CStudentsTLikelihood object + * @return casted StudentsTLikelihood object */ - static CStudentsTLikelihood* obtain_from_generic(CLikelihoodModel* likelihood); + static std::shared_ptr obtain_from_generic(std::shared_ptr likelihood); /** returns mean of the predictive marginal \f$p(y_*|X,y,x_*)\f$. * @@ -135,7 +135,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * @return final means evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$. * @@ -152,7 +152,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** get model type * @@ -171,7 +171,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const; /** get derivative of log likelihood \f$log(P(y|f))\f$ with respect to @@ -185,7 +185,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const; + std::shared_ptr lab, SGVector func, index_t i) const; /** get derivative of log likelihood \f$log(P(y|f))\f$ with respect to given * parameter @@ -196,7 +196,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * * @return derivative */ - virtual SGVector get_first_derivative(const CLabels* lab, + virtual SGVector get_first_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** get derivative of the first derivative of log likelihood with respect to @@ -209,7 +209,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * * @return derivative */ - virtual SGVector get_second_derivative(const CLabels* lab, + virtual SGVector get_second_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** get derivative of the second derivative of log likelihood with respect @@ -222,7 +222,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * * @return derivative */ - virtual SGVector get_third_derivative(const CLabels* lab, + virtual SGVector get_third_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** returns the zeroth moment of a given (unnormalized) probability @@ -242,7 +242,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * @return log zeroth moments \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -259,7 +259,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** returns the second moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -276,7 +276,7 @@ class CStudentsTLikelihood: public CLikelihoodModel * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** return whether Student's likelihood function supports regression * diff --git a/src/shogun/machine/gp/StudentsTVGLikelihood.cpp b/src/shogun/machine/gp/StudentsTVGLikelihood.cpp index a37e7e6c4c2..ecc57a9694a 100644 --- a/src/shogun/machine/gp/StudentsTVGLikelihood.cpp +++ b/src/shogun/machine/gp/StudentsTVGLikelihood.cpp @@ -46,16 +46,16 @@ using namespace Eigen; namespace shogun { -CStudentsTVGLikelihood::CStudentsTVGLikelihood() - : CNumericalVGLikelihood() +StudentsTVGLikelihood::StudentsTVGLikelihood() + : NumericalVGLikelihood() { m_log_sigma = 0.0; m_log_df = std::log(2.0); init(); } -CStudentsTVGLikelihood::CStudentsTVGLikelihood(float64_t sigma, float64_t df) - : CNumericalVGLikelihood() +StudentsTVGLikelihood::StudentsTVGLikelihood(float64_t sigma, float64_t df) + : NumericalVGLikelihood() { require(sigma>0.0, "Scale parameter ({}) must be greater than zero", sigma); require(df>1.0, "Number of degrees of freedom ({}) must be greater than one", df); @@ -65,18 +65,18 @@ CStudentsTVGLikelihood::CStudentsTVGLikelihood(float64_t sigma, float64_t df) init(); } -CStudentsTVGLikelihood::~CStudentsTVGLikelihood() +StudentsTVGLikelihood::~StudentsTVGLikelihood() { } -void CStudentsTVGLikelihood::init_likelihood() +void StudentsTVGLikelihood::init_likelihood() { set_likelihood( - new CStudentsTLikelihood( + std::make_shared( std::exp(m_log_sigma), std::exp(m_log_df) + 1.0)); } -void CStudentsTVGLikelihood::init() +void StudentsTVGLikelihood::init() { init_likelihood(); SG_ADD(&m_log_df, "log_df", "Degrees of freedom in log domain", ParameterProperties::HYPER | ParameterProperties::GRADIENT); diff --git a/src/shogun/machine/gp/StudentsTVGLikelihood.h b/src/shogun/machine/gp/StudentsTVGLikelihood.h index abb27e29f7e..64ba811228a 100644 --- a/src/shogun/machine/gp/StudentsTVGLikelihood.h +++ b/src/shogun/machine/gp/StudentsTVGLikelihood.h @@ -53,19 +53,19 @@ template class SGMatrix; * \sum_{{i=1}^n}{E_{q(f_i|{\mu}_i,{\sigma}^2_i)}[logP(y_i|f_i)]} * \f] */ -class CStudentsTVGLikelihood : public CNumericalVGLikelihood +class StudentsTVGLikelihood : public NumericalVGLikelihood { public: - CStudentsTVGLikelihood(); + StudentsTVGLikelihood(); /** constructor * * @param sigma noise variance * @param df degrees of freedom */ - CStudentsTVGLikelihood(float64_t sigma, float64_t df); + StudentsTVGLikelihood(float64_t sigma, float64_t df); - virtual ~CStudentsTVGLikelihood(); + virtual ~StudentsTVGLikelihood(); /** returns the name of the likelihood model * diff --git a/src/shogun/machine/gp/VarDTCInferenceMethod.cpp b/src/shogun/machine/gp/VarDTCInferenceMethod.cpp index 355b8a6715b..49ba6361787 100644 --- a/src/shogun/machine/gp/VarDTCInferenceMethod.cpp +++ b/src/shogun/machine/gp/VarDTCInferenceMethod.cpp @@ -45,19 +45,19 @@ using namespace shogun; using namespace Eigen; -CVarDTCInferenceMethod::CVarDTCInferenceMethod() : CSingleSparseInference() +VarDTCInferenceMethod::VarDTCInferenceMethod() : SingleSparseInference() { init(); } -CVarDTCInferenceMethod::CVarDTCInferenceMethod(CKernel* kern, CFeatures* feat, - CMeanFunction* m, CLabels* lab, CLikelihoodModel* mod, CFeatures* lat) - : CSingleSparseInference(kern, feat, m, lab, mod, lat) +VarDTCInferenceMethod::VarDTCInferenceMethod(std::shared_ptr kern, std::shared_ptr feat, + std::shared_ptr m, std::shared_ptr lab, std::shared_ptr mod, std::shared_ptr lat) + : SingleSparseInference(kern, feat, m, lab, mod, lat) { init(); } -void CVarDTCInferenceMethod::init() +void VarDTCInferenceMethod::init() { m_yy=0.0; m_f3=0.0; @@ -80,13 +80,13 @@ void CVarDTCInferenceMethod::init() SG_ADD(&m_Knm_inv_Lm, "Knm_Inv_Lm", "Knm_Inv_Lm"); } -CVarDTCInferenceMethod::~CVarDTCInferenceMethod() +VarDTCInferenceMethod::~VarDTCInferenceMethod() { } -void CVarDTCInferenceMethod::compute_gradient() +void VarDTCInferenceMethod::compute_gradient() { - CInference::compute_gradient(); + Inference::compute_gradient(); if (!m_gradient_update) { @@ -96,11 +96,11 @@ void CVarDTCInferenceMethod::compute_gradient() } } -void CVarDTCInferenceMethod::update() +void VarDTCInferenceMethod::update() { SG_TRACE("entering"); - CInference::update(); + Inference::update(); update_chol(); update_alpha(); m_gradient_update=false; @@ -109,8 +109,8 @@ void CVarDTCInferenceMethod::update() SG_TRACE("leaving"); } -CVarDTCInferenceMethod* CVarDTCInferenceMethod::obtain_from_generic( - CInference* inference) +std::shared_ptr VarDTCInferenceMethod::obtain_from_generic( + std::shared_ptr inference) { if (inference==NULL) return NULL; @@ -118,28 +118,27 @@ CVarDTCInferenceMethod* CVarDTCInferenceMethod::obtain_from_generic( if (inference->get_inference_type()!=INF_KL_SPARSE_REGRESSION) error("Provided inference is not of type CVarDTCInferenceMethod!"); - SG_REF(inference); - return (CVarDTCInferenceMethod*)inference; + + return inference->as(); } -void CVarDTCInferenceMethod::check_members() const +void VarDTCInferenceMethod::check_members() const { - CSingleSparseInference::check_members(); + SingleSparseInference::check_members(); require(m_model->get_model_type()==LT_GAUSSIAN, "VarDTC inference method can only use Gaussian likelihood function"); require(m_labels->get_label_type()==LT_REGRESSION, "Labels must be type " - "of CRegressionLabels"); + "of RegressionLabels"); } -SGVector CVarDTCInferenceMethod::get_diagonal_vector() +SGVector VarDTCInferenceMethod::get_diagonal_vector() { not_implemented(SOURCE_LOCATION); - //the inference method does not need to use this return SGVector(); } -float64_t CVarDTCInferenceMethod::get_negative_log_marginal_likelihood() +float64_t VarDTCInferenceMethod::get_negative_log_marginal_likelihood() { if (parameter_hash_changed()) update(); @@ -150,7 +149,7 @@ float64_t CVarDTCInferenceMethod::get_negative_log_marginal_likelihood() //F012 =-(model.n-model.m)*model.Likelihood.logtheta-0.5*model.n*log(2*pi)-(0.5/sigma2)*(model.yy)-sum(log(diag(La))); float64_t neg_f012 = (m_ktru.num_cols - m_ktru.num_rows) * std::log(m_sigma2) / 2.0 + - 0.5 * m_ktru.num_cols * std::log(2 * CMath::PI) + + 0.5 * m_ktru.num_cols * std::log(2 * Math::PI) + 0.5 * m_yy / (m_sigma2)-eigen_inv_La.diagonal().array().log().sum(); //F3 = (0.5/sigma2)*(yKnmInvLmInvLa*yKnmInvLmInvLa'); @@ -165,10 +164,10 @@ float64_t CVarDTCInferenceMethod::get_negative_log_marginal_likelihood() return neg_f012+neg_f3+neg_trk; } -void CVarDTCInferenceMethod::update_chol() +void VarDTCInferenceMethod::update_chol() { // get the sigma variable from the Gaussian likelihood model - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); m_sigma2=sigma*sigma; @@ -223,13 +222,13 @@ void CVarDTCInferenceMethod::update_chol() eigen_C.diagonal().array().sum()); } -void CVarDTCInferenceMethod::update_alpha() +void VarDTCInferenceMethod::update_alpha() { Map eigen_Knm_inv_Lm(m_Knm_inv_Lm.matrix, m_Knm_inv_Lm.num_rows, m_Knm_inv_Lm.num_cols); Map eigen_inv_La(m_inv_La.matrix, m_inv_La.num_rows, m_inv_La.num_cols); Map eigen_inv_Lm(m_inv_Lm.matrix, m_inv_Lm.num_rows, m_inv_Lm.num_cols); - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=m_labels->as()->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); @@ -249,7 +248,7 @@ void CVarDTCInferenceMethod::update_alpha() m_f3=0.5*eigen_y_Knm_inv_Lm_inv_La_transpose.dot(eigen_y_Knm_inv_Lm_inv_La_transpose)/m_sigma2; } -void CVarDTCInferenceMethod::update_deriv() +void VarDTCInferenceMethod::update_deriv() { Map eigen_inv_La(m_inv_La.matrix, m_inv_La.num_rows, m_inv_La.num_cols); Map eigen_inv_Lm(m_inv_Lm.matrix, m_inv_Lm.num_rows, m_inv_Lm.num_cols); @@ -265,7 +264,7 @@ void CVarDTCInferenceMethod::update_deriv() Map eigen_Tmm(m_Tmm.matrix, m_Tmm.num_rows, m_Tmm.num_cols); Map eigen_Tnm(m_Tnm.matrix, m_Tnm.num_rows, m_Tnm.num_cols); - CGaussianLikelihood* lik = m_model->as(); + auto lik = m_model->as(); float64_t sigma=lik->get_sigma(); m_sigma2=sigma*sigma; @@ -283,7 +282,7 @@ void CVarDTCInferenceMethod::update_deriv() //Tmm = Tmm - (invLm*(C*invLm'))/sigma2; eigen_Tmm = Tmm - (eigen_inv_Lm*eigen_C*eigen_inv_Lm.transpose()/m_sigma2); - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=m_labels->as()->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); @@ -292,21 +291,21 @@ void CVarDTCInferenceMethod::update_deriv() eigen_Tnm += (eigen_y-eigen_m)*eigen_alpha.transpose(); } -SGVector CVarDTCInferenceMethod::get_posterior_mean() +SGVector VarDTCInferenceMethod::get_posterior_mean() { not_implemented(SOURCE_LOCATION); //TODO: implement this method once I get time return SGVector(); } -SGMatrix CVarDTCInferenceMethod::get_posterior_covariance() +SGMatrix VarDTCInferenceMethod::get_posterior_covariance() { not_implemented(SOURCE_LOCATION); //TODO: implement this method once I get time return SGMatrix(); } -SGVector CVarDTCInferenceMethod::get_derivative_wrt_likelihood_model( +SGVector VarDTCInferenceMethod::get_derivative_wrt_likelihood_model( const TParameter* param) { require(!strcmp(param->m_name, "log_sigma"), "Can't compute derivative of " @@ -334,7 +333,7 @@ SGVector CVarDTCInferenceMethod::get_derivative_wrt_likelihood_model( return dlik; } -SGVector CVarDTCInferenceMethod::get_derivative_wrt_inducing_features( +SGVector VarDTCInferenceMethod::get_derivative_wrt_inducing_features( const TParameter* param) { //[DXu DXunm] = kernelSparseGradInd(model, Tmm, Tnm); @@ -349,7 +348,7 @@ SGVector CVarDTCInferenceMethod::get_derivative_wrt_inducing_features deriv_lat.zero(); m_lock->lock(); - CFeatures *inducing_features=get_inducing_features(); + auto inducing_features=get_inducing_features(); //asymtric part (related to xu and x) m_kernel->init(inducing_features, m_features); for(int32_t lat_idx=0; lat_idx CVarDTCInferenceMethod::get_derivative_wrt_inducing_features deriv_lat_col_vec += eigen_deriv_mat * (-std::exp(m_log_scale * 2.0) * eigen_Tmm.col(lat_lidx)); } - SG_UNREF(inducing_features); + m_lock->unlock(); return deriv_lat; } -SGVector CVarDTCInferenceMethod::get_derivative_wrt_inducing_noise( +SGVector VarDTCInferenceMethod::get_derivative_wrt_inducing_noise( const TParameter* param) { require(param, "Param not set"); @@ -396,7 +395,7 @@ SGVector CVarDTCInferenceMethod::get_derivative_wrt_inducing_noise( return result; } -float64_t CVarDTCInferenceMethod::get_derivative_related_cov(SGVector ddiagKi, +float64_t VarDTCInferenceMethod::get_derivative_related_cov(SGVector ddiagKi, SGMatrix dKuui, SGMatrix dKui) { Map eigen_ddiagKi(ddiagKi.vector, ddiagKi.vlen); @@ -414,7 +413,7 @@ float64_t CVarDTCInferenceMethod::get_derivative_related_cov(SGVector return dkern; } -SGVector CVarDTCInferenceMethod::get_derivative_wrt_mean( +SGVector VarDTCInferenceMethod::get_derivative_wrt_mean( const TParameter* param) { require(param, "Param not set"); @@ -422,7 +421,7 @@ SGVector CVarDTCInferenceMethod::get_derivative_wrt_mean( int64_t len=const_cast(param)->m_datatype.get_num_elements(); result=SGVector(len); - SGVector y=((CRegressionLabels*) m_labels)->get_labels(); + SGVector y=m_labels->as()->get_labels(); Map eigen_y(y.vector, y.vlen); SGVector m=m_mean->get_mean_vector(m_features); Map eigen_m(m.vector, m.vlen); @@ -445,7 +444,7 @@ SGVector CVarDTCInferenceMethod::get_derivative_wrt_mean( return result; } -void CVarDTCInferenceMethod::register_minimizer(Minimizer* minimizer) +void VarDTCInferenceMethod::register_minimizer(std::shared_ptr minimizer) { io::warn("The method does not require a minimizer. The provided minimizer will not be used."); } diff --git a/src/shogun/machine/gp/VarDTCInferenceMethod.h b/src/shogun/machine/gp/VarDTCInferenceMethod.h index 1dc87b4a9f3..86292da1381 100644 --- a/src/shogun/machine/gp/VarDTCInferenceMethod.h +++ b/src/shogun/machine/gp/VarDTCInferenceMethod.h @@ -49,11 +49,11 @@ namespace shogun * method. * */ -class CVarDTCInferenceMethod: public CSingleSparseInference +class VarDTCInferenceMethod: public SingleSparseInference { public: /** default constructor */ - CVarDTCInferenceMethod(); + VarDTCInferenceMethod(); /** constructor * @@ -64,11 +64,11 @@ class CVarDTCInferenceMethod: public CSingleSparseInference * @param model likelihood model to use * @param inducing_features features to use */ - CVarDTCInferenceMethod(CKernel* kernel, CFeatures* features, - CMeanFunction* mean, CLabels* labels, CLikelihoodModel* model, - CFeatures* inducing_features); + VarDTCInferenceMethod(std::shared_ptr kernel, std::shared_ptr features, + std::shared_ptr mean, std::shared_ptr labels, std::shared_ptr model, + std::shared_ptr inducing_features); - virtual ~CVarDTCInferenceMethod(); + virtual ~VarDTCInferenceMethod(); /** returns the name of the inference method * @@ -87,7 +87,7 @@ class CVarDTCInferenceMethod: public CSingleSparseInference * @param inference inference method * @return casted CVarDTCInferenceMethod object */ - static CVarDTCInferenceMethod* obtain_from_generic(CInference* inference); + static std::shared_ptr obtain_from_generic(std::shared_ptr inference); /** get negative log marginal likelihood * @@ -167,7 +167,7 @@ class CVarDTCInferenceMethod: public CSingleSparseInference * * @param minimizer minimizer used in inference method */ - virtual void register_minimizer(Minimizer* minimizer); + virtual void register_minimizer(std::shared_ptr minimizer); protected: /** check if members of object are valid for inference */ diff --git a/src/shogun/machine/gp/VariationalGaussianLikelihood.cpp b/src/shogun/machine/gp/VariationalGaussianLikelihood.cpp index 105880e67cf..684fdc504dc 100644 --- a/src/shogun/machine/gp/VariationalGaussianLikelihood.cpp +++ b/src/shogun/machine/gp/VariationalGaussianLikelihood.cpp @@ -35,13 +35,13 @@ namespace shogun { -CVariationalGaussianLikelihood::CVariationalGaussianLikelihood() - : CVariationalLikelihood() +VariationalGaussianLikelihood::VariationalGaussianLikelihood() + : VariationalLikelihood() { init(); } -void CVariationalGaussianLikelihood::init() +void VariationalGaussianLikelihood::init() { SG_ADD(&m_mu, "mu", "The mean of variational normal distribution\n"); @@ -54,14 +54,14 @@ void CVariationalGaussianLikelihood::init() m_noise_factor=1e-6; } -void CVariationalGaussianLikelihood::set_noise_factor(float64_t noise_factor) +void VariationalGaussianLikelihood::set_noise_factor(float64_t noise_factor) { require(noise_factor>=0, "The noise_factor ({}) should be non negative", noise_factor); m_noise_factor=noise_factor; } -bool CVariationalGaussianLikelihood::set_variational_distribution(SGVector mu, - SGVector s2, const CLabels* lab) +bool VariationalGaussianLikelihood::set_variational_distribution(SGVector mu, + SGVector s2, std::shared_ptr lab) { require(lab, "Labels are required (lab should not be NULL)"); require((mu.vlen==s2.vlen) && (mu.vlen==lab->get_num_labels()), diff --git a/src/shogun/machine/gp/VariationalGaussianLikelihood.h b/src/shogun/machine/gp/VariationalGaussianLikelihood.h index cac64c7fe3e..dd04224fd77 100644 --- a/src/shogun/machine/gp/VariationalGaussianLikelihood.h +++ b/src/shogun/machine/gp/VariationalGaussianLikelihood.h @@ -46,13 +46,13 @@ namespace shogun * The variational distribution is Gaussian * */ -class CVariationalGaussianLikelihood : public CVariationalLikelihood +class VariationalGaussianLikelihood : public VariationalLikelihood { public: /** default constructor */ - CVariationalGaussianLikelihood(); + VariationalGaussianLikelihood(); - virtual ~CVariationalGaussianLikelihood() {}; + virtual ~VariationalGaussianLikelihood() {}; /** set the variational distribution given data and parameters * @@ -64,7 +64,7 @@ class CVariationalGaussianLikelihood : public CVariationalLikelihood * Note that the variational distribution is Gaussian */ virtual bool set_variational_distribution(SGVector mu, - SGVector s2, const CLabels* lab); + SGVector s2, std::shared_ptr lab); /** set a non-negative noise factor in order to correct the variance if variance is close to zero or negative * setting 0 means correction is not applied diff --git a/src/shogun/machine/gp/VariationalLikelihood.cpp b/src/shogun/machine/gp/VariationalLikelihood.cpp index 66d42fe7a28..c86d1b4883c 100644 --- a/src/shogun/machine/gp/VariationalLikelihood.cpp +++ b/src/shogun/machine/gp/VariationalLikelihood.cpp @@ -35,135 +35,135 @@ namespace shogun { -CVariationalLikelihood::CVariationalLikelihood() - : CLikelihoodModel() +VariationalLikelihood::VariationalLikelihood() + : LikelihoodModel() { init(); } -CVariationalLikelihood::~CVariationalLikelihood() +VariationalLikelihood::~VariationalLikelihood() { - SG_UNREF(m_likelihood); + } -void CVariationalLikelihood::set_likelihood(CLikelihoodModel * lik) +void VariationalLikelihood::set_likelihood(std::shared_ptr lik) { - SG_UNREF(m_likelihood); + m_likelihood=lik; - SG_REF(m_likelihood); + } -void CVariationalLikelihood::init() +void VariationalLikelihood::init() { //m_likelihood will be specified by its subclass //via the init_likelihood method m_likelihood = NULL; - SG_REF(m_likelihood); + SG_ADD(&m_lab, "labels", "The label of the data\n"); - SG_ADD((CSGObject**)&m_likelihood, "likelihood", + SG_ADD((std::shared_ptr*)&m_likelihood, "likelihood", "The distribution used to model the data\n"); } -SGVector CVariationalLikelihood::get_predictive_means( +SGVector VariationalLikelihood::get_predictive_means( SGVector mu, SGVector s2, - const CLabels* lab) const + std::shared_ptr lab) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_predictive_means(mu, s2, lab); } -SGVector CVariationalLikelihood::get_predictive_variances( +SGVector VariationalLikelihood::get_predictive_variances( SGVector mu, SGVector s2, - const CLabels* lab) const + std::shared_ptr lab) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_predictive_variances(mu, s2, lab); } -SGVector CVariationalLikelihood::get_first_derivative( - const CLabels* lab, SGVector func, +SGVector VariationalLikelihood::get_first_derivative( + std::shared_ptr lab, SGVector func, const TParameter* param) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_first_derivative(lab, func, param); } -SGVector CVariationalLikelihood::get_second_derivative( - const CLabels* lab, SGVector func, +SGVector VariationalLikelihood::get_second_derivative( + std::shared_ptr lab, SGVector func, const TParameter* param) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_second_derivative(lab, func, param); } -SGVector CVariationalLikelihood::get_third_derivative( - const CLabels* lab, SGVector func, +SGVector VariationalLikelihood::get_third_derivative( + std::shared_ptr lab, SGVector func, const TParameter* param) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_third_derivative(lab, func, param); } -ELikelihoodModelType CVariationalLikelihood::get_model_type() const +ELikelihoodModelType VariationalLikelihood::get_model_type() const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_model_type(); } -SGVector CVariationalLikelihood::get_log_probability_f( - const CLabels* lab, SGVector func) const +SGVector VariationalLikelihood::get_log_probability_f( + std::shared_ptr lab, SGVector func) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_log_probability_f(lab, func); } -SGVector CVariationalLikelihood::get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const +SGVector VariationalLikelihood::get_log_probability_derivative_f( + std::shared_ptr lab, SGVector func, index_t i) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_log_probability_derivative_f(lab, func, i); } -SGVector CVariationalLikelihood::get_log_zeroth_moments( +SGVector VariationalLikelihood::get_log_zeroth_moments( SGVector mu, SGVector s2, - const CLabels* lab) const + std::shared_ptr lab) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_log_zeroth_moments(mu, s2, lab); } -float64_t CVariationalLikelihood::get_first_moment( +float64_t VariationalLikelihood::get_first_moment( SGVector mu, SGVector s2, - const CLabels* lab, index_t i) const + std::shared_ptr lab, index_t i) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_first_moment(mu, s2, lab, i); } -float64_t CVariationalLikelihood::get_second_moment( +float64_t VariationalLikelihood::get_second_moment( SGVector mu, SGVector s2, - const CLabels* lab, index_t i) const + std::shared_ptr lab, index_t i) const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->get_second_moment(mu, s2, lab, i); } -bool CVariationalLikelihood::supports_regression() const +bool VariationalLikelihood::supports_regression() const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->supports_regression(); } -bool CVariationalLikelihood::supports_binary() const +bool VariationalLikelihood::supports_binary() const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->supports_binary(); } -bool CVariationalLikelihood::supports_multiclass() const +bool VariationalLikelihood::supports_multiclass() const { require(m_likelihood != NULL, "The likelihood should be initialized"); return m_likelihood->supports_multiclass(); diff --git a/src/shogun/machine/gp/VariationalLikelihood.h b/src/shogun/machine/gp/VariationalLikelihood.h index 2d363731b08..d2b0d0d1aa0 100644 --- a/src/shogun/machine/gp/VariationalLikelihood.h +++ b/src/shogun/machine/gp/VariationalLikelihood.h @@ -51,13 +51,13 @@ template class SGVector; * where \f$y\f$ are the labels, \f$f\f$ is the prediction function, * q is a variational distribution and p is a modeling distribution. */ -class CVariationalLikelihood : public CLikelihoodModel +class VariationalLikelihood : public LikelihoodModel { public: /** default constructor */ - CVariationalLikelihood(); + VariationalLikelihood(); - virtual ~CVariationalLikelihood(); + virtual ~VariationalLikelihood(); /** returns the expection of the logarithm of a given probability distribution * wrt the variational distribution. @@ -90,7 +90,7 @@ class CVariationalLikelihood : public CLikelihoodModel * @return final means evaluated by likelihood function */ virtual SGVector get_predictive_means(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** returns variance of the predictive marginal \f$p(y_*|X,y,x_*)\f$ * @@ -107,7 +107,7 @@ class CVariationalLikelihood : public CLikelihoodModel * @return final variances evaluated by likelihood function */ virtual SGVector get_predictive_variances(SGVector mu, - SGVector s2, const CLabels* lab=NULL) const; + SGVector s2, std::shared_ptr lab=NULL) const; /** get model type * @@ -126,7 +126,7 @@ class CVariationalLikelihood : public CLikelihoodModel * * @return logarithm of the point-wise likelihood */ - virtual SGVector get_log_probability_f(const CLabels* lab, + virtual SGVector get_log_probability_f(std::shared_ptr lab, SGVector func) const; /** get derivative of log likelihood \f$log(p(y|f))\f$ with respect to @@ -140,7 +140,7 @@ class CVariationalLikelihood : public CLikelihoodModel * @return derivative */ virtual SGVector get_log_probability_derivative_f( - const CLabels* lab, SGVector func, index_t i) const; + std::shared_ptr lab, SGVector func, index_t i) const; /** returns the zeroth moment of a given (unnormalized) probability * distribution: @@ -159,7 +159,7 @@ class CVariationalLikelihood : public CLikelihoodModel * @return log zeroth moment \f$log(Z_i)\f$ */ virtual SGVector get_log_zeroth_moments(SGVector mu, - SGVector s2, const CLabels* lab) const; + SGVector s2, std::shared_ptr lab) const; /** returns the first moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -176,7 +176,7 @@ class CVariationalLikelihood : public CLikelihoodModel * @return first moment of \f$q(f_i)\f$ */ virtual float64_t get_first_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** returns the second moment of a given (unnormalized) probability * distribution \f$q(f_i) = Z_i^-1 @@ -193,7 +193,7 @@ class CVariationalLikelihood : public CLikelihoodModel * @return the second moment of \f$q(f_i)\f$ */ virtual float64_t get_second_moment(SGVector mu, - SGVector s2, const CLabels* lab, index_t i) const; + SGVector s2, std::shared_ptr lab, index_t i) const; /** return whether likelihood function supports regression * @@ -222,7 +222,7 @@ class CVariationalLikelihood : public CLikelihoodModel * * @return derivative */ - virtual SGVector get_first_derivative(const CLabels* lab, + virtual SGVector get_first_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** get derivative of the first derivative of log likelihood with respect to @@ -235,7 +235,7 @@ class CVariationalLikelihood : public CLikelihoodModel * * @return derivative */ - virtual SGVector get_second_derivative(const CLabels* lab, + virtual SGVector get_second_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** get derivative of the second derivative of log likelihood with respect @@ -248,7 +248,7 @@ class CVariationalLikelihood : public CLikelihoodModel * * @return derivative */ - virtual SGVector get_third_derivative(const CLabels* lab, + virtual SGVector get_third_derivative(std::shared_ptr lab, SGVector func, const TParameter* param) const; /** return whether likelihood function supports @@ -277,10 +277,10 @@ class CVariationalLikelihood : public CLikelihoodModel SGVector m_lab; /** the distribution used to model data */ - CLikelihoodModel* m_likelihood; + std::shared_ptr m_likelihood; /** this method used to set m_likelihood*/ - virtual void set_likelihood(CLikelihoodModel * lik); + virtual void set_likelihood(std::shared_ptr lik); private: void init(); }; diff --git a/src/shogun/machine/gp/ZeroMean.cpp b/src/shogun/machine/gp/ZeroMean.cpp index ee6a4841408..ed675063b77 100644 --- a/src/shogun/machine/gp/ZeroMean.cpp +++ b/src/shogun/machine/gp/ZeroMean.cpp @@ -33,15 +33,15 @@ using namespace shogun; -CZeroMean::CZeroMean() : CMeanFunction() +ZeroMean::ZeroMean() : MeanFunction() { } -CZeroMean::~CZeroMean() +ZeroMean::~ZeroMean() { } -SGVector CZeroMean::get_mean_vector(const CFeatures* features) const +SGVector ZeroMean::get_mean_vector(std::shared_ptr features) const { SGVector result(features->get_num_vectors()); result.zero(); diff --git a/src/shogun/machine/gp/ZeroMean.h b/src/shogun/machine/gp/ZeroMean.h index 98be2ff0843..0b4ddfaecc5 100644 --- a/src/shogun/machine/gp/ZeroMean.h +++ b/src/shogun/machine/gp/ZeroMean.h @@ -43,13 +43,13 @@ namespace shogun * * Simple mean function that assumes a mean of zero. */ -class CZeroMean : public CMeanFunction +class ZeroMean : public MeanFunction { public: /** constructor */ - CZeroMean(); + ZeroMean(); - virtual ~CZeroMean(); + virtual ~ZeroMean(); /** returns name of the mean function * @@ -64,7 +64,7 @@ class CZeroMean : public CMeanFunction * * @return mean of feature vectors */ - virtual SGVector get_mean_vector(const CFeatures* features) const; + virtual SGVector get_mean_vector(std::shared_ptr features) const; }; } #endif /* CZEROMEAN_H_ */ diff --git a/src/shogun/mathematics/Cplex.cpp b/src/shogun/mathematics/Cplex.cpp index 701ab9bf9fb..473df3db563 100644 --- a/src/shogun/mathematics/Cplex.cpp +++ b/src/shogun/mathematics/Cplex.cpp @@ -18,7 +18,7 @@ using namespace shogun; CCplex::CCplex() -: CSGObject(), env(NULL), lp(NULL), lp_initialized(false) +: SGObject(), env(NULL), lp(NULL), lp_initialized(false) { } @@ -82,7 +82,7 @@ bool CCplex::init(E_PROB_TYPE typ, int32_t timeout) } bool CCplex::setup_subgradientlpm_QP( - float64_t C, CBinaryLabels* labels, CSparseFeatures* features, + float64_t C, BinaryLabels* labels, SparseFeatures* features, int32_t* idx_bound, int32_t num_bound, int32_t* w_zero, int32_t num_zero, float64_t* vee, int32_t num_dim, bool use_bias) { @@ -288,7 +288,7 @@ bool CCplex::setup_lpboost(float64_t C, int32_t num_cols) bool CCplex::add_lpboost_constraint( float64_t factor, SGSparseVectorEntry* h, int32_t len, int32_t ulen, - CBinaryLabels* label) + BinaryLabels* label) { int amatbeg[1]; /* for calling external lib */ int amatind[len+1]; /* for calling external lib */ @@ -317,7 +317,7 @@ bool CCplex::add_lpboost_constraint( } bool CCplex::setup_lpm( - float64_t C, CSparseFeatures* x, CBinaryLabels* y, bool use_bias) + float64_t C, SparseFeatures* x, BinaryLabels* y, bool use_bias) { ASSERT(x) ASSERT(y) diff --git a/src/shogun/mathematics/Cplex.h b/src/shogun/mathematics/Cplex.h index 20981222f4c..6ccddb16e33 100644 --- a/src/shogun/mathematics/Cplex.h +++ b/src/shogun/mathematics/Cplex.h @@ -35,7 +35,7 @@ enum E_PROB_TYPE * a number of optimization problems that are used in shogun, like for Multiple * Kernel Learning, Linear Programming Machines and Linear Programming Boosting. */ -class CCplex : public CSGObject +class CCplex : public SGObject { public: @@ -49,14 +49,14 @@ class CCplex : public CSGObject // A = [ E Z_w Z_x ] dim(A)=(num_dim+1, num_dim+1 + num_zero + num_bound) // (+1 for bias!) bool setup_subgradientlpm_QP( - float64_t C, CBinaryLabels* labels, CSparseFeatures* features, + float64_t C, BinaryLabels* labels, SparseFeatures* features, int32_t* idx_bound, int32_t num_bound, int32_t* w_zero, int32_t num_zero, float64_t* vee, int32_t num_dim, bool use_bias); bool setup_lpboost(float64_t C, int32_t num_cols); bool add_lpboost_constraint( float64_t factor, SGSparseVectorEntry* h, int32_t len, - int32_t ulen, CBinaryLabels* label); + int32_t ulen, BinaryLabels* label); // given N sparse inputs x_i, and corresponding labels y_i i=0...N-1 // create the following 1-norm SVM problem & transfer to cplex @@ -95,7 +95,7 @@ class CCplex : public CSGObject // // b = -1 -1 -1 -1 ... bool setup_lpm( - float64_t C, CSparseFeatures* x, CBinaryLabels* y, bool use_bias); + float64_t C, SparseFeatures* x, BinaryLabels* y, bool use_bias); // call this to setup linear part // diff --git a/src/shogun/mathematics/Function.h b/src/shogun/mathematics/Function.h index 48a5c88d3ad..42c5f19518b 100644 --- a/src/shogun/mathematics/Function.h +++ b/src/shogun/mathematics/Function.h @@ -16,7 +16,7 @@ namespace shogun /** @brief Class of a function of one variable */ -class CFunction : public CSGObject +class Function : public SGObject { public: /** returns the real value of the function at given point diff --git a/src/shogun/mathematics/Math.cpp b/src/shogun/mathematics/Math.cpp index eec68e5c0cf..bbe1cc295f4 100644 --- a/src/shogun/mathematics/Math.cpp +++ b/src/shogun/mathematics/Math.cpp @@ -28,37 +28,37 @@ using namespace shogun; #define MAX_LOG_TABLE_SIZE 123*1024*1024 #define LOG_TABLE_PRECISION 1e-15 #endif //USE_HMMDEBUG -int32_t CMath::LOGACCURACY = 0; // 100000 steps per integer +int32_t Math::LOGACCURACY = 0; // 100000 steps per integer #endif // USE_LOGCACHE -int32_t CMath::LOGRANGE = 0; // range for logtable: log(1+exp(x)) -25 <= x <= 0 - -const float64_t CMath::NOT_A_NUMBER = NAN; -const float64_t CMath::INFTY = INFINITY; // infinity -const float64_t CMath::ALMOST_INFTY = +1e+300; //a large number -const float64_t CMath::ALMOST_NEG_INFTY = -1e+300; -const float64_t CMath::PI=M_PI; -const float64_t CMath::MACHINE_EPSILON=DBL_EPSILON; -const float64_t CMath::MAX_REAL_NUMBER=DBL_MAX; -const float64_t CMath::MIN_REAL_NUMBER=DBL_MIN; -const float32_t CMath::F_MAX_VAL32=FLT_MAX; -const float32_t CMath::F_MIN_NORM_VAL32=FLT_MIN; -const float64_t CMath::F_MAX_VAL64=DBL_MAX; -const float64_t CMath::F_MIN_NORM_VAL64=DBL_MIN; -const float32_t CMath::F_MIN_VAL32=(FLT_MIN * FLT_EPSILON); -const float64_t CMath::F_MIN_VAL64=(DBL_MIN * DBL_EPSILON); +int32_t Math::LOGRANGE = 0; // range for logtable: log(1+exp(x)) -25 <= x <= 0 + +const float64_t Math::NOT_A_NUMBER = NAN; +const float64_t Math::INFTY = INFINITY; // infinity +const float64_t Math::ALMOST_INFTY = +1e+300; //a large number +const float64_t Math::ALMOST_NEG_INFTY = -1e+300; +const float64_t Math::PI=M_PI; +const float64_t Math::MACHINE_EPSILON=DBL_EPSILON; +const float64_t Math::MAX_REAL_NUMBER=DBL_MAX; +const float64_t Math::MIN_REAL_NUMBER=DBL_MIN; +const float32_t Math::F_MAX_VAL32=FLT_MAX; +const float32_t Math::F_MIN_NORM_VAL32=FLT_MIN; +const float64_t Math::F_MAX_VAL64=DBL_MAX; +const float64_t Math::F_MIN_NORM_VAL64=DBL_MIN; +const float32_t Math::F_MIN_VAL32=(FLT_MIN * FLT_EPSILON); +const float64_t Math::F_MIN_VAL64=(DBL_MIN * DBL_EPSILON); #ifdef USE_LOGCACHE -float64_t* CMath::logtable = NULL; +float64_t* Math::logtable = NULL; #endif -CMath::CMath() -: CSGObject() +Math::Math() +: SGObject() { #ifdef USE_LOGCACHE - LOGRANGE=CMath::determine_logrange(); - LOGACCURACY=CMath::determine_logaccuracy(LOGRANGE); - CMath::logtable=SG_MALLOC(float64_t, LOGRANGE*LOGACCURACY); + LOGRANGE=Math::determine_logrange(); + LOGACCURACY=Math::determine_logaccuracy(LOGRANGE); + Math::logtable=SG_MALLOC(float64_t, LOGRANGE*LOGACCURACY); init_log_table(); #else int32_t i=0; @@ -69,16 +69,16 @@ CMath::CMath() #endif } -CMath::~CMath() +Math::~Math() { #ifdef USE_LOGCACHE - SG_FREE(CMath::logtable); - CMath::logtable=NULL; + SG_FREE(Math::logtable); + Math::logtable=NULL; #endif } #ifdef USE_LOGCACHE -int32_t CMath::determine_logrange() +int32_t Math::determine_logrange() { int32_t i; float64_t acc=0; @@ -96,7 +96,7 @@ int32_t CMath::determine_logrange() return i; } -int32_t CMath::determine_logaccuracy(int32_t range) +int32_t Math::determine_logaccuracy(int32_t range) { range=MAX_LOG_TABLE_SIZE/range/((int)sizeof(float64_t)); io::info( @@ -107,7 +107,7 @@ int32_t CMath::determine_logaccuracy(int32_t range) } //init log table of form log(1+exp(x)) -void CMath::init_log_table() +void Math::init_log_table() { for (int32_t i=0; i< LOGACCURACY*LOGRANGE; i++) logtable[i] = @@ -115,7 +115,7 @@ void CMath::init_log_table() } #endif -void CMath::sort(int32_t *a, int32_t cols, int32_t sort_col) +void Math::sort(int32_t *a, int32_t cols, int32_t sort_col) { int32_t changed=1; if (a[0]==-1) return; @@ -127,7 +127,7 @@ void CMath::sort(int32_t *a, int32_t cols, int32_t sort_col) if (a[i*cols+sort_col]>a[(i+1)*cols+sort_col]) { for (int32_t j=0; j 0 && rel_tolerance < 1.0, "Relative tolerance ({}) should be less than 1.0 and positive", rel_tolerance); diff --git a/src/shogun/mathematics/Math.h b/src/shogun/mathematics/Math.h index 231668ea009..b8f717d75e6 100644 --- a/src/shogun/mathematics/Math.h +++ b/src/shogun/mathematics/Math.h @@ -87,7 +87,7 @@ template struct thread_qsort #define COMPLEX128_ERROR_ONEARG(function) \ static inline complex128_t function(complex128_t a) \ { \ - error("CMath::{}():: Not supported for complex128_t",\ + error("Math::{}():: Not supported for complex128_t",\ #function);\ return complex128_t(0.0, 0.0); \ } @@ -102,17 +102,17 @@ namespace shogun { /** @brief Class which collects generic mathematical functions */ -class CMath : public CSGObject +class Math : public SGObject { public: /**@name Constructor/Destructor. */ //@{ ///Constructor - initializes log-table - CMath(); + Math(); ///Destructor - frees logtable - virtual ~CMath(); + virtual ~Math(); //@} #ifndef SWIG // SWIG should skip this part @@ -282,7 +282,7 @@ class CMath : public CSGObject static inline bool fequals_abs(const T& a, const T& b, const float64_t eps) { - const T diff = CMath::abs((a-b)); + const T diff = Math::abs((a-b)); return (diff < eps); } @@ -304,22 +304,22 @@ class CMath : public CSGObject float64_t eps = std::max( eps_,env()->fequals_epsilon()); - const T absA = CMath::abs(a); - const T absB = CMath::abs(b); - const T diff = CMath::abs((a - b)); + const T absA = Math::abs(a); + const T absB = Math::abs(b); + const T diff = Math::abs((a - b)); // Handle this separately since NAN is unordered - if (CMath::is_nan((float64_t)a) && CMath::is_nan((float64_t)b)) + if (Math::is_nan((float64_t)a) && Math::is_nan((float64_t)b)) return true; // Required for JSON Serialization Tests if (env()->fequals_tolerant()) - return CMath::fequals_abs(a, b, eps); + return Math::fequals_abs(a, b, eps); // handles float32_t and float64_t separately T comp = (std::is_same::value) - ? CMath::F_MIN_NORM_VAL32 - : CMath::F_MIN_NORM_VAL64; + ? Math::F_MIN_NORM_VAL32 + : Math::F_MIN_NORM_VAL64; if (a == b) return true; @@ -826,7 +826,7 @@ class CMath : public CSGObject if (size==2) { if (output[0] > output [1]) - CMath::swap(output[0],output[1]); + Math::swap(output[0],output[1]); return; } //T split=output[random(0,size-1)]; @@ -844,7 +844,7 @@ class CMath : public CSGObject if (left<=right) { - CMath::swap(output[left],output[right]); + Math::swap(output[left],output[right]); left++; right--; } @@ -905,7 +905,7 @@ class CMath : public CSGObject /// byte not implemented for complex128_t static inline uint8_t byte(complex128_t word, uint16_t p) { - error("CMath::byte():: Not supported for complex128_t"); + error("Math::byte():: Not supported for complex128_t"); return uint8_t(0); } @@ -1016,7 +1016,7 @@ class CMath : public CSGObject /// radix_sort_helper not implemented for complex128_t static void radix_sort_helper(complex128_t* array, int32_t size, uint16_t i) { - error("CMath::radix_sort_helper():: Not supported for complex128_t"); + error("Math::radix_sort_helper():: Not supported for complex128_t"); } #ifndef SWIG // SWIG should skip this part @@ -1144,7 +1144,7 @@ class CMath : public CSGObject static void display_bits(complex128_t word, int32_t width=8*sizeof(complex128_t)) { - error("CMath::display_bits():: Not supported for complex128_t"); + error("Math::display_bits():: Not supported for complex128_t"); } /** Performs a quicksort on an array output of length size @@ -1162,7 +1162,7 @@ class CMath : public CSGObject template static void qsort_index(complex128_t* output, T* index, uint32_t size) { - error("CMath::qsort_index():: Not supported for complex128_t"); + error("Math::qsort_index():: Not supported for complex128_t"); } /** Performs a quicksort on an array output of length size @@ -1182,7 +1182,7 @@ class CMath : public CSGObject static void qsort_backword_index( complex128_t* output, T* index, uint32_t size) { - error("CMath::qsort_backword_index():: \ + error("Math::qsort_backword_index():: \ Not supported for complex128_t"); } @@ -1216,7 +1216,7 @@ class CMath : public CSGObject inline static void parallel_qsort_index(complex128_t* output, T* index, uint32_t size, int32_t n_threads, int32_t limit=0) { - error("CMath::parallel_qsort_index():: Not supported for complex128_t"); + error("Math::parallel_qsort_index():: Not supported for complex128_t"); } /// helper function for parallel_qsort_index. @@ -1235,7 +1235,7 @@ class CMath : public CSGObject /// complex128_t cannot be used as index static void min(float64_t* output, complex128_t* index, int32_t size) { - error("CMath::min():: Not supported for complex128_t"); + error("Math::min():: Not supported for complex128_t"); } /** Finds the n smallest elements in output and puts these elements as the @@ -1249,7 +1249,7 @@ class CMath : public CSGObject static void nmin(float64_t* output, complex128_t* index, int32_t size, int32_t n) { - error("CMath::nmin():: Not supported for complex128_t"); + error("Math::nmin():: Not supported for complex128_t"); } @@ -1284,7 +1284,7 @@ class CMath : public CSGObject /// binary_search_helper not implemented for complex128_t static int32_t binary_search_helper(complex128_t* output, int32_t size, complex128_t elem) { - error("CMath::binary_search_helper():: Not supported for complex128_t"); + error("Math::binary_search_helper():: Not supported for complex128_t"); return int32_t(0); } @@ -1306,7 +1306,7 @@ class CMath : public CSGObject /// binary_search not implemented for complex128_t static inline int32_t binary_search(complex128_t* output, int32_t size, complex128_t elem) { - error("CMath::binary_search():: Not supported for complex128_t"); + error("Math::binary_search():: Not supported for complex128_t"); return int32_t(-1); } @@ -1352,7 +1352,7 @@ class CMath : public CSGObject /// binary_search not implemented for complex128_t static inline int32_t binary_search(complex128_t** vector, index_t length, complex128_t* elem) { - error("CMath::binary_search():: Not supported for complex128_t"); + error("Math::binary_search():: Not supported for complex128_t"); return int32_t(-1); } @@ -1379,7 +1379,7 @@ class CMath : public CSGObject static int32_t binary_search_max_lower_equal(complex128_t* output, int32_t size, complex128_t elem) { - error("CMath::binary_search_max_lower_equal():: \ + error("Math::binary_search_max_lower_equal():: \ Not supported for complex128_t"); return int32_t(-1); } @@ -1419,14 +1419,14 @@ class CMath : public CSGObject /// returns range of logtable inline static uint32_t get_log_range() { - return CMath::LOGRANGE; + return Math::LOGRANGE; } #ifdef USE_LOGCACHE /// returns range of logtable inline static uint32_t get_log_accuracy() { - return CMath::LOGACCURACY; + return Math::LOGACCURACY; } #endif @@ -1450,10 +1450,10 @@ class CMath : public CSGObject { float64_t diff; - if (!CMath::is_finite(p)) + if (!Math::is_finite(p)) return q; - if (!CMath::is_finite(q)) + if (!Math::is_finite(q)) { io::warn("INVALID second operand to logsum({},{}) expect undefined results", p, q); return NOT_A_NUMBER; @@ -1478,9 +1478,9 @@ class CMath : public CSGObject { float64_t diff; - if (!CMath::is_finite(p)) + if (!Math::is_finite(p)) return q; - if (!CMath::is_finite(q)) + if (!Math::is_finite(q)) return p; diff = p - q; if (diff > 0) @@ -1567,7 +1567,7 @@ class CMath : public CSGObject //implementations of template functions template -void* CMath::parallel_qsort_index(void* p) +void* Math::parallel_qsort_index(void* p) { struct thread_qsort* ps=(thread_qsort*) p; T1* output=ps->output; @@ -1676,7 +1676,7 @@ void* CMath::parallel_qsort_index(void* p) } template -void CMath::qsort_index(T1* output, T2* index, uint32_t size) +void Math::qsort_index(T1* output, T2* index, uint32_t size) { if (size<=1) return; @@ -1720,7 +1720,7 @@ void CMath::qsort_index(T1* output, T2* index, uint32_t size) } template -void CMath::qsort_backward_index(T1* output, T2* index, int32_t size) +void Math::qsort_backward_index(T1* output, T2* index, int32_t size) { if (size<=1) return; @@ -1765,7 +1765,7 @@ void CMath::qsort_backward_index(T1* output, T2* index, int32_t size) } template -void CMath::nmin(float64_t* output, T* index, int32_t size, int32_t n) +void Math::nmin(float64_t* output, T* index, int32_t size, int32_t n) { if (6 * n * size < 13 * size * std::log(size)) for (int32_t i=0; i -void CMath::min(float64_t* output, T* index, int32_t size) +void Math::min(float64_t* output, T* index, int32_t size) { if (size<=1) return; @@ -1796,9 +1796,9 @@ void CMath::min(float64_t* output, T* index, int32_t size) #define COMPLEX128_ERROR_ONEARG_T(function) \ template <> \ -inline complex128_t CMath::function(complex128_t a) \ +inline complex128_t Math::function(complex128_t a) \ { \ - error("CMath::{}():: Not supported for complex128_t",\ + error("Math::{}():: Not supported for complex128_t",\ #function);\ return complex128_t(0.0, 0.0); \ } diff --git a/src/shogun/mathematics/Mosek.cpp b/src/shogun/mathematics/Mosek.cpp index c97fb6f5d5e..b93114a7bff 100644 --- a/src/shogun/mathematics/Mosek.cpp +++ b/src/shogun/mathematics/Mosek.cpp @@ -17,12 +17,12 @@ using namespace shogun; CMosek::CMosek() -: CSGObject() +: SGObject() { } CMosek::CMosek(int32_t num_con, int32_t num_var) -: CSGObject() +: SGObject() { // Create MOSEK environment #if (MSK_VERSION_MAJOR == 6) @@ -204,7 +204,7 @@ MSKrescodee CMosek::add_constraint_sosvm( float64_t bi) { // Count the number of non-zero element in dPsi - int32_t nnz = CMath::get_num_nonzero(dPsi.vector, dPsi.vlen); + int32_t nnz = Math::get_num_nonzero(dPsi.vector, dPsi.vlen); // Indices of the non-zero elements in the row of A to add SGVector< index_t > asub(nnz+1); // +1 because of the -1 element // Values of the non-zero elements @@ -255,7 +255,7 @@ MSKrescodee CMosek::wrapper_putaveclist( sub[i] = i; // Non-zero elements of A - int32_t nnza = CMath::get_num_nonzero(A.matrix, A.num_rows*A.num_cols); + int32_t nnza = Math::get_num_nonzero(A.matrix, A.num_rows*A.num_cols); SGVector< float64_t > aval(nnza); // For each of the rows, indices to non-zero elements SGVector< index_t > asub(nnza); diff --git a/src/shogun/mathematics/Mosek.h b/src/shogun/mathematics/Mosek.h index f423257982a..eaf094847da 100644 --- a/src/shogun/mathematics/Mosek.h +++ b/src/shogun/mathematics/Mosek.h @@ -26,7 +26,7 @@ namespace shogun * This class provides methods to set up optimization problems that are * used in shogun, e.g. from PrimalMosekSOSVM. */ -class CMosek : public CSGObject +class CMosek : public SGObject { public: diff --git a/src/shogun/mathematics/RandomMixin.h b/src/shogun/mathematics/RandomMixin.h index cbb228031d9..a9319ca3237 100644 --- a/src/shogun/mathematics/RandomMixin.h +++ b/src/shogun/mathematics/RandomMixin.h @@ -17,17 +17,27 @@ namespace shogun */ template < typename T, typename PRNG, - std::enable_if_t::type>::value>* = + std::enable_if_t>>* = nullptr> static inline void seed(T* object, PRNG&& prng) { - if (object->has("seed")) - object->put("seed", (int32_t)prng()); + if (object->has(kSeed)) + object->put(kSeed, (int32_t)prng()); + } + + template < + typename T, typename PRNG, + std::enable_if_t>>* = + nullptr> + static inline void seed(std::shared_ptr object, PRNG&& prng) + { + seed(object.get(), prng); } } // namespace random - static inline void random_seed_callback(CSGObject*); + static inline void random_seed_callback(SGObject*); template class RandomMixin : public Seedable @@ -44,9 +54,9 @@ namespace shogun init(); } - virtual CSGObject* clone(ParameterProperties pp = ParameterProperties::ALL) const override + virtual std::shared_ptr clone(ParameterProperties pp = ParameterProperties::ALL) const override { - auto clone = dynamic_cast(Parent::clone(pp)); + auto clone = std::dynamic_pointer_cast(Parent::clone()); clone->m_prng = m_prng; return clone; } @@ -74,12 +84,12 @@ namespace shogun { init_random_seed(); - Parent::add_callback_function(kSeed, [&]() { + Parent::add_callback_function(random::kSeed, [&]() { m_prng = PRNG(Seedable::m_seed); }); Parent::watch_method( - kSetRandomSeed, &this_t::set_random_seed); + random::kSetRandomSeed, &this_t::set_random_seed); } protected: @@ -88,29 +98,23 @@ namespace shogun */ template < typename T, - std::enable_if_t::type>::value>* = + std::enable_if_t::type>>* = nullptr> - inline void seed(T* object) const + inline void seed(std::shared_ptr object) const { random::seed(object, m_prng); } mutable PRNG m_prng; - -#ifndef SWIG - public: - static constexpr std::string_view kSetRandomSeed = "set_random_seed"; - static constexpr std::string_view kSeed = "seed"; -#endif // SWIG }; - static inline void random_seed_callback(CSGObject* obj) + static inline void random_seed_callback(SGObject* obj) { - obj->for_each_param_of_type( - [&](const std::string& name, CSGObject** param) { - if ((*param)->has("seed")) - (*param)->run("set_random_seed"); + obj->for_each_param_of_type( + [&](const std::string& name, SGObject** param) { + if ((*param)->has(random::kSeed)) + (*param)->run(random::kSetRandomSeed); else random_seed_callback(*param); }); diff --git a/src/shogun/mathematics/Seedable.h b/src/shogun/mathematics/Seedable.h index 9f74d60b8b8..15b5c5c0aa1 100644 --- a/src/shogun/mathematics/Seedable.h +++ b/src/shogun/mathematics/Seedable.h @@ -10,21 +10,25 @@ namespace shogun { namespace random { +#ifndef SWIG + static constexpr std::string_view kSetRandomSeed = "set_random_seed"; + static constexpr std::string_view kSeed = "seed"; +#endif // SWIG /** Seeds an SGObject using a specific seed */ template < typename T, typename PRNG, std::enable_if_t::type>::value>* = + SGObject, typename std::remove_pointer::type>::value>* = nullptr> static inline void seed(T* object, int32_t seed) { - if (object->has("seed")) - object->put("seed", seed); + if (object->has(kSeed)) + object->put(kSeed, seed); } } // namespace random - static inline void seed_callback(CSGObject*, int32_t); + static inline void seed_callback(SGObject*, int32_t); template class Seedable : public Parent @@ -44,9 +48,9 @@ namespace shogun private: void init() { - Parent::watch_param("seed", &m_seed); + Parent::watch_param(random::kSeed, &m_seed); Parent::add_callback_function( - "seed", std::bind(seed_callback, this, std::ref(m_seed))); + random::kSeed, std::bind(seed_callback, this, std::ref(m_seed))); } protected: @@ -57,7 +61,7 @@ namespace shogun template < typename T, std::enable_if_t::type>::value>* = + SGObject, typename std::remove_pointer::type>::value>* = nullptr> inline void seed(T* object) const { @@ -67,12 +71,12 @@ namespace shogun int32_t m_seed; }; - static inline void seed_callback(CSGObject* obj, int32_t seed) + static inline void seed_callback(SGObject* obj, int32_t seed) { - obj->for_each_param_of_type( - [&](const std::string& name, CSGObject** param) { - if ((*param)->has("seed")) - (*param)->put("seed", seed); + obj->for_each_param_of_type( + [&](const std::string& name, SGObject** param) { + if ((*param)->has(random::kSeed)) + (*param)->put(random::kSeed, seed); else seed_callback(*param, seed); }); diff --git a/src/shogun/mathematics/Statistics.cpp b/src/shogun/mathematics/Statistics.cpp index 9d1092d6d87..cf2eefdec41 100644 --- a/src/shogun/mathematics/Statistics.cpp +++ b/src/shogun/mathematics/Statistics.cpp @@ -30,21 +30,21 @@ using namespace shogun; #define M_SQRT1_2 0.707106781186547524401 #endif -float64_t CStatistics::variance(SGVector values) +float64_t Statistics::variance(SGVector values) { require(values.vlen>1, "Number of observations ({}) needs to be at least 1.", values.vlen); - float64_t mean=CStatistics::mean(values); + float64_t mean=Statistics::mean(values); float64_t sum_squared_diff=0; for (index_t i=0; i CStatistics::matrix_mean(SGMatrix values, +SGVector Statistics::matrix_mean(SGMatrix values, bool col_wise) { ASSERT(values.num_rows>0) @@ -81,7 +81,7 @@ SGVector CStatistics::matrix_mean(SGMatrix values, return result; } -SGVector CStatistics::matrix_variance(SGMatrix values, +SGVector Statistics::matrix_variance(SGMatrix values, bool col_wise) { ASSERT(values.num_rows>0) @@ -89,7 +89,7 @@ SGVector CStatistics::matrix_variance(SGMatrix values, ASSERT(values.matrix) /* first compute mean */ - SGVector mean=CStatistics::matrix_mean(values, col_wise); + SGVector mean=Statistics::matrix_mean(values, col_wise); SGVector result; @@ -100,7 +100,7 @@ SGVector CStatistics::matrix_variance(SGMatrix values, { result[j]=0; for (index_t i=0; i CStatistics::matrix_variance(SGMatrix values, { result[j]=0; for (index_t i=0; i CStatistics::matrix_variance(SGMatrix values, return result; } -float64_t CStatistics::std_deviation(SGVector values) +float64_t Statistics::std_deviation(SGVector values) { return std::sqrt(variance(values)); } -SGVector CStatistics::matrix_std_deviation( +SGVector Statistics::matrix_std_deviation( SGMatrix values, bool col_wise) { - SGVector var=CStatistics::matrix_variance(values, col_wise); + SGVector var=Statistics::matrix_variance(values, col_wise); for (index_t i=0; i CStatistics::covariance_matrix( +SGMatrix Statistics::covariance_matrix( SGMatrix observations, bool in_place) { int32_t D = observations.num_rows; @@ -169,7 +169,7 @@ SGMatrix CStatistics::covariance_matrix( return cov; } -SGVector CStatistics::fishers_exact_test_for_multiple_2x3_tables( +SGVector Statistics::fishers_exact_test_for_multiple_2x3_tables( SGMatrix tables) { SGMatrix table(NULL, 2, 3, false); @@ -184,7 +184,7 @@ SGVector CStatistics::fishers_exact_test_for_multiple_2x3_tables( return v; } -float64_t CStatistics::fishers_exact_test_for_2x3_table( +float64_t Statistics::fishers_exact_test_for_2x3_table( SGMatrix table) { ASSERT(table.num_rows==2) @@ -199,7 +199,7 @@ float64_t CStatistics::fishers_exact_test_for_2x3_table( m[4]=table.matrix[4]+table.matrix[5]; float64_t n=SGVector::sum(m, m_len)/2.0; - int32_t x_len=2*3*CMath::sq(CMath::max(m, m_len)); + int32_t x_len=2*3*Math::sq(Math::max(m, m_len)); float64_t* x=SG_MALLOC(float64_t, x_len); SGVector::fill_vector(x, x_len, 0.0); @@ -219,14 +219,14 @@ float64_t CStatistics::fishers_exact_test_for_2x3_table( floatmax_t prob_table_log=log_nom-log_denom; - int32_t dim1=CMath::min(m[0], m[2]); + int32_t dim1=Math::min(m[0], m[2]); //traverse all possible tables with given m int32_t counter=0; for (int32_t k=0; k<=dim1; k++) { - for (int32_t l=CMath::max(0.0, m[0]-m[4]-k); - l<=CMath::min(m[0]-k, m[3]); l++) + for (int32_t l=Math::max(0.0, m[0]-m[4]-k); + l<=Math::min(m[0]-k, m[3]); l++) { x[0+0*2+counter*2*3]=k; x[0+1*2+counter*2*3]=l; @@ -243,7 +243,7 @@ float64_t CStatistics::fishers_exact_test_for_2x3_table( #ifdef DEBUG_FISHER_TABLE io::print("counter={}\n", counter); io::print("dim1={}\n", dim1); - io::print("l={:g}...{:g}\n", CMath::max(0.0,m[0]-m[4]-0), CMath::min(m[0]-0, m[3])); + io::print("l={:g}...{:g}\n", Math::max(0.0,m[0]-m[4]-0), Math::min(m[0]-0, m[3])); io::print("n={:g}\n", n); io::print("prob_table_log=%.18Lg\n", prob_table_log); io::print("log_denomf={:.18g}\n", log_denomf); @@ -272,11 +272,11 @@ float64_t CStatistics::fishers_exact_test_for_2x3_table( display_vector(log_denom_vec, counter, "log_denom_vec"); #endif // DEBUG_FISHER_TABLE - float64_t nonrand_p=-CMath::INFTY; + float64_t nonrand_p=-Math::INFTY; for (int32_t i=0; i -SGVector CStatistics::sample_indices(int32_t sample_size, int32_t N, PRNG& prng) +SGVector Statistics::sample_indices(int32_t sample_size, int32_t N, PRNG& prng) { require(sample_size CStatistics::sample_indices(int32_t sample_size, int32_t N, PR SG_FREE(idxs); SGVector result=SGVector(permuted_idxs, sample_size); - CMath::qsort(result); + Math::qsort(result); return result; } -template SGVector CStatistics::sample_indices(int32_t sample_size, int32_t N, std::mt19937_64& prng); +template SGVector Statistics::sample_indices(int32_t sample_size, int32_t N, std::mt19937_64& prng); -float64_t CStatistics::inverse_normal_cdf(float64_t p, float64_t mean, +float64_t Statistics::inverse_normal_cdf(float64_t p, float64_t mean, float64_t std_deviation) { require(p>=0, "p ({}); must be greater or equal to 0.", p); @@ -379,7 +379,7 @@ float64_t CStatistics::inverse_normal_cdf(float64_t p, float64_t mean, // double *sd, int *status, double *bound ) } -float64_t CStatistics::chi2_cdf(float64_t x, float64_t k) +float64_t Statistics::chi2_cdf(float64_t x, float64_t k) { require(x>=0, "x ({}) has to be greater or equal to 0.", x); require(k>0, "Degrees of freedom ({}) has to be positive.", k); @@ -400,7 +400,7 @@ float64_t CStatistics::chi2_cdf(float64_t x, float64_t k) return output_p; } -float64_t CStatistics::gamma_cdf(float64_t x, float64_t a, float64_t b) +float64_t Statistics::gamma_cdf(float64_t x, float64_t a, float64_t b) { require(x>=0, "x ({}) has to be greater or equal to 0.", x); require(a>=0, "a ({}) has to be greater or equal to 0.", a); @@ -423,7 +423,7 @@ float64_t CStatistics::gamma_cdf(float64_t x, float64_t a, float64_t b) return output_p; } -float64_t CStatistics::lnormal_cdf(float64_t x) +float64_t Statistics::lnormal_cdf(float64_t x) { /* Loosely based on logphi.m in * Gaussian Process Machine Learning Toolbox file logphi.m @@ -448,8 +448,8 @@ float64_t CStatistics::lnormal_cdf(float64_t x) -0.01621575378835404, 0.02629651521057465, -0.001829764677455021, - 2.0*(1.0-CMath::PI/3.0), - (4.0-CMath::PI)/3.0, + 2.0*(1.0-Math::PI/3.0), + (4.0-Math::PI)/3.0, 1.0, 1.0 }; @@ -469,7 +469,7 @@ float64_t CStatistics::lnormal_cdf(float64_t x) return std::log(normal_cdf(x)); } -float64_t CStatistics::erfc8_weighted_sum(float64_t x) +float64_t Statistics::erfc8_weighted_sum(float64_t x) { /* This is based on index 5725 in Hart et al */ @@ -513,12 +513,12 @@ float64_t CStatistics::erfc8_weighted_sum(float64_t x) return num/den; } -float64_t CStatistics::normal_cdf(float64_t x, float64_t std_dev) +float64_t Statistics::normal_cdf(float64_t x, float64_t std_dev) { return 0.5*(erfc(-x*M_SQRT1_2/std_dev)); } -float64_t CStatistics::gamma_inverse_cdf(float64_t p, float64_t a, +float64_t Statistics::gamma_inverse_cdf(float64_t p, float64_t a, float64_t b) { require(p>=0, "p ({}) has to be greater or equal to 0.", p); @@ -542,7 +542,7 @@ float64_t CStatistics::gamma_inverse_cdf(float64_t p, float64_t a, return output_x; } -float64_t CStatistics::fdistribution_cdf(float64_t x, float64_t d1, float64_t d2) +float64_t Statistics::fdistribution_cdf(float64_t x, float64_t d1, float64_t d2) { require(x>=0, "x ({}) has to be greater or equal to 0.", x); require(d1>0, "d1 ({}) has to be positive.", d1); @@ -563,7 +563,7 @@ float64_t CStatistics::fdistribution_cdf(float64_t x, float64_t d1, float64_t d2 return output_p; } -float64_t CStatistics::dlgamma(float64_t x) +float64_t Statistics::dlgamma(float64_t x) { float64_t result=0.0; @@ -571,7 +571,7 @@ float64_t CStatistics::dlgamma(float64_t x) { // use reflection formula x=1.0-x; - result = CMath::PI / std::tan(CMath::PI * x); + result = Math::PI / std::tan(Math::PI * x); } // make x>7 for approximation @@ -599,7 +599,7 @@ float64_t CStatistics::dlgamma(float64_t x) -26.45616165999210241989}; float64_t power=1.0; - float64_t ix2=1.0/CMath::sq(x); + float64_t ix2=1.0/Math::sq(x); // perform approximation for (index_t i=0; i<10; i++) @@ -611,7 +611,7 @@ float64_t CStatistics::dlgamma(float64_t x) return result; } -float64_t CStatistics::log_det_general(const SGMatrix A) +float64_t Statistics::log_det_general(const SGMatrix A) { Map eigen_A(A.matrix, A.num_rows, A.num_cols); require(eigen_A.rows()==eigen_A.cols(), @@ -657,7 +657,7 @@ float64_t CStatistics::log_det_general(const SGMatrix A) } } - float64_t result=CMath::INFTY; + float64_t result=Math::INFTY; if (check_u==detP) result=u.array().abs().log().sum(); @@ -665,7 +665,7 @@ float64_t CStatistics::log_det_general(const SGMatrix A) return result; } -float64_t CStatistics::log_det(SGMatrix m) +float64_t Statistics::log_det(SGMatrix m) { /* map the matrix to eigen3 to perform cholesky */ Map M(m.matrix, m.num_rows, m.num_cols); @@ -688,7 +688,7 @@ float64_t CStatistics::log_det(SGMatrix m) return retval; } -float64_t CStatistics::log_det(const SGSparseMatrix m) +float64_t Statistics::log_det(const SGSparseMatrix m) { typedef SparseMatrix MatrixType; const MatrixType &M=EigenSparseUtil::toEigenSparse(m); @@ -709,7 +709,7 @@ float64_t CStatistics::log_det(const SGSparseMatrix m) } template -SGMatrix CStatistics::sample_from_gaussian(SGVector mean, +SGMatrix Statistics::sample_from_gaussian(SGVector mean, SGMatrix cov, PRNG& prng, int32_t N, bool precision_matrix) { require(cov.num_rows>0, "Number of covariance rows must be positive!"); @@ -757,27 +757,27 @@ SGMatrix CStatistics::sample_from_gaussian(SGVector mean, return S; } -template SGMatrix CStatistics::sample_from_gaussian(SGVector mean, +template SGMatrix Statistics::sample_from_gaussian(SGVector mean, SGMatrix cov, std::mt19937_64& prng, int32_t N, bool precision_matrix); template -SGMatrix CStatistics::sample_from_gaussian(SGVector mean, +SGMatrix Statistics::sample_from_gaussian(SGVector mean, SGSparseMatrix cov, PRNG& prng, int32_t N, bool precision_matrix) { require(cov.num_vectors>0, - "CStatistics::sample_from_gaussian(): \ + "Statistics::sample_from_gaussian(): \ Number of covariance rows must be positive!"); require(cov.num_features>0, - "CStatistics::sample_from_gaussian(): \ + "Statistics::sample_from_gaussian(): \ Number of covariance cols must be positive!"); require(cov.sparse_matrix, - "CStatistics::sample_from_gaussian(): \ + "Statistics::sample_from_gaussian(): \ Covariance is not initialized!"); require(cov.num_vectors==cov.num_features, - "CStatistics::sample_from_gaussian(): \ + "Statistics::sample_from_gaussian(): \ Covariance should be square matrix!"); require(mean.vlen==cov.num_vectors, - "CStatistics::sample_from_gaussian(): \ + "Statistics::sample_from_gaussian(): \ Mean and covariance dimension mismatch!"); int32_t dim=mean.vlen; @@ -826,11 +826,11 @@ SGMatrix CStatistics::sample_from_gaussian(SGVector mean, return S; } -template SGMatrix CStatistics::sample_from_gaussian(SGVector mean, +template SGMatrix Statistics::sample_from_gaussian(SGVector mean, SGSparseMatrix cov, std::mt19937_64& prng, int32_t N, bool precision_matrix); -CStatistics::SigmoidParamters CStatistics::fit_sigmoid( +Statistics::SigmoidParamters Statistics::fit_sigmoid( SGVector scores, SGVector labels, index_t maxiter, float64_t minstep, float64_t sigma, float64_t epsilon) { @@ -916,7 +916,7 @@ CStatistics::SigmoidParamters CStatistics::fit_sigmoid( } /* Stopping Criteria */ - if (CMath::abs(g1) < epsilon && CMath::abs(g2) < epsilon) + if (Math::abs(g1) < epsilon && Math::abs(g2) < epsilon) break; /* Finding Newton direction: -inv(H') * g */ @@ -975,18 +975,18 @@ CStatistics::SigmoidParamters CStatistics::fit_sigmoid( SG_DEBUG("fitted sigmoid: a={}, b={}", a, b) - CStatistics::SigmoidParamters result; + Statistics::SigmoidParamters result; result.a = a; result.b = b; return result; } -CStatistics::SigmoidParamters CStatistics::fit_sigmoid(SGVector scores) +Statistics::SigmoidParamters Statistics::fit_sigmoid(SGVector scores) { - SG_TRACE("entering CStatistics::fit_sigmoid()"); + SG_TRACE("entering Statistics::fit_sigmoid()"); - require(scores.vector, "CStatistics::fit_sigmoid() requires " + require(scores.vector, "Statistics::fit_sigmoid() requires " "scores vector!"); /* count prior0 and prior1 if needed */ @@ -1084,7 +1084,7 @@ CStatistics::SigmoidParamters CStatistics::fit_sigmoid(SGVector score } /* Stopping Criteria */ - if (CMath::abs(g1) score if (stepsize score if (it>=maxiter-1) { - io::warn("CStatistics::fit_sigmoid(): reaching maximal iterations," + io::warn("Statistics::fit_sigmoid(): reaching maximal iterations," " g1={}, g2={}", g1, g2); } SG_DEBUG("fitted sigmoid: a={}, b={}", a, b) - CStatistics::SigmoidParamters result; + Statistics::SigmoidParamters result; result.a=a; result.b=b; - SG_TRACE("leaving CStatistics::fit_sigmoid()"); + SG_TRACE("leaving Statistics::fit_sigmoid()"); return result; } -const float64_t CStatistics::ERFC_CASE1=0.0492; +const float64_t Statistics::ERFC_CASE1=0.0492; -const float64_t CStatistics::ERFC_CASE2=-11.3137; +const float64_t Statistics::ERFC_CASE2=-11.3137; diff --git a/src/shogun/mathematics/Statistics.h b/src/shogun/mathematics/Statistics.h index 9e0f2319181..dce32e5a161 100644 --- a/src/shogun/mathematics/Statistics.h +++ b/src/shogun/mathematics/Statistics.h @@ -22,7 +22,7 @@ template class SGSparseMatrix; /** @brief Class that contains certain functions related to statistics, such as * probability/cumulative distribution functions, different statistics, etc. */ -class CStatistics: public CSGObject +class Statistics: public SGObject { public: @@ -340,7 +340,7 @@ class CStatistics: public CSGObject /** The log determinant of a dense matrix * * If determinant of the input matrix is positive, it returns the logarithm of the value. - * If not, it returns CMath::INFTY + * If not, it returns Math::INFTY * Note that the input matrix is not required to be symmetric positive definite. * This method is slower than log_det() if input matrix is known to be symmetric positive definite * @@ -435,7 +435,7 @@ class CStatistics: public CSGObject /// mean not implemented for complex128_t, returns 0.0 instead template <> - inline floatmax_t CStatistics::mean(SGVector vec) + inline floatmax_t Statistics::mean(SGVector vec) { not_implemented(SOURCE_LOCATION); return floatmax_t(0.0); diff --git a/src/shogun/mathematics/ajd/ApproxJointDiagonalizer.h b/src/shogun/mathematics/ajd/ApproxJointDiagonalizer.h index bfd190de914..29fc2645c5c 100644 --- a/src/shogun/mathematics/ajd/ApproxJointDiagonalizer.h +++ b/src/shogun/mathematics/ajd/ApproxJointDiagonalizer.h @@ -27,17 +27,17 @@ namespace shogun * a set \f${C^1 ... C^k}\f$ of real valued symmetric * \f$NxN\f$ matrices - \f$V*C*V^T\f$ */ -class CApproxJointDiagonalizer : public CSGObject +class ApproxJointDiagonalizer : public SGObject { public: /** constructor */ - CApproxJointDiagonalizer() : CSGObject() + ApproxJointDiagonalizer() : SGObject() { }; /** destructor */ - virtual ~CApproxJointDiagonalizer() + virtual ~ApproxJointDiagonalizer() { } @@ -50,7 +50,7 @@ class CApproxJointDiagonalizer : public CSGObject */ virtual SGMatrix compute(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) = 0; /** return the matrix V that best diagonalizes C */ diff --git a/src/shogun/mathematics/ajd/FFDiag.cpp b/src/shogun/mathematics/ajd/FFDiag.cpp index e2e44e1922d..ca850a6ce9c 100644 --- a/src/shogun/mathematics/ajd/FFDiag.cpp +++ b/src/shogun/mathematics/ajd/FFDiag.cpp @@ -8,7 +8,7 @@ using namespace Eigen; void getW(float64_t *C, int *ptN, int *ptK, float64_t *W); -SGMatrix CFFDiag::diagonalize(SGNDArray C0, SGMatrix V0, +SGMatrix FFDiag::diagonalize(SGNDArray C0, SGMatrix V0, double eps, int itermax) { int n = C0.dims[0]; @@ -69,7 +69,7 @@ SGMatrix CFFDiag::diagonalize(SGNDArray C0, SGMatrix 1) - df = CMath::abs(crit[inum-1]-crit[inum]); + df = Math::abs(crit[inum-1]-crit[inum]); inum++; } diff --git a/src/shogun/mathematics/ajd/FFDiag.h b/src/shogun/mathematics/ajd/FFDiag.h index 54204e81194..de604f5471a 100644 --- a/src/shogun/mathematics/ajd/FFDiag.h +++ b/src/shogun/mathematics/ajd/FFDiag.h @@ -27,17 +27,17 @@ namespace shogun * The Journal of Machine Learning Research, 5, 777-800. * */ -class CFFDiag : public CApproxJointDiagonalizer +class FFDiag : public ApproxJointDiagonalizer { public: /** constructor */ - CFFDiag() + FFDiag() { } /** destructor */ - virtual ~CFFDiag() + virtual ~FFDiag() { } @@ -50,7 +50,7 @@ class CFFDiag : public CApproxJointDiagonalizer */ static SGMatrix diagonalize(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200); /** Computes the matrix V that best diagonalizes C @@ -62,7 +62,7 @@ class CFFDiag : public CApproxJointDiagonalizer */ virtual SGMatrix compute(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) { m_V = diagonalize(C,V0,eps,itermax); diff --git a/src/shogun/mathematics/ajd/JADiag.cpp b/src/shogun/mathematics/ajd/JADiag.cpp index 6a4039b5771..a008676d717 100644 --- a/src/shogun/mathematics/ajd/JADiag.cpp +++ b/src/shogun/mathematics/ajd/JADiag.cpp @@ -9,7 +9,7 @@ using namespace Eigen; void jadiagw(float64_t c[], float64_t w[], int *ptn, int *ptm, float64_t a[], float64_t *logdet, float64_t *decr, float64_t *result); -SGMatrix CJADiag::diagonalize(SGNDArray C, SGMatrix V0, +SGMatrix JADiag::diagonalize(SGNDArray C, SGMatrix V0, double eps, int itermax) { int d = C.dims[0]; diff --git a/src/shogun/mathematics/ajd/JADiag.h b/src/shogun/mathematics/ajd/JADiag.h index 89d8c87d854..5e8c28c5d4e 100644 --- a/src/shogun/mathematics/ajd/JADiag.h +++ b/src/shogun/mathematics/ajd/JADiag.h @@ -26,17 +26,17 @@ namespace shogun * Signal Processing, IEEE Transactions on, 49(9), 1837-1848. * */ -class CJADiag : public CApproxJointDiagonalizer +class JADiag : public ApproxJointDiagonalizer { public: /** constructor */ - CJADiag() + JADiag() { } /** destructor */ - virtual ~CJADiag() + virtual ~JADiag() { } @@ -49,7 +49,7 @@ class CJADiag : public CApproxJointDiagonalizer */ static SGMatrix diagonalize(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200); /** Computes the matrix V that best diagonalizes C @@ -61,7 +61,7 @@ class CJADiag : public CApproxJointDiagonalizer */ virtual SGMatrix compute(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) { m_V = diagonalize(C,V0,eps,itermax); diff --git a/src/shogun/mathematics/ajd/JADiagOrth.cpp b/src/shogun/mathematics/ajd/JADiagOrth.cpp index 4231e7febe0..c49f037f871 100644 --- a/src/shogun/mathematics/ajd/JADiagOrth.cpp +++ b/src/shogun/mathematics/ajd/JADiagOrth.cpp @@ -11,7 +11,7 @@ void left_rot_stack(float64_t *A, int M, int N, int K, int p, int q, float64_t c void right_rot_stack(float64_t *A, int M, int N, int K, int p, int q, float64_t c, float64_t s); void left_rot_simple(float64_t *A, int m, int n, int p, int q, float64_t c, float64_t s); -SGMatrix CJADiagOrth::diagonalize(SGNDArray C, SGMatrix V0, +SGMatrix JADiagOrth::diagonalize(SGNDArray C, SGMatrix V0, double eps, int itermax) { int m = C.dims[0]; diff --git a/src/shogun/mathematics/ajd/JADiagOrth.h b/src/shogun/mathematics/ajd/JADiagOrth.h index 2aee8c547e4..0622d301a7d 100644 --- a/src/shogun/mathematics/ajd/JADiagOrth.h +++ b/src/shogun/mathematics/ajd/JADiagOrth.h @@ -27,17 +27,17 @@ namespace shogun * (Vol. 140, No. 6, pp. 362-370). IET Digital Library. * */ -class CJADiagOrth : public CApproxJointDiagonalizer +class JADiagOrth : public ApproxJointDiagonalizer { public: /** constructor */ - CJADiagOrth() + JADiagOrth() { } /** destructor */ - virtual ~CJADiagOrth() + virtual ~JADiagOrth() { } @@ -50,7 +50,7 @@ class CJADiagOrth : public CApproxJointDiagonalizer */ static SGMatrix diagonalize(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200); /** Computes the matrix V that best diagonalizes C @@ -62,7 +62,7 @@ class CJADiagOrth : public CApproxJointDiagonalizer */ virtual SGMatrix compute(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) { m_V = diagonalize(C,V0,eps,itermax); diff --git a/src/shogun/mathematics/ajd/JediDiag.cpp b/src/shogun/mathematics/ajd/JediDiag.cpp index 6a4ae3eb8e0..aeb5a061557 100644 --- a/src/shogun/mathematics/ajd/JediDiag.cpp +++ b/src/shogun/mathematics/ajd/JediDiag.cpp @@ -13,7 +13,7 @@ void sweepjedi(float64_t *C, int *pMatSize, int *pMatNumber, void iterJDI(float64_t *C, int *pMatSize, int *pMatNumber, int *ptn,int *ptm, float64_t *s_max, float64_t *sh_max, float64_t *A); -SGMatrix CJediDiag::diagonalize(SGNDArray C, SGMatrix V0, +SGMatrix JediDiag::diagonalize(SGNDArray C, SGMatrix V0, double eps, int itermax) { int d = C.dims[0]; diff --git a/src/shogun/mathematics/ajd/JediDiag.h b/src/shogun/mathematics/ajd/JediDiag.h index 7fde90cad71..f663a578c3c 100644 --- a/src/shogun/mathematics/ajd/JediDiag.h +++ b/src/shogun/mathematics/ajd/JediDiag.h @@ -25,17 +25,17 @@ namespace shogun * Signal Processing, IEEE Transactions on, 57(6), 2222-2231. * */ -class CJediDiag : public CApproxJointDiagonalizer +class JediDiag : public ApproxJointDiagonalizer { public: /** constructor */ - CJediDiag() + JediDiag() { } /** destructor */ - virtual ~CJediDiag() + virtual ~JediDiag() { } @@ -48,7 +48,7 @@ class CJediDiag : public CApproxJointDiagonalizer */ static SGMatrix diagonalize(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200); /** Computes the matrix V that best diagonalizes C @@ -60,7 +60,7 @@ class CJediDiag : public CApproxJointDiagonalizer */ virtual SGMatrix compute(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) { m_V = diagonalize(C,V0,eps,itermax); diff --git a/src/shogun/mathematics/ajd/QDiag.cpp b/src/shogun/mathematics/ajd/QDiag.cpp index acaffacfef0..91ef338124a 100644 --- a/src/shogun/mathematics/ajd/QDiag.cpp +++ b/src/shogun/mathematics/ajd/QDiag.cpp @@ -6,7 +6,7 @@ using namespace shogun; using namespace Eigen; -SGMatrix CQDiag::diagonalize(SGNDArray C, SGMatrix V0, +SGMatrix QDiag::diagonalize(SGNDArray C, SGMatrix V0, double eps, int itermax) { int N = C.dims[0]; @@ -16,7 +16,7 @@ SGMatrix CQDiag::diagonalize(SGNDArray C, SGMatrix CQDiag::diagonalize_impl(SGNDArray& C, SGMatrix& V, +SGMatrix QDiag::diagonalize_impl(SGNDArray& C, SGMatrix& V, double eps, int itermax) { int N = C.dims[0]; @@ -150,7 +150,7 @@ SGMatrix CQDiag::diagonalize_impl(SGNDArray& C, SGMatrix 1) - deltacrit = CMath::abs( crit[iter] - crit[iter-1] ); + deltacrit = Math::abs( crit[iter] - crit[iter-1] ); iter++; } diff --git a/src/shogun/mathematics/ajd/QDiag.h b/src/shogun/mathematics/ajd/QDiag.h index a41f0684240..cc8ba9bebed 100644 --- a/src/shogun/mathematics/ajd/QDiag.h +++ b/src/shogun/mathematics/ajd/QDiag.h @@ -27,17 +27,17 @@ namespace shogun * Signal Processing, IEEE Transactions on, 54(9), 3270-3278. * */ -class CQDiag : public RandomMixin +class QDiag : public RandomMixin { public: /** constructor */ - CQDiag() + QDiag() { } /** destructor */ - virtual ~CQDiag() + virtual ~QDiag() { } @@ -50,13 +50,13 @@ class CQDiag : public RandomMixin */ static SGMatrix diagonalize(SGNDArray C, SGMatrix V0, - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200); template static SGMatrix diagonalize(SGNDArray C, PRNG& prng, - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) { int N = C.dims[0]; @@ -75,7 +75,7 @@ class CQDiag : public RandomMixin */ virtual SGMatrix compute(SGNDArray C, SGMatrix V0 = SGMatrix(NULL,0,0,false), - double eps=CMath::MACHINE_EPSILON, + double eps=Math::MACHINE_EPSILON, int itermax=200) { int N = C.dims[0]; diff --git a/src/shogun/mathematics/ajd/UWedge.cpp b/src/shogun/mathematics/ajd/UWedge.cpp index f13189f5d00..78f50f66ad5 100644 --- a/src/shogun/mathematics/ajd/UWedge.cpp +++ b/src/shogun/mathematics/ajd/UWedge.cpp @@ -6,7 +6,7 @@ using namespace shogun; using namespace Eigen; -SGMatrix CUWedge::diagonalize(SGNDArray C, SGMatrix V0, +SGMatrix UWedge::diagonalize(SGNDArray C, SGMatrix V0, double eps, int itermax) { int d = C.dims[0]; @@ -105,7 +105,7 @@ SGMatrix CUWedge::diagonalize(SGNDArray C, SGMatrix& A, const SGVector& b, T alpha = 1, T beta = 1) { - auto diagonal_len = CMath::min(A.num_cols, A.num_rows); + auto diagonal_len = Math::min(A.num_cols, A.num_rows); require( diagonal_len == b.vlen, "Length of main diagonal of matrix A " "({}) doesn't match length of vector b " @@ -601,7 +601,7 @@ namespace shogun template void add_ridge(SGMatrix& A, T beta) { - auto diagonal_len = CMath::min(A.num_cols, A.num_rows); + auto diagonal_len = Math::min(A.num_cols, A.num_rows); require(diagonal_len > 0, "Matrix can't be empty."); infer_backend(A)->add_ridge(A, beta); } @@ -1878,7 +1878,7 @@ namespace shogun bool thin_U = true, SVDAlgorithm alg = SVDAlgorithm::BidiagonalDivideConquer) { - auto r = CMath::min(A.num_cols, A.num_rows); + auto r = Math::min(A.num_cols, A.num_rows); require( (A.num_rows == U.num_rows), "Number of rows of matrix A ({}) must match matrix U ({}).", diff --git a/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp b/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp index b43cdc69b0c..2c5058e173e 100644 --- a/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp +++ b/src/shogun/mathematics/linalg/backend/eigen/BasicOps.cpp @@ -286,7 +286,7 @@ void LinalgBackendEigen::pinvh_impl( typename SGVector::EigenVectorXtMap s_eig = s; typename SGMatrix::EigenMatrixXtMap V_eig = V; - float64_t pinv_tol = CMath::MACHINE_EPSILON * m * s_eig.real().maxCoeff(); + float64_t pinv_tol = Math::MACHINE_EPSILON * m * s_eig.real().maxCoeff(); s_eig.array() = (s_eig.real().array() > pinv_tol) .select(s_eig.real().array().inverse(), 0); result_eig = V_eig * s_eig.asDiagonal() * V_eig.transpose(); @@ -305,7 +305,7 @@ void LinalgBackendEigen::pinv_impl( auto U_eig = svd_eig.matrixU().real(); auto V_eig = svd_eig.matrixV().real(); - float64_t pinv_tol = CMath::MACHINE_EPSILON * m * s_eig.maxCoeff(); + float64_t pinv_tol = Math::MACHINE_EPSILON * m * s_eig.maxCoeff(); s_eig.array() = (s_eig.array() > pinv_tol).select(s_eig.array().inverse(), 0); result_eig = V_eig * s_eig.asDiagonal() * U_eig.transpose(); diff --git a/src/shogun/mathematics/linalg/backend/eigen/SpecialPurposes.cpp b/src/shogun/mathematics/linalg/backend/eigen/SpecialPurposes.cpp index b36484d73fd..6c8d4d51534 100644 --- a/src/shogun/mathematics/linalg/backend/eigen/SpecialPurposes.cpp +++ b/src/shogun/mathematics/linalg/backend/eigen/SpecialPurposes.cpp @@ -154,7 +154,7 @@ void LinalgBackendEigen::rectified_linear_impl( typename SGMatrix::EigenMatrixXtMap result_eig = result; for (index_t i = 0; i < a_eig.rows() * a_eig.cols(); ++i) - result_eig(i) = CMath::max((T)0, a_eig(i)); + result_eig(i) = Math::max((T)0, a_eig(i)); } /** Eigen3 softmax method */ diff --git a/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.cpp b/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.cpp index 99f18f5643d..236b66c5a6f 100644 --- a/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.cpp +++ b/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.cpp @@ -16,25 +16,25 @@ using namespace Eigen; namespace shogun { -CDirectEigenSolver::CDirectEigenSolver() - : CEigenSolver() +DirectEigenSolver::DirectEigenSolver() + : EigenSolver() { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CDirectEigenSolver::CDirectEigenSolver( - CDenseMatrixOperator* linear_operator) - : CEigenSolver((CLinearOperator*)linear_operator) +DirectEigenSolver::DirectEigenSolver( + std::shared_ptr> linear_operator) + : EigenSolver(linear_operator->as>()) { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CDirectEigenSolver::~CDirectEigenSolver() +DirectEigenSolver::~DirectEigenSolver() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } -void CDirectEigenSolver::compute() +void DirectEigenSolver::compute() { if (m_is_computed_min && m_is_computed_max) { @@ -42,8 +42,8 @@ void CDirectEigenSolver::compute() return; } - CDenseMatrixOperator* op - =dynamic_cast*>(m_linear_operator); + auto op + =m_linear_operator->as>(); require(op, "Linear operator is not of CDenseMatrixOperator type!"); SGMatrix m=op->get_matrix_operator(); diff --git a/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.h b/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.h index 78f2a561c28..c0a26e2c4a8 100644 --- a/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.h +++ b/src/shogun/mathematics/linalg/eigsolver/DirectEigenSolver.h @@ -13,16 +13,16 @@ namespace shogun { -template class CDenseMatrixOperator; +template class DenseMatrixOperator; /** @brief Class that computes eigenvalues of a real valued, self-adjoint * dense matrix linear operator using Eigen3 */ -class CDirectEigenSolver : public CEigenSolver +class DirectEigenSolver : public EigenSolver { public: /** default constructor */ - CDirectEigenSolver(); + DirectEigenSolver(); /** * constructor @@ -30,10 +30,10 @@ class CDirectEigenSolver : public CEigenSolver * @param linear_operator self-adjoint dense-matrix linear operator whose * eigenvalues have to be found */ - CDirectEigenSolver(CDenseMatrixOperator* linear_operator); + DirectEigenSolver(std::shared_ptr> linear_operator); /** destructor */ - virtual ~CDirectEigenSolver(); + virtual ~DirectEigenSolver(); /** * compute method for computing eigenvalues of a real valued dense matrix diff --git a/src/shogun/mathematics/linalg/eigsolver/EigenSolver.h b/src/shogun/mathematics/linalg/eigsolver/EigenSolver.h index c46ec085888..6356b517ef8 100644 --- a/src/shogun/mathematics/linalg/eigsolver/EigenSolver.h +++ b/src/shogun/mathematics/linalg/eigsolver/EigenSolver.h @@ -18,12 +18,12 @@ namespace shogun * computing eigenvalues of a real valued, self-adjoint linear operator. It * also provides method for getting min and max eigenvalues. */ -class CEigenSolver : public CSGObject +class EigenSolver : public SGObject { public: /** default constructor */ - CEigenSolver() - : CSGObject() + EigenSolver() + : SGObject() { init(); } @@ -34,18 +34,18 @@ class CEigenSolver : public CSGObject * @param linear_operator real valued self-adjoint linear operator * whose eigenvalues have to be found */ - CEigenSolver(CLinearOperator* linear_operator) - : CSGObject() + EigenSolver(std::shared_ptr> linear_operator) + : SGObject() { init(); m_linear_operator=linear_operator; - SG_REF(m_linear_operator); + } /** destructor */ - virtual ~CEigenSolver() + virtual ~EigenSolver() { - SG_UNREF(m_linear_operator); + } /** @@ -93,7 +93,7 @@ class CEigenSolver : public CSGObject float64_t m_max_eigenvalue; /** the linear solver whose eigenvalues have to be found */ - CLinearOperator* m_linear_operator; + std::shared_ptr> m_linear_operator; /** flag that denotes that the minimum eigenvalue is already computed */ bool m_is_computed_min; @@ -117,7 +117,7 @@ class CEigenSolver : public CSGObject SG_ADD(&m_max_eigenvalue, "max_eigenvalue", "Maximum eigenvalue of a real valued self-adjoint linear operator"); - SG_ADD((CSGObject**)&m_linear_operator, "linear_operator", + SG_ADD((std::shared_ptr*)&m_linear_operator, "linear_operator", "Self-adjoint linear operator"); SG_ADD(&m_is_computed_min, "is_computed_min", diff --git a/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.cpp b/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.cpp index 6bf6bcb7f15..16ff83f8982 100644 --- a/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.cpp +++ b/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.cpp @@ -23,20 +23,20 @@ using namespace Eigen; namespace shogun { -CLanczosEigenSolver::CLanczosEigenSolver() - : CEigenSolver() +LanczosEigenSolver::LanczosEigenSolver() + : EigenSolver() { init(); } -CLanczosEigenSolver::CLanczosEigenSolver( - CLinearOperator* linear_operator) - : CEigenSolver(linear_operator) +LanczosEigenSolver::LanczosEigenSolver( + std::shared_ptr> linear_operator) + : EigenSolver(linear_operator) { init(); } -void CLanczosEigenSolver::init() +void LanczosEigenSolver::init() { m_max_iteration_limit=1000; m_relative_tolerence=1E-6; @@ -52,11 +52,11 @@ void CLanczosEigenSolver::init() "Absolute tolerence of solver"); } -CLanczosEigenSolver::~CLanczosEigenSolver() +LanczosEigenSolver::~LanczosEigenSolver() { } -void CLanczosEigenSolver::compute() +void LanczosEigenSolver::compute() { SG_TRACE("Entering"); diff --git a/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.h b/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.h index f1b4e8bc3ee..ad242c2c1df 100644 --- a/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.h +++ b/src/shogun/mathematics/linalg/eigsolver/LanczosEigenSolver.h @@ -14,16 +14,16 @@ namespace shogun { -template class CLinearOperator; +template class LinearOperator; /** @brief Class that computes eigenvalues of a real valued, self-adjoint * linear operator using Lanczos algorithm */ -class CLanczosEigenSolver : public CEigenSolver +class LanczosEigenSolver : public EigenSolver { public: /** default constructor */ - CLanczosEigenSolver(); + LanczosEigenSolver(); /** * constructor @@ -31,10 +31,10 @@ class CLanczosEigenSolver : public CEigenSolver * @param linear_operator self-adjoint linear operator whose eigenvalues * are to be found */ - CLanczosEigenSolver(CLinearOperator* linear_operator); + LanczosEigenSolver(std::shared_ptr> linear_operator); /** destructor */ - virtual ~CLanczosEigenSolver(); + virtual ~LanczosEigenSolver(); /** * compute method for computing eigenvalues of a real valued linear operator diff --git a/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.cpp b/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.cpp index 6d315f8f7aa..eff1d09bfb8 100644 --- a/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.cpp +++ b/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.cpp @@ -18,8 +18,8 @@ namespace shogun { template -CDenseMatrixOperator::CDenseMatrixOperator() - : CMatrixOperator() +DenseMatrixOperator::DenseMatrixOperator() + : MatrixOperator() { init(); @@ -27,8 +27,8 @@ CDenseMatrixOperator::CDenseMatrixOperator() } template -CDenseMatrixOperator::CDenseMatrixOperator(SGMatrix op) - : CMatrixOperator(op.num_cols), +DenseMatrixOperator::DenseMatrixOperator(SGMatrix op) + : MatrixOperator(op.num_cols), m_operator(op) { init(); @@ -37,9 +37,9 @@ CDenseMatrixOperator::CDenseMatrixOperator(SGMatrix op) } template -CDenseMatrixOperator::CDenseMatrixOperator( - const CDenseMatrixOperator& orig) - : CMatrixOperator(orig.get_dimension()) +DenseMatrixOperator::DenseMatrixOperator( + const DenseMatrixOperator& orig) + : MatrixOperator(orig.get_dimension()) { init(); @@ -54,25 +54,25 @@ CDenseMatrixOperator::CDenseMatrixOperator( } template -void CDenseMatrixOperator::init() +void DenseMatrixOperator::init() { - CSGObject::set_generic(); + SGObject::set_generic(); } template -CDenseMatrixOperator::~CDenseMatrixOperator() +DenseMatrixOperator::~DenseMatrixOperator() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } template -SGMatrix CDenseMatrixOperator::get_matrix_operator() const +SGMatrix DenseMatrixOperator::get_matrix_operator() const { return m_operator; } template -SGVector CDenseMatrixOperator::get_diagonal() const +SGVector DenseMatrixOperator::get_diagonal() const { require(m_operator.matrix, "Operator not initialized!"); @@ -90,7 +90,7 @@ SGVector CDenseMatrixOperator::get_diagonal() const } template -void CDenseMatrixOperator::set_diagonal(SGVector diag) +void DenseMatrixOperator::set_diagonal(SGVector diag) { require(m_operator.matrix, "Operator not initialized!"); require(diag.vector, "Diagonal not initialized!"); @@ -109,7 +109,7 @@ void CDenseMatrixOperator::set_diagonal(SGVector diag) } template -SGVector CDenseMatrixOperator::apply(SGVector b) const +SGVector DenseMatrixOperator::apply(SGVector b) const { require(m_operator.matrix, "Operator not initialized!"); require(this->get_dimension()==b.vlen, @@ -132,7 +132,7 @@ SGVector CDenseMatrixOperator::apply(SGVector b) const #define UNDEFINED(type) \ template<> \ -SGVector CDenseMatrixOperator::apply(SGVector b) const \ +SGVector DenseMatrixOperator::apply(SGVector b) const \ { \ error("Not supported for {}", #type);\ return b; \ @@ -144,18 +144,18 @@ UNDEFINED(int8_t) UNDEFINED(uint8_t) #undef UNDEFINED -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; -template class CDenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; +template class DenseMatrixOperator; } diff --git a/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.h b/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.h index 0ddee4d2e50..3207c0ccdc0 100644 --- a/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.h +++ b/src/shogun/mathematics/linalg/linop/DenseMatrixOperator.h @@ -22,31 +22,31 @@ template class SGMatrix; * being the matrix operator and \f$x\in\mathbb{C}^{n}\f$ being the vector. * The result is a vector \f$y\in\mathbb{C}^{m}\f$. */ -template class CDenseMatrixOperator : public CMatrixOperator +template class DenseMatrixOperator : public MatrixOperator { /** this class has support for complex128_t */ typedef bool supports_complex128_t; public: /** default constructor */ - CDenseMatrixOperator(); + DenseMatrixOperator(); /** * constructor * * @param op the dense matrix to be used as the linear operator */ - explicit CDenseMatrixOperator(SGMatrix op); + explicit DenseMatrixOperator(SGMatrix op); /** * copy constructor that creates a deep copy * * @param orig the original dense matrix operator */ - CDenseMatrixOperator(const CDenseMatrixOperator& orig); + DenseMatrixOperator(const DenseMatrixOperator& orig); /** destructor */ - ~CDenseMatrixOperator(); + ~DenseMatrixOperator(); /** * method that applies the dense-matrix linear operator to a vector @@ -77,7 +77,7 @@ typedef bool supports_complex128_t; * create a new dense matrix operator of Scalar type */ template - inline operator CDenseMatrixOperator*() const + inline operator DenseMatrixOperator*() const { require(m_operator.matrix, "Matrix is not initialized!"); @@ -88,7 +88,7 @@ typedef bool supports_complex128_t; casted_m(j,i)=static_cast(m_operator(j,i)); } - return new CDenseMatrixOperator(casted_m); + return new DenseMatrixOperator(casted_m); } /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linop/LinearOperator.cpp b/src/shogun/mathematics/linalg/linop/LinearOperator.cpp index 57c676af7e9..997d5683b86 100644 --- a/src/shogun/mathematics/linalg/linop/LinearOperator.cpp +++ b/src/shogun/mathematics/linalg/linop/LinearOperator.cpp @@ -11,13 +11,13 @@ namespace shogun { template -CLinearOperator::CLinearOperator() : CSGObject() +LinearOperator::LinearOperator() : SGObject() { init(); } template -CLinearOperator::CLinearOperator(index_t dimension) : CSGObject() +LinearOperator::LinearOperator(index_t dimension) : SGObject() { init(); @@ -25,18 +25,18 @@ CLinearOperator::CLinearOperator(index_t dimension) : CSGObject() } template -CLinearOperator::~CLinearOperator() +LinearOperator::~LinearOperator() { } template -const index_t CLinearOperator::get_dimension() const +const index_t LinearOperator::get_dimension() const { return m_dimension; } template -void CLinearOperator::init() +void LinearOperator::init() { m_dimension=0; @@ -44,19 +44,19 @@ void CLinearOperator::init() "Dimension of the vector on which linear operator can apply"); } -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; -template class CLinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; +template class LinearOperator; } diff --git a/src/shogun/mathematics/linalg/linop/LinearOperator.h b/src/shogun/mathematics/linalg/linop/LinearOperator.h index 5ee0459681e..576bc9bd435 100644 --- a/src/shogun/mathematics/linalg/linop/LinearOperator.h +++ b/src/shogun/mathematics/linalg/linop/LinearOperator.h @@ -17,21 +17,21 @@ namespace shogun * e.g. a matrix */ template -class CLinearOperator : public CSGObject +class LinearOperator : public SGObject { public: /** default constructor */ - CLinearOperator(); + LinearOperator(); /** * constructor * * @param dimension dimension of the vector on which the operator can be applied */ - CLinearOperator(index_t dimension); + LinearOperator(index_t dimension); /** destructor */ - virtual ~CLinearOperator(); + virtual ~LinearOperator(); /** @return the dimension on which the linear operator can apply */ const index_t get_dimension() const; diff --git a/src/shogun/mathematics/linalg/linop/MatrixOperator.cpp b/src/shogun/mathematics/linalg/linop/MatrixOperator.cpp index 010fdded23f..eb3a3d5bd3f 100644 --- a/src/shogun/mathematics/linalg/linop/MatrixOperator.cpp +++ b/src/shogun/mathematics/linalg/linop/MatrixOperator.cpp @@ -8,18 +8,18 @@ namespace shogun { -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; -template class CMatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; +template class MatrixOperator; } diff --git a/src/shogun/mathematics/linalg/linop/MatrixOperator.h b/src/shogun/mathematics/linalg/linop/MatrixOperator.h index 228b97814a2..bad2b060e22 100644 --- a/src/shogun/mathematics/linalg/linop/MatrixOperator.h +++ b/src/shogun/mathematics/linalg/linop/MatrixOperator.h @@ -20,12 +20,12 @@ namespace shogun * \mathbb{C}^{n}\f$ being the vector. The result is a vector \f$y\in * \mathbb{C}^{m}\f$. */ -template class CMatrixOperator : public CLinearOperator +template class MatrixOperator : public LinearOperator { public: /** default constructor */ - CMatrixOperator() - : CLinearOperator() + MatrixOperator() + : LinearOperator() { } @@ -34,13 +34,13 @@ template class CMatrixOperator : public CLinearOperator * * @param dimension the dimension of the vector on which this it can apply */ - CMatrixOperator(index_t dimension) - : CLinearOperator(dimension) + MatrixOperator(index_t dimension) + : LinearOperator(dimension) { } /** destructor */ - ~CMatrixOperator() + ~MatrixOperator() { } diff --git a/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.cpp b/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.cpp index 554ea3a4cf6..df83d8d85c0 100644 --- a/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.cpp +++ b/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.cpp @@ -16,24 +16,24 @@ namespace shogun { template -CSparseMatrixOperator::CSparseMatrixOperator() - : CMatrixOperator() +SparseMatrixOperator::SparseMatrixOperator() + : MatrixOperator() { init(); } template -CSparseMatrixOperator::CSparseMatrixOperator(SGSparseMatrix op) - : CMatrixOperator(op.num_features), +SparseMatrixOperator::SparseMatrixOperator(SGSparseMatrix op) + : MatrixOperator(op.num_features), m_operator(op) { init(); } template -CSparseMatrixOperator::CSparseMatrixOperator - (const CSparseMatrixOperator& orig) - : CMatrixOperator(orig.get_dimension()) +SparseMatrixOperator::SparseMatrixOperator + (const SparseMatrixOperator& orig) + : MatrixOperator(orig.get_dimension()) { init(); @@ -60,31 +60,31 @@ CSparseMatrixOperator::CSparseMatrixOperator } template -void CSparseMatrixOperator::init() +void SparseMatrixOperator::init() { - CSGObject::set_generic(); + SGObject::set_generic(); } template -CSparseMatrixOperator::~CSparseMatrixOperator() +SparseMatrixOperator::~SparseMatrixOperator() { } template -SGSparseMatrix CSparseMatrixOperator::get_matrix_operator() const +SGSparseMatrix SparseMatrixOperator::get_matrix_operator() const { return m_operator; } template -SparsityStructure* CSparseMatrixOperator::get_sparsity_structure( +SparsityStructure* SparseMatrixOperator::get_sparsity_structure( int64_t power) const { require(power>0, "matrix-power is non-positive!"); // create casted operator in bool for capturing the sparsity - CSparseMatrixOperator* sp_str - =static_cast*>(*this); + SparseMatrixOperator* sp_str + =static_cast*>(*this); // eigen3 map for this sparse matrix in which we compute current power Eigen::SparseMatrix current_power @@ -123,14 +123,12 @@ SparsityStructure* CSparseMatrixOperator::get_sparsity_structure( int32_t* outerIndexPtr=const_cast(matrix_power.outerIndexPtr()); int32_t* innerIndexPtr=const_cast(matrix_power.innerIndexPtr()); - SG_UNREF(sp_str); - return new SparsityStructure(outerIndexPtr, innerIndexPtr, matrix_power.rows()); } template<> \ -SparsityStructure* CSparseMatrixOperator +SparsityStructure* SparseMatrixOperator ::get_sparsity_structure(int64_t power) const { error("Not supported for complex128_t"); @@ -138,7 +136,7 @@ SparsityStructure* CSparseMatrixOperator } template -SGVector CSparseMatrixOperator::get_diagonal() const +SGVector SparseMatrixOperator::get_diagonal() const { require(m_operator.sparse_matrix, "Operator not initialized!"); @@ -164,7 +162,7 @@ SGVector CSparseMatrixOperator::get_diagonal() const } template -void CSparseMatrixOperator::set_diagonal(SGVector diag) +void SparseMatrixOperator::set_diagonal(SGVector diag) { require(m_operator.sparse_matrix, "Operator not initialized!"); require(diag.vector, "Diagonal not initialized!"); @@ -208,7 +206,7 @@ void CSparseMatrixOperator::set_diagonal(SGVector diag) } template -SGVector CSparseMatrixOperator::apply(SGVector b) const +SGVector SparseMatrixOperator::apply(SGVector b) const { require(m_operator.sparse_matrix, "Operator not initialized!"); require(this->get_dimension()==b.vlen, @@ -223,7 +221,7 @@ SGVector CSparseMatrixOperator::apply(SGVector b) const #define UNDEFINED(type) \ template<> \ -SGVector CSparseMatrixOperator::apply(SGVector b) const \ +SGVector SparseMatrixOperator::apply(SGVector b) const \ { \ error("Not supported for {}", #type);\ return b; \ @@ -243,18 +241,18 @@ UNDEFINED(float32_t) UNDEFINED(floatmax_t) #undef UNDEFINED -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; -template class CSparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; +template class SparseMatrixOperator; } diff --git a/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.h b/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.h index d0cb6c0f107..376e0f438db 100644 --- a/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.h +++ b/src/shogun/mathematics/linalg/linop/SparseMatrixOperator.h @@ -92,31 +92,31 @@ struct SparsityStructure * being the matrix operator and \f$x\in\mathbb{C}^{n}\f$ being the vector. * The result is a vector \f$y\in\mathbb{C}^{m}\f$. */ -template class CSparseMatrixOperator : public CMatrixOperator +template class SparseMatrixOperator : public MatrixOperator { /** this class has support for complex128_t */ typedef bool supports_complex128_t; public: /** default constructor */ - CSparseMatrixOperator(); + SparseMatrixOperator(); /** * constructor * * @param op the sparse matrix to be used as the linear operator */ - explicit CSparseMatrixOperator(SGSparseMatrix op); + explicit SparseMatrixOperator(SGSparseMatrix op); /** * copy constructor that creates a deep copy * * @param orig the original sparse matrix operator */ - CSparseMatrixOperator(const CSparseMatrixOperator& orig); + SparseMatrixOperator(const SparseMatrixOperator& orig); /** destructor */ - ~CSparseMatrixOperator(); + ~SparseMatrixOperator(); /** * method that applies the sparse-matrix linear operator to a vector @@ -150,7 +150,7 @@ typedef bool supports_complex128_t; * create a new sparse matrix operator of Scalar type */ template - inline operator CSparseMatrixOperator*() const + inline operator SparseMatrixOperator*() const { require(m_operator.sparse_matrix, "Matrix is not initialized!"); typedef SGSparseVector vector; @@ -175,7 +175,7 @@ typedef bool supports_complex128_t; casted_m.num_vectors=m_operator.num_vectors; casted_m.num_features= m_operator.num_features; - return new CSparseMatrixOperator(casted_m); + return new SparseMatrixOperator(casted_m); } /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.cpp b/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.cpp index 81878adb269..7d35c523e3f 100644 --- a/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.cpp @@ -20,22 +20,22 @@ using namespace Eigen; namespace shogun { -CCGMShiftedFamilySolver::CCGMShiftedFamilySolver() - : CIterativeShiftedLinearFamilySolver() +CGMShiftedFamilySolver::CGMShiftedFamilySolver() + : IterativeShiftedLinearFamilySolver() { } -CCGMShiftedFamilySolver::CCGMShiftedFamilySolver(bool store_residuals) - : CIterativeShiftedLinearFamilySolver(store_residuals) +CGMShiftedFamilySolver::CGMShiftedFamilySolver(bool store_residuals) + : IterativeShiftedLinearFamilySolver(store_residuals) { } -CCGMShiftedFamilySolver::~CCGMShiftedFamilySolver() +CGMShiftedFamilySolver::~CGMShiftedFamilySolver() { } -SGVector CCGMShiftedFamilySolver::solve( - CLinearOperator* A, SGVector b) +SGVector CGMShiftedFamilySolver::solve( + std::shared_ptr> A, SGVector b) { SGVector shifts(1); shifts[0]=0.0; @@ -45,8 +45,8 @@ SGVector CCGMShiftedFamilySolver::solve( return solve_shifted_weighted(A, b, shifts, weights).get_real(); } -SGVector CCGMShiftedFamilySolver::solve_shifted_weighted( - CLinearOperator* A, SGVector b, +SGVector CGMShiftedFamilySolver::solve_shifted_weighted( + std::shared_ptr> A, SGVector b, SGVector shifts, SGVector weights, bool negate) { SG_TRACE("Entering"); @@ -99,7 +99,7 @@ SGVector CCGMShiftedFamilySolver::solve_shifted_weighted( m_relative_tolerence, m_absolute_tolerence); // start the timer - CTime time; + Time time; time.start(); // set the residuals to zero diff --git a/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.h b/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.h index d5e6243cfce..f9cfe2146f4 100644 --- a/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.h +++ b/src/shogun/mathematics/linalg/linsolver/CGMShiftedFamilySolver.h @@ -13,7 +13,7 @@ namespace shogun { -template class CLinearOperator; +template class LinearOperator; template class SGVector; /** @@ -25,19 +25,19 @@ template class SGVector; * open source library Krylstat (https://github.com/Froskekongen/KRYLSTAT/), * written by Erlend Aune, under GPL2+ */ -class CCGMShiftedFamilySolver - : public CIterativeShiftedLinearFamilySolver +class CGMShiftedFamilySolver + : public IterativeShiftedLinearFamilySolver { public: /** default constructor */ - CCGMShiftedFamilySolver(); + CGMShiftedFamilySolver(); /** one arg constructor */ - CCGMShiftedFamilySolver(bool store_residuals); + CGMShiftedFamilySolver(bool store_residuals); /** destructor */ - virtual ~CCGMShiftedFamilySolver(); + virtual ~CGMShiftedFamilySolver(); /** * solve method for solving linear systems assuming no shift @@ -46,7 +46,7 @@ class CCGMShiftedFamilySolver * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, + virtual SGVector solve(std::shared_ptr> A, SGVector b); /** @@ -61,7 +61,7 @@ class CCGMShiftedFamilySolver * shift */ virtual SGVector solve_shifted_weighted( - CLinearOperator* A, SGVector b, + std::shared_ptr> A, SGVector b, SGVector shifts, SGVector weights, bool negate = false); diff --git a/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.cpp b/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.cpp index b91833704f6..d20c5b21ccb 100644 --- a/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.cpp @@ -20,25 +20,25 @@ using namespace Eigen; namespace shogun { -CConjugateGradientSolver::CConjugateGradientSolver() - : CIterativeLinearSolver() +ConjugateGradientSolver::ConjugateGradientSolver() + : IterativeLinearSolver() { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CConjugateGradientSolver::CConjugateGradientSolver(bool store_residuals) - : CIterativeLinearSolver(store_residuals) +ConjugateGradientSolver::ConjugateGradientSolver(bool store_residuals) + : IterativeLinearSolver(store_residuals) { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CConjugateGradientSolver::~CConjugateGradientSolver() +ConjugateGradientSolver::~ConjugateGradientSolver() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } -SGVector CConjugateGradientSolver::solve( - CLinearOperator* A, SGVector b) +SGVector ConjugateGradientSolver::solve( + std::shared_ptr> A, SGVector b) { SG_TRACE("CConjugateGradientSolve::solve(): Entering.."); @@ -72,7 +72,7 @@ SGVector CConjugateGradientSolver::solve( float64_t r_norm2=r.dot(r); // start the timer - CTime time; + Time time; time.start(); // set the residuals to zero diff --git a/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.h b/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.h index 73dfb6351c8..d59dc90e5c6 100644 --- a/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.h +++ b/src/shogun/mathematics/linalg/linsolver/ConjugateGradientSolver.h @@ -13,7 +13,7 @@ namespace shogun { -template class CLinearOperator; +template class LinearOperator; template class SGVector; /** @@ -21,18 +21,18 @@ template class SGVector; * involving a real valued linear operator and vector. Useful for large sparse * systems involving sparse symmetric and positive-definite matrices. */ -class CConjugateGradientSolver : public CIterativeLinearSolver +class ConjugateGradientSolver : public IterativeLinearSolver { public: /** default constructor */ - CConjugateGradientSolver(); + ConjugateGradientSolver(); /** one arg constructor */ - CConjugateGradientSolver(bool store_residuals); + ConjugateGradientSolver(bool store_residuals); /** destructor */ - virtual ~CConjugateGradientSolver(); + virtual ~ConjugateGradientSolver(); /** * solve method for solving real linear systems @@ -41,7 +41,7 @@ class CConjugateGradientSolver : public CIterativeLinearSolver solve(CLinearOperator* A, + virtual SGVector solve(std::shared_ptr> A, SGVector b); /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.cpp b/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.cpp index b093cfd1178..595b773e51e 100644 --- a/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.cpp @@ -19,27 +19,27 @@ using namespace Eigen; namespace shogun { -CConjugateOrthogonalCGSolver::CConjugateOrthogonalCGSolver() - : CIterativeLinearSolver() +ConjugateOrthogonalCGSolver::ConjugateOrthogonalCGSolver() + : IterativeLinearSolver() { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CConjugateOrthogonalCGSolver::CConjugateOrthogonalCGSolver(bool store_residuals) - : CIterativeLinearSolver(store_residuals) +ConjugateOrthogonalCGSolver::ConjugateOrthogonalCGSolver(bool store_residuals) + : IterativeLinearSolver(store_residuals) { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CConjugateOrthogonalCGSolver::~CConjugateOrthogonalCGSolver() +ConjugateOrthogonalCGSolver::~ConjugateOrthogonalCGSolver() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } -SGVector CConjugateOrthogonalCGSolver::solve( - CLinearOperator* A, SGVector b) +SGVector ConjugateOrthogonalCGSolver::solve( + std::shared_ptr> A, SGVector b) { - SG_TRACE("CConjugateOrthogonalCGSolver::solve(): Entering.."); + SG_TRACE("ConjugateOrthogonalCGSolver::solve(): Entering.."); // sanity check require(A, "Operator is NULL!"); @@ -69,7 +69,7 @@ SGVector CConjugateOrthogonalCGSolver::solve( m_relative_tolerence, m_absolute_tolerence); // start the timer - CTime time; + Time time; time.start(); // set the residuals to zero @@ -131,7 +131,7 @@ SGVector CConjugateOrthogonalCGSolver::solve( io::info("Iteration took {} times, residual norm={:.20f}, time elapsed={}", it.get_iter_info().iteration_count, it.get_iter_info().residual_norm, elapsed); - SG_DEBUG("CConjugateOrthogonalCGSolver::solve(): Leaving.."); + SG_DEBUG("ConjugateOrthogonalCGSolver::solve(): Leaving.."); return result; } diff --git a/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.h b/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.h index fbd6c98b17f..a7a816d8cdd 100644 --- a/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.h +++ b/src/shogun/mathematics/linalg/linsolver/ConjugateOrthogonalCGSolver.h @@ -13,7 +13,7 @@ namespace shogun { -template class CLinearOperator; +template class LinearOperator; template class SGVector; /** @@ -26,19 +26,19 @@ template class SGVector; * Where A Is Symmetric Complex". IEEE Transactions on Magnetics, Vol. 26, * No. 2, March 1990 */ -class CConjugateOrthogonalCGSolver - : public CIterativeLinearSolver +class ConjugateOrthogonalCGSolver + : public IterativeLinearSolver { public: /** default constructor */ - CConjugateOrthogonalCGSolver(); + ConjugateOrthogonalCGSolver(); /** one arg constructor */ - CConjugateOrthogonalCGSolver(bool store_residuals); + ConjugateOrthogonalCGSolver(bool store_residuals); /** destructor */ - virtual ~CConjugateOrthogonalCGSolver(); + virtual ~ConjugateOrthogonalCGSolver(); /** * solve method for solving complex linear systems @@ -47,7 +47,7 @@ class CConjugateOrthogonalCGSolver * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, + virtual SGVector solve(std::shared_ptr> A, SGVector b); /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.cpp b/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.cpp index 3e29aaf8309..f241cff6f9a 100644 --- a/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.cpp +++ b/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.cpp @@ -18,36 +18,35 @@ using namespace Eigen; namespace shogun { -CDirectLinearSolverComplex::CDirectLinearSolverComplex() - : CLinearSolver(), +DirectLinearSolverComplex::DirectLinearSolverComplex() + : LinearSolver(), m_type(DS_QR_NOPERM) { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CDirectLinearSolverComplex::CDirectLinearSolverComplex(EDirectSolverType type) - : CLinearSolver(), +DirectLinearSolverComplex::DirectLinearSolverComplex(EDirectSolverType type) + : LinearSolver(), m_type(type) { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CDirectLinearSolverComplex::~CDirectLinearSolverComplex() +DirectLinearSolverComplex::~DirectLinearSolverComplex() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } -SGVector CDirectLinearSolverComplex::solve( - CLinearOperator* A, SGVector b) +SGVector DirectLinearSolverComplex::solve( + std::shared_ptr> A, SGVector b) { SGVector x(b.vlen); require(A, "Operator is NULL!"); require(A->get_dimension()==b.vlen, "Dimension mismatch!"); - CDenseMatrixOperator *op= - dynamic_cast*>(A); - require(op, "Operator is not CDenseMatrixOperator type!"); + auto op=A->as>(); + require(op, "Operator is not DenseMatrixOperator type!"); SGMatrix mat_A=op->get_matrix_operator(); Map map_A(mat_A.matrix, mat_A.num_rows, mat_A.num_cols); diff --git a/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.h b/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.h index c97d63ef2ce..d070e1ad83f 100644 --- a/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.h +++ b/src/shogun/mathematics/linalg/linsolver/DirectLinearSolverComplex.h @@ -27,21 +27,21 @@ enum EDirectSolverType /** @brief Class that provides a solve method for complex dense-matrix * linear systems */ -class CDirectLinearSolverComplex : public CLinearSolver +class DirectLinearSolverComplex : public LinearSolver { public: /** default constructor */ - CDirectLinearSolverComplex(); + DirectLinearSolverComplex(); /** * constructor * * @param type the type of solver to be used in solve method */ - CDirectLinearSolverComplex(EDirectSolverType type); + DirectLinearSolverComplex(EDirectSolverType type); /** destructor */ - virtual ~CDirectLinearSolverComplex(); + virtual ~DirectLinearSolverComplex(); /** * solve method for solving complex linear systems @@ -50,7 +50,7 @@ class CDirectLinearSolverComplex : public CLinearSolver * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, + virtual SGVector solve(std::shared_ptr> A, SGVector b); /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.cpp b/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.cpp index 98ef13c540c..ad151fbb1a1 100644 --- a/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.cpp @@ -17,22 +17,21 @@ using namespace Eigen; namespace shogun { -CDirectSparseLinearSolver::CDirectSparseLinearSolver() - : CLinearSolver() +DirectSparseLinearSolver::DirectSparseLinearSolver() + : LinearSolver() { } -CDirectSparseLinearSolver::~CDirectSparseLinearSolver() +DirectSparseLinearSolver::~DirectSparseLinearSolver() { } -SGVector CDirectSparseLinearSolver::solve( - CLinearOperator* A, SGVector b) +SGVector DirectSparseLinearSolver::solve( + std::shared_ptr> A, SGVector b) { require(A, "Operator is NULL!"); require(A->get_dimension()==b.vlen, "Dimension mismatch!"); - CSparseMatrixOperator* op - =dynamic_cast*>(A); + auto op=A->as>(); require(op, "Operator is not SparseMatrixOperator type!"); // creating eigen3 Sparse Matrix diff --git a/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.h b/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.h index 32671d79125..037d089972a 100644 --- a/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.h +++ b/src/shogun/mathematics/linalg/linsolver/DirectSparseLinearSolver.h @@ -17,14 +17,14 @@ namespace shogun /** @brief Class that provides a solve method for real sparse-matrix * linear systems using LLT */ -class CDirectSparseLinearSolver : public CLinearSolver +class DirectSparseLinearSolver : public LinearSolver { public: /** default constructor */ - CDirectSparseLinearSolver(); + DirectSparseLinearSolver(); /** destructor */ - virtual ~CDirectSparseLinearSolver(); + virtual ~DirectSparseLinearSolver(); /** * solve method for solving real-valued sparse linear systems @@ -33,7 +33,7 @@ class CDirectSparseLinearSolver : public CLinearSolver * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, + virtual SGVector solve(std::shared_ptr> A, SGVector b); /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.cpp b/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.cpp index 32c232c4351..d542be71cf5 100644 --- a/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.cpp @@ -12,15 +12,15 @@ namespace shogun { template -CIterativeLinearSolver::CIterativeLinearSolver() - : CLinearSolver() +IterativeLinearSolver::IterativeLinearSolver() + : LinearSolver() { init(); } template -CIterativeLinearSolver::CIterativeLinearSolver(bool store_residuals) - : CLinearSolver() +IterativeLinearSolver::IterativeLinearSolver(bool store_residuals) + : LinearSolver() { init(); m_store_residuals=store_residuals; @@ -32,7 +32,7 @@ CIterativeLinearSolver::CIterativeLinearSolver(bool store_residuals) } template -void CIterativeLinearSolver::init() +void IterativeLinearSolver::init() { m_max_iteration_limit=1000; m_relative_tolerence=1E-5; @@ -41,10 +41,10 @@ void CIterativeLinearSolver::init() } template -CIterativeLinearSolver::~CIterativeLinearSolver() +IterativeLinearSolver::~IterativeLinearSolver() { } -template class CIterativeLinearSolver; -template class CIterativeLinearSolver; +template class IterativeLinearSolver; +template class IterativeLinearSolver; } diff --git a/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.h b/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.h index fde9e4d80e3..2beb1fc3181 100644 --- a/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.h +++ b/src/shogun/mathematics/linalg/linsolver/IterativeLinearSolver.h @@ -19,18 +19,18 @@ namespace shogun * conjugate gradient (CG) solvers. provides interface for setting the * iteration limit, relative/absolute tolerence. solve method is abstract. */ -template class CIterativeLinearSolver : public CLinearSolver +template class IterativeLinearSolver : public LinearSolver { public: /** default constructor */ - CIterativeLinearSolver(); + IterativeLinearSolver(); /** one arg constructor */ - CIterativeLinearSolver(bool store_residuals); + IterativeLinearSolver(bool store_residuals); /** destructor */ - virtual ~CIterativeLinearSolver(); + virtual ~IterativeLinearSolver(); /** * abstract solve method for solving real linear systems @@ -39,7 +39,7 @@ template class CIterativeLinearSolver : public CLinearSolve * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, SGVector b) = 0; + virtual SGVector solve(std::shared_ptr> A, SGVector b) = 0; /** set maximum iteration limit */ void set_iteration_limit(index_t iteration_limit) diff --git a/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.cpp b/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.cpp index b14f404f64b..d72c7bd0e2c 100644 --- a/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.cpp @@ -13,25 +13,25 @@ namespace shogun { template -CIterativeShiftedLinearFamilySolver::CIterativeShiftedLinearFamilySolver() - : CIterativeLinearSolver() +IterativeShiftedLinearFamilySolver::IterativeShiftedLinearFamilySolver() + : IterativeLinearSolver() { } template -CIterativeShiftedLinearFamilySolver - ::CIterativeShiftedLinearFamilySolver(bool store_residuals) - : CIterativeLinearSolver(store_residuals) +IterativeShiftedLinearFamilySolver + ::IterativeShiftedLinearFamilySolver(bool store_residuals) + : IterativeLinearSolver(store_residuals) { } template -CIterativeShiftedLinearFamilySolver::~CIterativeShiftedLinearFamilySolver() +IterativeShiftedLinearFamilySolver::~IterativeShiftedLinearFamilySolver() { } template - void CIterativeShiftedLinearFamilySolver::compute_zeta_sh_new( + void IterativeShiftedLinearFamilySolver::compute_zeta_sh_new( const SGVector& zeta_sh_old, const SGVector& zeta_sh_cur, const SGVector& shifts, const T& beta_old, const T& beta_cur, const T& alpha, SGVector& zeta_sh_new, bool negate) @@ -51,14 +51,14 @@ CIterativeShiftedLinearFamilySolver::~CIterativeShiftedLinearFamilySolver // handle division by zero if (denom==static_cast(0.0)) - denom=static_cast(CMath::MACHINE_EPSILON); + denom=static_cast(Math::MACHINE_EPSILON); zeta_sh_new[i]=numer/denom; } } template -void CIterativeShiftedLinearFamilySolver::compute_beta_sh( +void IterativeShiftedLinearFamilySolver::compute_beta_sh( const SGVector& zeta_sh_new, const SGVector& zeta_sh_cur, const T& beta_cur, SGVector& beta_sh_cur) { @@ -71,14 +71,14 @@ void CIterativeShiftedLinearFamilySolver::compute_beta_sh( // handle division by zero if (denom==static_cast(0.0)) - denom=static_cast(CMath::MACHINE_EPSILON); + denom=static_cast(Math::MACHINE_EPSILON); beta_sh_cur[i]=numer/denom; } } template -void CIterativeShiftedLinearFamilySolver::compute_alpha_sh( +void IterativeShiftedLinearFamilySolver::compute_alpha_sh( const SGVector& zeta_sh_cur, const SGVector& zeta_sh_old, const SGVector& beta_sh_old, const T& beta_old, const T& alpha, SGVector& alpha_sh) { @@ -91,12 +91,12 @@ void CIterativeShiftedLinearFamilySolver::compute_alpha_sh( // handle division by zero if (denom==static_cast(0.0)) - denom=static_cast(CMath::MACHINE_EPSILON); + denom=static_cast(Math::MACHINE_EPSILON); alpha_sh[i]=numer/denom; } } -template class CIterativeShiftedLinearFamilySolver; -template class CIterativeShiftedLinearFamilySolver; +template class IterativeShiftedLinearFamilySolver; +template class IterativeShiftedLinearFamilySolver; } diff --git a/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.h b/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.h index 1dc37f6287e..29b04e3c014 100644 --- a/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.h +++ b/src/shogun/mathematics/linalg/linsolver/IterativeShiftedLinearFamilySolver.h @@ -13,7 +13,7 @@ namespace shogun { template class SGVector; -template class CLinearOperator; +template class LinearOperator; /** * @brief abstract template base for CG based solvers to the solution of @@ -28,18 +28,18 @@ template class CLinearOperator; * Reference: Beat Jegerlehner, Krylov space solvers for shifted linear * systems, 1996. */ -template class CIterativeShiftedLinearFamilySolver : public CIterativeLinearSolver +template class IterativeShiftedLinearFamilySolver : public IterativeLinearSolver { public: /** default constructor */ - CIterativeShiftedLinearFamilySolver(); + IterativeShiftedLinearFamilySolver(); /** one arg constructor */ - CIterativeShiftedLinearFamilySolver(bool store_residuals); + IterativeShiftedLinearFamilySolver(bool store_residuals); /** destructor */ - virtual ~CIterativeShiftedLinearFamilySolver(); + virtual ~IterativeShiftedLinearFamilySolver(); /** * abstract solve method for solving real linear systems which computes @@ -49,7 +49,7 @@ template class CIterativeShiftedLinearFamilySolver : public * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, SGVector b) = 0; + virtual SGVector solve(std::shared_ptr> A, SGVector b) = 0; /** * abstract method that solves the shifted family of linear systems, multiples @@ -63,7 +63,7 @@ template class CIterativeShiftedLinearFamilySolver : public * shift */ virtual SGVector solve_shifted_weighted( - CLinearOperator* A, SGVector b, SGVector shifts, + std::shared_ptr> A, SGVector b, SGVector shifts, SGVector weights, bool negate) = 0; /** @return object name */ diff --git a/src/shogun/mathematics/linalg/linsolver/LinearSolver.cpp b/src/shogun/mathematics/linalg/linsolver/LinearSolver.cpp index 7a4d5e2bfcb..e3b0acda497 100644 --- a/src/shogun/mathematics/linalg/linsolver/LinearSolver.cpp +++ b/src/shogun/mathematics/linalg/linsolver/LinearSolver.cpp @@ -8,7 +8,7 @@ namespace shogun { -template class CLinearSolver; -template class CLinearSolver; -template class CLinearSolver; +template class LinearSolver; +template class LinearSolver; +template class LinearSolver; } diff --git a/src/shogun/mathematics/linalg/linsolver/LinearSolver.h b/src/shogun/mathematics/linalg/linsolver/LinearSolver.h index 90b36eb5e16..e34cdf35ee7 100644 --- a/src/shogun/mathematics/linalg/linsolver/LinearSolver.h +++ b/src/shogun/mathematics/linalg/linsolver/LinearSolver.h @@ -13,23 +13,23 @@ namespace shogun { template class SGVector; -template class CLinearOperator; +template class LinearOperator; /** @brief Abstract template base class that provides an abstract solve method * for linear systems, that takes a linear operator \f$A\f$, a vector \f$b\f$, * solves the system \f$Ax=b\f$ and returns the vector \f$x\f$. */ -template class CLinearSolver : public CSGObject +template class LinearSolver : public SGObject { public: /** default constructor */ - CLinearSolver() - : CSGObject() + LinearSolver() + : SGObject() { } /** destructor */ - virtual ~CLinearSolver() + virtual ~LinearSolver() { } @@ -40,7 +40,7 @@ template class CLinearSolver : public CSGObject * @param b the vector of the system * @return the solution vector */ - virtual SGVector solve(CLinearOperator* A, SGVector b) = 0; + virtual SGVector solve(std::shared_ptr> A, SGVector b) = 0; /** @return object name */ virtual const char* get_name() const diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.cpp b/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.cpp index 7b2fd3d2624..bd2a2f957ab 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.cpp @@ -22,37 +22,37 @@ namespace shogun { -CLogDetEstimator::CLogDetEstimator() - : CSGObject() +LogDetEstimator::LogDetEstimator() + : SGObject() { init(); } #ifdef HAVE_LAPACK -CLogDetEstimator::CLogDetEstimator(SGSparseMatrix sparse_mat) - : CSGObject() +LogDetEstimator::LogDetEstimator(SGSparseMatrix sparse_mat) + : SGObject() { init(); - CSparseMatrixOperator* op= - new CSparseMatrixOperator(sparse_mat); + auto op= + std::make_shared>(sparse_mat); float64_t accuracy=1E-5; - CLanczosEigenSolver* eig_solver=new CLanczosEigenSolver(op); - CCGMShiftedFamilySolver* linear_solver=new CCGMShiftedFamilySolver(); + auto eig_solver=std::make_shared(op); + auto linear_solver=std::make_shared(); - m_operator_log = new CLogRationalApproximationCGM( + m_operator_log = std::make_shared( op, eig_solver, linear_solver, accuracy); - SG_REF(m_operator_log); + #ifdef HAVE_COLPACK - m_trace_sampler=new CProbingSampler(op,1,NATURAL,DISTANCE_TWO); + m_trace_sampler=std::make_shared(op,1,NATURAL,DISTANCE_TWO); #else - m_trace_sampler=new CNormalSampler(op->get_dimension()); + m_trace_sampler=std::make_shared(op->get_dimension()); #endif - SG_REF(m_trace_sampler); + io::info( "LogDetEstimator: {} with 1E-5 accuracy, {} as default", @@ -60,50 +60,50 @@ CLogDetEstimator::CLogDetEstimator(SGSparseMatrix sparse_mat) } #endif //HAVE_LAPACK -CLogDetEstimator::CLogDetEstimator( - CTraceSampler* trace_sampler, COperatorFunction* operator_log) - : CSGObject() +LogDetEstimator::LogDetEstimator( + std::shared_ptr trace_sampler, std::shared_ptr> operator_log) + : SGObject() { init(); m_trace_sampler=trace_sampler; - SG_REF(m_trace_sampler); + m_operator_log=operator_log; - SG_REF(m_operator_log); + } -void CLogDetEstimator::init() +void LogDetEstimator::init() { m_trace_sampler=NULL; m_operator_log=NULL; - SG_ADD((CSGObject**)&m_trace_sampler, "trace_sampler", + SG_ADD((std::shared_ptr*)&m_trace_sampler, "trace_sampler", "Trace sampler for the log operator"); - SG_ADD((CSGObject**)&m_operator_log, "operator_log", + SG_ADD((std::shared_ptr*)&m_operator_log, "operator_log", "The log operator function"); } -CLogDetEstimator::~CLogDetEstimator() +LogDetEstimator::~LogDetEstimator() { - SG_UNREF(m_trace_sampler); - SG_UNREF(m_operator_log); + + } -CTraceSampler* CLogDetEstimator::get_trace_sampler(void) const +std::shared_ptr LogDetEstimator::get_trace_sampler(void) const { - SG_REF(m_trace_sampler); + return m_trace_sampler; } -COperatorFunction* CLogDetEstimator::get_operator_function(void) const +std::shared_ptr> LogDetEstimator::get_operator_function(void) const { - SG_REF(m_operator_log); + return m_operator_log; } -SGVector CLogDetEstimator::sample(index_t num_estimates) +SGVector LogDetEstimator::sample(index_t num_estimates) { SG_TRACE("Entering"); io::info("Computing {} log-det estimates", num_estimates); @@ -150,7 +150,7 @@ SGVector CLogDetEstimator::sample(index_t num_estimates) return samples; } -SGMatrix CLogDetEstimator::sample_without_averaging( +SGMatrix LogDetEstimator::sample_without_averaging( index_t num_estimates) { SG_TRACE("Entering..."); diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.h b/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.h index f3848f750f9..ee21f028e97 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.h +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/LogDetEstimator.h @@ -14,21 +14,21 @@ namespace shogun { -class CTraceSampler; -template class COperatorFunction; +class TraceSampler; +template class OperatorFunction; template class SGVector; template class SGMatrix; /** @brief Class to create unbiased estimators of \f$log(\left|C\right|)= * trace(log(C))\f$. For each estimate, it samples trace vectors (one by one) - * and calls solve of COperatorFunction, stores the resulting in + * and calls solve of OperatorFunction, stores the resulting in * a vector, then simply computes running averages over the estimates */ -class CLogDetEstimator : public CSGObject +class LogDetEstimator : public SGObject { public: /** Default constructor */ - CLogDetEstimator(); + LogDetEstimator(); #ifdef HAVE_LAPACK /** @@ -36,16 +36,16 @@ class CLogDetEstimator : public CSGObject * Eigen3 and LAPACK libraries are available. * * Uses the default configuration: - * - CLanczosEigenSolver, + * - LanczosEigenSolver, * - CLogRationalApproximationCGM with 1E-5 accuracy - * - CCGMShiftedFamilySolver, + * - CGMShiftedFamilySolver, * when COLPACK package is available then CProbingsampler with power = 1, * EOrderingVariant NATURAL, EColoringVariant DISTANCE_TWO is used, * Otherwise CNormalsampler is used. * * @param sparse_mat the input sparse matrix */ - CLogDetEstimator(SGSparseMatrix sparse_mat); + LogDetEstimator(SGSparseMatrix sparse_mat); #endif /** @@ -54,12 +54,12 @@ class CLogDetEstimator : public CSGObject * @param trace_sampler the trace sampler * @param operator_log the operator function */ - CLogDetEstimator( - CTraceSampler* trace_sampler, - COperatorFunction* operator_log); + LogDetEstimator( + std::shared_ptr trace_sampler, + std::shared_ptr> operator_log); /** Destructor */ - virtual ~CLogDetEstimator(); + virtual ~LogDetEstimator(); /** * Method that gives num_estimates number of log-det estimates with running @@ -86,17 +86,17 @@ class CLogDetEstimator : public CSGObject } /** @return trace sampler */ - CTraceSampler* get_trace_sampler(void) const; + std::shared_ptr get_trace_sampler(void) const; /** @return operator function */ - COperatorFunction* get_operator_function(void) const; + std::shared_ptr> get_operator_function(void) const; private: /** the trace sampler */ - CTraceSampler* m_trace_sampler; + std::shared_ptr m_trace_sampler; /** the linear operator function, which is log in this case */ - COperatorFunction* m_operator_log; + std::shared_ptr> m_operator_log; /** initialize with default values and register params */ void init(); diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.cpp b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.cpp index 7265c866381..e6755200abe 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.cpp @@ -23,29 +23,29 @@ using namespace Eigen; namespace shogun { - CDenseMatrixExactLog::CDenseMatrixExactLog() - : COperatorFunction(nullptr, OF_LOG) + DenseMatrixExactLog::DenseMatrixExactLog() + : OperatorFunction(nullptr, OF_LOG) { } - CDenseMatrixExactLog::CDenseMatrixExactLog( - CDenseMatrixOperator* op) - : COperatorFunction((CLinearOperator*)op, OF_LOG) + DenseMatrixExactLog::DenseMatrixExactLog( + std::shared_ptr> op) + : OperatorFunction(op->as>(), OF_LOG) { } - CDenseMatrixExactLog::~CDenseMatrixExactLog() + DenseMatrixExactLog::~DenseMatrixExactLog() { } #if EIGEN_VERSION_AT_LEAST(3,1,0) -void CDenseMatrixExactLog::precompute() +void DenseMatrixExactLog::precompute() { SG_TRACE("Entering..."); // check for proper downcast - CDenseMatrixOperator* op - =dynamic_cast*>(m_linear_operator); + auto op + =m_linear_operator->as>(); require(op, "Operator not an instance of DenseMatrixOperator!"); SGMatrix m=op->get_matrix_operator(); @@ -62,25 +62,25 @@ void CDenseMatrixExactLog::precompute() // the log(C) is also a linear operator here // reset the operator of this function with log(C) - SG_UNREF(m_linear_operator); - m_linear_operator=new CDenseMatrixOperator(log_m); - SG_REF(m_linear_operator); + + m_linear_operator=std::make_shared>(log_m); + SG_TRACE("Leaving..."); } #else -void CDenseMatrixExactLog::precompute() +void DenseMatrixExactLog::precompute() { io::warn("Eigen3.1.0 or later required!"); } #endif // EIGEN_VERSION_AT_LEAST(3,1,0) -float64_t CDenseMatrixExactLog::compute(SGVector sample) const +float64_t DenseMatrixExactLog::compute(SGVector sample) const { SG_TRACE("Entering..."); - CDenseMatrixOperator* m_log_operator = - dynamic_cast*>(m_linear_operator); + auto m_log_operator = + m_linear_operator->as>(); SGVector vec = m_log_operator->apply(sample); float64_t result = linalg::dot(sample, vec); diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.h b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.h index 038c9626fff..a25ec48181e 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.h +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/DenseMatrixExactLog.h @@ -15,26 +15,26 @@ namespace shogun { template class SGVector; -template class CDenseMatrixOperator; +template class DenseMatrixOperator; /** @brief Class that generates jobs for computing logarithm of * a dense matrix linear operator */ -class CDenseMatrixExactLog : public COperatorFunction +class DenseMatrixExactLog : public OperatorFunction { public: /** default constructor */ - CDenseMatrixExactLog(); + DenseMatrixExactLog(); /** * constructor * * @param op the dense matrix linear operator for this operator function */ - CDenseMatrixExactLog(CDenseMatrixOperator* op); + DenseMatrixExactLog(std::shared_ptr> op); /** destructor */ - virtual ~CDenseMatrixExactLog(); + virtual ~DenseMatrixExactLog(); /** * precompute method that computes the log of the linear operator using diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.cpp b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.cpp index 2a0b31147bd..441f9d208b4 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.cpp @@ -18,39 +18,39 @@ using namespace Eigen; namespace shogun { - CLogRationalApproximationCGM::CLogRationalApproximationCGM() - : CRationalApproximation(nullptr, nullptr, 0, OF_LOG) + LogRationalApproximationCGM::LogRationalApproximationCGM() + : RationalApproximation(nullptr, nullptr, 0, OF_LOG) { init(); } -CLogRationalApproximationCGM::CLogRationalApproximationCGM( - CLinearOperator* linear_operator, CEigenSolver* eigen_solver, - CCGMShiftedFamilySolver* linear_solver, float64_t desired_accuracy) - : CRationalApproximation( +LogRationalApproximationCGM::LogRationalApproximationCGM( + std::shared_ptr> linear_operator, std::shared_ptr eigen_solver, + std::shared_ptr linear_solver, float64_t desired_accuracy) + : RationalApproximation( linear_operator, eigen_solver, desired_accuracy, OF_LOG) { init(); m_linear_solver=linear_solver; - SG_REF(m_linear_solver); + } -void CLogRationalApproximationCGM::init() +void LogRationalApproximationCGM::init() { m_linear_solver=NULL; - SG_ADD((CSGObject**)&m_linear_solver, "linear_solver", + SG_ADD((std::shared_ptr*)&m_linear_solver, "linear_solver", "Linear solver for complex systems"); } -CLogRationalApproximationCGM::~CLogRationalApproximationCGM() +LogRationalApproximationCGM::~LogRationalApproximationCGM() { - SG_UNREF(m_linear_solver); + } float64_t -CLogRationalApproximationCGM::compute(SGVector sample) const +LogRationalApproximationCGM::compute(SGVector sample) const { SG_TRACE("Entering"); require(sample.vector, "Sample is not initialized!"); @@ -61,7 +61,7 @@ CLogRationalApproximationCGM::compute(SGVector sample) const SGVector vec = m_linear_solver->solve_shifted_weighted( m_linear_operator, sample, m_shifts, m_weights, true); - // Take negative (see CRationalApproximation for the formula) + // Take negative (see RationalApproximation for the formula) Map v(vec.vector, vec.vlen); v = -v; diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.h b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.h index a55029c38c0..440f92980d1 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.h +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationCGM.h @@ -15,8 +15,8 @@ namespace shogun { template class SGVector; -template class CLinearOperator; -class CCGMShiftedFamilySolver; +template class LinearOperator; +class CGMShiftedFamilySolver; /** @brief Implementaion of rational approximation of a operator-function times * vector where the operator function is log of a linear operator. Each complex @@ -24,11 +24,11 @@ class CCGMShiftedFamilySolver; * log times vector expression are solved at once with a shifted linear-family * solver. */ -class CLogRationalApproximationCGM : public CRationalApproximation +class LogRationalApproximationCGM : public RationalApproximation { public: /** default constructor */ - CLogRationalApproximationCGM(); + LogRationalApproximationCGM(); /** * Constructor. Number of shifts will be computed using a specified accuracy. @@ -41,14 +41,14 @@ class CLogRationalApproximationCGM : public CRationalApproximation * @param desired_accuracy desired error bound on approximation. Computes * the number of shifts automatically */ - CLogRationalApproximationCGM( - CLinearOperator* linear_operator, - CEigenSolver* eigen_solver, - CCGMShiftedFamilySolver* linear_solver, + LogRationalApproximationCGM( + std::shared_ptr> linear_operator, + std::shared_ptr eigen_solver, + std::shared_ptr linear_solver, float64_t desired_accuracy); /** destructor */ - virtual ~CLogRationalApproximationCGM(); + virtual ~LogRationalApproximationCGM(); /** * method that solves the result for a sample @@ -63,7 +63,7 @@ class CLogRationalApproximationCGM : public CRationalApproximation private: /** the linear solver for solving complex systems */ - CCGMShiftedFamilySolver* m_linear_solver; + std::shared_ptr m_linear_solver; /** initialize with default values and register params */ void init(); diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.cpp b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.cpp index 9876755be87..256636e8345 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.cpp @@ -20,40 +20,40 @@ using namespace Eigen; namespace shogun { - CLogRationalApproximationIndividual::CLogRationalApproximationIndividual() - : CRationalApproximation(nullptr, nullptr, 0, OF_LOG) + LogRationalApproximationIndividual::LogRationalApproximationIndividual() + : RationalApproximation(nullptr, nullptr, 0, OF_LOG) { init(); } -CLogRationalApproximationIndividual::CLogRationalApproximationIndividual( - CMatrixOperator* linear_operator, CEigenSolver* eigen_solver, - CLinearSolver* linear_solver, +LogRationalApproximationIndividual::LogRationalApproximationIndividual( + std::shared_ptr> linear_operator, std::shared_ptr eigen_solver, + std::shared_ptr> linear_solver, float64_t desired_accuracy) - : CRationalApproximation( + : RationalApproximation( linear_operator, eigen_solver, desired_accuracy, OF_LOG) { init(); m_linear_solver=linear_solver; - SG_REF(m_linear_solver); + } -void CLogRationalApproximationIndividual::init() +void LogRationalApproximationIndividual::init() { m_linear_solver=NULL; - SG_ADD((CSGObject**)&m_linear_solver, "linear_solver", + SG_ADD((std::shared_ptr*)&m_linear_solver, "linear_solver", "Linear solver for complex systems"); } -CLogRationalApproximationIndividual::~CLogRationalApproximationIndividual() +LogRationalApproximationIndividual::~LogRationalApproximationIndividual() { - SG_UNREF(m_linear_solver); + } float64_t -CLogRationalApproximationIndividual::compute(SGVector sample) const +LogRationalApproximationIndividual::compute(SGVector sample) const { SG_TRACE("Entering.."); require(sample.vector, "Sample is not initialized!"); @@ -63,30 +63,30 @@ CLogRationalApproximationIndividual::compute(SGVector sample) const enum typeID {DENSE=1, SPARSE, UNKNOWN} operator_type=UNKNOWN; // create a complex copy of the matrix linear operator - CMatrixOperator* complex_op=NULL; - if (typeid(*m_linear_operator)==typeid(CDenseMatrixOperator)) + std::shared_ptr> complex_op=NULL; + if (typeid(*m_linear_operator)==typeid(DenseMatrixOperator)) { operator_type=DENSE; - CDenseMatrixOperator* op - =dynamic_cast*>(m_linear_operator); + auto op + =m_linear_operator->as>(); require(op->get_matrix_operator().matrix, "Matrix is not initialized!"); // create complex dense matrix operator - complex_op=static_cast*>(*op); + complex_op=std::shared_ptr>(static_cast*>(*op)); } - else if (typeid(*m_linear_operator)==typeid(CSparseMatrixOperator)) + else if (typeid(*m_linear_operator)==typeid(SparseMatrixOperator)) { operator_type=SPARSE; - CSparseMatrixOperator* op - =dynamic_cast*>(m_linear_operator); + auto op + =m_linear_operator->as>(); require(op->get_matrix_operator().sparse_matrix, "Matrix is not initialized!"); // create complex sparse matrix operator - complex_op=static_cast*>(*op); + complex_op=std::shared_ptr>(static_cast*>(*op)); } else { @@ -99,17 +99,17 @@ CLogRationalApproximationIndividual::compute(SGVector sample) const for (index_t i=0; i* shifted_op=NULL; + std::shared_ptr> shifted_op=NULL; switch(operator_type) { case DENSE: - shifted_op=new CDenseMatrixOperator - (*dynamic_cast*>(complex_op)); + shifted_op=std::make_shared> + (*(complex_op->as>())); break; case SPARSE: - shifted_op=new CSparseMatrixOperator - (*dynamic_cast*>(complex_op)); + shifted_op=std::make_shared> + (*(complex_op->as>())); break; default: break; @@ -122,7 +122,7 @@ CLogRationalApproximationIndividual::compute(SGVector sample) const SGVector vec = m_linear_solver->solve(shifted_op, sample); // multiply with the weight using Eigen3 and take negative - // (see CRationalApproximation for the formula) + // (see RationalApproximation for the formula) Map v(vec.vector, vec.vlen); v *= m_weights[i]; v = -v; diff --git a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.h b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.h index 7f84f9047ca..ccdaed560b1 100644 --- a/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.h +++ b/src/shogun/mathematics/linalg/ratapprox/logdet/opfunc/LogRationalApproximationIndividual.h @@ -15,8 +15,8 @@ namespace shogun { template class SGVector; -template class CMatrixOperator; -template class CLinearSolver; +template class MatrixOperator; +template class LinearSolver; /** @brief Implementaion of rational approximation of a operator-function times * vector where the operator function is log of a dense-matrix. Each complex @@ -24,11 +24,11 @@ template class CLinearSolver; * log times vector expression are solved individually with a complex linear * solver. */ -class CLogRationalApproximationIndividual : public CRationalApproximation +class LogRationalApproximationIndividual : public RationalApproximation { public: /** default constructor */ - CLogRationalApproximationIndividual(); + LogRationalApproximationIndividual(); /** * Constructor. Number of shifts will be computed using a specified accuracy. @@ -41,14 +41,14 @@ class CLogRationalApproximationIndividual : public CRationalApproximation * @param desired_accuracy desired error bound on approximation. Computes * the number of shifts automatically */ - CLogRationalApproximationIndividual( - CMatrixOperator* linear_operator, - CEigenSolver* eigen_solver, - CLinearSolver* linear_solver, + LogRationalApproximationIndividual( + std::shared_ptr> linear_operator, + std::shared_ptr eigen_solver, + std::shared_ptr> linear_solver, float64_t desired_accuracy); /** destructor */ - virtual ~CLogRationalApproximationIndividual(); + virtual ~LogRationalApproximationIndividual(); /** *method that solves the result for a sample @@ -63,7 +63,7 @@ class CLogRationalApproximationIndividual : public CRationalApproximation private: /** the linear solver for solving complex systems */ - CLinearSolver* m_linear_solver; + std::shared_ptr> m_linear_solver; /** initialize with default values and register params */ void init(); diff --git a/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.cpp b/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.cpp index 3444bdd1177..cd8d0877590 100644 --- a/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.cpp @@ -8,18 +8,18 @@ namespace shogun { -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; -template class COperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; +template class OperatorFunction; } diff --git a/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.h b/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.h index caa6db0dba8..5fa42a633cf 100644 --- a/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.h +++ b/src/shogun/mathematics/linalg/ratapprox/opfunc/OperatorFunction.h @@ -25,17 +25,17 @@ enum EOperatorFunction }; template class SGVector; -template class CLinearOperator; +template class LinearOperator; /** @brief Abstract template base class for computing \f$s^{T} f(C) s\f$ for a * linear operator C and a vector s. */ -template class COperatorFunction : public CSGObject +template class OperatorFunction : public SGObject { public: /** default constructor */ - COperatorFunction() - : CSGObject(), + OperatorFunction() + : SGObject(), m_function_type(OF_UNDEFINED) { init(); @@ -47,25 +47,23 @@ template class COperatorFunction : public CSGObject * @param op the linear operator of this operator function * @param type the type of the operator function (sqrt, log, etc) */ - COperatorFunction(CLinearOperator* op, + OperatorFunction(std::shared_ptr> op, EOperatorFunction type=OF_UNDEFINED) - : CSGObject(), + : SGObject(), m_function_type(type) { init(); m_linear_operator=op; - SG_REF(m_linear_operator); } /** destructor */ - virtual ~COperatorFunction() + virtual ~OperatorFunction() { - SG_UNREF(m_linear_operator); } /** @return the operator */ - CLinearOperator* get_operator() const + std::shared_ptr> get_operator() const { return m_linear_operator; } @@ -88,7 +86,7 @@ template class COperatorFunction : public CSGObject } protected: /** the linear operator */ - CLinearOperator* m_linear_operator; + std::shared_ptr> m_linear_operator; /** the linear operator function type */ const EOperatorFunction m_function_type; @@ -99,7 +97,7 @@ template class COperatorFunction : public CSGObject { m_linear_operator=NULL; - SG_ADD((CSGObject**)&m_linear_operator, "linear_operator", + SG_ADD((std::shared_ptr*)&m_linear_operator, "linear_operator", "Linear operator of this operator function"); } }; diff --git a/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.cpp b/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.cpp index 191e421689b..9401fd6b821 100644 --- a/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.cpp @@ -19,44 +19,44 @@ namespace shogun { -CRationalApproximation::CRationalApproximation() - : COperatorFunction() +RationalApproximation::RationalApproximation() + : OperatorFunction() { init(); SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CRationalApproximation::CRationalApproximation( - CLinearOperator* linear_operator, CEigenSolver* eigen_solver, +RationalApproximation::RationalApproximation( + std::shared_ptr> linear_operator, std::shared_ptr eigen_solver, float64_t desired_accuracy, EOperatorFunction function_type) - : COperatorFunction(linear_operator, function_type) + : OperatorFunction(linear_operator, function_type) { init(); m_eigen_solver=eigen_solver; - SG_REF(m_eigen_solver); + m_desired_accuracy=desired_accuracy; SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CRationalApproximation::~CRationalApproximation() +RationalApproximation::~RationalApproximation() { - SG_UNREF(m_eigen_solver); + SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } -void CRationalApproximation::init() +void RationalApproximation::init() { m_eigen_solver=NULL; m_constant_multiplier=0.0; m_num_shifts=0; m_desired_accuracy=0.0; - SG_ADD((CSGObject**)&m_eigen_solver, "eigen_solver", + SG_ADD((std::shared_ptr*)&m_eigen_solver, "eigen_solver", "Eigen solver for computing extremal eigenvalues"); SG_ADD(&m_shifts, "complex_shifts", "Complex shifts in the linear system"); @@ -73,32 +73,32 @@ void CRationalApproximation::init() "Desired accuracy of the rational approximation"); } -SGVector CRationalApproximation::get_shifts() const +SGVector RationalApproximation::get_shifts() const { return m_shifts; } -SGVector CRationalApproximation::get_weights() const +SGVector RationalApproximation::get_weights() const { return m_weights; } -float64_t CRationalApproximation::get_constant_multiplier() const +float64_t RationalApproximation::get_constant_multiplier() const { return m_constant_multiplier; } -index_t CRationalApproximation::get_num_shifts() const +index_t RationalApproximation::get_num_shifts() const { return m_num_shifts; } -void CRationalApproximation::set_num_shifts(index_t num_shifts) +void RationalApproximation::set_num_shifts(index_t num_shifts) { m_num_shifts=num_shifts; } -void CRationalApproximation::precompute() +void RationalApproximation::precompute() { // compute extremal eigenvalues m_eigen_solver->compute(); @@ -116,7 +116,7 @@ void CRationalApproximation::precompute() compute_shifts_weights_const(); } -int32_t CRationalApproximation::compute_num_shifts_from_accuracy() +int32_t RationalApproximation::compute_num_shifts_from_accuracy() { require(m_desired_accuracy>0, "Desired accuracy must be positive but is {}", m_desired_accuracy); @@ -134,7 +134,7 @@ int32_t CRationalApproximation::compute_num_shifts_from_accuracy() return num_shifts; } -void CRationalApproximation::compute_shifts_weights_const() +void RationalApproximation::compute_shifts_weights_const() { float64_t PI=M_PI; @@ -146,8 +146,8 @@ void CRationalApproximation::compute_shifts_weights_const() // $(\lambda_{M}*\lambda_{m})^{frac{1}{4}}$ for the rest of the // calculation where $lambda_{M}$ and $\lambda_{m} are the maximum // and minimum eigenvalue respectively - float64_t m_div=CMath::pow(max_eig/min_eig, 0.25); - float64_t m_mult=CMath::pow(max_eig*min_eig, 0.25); + float64_t m_div=Math::pow(max_eig/min_eig, 0.25); + float64_t m_mult=Math::pow(max_eig*min_eig, 0.25); // k=$\frac{(\frac{\lambda_{M}}{\lambda_{m}})^\frac{1}{4}-1} // {(\frac{\lambda_{M}}{\lambda_{m}})^\frac{1}{4}+1}$ @@ -157,7 +157,7 @@ void CRationalApproximation::compute_shifts_weights_const() // compute K and K' float64_t K=0.0, Kp=0.0; #ifdef USE_GPL_SHOGUN - CJacobiEllipticFunctions::ellipKKp(L, K, Kp); + JacobiEllipticFunctions::ellipKKp(L, K, Kp); #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN @@ -168,7 +168,7 @@ void CRationalApproximation::compute_shifts_weights_const() // compute Jacobi elliptic functions sn, cn, dn and compute shifts, weights // using conformal mapping of the quadrature rule for discretization of the // contour integral - float64_t m=CMath::sq(k); + float64_t m=Math::sq(k); // allocate memory for shifts m_shifts=SGVector(m_num_shifts); @@ -180,16 +180,16 @@ void CRationalApproximation::compute_shifts_weights_const() complex128_t sn, cn, dn; #ifdef USE_GPL_SHOGUN - CJacobiEllipticFunctions::ellipJC(t, m, sn, cn, dn); + JacobiEllipticFunctions::ellipJC(t, m, sn, cn, dn); #else gpl_only(SOURCE_LOCATION); #endif //USE_GPL_SHOGUN complex128_t w=m_mult*(1.0+k*sn)/(1.0-k*sn); - complex128_t dzdt=cn*dn/CMath::sq(1.0/k-sn); + complex128_t dzdt=cn*dn/Math::sq(1.0/k-sn); // compute shifts and weights as per Hale et. al. (2008) [2] - m_shifts[i]=CMath::sq(w); + m_shifts[i]=Math::sq(w); switch (m_function_type) { diff --git a/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.h b/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.h index 357cd3b25a8..db2f0e46582 100644 --- a/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.h +++ b/src/shogun/mathematics/linalg/ratapprox/opfunc/RationalApproximation.h @@ -14,8 +14,8 @@ namespace shogun { template class SGVector; -template class CLinearOperator; -class CEigenSolver; +template class LinearOperator; +class EigenSolver; /** @brief Abstract base class of the rational approximation of a function of a * linear operator (A) times vector (v) using Cauchy's integral formula - @@ -54,11 +54,11 @@ class CEigenSolver; * adapted from KRYLSTAT (Copyright 2011 by Erlend Aune ) * under GPL2+. See https://github.com/Froskekongen/KRYLSTAT. */ -class CRationalApproximation : public COperatorFunction +class RationalApproximation : public OperatorFunction { public: /** default constructor */ - CRationalApproximation(); + RationalApproximation(); /** * Constructor. Number of shifts will be computed using a specified accuracy. @@ -71,14 +71,14 @@ class CRationalApproximation : public COperatorFunction * the number of shifts automatically * @param function_type operator function type */ - CRationalApproximation( - CLinearOperator* linear_operator, - CEigenSolver* eigen_solver, + RationalApproximation( + std::shared_ptr> linear_operator, + std::shared_ptr eigen_solver, float64_t desired_accuracy, EOperatorFunction function_type); /** destructor */ - virtual ~CRationalApproximation(); + virtual ~RationalApproximation(); /** * precompute method that computes extremal eigenvalues using the eigensolver @@ -131,7 +131,7 @@ class CRationalApproximation : public COperatorFunction protected: /** the eigen solver for computing extremal eigenvalues */ - CEigenSolver* m_eigen_solver; + std::shared_ptr m_eigen_solver; /** complex shifts in the systems coming from rational approximation */ SGVector m_shifts; diff --git a/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.cpp b/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.cpp index c46f3af21dd..3f410987e8f 100644 --- a/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.cpp @@ -13,29 +13,29 @@ namespace shogun { -CNormalSampler::CNormalSampler() - : RandomMixin() +NormalSampler::NormalSampler() + : RandomMixin() { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CNormalSampler::CNormalSampler(index_t dimension) - : RandomMixin(dimension) +NormalSampler::NormalSampler(index_t dimension) + : RandomMixin(dimension) { SG_TRACE("{} created ({})", this->get_name(), fmt::ptr(this)); } -CNormalSampler::~CNormalSampler() +NormalSampler::~NormalSampler() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } -void CNormalSampler::precompute() +void NormalSampler::precompute() { m_num_samples=1; } -SGVector CNormalSampler::sample(index_t idx) const +SGVector NormalSampler::sample(index_t idx) const { // ignore idx since it doesnt matter, all samples are independent SGVector s(m_dimension); diff --git a/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.h b/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.h index fdc7cdde99d..0e17b5571a5 100644 --- a/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.h +++ b/src/shogun/mathematics/linalg/ratapprox/tracesampler/NormalSampler.h @@ -17,19 +17,19 @@ template class SGVector; /** @brief Class that provides a sample method for Gaussian samples */ -class CNormalSampler : public RandomMixin +class NormalSampler : public RandomMixin { public: /** default constructor */ - CNormalSampler(); + NormalSampler(); /** constructor * @param dimension the dimension of the Gaussian sample vectors ~(0,I) */ - CNormalSampler(index_t dimension); + NormalSampler(index_t dimension); /** destructor */ - virtual ~CNormalSampler(); + virtual ~NormalSampler(); /** method that generates the samples * @param idx the index (this is effectively ignored) diff --git a/src/shogun/mathematics/linalg/ratapprox/tracesampler/ProbingSampler.cpp b/src/shogun/mathematics/linalg/ratapprox/tracesampler/ProbingSampler.cpp index 1dddba61f27..2b6f49f1f48 100644 --- a/src/shogun/mathematics/linalg/ratapprox/tracesampler/ProbingSampler.cpp +++ b/src/shogun/mathematics/linalg/ratapprox/tracesampler/ProbingSampler.cpp @@ -25,15 +25,15 @@ using namespace ColPack; namespace shogun { -CProbingSampler::CProbingSampler() : RandomMixin() +ProbingSampler::ProbingSampler() : RandomMixin() { init(); } -CProbingSampler::CProbingSampler( - CSparseMatrixOperator* matrix_operator, int64_t power, +ProbingSampler::ProbingSampler( + std::shared_ptr> matrix_operator, int64_t power, EOrderingVariant ordering, EColoringVariant coloring) - : RandomMixin(matrix_operator->get_dimension()) + : RandomMixin(matrix_operator->get_dimension()) { init(); @@ -41,11 +41,9 @@ CProbingSampler::CProbingSampler( m_matrix_operator=matrix_operator; m_ordering=ordering; m_coloring=coloring; - - SG_REF(m_matrix_operator); } -void CProbingSampler::init() +void ProbingSampler::init() { m_matrix_operator=NULL; m_power=1; @@ -53,6 +51,7 @@ void CProbingSampler::init() m_coloring=DISTANCE_TWO; m_is_precomputed=false; +/* SG_ADD(&m_coloring_vector, "coloring_vector", "the coloring vector generated" " from coloring"); @@ -61,27 +60,27 @@ void CProbingSampler::init() SG_ADD(&m_is_precomputed, "is_precomputed", "flag that is true if already precomputed"); - SG_ADD((CSGObject**)&m_matrix_operator, "matrix_operator", + SG_ADD((std::shared_ptr*)&m_matrix_operator, "matrix_operator", "the sparse-matrix linear opeator for coloring"); + */ } -CProbingSampler::~CProbingSampler() +ProbingSampler::~ProbingSampler() { - SG_UNREF(m_matrix_operator); } -void CProbingSampler::set_coloring_vector(SGVector coloring_vector) +void ProbingSampler::set_coloring_vector(SGVector coloring_vector) { m_coloring_vector=coloring_vector; m_is_precomputed=true; } -SGVector CProbingSampler::get_coloring_vector() const +SGVector ProbingSampler::get_coloring_vector() const { return m_coloring_vector; } -void CProbingSampler::precompute() +void ProbingSampler::precompute() { SG_TRACE("Entering"); @@ -184,7 +183,7 @@ void CProbingSampler::precompute() SG_TRACE("Leaving"); } -SGVector CProbingSampler::sample(index_t idx) const +SGVector ProbingSampler::sample(index_t idx) const { require(idx class SGVector; -template class CSparseMatrixOperator; +template class SparseMatrixOperator; /** @brief Class that provides a sample method for probing vector using * greedy graph coloring. It depends on an external library ColPack (used * under GPL2+) for graph coloring related things. */ -class CProbingSampler : public RandomMixin +class ProbingSampler : public RandomMixin { public: /** default constructor */ - CProbingSampler(); + ProbingSampler(); /** * constructor @@ -64,12 +64,12 @@ class CProbingSampler : public RandomMixin * @param ordering the ordering variant * @param coloring the coloring variant */ - CProbingSampler(CSparseMatrixOperator* matrix_operator, + ProbingSampler(std::shared_ptr> matrix_operator, int64_t power=1, EOrderingVariant ordering=NATURAL, EColoringVariant coloring=DISTANCE_TWO); /** destructor */ - virtual ~CProbingSampler(); + virtual ~ProbingSampler(); /** * set the coloring vector @@ -99,7 +99,7 @@ class CProbingSampler : public RandomMixin private: /** the matrix operator */ - CSparseMatrixOperator* m_matrix_operator; + std::shared_ptr> m_matrix_operator; /** power of the matrix */ int64_t m_power; diff --git a/src/shogun/mathematics/linalg/ratapprox/tracesampler/TraceSampler.h b/src/shogun/mathematics/linalg/ratapprox/tracesampler/TraceSampler.h index a17fd149b62..7bcafbaee53 100644 --- a/src/shogun/mathematics/linalg/ratapprox/tracesampler/TraceSampler.h +++ b/src/shogun/mathematics/linalg/ratapprox/tracesampler/TraceSampler.h @@ -17,12 +17,12 @@ template class SGVector; /** @brief Abstract template base class that provides an interface for sampling * the trace of a linear operator using an abstract sample method */ -class CTraceSampler : public CSGObject +class TraceSampler : public SGObject { public: /** default constructor */ - CTraceSampler() - : CSGObject() + TraceSampler() + : SGObject() { init(); @@ -34,8 +34,8 @@ class CTraceSampler : public CSGObject * * @param dimension the dimension of the sample vectors */ - CTraceSampler(index_t dimension) - : CSGObject() + TraceSampler(index_t dimension) + : SGObject() { init(); @@ -45,7 +45,7 @@ class CTraceSampler : public CSGObject } /** destructor */ - virtual ~CTraceSampler() + virtual ~TraceSampler() { SG_TRACE("{} destroyed ({})", this->get_name(), fmt::ptr(this)); } diff --git a/src/shogun/metric/LMNN.cpp b/src/shogun/metric/LMNN.cpp index ed251d36c80..d3a03a6eb3e 100644 --- a/src/shogun/metric/LMNN.cpp +++ b/src/shogun/metric/LMNN.cpp @@ -14,15 +14,15 @@ using namespace shogun; -CLMNN::CLMNN() +LMNN::LMNN() { init(); - m_statistics = new CLMNNStatistics(); - SG_REF(m_statistics); + m_statistics = std::make_shared(); + } -CLMNN::CLMNN(CFeatures* features, CMulticlassLabels* labels, int32_t k) +LMNN::LMNN(std::shared_ptr features, std::shared_ptr labels, int32_t k) { init(); @@ -30,46 +30,46 @@ CLMNN::CLMNN(CFeatures* features, CMulticlassLabels* labels, int32_t k) m_labels = labels; m_k = k; - SG_REF(m_features) - SG_REF(m_labels) - m_statistics = new CLMNNStatistics(); - SG_REF(m_statistics); + + + m_statistics = std::make_shared(); + } -CLMNN::~CLMNN() +LMNN::~LMNN() { - SG_UNREF(m_features) - SG_UNREF(m_labels) - SG_UNREF(m_statistics); + + + } -const char* CLMNN::get_name() const +const char* LMNN::get_name() const { return "LMNN"; } -void CLMNN::train(SGMatrix init_transform) +void LMNN::train(SGMatrix init_transform) { - SG_TRACE("Entering CLMNN::train()."); + SG_TRACE("Entering LMNN::train()."); // Check training data and arguments, initializing, if necessary, init_transform - CLMNNImpl::check_training_setup(m_features, m_labels, init_transform, m_k); + LMNNImpl::check_training_setup(m_features, m_labels, init_transform, m_k); // Initializations // cast is safe, check_training_setup ensures features are dense - CDenseFeatures* x = static_cast*>(m_features); - CMulticlassLabels* y = multiclass_labels(m_labels); + auto x = m_features->as>(); + auto y = multiclass_labels(m_labels); SG_DEBUG("{} input vectors with {} dimensions.", x->get_num_vectors(), x->get_num_features()); auto& L = init_transform; // Compute target or genuine neighbours SG_DEBUG("Finding target nearest neighbors.") - SGMatrix target_nn = CLMNNImpl::find_target_nn(x, y, m_k); + SGMatrix target_nn = LMNNImpl::find_target_nn(x, y, m_k); // Initialize (sub-)gradient SG_DEBUG("Summing outer products for (sub-)gradient initialization.") - auto gradient = CLMNNImpl::sum_outer_products(x, target_nn); + auto gradient = LMNNImpl::sum_outer_products(x, target_nn); linalg::scale(gradient, gradient, 1 - m_regularization); // Value of the objective function at every iteration SGVector obj(m_maxiter); @@ -93,15 +93,15 @@ void CLMNN::train(SGMatrix init_transform) { // Find current set of impostors SG_DEBUG("Finding impostors.") - cur_impostors = CLMNNImpl::find_impostors(x,y,L,target_nn,iter,m_correction); + cur_impostors = LMNNImpl::find_impostors(x,y,L,target_nn,iter,m_correction); SG_DEBUG("Found {} impostors in the current set.", cur_impostors.size()) // (Sub-) gradient computation SG_DEBUG("Updating gradient.") - CLMNNImpl::update_gradient(x, gradient, cur_impostors, prev_impostors, m_regularization); + LMNNImpl::update_gradient(x, gradient, cur_impostors, prev_impostors, m_regularization); // Take gradient step SG_DEBUG("Taking gradient step.") - CLMNNImpl::gradient_step(L, gradient, stepsize, m_diagonal); + LMNNImpl::gradient_step(L, gradient, stepsize, m_diagonal); // Compute the objective, trace of Mahalanobis distance matrix (L squared) times the gradient // plus the number of current impostors to account for the margin @@ -111,10 +111,10 @@ void CLMNN::train(SGMatrix init_transform) linalg::trace_dot(linalg::matrix_prod(L, L, true, false), gradient); // Correct step size - CLMNNImpl::correct_stepsize(stepsize, obj, iter); + LMNNImpl::correct_stepsize(stepsize, obj, iter); // Check termination criterion - stop = CLMNNImpl::check_termination(stepsize, obj, iter, m_maxiter, m_stepsize_threshold, m_obj_threshold); + stop = LMNNImpl::check_termination(stepsize, obj, iter, m_maxiter, m_stepsize_threshold, m_obj_threshold); // Update iteration counter iter = iter + 1; @@ -141,126 +141,123 @@ void CLMNN::train(SGMatrix init_transform) SGMatrix::clone_matrix(L.matrix, nfeats, nfeats); m_linear_transform = SGMatrix(cloned_data, nfeats, nfeats); - SG_TRACE("Leaving CLMNN::train()."); + SG_TRACE("Leaving LMNN::train()."); } -SGMatrix CLMNN::get_linear_transform() const +SGMatrix LMNN::get_linear_transform() const { return m_linear_transform; } -CDistance* CLMNN::get_distance() const +std::shared_ptr LMNN::get_distance() const { // Compute Mahalanobis distance matrix M = L^T*L auto M = linalg::matrix_prod( m_linear_transform, m_linear_transform, true, false); // Create custom Mahalanobis distance with matrix M associated with the training features - auto distance = new CCustomMahalanobisDistance(m_features, m_features, M); - SG_REF(distance) - - return distance; + return std::make_shared(m_features, m_features, M); } -int32_t CLMNN::get_k() const +int32_t LMNN::get_k() const { return m_k; } -void CLMNN::set_k(const int32_t k) +void LMNN::set_k(const int32_t k) { require(k>0, "The number of target neighbors per example must be larger than zero"); m_k = k; } -float64_t CLMNN::get_regularization() const +float64_t LMNN::get_regularization() const { return m_regularization; } -void CLMNN::set_regularization(const float64_t regularization) +void LMNN::set_regularization(const float64_t regularization) { m_regularization = regularization; } -float64_t CLMNN::get_stepsize() const +float64_t LMNN::get_stepsize() const { return m_stepsize; } -void CLMNN::set_stepsize(const float64_t stepsize) +void LMNN::set_stepsize(const float64_t stepsize) { require(stepsize>0, "The step size used in gradient descent must be larger than zero"); m_stepsize = stepsize; } -float64_t CLMNN::get_stepsize_threshold() const +float64_t LMNN::get_stepsize_threshold() const { return m_stepsize_threshold; } -void CLMNN::set_stepsize_threshold(const float64_t stepsize_threshold) +void LMNN::set_stepsize_threshold(const float64_t stepsize_threshold) { require(stepsize_threshold>0, "The threshold for the step size must be larger than zero"); m_stepsize_threshold = stepsize_threshold; } -int32_t CLMNN::get_maxiter() const +int32_t LMNN::get_maxiter() const { return m_maxiter; } -void CLMNN::set_maxiter(const int32_t maxiter) +void LMNN::set_maxiter(const int32_t maxiter) { require(maxiter>0, "The number of maximum iterations must be larger than zero"); m_maxiter = maxiter; } -int32_t CLMNN::get_correction() const +int32_t LMNN::get_correction() const { return m_correction; } -void CLMNN::set_correction(const int32_t correction) +void LMNN::set_correction(const int32_t correction) { m_correction = correction; } -float64_t CLMNN::get_obj_threshold() const +float64_t LMNN::get_obj_threshold() const { return m_obj_threshold; } -void CLMNN::set_obj_threshold(const float64_t obj_threshold) +void LMNN::set_obj_threshold(const float64_t obj_threshold) { require(obj_threshold>0, "The threshold for the objective must be larger than zero"); m_obj_threshold = obj_threshold; } -bool CLMNN::get_diagonal() const +bool LMNN::get_diagonal() const { return m_diagonal; } -void CLMNN::set_diagonal(const bool diagonal) +void LMNN::set_diagonal(const bool diagonal) { m_diagonal = diagonal; } -CLMNNStatistics* CLMNN::get_statistics() const +std::shared_ptr LMNN::get_statistics() const { - SG_REF(m_statistics); + return m_statistics; } -void CLMNN::init() +void LMNN::init() { SG_ADD(&m_linear_transform, "linear_transform", "Linear transform in matrix form"); - SG_ADD((CSGObject**) &m_features, "features", "Training features"); - SG_ADD((CSGObject**) &m_labels, "labels", "Training labels"); + SG_ADD((std::shared_ptr*) &m_features, "features", "Training features"); + SG_ADD((std::shared_ptr*) &m_labels, "labels", "Training labels"); SG_ADD(&m_k, "k", "Number of target neighbours per example"); SG_ADD(&m_regularization, "regularization", "Regularization", ParameterProperties::HYPER); @@ -271,7 +268,7 @@ void CLMNN::init() "Iterations between exact impostors search"); SG_ADD(&m_obj_threshold, "obj_threshold", "Objective threshold"); SG_ADD(&m_diagonal, "m_diagonal", "Diagonal transformation"); - SG_ADD((CSGObject**) &m_statistics, "statistics", "Training statistics"); + SG_ADD((std::shared_ptr*) &m_statistics, "statistics", "Training statistics"); m_features = NULL; m_labels = NULL; @@ -286,23 +283,23 @@ void CLMNN::init() m_statistics = NULL; } -CLMNNStatistics::CLMNNStatistics() +LMNNStatistics::LMNNStatistics() { init(); } -CLMNNStatistics::~CLMNNStatistics() +LMNNStatistics::~LMNNStatistics() { } -const char* CLMNNStatistics::get_name() const +const char* LMNNStatistics::get_name() const { return "LMNNStatistics"; } -void CLMNNStatistics::resize(int32_t size) +void LMNNStatistics::resize(int32_t size) { - require(size > 0, "The new size in CLMNNStatistics::resize must be larger than zero." + require(size > 0, "The new size in LMNNStatistics::resize must be larger than zero." " Given value is {}.", size); obj.resize_vector(size); @@ -310,10 +307,10 @@ void CLMNNStatistics::resize(int32_t size) num_impostors.resize_vector(size); } -void CLMNNStatistics::set(index_t iter, float64_t obj_iter, float64_t stepsize_iter, +void LMNNStatistics::set(index_t iter, float64_t obj_iter, float64_t stepsize_iter, uint32_t num_impostors_iter) { - require(iter >= 0 && iter < obj.vlen, "The iteration index in CLMNNStatistics::set " + require(iter >= 0 && iter < obj.vlen, "The iteration index in LMNNStatistics::set " "must be larger or equal to zero and less than the size ({}). Given valu is {}.", obj.vlen, iter); obj[iter] = obj_iter; @@ -321,7 +318,7 @@ void CLMNNStatistics::set(index_t iter, float64_t obj_iter, float64_t stepsize_i num_impostors[iter] = num_impostors_iter; } -void CLMNNStatistics::init() +void LMNNStatistics::init() { SG_ADD(&obj, "obj", "Objective at each iteration"); SG_ADD(&stepsize, "stepsize", "Step size at each iteration"); diff --git a/src/shogun/metric/LMNN.h b/src/shogun/metric/LMNN.h index 033096f5b03..06e7f33acd1 100644 --- a/src/shogun/metric/LMNN.h +++ b/src/shogun/metric/LMNN.h @@ -20,7 +20,7 @@ namespace shogun { // Forward declaration -class CLMNNStatistics; +class LMNNStatistics; /** * @brief Class LMNN that implements the distance metric learning technique @@ -29,11 +29,11 @@ class CLMNNStatistics; * Weinberger, K. Q., Saul, L. K. * Distance Metric Learning for Large Margin Nearest Neighbor Classification. */ -class CLMNN : public CSGObject +class LMNN : public SGObject { public: /** default constructor */ - CLMNN(); + LMNN(); /** standard constructor * @@ -41,10 +41,10 @@ class CLMNN : public CSGObject * @param labels labels of the features * @param k number of target neighbours per example */ - CLMNN(CFeatures* features, CMulticlassLabels* labels, int32_t k); + LMNN(std::shared_ptr features, std::shared_ptr labels, int32_t k); /** destructor */ - virtual ~CLMNN(); + virtual ~LMNN(); /** @return name of SGSerializable */ virtual const char* get_name() const; @@ -70,7 +70,7 @@ class CLMNN : public CSGObject * * @return the distance M */ - CDistance* get_distance() const; + std::shared_ptr get_distance() const; /** get the number of target neighbours per example * @@ -172,7 +172,7 @@ class CLMNN : public CSGObject * * @return LMNN training statistics */ - CLMNNStatistics* get_statistics() const; + std::shared_ptr get_statistics() const; private: /** register parameters */ @@ -183,10 +183,10 @@ class CLMNN : public CSGObject SGMatrix m_linear_transform; /** training features */ - CFeatures* m_features; + std::shared_ptr m_features; /** training labels */ - CLabels* m_labels; + std::shared_ptr m_labels; /** * trade-off between pull and push forces in the objective. @@ -233,30 +233,30 @@ class CLMNN : public CSGObject */ bool m_diagonal; - /** training statistics, @see CLMNNStatistics */ - CLMNNStatistics* m_statistics; + /** training statistics, @see LMNNStatistics */ + std::shared_ptr m_statistics; -}; /* class CLMNN */ +}; /* class LMNN */ /** * @brief Class LMNNStatistics used to give access to intermediate results * obtained training LMNN. */ -class CLMNNStatistics : public CSGObject +class LMNNStatistics : public SGObject { public: /** default constructor */ - CLMNNStatistics(); + LMNNStatistics(); /** destructor */ - virtual ~CLMNNStatistics(); + virtual ~LMNNStatistics(); /** @return name of SGSerializable */ virtual const char* get_name() const; /** - * resize CLMNNStatistics::obj, CLMNNStatistics::stepsize and - * CLMNNStatistics::num_impostors to fit the specified number of elements + * resize LMNNStatistics::obj, LMNNStatistics::stepsize and + * LMNNStatistics::num_impostors to fit the specified number of elements * * @param size number of elements */ diff --git a/src/shogun/metric/LMNNImpl.cpp b/src/shogun/metric/LMNNImpl.cpp index de2bbc77f68..b4bfcfb1cd3 100644 --- a/src/shogun/metric/LMNNImpl.cpp +++ b/src/shogun/metric/LMNNImpl.cpp @@ -36,8 +36,8 @@ bool CImpostorNode::operator<(const CImpostorNode& rhs) const return example < rhs.example; } -void CLMNNImpl::check_training_setup( - CFeatures* features, CLabels* labels, SGMatrix& init_transform, +void LMNNImpl::check_training_setup( + std::shared_ptr features, std::shared_ptr labels, SGMatrix& init_transform, int32_t k) { require(features->has_property(FP_DOT), @@ -51,11 +51,11 @@ void CLMNNImpl::check_training_setup( "Currently, LMNN supports only DenseFeatures"); // cast is safe, we ensure above that features are dense - CDenseFeatures* x = static_cast*>(features); + auto x = features->as>(); /// Initialize, if necessary, the initial transform if (init_transform.num_rows==0) - init_transform = CLMNNImpl::compute_pca_transform(x); + init_transform = LMNNImpl::compute_pca_transform(x); require(init_transform.num_rows==x->get_num_features() && init_transform.num_rows==init_transform.num_cols, @@ -65,9 +65,9 @@ void CLMNNImpl::check_training_setup( check_maximum_k(labels, k); } -void CLMNNImpl::check_maximum_k(CLabels* labels, int32_t k) +void LMNNImpl::check_maximum_k(std::shared_ptr labels, int32_t k) { - CMulticlassLabels* y = multiclass_labels(labels); + auto y = multiclass_labels(labels); SGVector int_labels = y->get_int_labels(); // back-up initial values because they will be overwritten by unique @@ -104,10 +104,10 @@ void CLMNNImpl::check_maximum_k(CLabels* labels, int32_t k) min_num_examples, k); } -SGMatrix CLMNNImpl::find_target_nn(CDenseFeatures* x, - CMulticlassLabels* y, int32_t k) +SGMatrix LMNNImpl::find_target_nn(std::shared_ptr> x, + std::shared_ptr y, int32_t k) { - SG_TRACE("Entering CLMNNImpl::find_target_nn()."); + SG_TRACE("Entering LMNNImpl::find_target_nn()."); // get the number of features int32_t d = x->get_num_features(); @@ -137,10 +137,10 @@ SGMatrix CLMNNImpl::find_target_nn(CDenseFeatures* x, SGVector labels_vec(slice_size); labels_vec.set_const(unique_labels[i]); - auto features_slice = some>(slice_mat); - auto labels_slice = some(labels_vec); + auto features_slice = std::make_shared>(slice_mat); + auto labels_slice = std::make_shared(labels_vec); - CKNN* knn = new CKNN(k+1, new CEuclideanDistance(features_slice, features_slice), labels_slice); + auto knn = std::make_shared(k+1, std::make_shared(features_slice, features_slice), labels_slice); SGMatrix target_slice = knn->nearest_neighbors(); // sanity check ASSERT(target_slice.num_rows==k+1 && target_slice.num_cols==slice_size) @@ -152,16 +152,16 @@ SGMatrix CLMNNImpl::find_target_nn(CDenseFeatures* x, } // clean up knn - SG_UNREF(knn) + } - SG_TRACE("Leaving CLMNNImpl::find_target_nn()."); + SG_TRACE("Leaving LMNNImpl::find_target_nn()."); return target_neighbors; } -SGMatrix CLMNNImpl::sum_outer_products( - CDenseFeatures* x, const SGMatrix& target_nn) +SGMatrix LMNNImpl::sum_outer_products( + std::shared_ptr> x, const SGMatrix& target_nn) { // get the number of features int32_t d = x->get_num_features(); @@ -184,12 +184,12 @@ SGMatrix CLMNNImpl::sum_outer_products( return sop; } -ImpostorsSetType CLMNNImpl::find_impostors( - CDenseFeatures* x, CMulticlassLabels* y, +ImpostorsSetType LMNNImpl::find_impostors( + std::shared_ptr> x, std::shared_ptr y, const SGMatrix& L, const SGMatrix& target_nn, const int32_t iter, const int32_t correction) { - SG_TRACE("Entering CLMNNImpl::find_impostors()."); + SG_TRACE("Entering LMNNImpl::find_impostors()."); // get the number of neighbors int32_t k = target_nn.num_rows; @@ -199,7 +199,7 @@ ImpostorsSetType CLMNNImpl::find_impostors( auto LX = linalg::matrix_prod(L, X); // compute square distances plus margin from examples to target neighbors - auto sqdists = CLMNNImpl::compute_sqdists(LX, target_nn); + auto sqdists = LMNNImpl::compute_sqdists(LX, target_nn); // initialize impostors set ImpostorsSetType N; @@ -211,23 +211,23 @@ ImpostorsSetType CLMNNImpl::find_impostors( "impostors set must be greater than 0"); if ((iter % correction)==0) { - Nexact = CLMNNImpl::find_impostors_exact( + Nexact = LMNNImpl::find_impostors_exact( SGMatrix(LX), sqdists, y, target_nn, k); N = Nexact; } else { - N = CLMNNImpl::find_impostors_approx( + N = LMNNImpl::find_impostors_approx( SGMatrix(LX), sqdists, Nexact, target_nn); } - SG_TRACE("Leaving CLMNNImpl::find_impostors()."); + SG_TRACE("Leaving LMNNImpl::find_impostors()."); return N; } -void CLMNNImpl::update_gradient( - CDenseFeatures* x, SGMatrix& G, +void LMNNImpl::update_gradient( + std::shared_ptr> x, SGMatrix& G, const ImpostorsSetType& Nc, const ImpostorsSetType& Np, float64_t regularization) { @@ -264,7 +264,7 @@ void CLMNNImpl::update_gradient( } } -void CLMNNImpl::gradient_step( +void LMNNImpl::gradient_step( SGMatrix& L, const SGMatrix& G, float64_t stepsize, bool diagonal) { @@ -290,7 +290,7 @@ void CLMNNImpl::gradient_step( } } -void CLMNNImpl::correct_stepsize( +void LMNNImpl::correct_stepsize( float64_t& stepsize, const SGVector obj, const int32_t iter) { if (iter > 0) @@ -313,7 +313,7 @@ void CLMNNImpl::correct_stepsize( } } -bool CLMNNImpl::check_termination( +bool LMNNImpl::check_termination( float64_t stepsize, const SGVector obj, int32_t iter, int32_t maxiter, float64_t stepsize_threshold, float64_t obj_threshold) { @@ -334,7 +334,7 @@ bool CLMNNImpl::check_termination( obj_threshold *= obj[iter - 1]; for (int32_t i = 0; i < 3; ++i) { - if (CMath::abs(obj[iter-i]-obj[iter-i-1]) >= obj_threshold) + if (Math::abs(obj[iter-i]-obj[iter-i-1]) >= obj_threshold) return false; } @@ -346,19 +346,19 @@ bool CLMNNImpl::check_termination( return false; } -SGMatrix CLMNNImpl::compute_pca_transform(CDenseFeatures* features) +SGMatrix LMNNImpl::compute_pca_transform(std::shared_ptr> features) { SG_TRACE("Initializing LMNN transform using PCA."); // Substract the mean of the features // Create clone of the features to keep the input features unmodified auto mean_substractor = - some(false); // false to avoid variance normalization + std::make_shared(false); // false to avoid variance normalization mean_substractor->fit(features); - auto centered_feats = mean_substractor->transform(features)->as>(); + auto centered_feats = mean_substractor->transform(features)->as>(); // Obtain the linear transform applying PCA - auto pca = some(); + auto pca = std::make_shared(); pca->set_target_dim(centered_feats->get_num_features()); pca->fit(centered_feats); SGMatrix pca_transform = pca->get_transformation_matrix(); @@ -366,7 +366,7 @@ SGMatrix CLMNNImpl::compute_pca_transform(CDenseFeatures* return pca_transform; } -SGMatrix CLMNNImpl::compute_sqdists( +SGMatrix LMNNImpl::compute_sqdists( const SGMatrix& LX, const SGMatrix& target_nn) { // get the number of examples @@ -378,7 +378,7 @@ SGMatrix CLMNNImpl::compute_sqdists( /// compute square distances to target neighbors plus margin // create Shogun features from LX to later apply subset - CDenseFeatures* lx = new CDenseFeatures(LX); + auto lx = std::make_shared>(LX); // initialize distances SGMatrix sqdists(k, n); @@ -399,22 +399,22 @@ SGMatrix CLMNNImpl::compute_sqdists( } // clean up features used to apply subset - SG_UNREF(lx); + return sqdists; } -ImpostorsSetType CLMNNImpl::find_impostors_exact( +ImpostorsSetType LMNNImpl::find_impostors_exact( const SGMatrix& LX, const SGMatrix& sqdists, - CMulticlassLabels* y, const SGMatrix& target_nn, int32_t k) + std::shared_ptr y, const SGMatrix& target_nn, int32_t k) { - SG_TRACE("Entering CLMNNImpl::find_impostors_exact()."); + SG_TRACE("Entering LMNNImpl::find_impostors_exact()."); // initialize empty impostors set ImpostorsSetType N = ImpostorsSetType(); // create Shogun features from LX to later apply subset - CDenseFeatures* lx = new CDenseFeatures(LX); + auto lx = std::make_shared>(LX); // get a vector with unique label values SGVector unique = y->get_unique_labels(); @@ -423,13 +423,13 @@ ImpostorsSetType CLMNNImpl::find_impostors_exact( for (index_t i = 0; i < unique.vlen-1; ++i) { // get the indices of the examples labelled as unique[i] - std::vector iidxs = CLMNNImpl::get_examples_label(y,unique[i]); + std::vector iidxs = LMNNImpl::get_examples_label(y,unique[i]); // get the indices of the examples that have a larger label value, so that // pairwise distances are computed once - std::vector gtidxs = CLMNNImpl::get_examples_gtlabel(y,unique[i]); + std::vector gtidxs = LMNNImpl::get_examples_gtlabel(y,unique[i]); // get distance with features indexed by iidxs and gtidxs separated - CEuclideanDistance* euclidean = CLMNNImpl::setup_distance(lx,iidxs,gtidxs); + auto euclidean = LMNNImpl::setup_distance(lx,iidxs,gtidxs); euclidean->set_disable_sqrt(true); for (int32_t j = 0; j < k; ++j) @@ -450,27 +450,26 @@ ImpostorsSetType CLMNNImpl::find_impostors_exact( } } - SG_UNREF(euclidean); + } - SG_UNREF(lx); - SG_TRACE("Leaving CLMNNImpl::find_impostors_exact()."); + SG_TRACE("Leaving LMNNImpl::find_impostors_exact()."); return N; } -ImpostorsSetType CLMNNImpl::find_impostors_approx( +ImpostorsSetType LMNNImpl::find_impostors_approx( const SGMatrix& LX, const SGMatrix& sqdists, const ImpostorsSetType& Nexact, const SGMatrix& target_nn) { - SG_TRACE("Entering CLMNNImpl::find_impostors_approx()."); + SG_TRACE("Entering LMNNImpl::find_impostors_approx()."); // initialize empty impostors set ImpostorsSetType N = ImpostorsSetType(); // compute square distances from examples to impostors - SGVector impostors_sqdists = CLMNNImpl::compute_impostors_sqdists(LX,Nexact); + SGVector impostors_sqdists = LMNNImpl::compute_impostors_sqdists(LX,Nexact); // find in the exact set of impostors computed last, the triplets that remain impostors index_t i = 0; @@ -489,12 +488,12 @@ ImpostorsSetType CLMNNImpl::find_impostors_approx( N.insert(*it); } - SG_TRACE("Leaving CLMNNImpl::find_impostors_approx()."); + SG_TRACE("Leaving LMNNImpl::find_impostors_approx()."); return N; } -SGVector CLMNNImpl::compute_impostors_sqdists( +SGVector LMNNImpl::compute_impostors_sqdists( const SGMatrix& LX, const ImpostorsSetType& Nexact) { // get the number of impostors @@ -503,8 +502,8 @@ SGVector CLMNNImpl::compute_impostors_sqdists( /// compute square distances to impostors // create Shogun features from LX and distance - CDenseFeatures* lx = new CDenseFeatures(LX); - CEuclideanDistance* euclidean = new CEuclideanDistance(lx,lx); + auto lx = std::make_shared>(LX); + auto euclidean = std::make_shared(lx,lx); euclidean->set_disable_sqrt(true); // initialize vector of square distances @@ -515,12 +514,12 @@ SGVector CLMNNImpl::compute_impostors_sqdists( sqdists[i++] = euclidean->distance(it->example,it->impostor); // clean up distance - SG_UNREF(euclidean); + return sqdists; } -std::vector CLMNNImpl::get_examples_label(CMulticlassLabels* y, +std::vector LMNNImpl::get_examples_label(std::shared_ptr y, float64_t yi) { // indices of the examples with label equal to yi @@ -535,7 +534,7 @@ std::vector CLMNNImpl::get_examples_label(CMulticlassLabels* y, return idxs; } -std::vector CLMNNImpl::get_examples_gtlabel(CMulticlassLabels* y, +std::vector LMNNImpl::get_examples_gtlabel(std::shared_ptr y, float64_t yi) { // indices of the examples with label equal greater than yi @@ -550,7 +549,7 @@ std::vector CLMNNImpl::get_examples_gtlabel(CMulticlassLabels* y, return idxs; } -CEuclideanDistance* CLMNNImpl::setup_distance(CDenseFeatures* x, +std::shared_ptr LMNNImpl::setup_distance(std::shared_ptr> x, std::vector& a, std::vector& b) { // create new features only containing examples whose indices are in a @@ -560,7 +559,6 @@ CEuclideanDistance* CLMNNImpl::setup_distance(CDenseFeatures* x, auto bfeats = view(x, SGVector(b.data(), b.size(), false)); // create and return distance - CEuclideanDistance* euclidean = new CEuclideanDistance(afeats,bfeats); - return euclidean; + return std::make_shared(afeats,bfeats); } diff --git a/src/shogun/metric/LMNNImpl.h b/src/shogun/metric/LMNNImpl.h index c321cb0f6ce..0cf4e243134 100644 --- a/src/shogun/metric/LMNNImpl.h +++ b/src/shogun/metric/LMNNImpl.h @@ -68,9 +68,9 @@ struct CImpostorNode }; /** - * Class CLMNNImpl used to hide the implementation details of LMNN. + * Class LMNNImpl used to hide the implementation details of LMNN. */ -class CLMNNImpl +class LMNNImpl { public: @@ -79,28 +79,28 @@ class CLMNNImpl * if the initial transform has not been initialized, do it using PCA */ static void check_training_setup( - CFeatures* features, CLabels* labels, + std::shared_ptr features, std::shared_ptr labels, SGMatrix& init_transform, int32_t k); /** * for each feature in x, find its target neighbors; that is, its k * nearest neighbors with the same label as indicated by y */ - static SGMatrix find_target_nn(CDenseFeatures* x, CMulticlassLabels* y, int32_t k); + static SGMatrix find_target_nn(std::shared_ptr> x, std::shared_ptr y, int32_t k); /** sum the outer products indicated by target_nn */ static SGMatrix sum_outer_products( - CDenseFeatures* x, const SGMatrix& target_nn); + std::shared_ptr> x, const SGMatrix& target_nn); /** find the impostors that remain after applying the transformation L */ static ImpostorsSetType find_impostors( - CDenseFeatures* x, CMulticlassLabels* y, + std::shared_ptr> x, std::shared_ptr y, const SGMatrix& L, const SGMatrix& target_nn, const int32_t iter, const int32_t correction); /** update the gradient using the last transition in the impostors sets */ static void update_gradient( - CDenseFeatures* x, SGMatrix& G, + std::shared_ptr> x, SGMatrix& G, const ImpostorsSetType& Nc, const ImpostorsSetType& Np, float64_t mu); @@ -127,7 +127,7 @@ class CLMNNImpl private: /** initial default transform given by PCA */ - static SGMatrix compute_pca_transform(CDenseFeatures* features); + static SGMatrix compute_pca_transform(std::shared_ptr> features); /** * compute squared distances plus margin between each example and its target neighbors @@ -146,7 +146,7 @@ class CLMNNImpl /** find impostors; variant computing the impostors exactly, using all the data */ static ImpostorsSetType find_impostors_exact( const SGMatrix& LX, const SGMatrix& sqdists, - CMulticlassLabels* y, const SGMatrix& target_nn, + std::shared_ptr y, const SGMatrix& target_nn, int32_t k); /** find impostors; approximate variant, using the last exact set of impostors */ @@ -155,17 +155,17 @@ class CLMNNImpl const ImpostorsSetType& Nexact, const SGMatrix& target_nn); /** get the indices of the examples whose label is equal to yi */ - static std::vector get_examples_label(CMulticlassLabels* y, float64_t yi); + static std::vector get_examples_label(std::shared_ptr y, float64_t yi); /** get the indices of the examples whose label is greater than yi */ - static std::vector get_examples_gtlabel(CMulticlassLabels* y, float64_t yi); + static std::vector get_examples_gtlabel(std::shared_ptr y, float64_t yi); /** * create Euclidean distance where the lhs features are the features in x indexed * by the elements in a, and the rhs features are the ones indexed by b; caller * is responsible of releasing memory */ - static CEuclideanDistance* setup_distance(CDenseFeatures* x, std::vector& a, std::vector& b); + static std::shared_ptr setup_distance(std::shared_ptr> x, std::vector& a, std::vector& b); /** * check that k is less than the minimum number of examples in any @@ -173,9 +173,9 @@ class CLMNNImpl * k must be less than the number of examples in any class because each * example needs k other examples (the nearest ones) of the same class */ - static void check_maximum_k(CLabels* labels, int32_t k); + static void check_maximum_k(std::shared_ptr labels, int32_t k); -}; /* class CLMNNImpl */ +}; /* class LMNNImpl */ } /* namespace shogun */ diff --git a/src/shogun/modelselection/GradientModelSelection.cpp b/src/shogun/modelselection/GradientModelSelection.cpp index 5020c546d97..b3ecd33e094 100644 --- a/src/shogun/modelselection/GradientModelSelection.cpp +++ b/src/shogun/modelselection/GradientModelSelection.cpp @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Wu Lin, Heiko Strathmann, Roman Votyakov, Viktor Gal, - * Weijie Lin, Fernando Iglesias, Sergey Lisitsyn, Bjoern Esser, + * Authors: Jacob Walker, Wu Lin, Heiko Strathmann, Roman Votyakov, Viktor Gal, + * Weijie Lin, Fernando Iglesias, Sergey Lisitsyn, Bjoern Esser, * Soeren Sonnenburg */ @@ -27,14 +27,14 @@ class GradientModelSelectionCostFunction: public FirstOrderCostFunction { public: GradientModelSelectionCostFunction():FirstOrderCostFunction() { init(); } - virtual ~GradientModelSelectionCostFunction() { SG_UNREF(m_obj); } - void set_target(CGradientModelSelection *obj) + virtual ~GradientModelSelectionCostFunction() { } + void set_target(std::shared_ptrobj) { require(obj,"Obj must set"); if(m_obj!=obj) { - SG_REF(obj); - SG_UNREF(m_obj); + + m_obj=obj; } } @@ -42,7 +42,7 @@ class GradientModelSelectionCostFunction: public FirstOrderCostFunction { if(is_unref) { - SG_UNREF(m_obj); + } m_obj=NULL; } @@ -82,7 +82,7 @@ class GradientModelSelectionCostFunction: public FirstOrderCostFunction void init() { m_obj=NULL; - SG_ADD((CSGObject **)&m_obj, "GradientModelSelectionCostFunction__m_obj", + SG_ADD((std::shared_ptr*)&m_obj, "GradientModelSelectionCostFunction__m_obj", "obj in GradientModelSelectionCostFunction"); m_func_data = NULL; m_val = SGVector(); @@ -95,7 +95,7 @@ class GradientModelSelectionCostFunction: public FirstOrderCostFunction "grad in GradientModelSelectionCostFunction"); } - CGradientModelSelection *m_obj; + std::shared_ptrm_obj; void* m_func_data; SGVector m_val; SGVector m_grad; @@ -106,16 +106,16 @@ class GradientModelSelectionCostFunction: public FirstOrderCostFunction struct nlopt_params { /** pointer to current combination */ - CParameterCombination* current_combination; + std::shared_ptr current_combination; /** pointer to parmeter dictionary */ - CMap* parameter_dictionary; + std::shared_ptr> parameter_dictionary; /** do we want to print the state? */ bool print_state; }; -float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVector model_grads, void* func_data) +float64_t GradientModelSelection::get_cost(SGVector model_vars, SGVector model_grads, void* func_data) { require(func_data!=NULL, "func_data must set"); require(model_vars.vlen==model_grads.vlen, "length of variable ({}) and gradient ({}) must equal", @@ -123,8 +123,8 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe nlopt_params* params=(nlopt_params*)func_data; - CParameterCombination* current_combination=params->current_combination; - CMap* parameter_dictionary=params->parameter_dictionary; + auto current_combination=params->current_combination; + auto parameter_dictionary=params->parameter_dictionary; bool print_state=params->print_state; index_t offset=0; @@ -132,10 +132,10 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe // set parameters from vector model_vars for (auto i : SG_PROGRESS(range(parameter_dictionary->get_num_elements()))) { - CMapNode* node=parameter_dictionary->get_node_ptr(i); + CMapNode* node=parameter_dictionary->get_node_ptr(i); TParameter* param=node->key; - CSGObject* parent=node->data; + auto parent=node->data; if (param->m_datatype.m_ctype==CT_VECTOR || param->m_datatype.m_ctype==CT_SGVECTOR || @@ -162,20 +162,17 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe } // apply current combination to the machine - CMachine* machine=m_machine_eval->get_machine(); + auto machine=m_machine_eval->get_machine(); current_combination->apply_to_machine(machine); if (print_state) { io::print("Current combination\n"); current_combination->print_tree(); } - SG_UNREF(machine); // evaluate the machine - CEvaluationResult* evaluation_result=m_machine_eval->evaluate(); - CGradientResult* gradient_result = evaluation_result->as(); - SG_REF(gradient_result); - SG_UNREF(evaluation_result); + auto evaluation_result=m_machine_eval->evaluate(); + auto gradient_result = evaluation_result->as(); if (print_state) { @@ -188,7 +185,7 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe float64_t cost = SGVector::sum(value); - if (CMath::is_nan(cost) || std::isinf(cost)) + if (Math::is_nan(cost) || std::isinf(cost)) { if (m_machine_eval->get_evaluation_direction()==ED_MINIMIZE) return cost; @@ -196,23 +193,22 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe return -cost; } - CMap >* gradient=gradient_result->get_gradient(); - CMap* gradient_dictionary= - gradient_result->get_paramter_dictionary(); - SG_UNREF(gradient_result); + auto gradient=gradient_result->get_gradient(); + auto gradient_dictionary=gradient_result->get_paramter_dictionary(); + offset=0; // set derivative for each parameter from parameter dictionary for (index_t i=0; iget_num_elements(); i++) { - CMapNode* node=parameter_dictionary->get_node_ptr(i); + CMapNode* node=parameter_dictionary->get_node_ptr(i); SGVector derivative; for (index_t j=0; jget_num_elements(); j++) { - CMapNode* gradient_node= + CMapNode* gradient_node= gradient_dictionary->get_node_ptr(j); if (gradient_node->data==node->data && @@ -230,9 +226,6 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe offset+=derivative.vlen; } - SG_UNREF(gradient); - SG_UNREF(gradient_dictionary); - if (m_machine_eval->get_evaluation_direction()==ED_MINIMIZE) { return cost; @@ -246,50 +239,48 @@ float64_t CGradientModelSelection::get_cost(SGVector model_vars, SGVe } #endif /* DOXYGEN_SHOULD_SKIP_THIS */ -void CGradientModelSelection::set_minimizer(FirstOrderMinimizer* minimizer) +void GradientModelSelection::set_minimizer(std::shared_ptr minimizer) { require(minimizer!=NULL, "Minimizer must set"); - SG_REF(minimizer); - SG_UNREF(m_mode_minimizer); m_mode_minimizer=minimizer; } -CGradientModelSelection::CGradientModelSelection() : CModelSelection() +GradientModelSelection::GradientModelSelection() : ModelSelection() { init(); } -CGradientModelSelection::CGradientModelSelection(CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters) - : CModelSelection(machine_eval, model_parameters) +GradientModelSelection::GradientModelSelection(std::shared_ptr machine_eval, + std::shared_ptr model_parameters) + : ModelSelection(machine_eval, model_parameters) { init(); } -CGradientModelSelection::~CGradientModelSelection() +GradientModelSelection::~GradientModelSelection() { - SG_UNREF(m_mode_minimizer); + } -void CGradientModelSelection::init() +void GradientModelSelection::init() { - m_mode_minimizer = new CLBFGSMinimizer(); - SG_REF(m_mode_minimizer); + m_mode_minimizer = std::make_shared(); + - SG_ADD((CSGObject **)&m_mode_minimizer, + SG_ADD((std::shared_ptr*)&m_mode_minimizer, "mode_minimizer", "Minimizer used in mode selection"); } -CParameterCombination* CGradientModelSelection::select_model(bool print_state) +std::shared_ptr GradientModelSelection::select_model(bool print_state) { if (!m_model_parameters) { - CMachine* machine=m_machine_eval->get_machine(); + auto machine=m_machine_eval->get_machine(); + + auto current_combination=std::make_shared(machine); - CParameterCombination* current_combination=new CParameterCombination(machine); - SG_REF(current_combination); if (print_state) { @@ -301,8 +292,8 @@ CParameterCombination* CGradientModelSelection::select_model(bool print_state) index_t total_variables=current_combination->get_parameters_length(); // build parameter->value map - CMap >* argument= - new CMap >(); + auto argument= + std::make_shared>>(); current_combination->build_parameter_values_map(argument); // unroll current parameter combination into vector @@ -317,11 +308,11 @@ CParameterCombination* CGradientModelSelection::select_model(bool print_state) offset+=node->data.vlen; } - SG_UNREF(argument); + // build parameter->sgobject map from current parameter combination - CMap* parameter_dictionary= - new CMap(); + auto parameter_dictionary= + std::make_shared>(); current_combination->build_parameter_parent_map(parameter_dictionary); //data for computing the gradient @@ -344,19 +335,15 @@ CParameterCombination* CGradientModelSelection::select_model(bool print_state) } } - GradientModelSelectionCostFunction *cost_fun=new GradientModelSelectionCostFunction(); - cost_fun->set_target(this); + auto cost_fun=std::make_shared(); + cost_fun->set_target(shared_from_this()->as()); cost_fun->set_variables(model_vars); cost_fun->set_func_data(¶ms); - bool cleanup=false; - if(this->ref_count()>1) - cleanup=true; m_mode_minimizer->set_cost_function(cost_fun); m_mode_minimizer->minimize(); m_mode_minimizer->unset_cost_function(false); - cost_fun->unset_target(cleanup); - SG_UNREF(cost_fun); + if (print_state) { @@ -364,8 +351,6 @@ CParameterCombination* CGradientModelSelection::select_model(bool print_state) current_combination->print_tree(); } - SG_UNREF(machine); - SG_UNREF(parameter_dictionary); return current_combination; } diff --git a/src/shogun/modelselection/GradientModelSelection.h b/src/shogun/modelselection/GradientModelSelection.h index a4ea7e04a78..b1eed3f9842 100644 --- a/src/shogun/modelselection/GradientModelSelection.h +++ b/src/shogun/modelselection/GradientModelSelection.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Roman Votyakov, + * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Roman Votyakov, * Soeren Sonnenburg, Wu Lin */ @@ -23,13 +23,13 @@ class GradientModelSelectionCostFunction; /** @brief Model selection class which searches for the best model by a * gradient-search. */ -class CGradientModelSelection : public CModelSelection +class GradientModelSelection : public ModelSelection { friend class GradientModelSelectionCostFunction; public: /** default constructor */ - CGradientModelSelection(); + GradientModelSelection(); /** constructor * @@ -39,10 +39,10 @@ friend class GradientModelSelectionCostFunction; * @param machine_eval machine evaluation object * @param model_parameters parameters */ - CGradientModelSelection(CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters=NULL); + GradientModelSelection(std::shared_ptr machine_eval, + std::shared_ptr model_parameters=NULL); - virtual ~CGradientModelSelection(); + virtual ~GradientModelSelection(); /** method to select model via gradient search * @@ -50,7 +50,7 @@ friend class GradientModelSelectionCostFunction; * * @return best combination of model parameters */ - virtual CParameterCombination* select_model(bool print_state=false); + virtual std::shared_ptr select_model(bool print_state=false); /** returns the name of the model selection object * @@ -62,7 +62,7 @@ friend class GradientModelSelectionCostFunction; * * @param minimizer minimizer used in model selection */ - virtual void set_minimizer(FirstOrderMinimizer* minimizer); + virtual void set_minimizer(std::shared_ptr minimizer); private: /** initialize object */ @@ -71,7 +71,7 @@ friend class GradientModelSelectionCostFunction; protected: /** minimizer */ - FirstOrderMinimizer* m_mode_minimizer; + std::shared_ptr m_mode_minimizer; /** get the cost given current model parameter * Note that this function will compute the gradient diff --git a/src/shogun/modelselection/GridSearchModelSelection.cpp b/src/shogun/modelselection/GridSearchModelSelection.cpp index defe17af44f..cf186181862 100644 --- a/src/shogun/modelselection/GridSearchModelSelection.cpp +++ b/src/shogun/modelselection/GridSearchModelSelection.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Jacob Walker, Soeren Sonnenburg, Sergey Lisitsyn, + * Authors: Heiko Strathmann, Jacob Walker, Soeren Sonnenburg, Sergey Lisitsyn, * Giovanni De Toni, Thoralf Klein, Roman Votyakov, Kyle McQuisten */ @@ -14,52 +14,51 @@ using namespace shogun; -CGridSearchModelSelection::CGridSearchModelSelection() : CModelSelection() +GridSearchModelSelection::GridSearchModelSelection() : ModelSelection() { } -CGridSearchModelSelection::CGridSearchModelSelection( - CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters) - : CModelSelection(machine_eval, model_parameters) +GridSearchModelSelection::GridSearchModelSelection( + std::shared_ptr machine_eval, + std::shared_ptr model_parameters) + : ModelSelection(machine_eval, model_parameters) { } -CGridSearchModelSelection::~CGridSearchModelSelection() +GridSearchModelSelection::~GridSearchModelSelection() { } -CParameterCombination* CGridSearchModelSelection::select_model(bool print_state) +std::shared_ptr GridSearchModelSelection::select_model(bool print_state) { if (print_state) io::print("Generating parameter combinations\n"); /* Retrieve all possible parameter combinations */ - CDynamicObjectArray* combinations= - (CDynamicObjectArray*)m_model_parameters->get_combinations(); + auto combinations= + m_model_parameters->get_combinations()->as(); - CCrossValidationResult* best_result=new CCrossValidationResult(); + auto best_result=std::make_shared(); - CParameterCombination* best_combination=NULL; + std::shared_ptr best_combination=NULL; if (m_machine_eval->get_evaluation_direction()==ED_MAXIMIZE) { if (print_state) io::print("Direction is maximize\n"); - best_result->set_mean(CMath::ALMOST_NEG_INFTY); + best_result->set_mean(Math::ALMOST_NEG_INFTY); } else { if (print_state) io::print("Direction is minimize\n"); - best_result->set_mean(CMath::ALMOST_INFTY); + best_result->set_mean(Math::ALMOST_INFTY); } /* underlying learning machine */ - CMachine* machine=m_machine_eval->get_machine(); + auto machine=m_machine_eval->get_machine(); /* apply all combinations and search for best one */ for (auto i : SG_PROGRESS(range(combinations->get_num_elements()))) { - CParameterCombination* current_combination=(CParameterCombination*) - combinations->get_element(i); + auto current_combination=combinations->get_element(i); /* eventually print */ if (print_state) @@ -72,8 +71,8 @@ CParameterCombination* CGridSearchModelSelection::select_model(bool print_state) machine->m_model_selection_parameters); /* note that this may implicitly lock and unlockthe machine */ - CCrossValidationResult* result = - m_machine_eval->evaluate()->as(); + auto result = + m_machine_eval->evaluate()->as(); if (print_state) result->print_result(); @@ -84,20 +83,20 @@ CParameterCombination* CGridSearchModelSelection::select_model(bool print_state) if (result->get_mean() > best_result->get_mean()) { if (best_combination) - SG_UNREF(best_combination); - best_combination=(CParameterCombination*) - combinations->get_element(i); - SG_REF(result); - SG_UNREF(best_result); + best_combination= + combinations->get_element(i); + + + best_result=result; } else { - CParameterCombination* combination=(CParameterCombination*) - combinations->get_element(i); - SG_UNREF(combination); + auto combination= + combinations->get_element(i); + } } else @@ -105,30 +104,27 @@ CParameterCombination* CGridSearchModelSelection::select_model(bool print_state) if (result->get_mean() < best_result->get_mean()) { if (best_combination) - SG_UNREF(best_combination); - best_combination=(CParameterCombination*) - combinations->get_element(i); - SG_REF(result); - SG_UNREF(best_result); + best_combination= + combinations->get_element(i); + + + best_result=result; } else { - CParameterCombination* combination=(CParameterCombination*) - combinations->get_element(i); - SG_UNREF(combination); + auto combination= + combinations->get_element(i); + } } - SG_UNREF(result); - SG_UNREF(current_combination); + + } - SG_UNREF(best_result); - SG_UNREF(machine); - SG_UNREF(combinations); return best_combination; } diff --git a/src/shogun/modelselection/GridSearchModelSelection.h b/src/shogun/modelselection/GridSearchModelSelection.h index 492ee0d593a..1fa15c57c95 100644 --- a/src/shogun/modelselection/GridSearchModelSelection.h +++ b/src/shogun/modelselection/GridSearchModelSelection.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, Jacob Walker, + * Authors: Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, Jacob Walker, * Yuyu Zhang, Roman Votyakov */ @@ -15,27 +15,27 @@ namespace shogun { -class CModelSelectionParameters; +class ModelSelectionParameters; /** @brief Model selection class which searches for the best model by a grid- - * search. See CModelSelection for details. + * search. See ModelSelection for details. */ -class CGridSearchModelSelection : public CModelSelection +class GridSearchModelSelection : public ModelSelection { public: /** constructor */ - CGridSearchModelSelection(); + GridSearchModelSelection(); /** constructor * * @param machine_eval machine evaluation object * @param model_parameters parameters */ - CGridSearchModelSelection(CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters); + GridSearchModelSelection(std::shared_ptr machine_eval, + std::shared_ptr model_parameters); /** destructor */ - virtual ~CGridSearchModelSelection(); + virtual ~GridSearchModelSelection(); /** method to select model via grid search * @@ -43,7 +43,7 @@ class CGridSearchModelSelection : public CModelSelection * * @return best combination of model parameters */ - virtual CParameterCombination* select_model(bool print_state=false); + virtual std::shared_ptr select_model(bool print_state=false); /** @return name of the SGSerializable */ virtual const char* get_name() const { return "GridSearchModelSelection"; } diff --git a/src/shogun/modelselection/ModelSelection.cpp b/src/shogun/modelselection/ModelSelection.cpp index bd4129a4493..2e6b0035391 100644 --- a/src/shogun/modelselection/ModelSelection.cpp +++ b/src/shogun/modelselection/ModelSelection.cpp @@ -12,37 +12,37 @@ using namespace shogun; -CModelSelection::CModelSelection() +ModelSelection::ModelSelection() { init(); } -CModelSelection::CModelSelection(CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters) +ModelSelection::ModelSelection(std::shared_ptr machine_eval, + std::shared_ptr model_parameters) { init(); m_model_parameters=model_parameters; - SG_REF(m_model_parameters); + m_machine_eval=machine_eval; - SG_REF(m_machine_eval); + } -void CModelSelection::init() +void ModelSelection::init() { m_model_parameters=NULL; m_machine_eval=NULL; - SG_ADD((CSGObject**)&m_model_parameters, "model_parameters", + SG_ADD((std::shared_ptr*)&m_model_parameters, "model_parameters", "Parameter tree for model selection"); - SG_ADD((CSGObject**)&m_machine_eval, "machine_evaluation", + SG_ADD((std::shared_ptr*)&m_machine_eval, "machine_evaluation", "Machine evaluation strategy"); } -CModelSelection::~CModelSelection() +ModelSelection::~ModelSelection() { - SG_UNREF(m_model_parameters); - SG_UNREF(m_machine_eval); + + } diff --git a/src/shogun/modelselection/ModelSelection.h b/src/shogun/modelselection/ModelSelection.h index 8cf85dee8a5..b592db95ed4 100644 --- a/src/shogun/modelselection/ModelSelection.h +++ b/src/shogun/modelselection/ModelSelection.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Heiko Strathmann, Jacob Walker, Sergey Lisitsyn, Roman Votyakov, + * Authors: Heiko Strathmann, Jacob Walker, Sergey Lisitsyn, Roman Votyakov, * Soeren Sonnenburg, Yuyu Zhang */ @@ -15,8 +15,8 @@ namespace shogun { -class CModelSelectionParameters; -class CParameterCombination; +class ModelSelectionParameters; +class ParameterCombination; /** @brief Abstract base class for model selection. * @@ -25,22 +25,22 @@ class CParameterCombination; * in the abstract method select_model(), which has to be implemented in * concrete sub-classes. */ -class CModelSelection: public CSGObject +class ModelSelection: public SGObject { public: /** default constructor */ - CModelSelection(); + ModelSelection(); /** constructor * * @param machine_eval object that computes the actual evaluation * @param model_parameters parameter tree with model parameters to optimize */ - CModelSelection(CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters); + ModelSelection(std::shared_ptr machine_eval, + std::shared_ptr model_parameters); /** destructor */ - virtual ~CModelSelection(); + virtual ~ModelSelection(); /** abstract method to select model * @@ -48,7 +48,7 @@ class CModelSelection: public CSGObject * * @return best combination of model parameters */ - virtual CParameterCombination* select_model(bool print_state=false)=0; + virtual std::shared_ptr select_model(bool print_state=false)=0; private: /** initializer */ @@ -56,9 +56,9 @@ class CModelSelection: public CSGObject protected: /** model parameters */ - CModelSelectionParameters* m_model_parameters; + std::shared_ptr m_model_parameters; /** cross validation */ - CMachineEvaluation* m_machine_eval; + std::shared_ptr m_machine_eval; }; } #endif /* __MODELSELECTION_H_ */ diff --git a/src/shogun/modelselection/ModelSelectionParameters.cpp b/src/shogun/modelselection/ModelSelectionParameters.cpp index ad9f170fd55..76d8260fc6e 100644 --- a/src/shogun/modelselection/ModelSelectionParameters.cpp +++ b/src/shogun/modelselection/ModelSelectionParameters.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Jacob Walker, Heiko Strathmann, Evgeniy Andreev, + * Authors: Soeren Sonnenburg, Jacob Walker, Heiko Strathmann, Evgeniy Andreev, * Bjoern Esser, Leon Kuchenbecker */ @@ -15,34 +15,34 @@ using namespace shogun; -CModelSelectionParameters::CModelSelectionParameters() +ModelSelectionParameters::ModelSelectionParameters() { init(); } -CModelSelectionParameters::CModelSelectionParameters(const char* node_name) +ModelSelectionParameters::ModelSelectionParameters(const char* node_name) { init(); m_node_name=node_name; } -CModelSelectionParameters::CModelSelectionParameters(const char* node_name, - CSGObject* sgobject) +ModelSelectionParameters::ModelSelectionParameters(const char* node_name, + std::shared_ptr sgobject) { init(); m_node_name=node_name; m_sgobject=sgobject; - SG_REF(sgobject); + } -void CModelSelectionParameters::init() +void ModelSelectionParameters::init() { m_node_name=NULL; m_sgobject=NULL; - m_child_nodes=new CDynamicObjectArray(); - SG_REF(m_child_nodes); + m_child_nodes=std::make_shared(); + m_value_type=MSPT_NONE; m_values=NULL; m_values_length=0; @@ -50,15 +50,15 @@ void CModelSelectionParameters::init() /* no parameter registering. These parameter nodes will not be serialized */ } -CModelSelectionParameters::~CModelSelectionParameters() +ModelSelectionParameters::~ModelSelectionParameters() { - SG_UNREF(m_child_nodes); - SG_UNREF(m_sgobject); + + delete_values(); } -void CModelSelectionParameters::append_child(CModelSelectionParameters* child) +void ModelSelectionParameters::append_child(std::shared_ptr child) { /* only possible if there are no values set */ if (m_values) @@ -67,14 +67,14 @@ void CModelSelectionParameters::append_child(CModelSelectionParameters* child) /* do a basic check if the add is possible */ if (m_sgobject) { - /* (does this node's CSGObject contain a parameter with the name of the + /* (does this node's SGObject contain a parameter with the name of the * child?) to prevent problems when trying to set parameters that do not * exist */ if (child->m_node_name) { if (!m_sgobject->m_parameters->contains_parameter(child->m_node_name)) { - error("Not possible to add child, node with CSGObject \"{}\"" + error("Not possible to add child, node with SGObject \"{}\"" " does not contain a parameter called \"{}\"", m_sgobject->get_name(), child->m_node_name); } @@ -88,14 +88,14 @@ void CModelSelectionParameters::append_child(CModelSelectionParameters* child) m_child_nodes->append_element(child); } -void CModelSelectionParameters::build_values(float64_t min, float64_t max, +void ModelSelectionParameters::build_values(float64_t min, float64_t max, ERangeType type, float64_t step, float64_t type_base) { build_values(MSPT_FLOAT64, (void*)&min, (void*)&max, type, (void*)&step, (void*)&type_base); } -void CModelSelectionParameters::build_values_vector(float64_t min, float64_t max, +void ModelSelectionParameters::build_values_vector(float64_t min, float64_t max, ERangeType type, void* vector, index_t* size, float64_t step, float64_t type_base) { build_values(MSPT_FLOAT64_VECTOR, (void*)&min, (void*)&max, type, (void*)&step, @@ -104,7 +104,7 @@ void CModelSelectionParameters::build_values_vector(float64_t min, float64_t max m_vector = vector; } -void CModelSelectionParameters::build_values_sgvector(float64_t min, float64_t max, +void ModelSelectionParameters::build_values_sgvector(float64_t min, float64_t max, ERangeType type, void* vector, float64_t step, float64_t type_base) { build_values(MSPT_FLOAT64_SGVECTOR, (void*)&min, (void*)&max, type, (void*)&step, @@ -112,14 +112,14 @@ void CModelSelectionParameters::build_values_sgvector(float64_t min, float64_t m m_vector = vector; } -void CModelSelectionParameters::build_values(int32_t min, int32_t max, +void ModelSelectionParameters::build_values(int32_t min, int32_t max, ERangeType type, int32_t step, int32_t type_base) { build_values(MSPT_INT32, (void*)&min, (void*)&max, type, (void*)&step, (void*)&type_base); } -void CModelSelectionParameters::build_values_vector(int32_t min, int32_t max, +void ModelSelectionParameters::build_values_vector(int32_t min, int32_t max, ERangeType type, void* vector, index_t* size, int32_t step, int32_t type_base) { build_values(MSPT_INT32_VECTOR, (void*)&min, (void*)&max, type, (void*)&step, @@ -128,7 +128,7 @@ void CModelSelectionParameters::build_values_vector(int32_t min, int32_t max, m_vector = vector; } -void CModelSelectionParameters::build_values_sgvector(int32_t min, int32_t max, +void ModelSelectionParameters::build_values_sgvector(int32_t min, int32_t max, ERangeType type, void* vector, int32_t step, int32_t type_base) { build_values(MSPT_INT32_SGVECTOR, (void*)&min, (void*)&max, type, (void*)&step, @@ -136,12 +136,12 @@ void CModelSelectionParameters::build_values_sgvector(int32_t min, int32_t max, m_vector = vector; } -void CModelSelectionParameters::build_values(EMSParamType value_type, void* min, +void ModelSelectionParameters::build_values(EMSParamType value_type, void* min, void* max, ERangeType type, void* step, void* type_base) { if (m_sgobject || has_children()) { - error("unable to set range for an CSGObject model selection " + error("unable to set range for an SGObject model selection " "parameter"); } @@ -189,7 +189,7 @@ void CModelSelectionParameters::build_values(EMSParamType value_type, void* min, } } -CParameterCombination* CModelSelectionParameters::get_single_combination( +std::shared_ptr ModelSelectionParameters::get_single_combination( bool is_rand) { /* If this is a value node, then randomly pick a value from the built @@ -230,7 +230,7 @@ CParameterCombination* CModelSelectionParameters::get_single_combination( (param_vect)[j] = ((float64_t*)m_values)[i]; } p->add_vector(¶m_vect, m_vector_length, m_node_name); - watch_param(m_node_name, ¶m_vect, m_vector_length); + /*watch_param(m_node_name, ¶m_vect, m_vector_length)*/; break; } @@ -258,7 +258,7 @@ CParameterCombination* CModelSelectionParameters::get_single_combination( (param_vect)[j] = ((int32_t*)m_values)[i]; } p->add_vector(¶m_vect, m_vector_length, m_node_name); - watch_param(m_node_name, ¶m_vect, m_vector_length); + /*watch_param(m_node_name, ¶m_vect, m_vector_length)*/; break; } @@ -276,14 +276,14 @@ CParameterCombination* CModelSelectionParameters::get_single_combination( break; } - return new CParameterCombination(p); + return std::make_shared(p); } - CParameterCombination* new_root=NULL; + std::shared_ptr new_root=NULL; /*Complain if we have a bad node*/ if (!((m_sgobject && m_node_name) || (!m_node_name && !m_sgobject))) - error("Illegal CModelSelectionParameters node type."); + error("Illegal ModelSelectionParameters node type."); /* Incorporate SGObject and root nodes with children*/ if (m_child_nodes->get_num_elements()) @@ -292,23 +292,24 @@ CParameterCombination* CModelSelectionParameters::get_single_combination( if (m_sgobject) { Parameter* p=new Parameter(); - p->add(&m_sgobject, m_node_name); - new_root = new CParameterCombination(p); + //FIXME + //p->add(&m_sgobject, m_node_name); + new_root = std::make_shared(p); } else - new_root = new CParameterCombination(); + new_root = std::make_shared(); for (index_t i = 0; i < m_child_nodes->get_num_elements(); ++i) { - CModelSelectionParameters* current = - (CModelSelectionParameters*)m_child_nodes->get_element(i); + auto current = + m_child_nodes->get_element(i); - CParameterCombination* c = current->get_single_combination(is_rand); + auto c = current->get_single_combination(is_rand); new_root->append_child(c); - SG_UNREF(current); + } return new_root; @@ -321,13 +322,14 @@ CParameterCombination* CModelSelectionParameters::get_single_combination( if (m_sgobject) { Parameter* p = new Parameter(); - p->add(&m_sgobject, m_node_name); - return new CParameterCombination(p); + //FIXME + //p->add(&m_sgobject, m_node_name); + return std::make_shared(p); } else { - new_root = new CParameterCombination(); + new_root = std::make_shared(); return new_root; } } @@ -336,7 +338,7 @@ CParameterCombination* CModelSelectionParameters::get_single_combination( -CDynamicObjectArray* CModelSelectionParameters::get_combinations( +std::shared_ptr ModelSelectionParameters::get_combinations( index_t num_prefix) { char* prefix=SG_MALLOC(char, num_prefix+1); @@ -344,9 +346,9 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( for (index_t i=0; ientering CModelSelectionParameters::get_combinations() " + SG_DEBUG("{}------>entering ModelSelectionParameters::get_combinations() " "for \"{}\"", prefix, m_node_name ? m_node_name : "root"); - CDynamicObjectArray* result=new CDynamicObjectArray(); + auto result=std::make_shared(); /* value case: node with values and no children. * build trees of Parameter instances which each contain one value @@ -376,37 +378,36 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( break; } - result->append_element(new CParameterCombination(p)); + result->append_element(std::make_shared(p)); } - SG_DEBUG("{}------>leaving CModelSelectionParameters::get_combinations()" + SG_DEBUG("{}------>leaving ModelSelectionParameters::get_combinations()" "for \"{}\"", prefix, m_node_name ? m_node_name : "root"); - SG_FREE(prefix); return result; } /* two cases here, similar - * -case CSGObject: + * -case SGObject: * -case root node (no name, no values, but children * build all permutations of the result trees of children with values and * combine them iteratively children which are something different */ if (!((m_sgobject && m_node_name) || (!m_node_name && !m_sgobject))) - error("{}Illegal CModelSelectionParameters node type.", prefix); + error("{}Illegal ModelSelectionParameters node type.", prefix); /* only consider combinations if this node has children */ if (m_child_nodes->get_num_elements()) { /* split value and non-value child combinations */ - CDynamicObjectArray value_children; - CDynamicObjectArray non_value_children; + DynamicObjectArray value_children; + DynamicObjectArray non_value_children; for (index_t i=0; iget_num_elements(); ++i) { - CModelSelectionParameters* current= - (CModelSelectionParameters*)m_child_nodes->get_element(i); + auto current= + m_child_nodes->get_element(i); /* split children with values and children with other */ if (current->m_values) @@ -414,41 +415,42 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( else non_value_children.append_element(current); - SG_UNREF(current); + } /* extract all tree sets of all value children */ - CDynamicObjectArray value_node_sets; + DynamicObjectArray value_node_sets; for (index_t i=0; i(i); value_node_sets.append_element(value_child->get_combinations( num_prefix+1)); - SG_UNREF(value_child); + } /* build product of all these tree sets */ /* new root node is needed for new trees, depends on current case */ - CParameterCombination* new_root=NULL; + std::shared_ptr new_root=NULL; if (m_sgobject) { Parameter* p=new Parameter(); - p->add(&m_sgobject, m_node_name); - new_root=new CParameterCombination(p); + //FIXME + //p->add(&m_sgobject, m_node_name); + new_root=std::make_shared(p); } else - new_root=new CParameterCombination(); + new_root=std::make_shared(); + - SG_REF(new_root); - CDynamicObjectArray* value_combinations= - CParameterCombination::leaf_sets_multiplication(value_node_sets, + auto value_combinations= + ParameterCombination::leaf_sets_multiplication(value_node_sets, new_root); - SG_UNREF(new_root); + if (!non_value_children.get_num_elements()) *result=*value_combinations; @@ -458,22 +460,21 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( { /* extract all tree sets of non-value nodes */ // io::print("{}extracting combinations of non-value nodes\n", prefix); - CDynamicObjectArray* non_value_combinations= - new CDynamicObjectArray(); + auto non_value_combinations= + std::make_shared(); for (index_t i=0; i(i); // io::print("{}\tcurrent non-value child\n", prefix); // non_value_child->print_tree(num_prefix+1); - CDynamicObjectArray* current_combination= + auto current_combination= non_value_child->get_combinations(num_prefix+2); non_value_combinations->append_element(current_combination); - SG_UNREF(non_value_child); + // io::print("{}\tcombinations of non-value nodes:\n", prefix); // for (index_t j=0; jget_num_elements(); ++j) @@ -499,28 +500,29 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( if (m_sgobject) { Parameter* p=new Parameter(); - p->add(&m_sgobject, m_node_name); - new_root=new CParameterCombination(p); + //FIXME + //p->add(&(ms_sgobject.get()), m_node_name); + new_root=std::make_shared(p); } else - new_root=new CParameterCombination(); + new_root=std::make_shared(); - CDynamicObjectArray* non_value_products= - CParameterCombination::non_value_tree_multiplication( + auto non_value_products= + ParameterCombination::non_value_tree_multiplication( non_value_combinations, new_root); - SG_UNREF(new_root); - SG_UNREF(non_value_combinations); + + non_value_combinations=non_value_products; /* append all non-value combinations to result */ for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* current=(CParameterCombination*) - non_value_combinations->get_element(i); + auto current= + non_value_combinations->get_element(i); result->append_element(current); - SG_UNREF(current); + } } else @@ -531,54 +533,53 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( if (m_sgobject) { Parameter* p=new Parameter(); - p->add(&m_sgobject, m_node_name); - new_root=new CParameterCombination(p); + //FIXME + //p->add(&(ms_sgobject.get()), m_node_name); + new_root=std::make_shared(p); } else - new_root=new CParameterCombination(); + new_root=std::make_shared(); - CDynamicObjectArray* non_value_products= - CParameterCombination::non_value_tree_multiplication( + auto non_value_products= + ParameterCombination::non_value_tree_multiplication( non_value_combinations, new_root); - SG_UNREF(new_root); - SG_UNREF(non_value_combinations); + + non_value_combinations=non_value_products; for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* current_value_tree= - (CParameterCombination*) - value_combinations->get_element(i); + auto current_value_tree= + value_combinations->get_element(i); for (index_t j=0; j get_num_elements(); ++j) { - CParameterCombination* current_non_value_tree= - (CParameterCombination*) - non_value_combinations->get_element(j); + auto current_non_value_tree= + non_value_combinations->get_element(j); /* copy current value tree and add all childs of non- * value combination. Then add new node to result */ - CParameterCombination* value_copy= + auto value_copy= current_value_tree->copy_tree(); value_copy->merge_with(current_non_value_tree); result->append_element(value_copy); - SG_UNREF(current_non_value_tree); + } - SG_UNREF(current_value_tree); + } } /* clean up*/ - SG_UNREF(non_value_combinations); + } - SG_UNREF(value_combinations); + } else { @@ -588,8 +589,9 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( if (m_sgobject) { Parameter* p=new Parameter(); - p->add(&m_sgobject, m_node_name); - result->append_element(new CParameterCombination(p)); + //FIXME + //p->add(&(ms_sgobject.get()), m_node_name); + result->append_element(std::make_shared(p)); } } @@ -603,13 +605,13 @@ CDynamicObjectArray* CModelSelectionParameters::get_combinations( // SG_UNREF(current); // } - SG_DEBUG("{}------>leaving CModelSelectionParameters::get_combinations()" + SG_DEBUG("{}------>leaving ModelSelectionParameters::get_combinations()" "for \"{}\"", prefix, m_node_name ? m_node_name : "root"); SG_FREE(prefix); return result; } -void CModelSelectionParameters::print_tree(int prefix_num) +void ModelSelectionParameters::print_tree(int prefix_num) { /* prefix is enlarged */ char* prefix=SG_MALLOC(char, prefix_num+1); @@ -627,13 +629,13 @@ void CModelSelectionParameters::print_tree(int prefix_num) /* now recursively print successors */ - /* cast safe because only CModelSelectionParameters are added to list */ + /* cast safe because only ModelSelectionParameters are added to list */ for (index_t i=0; iget_num_elements(); ++i) { - CModelSelectionParameters* child= - (CModelSelectionParameters*)m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); child->print_tree(prefix_num+1); - SG_UNREF(child); + } } else @@ -681,7 +683,7 @@ void CModelSelectionParameters::print_tree(int prefix_num) SG_FREE(prefix); } -void CModelSelectionParameters::delete_values() +void ModelSelectionParameters::delete_values() { if (m_values) { diff --git a/src/shogun/modelselection/ModelSelectionParameters.h b/src/shogun/modelselection/ModelSelectionParameters.h index 05f5fb79710..f0bea2d4aeb 100644 --- a/src/shogun/modelselection/ModelSelectionParameters.h +++ b/src/shogun/modelselection/ModelSelectionParameters.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, + * Authors: Jacob Walker, Soeren Sonnenburg, Heiko Strathmann, Sergey Lisitsyn, * Yuyu Zhang, Evgeniy Andreev, Thoralf Klein, Leon Kuchenbecker */ @@ -18,7 +18,7 @@ namespace shogun { -class CParameterCombination; +class ParameterCombination; /** type of range */ enum ERangeType @@ -50,12 +50,12 @@ enum EMSParamType /** * @brief Class to select parameters and their ranges for model selection. The * structure is organized as a tree with different kinds of nodes, depending on - * the values of its member variables of name and CSGObject. + * the values of its member variables of name and SGObject. * - * -root node: no name and no CSGObject, may have children + * -root node: no name and no SGObject, may have children * - * -CSGObject node: has name and a CSGObject, may have children which are the - * parameters of the CSGObject. CSGObjects are SG_REF'ed/SG_UNREF'ed + * -SGObject node: has name and a SGObject, may have children which are the + * parameters of the SGObject. SGObjects are SG_REF'ed/SG_UNREF'ed * * -value node: a node with a (parameter) name and an array of values for that * parameter. These ranges may be set using build_values(). @@ -66,33 +66,33 @@ enum EMSParamType * get_combinations method. It generates a set of trees (different kind than * this one) that contain the instantiated parameter combinations. */ -class CModelSelectionParameters: public RandomMixin +class ModelSelectionParameters: public RandomMixin { public: /** constructor for a root node */ - CModelSelectionParameters(); + ModelSelectionParameters(); /** constructor for a value node * * @param node_name name of the parameter the values will belong to */ - CModelSelectionParameters(const char* node_name); + ModelSelectionParameters(const char* node_name); - /** constructor for a CSGObject node + /** constructor for a SGObject node * - * @param sgobject the CSGObject for this node. Is SG_REF'ed - * @name name of the parameter of the CSGObject + * @param sgobject the SGObject for this node. Is SG_REF'ed + * @name name of the parameter of the SGObject */ - CModelSelectionParameters(const char* node_name, CSGObject* sgobject); + ModelSelectionParameters(const char* node_name, std::shared_ptr sgobject); - /** destructor. If set, deletes data array and SG_UNREF's the CSGObject */ - ~CModelSelectionParameters(); + /** destructor. If set, deletes data array and SG_UNREF's the SGObject */ + ~ModelSelectionParameters(); /** appends a child to this tree. only possible if this is no value node * * @param child child to append */ - void append_child(CModelSelectionParameters* child); + void append_child(std::shared_ptr child); /** setter for values of this node. * If the latter are not possible to be produced by set_range, a vector may @@ -126,7 +126,7 @@ class CModelSelectionParameters: public RandomMixin * to have a more readable print layout * @return result all trees of parameter combinations are put into here */ - CDynamicObjectArray* get_combinations(index_t prefix_num=1); + std::shared_ptr get_combinations(index_t prefix_num=1); /** Instead of generating an array of combinations, get_single_combination * generates a single combination of parameters. The choice of @@ -138,7 +138,7 @@ class CModelSelectionParameters: public RandomMixin * * @return parameter tree of random parameter values. */ - CParameterCombination* get_single_combination(bool rand = true); + std::shared_ptr get_single_combination(bool rand = true); /** float64_t wrapper for build_values() */ void build_values(float64_t min, float64_t max, ERangeType type, @@ -189,12 +189,12 @@ class CModelSelectionParameters: public RandomMixin } private: - CSGObject* m_sgobject; + std::shared_ptr m_sgobject; const char* m_node_name; void* m_values; index_t m_values_length; index_t* m_vector_length; - CDynamicObjectArray* m_child_nodes; + std::shared_ptr m_child_nodes; EMSParamType m_value_type; void* m_vector; }; @@ -219,7 +219,7 @@ template SGVector create_range_array(T min, T max, error("unable build values: max={} < min={}", max, min); /* create value vector, no ref-counting */ - index_t num_values=CMath::round((max-min)/step)+1; + index_t num_values=Math::round((max-min)/step)+1; SGVector result(num_values, false); /* fill array */ @@ -233,14 +233,14 @@ template SGVector create_range_array(T min, T max, result.vector[i]=current; break; case R_EXP: - result.vector[i]=CMath::pow((float64_t)type_base, current); + result.vector[i]=Math::pow((float64_t)type_base, current); break; case R_LOG: if (current<=0) error("log(x) with x={}", current); /* custom base b: log_b(i*step)=log_2(i*step)/log_2(b) */ - result.vector[i]=CMath::log2(current)/CMath::log2(type_base); + result.vector[i]=Math::log2(current)/Math::log2(type_base); break; default: error("unknown range type!"); diff --git a/src/shogun/modelselection/ParameterCombination.cpp b/src/shogun/modelselection/ParameterCombination.cpp index 59bc1a7ebb9..920f672c2d9 100644 --- a/src/shogun/modelselection/ParameterCombination.cpp +++ b/src/shogun/modelselection/ParameterCombination.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Soeren Sonnenburg, Heiko Strathmann, Wu Lin, + * Authors: Jacob Walker, Soeren Sonnenburg, Heiko Strathmann, Wu Lin, * Roman Votyakov, Bjoern Esser, Esben Sorig, Sanuj Sharma */ @@ -14,19 +14,19 @@ using namespace shogun; using namespace std; -CParameterCombination::CParameterCombination() +ParameterCombination::ParameterCombination() { init(); } -CParameterCombination::CParameterCombination(Parameter* param) +ParameterCombination::ParameterCombination(Parameter* param) { init(); m_param=param; } -CParameterCombination::CParameterCombination(CSGObject* obj) +ParameterCombination::ParameterCombination(std::shared_ptr obj) { init(); @@ -46,7 +46,7 @@ CParameterCombination::CParameterCombination(CSGObject* obj) p->add_vector((float64_t**)param->m_parameter, type.m_length_y, param->m_name); - m_child_nodes->append_element(new CParameterCombination(p)); + m_child_nodes->append_element(std::make_shared(p)); m_parameters_length+=*(type.m_length_y); } else if (type.m_ctype==CT_SGMATRIX || type.m_ctype==CT_MATRIX) @@ -55,7 +55,7 @@ CParameterCombination::CParameterCombination(CSGObject* obj) p->add_matrix((float64_t**)param->m_parameter, type.m_length_y, type.m_length_x, param->m_name); - m_child_nodes->append_element(new CParameterCombination(p)); + m_child_nodes->append_element(std::make_shared(p)); m_parameters_length+=type.get_num_elements(); } else if (type.m_ctype==CT_SCALAR) @@ -63,7 +63,7 @@ CParameterCombination::CParameterCombination(CSGObject* obj) Parameter* p=new Parameter(); p->add((float64_t*)param->m_parameter, param->m_name); - m_child_nodes->append_element(new CParameterCombination(p)); + m_child_nodes->append_element(std::make_shared(p)); m_parameters_length++; } else @@ -92,14 +92,15 @@ CParameterCombination::CParameterCombination(CSGObject* obj) { if (type.m_ctype==CT_SCALAR) { - CSGObject* child=*((CSGObject**)(param->m_parameter)); + auto child=*((SGObject**)(param->m_parameter)); if (child->m_gradient_parameters->get_num_parameters()>0) { - CParameterCombination* comb=new CParameterCombination(child); - + //FIXME + //auto comb=std::make_shared(child); + std::shared_ptr comb; comb->m_param=new Parameter(); - comb->m_param->add((CSGObject**)(param->m_parameter), + comb->m_param->add((SGObject**)(param->m_parameter), param->m_name); m_child_nodes->append_element(comb); @@ -114,28 +115,28 @@ CParameterCombination::CParameterCombination(CSGObject* obj) } } -void CParameterCombination::init() +void ParameterCombination::init() { m_parameters_length=0; m_param=NULL; - m_child_nodes=new CDynamicObjectArray(); - SG_REF(m_child_nodes); + m_child_nodes=std::make_shared(); + - SG_ADD((CSGObject**)&m_child_nodes, "child_nodes", "Children of this node"); + /*SG_ADD((SGObject**)&m_child_nodes, "child_nodes", "Children of this node")*/; } -CParameterCombination::~CParameterCombination() +ParameterCombination::~ParameterCombination() { delete m_param; - SG_UNREF(m_child_nodes); + } -void CParameterCombination::append_child(CParameterCombination* child) +void ParameterCombination::append_child(std::shared_ptr child) { m_child_nodes->append_element(child); } -bool CParameterCombination::set_parameter_helper( +bool ParameterCombination::set_parameter_helper( const char* name, bool value, index_t index) { if (m_param) @@ -165,7 +166,7 @@ bool CParameterCombination::set_parameter_helper( return false; } -bool CParameterCombination::set_parameter_helper( +bool ParameterCombination::set_parameter_helper( const char* name, int32_t value, index_t index) { if (m_param) @@ -194,7 +195,7 @@ bool CParameterCombination::set_parameter_helper( return false; } -bool CParameterCombination::set_parameter_helper( +bool ParameterCombination::set_parameter_helper( const char* name, float64_t value, index_t index) { if (m_param) @@ -225,7 +226,7 @@ bool CParameterCombination::set_parameter_helper( } -TParameter* CParameterCombination::get_parameter_helper(const char* name) +TParameter* ParameterCombination::get_parameter_helper(const char* name) { if (m_param) { @@ -241,8 +242,8 @@ TParameter* CParameterCombination::get_parameter_helper(const char* name) } -TParameter* CParameterCombination::get_parameter(const char* name, - CSGObject* parent) +TParameter* ParameterCombination::get_parameter(const char* name, + SGObject* parent) { bool match = false; @@ -252,8 +253,8 @@ TParameter* CParameterCombination::get_parameter(const char* name, { if (m_param->get_parameter(i)->m_datatype.m_ptype==PT_SGOBJECT) { - CSGObject* obj = - (*((CSGObject**)m_param->get_parameter(i)->m_parameter)); + SGObject* obj = + (*((SGObject**)m_param->get_parameter(i)->m_parameter)); if (parent == obj) match = true; } @@ -263,8 +264,8 @@ TParameter* CParameterCombination::get_parameter(const char* name, for (index_t i = 0; i < m_child_nodes->get_num_elements(); ++i) { - CParameterCombination* child = (CParameterCombination*) - m_child_nodes->get_element(i); + auto child = + m_child_nodes->get_element(i); TParameter* p; @@ -276,29 +277,29 @@ TParameter* CParameterCombination::get_parameter(const char* name, if (p) { - SG_UNREF(child); + return p; } - SG_UNREF(child); + } return NULL; } -void CParameterCombination::merge_with(CParameterCombination* node) +void ParameterCombination::merge_with(std::shared_ptr node) { for (index_t i=0; im_child_nodes->get_num_elements(); ++i) { - CParameterCombination* child= - (CParameterCombination*)node->m_child_nodes->get_element(i); + auto child= + node->m_child_nodes->get_element(i); append_child(child->copy_tree()); - SG_UNREF(child); + } } -void CParameterCombination::print_tree(int prefix_num) const +void ParameterCombination::print_tree(int prefix_num) const { /* prefix is enlarged */ char* prefix=SG_MALLOC(char, prefix_num+1); @@ -323,7 +324,7 @@ void CParameterCombination::print_tree(int prefix_num) const if (m_param->get_parameter(i)->m_datatype.m_ptype==PT_SGOBJECT) { TParameter* param=m_param->get_parameter(i); - CSGObject* current_sgobject=*((CSGObject**) param->m_parameter); + SGObject* current_sgobject=*((SGObject**) param->m_parameter); io::print("\"{}\":{} at {} ", param->m_name, current_sgobject->get_name(), fmt::ptr(current_sgobject)); } @@ -364,16 +365,16 @@ void CParameterCombination::print_tree(int prefix_num) const for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* child=(CParameterCombination*) - m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); child->print_tree(prefix_num+1); - SG_UNREF(child); + } SG_FREE(prefix); } -DynArray* CParameterCombination::parameter_set_multiplication( +DynArray* ParameterCombination::parameter_set_multiplication( const DynArray& set_1, const DynArray& set_2) { SG_DEBUG("entering CParameterCombination::parameter_set_multiplication()") @@ -409,32 +410,32 @@ DynArray* CParameterCombination::parameter_set_multiplication( return result; } -CDynamicObjectArray* CParameterCombination::leaf_sets_multiplication( - const CDynamicObjectArray& sets, const CParameterCombination* new_root) +std::shared_ptr ParameterCombination::leaf_sets_multiplication( + const DynamicObjectArray& sets, std::shared_ptr new_root) { - CDynamicObjectArray* result=new CDynamicObjectArray(); + auto result=std::make_shared(); /* check marginal cases */ if (sets.get_num_elements()==1) { - CDynamicObjectArray* current_set= - (CDynamicObjectArray*)sets.get_element(0); + auto current_set= + sets.get_element(0); /* just use the only element into result array. * put root node before all combinations*/ *result=*current_set; - SG_UNREF(current_set); + for (index_t i=0; iget_num_elements(); ++i) { /* put new root as root into the tree and replace tree */ - CParameterCombination* current=(CParameterCombination*) - result->get_element(i); - CParameterCombination* root=new_root->copy_tree(); + auto current= + result->get_element(i); + auto root=new_root->copy_tree(); root->append_child(current); result->set_element(root, i); - SG_UNREF(current); + } } else if (sets.get_num_elements()>1) @@ -446,15 +447,15 @@ CDynamicObjectArray* CParameterCombination::leaf_sets_multiplication( for (index_t set_nr=0; set_nr(set_nr); DynArray* new_param_set=new DynArray (); param_sets.append_element(new_param_set); for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* current_node=(CParameterCombination*) - current_set->get_element(i); + auto current_node= + current_set->get_element(i); if (current_node->m_child_nodes->get_num_elements()) { @@ -472,10 +473,10 @@ CDynamicObjectArray* CParameterCombination::leaf_sets_multiplication( "leafs have non-NULL Parameter instances"); } - SG_UNREF(current_node); + } - SG_UNREF(current_set); + } /* second, build products of all parameter sets */ @@ -511,11 +512,11 @@ CDynamicObjectArray* CParameterCombination::leaf_sets_multiplication( for (index_t i=0; iget_num_elements(); ++i) { /* build parameter node from parameter product to append to root */ - CParameterCombination* param_node=new CParameterCombination( + auto param_node=std::make_shared( param_product->get_element(i)); /* copy new root node, has to be a new one each time */ - CParameterCombination* root=new_root->copy_tree(); + auto root=new_root->copy_tree(); /* append both and add them to result set */ root->append_child(param_node); @@ -530,12 +531,12 @@ CDynamicObjectArray* CParameterCombination::leaf_sets_multiplication( return result; } -CDynamicObjectArray* CParameterCombination::non_value_tree_multiplication( - const CDynamicObjectArray* sets, - const CParameterCombination* new_root) +std::shared_ptr ParameterCombination::non_value_tree_multiplication( + std::shared_ptr sets, + std::shared_ptr new_root) { - SG_DEBUG("entering CParameterCombination::non_value_tree_multiplication()") - CDynamicObjectArray* result=new CDynamicObjectArray(); + SG_DEBUG("entering ParameterCombination::non_value_tree_multiplication()") + auto result=std::make_shared(); /* first step: get all names in the sets */ std::set names; @@ -543,22 +544,21 @@ CDynamicObjectArray* CParameterCombination::non_value_tree_multiplication( for (index_t j=0; jget_num_elements(); ++j) { - CDynamicObjectArray* current_set= - (CDynamicObjectArray*) - sets->get_element(j); + auto current_set= + sets->get_element(j); for (index_t k=0; k get_num_elements(); ++k) { - CParameterCombination* current_tree=(CParameterCombination*) - current_set->get_element(k); + auto current_tree= + current_set->get_element(k); names.insert(string(current_tree->m_param->get_parameter(0)->m_name)); - SG_UNREF(current_tree); + } - SG_UNREF(current_set); + } SG_DEBUG("all names") @@ -575,23 +575,23 @@ CDynamicObjectArray* CParameterCombination::non_value_tree_multiplication( /* extract all trees with first name */ const char* first_name=(*(names.begin())).c_str(); - CDynamicObjectArray* trees= - CParameterCombination::extract_trees_with_name(sets, first_name); + auto trees= + ParameterCombination::extract_trees_with_name(sets, first_name); SG_DEBUG("adding trees for first name \"{}\":", first_name) for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* current_tree= - (CParameterCombination*)trees->get_element(i); + auto current_tree= + trees->get_element(i); - CParameterCombination* current_root=new_root->copy_tree(); + auto current_root=new_root->copy_tree(); current_root->append_child(current_tree); result->append_element(current_root); // current_tree->print_tree(1); - SG_UNREF(current_tree); + } - SG_UNREF(trees); + /* now iterate over the remaining names and build products */ SG_DEBUG("building products with remaining trees:") @@ -602,27 +602,27 @@ CDynamicObjectArray* CParameterCombination::non_value_tree_multiplication( /* extract all trees with current name */ const char* current_name=(*it).c_str(); - trees=CParameterCombination::extract_trees_with_name(sets, + trees=ParameterCombination::extract_trees_with_name(sets, current_name); /* create new set of trees where each element is put once for each * of the just generated trees */ - CDynamicObjectArray* new_result=new CDynamicObjectArray(); + auto new_result=std::make_shared(); for (index_t i=0; iget_num_elements(); ++i) { for (index_t j=0; jget_num_elements(); ++j) { - CParameterCombination* to_copy= - (CParameterCombination*)result->get_element(i); + auto to_copy= + result->get_element(i); /* create a copy of current element */ - CParameterCombination* new_element=to_copy->copy_tree(); - SG_UNREF(to_copy); + auto new_element=to_copy->copy_tree(); - CParameterCombination* to_add= - (CParameterCombination*)trees->get_element(j); + + auto to_add= + trees->get_element(j); new_element->append_child(to_add); - SG_UNREF(to_add); + new_result->append_element(new_element); // SG_DEBUG("added:") // new_element->print_tree(); @@ -630,10 +630,10 @@ CDynamicObjectArray* CParameterCombination::non_value_tree_multiplication( } /* clean up */ - SG_UNREF(trees); + /* replace result by new_result */ - SG_UNREF(result); + result=new_result; } } @@ -642,39 +642,39 @@ CDynamicObjectArray* CParameterCombination::non_value_tree_multiplication( return result; } -CDynamicObjectArray* CParameterCombination::extract_trees_with_name( - const CDynamicObjectArray* sets, const char* desired_name) +std::shared_ptr ParameterCombination::extract_trees_with_name( + std::shared_ptr sets, const char* desired_name) { - CDynamicObjectArray* result=new CDynamicObjectArray(); + auto result=std::make_shared(); for (index_t j=0; jget_num_elements(); ++j) { - CDynamicObjectArray* current_set= - (CDynamicObjectArray*) sets->get_element(j); + auto current_set= + sets->get_element(j); for (index_t k=0; kget_num_elements(); ++k) { - CParameterCombination* current_tree=(CParameterCombination*) - current_set->get_element(k); + auto current_tree= + current_set->get_element(k); char* current_name=current_tree->m_param->get_parameter(0)->m_name; if (!strcmp(current_name, desired_name)) result->append_element(current_tree); - SG_UNREF(current_tree); + } - SG_UNREF(current_set); + } return result; } -CParameterCombination* CParameterCombination::copy_tree() const +std::shared_ptr ParameterCombination::copy_tree() const { - CParameterCombination* copy=new CParameterCombination(); + auto copy=std::make_shared(); /* but build new Parameter instance */ @@ -689,21 +689,21 @@ CParameterCombination* CParameterCombination::copy_tree() const /* recursively copy all children */ for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* child=(CParameterCombination*) - m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); copy->m_child_nodes->append_element(child->copy_tree()); - SG_UNREF(child); + } return copy; } -void CParameterCombination::apply_to_machine(CMachine* machine) const +void ParameterCombination::apply_to_machine(std::shared_ptr machine) const { apply_to_modsel_parameter(machine->m_model_selection_parameters); } -void CParameterCombination::apply_to_modsel_parameter( +void ParameterCombination::apply_to_modsel_parameter( Parameter* parameter) const { /* case root node */ @@ -714,10 +714,10 @@ void CParameterCombination::apply_to_modsel_parameter( * recursion level downwards) */ for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* child=(CParameterCombination*) - m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); child->apply_to_modsel_parameter(parameter); - SG_UNREF(child); + } } /* case parameter node */ @@ -729,29 +729,29 @@ void CParameterCombination::apply_to_modsel_parameter( /* does this node has sub parameters? */ if (has_children()) { - /* if a parameter node has children, it has to have ONE CSGObject as + /* if a parameter node has children, it has to have ONE SGObject as * parameter */ if (m_param->get_num_parameters()>1 || m_param->get_parameter(0)->m_datatype.m_ptype!=PT_SGOBJECT) { error("invalid CParameterCombination node type, has children" " and more than one parameter or is not a " - "CSGObject."); + "SGObject."); } /* cast is now safe */ - CSGObject* current_sgobject= - *((CSGObject**)(m_param->get_parameter(0)->m_parameter)); + SGObject* current_sgobject= + *((SGObject**)(m_param->get_parameter(0)->m_parameter)); /* iterate over all children and recursively set parameters from * their values */ for (index_t i=0; iget_num_elements(); ++i) { - CParameterCombination* child=(CParameterCombination*) - m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); child->apply_to_modsel_parameter( current_sgobject->m_model_selection_parameters); - SG_UNREF(child); + } } } @@ -759,8 +759,8 @@ void CParameterCombination::apply_to_modsel_parameter( error("CParameterCombination node has illegal type."); } -void CParameterCombination::build_parameter_values_map( - CMap >* dict) +void ParameterCombination::build_parameter_values_map( + std::shared_ptr >> dict) { if (m_param) { @@ -791,17 +791,17 @@ void CParameterCombination::build_parameter_values_map( for (index_t i=0; iget_num_elements(); i++) { - CParameterCombination* child=(CParameterCombination*) - m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); child->build_parameter_values_map(dict); - SG_UNREF(child); + } } -void CParameterCombination::build_parameter_parent_map( - CMap* dict) +void ParameterCombination::build_parameter_parent_map( + std::shared_ptr> dict) { - CSGObject* parent=NULL; + SGObject* parent=NULL; if (m_param) { @@ -814,7 +814,7 @@ void CParameterCombination::build_parameter_parent_map( { if (type.m_ctype==CT_SCALAR) { - parent=(*(CSGObject**)param->m_parameter); + parent=(*(SGObject**)param->m_parameter); break; } else @@ -827,8 +827,8 @@ void CParameterCombination::build_parameter_parent_map( for (index_t i=0; iget_num_elements(); i++) { - CParameterCombination* child=(CParameterCombination*) - m_child_nodes->get_element(i); + auto child= + m_child_nodes->get_element(i); for (index_t j=0; jm_param->get_num_parameters(); j++) { @@ -851,6 +851,6 @@ void CParameterCombination::build_parameter_parent_map( dict->add(param, parent); } } - SG_UNREF(child); + } } diff --git a/src/shogun/modelselection/ParameterCombination.h b/src/shogun/modelselection/ParameterCombination.h index aa3d6ae6cd7..a83afc8e7c4 100644 --- a/src/shogun/modelselection/ParameterCombination.h +++ b/src/shogun/modelselection/ParameterCombination.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, + * Authors: Jacob Walker, Heiko Strathmann, Sergey Lisitsyn, Soeren Sonnenburg, * Leon Kuchenbecker, Yuyu Zhang, Roman Votyakov */ @@ -15,8 +15,8 @@ namespace shogun { -class CModelSelectionParameters; -class CMachine; +class ModelSelectionParameters; +class Machine; class Parameter; /** @brief Class that holds ONE combination of parameters for a learning @@ -36,19 +36,19 @@ class Parameter; * * Again: Leafs of the tree may only be Parameter nodes. */ -class CParameterCombination : public CSGObject +class ParameterCombination : public SGObject { -friend class CModelSelectionParameters; +friend class ModelSelectionParameters; public: /** constructor for a root node */ - CParameterCombination(); + ParameterCombination(); /** constructor for a parameter node * * @param param parameter node */ - CParameterCombination(Parameter* param); + ParameterCombination(Parameter* param); /** constructor for an object. Builds parameter combination of the gradient * parameters. @@ -58,12 +58,12 @@ friend class CModelSelectionParameters; * * @param obj object to build parameter combination */ - CParameterCombination(CSGObject* obj); + ParameterCombination(std::shared_ptr obj); /** destructor also recursively destroys complete tree (SG_UNREF of child * nodes) */ - virtual ~CParameterCombination(); + virtual ~ParameterCombination(); /** Prints a representation of the current node * @@ -86,19 +86,19 @@ friend class CModelSelectionParameters; * * @param machine learning machine to apply parameter tree to */ - void apply_to_machine(CMachine* machine) const; + void apply_to_machine(std::shared_ptr machine) const; /** appends a child to this node * * @param child child to append */ - void append_child(CParameterCombination* child); + void append_child(std::shared_ptr child); /** Adds (copies of) all children of given node * * @param node (copies of) children of given node are added to this one */ - void merge_with(CParameterCombination* node); + void merge_with(std::shared_ptr node); /** Copies the complete tree of this node. Note that nodes are actually * copied. If this is a parameter node, a NEW Parameter instance to the same @@ -106,7 +106,7 @@ friend class CModelSelectionParameters; * * @return copy of the tree with this node as root as described above */ - CParameterCombination* copy_tree() const; + std::shared_ptr copy_tree() const; /** Takes a set of sets of leafs nodes (!) and produces a set of instances * of this class that contain every combination of the parameters in the leaf @@ -120,9 +120,9 @@ friend class CModelSelectionParameters; * trees * @result result set of tree combinations */ - static CDynamicObjectArray* leaf_sets_multiplication( - const CDynamicObjectArray& sets, - const CParameterCombination* new_root); + static std::shared_ptr leaf_sets_multiplication( + const DynamicObjectArray& sets, + std::shared_ptr new_root); /** Sets specific parameter to specified value. * @@ -135,7 +135,7 @@ friend class CModelSelectionParameters; */ template bool set_parameter(const char* name, - T value, CSGObject* parent, index_t index = -1) + T value, SGObject* parent, index_t index = -1) { bool match = false; @@ -148,7 +148,7 @@ friend class CModelSelectionParameters; if (m_param->get_parameter(i)->m_datatype.m_ptype ==PT_SGOBJECT) { - if (parent == (*((CSGObject**)param))) + if (parent == (*((SGObject**)param))) match = true; } @@ -160,8 +160,8 @@ friend class CModelSelectionParameters; for (index_t i = 0; i < m_child_nodes->get_num_elements(); ++i) { - CParameterCombination* child = (CParameterCombination*) - m_child_nodes->get_element(i); + auto child = + m_child_nodes->get_element(i); if (!match) result |= child->set_parameter(name, value, parent, index); @@ -169,7 +169,7 @@ friend class CModelSelectionParameters; else result |= child->set_parameter_helper(name, value, index); - SG_UNREF(child); + } @@ -183,7 +183,7 @@ friend class CModelSelectionParameters; * * return specified parameter. NULL if not found. */ - TParameter* get_parameter(const char* name, CSGObject* parent); + TParameter* get_parameter(const char* name, SGObject* parent); /** checks whether this node has children * @@ -215,14 +215,14 @@ friend class CModelSelectionParameters; * * @param param_combination its dynamic type must be CParameterCombination */ - static CParameterCombination* obtain_from_generic( - CSGObject* param_combination) + static std::shared_ptr obtain_from_generic( + std::shared_ptr param_combination) { if (param_combination) { - CParameterCombination* casted = dynamic_cast(param_combination); + auto casted = param_combination->as(); require(casted, "Error, provided object of class \"{}\" is not a subclass of" - " CParameterCombination!", + " ParameterCombination!", param_combination->get_name()); return casted; } @@ -243,14 +243,14 @@ friend class CModelSelectionParameters; * @param values_map map, which contains parameters and its values */ virtual void build_parameter_values_map( - CMap >* values_map); + std::shared_ptr >> values_map); /** builds map, which contains parameters and its parents * * @param parent_map map, which contains parameters and its parents */ virtual void build_parameter_parent_map( - CMap* parent_map); + std::shared_ptr> parent_map); protected: /** Takes a set of sets of (non-value) trees and returns a set with all @@ -263,9 +263,9 @@ friend class CModelSelectionParameters; * @return set of trees with the given root as root and all combinations * of the trees in the sets as children */ - static CDynamicObjectArray* non_value_tree_multiplication( - const CDynamicObjectArray* sets, - const CParameterCombination* new_root); + static std::shared_ptr non_value_tree_multiplication( + std::shared_ptr sets, + std::shared_ptr new_root); /** Takes a set of sets of trees and extracts all trees with a given name. * Assumes that in a (inner) set, all trees have the same name on their @@ -275,8 +275,8 @@ friend class CModelSelectionParameters; * @param desired_name tree with this name is searched * @return set of trees with the desired name */ - static CDynamicObjectArray* extract_trees_with_name( - const CDynamicObjectArray* sets, const char* desired_name); + static std::shared_ptr extract_trees_with_name( + std::shared_ptr sets, const char* desired_name); /** Gets parameter by name in current node. * @@ -323,7 +323,7 @@ friend class CModelSelectionParameters; Parameter* m_param; /** child parameters */ - CDynamicObjectArray* m_child_nodes; + std::shared_ptr m_child_nodes; /** total length of the parameters in combination */ uint32_t m_parameters_length; diff --git a/src/shogun/modelselection/RandomSearchModelSelection.cpp b/src/shogun/modelselection/RandomSearchModelSelection.cpp index 34ecf264b8c..904bf1645ed 100644 --- a/src/shogun/modelselection/RandomSearchModelSelection.cpp +++ b/src/shogun/modelselection/RandomSearchModelSelection.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Giovanni De Toni, Heiko Strathmann, Thoralf Klein, + * Authors: Giovanni De Toni, Heiko Strathmann, Thoralf Klein, * Soeren Sonnenburg, Sergey Lisitsyn, Roman Votyakov, Kyle McQuisten */ @@ -15,62 +15,62 @@ using namespace shogun; -CRandomSearchModelSelection::CRandomSearchModelSelection() : RandomMixin() +RandomSearchModelSelection::RandomSearchModelSelection() : RandomMixin() { set_ratio(0.5); } -CRandomSearchModelSelection::CRandomSearchModelSelection( - CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters, float64_t ratio) - : RandomMixin(machine_eval, model_parameters) +RandomSearchModelSelection::RandomSearchModelSelection( + std::shared_ptr machine_eval, + std::shared_ptr model_parameters, float64_t ratio) + : RandomMixin(machine_eval, model_parameters) { set_ratio(ratio); } -CRandomSearchModelSelection::~CRandomSearchModelSelection() +RandomSearchModelSelection::~RandomSearchModelSelection() { } -CParameterCombination* CRandomSearchModelSelection::select_model(bool print_state) +std::shared_ptr RandomSearchModelSelection::select_model(bool print_state) { if (print_state) io::print("Generating parameter combinations\n"); /* Retrieve all possible parameter combinations */ - CDynamicObjectArray* all_combinations= - (CDynamicObjectArray*)m_model_parameters->get_combinations(); + auto all_combinations= + std::static_pointer_cast(m_model_parameters->get_combinations()); int32_t n_all_combinations=all_combinations->get_num_elements(); - SGVector combinations_indices=CStatistics::sample_indices(n_all_combinations*m_ratio, n_all_combinations, m_prng); + SGVector combinations_indices=Statistics::sample_indices(n_all_combinations*m_ratio, n_all_combinations, m_prng); - CDynamicObjectArray* combinations=new CDynamicObjectArray(); + auto combinations=std::make_shared(); for (int32_t i=0; iappend_element(all_combinations->get_element(i)); - CCrossValidationResult* best_result=new CCrossValidationResult(); + auto best_result=std::make_shared(); - CParameterCombination* best_combination=NULL; + std::shared_ptr best_combination=NULL; if (m_machine_eval->get_evaluation_direction()==ED_MAXIMIZE) { if (print_state) io::print("Direction is maximize\n"); - best_result->set_mean(CMath::ALMOST_NEG_INFTY); + best_result->set_mean(Math::ALMOST_NEG_INFTY); } else { if (print_state) io::print("Direction is minimize\n"); - best_result->set_mean(CMath::ALMOST_INFTY); + best_result->set_mean(Math::ALMOST_INFTY); } /* underlying learning machine */ - CMachine* machine=m_machine_eval->get_machine(); + auto machine=m_machine_eval->get_machine(); /* apply all combinations and search for best one */ for (auto i : SG_PROGRESS(range(combinations->get_num_elements()))) { - CParameterCombination* current_combination=(CParameterCombination*) - combinations->get_element(i); + auto current_combination= + combinations->get_element(i); /* eventually print */ if (print_state) @@ -83,8 +83,8 @@ CParameterCombination* CRandomSearchModelSelection::select_model(bool print_stat machine->m_model_selection_parameters); /* note that this may implicitly lock and unlockthe machine */ - CCrossValidationResult* result = - m_machine_eval->evaluate()->as(); + auto result = + m_machine_eval->evaluate()->as(); if (print_state) result->print_result(); @@ -94,21 +94,18 @@ CParameterCombination* CRandomSearchModelSelection::select_model(bool print_stat { if (result->get_mean() > best_result->get_mean()) { - if (best_combination) - SG_UNREF(best_combination); + best_combination= + combinations->get_element(i); + - best_combination=(CParameterCombination*) - combinations->get_element(i); - SG_REF(result); - SG_UNREF(best_result); best_result=result; } else { - CParameterCombination* combination=(CParameterCombination*) - combinations->get_element(i); - SG_UNREF(combination); + auto combination= + combinations->get_element(i); + } } else @@ -116,30 +113,26 @@ CParameterCombination* CRandomSearchModelSelection::select_model(bool print_stat if (result->get_mean() < best_result->get_mean()) { if (best_combination) - SG_UNREF(best_combination); - best_combination=(CParameterCombination*) - combinations->get_element(i); - SG_REF(result); - SG_UNREF(best_result); + best_combination= + combinations->get_element(i); + + + best_result=result; } else { - CParameterCombination* combination=(CParameterCombination*) - combinations->get_element(i); - SG_UNREF(combination); + auto combination= + combinations->get_element(i); + } } - SG_UNREF(result); - SG_UNREF(current_combination); - } - SG_UNREF(best_result); - SG_UNREF(machine); - SG_UNREF(combinations); + + } return best_combination; } diff --git a/src/shogun/modelselection/RandomSearchModelSelection.h b/src/shogun/modelselection/RandomSearchModelSelection.h index 7ac328cfb16..795ef553378 100644 --- a/src/shogun/modelselection/RandomSearchModelSelection.h +++ b/src/shogun/modelselection/RandomSearchModelSelection.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Roman Votyakov, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Roman Votyakov, * Heiko Strathmann, Yuyu Zhang */ @@ -16,16 +16,16 @@ namespace shogun { -class CModelSelectionParameters; +class ModelSelectionParameters; /** @brief Model selection class which searches for the best model by a random - * search. See CModelSelection for details. + * search. See ModelSelection for details. */ -class CRandomSearchModelSelection : public RandomMixin +class RandomSearchModelSelection : public RandomMixin { public: /** constructor */ - CRandomSearchModelSelection(); + RandomSearchModelSelection(); /** constructor * @@ -33,11 +33,11 @@ class CRandomSearchModelSelection : public RandomMixin * @param model_parameters model parameters to use * @param ratio ratio in range [0,1] */ - CRandomSearchModelSelection(CMachineEvaluation* machine_eval, - CModelSelectionParameters* model_parameters, float64_t ratio); + RandomSearchModelSelection(std::shared_ptr machine_eval, + std::shared_ptr model_parameters, float64_t ratio); /** destructor */ - virtual ~CRandomSearchModelSelection(); + virtual ~RandomSearchModelSelection(); /** returns ratio * @@ -61,7 +61,7 @@ class CRandomSearchModelSelection : public RandomMixin * * @return best combination of model parameters */ - virtual CParameterCombination* select_model(bool print_state=false); + virtual std::shared_ptr select_model(bool print_state=false); /** @return name of the SGSerializable */ virtual const char* get_name() const { return "RandomSearchModelSelection"; } diff --git a/src/shogun/multiclass/BruteKNNSolver.cpp b/src/shogun/multiclass/BruteKNNSolver.cpp index 7fb864ea2c9..812564c4385 100644 --- a/src/shogun/multiclass/BruteKNNSolver.cpp +++ b/src/shogun/multiclass/BruteKNNSolver.cpp @@ -9,16 +9,16 @@ using namespace shogun; -CBruteKNNSolver::CBruteKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const SGMatrix NN): -CKNNSolver(k, q, num_classes, min_label, train_labels) +BruteKNNSolver::BruteKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const SGMatrix NN): +KNNSolver(k, q, num_classes, min_label, train_labels) { init(); nn=NN; } -CMulticlassLabels* CBruteKNNSolver::classify_objects(CDistance* knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +std::shared_ptr BruteKNNSolver::classify_objects(std::shared_ptr knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { - CMulticlassLabels* output=new CMulticlassLabels(num_lab); + auto output=std::make_shared(num_lab); //get the k nearest neighbors of each example SGMatrix NN = this->nn; @@ -40,7 +40,7 @@ CMulticlassLabels* CBruteKNNSolver::classify_objects(CDistance* knn_distance, co return output; } -SGVector CBruteKNNSolver::classify_objects_k(CDistance* knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +SGVector BruteKNNSolver::classify_objects_k(std::shared_ptr knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { SGVector output(m_k*num_lab); diff --git a/src/shogun/multiclass/BruteKNNSolver.h b/src/shogun/multiclass/BruteKNNSolver.h index b0c5f1150ba..7e507355bad 100644 --- a/src/shogun/multiclass/BruteKNNSolver.h +++ b/src/shogun/multiclass/BruteKNNSolver.h @@ -16,17 +16,17 @@ namespace shogun { /* Standard KNN solver. Test points are compared to all training data for each prediction. */ -class CBruteKNNSolver : public CKNNSolver +class BruteKNNSolver : public KNNSolver { public: /** default constructor */ - CBruteKNNSolver() : CKNNSolver() + BruteKNNSolver() : KNNSolver() { init(); } /** deconstructor */ - virtual ~CBruteKNNSolver() { /* nothing to do */ } + virtual ~BruteKNNSolver() { /* nothing to do */ } /** constructor * @@ -37,11 +37,11 @@ class CBruteKNNSolver : public CKNNSolver * @param train_labels m_train_labels * @param NN nn */ - CBruteKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const SGMatrix NN); + BruteKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const SGMatrix NN); - virtual CMulticlassLabels* classify_objects(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + virtual std::shared_ptr classify_objects(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; - virtual SGVector classify_objects_k(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + virtual SGVector classify_objects_k(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; /** @return object name */ const char* get_name() const { return "BruteKNNSolver"; } diff --git a/src/shogun/multiclass/GMNPLib.cpp b/src/shogun/multiclass/GMNPLib.cpp index 134c0b82f2d..e5d12fff19c 100644 --- a/src/shogun/multiclass/GMNPLib.cpp +++ b/src/shogun/multiclass/GMNPLib.cpp @@ -22,7 +22,7 @@ using namespace shogun; #define KDELTA(A,B) (A==B) #define KDELTA4(A1,A2,A3,A4) ((A1==A2)||(A1==A3)||(A1==A4)||(A2==A3)||(A2==A4)||(A3==A4)) -CGMNPLib::CGMNPLib() +GMNPLib::GMNPLib() { unstable(SOURCE_LOCATION); @@ -42,10 +42,10 @@ CGMNPLib::CGMNPLib() m_num_classes = 0; } -CGMNPLib::CGMNPLib( - float64_t* vector_y, CKernel* kernel, int32_t num_data, +GMNPLib::GMNPLib( + float64_t* vector_y, std::shared_ptr kernel, int32_t num_data, int32_t num_virt_data, int32_t num_classes, float64_t reg_const) -: CSGObject() +: SGObject() { m_num_classes=num_classes; m_num_virt_data=num_virt_data; @@ -55,7 +55,7 @@ CGMNPLib::CGMNPLib( m_kernel = kernel; Cache_Size = ((int64_t) kernel->get_cache_size())*1024*1024/(sizeof(float64_t)*num_data); - Cache_Size = CMath::min(Cache_Size, (int64_t) num_data); + Cache_Size = Math::min(Cache_Size, (int64_t) num_data); io::info("using {} kernel cache lines", Cache_Size); ASSERT(Cache_Size>=2) @@ -85,7 +85,7 @@ CGMNPLib::CGMNPLib( diag_H[i] = kernel_fce(i,i); } -CGMNPLib::~CGMNPLib() +GMNPLib::~GMNPLib() { for(int32_t i = 0; i < Cache_Size; i++ ) SG_FREE(kernel_columns[i]); @@ -103,7 +103,7 @@ CGMNPLib::~CGMNPLib() Returns pointer at a-th column of the kernel matrix. This function maintains FIFO cache of kernel columns. ------------------------------------------------------------ */ -float64_t* CGMNPLib::get_kernel_col( int32_t a ) +float64_t* GMNPLib::get_kernel_col( int32_t a ) { float64_t *col_ptr; int32_t i; @@ -136,7 +136,7 @@ float64_t* CGMNPLib::get_kernel_col( int32_t a ) Computes index of input example and its class label from index of virtual "single-class" example. ------------------------------------------------------------ */ -void CGMNPLib::get_indices2( int32_t *index, int32_t *c, int32_t i ) +void GMNPLib::get_indices2( int32_t *index, int32_t *c, int32_t i ) { *index = i / (m_num_classes-1); @@ -154,7 +154,7 @@ void CGMNPLib::get_indices2( int32_t *index, int32_t *c, int32_t i ) updating but b is from (a(t-2), a(t-1)) where a=a(t) and thus FIFO with three columns does not have to take care od b.) ------------------------------------------------------------ */ -float64_t* CGMNPLib::get_col( int32_t a, int32_t b ) +float64_t* GMNPLib::get_col( int32_t a, int32_t b ) { int32_t i; float64_t *col_ptr; @@ -202,7 +202,7 @@ float64_t* CGMNPLib::get_col( int32_t a, int32_t b ) tmax, tolabs, tolrel, th, &alpha, &t, &History ); -------------------------------------------------------------- */ -int8_t CGMNPLib::gmnp_imdm(float64_t *vector_c, +int8_t GMNPLib::gmnp_imdm(float64_t *vector_c, int32_t dim, int32_t tmax, float64_t tolabs, @@ -285,7 +285,7 @@ int8_t CGMNPLib::gmnp_imdm(float64_t *vector_c, /* Stopping conditions */ if( UB-LB <= tolabs ) exitflag = 1; - else if(UB-LB <= CMath::abs(UB)*tolrel ) exitflag = 2; + else if(UB-LB <= Math::abs(UB)*tolrel ) exitflag = 2; else if(LB > th ) exitflag = 3; else exitflag = -1; @@ -361,14 +361,14 @@ int8_t CGMNPLib::gmnp_imdm(float64_t *vector_c, /* Stopping conditions */ if( UB-LB <= tolabs ) exitflag = 1; - else if( UB-LB <= CMath::abs(UB)*tolrel) exitflag = 2; + else if( UB-LB <= Math::abs(UB)*tolrel) exitflag = 2; else if(LB > th ) exitflag = 3; else if(t >= tmax) exitflag = 0; /* print info */ pb.print_absolute( - CMath::abs((UB - LB) / UB), -CMath::log10(CMath::abs(UB - LB)), - -CMath::log10(1.0), -CMath::log10(tolrel)); + Math::abs((UB - LB) / UB), -Math::log10(Math::abs(UB - LB)), + -Math::log10(1.0), -Math::log10(tolrel)); if (verb && (t % verb) == 0) { @@ -426,7 +426,7 @@ int8_t CGMNPLib::gmnp_imdm(float64_t *vector_c, Retures (a,b)-th element of the virtual kernel matrix of size [num_virt_data x num_virt_data]. ------------------------------------------------------------ */ -float64_t CGMNPLib::kernel_fce( int32_t a, int32_t b ) +float64_t GMNPLib::kernel_fce( int32_t a, int32_t b ) { float64_t value; int32_t i1,c1,i2,c2; diff --git a/src/shogun/multiclass/GMNPLib.h b/src/shogun/multiclass/GMNPLib.h index b174bd44084..f0cdc4ebae3 100644 --- a/src/shogun/multiclass/GMNPLib.h +++ b/src/shogun/multiclass/GMNPLib.h @@ -53,11 +53,11 @@ namespace shogun * Methods. Research report. CTU-CMP-2005-22. CTU FEL Prague. 2005. * ftp://cmp.felk.cvut.cz/pub/cmp/articles/franc/Franc-PhD.pdf . */ -class CGMNPLib: public CSGObject +class GMNPLib: public SGObject { public: /** default constructor */ - CGMNPLib(); + GMNPLib(); /** constructor * @@ -68,11 +68,11 @@ class CGMNPLib: public CSGObject * @param num_classes number of classes * @param reg_const reg const */ - CGMNPLib( - float64_t* vector_y, CKernel* kernel, int32_t num_data, + GMNPLib( + float64_t* vector_y, std::shared_ptr kernel, int32_t num_data, int32_t num_virtual_data, int32_t num_classes, float64_t reg_const); - virtual ~CGMNPLib(); + virtual ~GMNPLib(); /** -------------------------------------------------------------- GMNP solver based on improved MDM algorithm 1. @@ -147,7 +147,7 @@ tmax, tolabs, tolrel, th, &alpha, &t, &History ); /** vectory */ float64_t* m_vector_y; /** kernel */ - CKernel* m_kernel; + std::shared_ptr m_kernel; /** index of first used column */ int32_t first_virt_inx; diff --git a/src/shogun/multiclass/GMNPSVM.cpp b/src/shogun/multiclass/GMNPSVM.cpp index ec63485c633..af1e417cf40 100644 --- a/src/shogun/multiclass/GMNPSVM.cpp +++ b/src/shogun/multiclass/GMNPSVM.cpp @@ -18,37 +18,37 @@ using namespace shogun; -CGMNPSVM::CGMNPSVM() -: CMulticlassSVM(new CMulticlassOneVsRestStrategy()) +GMNPSVM::GMNPSVM() +: MulticlassSVM(std::make_shared()) { init(); } -CGMNPSVM::CGMNPSVM(float64_t C, CKernel* k, CLabels* lab) -: CMulticlassSVM(new CMulticlassOneVsRestStrategy(), C, k, lab) +GMNPSVM::GMNPSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: MulticlassSVM(std::make_shared(), C, k, lab) { init(); } -CGMNPSVM::~CGMNPSVM() +GMNPSVM::~GMNPSVM() { if (m_basealphas != NULL) SG_FREE(m_basealphas); } void -CGMNPSVM::init() +GMNPSVM::init() { - m_parameters->add_matrix(&m_basealphas, + /*m_parameters->add_matrix(&m_basealphas, &m_basealphas_y, &m_basealphas_x, "m_basealphas", - "Is the basic untransformed alpha."); + "Is the basic untransformed alpha.");*/ watch_param( "m_basealphas", &m_basealphas, &m_basealphas_y, &m_basealphas_x); m_basealphas = NULL, m_basealphas_y = 0, m_basealphas_x = 0; } -bool CGMNPSVM::train_machine(CFeatures* data) +bool GMNPSVM::train_machine(std::shared_ptr data) { ASSERT(m_kernel) ASSERT(m_labels && m_labels->get_num_labels()) @@ -73,9 +73,10 @@ bool CGMNPSVM::train_machine(CFeatures* data) io::info("{} trainlabels, {} classes", num_data, num_classes); float64_t* vector_y = SG_MALLOC(float64_t, num_data); + auto mc = multiclass_labels(m_labels); for (int32_t i=0; iget_label(i)+1; + vector_y[i] = mc->get_label(i)+1; } @@ -98,7 +99,7 @@ bool CGMNPSVM::train_machine(CFeatures* data) float64_t* History = NULL; int32_t verb = 0; - CGMNPLib mnp(vector_y,m_kernel,num_data, num_virtual_data, num_classes, reg_const); + GMNPLib mnp(vector_y,m_kernel,num_data, num_virtual_data, num_classes, reg_const); mnp.gmnp_imdm(vector_c, num_virtual_data, tmax, tolabs, tolrel, thlb, alpha, &t, &History, verb); @@ -140,7 +141,7 @@ bool CGMNPSVM::train_machine(CFeatures* data) ASSERT(num_sv>0) SG_DEBUG("svm[{}] has {} sv, b={}", i, num_sv, all_bs[i]) - CSVM* svm=new CSVM(num_sv); + auto svm=std::make_shared(num_sv); int32_t k=0; for (int32_t j=0; j k, std::shared_ptr lab); /** default destructor */ - virtual ~CGMNPSVM(); + virtual ~GMNPSVM(); /** get classifier type * @@ -44,14 +44,14 @@ class CGMNPSVM : public CMulticlassSVM */ virtual EMachineType get_classifier_type() { return CT_GMNPSVM; } - /** required for CMKLMulticlass constraint computation + /** required for MKLMulticlass constraint computation * * @param y height of basealphas * @param x width of basealphas * * @return basealphas basealphas[k][j] is the alpha for class * k and sample j which is untransformed compared to - * the alphas stored in CSVM* members + * the alphas stored in SVM* members */ float64_t* get_basealphas_ptr(index_t* y, index_t* x); @@ -67,12 +67,12 @@ class CGMNPSVM : public CMulticlassSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); protected: - /** required for CMKLMulticlass + /** required for MKLMulticlass * stores the untransformed alphas of this algorithm - * whereas CSVM* members stores a transformed version of it + * whereas SVM* members stores a transformed version of it * m_basealphas[k][j] is the alpha for class k and sample j */ // is the basic untransformed alpha, needed for MKL diff --git a/src/shogun/multiclass/GaussianNaiveBayes.cpp b/src/shogun/multiclass/GaussianNaiveBayes.cpp index a479854c79f..d669966d1e6 100644 --- a/src/shogun/multiclass/GaussianNaiveBayes.cpp +++ b/src/shogun/multiclass/GaussianNaiveBayes.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Viktor Gal, Giovanni De Toni, Soeren Sonnenburg, + * Authors: Sergey Lisitsyn, Viktor Gal, Giovanni De Toni, Soeren Sonnenburg, * Thoralf Klein, Bjoern Esser */ @@ -18,15 +18,15 @@ using namespace shogun; -CGaussianNaiveBayes::CGaussianNaiveBayes() : CNativeMulticlassMachine(), m_features(NULL), +GaussianNaiveBayes::GaussianNaiveBayes() : NativeMulticlassMachine(), m_features(NULL), m_min_label(0), m_num_classes(0), m_dim(0), m_means(), m_variances(), m_label_prob(), m_rates() { init(); }; -CGaussianNaiveBayes::CGaussianNaiveBayes(CFeatures* train_examples, - CLabels* train_labels) : CNativeMulticlassMachine(), m_features(NULL), +GaussianNaiveBayes::GaussianNaiveBayes(std::shared_ptr train_examples, + std::shared_ptr train_labels) : NativeMulticlassMachine(), m_features(NULL), m_min_label(0), m_num_classes(0), m_dim(0), m_means(), m_variances(), m_label_prob(), m_rates() { @@ -37,38 +37,36 @@ CGaussianNaiveBayes::CGaussianNaiveBayes(CFeatures* train_examples, if (!train_examples->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*)train_examples); + set_features(train_examples->as()); }; -CGaussianNaiveBayes::~CGaussianNaiveBayes() +GaussianNaiveBayes::~GaussianNaiveBayes() { - SG_UNREF(m_features); + }; -CFeatures* CGaussianNaiveBayes::get_features() +std::shared_ptr GaussianNaiveBayes::get_features() { - SG_REF(m_features); + return m_features; } -void CGaussianNaiveBayes::set_features(CFeatures* features) +void GaussianNaiveBayes::set_features(std::shared_ptr features) { if (!features->has_property(FP_DOT)) - error("Specified features are not of type CDotFeatures"); + error("Specified features are not of type DotFeatures"); - SG_REF(features); - SG_UNREF(m_features); - m_features = (CDotFeatures*)features; + m_features = features->as(); } -bool CGaussianNaiveBayes::train_machine(CFeatures* data) +bool GaussianNaiveBayes::train_machine(std::shared_ptr data) { // init features with data if necessary and assure type is correct if (data) { if (!data->has_property(FP_DOT)) - error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + error("Specified features are not of type DotFeatures"); + set_features(data->as()); } // get int labels to train_labels and check length equality @@ -78,8 +76,8 @@ bool CGaussianNaiveBayes::train_machine(CFeatures* data) ASSERT(m_features->get_num_vectors()==train_labels.vlen) // find minimal and maximal label - auto min_label = CMath::min(train_labels.vector, train_labels.vlen); - auto max_label = CMath::max(train_labels.vector, train_labels.vlen); + auto min_label = Math::min(train_labels.vector, train_labels.vlen); + auto max_label = Math::max(train_labels.vector, train_labels.vlen); int i,j; // subtract minimal label from all labels @@ -137,7 +135,7 @@ bool CGaussianNaiveBayes::train_machine(CFeatures* data) for (j=0; j GaussianNaiveBayes::apply_multiclass(std::shared_ptr data) { if (data) set_features(data); @@ -169,7 +167,7 @@ CMulticlassLabels* CGaussianNaiveBayes::apply_multiclass(CFeatures* data) int32_t num_vectors = m_features->get_num_vectors(); // init result labels - CMulticlassLabels* result = new CMulticlassLabels(num_vectors); + auto result = std::make_shared(num_vectors); // classify each example of data for (auto i : SG_PROGRESS(range(num_vectors))) @@ -180,7 +178,7 @@ CMulticlassLabels* CGaussianNaiveBayes::apply_multiclass(CFeatures* data) return result; }; -float64_t CGaussianNaiveBayes::apply_one(int32_t idx) +float64_t GaussianNaiveBayes::apply_one(int32_t idx) { // get [idx] feature vector SGVector feature_vector = m_features->get_computed_dot_feature_vector(idx); @@ -205,7 +203,7 @@ float64_t CGaussianNaiveBayes::apply_one(int32_t idx) if (m_variances(k,i)!=0.0) m_rates.vector[i] += std::log(0.39894228 / std::sqrt(m_variances(k, i))) - - 0.5 * CMath::sq(feature_vector.vector[k] - m_means(k, i)) / + 0.5 * Math::sq(feature_vector.vector[k] - m_means(k, i)) / (m_variances(k, i)); } @@ -221,7 +219,7 @@ float64_t CGaussianNaiveBayes::apply_one(int32_t idx) return max_label_idx+m_min_label; }; -void CGaussianNaiveBayes::init() +void GaussianNaiveBayes::init() { SG_ADD(&m_min_label, "m_min_label", "minimal label"); SG_ADD(&m_num_classes, "m_num_classes", @@ -236,5 +234,5 @@ void CGaussianNaiveBayes::init() "a priori probabilities of labels"); SG_ADD(&m_rates, "m_rates", "label rates"); SG_ADD( - (CFeatures**)&m_features, "features", "Training features"); + (std::shared_ptr*)&m_features, "features", "Training features"); } diff --git a/src/shogun/multiclass/GaussianNaiveBayes.h b/src/shogun/multiclass/GaussianNaiveBayes.h index 95cc8abf900..b579c60d618 100644 --- a/src/shogun/multiclass/GaussianNaiveBayes.h +++ b/src/shogun/multiclass/GaussianNaiveBayes.h @@ -16,9 +16,9 @@ namespace shogun { -class CLabels; -class CDotFeatures; -class CFeatures; +class Labels; +class DotFeatures; +class Features; /** @brief Class GaussianNaiveBayes, a Gaussian Naive Bayes classifier * @@ -31,7 +31,7 @@ class CFeatures; * \f] * */ -class CGaussianNaiveBayes : public CNativeMulticlassMachine +class GaussianNaiveBayes : public NativeMulticlassMachine { public: @@ -40,34 +40,34 @@ class CGaussianNaiveBayes : public CNativeMulticlassMachine /** default constructor * */ - CGaussianNaiveBayes(); + GaussianNaiveBayes(); /** constructor * @param train_examples train examples * @param train_labels labels corresponding to train_examples */ - CGaussianNaiveBayes(CFeatures* train_examples, CLabels* train_labels); + GaussianNaiveBayes(std::shared_ptr train_examples, std::shared_ptr train_labels); /** destructor * */ - virtual ~CGaussianNaiveBayes(); + virtual ~GaussianNaiveBayes(); /** set features for classify * @param features features to be set */ - virtual void set_features(CFeatures* features); + virtual void set_features(std::shared_ptr features); /** get features for classify * @return current features */ - virtual CFeatures* get_features(); + virtual std::shared_ptr get_features(); /** classify specified examples * @param data examples to be classified * @return labels corresponding to data */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /** classifiy specified example * @param idx example index @@ -91,7 +91,7 @@ class CGaussianNaiveBayes : public CNativeMulticlassMachine * @param data train examples * @return true if successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); private: void init(); @@ -99,7 +99,7 @@ class CGaussianNaiveBayes : public CNativeMulticlassMachine protected: /// features for training or classifying - CDotFeatures* m_features; + std::shared_ptr m_features; /// minimal label int32_t m_min_label; diff --git a/src/shogun/multiclass/KDTreeKNNSolver.h b/src/shogun/multiclass/KDTreeKNNSolver.h index 64cd6623c48..91c201d63d1 100644 --- a/src/shogun/multiclass/KDTreeKNNSolver.h +++ b/src/shogun/multiclass/KDTreeKNNSolver.h @@ -21,17 +21,17 @@ namespace shogun * For more information, see https://en.wikipedia.org/wiki/K-d_tree * */ -class CKDTREEKNNSolver : public CKNNSolver +class KDTREEKNNSolver : public KNNSolver { public: /** default constructor */ - CKDTREEKNNSolver() : CKNNSolver() + KDTREEKNNSolver() : KNNSolver() { init(); } /** deconstructor */ - virtual ~CKDTREEKNNSolver() { /* nothing to do */ } + virtual ~KDTREEKNNSolver() { /* nothing to do */ } /** constructor * @@ -42,11 +42,11 @@ class CKDTREEKNNSolver : public CKNNSolver * @param train_labels m_train_labels * @param leaf_size m_leaf_size */ - CKDTREEKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t leaf_size); + KDTREEKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t leaf_size); - virtual CMulticlassLabels* classify_objects(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + virtual std::shared_ptr classify_objects(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; - virtual SGVector classify_objects_k(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + virtual SGVector classify_objects_k(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; /** @return object name */ const char* get_name() const { return "KDTREEKNNSolver"; } diff --git a/src/shogun/multiclass/KDTreeKNNsolver.cpp b/src/shogun/multiclass/KDTreeKNNsolver.cpp index 3af00d9045e..9094eeb6b1d 100644 --- a/src/shogun/multiclass/KDTreeKNNsolver.cpp +++ b/src/shogun/multiclass/KDTreeKNNsolver.cpp @@ -9,24 +9,23 @@ using namespace shogun; -CKDTREEKNNSolver::CKDTREEKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t leaf_size): -CKNNSolver(k, q, num_classes, min_label, train_labels) +KDTREEKNNSolver::KDTREEKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t leaf_size): +KNNSolver(k, q, num_classes, min_label, train_labels) { init(); m_leaf_size=leaf_size; } -CMulticlassLabels* CKDTREEKNNSolver::classify_objects(CDistance* knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +std::shared_ptr KDTREEKNNSolver::classify_objects(std::shared_ptr knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { - CMulticlassLabels* output=new CMulticlassLabels(num_lab); - CFeatures* lhs = knn_distance->get_lhs(); - CKDTree* kd_tree = new CKDTree(m_leaf_size); - kd_tree->build_tree(dynamic_cast*>(lhs)); - SG_UNREF(lhs); + auto output=std::make_shared(num_lab); + auto lhs = knn_distance->get_lhs(); + auto kd_tree = std::make_shared(m_leaf_size); + kd_tree->build_tree(lhs->as>()); - CFeatures* query = knn_distance->get_rhs(); - kd_tree->query_knn(dynamic_cast*>(query), m_k); + auto query = knn_distance->get_rhs(); + kd_tree->query_knn(query->as>(), m_k); SGMatrix NN = kd_tree->get_knn_indices(); for (int32_t i = 0; i < num_lab && (!cancel_computation()); i++) { @@ -39,25 +38,22 @@ CMulticlassLabels* CKDTREEKNNSolver::classify_objects(CDistance* knn_distance, c //write the label of 'nearest' in the output output->set_label(i, out_idx + m_min_label); } - SG_UNREF(query); - SG_UNREF(kd_tree); return output; } -SGVector CKDTREEKNNSolver::classify_objects_k(CDistance* knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +SGVector KDTREEKNNSolver::classify_objects_k(std::shared_ptr knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { SGVector output(m_k*num_lab); //allocation for distances to nearest neighbors SGVector dists(m_k); - CFeatures* lhs = knn_distance->get_lhs(); - CKDTree* kd_tree = new CKDTree(m_leaf_size); - kd_tree->build_tree(dynamic_cast*>(lhs)); - SG_UNREF(lhs); + auto lhs = knn_distance->get_lhs(); + auto kd_tree = std::make_shared(m_leaf_size); + kd_tree->build_tree(lhs->as>()); - CFeatures* data = knn_distance->get_rhs(); - kd_tree->query_knn(dynamic_cast*>(data), m_k); + auto data = knn_distance->get_rhs(); + kd_tree->query_knn(data->as>(), m_k); SGMatrix NN = kd_tree->get_knn_indices(); for (index_t i = 0; i < num_lab && (!cancel_computation()); i++) { @@ -67,13 +63,10 @@ SGVector CKDTREEKNNSolver::classify_objects_k(CDistance* knn_distance, train_lab[j] = m_train_labels[ NN(j,i) ]; dists[j] = knn_distance->distance(NN(j,i), i); } - CMath::qsort_index(dists.vector, train_lab.vector, m_k); + Math::qsort_index(dists.vector, train_lab.vector, m_k); choose_class_for_multiple_k(output.vector+i, classes.vector, train_lab.vector, num_lab); } - SG_UNREF(data); - SG_UNREF(kd_tree); - return output; } diff --git a/src/shogun/multiclass/KNN.cpp b/src/shogun/multiclass/KNN.cpp index 7baf8dce7e6..5cc8c09687e 100644 --- a/src/shogun/multiclass/KNN.cpp +++ b/src/shogun/multiclass/KNN.cpp @@ -20,14 +20,14 @@ using namespace shogun; -CKNN::CKNN() -: CDistanceMachine() +KNN::KNN() +: DistanceMachine() { init(); } -CKNN::CKNN(int32_t k, CDistance* d, CLabels* trainlab, KNN_SOLVER knn_solver) -: CDistanceMachine() +KNN::KNN(int32_t k, std::shared_ptr d, std::shared_ptr trainlab, KNN_SOLVER knn_solver) +: DistanceMachine() { init(); @@ -42,7 +42,7 @@ CKNN::CKNN(int32_t k, CDistance* d, CLabels* trainlab, KNN_SOLVER knn_solver) m_knn_solver=knn_solver; } -void CKNN::init() +void KNN::init() { m_k=3; m_q=1.0; @@ -65,11 +65,11 @@ void CKNN::init() SG_OPTIONS(KNN_BRUTE, KNN_KDTREE, KNN_COVER_TREE, KNN_LSH)); } -CKNN::~CKNN() +KNN::~KNN() { } -bool CKNN::train_machine(CFeatures* data) +bool KNN::train_machine(std::shared_ptr data) { require(m_labels, "No training labels provided."); require(distance, "No training distance provided."); @@ -84,13 +84,13 @@ bool CKNN::train_machine(CFeatures* data) distance->init(data, data); } - SGVector lab=((CMulticlassLabels*) m_labels)->get_int_labels(); + SGVector lab=multiclass_labels(m_labels)->get_int_labels(); m_train_labels=lab.clone(); require(m_train_labels.vlen > 0, "Provided training labels are empty"); // find minimal and maximal class - auto min_class = CMath::min(m_train_labels.vector, m_train_labels.vlen); - auto max_class = CMath::max(m_train_labels.vector, m_train_labels.vlen); + auto min_class = Math::min(m_train_labels.vector, m_train_labels.vlen); + auto max_class = Math::max(m_train_labels.vector, m_train_labels.vlen); linalg::add_scalar(m_train_labels, -min_class); @@ -103,7 +103,7 @@ bool CKNN::train_machine(CFeatures* data) return true; } -SGMatrix CKNN::nearest_neighbors() +SGMatrix KNN::nearest_neighbors() { //number of examples to which kNN is applied int32_t n=distance->get_num_vec_rhs(); @@ -134,7 +134,7 @@ SGMatrix CKNN::nearest_neighbors() train_idxs[j]=j; //sort the distance vector between test example i and all train examples - CMath::qsort_index(dists.vector, train_idxs.vector, m_train_labels.vlen); + Math::qsort_index(dists.vector, train_idxs.vector, m_train_labels.vlen); #ifdef DEBUG_KNN io::print("\nQuick sort query {}\n", i); @@ -153,7 +153,7 @@ SGMatrix CKNN::nearest_neighbors() return NN; } -CMulticlassLabels* CKNN::apply_multiclass(CFeatures* data) +std::shared_ptr KNN::apply_multiclass(std::shared_ptr data) { if (data) init_distance(data); @@ -179,14 +179,10 @@ CMulticlassLabels* CKNN::apply_multiclass(CFeatures* data) init_solver(m_knn_solver); - CMulticlassLabels* output = solver->classify_objects(distance, num_lab, train_lab, classes); - - SG_UNREF(solver); - - return output; + return solver->classify_objects(distance, num_lab, train_lab, classes); } -CMulticlassLabels* CKNN::classify_NN() +std::shared_ptr KNN::classify_NN() { require(distance, "Distance not set."); require(m_num_classes > 0, "Machine not trained."); @@ -194,7 +190,7 @@ CMulticlassLabels* CKNN::classify_NN() int32_t num_lab = distance->get_num_vec_rhs(); require(num_lab, "No vectors on right hand side"); - CMulticlassLabels* output = new CMulticlassLabels(num_lab); + auto output = std::make_shared(num_lab); SGVector distances(m_train_labels.vlen); io::info("{} test examples", num_lab); @@ -232,7 +228,7 @@ CMulticlassLabels* CKNN::classify_NN() return output; } -SGMatrix CKNN::classify_for_multiple_k() +SGMatrix KNN::classify_for_multiple_k() { require(distance, "Distance not set."); require(m_num_classes > 0, "Machine not trained."); @@ -256,60 +252,58 @@ SGMatrix CKNN::classify_for_multiple_k() SGVector output = solver->classify_objects_k(distance, num_lab, train_lab, classes); - SG_UNREF(solver); + return SGMatrix(output,num_lab,m_k); } -void CKNN::init_distance(CFeatures* data) +void KNN::init_distance(std::shared_ptr data) { require(distance, "Distance not set."); - CFeatures* lhs=distance->get_lhs(); + auto lhs=distance->get_lhs(); if (!lhs || !lhs->get_num_vectors()) { - SG_UNREF(lhs); error("No vectors on left hand side"); } distance->init(lhs, data); - SG_UNREF(lhs); } -bool CKNN::load(FILE* srcfile) +bool KNN::load(FILE* srcfile) { SG_SET_LOCALE_C; SG_RESET_LOCALE; return false; } -bool CKNN::save(FILE* dstfile) +bool KNN::save(FILE* dstfile) { SG_SET_LOCALE_C; SG_RESET_LOCALE; return false; } -void CKNN::init_solver(KNN_SOLVER knn_solver) +void KNN::init_solver(KNN_SOLVER knn_solver) { switch (knn_solver) { case KNN_BRUTE: { SGMatrix NN = nearest_neighbors(); - solver = new CBruteKNNSolver(m_k, m_q, m_num_classes, m_min_label, m_train_labels, NN); - SG_REF(solver); + solver = std::make_shared(m_k, m_q, m_num_classes, m_min_label, m_train_labels, NN); + break; } case KNN_KDTREE: { - solver = new CKDTREEKNNSolver(m_k, m_q, m_num_classes, m_min_label, m_train_labels, m_leaf_size); - SG_REF(solver); + solver = std::make_shared(m_k, m_q, m_num_classes, m_min_label, m_train_labels, m_leaf_size); + break; } case KNN_COVER_TREE: { #ifdef USE_GPL_SHOGUN - solver = new CCoverTreeKNNSolver(m_k, m_q, m_num_classes, m_min_label, m_train_labels); - SG_REF(solver); + solver = std::make_shared(m_k, m_q, m_num_classes, m_min_label, m_train_labels); + break; #else gpl_only(SOURCE_LOCATION); @@ -317,8 +311,8 @@ void CKNN::init_solver(KNN_SOLVER knn_solver) } case KNN_LSH: { - solver = new CLSHKNNSolver(m_k, m_q, m_num_classes, m_min_label, m_train_labels, m_lsh_l, m_lsh_t); - SG_REF(solver); + solver = std::make_shared(m_k, m_q, m_num_classes, m_min_label, m_train_labels, m_lsh_l, m_lsh_t); + break; } } diff --git a/src/shogun/multiclass/KNN.h b/src/shogun/multiclass/KNN.h index 6e80a9dce34..d87f1ce4c92 100644 --- a/src/shogun/multiclass/KNN.h +++ b/src/shogun/multiclass/KNN.h @@ -35,7 +35,7 @@ namespace shogun KNN_LSH }; -class CDistanceMachine; +class DistanceMachine; /** @brief Class KNN, an implementation of the standard k-nearest neigbor * classifier. @@ -59,20 +59,20 @@ class CDistanceMachine; * where \f$|q|<1\f$. * * To avoid ties, k should be an odd number. To define how close examples are - * k-NN requires a CDistance object to work with (e.g., CEuclideanDistance ). + * k-NN requires a Distance object to work with (e.g., EuclideanDistance ). * * Note that k-NN has zero training time but classification times increase * dramatically with the number of examples. Also note that k-NN is capable of * multi-class-classification. And finally, in case of k=1 classification will * take less time with an special optimization provided. */ -class CKNN : public CDistanceMachine +class KNN : public DistanceMachine { public: MACHINE_PROBLEM_TYPE(PT_MULTICLASS) /** default constructor */ - CKNN(); + KNN(); /** constructor * @@ -80,9 +80,9 @@ class CKNN : public CDistanceMachine * @param d distance * @param trainlab labels for training */ - CKNN(int32_t k, CDistance* d, CLabels* trainlab, KNN_SOLVER knn_solver=KNN_BRUTE); + KNN(int32_t k, std::shared_ptr d, std::shared_ptr trainlab, KNN_SOLVER knn_solver=KNN_BRUTE); - virtual ~CKNN(); + virtual ~KNN(); /** get classifier type * @@ -106,7 +106,7 @@ class CKNN : public CDistanceMachine * @param data (test)data to be classified * @return classified labels */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /// get output for example "vec_idx" virtual float64_t apply_one(int32_t vec_idx) @@ -214,12 +214,12 @@ class CKNN : public CDistanceMachine /** classify all examples with nearest neighbor (k=1) * @return classified labels */ - virtual CMulticlassLabels* classify_NN(); + virtual std::shared_ptr classify_NN(); /** init distances to test examples * @param data test examples */ - void init_distance(CFeatures* data); + void init_distance(std::shared_ptr data); /** train k-NN classifier * @@ -229,7 +229,7 @@ class CKNN : public CDistanceMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); private: void init(); @@ -284,7 +284,7 @@ class CKNN : public CDistanceMachine SGVector m_train_labels; /// Solver for KNN - CKNNSolver* solver; + std::shared_ptr solver; KNN_SOLVER m_knn_solver; diff --git a/src/shogun/multiclass/KNNSolver.cpp b/src/shogun/multiclass/KNNSolver.cpp index 1fcb1791097..97c58254723 100644 --- a/src/shogun/multiclass/KNNSolver.cpp +++ b/src/shogun/multiclass/KNNSolver.cpp @@ -11,8 +11,8 @@ using namespace shogun; -CKNNSolver::CKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels) -: CDistanceMachine() +KNNSolver::KNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels) +: DistanceMachine() { init(); @@ -23,7 +23,7 @@ CKNNSolver::CKNNSolver(const int32_t k, const float64_t q, const int32_t num_cla m_train_labels=train_labels; } -void CKNNSolver::init() +void KNNSolver::init() { m_k=3; m_q=1.0; @@ -32,7 +32,7 @@ void CKNNSolver::init() m_train_labels=0; } -int32_t CKNNSolver::choose_class(float64_t* classes, const int32_t* train_lab) const +int32_t KNNSolver::choose_class(float64_t* classes, const int32_t* train_lab) const { memset(classes, 0, sizeof(float64_t)*m_num_classes); @@ -59,7 +59,7 @@ int32_t CKNNSolver::choose_class(float64_t* classes, const int32_t* train_lab) c return out_idx; } -void CKNNSolver::choose_class_for_multiple_k(int32_t* output, int32_t* classes, const int32_t* train_lab, const int32_t step) const +void KNNSolver::choose_class_for_multiple_k(int32_t* output, int32_t* classes, const int32_t* train_lab, const int32_t step) const { //compute histogram of class outputs of the first k nearest neighbours memset(classes, 0, sizeof(int32_t)*m_num_classes); diff --git a/src/shogun/multiclass/KNNSolver.h b/src/shogun/multiclass/KNNSolver.h index 0cb3c61d844..f81a86cc367 100644 --- a/src/shogun/multiclass/KNNSolver.h +++ b/src/shogun/multiclass/KNNSolver.h @@ -17,15 +17,15 @@ namespace shogun { /* Virtual base class for all KNN solvers */ -class CDistanceMachine; -class CKNNSolver : public CDistanceMachine +class DistanceMachine; +class KNNSolver : public DistanceMachine { public: /** default constructor */ - CKNNSolver(): CDistanceMachine() { init(); } + KNNSolver(): DistanceMachine() { init(); } /** deconstructor */ - virtual ~CKNNSolver() { /* nothing to do */ } + virtual ~KNNSolver() { /* nothing to do */ } /** constructor * @@ -35,7 +35,7 @@ class CKNNSolver : public CDistanceMachine * @param min_label m_min_label * @param train_labels m_train_labels */ - CKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels); + KNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels); /** compute the histogram of class outputs of the k nearest * neighbors to a test vector and return the index of the most frequent class @@ -71,7 +71,7 @@ class CKNNSolver : public CDistanceMachine * @param classes vector used to store the histogram * @return the classified labels */ - virtual CMulticlassLabels* classify_objects(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const = 0; + virtual std::shared_ptr classify_objects(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const = 0; /** * classify all objects, and the implementation will depended on which knn solver been choosen. @@ -81,7 +81,7 @@ class CKNNSolver : public CDistanceMachine * @param classes vector used to store the histogram * @return the classified labels */ - virtual SGVector classify_objects_k(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const = 0; + virtual SGVector classify_objects_k(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const = 0; /** @return object name */ virtual const char* get_name() const { return "KNNSolver"; } diff --git a/src/shogun/multiclass/LSHKNNSolver.cpp b/src/shogun/multiclass/LSHKNNSolver.cpp index 8797ed75994..35182a9b552 100644 --- a/src/shogun/multiclass/LSHKNNSolver.cpp +++ b/src/shogun/multiclass/LSHKNNSolver.cpp @@ -15,8 +15,8 @@ using namespace Eigen; #include -CLSHKNNSolver::CLSHKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t lsh_l, const int32_t lsh_t): -CKNNSolver(k, q, num_classes, min_label, train_labels) +LSHKNNSolver::LSHKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t lsh_l, const int32_t lsh_t): +KNNSolver(k, q, num_classes, min_label, train_labels) { init(); @@ -28,7 +28,7 @@ template PointType get_falconn_point(FeatureType* f, index_t i); template<> -falconn::DenseVector get_falconn_point(CDenseFeatures* f, index_t i) +falconn::DenseVector get_falconn_point(DenseFeatures* f, index_t i) { index_t len; bool free; @@ -37,7 +37,7 @@ falconn::DenseVector get_falconn_point(CDenseFeatures* f, ind } template<> -falconn::SparseVector get_falconn_point(CSparseFeatures* f, index_t i) +falconn::SparseVector get_falconn_point(SparseFeatures* f, index_t i) { // FIXME: this basically copies the data :( auto fv = f->get_sparse_feature_vector(i); @@ -48,9 +48,9 @@ falconn::SparseVector get_falconn_point(CSparseFeatures* f, i } template -CMulticlassLabels* CLSHKNNSolver::classify_objects(FeatureType* lhs, FeatureType* query_features, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +std::shared_ptr LSHKNNSolver::classify_objects(FeatureType* lhs, FeatureType* query_features, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { - auto output = new CMulticlassLabels(num_lab); + auto output = std::make_shared(num_lab); std::vector feats(lhs->get_num_vectors()); for(index_t i = 0; i < lhs->get_num_vectors(); ++i) feats[i] = get_falconn_point(lhs, i); @@ -60,7 +60,7 @@ CMulticlassLabels* CLSHKNNSolver::classify_objects(FeatureType* lhs, FeatureType lhs->get_num_features(), falconn::DistanceFunction::EuclideanSquared, true); - SG_UNREF(lhs); + if (m_lsh_l && m_lsh_t) params.l = m_lsh_l; @@ -89,26 +89,26 @@ CMulticlassLabels* CLSHKNNSolver::classify_objects(FeatureType* lhs, FeatureType //write the label of 'nearest' in the output output->set_label(i, out_idx + m_min_label); } - SG_UNREF(query_features); + return output; } -CMulticlassLabels* CLSHKNNSolver::classify_objects(CDistance* knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +std::shared_ptr LSHKNNSolver::classify_objects(std::shared_ptr knn_distance, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { auto lhs = knn_distance->get_lhs(); auto rhs = knn_distance->get_rhs(); if ((lhs->get_feature_class() == C_DENSE) && (rhs->get_feature_class() == C_DENSE)) { - auto features = lhs->as>(); - auto query_features = rhs->as>(); - return classify_objects>(features, query_features, num_lab, train_lab, classes); + auto features = lhs->as>(); + auto query_features = rhs->as>(); + return classify_objects>(features.get(), query_features.get(), num_lab, train_lab, classes); } else if ((lhs->get_feature_class() == C_SPARSE) && (rhs->get_feature_class() == C_SPARSE)) { - auto features = lhs->as>(); - auto query_features = rhs->as>(); - return classify_objects>(features, query_features, num_lab, train_lab, classes); + auto features = lhs->as>(); + auto query_features = rhs->as>(); + return classify_objects>(features.get(), query_features.get(), num_lab, train_lab, classes); } else { @@ -116,7 +116,7 @@ CMulticlassLabels* CLSHKNNSolver::classify_objects(CDistance* knn_distance, cons } } -SGVector CLSHKNNSolver::classify_objects_k(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const +SGVector LSHKNNSolver::classify_objects_k(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const { not_implemented(SOURCE_LOCATION); return 0; diff --git a/src/shogun/multiclass/LSHKNNSolver.h b/src/shogun/multiclass/LSHKNNSolver.h index 9f0c443a9e9..1ec2fbef503 100644 --- a/src/shogun/multiclass/LSHKNNSolver.h +++ b/src/shogun/multiclass/LSHKNNSolver.h @@ -20,17 +20,17 @@ namespace shogun * For more information, see https://en.wikipedia.org/wiki/Locality-sensitive_hashing. * */ -class CLSHKNNSolver : public CKNNSolver +class LSHKNNSolver : public KNNSolver { public: /** default constructor */ - CLSHKNNSolver() : CKNNSolver() + LSHKNNSolver() : KNNSolver() { init(); } /** deconstructor */ - virtual ~CLSHKNNSolver() { /* nothing to do */ } + virtual ~LSHKNNSolver() { /* nothing to do */ } /** constructor * @@ -42,11 +42,11 @@ class CLSHKNNSolver : public CKNNSolver * @param lsh_l m_lsh_l * @param lsh_t m_lsh_t */ - CLSHKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t lsh_l, const int32_t lsh_t); + LSHKNNSolver(const int32_t k, const float64_t q, const int32_t num_classes, const int32_t min_label, const SGVector train_labels, const int32_t lsh_l, const int32_t lsh_t); - virtual CMulticlassLabels* classify_objects(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + virtual std::shared_ptr classify_objects(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; - virtual SGVector classify_objects_k(CDistance* d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + virtual SGVector classify_objects_k(std::shared_ptr d, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; /** @return object name */ const char* get_name() const { return "LSHKNNSolver"; } @@ -59,7 +59,7 @@ class CLSHKNNSolver : public CKNNSolver } template - CMulticlassLabels* classify_objects(FeatureType* lhs, FeatureType* query_features, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; + std::shared_ptr classify_objects(FeatureType* lhs, FeatureType* query_features, const int32_t num_lab, SGVector& train_lab, SGVector& classes) const; protected: /* Number of hash tables for LSH */ diff --git a/src/shogun/multiclass/MCLDA.cpp b/src/shogun/multiclass/MCLDA.cpp index 2c7bd01a298..1f88770253c 100644 --- a/src/shogun/multiclass/MCLDA.cpp +++ b/src/shogun/multiclass/MCLDA.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Kevin Hughes, Heiko Strathmann, Michele Mazzoni, Soumyajit De, + * Authors: Kevin Hughes, Heiko Strathmann, Michele Mazzoni, Soumyajit De, * Weijie Lin, Bjoern Esser, Soeren Sonnenburg, Sanuj Sharma */ @@ -20,8 +20,8 @@ using namespace shogun; using namespace Eigen; -CMCLDA::CMCLDA(float64_t tolerance, bool store_cov) -: CNativeMulticlassMachine() +MCLDA::MCLDA(float64_t tolerance, bool store_cov) +: NativeMulticlassMachine() { init(); m_tolerance=tolerance; @@ -29,8 +29,8 @@ CMCLDA::CMCLDA(float64_t tolerance, bool store_cov) } -CMCLDA::CMCLDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tolerance, bool store_cov) -: CNativeMulticlassMachine() +MCLDA::MCLDA(std::shared_ptr> traindat, std::shared_ptr trainlab, float64_t tolerance, bool store_cov) +: NativeMulticlassMachine() { init(); @@ -41,18 +41,18 @@ CMCLDA::CMCLDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t set_labels(trainlab); } -CMCLDA::~CMCLDA() +MCLDA::~MCLDA() { - SG_UNREF(m_features); + cleanup(); } -void CMCLDA::init() +void MCLDA::init() { SG_ADD(&m_tolerance, "m_tolerance", "Tolerance member.", ParameterProperties::HYPER); SG_ADD(&m_store_cov, "m_store_cov", "Store covariance member"); - SG_ADD((CSGObject**) &m_features, "m_features", "Feature object."); + SG_ADD((std::shared_ptr*) &m_features, "m_features", "Feature object."); SG_ADD(&m_means, "m_means", "Mean vectors list"); SG_ADD(&m_cov, "m_cov", "covariance matrix"); SG_ADD(&m_xbar, "m_xbar", "total mean"); @@ -70,19 +70,19 @@ void CMCLDA::init() m_rank=0; } -void CMCLDA::cleanup() +void MCLDA::cleanup() { m_num_classes = 0; } -CMulticlassLabels* CMCLDA::apply_multiclass(CFeatures* data) +std::shared_ptr MCLDA::apply_multiclass(std::shared_ptr data) { if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(data->as()); } if (!m_features) @@ -93,7 +93,7 @@ CMulticlassLabels* CMCLDA::apply_multiclass(CFeatures* data) ASSERT( m_dim == m_features->get_dim_feature_space() ); // collect features into a matrix - CDenseFeatures< float64_t >* rf = (CDenseFeatures< float64_t >*) m_features; + auto rf = m_features->as>(); MatrixXd X(num_vecs, m_dim); @@ -140,14 +140,14 @@ CMulticlassLabels* CMCLDA::apply_multiclass(CFeatures* data) #endif // argmax to apply labels - CMulticlassLabels* out = new CMulticlassLabels(num_vecs); + auto out = std::make_shared(num_vecs); for (int i = 0; i < num_vecs; i++) - out->set_label(i, CMath::arg_max(d.data()+i, num_vecs, m_num_classes)); + out->set_label(i, Math::arg_max(d.data()+i, num_vecs, m_num_classes)); return out; } -bool CMCLDA::train_machine(CFeatures* data) +bool MCLDA::train_machine(std::shared_ptr data) { if (!m_labels) error("No labels allocated in MCLDA training"); @@ -157,20 +157,20 @@ bool CMCLDA::train_machine(CFeatures* data) if (!data->has_property(FP_DOT)) error("Speficied features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(data->as()); } if (!m_features) error("No features allocated in MCLDA training"); - SGVector< int32_t > train_labels = ((CMulticlassLabels*) m_labels)->get_int_labels(); + SGVector< int32_t > train_labels = multiclass_labels(m_labels)->get_int_labels(); if (!train_labels.vector) error("No train_labels allocated in MCLDA training"); cleanup(); - m_num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes(); + m_num_classes = multiclass_labels(m_labels)->get_num_classes(); m_dim = m_features->get_dim_feature_space(); int32_t num_vec = m_features->get_num_vectors(); @@ -208,7 +208,7 @@ bool CMCLDA::train_machine(CFeatures* data) } } - CDenseFeatures< float64_t >* rf = (CDenseFeatures< float64_t >*) m_features; + auto rf = m_features->as>(); // if ( m_store_cov ) index_t * cov_dims = SG_MALLOC(index_t, 3); diff --git a/src/shogun/multiclass/MCLDA.h b/src/shogun/multiclass/MCLDA.h index 08f620d4fd3..f00cb1cbb4d 100644 --- a/src/shogun/multiclass/MCLDA.h +++ b/src/shogun/multiclass/MCLDA.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Kevin Hughes, Heiko Strathmann, Thoralf Klein, Soeren Sonnenburg, + * Authors: Kevin Hughes, Heiko Strathmann, Thoralf Klein, Soeren Sonnenburg, * Bjoern Esser */ @@ -23,13 +23,13 @@ namespace shogun /** @brief Class MCLDA implements multiclass Linear Discriminant Analysis. * - * MCLDA learns a linear classifier and requires examples to be CDenseFeatures. + * MCLDA learns a linear classifier and requires examples to be DenseFeatures. * The learned linear classification rule is optimal under the assumption that * the classes are gaussian distributed with equal co-variance * */ -class CMCLDA : public CNativeMulticlassMachine +class MCLDA : public NativeMulticlassMachine { public: MACHINE_PROBLEM_TYPE(PT_MULTICLASS) @@ -39,7 +39,7 @@ class CMCLDA : public CNativeMulticlassMachine * @param tolerance tolerance used in training * @param store_cov whether to store the within class covariances */ - CMCLDA(float64_t tolerance = 1e-4, bool store_cov = false); + MCLDA(float64_t tolerance = 1e-4, bool store_cov = false); /** constructor * @@ -48,16 +48,16 @@ class CMCLDA : public CNativeMulticlassMachine * @param tolerance tolerance used in training * @param store_cov whether to store the within class covariances */ - CMCLDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tolerance = 1e-4, bool store_cov = false); + MCLDA(std::shared_ptr> traindat, std::shared_ptr trainlab, float64_t tolerance = 1e-4, bool store_cov = false); - virtual ~CMCLDA(); + virtual ~MCLDA(); - /** apply CMCLDA to data + /** apply MCLDA to data * * @param data (test) data to be classified * @return labels result of classification */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /** set tolerance * @@ -81,14 +81,14 @@ class CMCLDA : public CNativeMulticlassMachine * * @param feat features to set */ - virtual void set_features(CDotFeatures* feat) + virtual void set_features(std::shared_ptr feat) { if (feat->get_feature_class() != C_DENSE || feat->get_feature_type() != F_DREAL) error("MCLDA requires SIMPLE REAL valued features"); - SG_REF(feat); - SG_UNREF(m_features); + + m_features = feat; } @@ -96,7 +96,7 @@ class CMCLDA : public CNativeMulticlassMachine * * @return features */ - virtual CDotFeatures* get_features() { SG_REF(m_features); return m_features; } + virtual std::shared_ptr get_features() { return m_features; } /** get object name * @@ -131,7 +131,7 @@ class CMCLDA : public CNativeMulticlassMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data = NULL); private: void init(); @@ -140,7 +140,7 @@ class CMCLDA : public CNativeMulticlassMachine private: /** feature vectors */ - CDotFeatures* m_features; + std::shared_ptr m_features; /** tolerance used during training */ float64_t m_tolerance; diff --git a/src/shogun/multiclass/MulticlassLibLinear.cpp b/src/shogun/multiclass/MulticlassLibLinear.cpp index 30d400c260d..f024b9b2356 100644 --- a/src/shogun/multiclass/MulticlassLibLinear.cpp +++ b/src/shogun/multiclass/MulticlassLibLinear.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Chiyuan Zhang, Giovanni De Toni, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Chiyuan Zhang, Giovanni De Toni, * Evan Shelhamer */ @@ -15,22 +15,22 @@ using namespace shogun; -CMulticlassLibLinear::CMulticlassLibLinear() : - RandomMixin() +MulticlassLibLinear::MulticlassLibLinear() : + RandomMixin() { register_parameters(); init_defaults(); } -CMulticlassLibLinear::CMulticlassLibLinear(float64_t C, CDotFeatures* features, CLabels* labs) : - RandomMixin(new CMulticlassOneVsRestStrategy(),features,(CMachine*)NULL,labs) +MulticlassLibLinear::MulticlassLibLinear(float64_t C, std::shared_ptr features, std::shared_ptr labs) : + RandomMixin(std::make_shared(),features,nullptr,labs) { register_parameters(); init_defaults(); set_C(C); } -void CMulticlassLibLinear::init_defaults() +void MulticlassLibLinear::init_defaults() { set_C(1.0); set_epsilon(1e-2); @@ -40,7 +40,7 @@ void CMulticlassLibLinear::init_defaults() m_train_state = NULL; } -void CMulticlassLibLinear::register_parameters() +void MulticlassLibLinear::register_parameters() { SG_ADD(&m_C, "C", "regularization constant",ParameterProperties::HYPER); SG_ADD(&m_epsilon, "epsilon", "tolerance epsilon"); @@ -48,12 +48,12 @@ void CMulticlassLibLinear::register_parameters() SG_ADD(&m_use_bias, "use_bias", "indicates whether bias should be used"); } -CMulticlassLibLinear::~CMulticlassLibLinear() +MulticlassLibLinear::~MulticlassLibLinear() { reset_train_state(); } -SGVector CMulticlassLibLinear::get_support_vectors() const +SGVector MulticlassLibLinear::get_support_vectors() const { if (!m_train_state) error("Please enable save_train_state option and train machine."); @@ -61,7 +61,7 @@ SGVector CMulticlassLibLinear::get_support_vectors() const ASSERT(m_labels && m_labels->get_label_type() == LT_MULTICLASS) int32_t num_vectors = m_features->get_num_vectors(); - int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes(); + int32_t num_classes = multiclass_labels(m_labels)->get_num_classes(); v_array nz_idxs; nz_idxs.reserve(num_vectors); @@ -70,7 +70,7 @@ SGVector CMulticlassLibLinear::get_support_vectors() const { for (int32_t y=0; yalpha[i*num_classes+y])>1e-6) + if (Math::abs(m_train_state->alpha[i*num_classes+y])>1e-6) { nz_idxs.push(i); break; @@ -82,15 +82,15 @@ SGVector CMulticlassLibLinear::get_support_vectors() const return SGVector(nz_idxs.begin,num_nz); } -SGMatrix CMulticlassLibLinear::obtain_regularizer_matrix() const +SGMatrix MulticlassLibLinear::obtain_regularizer_matrix() const { return SGMatrix(); } -bool CMulticlassLibLinear::train_machine(CFeatures* data) +bool MulticlassLibLinear::train_machine(std::shared_ptr data) { if (data) - set_features((CDotFeatures*)data); + set_features(data->as()); ASSERT(m_features) ASSERT(m_labels && m_labels->get_label_type()==LT_MULTICLASS) @@ -98,7 +98,7 @@ bool CMulticlassLibLinear::train_machine(CFeatures* data) init_strategy(); int32_t num_vectors = m_features->get_num_vectors(); - int32_t num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes(); + int32_t num_classes = multiclass_labels(m_labels)->get_num_classes(); int32_t bias_n = m_use_bias ? 1 : 0; liblinear_problem mc_problem; @@ -106,7 +106,7 @@ bool CMulticlassLibLinear::train_machine(CFeatures* data) mc_problem.n = m_features->get_dim_feature_space() + bias_n; mc_problem.y = SG_MALLOC(float64_t, mc_problem.l); for (int32_t i=0; iget_int_label(i); + mc_problem.y[i] = multiclass_labels(m_labels)->get_int_label(i); mc_problem.x = m_features; mc_problem.use_bias = m_use_bias; @@ -124,10 +124,10 @@ bool CMulticlassLibLinear::train_machine(CFeatures* data) m_max_iter,m_max_train_time,m_train_state); solver.solve(m_prng); - m_machines->reset_array(); + m_machines.clear(); for (int32_t i=0; i(); SGVector cw(mc_problem.n-bias_n); for (int32_t j=0; jset_bias(m_train_state->w[(mc_problem.n-bias_n)*num_classes+i]); - m_machines->push_back(machine); + m_machines.push_back(machine); } if (!m_save_train_state) diff --git a/src/shogun/multiclass/MulticlassLibLinear.h b/src/shogun/multiclass/MulticlassLibLinear.h index 4915c49364b..3393bdefe64 100644 --- a/src/shogun/multiclass/MulticlassLibLinear.h +++ b/src/shogun/multiclass/MulticlassLibLinear.h @@ -32,23 +32,23 @@ namespace shogun state can be forced to clear using reset_train_state() method. */ -class CMulticlassLibLinear : public RandomMixin +class MulticlassLibLinear : public RandomMixin { public: MACHINE_PROBLEM_TYPE(PT_MULTICLASS) /** default constructor */ - CMulticlassLibLinear(); + MulticlassLibLinear(); /** standard constructor * @param C C regularization constant value * @param features features * @param labs labels */ - CMulticlassLibLinear(float64_t C, CDotFeatures* features, CLabels* labs); + MulticlassLibLinear(float64_t C, std::shared_ptr features, std::shared_ptr labs); /** destructor */ - virtual ~CMulticlassLibLinear(); + virtual ~MulticlassLibLinear(); /** get name */ virtual const char* get_name() const @@ -143,7 +143,7 @@ class CMulticlassLibLinear : public RandomMixin protected: /** train machine */ - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data = NULL); /** obtain regularizer (w0) matrix */ virtual SGMatrix obtain_regularizer_matrix() const; diff --git a/src/shogun/multiclass/MulticlassLibSVM.cpp b/src/shogun/multiclass/MulticlassLibSVM.cpp index f9f76c79420..5057dcafddd 100644 --- a/src/shogun/multiclass/MulticlassLibSVM.cpp +++ b/src/shogun/multiclass/MulticlassLibSVM.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Chiyuan Zhang, Heiko Strathmann, + * Authors: Sergey Lisitsyn, Soeren Sonnenburg, Chiyuan Zhang, Heiko Strathmann, * Bjoern Esser, Leon Kuchenbecker */ @@ -12,21 +12,21 @@ using namespace shogun; -CMulticlassLibSVM::CMulticlassLibSVM(LIBSVM_SOLVER_TYPE st) -: CMulticlassSVM(new CMulticlassOneVsOneStrategy()), solver_type(st) +MulticlassLibSVM::MulticlassLibSVM(LIBSVM_SOLVER_TYPE st) +: MulticlassSVM(std::make_shared()), solver_type(st) { } -CMulticlassLibSVM::CMulticlassLibSVM(float64_t C, CKernel* k, CLabels* lab) -: CMulticlassSVM(new CMulticlassOneVsOneStrategy(), C, k, lab), solver_type(LIBSVM_C_SVC) +MulticlassLibSVM::MulticlassLibSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: MulticlassSVM(std::make_shared(), C, k, lab), solver_type(LIBSVM_C_SVC) { } -CMulticlassLibSVM::~CMulticlassLibSVM() +MulticlassLibSVM::~MulticlassLibSVM() { } -void CMulticlassLibSVM::register_params() +void MulticlassLibSVM::register_params() { SG_ADD_OPTIONS( (machine_int_t*)&solver_type, "libsvm_solver_type", @@ -34,7 +34,7 @@ void CMulticlassLibSVM::register_params() SG_OPTIONS(LIBSVM_C_SVC, LIBSVM_NU_SVC)); } -bool CMulticlassLibSVM::train_machine(CFeatures* data) +bool MulticlassLibSVM::train_machine(std::shared_ptr data) { svm_problem problem; svm_parameter param; @@ -72,7 +72,7 @@ bool CMulticlassLibSVM::train_machine(CFeatures* data) for (int32_t i=0; iget_label(i); + problem.y[i]=multiclass_labels(m_labels)->get_label(i); problem.x[i]=&x_space[2*i]; x_space[2*i].index=i; x_space[2*i+1].index=-1; @@ -86,7 +86,7 @@ bool CMulticlassLibSVM::train_machine(CFeatures* data) param.gamma = 0; // 1/k param.coef0 = 0; param.nu = get_nu(); // Nu - param.kernel=m_kernel; + param.kernel=m_kernel.get(); param.cache_size = m_kernel->get_cache_size(); param.max_train_time = m_max_train_time; param.C = get_C(); @@ -138,7 +138,7 @@ bool CMulticlassLibSVM::train_machine(CFeatures* data) ASSERT(num_sv>0) ASSERT(model->sv_coef[i] && model->sv_coef[j-1]) - CSVM* svm=new CSVM(num_sv); + auto svm=std::make_shared(num_sv); svm->set_bias(sgn*bias); diff --git a/src/shogun/multiclass/MulticlassLibSVM.h b/src/shogun/multiclass/MulticlassLibSVM.h index aef01bb3990..b886b4dd72f 100644 --- a/src/shogun/multiclass/MulticlassLibSVM.h +++ b/src/shogun/multiclass/MulticlassLibSVM.h @@ -18,11 +18,11 @@ namespace shogun { /** @brief class LibSVMMultiClass. Does one vs one * classification. */ -class CMulticlassLibSVM : public CMulticlassSVM +class MulticlassLibSVM : public MulticlassSVM { public: /** default constructor */ - CMulticlassLibSVM(LIBSVM_SOLVER_TYPE st=LIBSVM_C_SVC); + MulticlassLibSVM(LIBSVM_SOLVER_TYPE st=LIBSVM_C_SVC); /** constructor * @@ -30,10 +30,10 @@ class CMulticlassLibSVM : public CMulticlassSVM * @param k kernel * @param lab labels */ - CMulticlassLibSVM(float64_t C, CKernel* k, CLabels* lab); + MulticlassLibSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab); /** destructor */ - virtual ~CMulticlassLibSVM(); + virtual ~MulticlassLibSVM(); /** get classifier type * @@ -53,7 +53,7 @@ class CMulticlassLibSVM : public CMulticlassSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); private: void register_params(); diff --git a/src/shogun/multiclass/MulticlassOCAS.cpp b/src/shogun/multiclass/MulticlassOCAS.cpp index 0c0ba5fa303..5e6b656f300 100644 --- a/src/shogun/multiclass/MulticlassOCAS.cpp +++ b/src/shogun/multiclass/MulticlassOCAS.cpp @@ -15,7 +15,7 @@ using namespace shogun; struct mocas_data { - CDotFeatures* features; + std::shared_ptr features; float64_t* W; float64_t* oldW; float64_t* full_A; @@ -27,8 +27,8 @@ struct mocas_data float64_t* new_a; }; -CMulticlassOCAS::CMulticlassOCAS() : - CLinearMulticlassMachine() +MulticlassOCAS::MulticlassOCAS() : + LinearMulticlassMachine() { register_parameters(); set_C(1.0); @@ -38,8 +38,8 @@ CMulticlassOCAS::CMulticlassOCAS() : set_buf_size(5000); } -CMulticlassOCAS::CMulticlassOCAS(float64_t C, CFeatures* train_features, CLabels* train_labels) : - CLinearMulticlassMachine(new CMulticlassOneVsRestStrategy(), train_features->as(), NULL, train_labels), m_C(C) +MulticlassOCAS::MulticlassOCAS(float64_t C, std::shared_ptr train_features, std::shared_ptr train_labels) : + LinearMulticlassMachine(std::make_shared(), train_features->as(), NULL, train_labels), m_C(C) { register_parameters(); set_epsilon(1e-2); @@ -48,7 +48,7 @@ CMulticlassOCAS::CMulticlassOCAS(float64_t C, CFeatures* train_features, CLabels set_buf_size(5000); } -void CMulticlassOCAS::register_parameters() +void MulticlassOCAS::register_parameters() { SG_ADD(&m_C, "m_C", "regularization constant", ParameterProperties::HYPER); SG_ADD(&m_epsilon, "m_epsilon", "solver relative tolerance"); @@ -57,14 +57,14 @@ void CMulticlassOCAS::register_parameters() SG_ADD(&m_buf_size, "m_buf_size", "buffer size"); } -CMulticlassOCAS::~CMulticlassOCAS() +MulticlassOCAS::~MulticlassOCAS() { } -bool CMulticlassOCAS::train_machine(CFeatures* data) +bool MulticlassOCAS::train_machine(std::shared_ptr data) { if (data) - set_features((CDotFeatures*)data); + set_features(data->as()); ASSERT(m_features) ASSERT(m_labels) @@ -76,7 +76,7 @@ bool CMulticlassOCAS::train_machine(CFeatures* data) int32_t num_features = m_features->get_dim_feature_space(); float64_t C = m_C; - SGVector labels = ((CMulticlassLabels*) m_labels)->get_labels(); + SGVector labels = multiclass_labels(m_labels)->get_labels(); uint32_t nY = num_classes; uint32_t nData = num_vectors; float64_t TolRel = m_epsilon; @@ -101,12 +101,12 @@ bool CMulticlassOCAS::train_machine(CFeatures* data) ocas_return_value_T value = msvm_ocas_solver(C, labels.vector, nY, nData, TolRel, TolAbs, QPBound, MaxTime, BufSize, Method, - &CMulticlassOCAS::msvm_full_compute_W, - &CMulticlassOCAS::msvm_update_W, - &CMulticlassOCAS::msvm_full_add_new_cut, - &CMulticlassOCAS::msvm_full_compute_output, - &CMulticlassOCAS::msvm_sort_data, - &CMulticlassOCAS::msvm_print, + &MulticlassOCAS::msvm_full_compute_W, + &MulticlassOCAS::msvm_update_W, + &MulticlassOCAS::msvm_full_add_new_cut, + &MulticlassOCAS::msvm_full_compute_output, + &MulticlassOCAS::msvm_sort_data, + &MulticlassOCAS::msvm_print, &user_data); SG_DEBUG("Number of iterations [nIter] = {} ",value.nIter) @@ -125,13 +125,13 @@ bool CMulticlassOCAS::train_machine(CFeatures* data) SG_DEBUG("QP exit flag [qp_exitflag] = {} ",value.qp_exitflag) SG_DEBUG("Exit flag [exitflag] = {} ",value.exitflag) - m_machines->reset_array(); + m_machines.clear(); for (int32_t i=0; i(); machine->set_w(SGVector(&user_data.W[i*num_features],num_features,false).clone()); - m_machines->push_back(machine); + m_machines.push_back(machine); } SG_FREE(user_data.W); @@ -143,7 +143,7 @@ bool CMulticlassOCAS::train_machine(CFeatures* data) return true; } -float64_t CMulticlassOCAS::msvm_update_W(float64_t t, void* user_data) +float64_t MulticlassOCAS::msvm_update_W(float64_t t, void* user_data) { float64_t* oldW = ((mocas_data*)user_data)->oldW; uint32_t nY = ((mocas_data*)user_data)->nY; @@ -158,7 +158,7 @@ float64_t CMulticlassOCAS::msvm_update_W(float64_t t, void* user_data) return sq_norm_W; } -void CMulticlassOCAS::msvm_full_compute_W(float64_t *sq_norm_W, float64_t *dp_WoldW, +void MulticlassOCAS::msvm_full_compute_W(float64_t *sq_norm_W, float64_t *dp_WoldW, float64_t *alpha, uint32_t nSel, void* user_data) { float64_t* full_A = ((mocas_data*)user_data)->full_A; @@ -187,7 +187,7 @@ void CMulticlassOCAS::msvm_full_compute_W(float64_t *sq_norm_W, float64_t *dp_Wo return; } -int CMulticlassOCAS::msvm_full_add_new_cut(float64_t *new_col_H, uint32_t *new_cut, +int MulticlassOCAS::msvm_full_add_new_cut(float64_t *new_col_H, uint32_t *new_cut, uint32_t nSel, void* user_data) { float64_t* full_A = ((mocas_data*)user_data)->full_A; @@ -196,7 +196,7 @@ int CMulticlassOCAS::msvm_full_add_new_cut(float64_t *new_col_H, uint32_t *new_c uint32_t nDim = ((mocas_data*)user_data)->nDim; uint32_t nData = ((mocas_data*)user_data)->nData; SGVector new_a(((mocas_data*)user_data)->new_a, nDim*nY, false); - CDotFeatures* features = ((mocas_data*)user_data)->features; + auto features = ((mocas_data*)user_data)->features; float64_t sq_norm_a; uint32_t i, j, y, y2; @@ -233,14 +233,14 @@ int CMulticlassOCAS::msvm_full_add_new_cut(float64_t *new_col_H, uint32_t *new_c return 0; } -int CMulticlassOCAS::msvm_full_compute_output(float64_t *output, void* user_data) +int MulticlassOCAS::msvm_full_compute_output(float64_t *output, void* user_data) { float64_t* W = ((mocas_data*)user_data)->W; uint32_t nY = ((mocas_data*)user_data)->nY; uint32_t nDim = ((mocas_data*)user_data)->nDim; uint32_t nData = ((mocas_data*)user_data)->nData; float64_t* output_values = ((mocas_data*)user_data)->output_values; - CDotFeatures* features = ((mocas_data*)user_data)->features; + auto features = ((mocas_data*)user_data)->features; uint32_t i, y; @@ -254,13 +254,13 @@ int CMulticlassOCAS::msvm_full_compute_output(float64_t *output, void* user_data return 0; } -int CMulticlassOCAS::msvm_sort_data(float64_t* vals, float64_t* data, uint32_t size) +int MulticlassOCAS::msvm_sort_data(float64_t* vals, float64_t* data, uint32_t size) { - CMath::qsort_index(vals, data, size); + Math::qsort_index(vals, data, size); return 0; } -void CMulticlassOCAS::msvm_print(ocas_return_value_T value) +void MulticlassOCAS::msvm_print(ocas_return_value_T value) { } diff --git a/src/shogun/multiclass/MulticlassOCAS.h b/src/shogun/multiclass/MulticlassOCAS.h index 245f66f45b6..206492713de 100644 --- a/src/shogun/multiclass/MulticlassOCAS.h +++ b/src/shogun/multiclass/MulticlassOCAS.h @@ -17,23 +17,23 @@ namespace shogun { /** @brief multiclass OCAS wrapper */ -class CMulticlassOCAS : public CLinearMulticlassMachine +class MulticlassOCAS : public LinearMulticlassMachine { public: MACHINE_PROBLEM_TYPE(PT_MULTICLASS) /** default constructor */ - CMulticlassOCAS(); + MulticlassOCAS(); /** standard constructor * @param C C regularication constant value * @param features features * @param labs labels */ - CMulticlassOCAS(float64_t C, CFeatures* features, CLabels* labs); + MulticlassOCAS(float64_t C, std::shared_ptr features, std::shared_ptr labs); /** destructor */ - virtual ~CMulticlassOCAS(); + virtual ~MulticlassOCAS(); /** get name */ virtual const char* get_name() const @@ -109,7 +109,7 @@ class CMulticlassOCAS : public CLinearMulticlassMachine protected: /** train machine */ - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data = NULL); /** update W */ static float64_t msvm_update_W(float64_t t, void* user_data); diff --git a/src/shogun/multiclass/MulticlassOneVsOneStrategy.cpp b/src/shogun/multiclass/MulticlassOneVsOneStrategy.cpp index 18e961eb244..22133825713 100644 --- a/src/shogun/multiclass/MulticlassOneVsOneStrategy.cpp +++ b/src/shogun/multiclass/MulticlassOneVsOneStrategy.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Chiyuan Zhang, Shell Hu, Sergey Lisitsyn, + * Authors: Soeren Sonnenburg, Chiyuan Zhang, Shell Hu, Sergey Lisitsyn, * Bjoern Esser, Sanuj Sharma */ @@ -12,27 +12,27 @@ using namespace shogun; -CMulticlassOneVsOneStrategy::CMulticlassOneVsOneStrategy() - :CMulticlassStrategy(), m_num_machines(0), m_num_samples(SGVector()) +MulticlassOneVsOneStrategy::MulticlassOneVsOneStrategy() + :MulticlassStrategy(), m_num_machines(0), m_num_samples(SGVector()) { register_parameters(); } -CMulticlassOneVsOneStrategy::CMulticlassOneVsOneStrategy(EProbHeuristicType prob_heuris) - :CMulticlassStrategy(prob_heuris), m_num_machines(0), m_num_samples(SGVector()) +MulticlassOneVsOneStrategy::MulticlassOneVsOneStrategy(EProbHeuristicType prob_heuris) + :MulticlassStrategy(prob_heuris), m_num_machines(0), m_num_samples(SGVector()) { register_parameters(); } -void CMulticlassOneVsOneStrategy::register_parameters() +void MulticlassOneVsOneStrategy::register_parameters() { //SG_ADD(&m_num_samples, "num_samples", "Number of samples in each training machine"); - io::warn("{}::CMulticlassOneVsOneStrategy(): register parameters!", get_name()); + io::warn("{}::MulticlassOneVsOneStrategy(): register parameters!", get_name()); } -void CMulticlassOneVsOneStrategy::train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels) +void MulticlassOneVsOneStrategy::train_start(std::shared_ptrorig_labels, std::shared_ptrtrain_labels) { - CMulticlassStrategy::train_start(orig_labels, train_labels); + MulticlassStrategy::train_start(orig_labels, train_labels); m_num_machines=m_num_classes*(m_num_classes-1)/2; m_train_pair_idx_1 = 0; @@ -41,28 +41,30 @@ void CMulticlassOneVsOneStrategy::train_start(CMulticlassLabels *orig_labels, CB m_num_samples.resize_vector(m_num_machines); } -bool CMulticlassOneVsOneStrategy::train_has_more() +bool MulticlassOneVsOneStrategy::train_has_more() { return m_train_iter < m_num_machines; } -SGVector CMulticlassOneVsOneStrategy::train_prepare_next() +SGVector MulticlassOneVsOneStrategy::train_prepare_next() { - CMulticlassStrategy::train_prepare_next(); + MulticlassStrategy::train_prepare_next(); SGVector subset(m_orig_labels->get_num_labels()); int32_t tot=0; + auto mc_orig = multiclass_labels(m_orig_labels); + auto binary_train = binary_labels(m_train_labels); for (int32_t k=0; k < m_orig_labels->get_num_labels(); ++k) { - if (((CMulticlassLabels*) m_orig_labels)->get_int_label(k)==m_train_pair_idx_1) + if (mc_orig->get_int_label(k)==m_train_pair_idx_1) { - ((CBinaryLabels*) m_train_labels)->set_label(k, +1.0); + binary_train->set_label(k, +1.0); subset[tot]=k; tot++; } - else if (((CMulticlassLabels*) m_orig_labels)->get_int_label(k)==m_train_pair_idx_2) + else if (mc_orig->get_int_label(k)==m_train_pair_idx_2) { - ((CBinaryLabels*) m_train_labels)->set_label(k, -1.0); + binary_train->set_label(k, -1.0); subset[tot]=k; tot++; } @@ -82,11 +84,11 @@ SGVector CMulticlassOneVsOneStrategy::train_prepare_next() return subset; } -int32_t CMulticlassOneVsOneStrategy::decide_label(SGVector outputs) +int32_t MulticlassOneVsOneStrategy::decide_label(SGVector outputs) { // if OVO with prob outputs, find max posterior if (outputs.vlen==m_num_classes) - return CMath::arg_max(outputs.vector, 1, outputs.vlen); + return Math::arg_max(outputs.vector, 1, outputs.vlen); int32_t s=0; SGVector votes(m_num_classes); @@ -101,12 +103,12 @@ int32_t CMulticlassOneVsOneStrategy::decide_label(SGVector outputs) if (outputs[s]>0) { votes[i]++; - dec_vals[i] += CMath::abs(outputs[s]); + dec_vals[i] += Math::abs(outputs[s]); } else { votes[j]++; - dec_vals[j] += CMath::abs(outputs[s]); + dec_vals[j] += Math::abs(outputs[s]); } s++; } @@ -137,7 +139,7 @@ int32_t CMulticlassOneVsOneStrategy::decide_label(SGVector outputs) return i_max; } -void CMulticlassOneVsOneStrategy::rescale_outputs(SGVector outputs) +void MulticlassOneVsOneStrategy::rescale_outputs(SGVector outputs) { if (m_num_machines < 1) return; @@ -178,7 +180,7 @@ void CMulticlassOneVsOneStrategy::rescale_outputs(SGVector outputs) } } -void CMulticlassOneVsOneStrategy::rescale_heuris_price(SGVector outputs, +void MulticlassOneVsOneStrategy::rescale_heuris_price(SGVector outputs, const SGVector indx1, const SGVector indx2) { if (m_num_machines != outputs.vlen) @@ -210,7 +212,7 @@ void CMulticlassOneVsOneStrategy::rescale_heuris_price(SGVector outpu outputs[i] = new_outputs[i] / norm; } -void CMulticlassOneVsOneStrategy::rescale_heuris_hastie(SGVector outputs, +void MulticlassOneVsOneStrategy::rescale_heuris_hastie(SGVector outputs, const SGVector indx1, const SGVector indx2) { if (m_num_machines != outputs.vlen) @@ -286,7 +288,7 @@ void CMulticlassOneVsOneStrategy::rescale_heuris_hastie(SGVector outp outputs[i] = new_outputs[i]; } -void CMulticlassOneVsOneStrategy::rescale_heuris_hamamura(SGVector outputs, +void MulticlassOneVsOneStrategy::rescale_heuris_hamamura(SGVector outputs, const SGVector indx1, const SGVector indx2) { if (m_num_machines != outputs.vlen) diff --git a/src/shogun/multiclass/MulticlassOneVsOneStrategy.h b/src/shogun/multiclass/MulticlassOneVsOneStrategy.h index 9de0c055bfd..a341bed3238 100644 --- a/src/shogun/multiclass/MulticlassOneVsOneStrategy.h +++ b/src/shogun/multiclass/MulticlassOneVsOneStrategy.h @@ -21,22 +21,22 @@ namespace shogun * [1] J. Milgram, M. Cheriet, R.Sabourin, "One Against One" or "One Against One": * Which One is Better for Handwriting Recognition with SVMs? */ -class CMulticlassOneVsOneStrategy: public CMulticlassStrategy +class MulticlassOneVsOneStrategy: public MulticlassStrategy { public: /** constructor */ - CMulticlassOneVsOneStrategy(); + MulticlassOneVsOneStrategy(); /** constructor * @param prob_heuris probability estimation heuristic */ - CMulticlassOneVsOneStrategy(EProbHeuristicType prob_heuris); + MulticlassOneVsOneStrategy(EProbHeuristicType prob_heuris); /** destructor */ - virtual ~CMulticlassOneVsOneStrategy() {} + virtual ~MulticlassOneVsOneStrategy() {} /** start training */ - virtual void train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels); + virtual void train_start(std::shared_ptrorig_labels, std::shared_ptrtrain_labels); /** has more training phase */ virtual bool train_has_more(); @@ -76,7 +76,7 @@ class CMulticlassOneVsOneStrategy: public CMulticlassStrategy */ void set_num_classes(int32_t num_classes) { - CMulticlassStrategy::set_num_classes(num_classes); + MulticlassStrategy::set_num_classes(num_classes); m_num_machines = m_num_classes*(m_num_classes-1)/2; } diff --git a/src/shogun/multiclass/MulticlassOneVsRestStrategy.cpp b/src/shogun/multiclass/MulticlassOneVsRestStrategy.cpp index ef135709ce1..dd201c7410d 100644 --- a/src/shogun/multiclass/MulticlassOneVsRestStrategy.cpp +++ b/src/shogun/multiclass/MulticlassOneVsRestStrategy.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Chiyuan Zhang, Shell Hu, Soeren Sonnenburg, Sergey Lisitsyn, + * Authors: Chiyuan Zhang, Shell Hu, Soeren Sonnenburg, Sergey Lisitsyn, * Bjoern Esser, Sanuj Sharma */ @@ -12,41 +12,43 @@ using namespace shogun; -CMulticlassOneVsRestStrategy::CMulticlassOneVsRestStrategy() - : CMulticlassStrategy() +MulticlassOneVsRestStrategy::MulticlassOneVsRestStrategy() + : MulticlassStrategy() { } -CMulticlassOneVsRestStrategy::CMulticlassOneVsRestStrategy(EProbHeuristicType prob_heuris) - : CMulticlassStrategy(prob_heuris) +MulticlassOneVsRestStrategy::MulticlassOneVsRestStrategy(EProbHeuristicType prob_heuris) + : MulticlassStrategy(prob_heuris) { } -SGVector CMulticlassOneVsRestStrategy::train_prepare_next() +SGVector MulticlassOneVsRestStrategy::train_prepare_next() { + auto mc_orig = multiclass_labels(m_orig_labels); + auto binary_train = binary_labels(m_train_labels); for (int32_t i=0; i < m_orig_labels->get_num_labels(); ++i) { - if (((CMulticlassLabels*) m_orig_labels)->get_int_label(i)==m_train_iter) - ((CBinaryLabels*) m_train_labels)->set_label(i, +1.0); + if (mc_orig->get_int_label(i)==m_train_iter) + binary_train->set_label(i, +1.0); else - ((CBinaryLabels*) m_train_labels)->set_label(i, -1.0); + binary_train->set_label(i, -1.0); } // increase m_train_iter *after* setting labels - CMulticlassStrategy::train_prepare_next(); + MulticlassStrategy::train_prepare_next(); return SGVector(); } -int32_t CMulticlassOneVsRestStrategy::decide_label(SGVector outputs) +int32_t MulticlassOneVsRestStrategy::decide_label(SGVector outputs) { if (m_rejection_strategy && m_rejection_strategy->reject(outputs)) - return CDenseLabels::REJECTION_LABEL; + return DenseLabels::REJECTION_LABEL; - return CMath::arg_max(outputs.vector, 1, outputs.vlen); + return Math::arg_max(outputs.vector, 1, outputs.vlen); } -SGVector CMulticlassOneVsRestStrategy::decide_label_multiple_output(SGVector outputs, int32_t n_outputs) +SGVector MulticlassOneVsRestStrategy::decide_label_multiple_output(SGVector outputs, int32_t n_outputs) { float64_t* outputs_ = SG_MALLOC(float64_t, outputs.vlen); int32_t* indices_ = SG_MALLOC(int32_t, outputs.vlen); @@ -55,7 +57,7 @@ SGVector CMulticlassOneVsRestStrategy::decide_label_multiple_output(SGV outputs_[i] = outputs[i]; indices_[i] = i; } - CMath::qsort_backward_index(outputs_,indices_,outputs.vlen); + Math::qsort_backward_index(outputs_,indices_,outputs.vlen); SGVector result(n_outputs); for (int32_t i=0; i CMulticlassOneVsRestStrategy::decide_label_multiple_output(SGV return result; } -void CMulticlassOneVsRestStrategy::rescale_outputs(SGVector outputs) +void MulticlassOneVsRestStrategy::rescale_outputs(SGVector outputs) { switch(get_prob_heuris_type()) { @@ -82,7 +84,7 @@ void CMulticlassOneVsRestStrategy::rescale_outputs(SGVector outputs) } } -void CMulticlassOneVsRestStrategy::rescale_outputs(SGVector outputs, +void MulticlassOneVsRestStrategy::rescale_outputs(SGVector outputs, const SGVector As, const SGVector Bs) { if (get_prob_heuris_type()==OVA_SOFTMAX) @@ -91,7 +93,7 @@ void CMulticlassOneVsRestStrategy::rescale_outputs(SGVector outputs, rescale_outputs(outputs); } -void CMulticlassOneVsRestStrategy::rescale_heuris_norm(SGVector outputs) +void MulticlassOneVsRestStrategy::rescale_heuris_norm(SGVector outputs) { if (m_num_classes != outputs.vlen) { @@ -105,7 +107,7 @@ void CMulticlassOneVsRestStrategy::rescale_heuris_norm(SGVector outpu outputs[i] /= norm; } -void CMulticlassOneVsRestStrategy::rescale_heuris_softmax(SGVector outputs, +void MulticlassOneVsRestStrategy::rescale_heuris_softmax(SGVector outputs, const SGVector As, const SGVector Bs) { if (m_num_classes != outputs.vlen) diff --git a/src/shogun/multiclass/MulticlassOneVsRestStrategy.h b/src/shogun/multiclass/MulticlassOneVsRestStrategy.h index ad9b9e936ba..34fec730008 100644 --- a/src/shogun/multiclass/MulticlassOneVsRestStrategy.h +++ b/src/shogun/multiclass/MulticlassOneVsRestStrategy.h @@ -26,24 +26,24 @@ namespace shogun * [1] J. Milgram, M. Cheriet, R.Sabourin, "One Against One" or "One Against One": * Which One is Better for Handwriting Recognition with SVMs? */ -class CMulticlassOneVsRestStrategy: public CMulticlassStrategy +class MulticlassOneVsRestStrategy: public MulticlassStrategy { public: /** constructor */ - CMulticlassOneVsRestStrategy(); + MulticlassOneVsRestStrategy(); /** constructor * @param prob_heuris probability estimation heuristic */ - CMulticlassOneVsRestStrategy(EProbHeuristicType prob_heuris); + MulticlassOneVsRestStrategy(EProbHeuristicType prob_heuris); /** destructor */ - virtual ~CMulticlassOneVsRestStrategy() {} + virtual ~MulticlassOneVsRestStrategy() {} /** start training */ - virtual void train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels) + virtual void train_start(std::shared_ptrorig_labels, std::shared_ptrtrain_labels) { - CMulticlassStrategy::train_start(orig_labels, train_labels); + MulticlassStrategy::train_start(orig_labels, train_labels); } /** has more training phase */ diff --git a/src/shogun/multiclass/MulticlassSVM.cpp b/src/shogun/multiclass/MulticlassSVM.cpp index 73dc30f48cb..9ebc4e7dadd 100644 --- a/src/shogun/multiclass/MulticlassSVM.cpp +++ b/src/shogun/multiclass/MulticlassSVM.cpp @@ -11,67 +11,61 @@ using namespace shogun; -CMulticlassSVM::CMulticlassSVM() - :CKernelMulticlassMachine(new CMulticlassOneVsRestStrategy(), NULL, new CSVM(0), NULL) +MulticlassSVM::MulticlassSVM() + :MulticlassSVM(std::make_shared()) { - init(); } -CMulticlassSVM::CMulticlassSVM(CMulticlassStrategy *strategy) - :CKernelMulticlassMachine(strategy, NULL, new CSVM(0), NULL) +MulticlassSVM::MulticlassSVM(std::shared_ptrstrategy) + :KernelMulticlassMachine(strategy, NULL, std::make_shared(0), NULL) { init(); } -CMulticlassSVM::CMulticlassSVM( - CMulticlassStrategy *strategy, float64_t C, CKernel* k, CLabels* lab) - : CKernelMulticlassMachine(strategy, k, new CSVM(C, k, lab), lab) +MulticlassSVM::MulticlassSVM( + std::shared_ptrstrategy, float64_t C, std::shared_ptr k, std::shared_ptr lab) + : KernelMulticlassMachine(strategy, k, std::make_shared(C, k, lab), lab) { init(); m_C=C; } -CMulticlassSVM::~CMulticlassSVM() +MulticlassSVM::~MulticlassSVM() { } -void CMulticlassSVM::init() +void MulticlassSVM::init() { SG_ADD(&m_C, "C", "C regularization constant",ParameterProperties::HYPER); m_C=0; } -bool CMulticlassSVM::create_multiclass_svm(int32_t num_classes) +bool MulticlassSVM::create_multiclass_svm(int32_t num_classes) { if (num_classes>0) { - int32_t num_svms=m_multiclass_strategy->get_num_machines(); - - m_machines->reset_array(); - for (index_t i=0; ipush_back(NULL); - + m_machines.clear(); return true; } return false; } -bool CMulticlassSVM::set_svm(int32_t num, CSVM* svm) +bool MulticlassSVM::set_svm(int32_t num, std::shared_ptr svm) { - if (m_machines->get_num_elements()>0 && m_machines->get_num_elements()>num && num>=0 && svm) + if (!m_machines.empty() && m_machines.size()>num && num>=0 && svm) { - m_machines->set_element(svm, num); + m_machines.insert(m_machines.begin()+num, svm); return true; } return false; } -bool CMulticlassSVM::init_machines_for_apply(CFeatures* data) +bool MulticlassSVM::init_machines_for_apply(std::shared_ptr data) { if (!m_kernel) error("No kernel assigned!"); - CFeatures* lhs=m_kernel->get_lhs(); + auto lhs=m_kernel->get_lhs(); if (!lhs && m_kernel->get_kernel_type()!=K_COMBINED) error("{}: No left hand side specified", get_name()); @@ -85,203 +79,14 @@ bool CMulticlassSVM::init_machines_for_apply(CFeatures* data) if (data && m_kernel->get_kernel_type()!=K_COMBINED) m_kernel->init(lhs, data); - SG_UNREF(lhs); - for (int32_t i=0; iget_num_elements(); i++) + for (auto m: m_machines) { - CSVM *the_svm = (CSVM *)m_machines->get_element(i); + auto the_svm = m->as(); ASSERT(the_svm) the_svm->set_kernel(m_kernel); - SG_UNREF(the_svm); } return true; } -bool CMulticlassSVM::load(FILE* modelfl) -{ - bool result=true; - char char_buffer[1024]; - int32_t int_buffer; - float64_t double_buffer; - int32_t line_number=1; - int32_t svm_idx=-1; - - SG_SET_LOCALE_C; - - if (fscanf(modelfl,"%15s\n", char_buffer)==EOF) - error("error in svm file, line nr:{}", line_number); - else - { - char_buffer[15]='\0'; - if (strcmp("%MultiClassSVM", char_buffer)!=0) - error("error in multiclass svm file, line nr:{}", line_number); - - line_number++; - } - - int_buffer=0; - if (fscanf(modelfl," num_classes=%d; \n", &int_buffer) != 1) - error("error in svm file, line nr:{}", line_number); - - if (!feof(modelfl)) - line_number++; - - if (int_buffer < 2) - error("less than 2 classes - how is this multiclass?"); - - create_multiclass_svm(int_buffer); - - int_buffer=0; - if (fscanf(modelfl," num_svms=%d; \n", &int_buffer) != 1) - error("error in svm file, line nr:{}", line_number); - - if (!feof(modelfl)) - line_number++; - - if (m_machines->get_num_elements() != int_buffer) - error("Mismatch in number of svms: m_num_svms={} vs m_num_svms(file)={}", m_machines->get_num_elements(), int_buffer); - - if (fscanf(modelfl," kernel='%s'; \n", char_buffer) != 1) - error("error in svm file, line nr:{}", line_number); - - if (!feof(modelfl)) - line_number++; - - for (int32_t n=0; nget_num_elements(); n++) - { - svm_idx=-1; - if (fscanf(modelfl,"\n%4s %d of %d\n", char_buffer, &svm_idx, &int_buffer)==EOF) - { - result=false; - error("error in svm file, line nr:{}", line_number); - } - else - { - char_buffer[4]='\0'; - if (strncmp("%SVM", char_buffer, 4)!=0) - { - result=false; - error("error in svm file, line nr:{}", line_number); - } - - if (svm_idx != n) - error("svm index mismatch n={}, n(file)={}", n, svm_idx); - - line_number++; - } - - int_buffer=0; - if (fscanf(modelfl,"numsv%d=%d;\n", &svm_idx, &int_buffer) != 2) - error("error in svm file, line nr:{}", line_number); - - if (svm_idx != n) - error("svm index mismatch n={}, n(file)={}", n, svm_idx); - - if (!feof(modelfl)) - line_number++; - - io::info("loading {} support vectors for svm {}",int_buffer, svm_idx); - CSVM* svm=new CSVM(int_buffer); - - double_buffer=0; - - if (fscanf(modelfl," b%d=%lf; \n", &svm_idx, &double_buffer) != 2) - error("error in svm file, line nr:{}", line_number); - - if (svm_idx != n) - error("svm index mismatch n={}, n(file)={}", n, svm_idx); - - if (!feof(modelfl)) - line_number++; - - svm->set_bias(double_buffer); - - if (fscanf(modelfl,"alphas%d=[\n", &svm_idx) != 1) - error("error in svm file, line nr:{}", line_number); - - if (svm_idx != n) - error("svm index mismatch n={}, n(file)={}", n, svm_idx); - - if (!feof(modelfl)) - line_number++; - - for (int32_t i=0; iget_num_support_vectors(); i++) - { - double_buffer=0; - int_buffer=0; - - if (fscanf(modelfl,"\t[%lf,%d]; \n", &double_buffer, &int_buffer) != 2) - error("error in svm file, line nr:{}", line_number); - - if (!feof(modelfl)) - line_number++; - - svm->set_support_vector(i, int_buffer); - svm->set_alpha(i, double_buffer); - } - - if (fscanf(modelfl,"%2s", char_buffer) == EOF) - { - result=false; - error("error in svm file, line nr:{}", line_number); - } - else - { - char_buffer[3]='\0'; - if (strcmp("];", char_buffer)!=0) - { - result=false; - error("error in svm file, line nr:{}", line_number); - } - line_number++; - } - - set_svm(n, svm); - } - - svm_proto()->svm_loaded=result; - - SG_RESET_LOCALE; - return result; -} - -bool CMulticlassSVM::save(FILE* modelfl) -{ - SG_SET_LOCALE_C; - - if (!m_kernel) - error("Kernel not defined!"); - - if (m_machines->get_num_elements()<1) - error("Multiclass SVM not trained!"); - - io::info("Writing model file..."); - fprintf(modelfl,"%%MultiClassSVM\n"); - fprintf(modelfl,"num_classes=%d;\n", m_multiclass_strategy->get_num_classes()); - fprintf(modelfl,"num_svms=%d;\n", m_machines->get_num_elements()); - fprintf(modelfl,"kernel='%s';\n", m_kernel->get_name()); - - for (int32_t i=0; iget_num_elements(); i++) - { - CSVM* svm=get_svm(i); - ASSERT(svm) - fprintf(modelfl,"\n%%SVM %d of %d\n", i, m_machines->get_num_elements()-1); - fprintf(modelfl,"numsv%d=%d;\n", i, svm->get_num_support_vectors()); - fprintf(modelfl,"b%d=%+10.16e;\n",i,svm->get_bias()); - - fprintf(modelfl, "alphas%d=[\n", i); - - for(int32_t j=0; jget_num_support_vectors(); j++) - { - fprintf(modelfl,"\t[%+10.16e,%d];\n", - svm->get_alpha(j), svm->get_support_vector(j)); - } - - fprintf(modelfl, "];\n"); - } - - SG_RESET_LOCALE; - io::progress_done(); - return true ; -} diff --git a/src/shogun/multiclass/MulticlassSVM.h b/src/shogun/multiclass/MulticlassSVM.h index 7b22f102a57..2f99839f6da 100644 --- a/src/shogun/multiclass/MulticlassSVM.h +++ b/src/shogun/multiclass/MulticlassSVM.h @@ -18,23 +18,23 @@ namespace shogun { -class CSVM; +class SVM; /** @brief class MultiClassSVM */ -class CMulticlassSVM : public CKernelMulticlassMachine +class MulticlassSVM : public KernelMulticlassMachine { public: /** problem type */ MACHINE_PROBLEM_TYPE(PT_MULTICLASS); /** default constructor */ - CMulticlassSVM(); + MulticlassSVM(); /** constructor * * @param strategy multiclass strategy */ - CMulticlassSVM(CMulticlassStrategy *strategy); + MulticlassSVM(std::shared_ptrstrategy); /** constructor * @@ -43,9 +43,9 @@ class CMulticlassSVM : public CKernelMulticlassMachine * @param k kernel * @param lab labels */ - CMulticlassSVM( - CMulticlassStrategy *strategy, float64_t C, CKernel* k, CLabels* lab); - virtual ~CMulticlassSVM(); + MulticlassSVM( + std::shared_ptrstrategy, float64_t C, std::shared_ptr k, std::shared_ptr lab); + virtual ~MulticlassSVM(); /** create multiclass SVM. Appends the appropriate number of svm pointer * (depending on multiclass strategy) to m_machines. All pointers are @@ -62,28 +62,18 @@ class CMulticlassSVM : public CKernelMulticlassMachine * @param svm SVM to set * @return if setting was successful */ - bool set_svm(int32_t num, CSVM* svm); + bool set_svm(int32_t num, std::shared_ptr svm); /** get SVM * * @param num which SVM to get * @return SVM at number num */ - CSVM* get_svm(int32_t num) const + std::shared_ptr get_svm(int32_t num) const { - return m_machines->get_element_safe(num)->as(); + return std::dynamic_pointer_cast(m_machines.at(num)); } - /** load a Multiclass SVM from file - * @param svm_file the file handle - */ - bool load(FILE* svm_file); - - /** write a Multiclass SVM to a file - * @param svm_file the file handle - */ - bool save(FILE* svm_file); - // TODO remove if unnecessary here /** get linear term of base SVM * @return linear term of base SVM @@ -211,9 +201,9 @@ class CMulticlassSVM : public CKernelMulticlassMachine protected: /** casts m_machine to SVM */ - CSVM *svm_proto() + std::shared_ptrsvm_proto() { - return dynamic_cast(m_machine); + return std::dynamic_pointer_cast(m_machine); } /** returns support vectors */ SGVector svm_svs() @@ -222,12 +212,12 @@ class CMulticlassSVM : public CKernelMulticlassMachine } /** initializes machines (OvO, OvR) for apply */ - virtual bool init_machines_for_apply(CFeatures* data); + virtual bool init_machines_for_apply(std::shared_ptr data); /** is machine an SVM instance */ - virtual bool is_acceptable_machine(CMachine *machine) + virtual bool is_acceptable_machine(std::shared_ptrmachine) { - CSVM *svm = dynamic_cast(machine); + auto svm = std::dynamic_pointer_cast(machine); if (svm == NULL) return false; return true; diff --git a/src/shogun/multiclass/MulticlassStrategy.cpp b/src/shogun/multiclass/MulticlassStrategy.cpp index 8a5d6c96cd2..6a37ba1c9a6 100644 --- a/src/shogun/multiclass/MulticlassStrategy.cpp +++ b/src/shogun/multiclass/MulticlassStrategy.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Chiyuan Zhang, Heiko Strathmann, Soeren Sonnenburg, Shell Hu, + * Authors: Chiyuan Zhang, Heiko Strathmann, Soeren Sonnenburg, Shell Hu, * Sergey Lisitsyn */ @@ -11,21 +11,21 @@ using namespace shogun; -CMulticlassStrategy::CMulticlassStrategy() - : CSGObject() +MulticlassStrategy::MulticlassStrategy() + : SGObject() { init(); } -CMulticlassStrategy::CMulticlassStrategy(EProbHeuristicType prob_heuris) - : CSGObject() +MulticlassStrategy::MulticlassStrategy(EProbHeuristicType prob_heuris) + : SGObject() { init(); m_prob_heuris=prob_heuris; } -void CMulticlassStrategy::init() +void MulticlassStrategy::init() { m_rejection_strategy=NULL; m_train_labels=NULL; @@ -35,37 +35,36 @@ void CMulticlassStrategy::init() m_num_classes=0; SG_ADD( - (CSGObject**)&m_rejection_strategy, "rejection_strategy", + (std::shared_ptr*)&m_rejection_strategy, "rejection_strategy", "Strategy of rejection"); SG_ADD(&m_num_classes, "num_classes", "Number of classes"); SG_ADD_OPTIONS( (machine_int_t*)&m_prob_heuris, "prob_heuris", "Probability estimation heuristics", ParameterProperties::NONE, SG_OPTIONS(PROB_HEURIS_NONE, OVA_NORM, OVA_SOFTMAX, OVO_PRICE, OVO_HASTIE, - OVO_HAMAMURA)) + OVO_HAMAMURA)); } -void CMulticlassStrategy::train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels) +void MulticlassStrategy::train_start(std::shared_ptrorig_labels, std::shared_ptrtrain_labels) { if (m_train_labels != NULL) error("Stop the previous training task before starting a new one!"); - SG_REF(train_labels); m_train_labels=train_labels; - SG_REF(orig_labels); + m_orig_labels=orig_labels; m_train_iter=0; } -SGVector CMulticlassStrategy::train_prepare_next() +SGVector MulticlassStrategy::train_prepare_next() { m_train_iter++; return SGVector(); } -void CMulticlassStrategy::train_stop() +void MulticlassStrategy::train_stop() { - SG_UNREF(m_train_labels); - SG_UNREF(m_orig_labels); + + m_train_labels = NULL; m_orig_labels = NULL; } diff --git a/src/shogun/multiclass/MulticlassStrategy.h b/src/shogun/multiclass/MulticlassStrategy.h index 80df37e01da..1470f5e84ed 100644 --- a/src/shogun/multiclass/MulticlassStrategy.h +++ b/src/shogun/multiclass/MulticlassStrategy.h @@ -42,19 +42,19 @@ enum EProbHeuristicType /** @brief class MulticlassStrategy used to construct generic * multiclass classifiers with ensembles of binary classifiers */ -class CMulticlassStrategy: public CSGObject +class MulticlassStrategy: public SGObject { public: /** constructor */ - CMulticlassStrategy(); + MulticlassStrategy(); /** constructor * @param prob_heuris probability estimation heuristic */ - CMulticlassStrategy(EProbHeuristicType prob_heuris); + MulticlassStrategy(EProbHeuristicType prob_heuris); /** destructor */ - virtual ~CMulticlassStrategy() {} + virtual ~MulticlassStrategy() {} /** get name */ virtual const char* get_name() const @@ -75,22 +75,22 @@ class CMulticlassStrategy: public CSGObject } /** get rejection strategy */ - CRejectionStrategy *get_rejection_strategy() + std::shared_ptrget_rejection_strategy() { - SG_REF(m_rejection_strategy); + return m_rejection_strategy; } /** set rejection strategy */ - void set_rejection_strategy(CRejectionStrategy *rejection_strategy) + void set_rejection_strategy(std::shared_ptrrejection_strategy) { - SG_REF(rejection_strategy); - SG_UNREF(m_rejection_strategy); + + m_rejection_strategy = rejection_strategy; } /** start training */ - virtual void train_start(CMulticlassLabels *orig_labels, CBinaryLabels *train_labels); + virtual void train_start(std::shared_ptrorig_labels, std::shared_ptrtrain_labels); /** has more training phase */ virtual bool train_has_more()=0; @@ -148,7 +148,7 @@ class CMulticlassStrategy: public CSGObject /** rescale multiclass outputs according to the selected heuristic * this function only being called with OVA_SOFTMAX heuristic - * the CStatistics::fit_sigmoid() should be called first + * the Statistics::fit_sigmoid() should be called first * @param outputs a vector of output from each machine (in that order) * @param As fitted sigmoid parameters a one for each machine * @param Bs fitted sigmoid parameters b one for each machine @@ -165,9 +165,9 @@ class CMulticlassStrategy: public CSGObject protected: - CRejectionStrategy* m_rejection_strategy; ///< rejection strategy - CBinaryLabels *m_train_labels; ///< labels used to train the submachines - CMulticlassLabels *m_orig_labels; ///< original multiclass labels + std::shared_ptr m_rejection_strategy; ///< rejection strategy + std::shared_ptrm_train_labels; ///< labels used to train the submachines + std::shared_ptrm_orig_labels; ///< original multiclass labels int32_t m_train_iter; ///< index of current iterations int32_t m_num_classes; ///< number of classes in this problem EProbHeuristicType m_prob_heuris; ///< prob output heuristic diff --git a/src/shogun/multiclass/QDA.cpp b/src/shogun/multiclass/QDA.cpp index 44f872e1a8c..865cd094543 100644 --- a/src/shogun/multiclass/QDA.cpp +++ b/src/shogun/multiclass/QDA.cpp @@ -1,8 +1,8 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Kevin Hughes, Soeren Sonnenburg, Sergey Lisitsyn, Michele Mazzoni, - * Heiko Strathmann, Sanuj Sharma, Weijie Lin, Bjoern Esser, + * Authors: Kevin Hughes, Soeren Sonnenburg, Sergey Lisitsyn, Michele Mazzoni, + * Heiko Strathmann, Sanuj Sharma, Weijie Lin, Bjoern Esser, * Youssef Emad El-Din, Sourav Singh, Pan Deng */ @@ -21,29 +21,29 @@ using namespace shogun; using namespace Eigen; -CQDA::CQDA() : CNativeMulticlassMachine(), m_num_classes(0), m_dim(0) +QDA::QDA() : NativeMulticlassMachine(), m_num_classes(0), m_dim(0) { init(); } -CQDA::CQDA(float64_t tolerance, bool store_covs) -: CNativeMulticlassMachine(), m_num_classes(0), m_dim(0) +QDA::QDA(float64_t tolerance, bool store_covs) +: NativeMulticlassMachine(), m_num_classes(0), m_dim(0) { init(); m_tolerance = tolerance; m_store_covs = store_covs; } -CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab) -: CNativeMulticlassMachine(), m_num_classes(0), m_dim(0) +QDA::QDA(std::shared_ptr> traindat, std::shared_ptr trainlab) +: NativeMulticlassMachine(), m_num_classes(0), m_dim(0) { init(); set_features(traindat); set_labels(trainlab); } -CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tolerance) -: CNativeMulticlassMachine(), m_num_classes(0), m_dim(0) +QDA::QDA(std::shared_ptr> traindat, std::shared_ptr trainlab, float64_t tolerance) +: NativeMulticlassMachine(), m_num_classes(0), m_dim(0) { init(); set_features(traindat); @@ -51,8 +51,8 @@ CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tol m_tolerance = tolerance; } -CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab, bool store_covs) -: CNativeMulticlassMachine(), m_num_classes(0), m_dim(0) +QDA::QDA(std::shared_ptr> traindat, std::shared_ptr trainlab, bool store_covs) +: NativeMulticlassMachine(), m_num_classes(0), m_dim(0) { init(); set_features(traindat); @@ -62,8 +62,8 @@ CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab, bool store_co -CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tolerance, bool store_covs) -: CNativeMulticlassMachine(), m_num_classes(0), m_dim(0) +QDA::QDA(std::shared_ptr> traindat, std::shared_ptr trainlab, float64_t tolerance, bool store_covs) +: NativeMulticlassMachine(), m_num_classes(0), m_dim(0) { init(); set_features(traindat); @@ -72,20 +72,20 @@ CQDA::CQDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tol m_store_covs = store_covs; } -CQDA::~CQDA() +QDA::~QDA() { - SG_UNREF(m_features); + cleanup(); } -void CQDA::init() +void QDA::init() { m_tolerance = 1e-4; m_store_covs = false; SG_ADD(&m_tolerance, "m_tolerance", "Tolerance member.", ParameterProperties::HYPER); SG_ADD(&m_store_covs, "m_store_covs", "Store covariances member"); - SG_ADD((CSGObject**) &m_features, "m_features", "Feature object."); + SG_ADD((std::shared_ptr*) &m_features, "m_features", "Feature object."); SG_ADD(&m_means, "m_means", "Mean vectors list"); SG_ADD(&m_slog, "m_slog", "Vector used in classification"); SG_ADD(&m_dim, "m_dim", "dimension of feature space"); @@ -96,21 +96,21 @@ void CQDA::init() m_features = NULL; } -void CQDA::cleanup() +void QDA::cleanup() { m_means=SGMatrix(); m_num_classes = 0; } -CMulticlassLabels* CQDA::apply_multiclass(CFeatures* data) +std::shared_ptr QDA::apply_multiclass(std::shared_ptr data) { if (data) { if (!data->has_property(FP_DOT)) error("Specified features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(data->as()); } if ( !m_features ) @@ -120,7 +120,7 @@ CMulticlassLabels* CQDA::apply_multiclass(CFeatures* data) ASSERT(num_vecs > 0) ASSERT( m_dim == m_features->get_dim_feature_space() ) - CDenseFeatures< float64_t >* rf = (CDenseFeatures< float64_t >*) m_features; + auto rf = m_features->as>(); MatrixXd X(num_vecs, m_dim); MatrixXd A(num_vecs, m_dim); @@ -160,15 +160,15 @@ CMulticlassLabels* CQDA::apply_multiclass(CFeatures* data) } - CMulticlassLabels* out = new CMulticlassLabels(num_vecs); + auto out = std::make_shared(num_vecs); for (int i = 0 ; i < num_vecs; i++) - out->set_label(i, CMath::arg_max(norm2.data()+i, num_vecs, m_num_classes)); + out->set_label(i, Math::arg_max(norm2.data()+i, num_vecs, m_num_classes)); return out; } -bool CQDA::train_machine(CFeatures* data) +bool QDA::train_machine(std::shared_ptr data) { if (!m_labels) error("No labels allocated in QDA training"); @@ -178,20 +178,20 @@ bool CQDA::train_machine(CFeatures* data) if (!data->has_property(FP_DOT)) error("Speficied features are not of type CDotFeatures"); - set_features((CDotFeatures*) data); + set_features(data->as()); } if (!m_features) error("No features allocated in QDA training"); - SGVector< int32_t > train_labels = ((CMulticlassLabels*) m_labels)->get_int_labels(); + SGVector< int32_t > train_labels = multiclass_labels(m_labels)->get_int_labels(); if (!train_labels.vector) error("No train_labels allocated in QDA training"); cleanup(); - m_num_classes = ((CMulticlassLabels*) m_labels)->get_num_classes(); + m_num_classes = multiclass_labels(m_labels)->get_num_classes(); m_dim = m_features->get_dim_feature_space(); int32_t num_vec = m_features->get_num_vectors(); @@ -247,7 +247,7 @@ bool CQDA::train_machine(CFeatures* data) rot_dims[2] = m_num_classes; SGNDArray< float64_t > rotations = SGNDArray< float64_t >(rot_dims, 3); - CDenseFeatures< float64_t >* rf = (CDenseFeatures< float64_t >*) m_features; + auto rf = m_features->as>(); m_means.zero(); diff --git a/src/shogun/multiclass/QDA.h b/src/shogun/multiclass/QDA.h index 63032f688e3..af31390687c 100644 --- a/src/shogun/multiclass/QDA.h +++ b/src/shogun/multiclass/QDA.h @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Heiko Strathmann, Michele Mazzoni, Kevin Hughes, + * Authors: Soeren Sonnenburg, Heiko Strathmann, Michele Mazzoni, Kevin Hughes, * Thoralf Klein, Fernando Iglesias, Sergey Lisitsyn, Pan Deng */ @@ -29,27 +29,27 @@ namespace shogun * the classes are distributed with equal co-variance. * TODO */ -class CQDA : public CNativeMulticlassMachine +class QDA : public NativeMulticlassMachine { public: MACHINE_PROBLEM_TYPE(PT_MULTICLASS) /** default constructor */ - CQDA(); + QDA(); /** constructor * * @param tolerance tolerance used in training * @param store_covs whether to store the within class covariances */ - CQDA(float64_t tolerance, bool store_covs); + QDA(float64_t tolerance, bool store_covs); /** constructor * * @param traindat training features * @param trainlab labels for training features */ - CQDA(CDenseFeatures* traindat, CLabels* trainlab); + QDA(std::shared_ptr> traindat, std::shared_ptr trainlab); /** constructor * @@ -57,7 +57,7 @@ class CQDA : public CNativeMulticlassMachine * @param trainlab labels for training features * @param tolerance tolerance used in training */ - CQDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tolerance); + QDA(std::shared_ptr> traindat, std::shared_ptr trainlab, float64_t tolerance); /** constructor * @@ -65,7 +65,7 @@ class CQDA : public CNativeMulticlassMachine * @param trainlab labels for training features * @param store_covs whether to store the within class covariances */ - CQDA(CDenseFeatures* traindat, CLabels* trainlab, bool store_covs); + QDA(std::shared_ptr> traindat, std::shared_ptr trainlab, bool store_covs); /** constructor * @@ -74,16 +74,16 @@ class CQDA : public CNativeMulticlassMachine * @param tolerance tolerance used in training * @param store_covs whether to store the within class covariances */ - CQDA(CDenseFeatures* traindat, CLabels* trainlab, float64_t tolerance, bool store_covs); + QDA(std::shared_ptr> traindat, std::shared_ptr trainlab, float64_t tolerance, bool store_covs); - virtual ~CQDA(); + virtual ~QDA(); /** apply QDA to data * * @param data (test) data to be classified * @return labels result of classification */ - virtual CMulticlassLabels* apply_multiclass(CFeatures* data=NULL); + virtual std::shared_ptr apply_multiclass(std::shared_ptr data=NULL); /** set store_covs * @@ -119,14 +119,14 @@ class CQDA : public CNativeMulticlassMachine * * @param feat features to set */ - virtual void set_features(CDotFeatures* feat) + virtual void set_features(std::shared_ptr feat) { if (feat->get_feature_class() != C_DENSE || feat->get_feature_type() != F_DREAL) error("QDA requires SIMPLE REAL valued features"); - SG_REF(feat); - SG_UNREF(m_features); + + m_features = feat; } @@ -134,7 +134,7 @@ class CQDA : public CNativeMulticlassMachine * * @return features */ - virtual CDotFeatures* get_features() { SG_REF(m_features); return m_features; } + virtual std::shared_ptr get_features() { return m_features; } /** get object name * @@ -173,7 +173,7 @@ class CQDA : public CNativeMulticlassMachine * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data = NULL); + virtual bool train_machine(std::shared_ptr data = NULL); private: void init(); @@ -182,7 +182,7 @@ class CQDA : public CNativeMulticlassMachine private: /** feature vectors */ - CDotFeatures* m_features; + std::shared_ptr m_features; /** tolerance used during training */ float64_t m_tolerance; diff --git a/src/shogun/multiclass/RejectionStrategy.h b/src/shogun/multiclass/RejectionStrategy.h index 567514ffdaa..a64a27ec626 100644 --- a/src/shogun/multiclass/RejectionStrategy.h +++ b/src/shogun/multiclass/RejectionStrategy.h @@ -11,14 +11,14 @@ namespace shogun { /** @brief base rejection strategy class */ -class CRejectionStrategy : public CSGObject +class RejectionStrategy : public SGObject { public: /** default constructor */ - CRejectionStrategy() { }; + RejectionStrategy() { }; /** destructor */ - virtual ~CRejectionStrategy() { }; + virtual ~RejectionStrategy() { }; /** get name */ virtual const char* get_name() const @@ -32,19 +32,19 @@ class CRejectionStrategy : public CSGObject }; /** @brief threshold based rejection strategy */ -class CThresholdRejectionStrategy : public CRejectionStrategy +class ThresholdRejectionStrategy : public RejectionStrategy { public: /** constructor */ - CThresholdRejectionStrategy() : - CRejectionStrategy(), m_threshold(0.0) { }; + ThresholdRejectionStrategy() : + RejectionStrategy(), m_threshold(0.0) { }; /** constructor */ - CThresholdRejectionStrategy(float64_t threshold) : - CRejectionStrategy(), m_threshold(threshold) { }; + ThresholdRejectionStrategy(float64_t threshold) : + RejectionStrategy(), m_threshold(threshold) { }; - virtual ~CThresholdRejectionStrategy() {}; + virtual ~ThresholdRejectionStrategy() {}; /** get name */ virtual const char* get_name() const @@ -90,13 +90,13 @@ static const float64_t Q_test_statistic_values[10][8] = * rejection strategy. Statistic values are taken from * http://www.vias.org/tmdatanaleng/cc_outlier_tests_dixon.html * */ -class CDixonQTestRejectionStrategy : public CRejectionStrategy +class DixonQTestRejectionStrategy : public RejectionStrategy { public: /** constructor */ - CDixonQTestRejectionStrategy() : - CRejectionStrategy() + DixonQTestRejectionStrategy() : + RejectionStrategy() { s_index = 3; } @@ -105,8 +105,8 @@ class CDixonQTestRejectionStrategy : public CRejectionStrategy * @param significance_level either 0.001,0.002,0.005, * 0.01,0.02,0.05,0.1 or 0.2 */ - CDixonQTestRejectionStrategy(float64_t significance_level) : - CRejectionStrategy() + DixonQTestRejectionStrategy(float64_t significance_level) : + RejectionStrategy() { if (significance_level==0.001) s_index = 0; @@ -127,7 +127,7 @@ class CDixonQTestRejectionStrategy : public CRejectionStrategy else error("Given significance level is not supported"); } - virtual ~CDixonQTestRejectionStrategy() + virtual ~DixonQTestRejectionStrategy() { } @@ -147,7 +147,7 @@ class CDixonQTestRejectionStrategy : public CRejectionStrategy int32_t Ni = N/10 - 1; SGVector outputs_local = outputs.clone(); - CMath::qsort(outputs_local); + Math::qsort(outputs_local); float64_t Q = 0.0; if (N==10) diff --git a/src/shogun/multiclass/ScatterSVM.cpp b/src/shogun/multiclass/ScatterSVM.cpp index 95cc518460d..b796da730b8 100644 --- a/src/shogun/multiclass/ScatterSVM.cpp +++ b/src/shogun/multiclass/ScatterSVM.cpp @@ -1,7 +1,7 @@ /* * This software is distributed under BSD 3-clause license (see LICENSE file). * - * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Chiyuan Zhang, Viktor Gal, + * Authors: Soeren Sonnenburg, Sergey Lisitsyn, Chiyuan Zhang, Viktor Gal, * Leon Kuchenbecker, Kyle McQuisten */ #include @@ -17,38 +17,38 @@ using namespace shogun; -CScatterSVM::CScatterSVM() -: CMulticlassSVM(new CMulticlassOneVsRestStrategy()), scatter_type(NO_BIAS_LIBSVM), +ScatterSVM::ScatterSVM() +: MulticlassSVM(std::make_shared()), scatter_type(NO_BIAS_LIBSVM), norm_wc(NULL), norm_wc_len(0), norm_wcw(NULL), norm_wcw_len(0), rho(0), m_num_classes(0) { unstable(SOURCE_LOCATION); } -CScatterSVM::CScatterSVM(SCATTER_TYPE type) -: CMulticlassSVM(new CMulticlassOneVsRestStrategy()), scatter_type(type), +ScatterSVM::ScatterSVM(SCATTER_TYPE type) +: MulticlassSVM(std::make_shared()), scatter_type(type), norm_wc(NULL), norm_wc_len(0), norm_wcw(NULL), norm_wcw_len(0), rho(0), m_num_classes(0) { } -CScatterSVM::CScatterSVM(float64_t C, CKernel* k, CLabels* lab) -: CMulticlassSVM(new CMulticlassOneVsRestStrategy(), C, k, lab), scatter_type(NO_BIAS_LIBSVM), +ScatterSVM::ScatterSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab) +: MulticlassSVM(std::make_shared(), C, k, lab), scatter_type(NO_BIAS_LIBSVM), norm_wc(NULL), norm_wc_len(0), norm_wcw(NULL), norm_wcw_len(0), rho(0), m_num_classes(0) { } -CScatterSVM::~CScatterSVM() +ScatterSVM::~ScatterSVM() { SG_FREE(norm_wc); SG_FREE(norm_wcw); } -void CScatterSVM::register_params() +void ScatterSVM::register_params() { - m_parameters->add_vector(&norm_wc, &norm_wc_len, "norm_wc", "Norm of w_c"); + /*m_parameters->add_vector(&norm_wc, &norm_wc_len, "norm_wc", "Norm of w_c");*/ watch_param("norm_wc", &norm_wc, &norm_wc_len); - m_parameters->add_vector( - &norm_wcw, &norm_wcw_len, "norm_wcw", "Norm of w_cw"); + /*m_parameters->add_vector( + &norm_wcw, &norm_wcw_len, "norm_wcw", "Norm of w_cw");*/ watch_param("norm_wcw", &norm_wcw, &norm_wcw_len); SG_ADD(&rho, "rho", "Scatter SVM rho"); @@ -67,7 +67,7 @@ void CScatterSVM::register_params() #endif // USE_SVMLIGHT } -bool CScatterSVM::train_machine(CFeatures* data) +bool ScatterSVM::train_machine(std::shared_ptr data) { ASSERT(m_labels && m_labels->get_num_labels()) ASSERT(m_labels->get_label_type() == LT_MULTICLASS) @@ -86,8 +86,9 @@ bool CScatterSVM::train_machine(CFeatures* data) int32_t* numc=SG_MALLOC(int32_t, m_num_classes); SGVector::fill_vector(numc, m_num_classes, 0); + auto mc = multiclass_labels(m_labels); for (int32_t i=0; iget_int_label(i)]++; + numc[(int32_t) mc->get_int_label(i)]++; int32_t Nc=0; int32_t Nmin=num_vectors; @@ -96,7 +97,7 @@ bool CScatterSVM::train_machine(CFeatures* data) if (numc[i]>0) { Nc++; - Nmin=CMath::min(Nmin, numc[i]); + Nmin=Math::min(Nmin, numc[i]); } } @@ -133,7 +134,7 @@ bool CScatterSVM::train_machine(CFeatures* data) return result; } -bool CScatterSVM::train_no_bias_libsvm() +bool ScatterSVM::train_no_bias_libsvm() { svm_problem problem; svm_parameter param; @@ -168,10 +169,10 @@ bool CScatterSVM::train_no_bias_libsvm() param.gamma = 0; // 1/k param.coef0 = 0; param.nu = get_nu(); // Nu - CKernelNormalizer* prev_normalizer=m_kernel->get_normalizer(); - m_kernel->set_normalizer(new CScatterKernelNormalizer( + auto prev_normalizer=m_kernel->get_normalizer(); + m_kernel->set_normalizer(std::make_shared( m_num_classes-1, -1, m_labels, prev_normalizer)); - param.kernel=m_kernel; + param.kernel=m_kernel.get(); param.cache_size = m_kernel->get_cache_size(); param.C = 0; param.eps = get_epsilon(); @@ -190,7 +191,6 @@ bool CScatterSVM::train_no_bias_libsvm() model = svm_train(&problem, ¶m); m_kernel->set_normalizer(prev_normalizer); - SG_UNREF(prev_normalizer); if (model) { @@ -202,14 +202,14 @@ bool CScatterSVM::train_no_bias_libsvm() rho=model->rho[0]; SG_FREE(norm_wcw); - norm_wcw = SG_MALLOC(float64_t, m_machines->get_num_elements()); - norm_wcw_len = m_machines->get_num_elements(); + norm_wcw_len = m_machines.size(); + norm_wcw = SG_MALLOC(float64_t, norm_wcw_len); for (int32_t i=0; inSV[i]; - CSVM* svm=new CSVM(num_sv); + auto svm=std::make_shared(num_sv); svm->set_bias(model->rho[i+1]); norm_wcw[i]=model->normwcw[i]; @@ -244,15 +244,15 @@ bool CScatterSVM::train_no_bias_libsvm() } #ifdef USE_SVMLIGHT -bool CScatterSVM::train_no_bias_svmlight() +bool ScatterSVM::train_no_bias_svmlight() { - CKernelNormalizer* prev_normalizer=m_kernel->get_normalizer(); - CScatterKernelNormalizer* n=new CScatterKernelNormalizer( + auto prev_normalizer=m_kernel->get_normalizer(); + auto n=std::make_shared( m_num_classes-1, -1, m_labels, prev_normalizer); m_kernel->set_normalizer(n); m_kernel->init_normalizer(); - CSVMLightOneClass* light=new CSVMLightOneClass(get_C(), m_kernel); + auto light=std::make_shared(get_C(), m_kernel); light->set_linadd_enabled(false); light->train(); @@ -274,7 +274,7 @@ bool CScatterSVM::train_no_bias_svmlight() } #endif //USE_SVMLIGHT -bool CScatterSVM::train_testrule12() +bool ScatterSVM::train_testrule12() { svm_problem problem; svm_parameter param; @@ -288,9 +288,10 @@ bool CScatterSVM::train_testrule12() problem.x=SG_MALLOC(struct svm_node*, problem.l); x_space=SG_MALLOC(struct svm_node, 2*problem.l); + auto mc = multiclass_labels(m_labels); for (int32_t i=0; iget_label(i); + problem.y[i]=mc->get_label(i); problem.x[i]=&x_space[2*i]; x_space[2*i].index=i; x_space[2*i+1].index=-1; @@ -308,7 +309,7 @@ bool CScatterSVM::train_testrule12() param.gamma = 0; // 1/k param.coef0 = 0; param.nu = get_nu(); // Nu - param.kernel=m_kernel; + param.kernel=m_kernel.get(); param.cache_size = m_kernel->get_cache_size(); param.C = 0; param.eps = get_epsilon(); @@ -337,14 +338,14 @@ bool CScatterSVM::train_testrule12() rho=model->rho[0]; SG_FREE(norm_wcw); - norm_wcw = SG_MALLOC(float64_t, m_machines->get_num_elements()); - norm_wcw_len = m_machines->get_num_elements(); + norm_wcw_len = m_machines.size(); + norm_wcw = SG_MALLOC(float64_t, norm_wcw_len); for (int32_t i=0; inSV[i]; - CSVM* svm=new CSVM(num_sv); + auto svm=std::make_shared(num_sv); svm->set_bias(model->rho[i+1]); norm_wcw[i]=model->normwcw[i]; @@ -378,18 +379,14 @@ bool CScatterSVM::train_testrule12() return false; } -void CScatterSVM::compute_norm_wc() +void ScatterSVM::compute_norm_wc() { SG_FREE(norm_wc); - norm_wc = SG_MALLOC(float64_t, m_machines->get_num_elements()); - norm_wc_len = m_machines->get_num_elements(); - for (int32_t i=0; iget_num_elements(); i++) - norm_wc[i]=0; - - - for (int c=0; cget_num_elements(); c++) + norm_wc_len = m_machines.size(); + norm_wc = SG_CALLOC(float64_t, norm_wc_len); + for (size_t c=0; cget_num_support_vectors(); for (int32_t i=0; iget_num_elements(); i++) + for (size_t i=0; i::display_vector(norm_wc, m_machines->get_num_elements(), "norm_wc"); } -CLabels* CScatterSVM::classify_one_vs_rest() +std::shared_ptr ScatterSVM::classify_one_vs_rest() { - CMulticlassLabels* output=NULL; if (!m_kernel) { error("SVM can not proceed without kernel!"); @@ -423,12 +417,12 @@ CLabels* CScatterSVM::classify_one_vs_rest() int32_t num_vectors=m_kernel->get_num_vec_rhs(); - output=new CMulticlassLabels(num_vectors); - SG_REF(output); + auto output=std::make_shared(num_vectors); + if (scatter_type == TEST_RULE1) { - ASSERT(m_machines->get_num_elements()>0) + ASSERT(!m_machines.empty()) for (int32_t i=0; iset_label(i, apply_one(i)); } @@ -438,12 +432,13 @@ CLabels* CScatterSVM::classify_one_vs_rest() float64_t* outputs=SG_MALLOC(float64_t, num_vectors*m_num_classes); SGVector::fill_vector(outputs,num_vectors*m_num_classes,0.0); + auto mc = multiclass_labels(m_labels); for (int32_t i=0; iget_num_support_vectors(); j++) { float64_t score=m_kernel->kernel(svm_proto()->get_support_vector(j), i)*svm_proto()->get_alpha(j); - int32_t label=((CMulticlassLabels*) m_labels)->get_int_label(svm_proto()->get_support_vector(j)); + int32_t label=mc->get_int_label(svm_proto()->get_support_vector(j)); for (int32_t c=0; cget_num_elements()>0) + ASSERT(!m_machines.empty()) ASSERT(num_vectors==output->get_num_labels()) - CLabels** outputs=SG_MALLOC(CLabels*, m_machines->get_num_elements()); + std::vector> outputs(m_machines.size()); - for (int32_t i=0; iget_num_elements(); i++) + for (size_t i=0; iset_kernel(m_kernel); svm->set_labels(m_labels); outputs[i]=svm->apply(); - SG_UNREF(svm); + } for (int32_t i=0; iget_label(i)/norm_wc[0]; + float64_t max_out=outputs[0]->as()->get_label(i)/norm_wc[0]; - for (int32_t j=1; jget_num_elements(); j++) + for (size_t j=1; jget_label(i)/norm_wc[j]; + float64_t out=outputs[j]->as()->get_label(i)/norm_wc[j]; if (out>max_out) { @@ -510,27 +505,23 @@ CLabels* CScatterSVM::classify_one_vs_rest() output->set_label(i, winner); } - for (int32_t i=0; iget_num_elements(); i++) - SG_UNREF(outputs[i]); - - SG_FREE(outputs); } return output; } -float64_t CScatterSVM::apply_one(int32_t num) +float64_t ScatterSVM::apply_one(int32_t num) { - ASSERT(m_machines->get_num_elements()>0) - float64_t* outputs=SG_MALLOC(float64_t, m_machines->get_num_elements()); + ASSERT(!m_machines.empty()) + float64_t* outputs=SG_MALLOC(float64_t, m_machines.size()); int32_t winner=0; if (scatter_type == TEST_RULE1) { - for (int32_t c=0; cget_num_elements(); c++) + for (size_t c=0; cget_bias()-rho; - for (int32_t c=0; cget_num_elements(); c++) + for (size_t c=0; cget_num_elements(); j++) - outputs[j] -= v/m_machines->get_num_elements(); + for (size_t j=0; jget_num_elements(); j++) + for (size_t j=0; jget_num_elements(); j++) + for (size_t j=0; jmax_out) { @@ -569,7 +560,7 @@ float64_t CScatterSVM::apply_one(int32_t num) { float64_t max_out=get_svm(0)->apply_one(num)/norm_wc[0]; - for (int32_t i=1; iget_num_elements(); i++) + for (size_t i=1; iapply_one(num)/norm_wc[i]; if (outputs[i]>max_out) diff --git a/src/shogun/multiclass/ScatterSVM.h b/src/shogun/multiclass/ScatterSVM.h index 1358d4f12f4..ae837cb7a67 100644 --- a/src/shogun/multiclass/ScatterSVM.h +++ b/src/shogun/multiclass/ScatterSVM.h @@ -45,14 +45,14 @@ namespace shogun * TU Berlin, 2009 * * */ -class CScatterSVM : public CMulticlassSVM +class ScatterSVM : public MulticlassSVM { public: /** default constructor */ - CScatterSVM(); + ScatterSVM(); /** constructor */ - CScatterSVM(SCATTER_TYPE type); + ScatterSVM(SCATTER_TYPE type); /** constructor (using NO_BIAS as default scatter_type) * @@ -60,10 +60,10 @@ class CScatterSVM : public CMulticlassSVM * @param k kernel * @param lab labels */ - CScatterSVM(float64_t C, CKernel* k, CLabels* lab); + ScatterSVM(float64_t C, std::shared_ptr k, std::shared_ptr lab); /** default destructor */ - virtual ~CScatterSVM(); + virtual ~ScatterSVM(); /** get classifier type * @@ -82,7 +82,7 @@ class CScatterSVM : public CMulticlassSVM * * @return resulting labels */ - virtual CLabels* classify_one_vs_rest(); + virtual std::shared_ptr classify_one_vs_rest(); /** @return object name */ virtual const char* get_name() const { return "ScatterSVM"; } @@ -96,7 +96,7 @@ class CScatterSVM : public CMulticlassSVM * * @return whether training was successful */ - virtual bool train_machine(CFeatures* data=NULL); + virtual bool train_machine(std::shared_ptr data=NULL); private: void compute_norm_wc(); diff --git a/src/shogun/multiclass/ShareBoost.cpp b/src/shogun/multiclass/ShareBoost.cpp index 9c49b1f6041..859f09cca77 100644 --- a/src/shogun/multiclass/ShareBoost.cpp +++ b/src/shogun/multiclass/ShareBoost.cpp @@ -17,34 +17,34 @@ using namespace shogun; -CShareBoost::CShareBoost() - :CLinearMulticlassMachine(), m_nonzero_feas(0) +ShareBoost::ShareBoost() + :LinearMulticlassMachine(), m_nonzero_feas(0) { init_sb_params(); } -CShareBoost::CShareBoost(CDenseFeatures *features, CMulticlassLabels *labs, int32_t num_nonzero_feas) - :CLinearMulticlassMachine(new CMulticlassOneVsRestStrategy(), features, NULL, labs), m_nonzero_feas(num_nonzero_feas) +ShareBoost::ShareBoost(std::shared_ptr >features, std::shared_ptrlabs, int32_t num_nonzero_feas) + :LinearMulticlassMachine(std::make_shared(), features, NULL, labs), m_nonzero_feas(num_nonzero_feas) { init_sb_params(); } -void CShareBoost::init_sb_params() +void ShareBoost::init_sb_params() { SG_ADD(&m_nonzero_feas, "nonzero_feas", "Number of non-zero features"); SG_ADD(&m_activeset, "active_set", "Selected features"); } -SGVector CShareBoost::get_activeset() +SGVector ShareBoost::get_activeset() { return m_activeset; } -bool CShareBoost::train_machine(CFeatures* data) +bool ShareBoost::train_machine(std::shared_ptr data) { if (data) set_features(data); - CDenseFeatures *fea = dynamic_cast*>(m_features); + auto fea = m_features->as>(); if (m_features == NULL) error("No features given for training"); @@ -68,11 +68,11 @@ bool CShareBoost::train_machine(CFeatures* data) m_activeset = SGVector(m_fea.num_rows); m_activeset.vlen = 0; - m_machines->reset_array(); + m_machines.clear(); for (int32_t i=0; i < m_multiclass_strategy->get_num_classes(); ++i) - m_machines->push_back(new CLinearMachine()); + m_machines.push_back(std::make_shared()); - CTime *timer = new CTime(); + auto timer = std::make_shared