-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
replaced SGVector::scale() with linalg::scale() #2928
Changes from 7 commits
e70c99d
0e1a8db
40eac07
a6c2269
d51b2e8
fc0e3f5
0b250de
e551596
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <shogun/lib/SGVector.h> | ||
#include <shogun/lib/SGSparseVector.h> | ||
#include <shogun/mathematics/linalg/linalg.h> | ||
#include <algorithm> | ||
#include <hayai/hayai.hpp> | ||
#include <iostream> | ||
|
||
using namespace shogun; | ||
|
||
/** | ||
* Instructions : | ||
* 1. Install benchmarking toolkit "hayai" (https://github.com/nickbruun/hayai) | ||
* 2. Compile against libhayai_main, e.g. | ||
* g++ -O3 -std=c++11 sgvector_add_operator_benchmark.cpp -I/usr/include/eigen3 -I/usr/local/include/viennacl -lshogun -lhayai_main -lOpenCL -o benchmark | ||
* 3. ./benchmark | ||
*/ | ||
|
||
/** Generate data only once */ | ||
struct Data | ||
{ | ||
Data() | ||
{ | ||
init(); | ||
} | ||
|
||
void init() | ||
{ | ||
m_vec = SGVector<float32_t>(num_elems); | ||
std::iota(m_vec.data(), m_vec.data()+m_vec.size(), 1); | ||
} | ||
|
||
SGVector<float32_t> m_vec; | ||
static constexpr index_t num_elems=1000; | ||
}; | ||
|
||
Data data; | ||
|
||
|
||
BENCHMARK(SGVector, addoperator_SGVector, 10, 100000000) | ||
{ | ||
SGVector<float32_t> test_vec = SGVector<float32_t>(data.num_elems); | ||
std::iota(test_vec.data(), test_vec.data()+test_vec.size(), 1); | ||
data.m_vec += test_vec; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
#include <shogun/ensemble/MeanRule.h> | ||
#include <shogun/lib/SGVector.h> | ||
#include <shogun/lib/SGMatrix.h> | ||
#include <shogun/mathematics/linalg/linalg.h> | ||
|
||
using namespace shogun; | ||
|
||
|
@@ -35,7 +36,7 @@ SGVector<float64_t> CMeanRule::combine(const SGMatrix<float64_t>& ensemble_resul | |
SGVector<float64_t> mean_labels(row_sum, ensemble_result.num_rows); | ||
|
||
float64_t scale = 1/(float64_t)ensemble_result.num_cols; | ||
mean_labels.scale(scale); | ||
linalg::scale<linalg::Backend::NATIVE>(mean_labels, scale); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you have already written There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
|
||
return mean_labels; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
#include <algorithm> | ||
|
||
#include <shogun/mathematics/eigen3.h> | ||
#include <shogun/mathematics/linalg/linalg.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to be guarded |
||
|
||
#define COMPLEX128_ERROR_NOARG(function) \ | ||
template <> \ | ||
|
@@ -277,6 +278,15 @@ SGVector<T> SGVector<T>::operator+ (SGVector<T> x) | |
return result; | ||
} | ||
|
||
|
||
template<class T> | ||
SGVector<T> SGVector<T>::operator+= (SGVector<T> x) | ||
{ | ||
linalg::add<linalg::Backend::NATIVE>(*this, x, *this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the a reason for NATIVE here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return *this; | ||
} | ||
|
||
|
||
template<class T> | ||
void SGVector<T>::add(const SGVector<T> x) | ||
{ | ||
|
@@ -839,12 +849,6 @@ void SGVector<float32_t>::scale_vector(float32_t alpha, float32_t* vec, int32_t | |
} | ||
#endif | ||
|
||
template<class T> | ||
void SGVector<T>::scale(T alpha) | ||
{ | ||
scale_vector(alpha, vector, vlen); | ||
} | ||
|
||
template<class T> void SGVector<T>::load(CFile* loader) | ||
{ | ||
REQUIRE(loader, "Require a valid 'c FILE pointer'\n"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,9 +42,11 @@ | |
#include <shogun/lib/DynamicArray.h> | ||
|
||
#include <shogun/mathematics/eigen3.h> | ||
#include <shogun/mathematics/linalg/linalg.h> | ||
|
||
using namespace shogun; | ||
using namespace Eigen; | ||
using namespace linalg; | ||
|
||
// try to use previously allocated memory for SGVector | ||
#define CREATE_SGVECTOR(vec, len, sg_type) \ | ||
|
@@ -179,7 +181,7 @@ void CEPInferenceMethod::update() | |
|
||
// get and scale diagonal of the kernel matrix | ||
SGVector<float64_t> ktrtr_diag=m_ktrtr.get_diagonal_vector(); | ||
ktrtr_diag.scale(CMath::exp(m_log_scale*2.0)); | ||
scale<linalg::Backend::NATIVE>(ktrtr_diag, CMath::exp(m_log_scale*2.0)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be easuer to read if we overloaded the *= operator of SGVector There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @karlnapf you mean this overload has to be like in |
||
|
||
// marginal likelihood for ttau = tnu = 0 | ||
float64_t nlZ0=-SGVector<float64_t>::sum(m_model->get_log_zeroth_moments( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,8 +37,10 @@ | |
#include <shogun/lib/external/brent.h> | ||
#include <shogun/mathematics/eigen3.h> | ||
#include <shogun/features/DotFeatures.h> | ||
#include <shogun/mathematics/linalg/linalg.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. guard, also below |
||
|
||
using namespace shogun; | ||
using namespace linalg; | ||
using namespace Eigen; | ||
|
||
namespace shogun | ||
|
@@ -80,8 +82,7 @@ class CFITCPsiLine : public func_base | |
(*dlp)=lik->get_log_probability_derivative_f(lab, (*f), 1); | ||
|
||
(*W)=lik->get_log_probability_derivative_f(lab, (*f), 2); | ||
W->scale(-1.0); | ||
|
||
scale<linalg::Backend::NATIVE>(*W, -1.0); | ||
// compute psi=alpha'*(f-m)/2-lp | ||
float64_t result = eigen_alpha.dot(eigen_f-eigen_m)/2.0- | ||
SGVector<float64_t>::sum(lik->get_log_probability_f(lab, *f)); | ||
|
@@ -360,7 +361,7 @@ void CSingleFITCLaplacianInferenceMethod::update_alpha() | |
|
||
// compute W = -d2lp | ||
m_W=m_model->get_log_probability_derivative_f(m_labels, m_mu, 2); | ||
m_W.scale(-1.0); | ||
scale<linalg::Backend::NATIVE>(m_W, -1.0); | ||
|
||
//n-by-1 vector | ||
Map<VectorXd> eigen_al(m_al.vector, m_al.vlen); | ||
|
@@ -469,7 +470,7 @@ void CSingleFITCLaplacianInferenceMethod::update_chol() | |
|
||
// W = -d2lp | ||
m_W=m_d2lp.clone(); | ||
m_W.scale(-1.0); | ||
scale<linalg::Backend::NATIVE>(m_W, -1.0); | ||
|
||
Map<VectorXd> eigen_W(m_W.vector, m_W.vlen); | ||
m_sW=SGVector<float64_t>(m_W.vlen); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,10 @@ | |
#include <shogun/mathematics/Mosek.h> | ||
#include <shogun/lib/SGSparseVector.h> | ||
#include <shogun/mathematics/Math.h> | ||
#include <shogun/mathematics/linalg/linalg.h> | ||
|
||
using namespace shogun; | ||
using namespace linalg; | ||
|
||
CCCSOSVM::CCCSOSVM() | ||
: CLinearStructuredOutputMachine() | ||
|
@@ -537,9 +539,9 @@ SGSparseVector<float64_t> CCCSOSVM::find_cutting_plane(float64_t* margin) | |
CResultSet* result = m_model->argmax(m_w, i); | ||
if (result->psi_computed) | ||
{ | ||
new_constraint.add(result->psi_truth); | ||
result->psi_pred.scale(-1.0); | ||
new_constraint.add(result->psi_pred); | ||
add<linalg::Backend::NATIVE>(new_constraint, result->psi_truth, new_constraint); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here we have the + operator, why not use it? |
||
scale<linalg::Backend::NATIVE>(result->psi_pred, -1.0); | ||
add<linalg::Backend::NATIVE>(new_constraint, result->psi_pred, new_constraint); | ||
} | ||
else if(result->psi_computed_sparse) | ||
{ | ||
|
@@ -563,7 +565,7 @@ SGSparseVector<float64_t> CCCSOSVM::find_cutting_plane(float64_t* margin) | |
} | ||
/* scaling */ | ||
float64_t scale = 1/(float64_t)num_samples; | ||
new_constraint.scale(scale); | ||
linalg::scale<linalg::Backend::NATIVE>(new_constraint, scale); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't remove |
||
*margin *= scale; | ||
|
||
/* find the nnz elements in new_constraint */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vigsterkr whats your suggestion here?
Ignore linalg, make a double implementation, or make this class unavailable for non c++11 ?