Skip to content
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

[eigen] use more eigen #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ if(UNIX OR MINGW OR CYGWIN)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
endif()

MESSAGE( STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS} )

add_subdirectory(src)


Expand Down
4 changes: 2 additions & 2 deletions src/dataset.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace microgbt {

using Vector = std::vector<double>;
using Vector = Eigen::RowVectorXf;
using VectorT = std::vector<size_t>;
using MatrixType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
using SortedMatrixType = Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
Expand Down Expand Up @@ -136,7 +136,7 @@ namespace microgbt {

inline Vector y() const {
Vector proj(_rowIndices.size());
for (size_t i = 0; i < proj.size(); i++) {
for (long i = 0; i < proj.size(); i++) {
proj[i] = (*_y)[_rowIndices[i]];
}
return proj;
Expand Down
2 changes: 0 additions & 2 deletions src/metrics/logloss.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace microgbt {

using Vector = std::vector<double>;

/**
* Log loss metric
*
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace microgbt {

using Vector = std::vector<double>;
using Vector = Eigen::RowVectorXf;

/**
* A generic metric that is defined by:
Expand Down
16 changes: 4 additions & 12 deletions src/metrics/rmse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,27 @@

namespace microgbt {

using Vector = std::vector<double>;

class RMSE: public Metric {

public:

RMSE() = default;

Vector gradients(const Vector &predictions, const Vector &labels) const override {
Vector grads(predictions.size());

for (size_t i = 0; i < predictions.size(); i++) {
grads[i] = 2 * (predictions[i] - labels[i]);
}

return grads;
return 2 * (predictions - labels);
}

Vector hessian(const Vector &predictions) const override {
// Hessian is constant vector 2.0
return Vector(predictions.size(), 2.0);
return Eigen::VectorXf::Constant(predictions.size(), 2.0);

}

double lossAt(const Vector &predictions, const Vector &labels) const override {
long double loss = 0.0;

size_t n = predictions.size();
for (size_t i = 0; i< n; i ++){
long n = predictions.size();
for (long i = 0; i< n; i ++){
loss += pow(labels[i] - predictions[i], 2.0);
}

Expand Down
21 changes: 12 additions & 9 deletions src/trees/numerical_splliter.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ namespace microgbt {
const Vector &hessian,
int featureId) const {

long n = static_cast<long>(dataset.nRows());

// Sort the feature by value and return permutation of indices (i.e., argsort)
const Eigen::RowVectorXi& sortedInstanceIds = dataset.sortedColumnIndices(featureId);

// Cummulative sum of gradients and Hessian
Vector cum_sum_G(dataset.nRows()), cum_sum_H(dataset.nRows());
Vector cum_sum_G(n), cum_sum_H(n);
double cum_sum_g = 0.0, cum_sum_h = 0.0;
for (size_t i = 0 ; i < dataset.nRows(); i++) {
for (long i = 0 ; i < n; i++) {
size_t idx = sortedInstanceIds[i];
cum_sum_g += gradient[idx];
cum_sum_h += hessian[idx];
Expand All @@ -68,15 +70,16 @@ namespace microgbt {
}

// For each feature, compute split gain and keep the split index with maximum gain
Vector gainPerOrderedSampleIndex(dataset.nRows());
for (size_t i = 0 ; i < dataset.nRows(); i++){
gainPerOrderedSampleIndex[i] = calc_split_gain(cum_sum_g, cum_sum_h, cum_sum_G[i], cum_sum_H[i]);
double bestGain = 0.0;
long bestGainIndex = 0;
for (long i = 0 ; i < n; i++){
double currentGain = calc_split_gain(cum_sum_g, cum_sum_h, cum_sum_G[i], cum_sum_H[i]);
if (bestGain < currentGain) {
bestGain = currentGain;
bestGainIndex = i;
}
}

long bestGainIndex =
std::max_element(gainPerOrderedSampleIndex.begin(), gainPerOrderedSampleIndex.end())
- gainPerOrderedSampleIndex.begin();
double bestGain = gainPerOrderedSampleIndex[bestGainIndex];
double bestSplitNumericValue = dataset.row(sortedInstanceIds[bestGainIndex])[featureId];
size_t bestSortedIndex = bestGainIndex + 1;

Expand Down
12 changes: 5 additions & 7 deletions src/trees/split_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace microgbt {

using VectorD = std::vector<double>;
using VectorD = Eigen::RowVectorXf;
using VectorT = std::vector<size_t>;

/**
Expand Down Expand Up @@ -92,12 +92,10 @@ namespace microgbt {
rowIndices = getRightLocalIds();
}

VectorD splitVector;
std::transform(rowIndices.begin(), rowIndices.end(),
std::back_inserter(splitVector),
[&vector](size_t rowIndex){
return vector[rowIndex];
});
VectorD splitVector(rowIndices.size());
for( size_t i = 0 ; i < rowIndices.size(); i++) {
splitVector[i] = vector[rowIndices[i]];
}

return splitVector;
}
Expand Down
3 changes: 1 addition & 2 deletions src/trees/treenode.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ namespace microgbt {
*/
inline double calc_leaf_weight(const Vector &gradient,
const Vector &hessian) const {
return - std::accumulate(gradient.begin(), gradient.end(), 0.0)
/ (std::accumulate(hessian.begin(), hessian.end(), 0.0) + _lambda);
return - gradient.sum() / (hessian.sum() + _lambda);
}


Expand Down
6 changes: 4 additions & 2 deletions test/test_dataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ TEST(Dataset, DefaultConstructor)

size_t m = 2, n = 3;
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(m, n);
microgbt::Vector y = {1.0, 2.0};
microgbt::Vector y(2);
y << 1.0, 2.0;
microgbt::Dataset dataset(A, y);

ASSERT_EQ(dataset.nRows(), m);
Expand All @@ -18,7 +19,8 @@ TEST(Dataset, Constructor)

size_t m = 2, n = 3;
Eigen::MatrixXd A = Eigen::MatrixXd::Zero(m, n);
microgbt::Vector y = {1.0, 2.0};
microgbt::Vector y(2);
y << 1.0, 2.0;
microgbt::Dataset dataset(A, y);

std::vector<size_t> left = {0};
Expand Down
18 changes: 5 additions & 13 deletions test/test_metric_logloss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ TEST(LogLoss, LogLossClipLowerUnderFlow)
TEST(LogLoss, LogLossGradient)
{
LogLoss logloss;
Vector preds = Vector(10);
Vector targets = Vector(10);

std::fill(preds.begin(), preds.end(), 100.0);
std::fill(targets.begin(), targets.end(), 99.0);
Vector preds = Vector::Constant(10, 100.0);
Vector targets = Vector::Constant(10, 99.0);

Vector grads = logloss.gradients(preds, targets);
ASSERT_EQ(grads.size(), preds.size());
Expand All @@ -57,9 +54,7 @@ TEST(LogLoss, LogLossGradient)
TEST(LogLoss, LogLossHessian)
{
LogLoss logloss;
Vector preds = Vector(10);

std::fill(preds.begin(), preds.end(), 0.5);
Vector preds = Vector::Contant(10, 0.5);

Vector hessian = logloss.hessian(preds);
ASSERT_EQ(hessian.size(), preds.size());
Expand All @@ -69,11 +64,8 @@ TEST(LogLoss, LogLossHessian)
TEST(LogLoss, LogLossLossAtMustBeZero)
{
LogLoss logloss;
Vector preds = Vector(10);
Vector targets = Vector(10);

std::fill(preds.begin(), preds.end(), 1.0);
std::fill(targets.begin(), targets.end(), 1.0);
Vector preds = Vector::Constant(10, 1.0);
Vector targets = Vector::Constant(10, 1.0);

double loss = logloss.lossAt(preds, targets);
ASSERT_NEAR(loss, 0, 1.0e-7);
Expand Down
31 changes: 9 additions & 22 deletions test/test_metric_rmse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ TEST(microgbt, LogLossClipLowerUnderFlow)
TEST(microgbt, LogLossGradient)
{
LogLoss logloss;
Vector preds = Vector(10);
Vector targets = Vector(10);

std::fill(preds.begin(), preds.end(), 100.0);
std::fill(targets.begin(), targets.end(), 99.0);
Vector preds = Vector::Constant(10, 100.0);
Vector targets = Vector::Constant(10, 99.0);

Vector grads = logloss.gradients(preds, targets);
ASSERT_EQ(grads.size(), preds.size());
Expand All @@ -57,9 +54,7 @@ TEST(microgbt, LogLossGradient)
TEST(microgbt, LogLossHessian)
{
LogLoss logloss;
Vector preds = Vector(10);

std::fill(preds.begin(), preds.end(), 0.5);
Vector preds = Vector::Constant(10, 0.5);

Vector hessian = logloss.hessian(preds);
ASSERT_EQ(hessian.size(), preds.size());
Expand All @@ -69,11 +64,8 @@ TEST(microgbt, LogLossHessian)
TEST(microgbt, LogLossLossAtMustBeZero)
{
LogLoss logloss;
Vector preds = Vector(10);
Vector targets = Vector(10);

std::fill(preds.begin(), preds.end(), 1.0);
std::fill(targets.begin(), targets.end(), 1.0);
Vector preds = Vector::Constant(10, 1.0);
Vector targets = Vector::Constant(10, 1.0);

double loss = logloss.lossAt(preds, targets);
ASSERT_NEAR(loss, 0, 1.0e-7);
Expand All @@ -100,11 +92,8 @@ TEST(microgbt, RMSEHessian)
TEST(microgbt, RMSEGradient)
{
RMSE rmse;
Vector preds = Vector(10);
Vector targets = Vector(10);

std::fill(preds.begin(), preds.end(), 100.0);
std::fill(targets.begin(), targets.end(), 99.0);
Vector preds = Vector::Constant(10, 100.0);
Vector targets = Vector::Constant(10, 99.0);

Vector grads = rmse.gradients(preds, targets);
ASSERT_EQ(grads.size(), preds.size());
Expand All @@ -114,11 +103,9 @@ TEST(microgbt, RMSEGradient)
TEST(microgbt, RMSELossAtMustBeZero)
{
RMSE rmse;
Vector preds = Vector(10);
Vector targets = Vector(10);

std::fill(preds.begin(), preds.end(), 1.0);
std::fill(targets.begin(), targets.end(), 1.0);
Vector preds = Vector::Constant(10, 1.0);
Vector targets = Vector::Constant(10, 1.0);

double loss = rmse.lossAt(preds, targets);
ASSERT_NEAR(loss, 0, 1.0e-7);
Expand Down