-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add class BitDecomposableStatisticVector.
- Loading branch information
1 parent
457e7db
commit 1171d48
Showing
4 changed files
with
385 additions
and
4 deletions.
There are no files selected for viewing
183 changes: 183 additions & 0 deletions
183
cpp/subprojects/boosting/include/mlrl/boosting/data/vector_statistic_decomposable_bit.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* @author Michael Rapp ([email protected]) | ||
*/ | ||
#pragma once | ||
|
||
#include "mlrl/boosting/data/view_statistic_decomposable_bit.hpp" | ||
#include "mlrl/common/data/view_composite.hpp" | ||
#include "mlrl/common/indices/index_vector_complete.hpp" | ||
#include "mlrl/common/indices/index_vector_partial.hpp" | ||
|
||
namespace boosting { | ||
|
||
/** | ||
* An one-dimensional vector that stores aggregated gradients and Hessians that have been calculated using a | ||
* decomposable loss function in a bit vector. For each element in the vector a single gradient and Hessian is | ||
* stored. | ||
*/ | ||
class BitDecomposableStatisticVector final | ||
: public CompositeView<AllocatedBitVector<uint32>, AllocatedBitVector<uint32>> { | ||
public: | ||
|
||
/** | ||
* @param numElements The number of gradients and Hessians in the vector | ||
* @param numBitsPerElement The number of bits per element in the bit vector | ||
* @param init True, if all gradients and Hessians in the vector should be initialized with | ||
* zero, false otherwise | ||
*/ | ||
BitDecomposableStatisticVector(uint32 numElements, uint32 numBitsPerElement, bool init = false); | ||
|
||
/** | ||
* @param other A reference to an object of type `BitDecomposableStatisticVector` to be copied | ||
*/ | ||
BitDecomposableStatisticVector(const BitDecomposableStatisticVector& other); | ||
|
||
/** | ||
* Returns the number of gradients and Hessians in the vector. | ||
* | ||
* @return The number of gradients and Hessians | ||
*/ | ||
uint32 getNumElements() const; | ||
|
||
/** | ||
* Returns the number of bits per gradient or Hessian in the vector. | ||
* | ||
* @return The number of bits per gradient or Hessian | ||
*/ | ||
uint32 getNumBitsPerElement() const; | ||
|
||
/** | ||
* Adds all gradients and Hessians in another vector to this vector. | ||
* | ||
* @param vector A reference to an object of type `BitDecomposableStatisticVector` that stores the gradients | ||
* and Hessians to be added to this vector | ||
*/ | ||
void add(const BitDecomposableStatisticVector& vector); | ||
|
||
/** | ||
* Adds all gradients and Hessians in a single row of a `BitDecomposableStatisticView` to this vector. | ||
* | ||
* @param view A reference to an object of type `BitDecomposableStatisticView` that stores the gradients | ||
* and Hessians to be added to this vector | ||
* @param row The index of the row to be added to this vector | ||
*/ | ||
void add(const BitDecomposableStatisticView& view, uint32 row); | ||
|
||
/** | ||
* Adds all gradients and Hessians in a single row of a `CContiguousView` to this vector. The gradients and | ||
* Hessians to be added are multiplied by a specific weight. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and | ||
* Hessians to be added to this vector | ||
* @param row The index of the row to be added to this vector | ||
* @param weight The weight, the gradients and Hessians should be multiplied by | ||
*/ | ||
void add(const BitDecomposableStatisticView& view, uint32 row, float64 weight); | ||
|
||
/** | ||
* Removes all gradients and Hessians in a single row of a `CContiguousView` from this vector. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and Hessians to | ||
* be removed from this vector | ||
* @param row The index of the row to be removed from this vector | ||
*/ | ||
void remove(const BitDecomposableStatisticView& view, uint32 row); | ||
|
||
/** | ||
* Removes all gradients and Hessians in a single row of a `CContiguousView` from this vector. The gradients | ||
* and Hessians to be removed are multiplied by a specific weight. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and | ||
* Hessians to be removed from this vector | ||
* @param row The index of the row to be removed from this vector | ||
* @param weight The weight, the gradients and Hessians should be multiplied by | ||
*/ | ||
void remove(const BitDecomposableStatisticView& view, uint32 row, float64 weight); | ||
|
||
/** | ||
* Adds certain gradients and Hessians in a single row of a `CContiguousView`, whose positions are given as | ||
* a `CompleteIndexVector`, to this vector. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and | ||
* Hessians to be added to this vector | ||
* @param row The index of the row to be added to this vector | ||
* @param indices A reference to a `CompleteIndexVector' that provides access to the indices | ||
*/ | ||
void addToSubset(const BitDecomposableStatisticView& view, uint32 row, const CompleteIndexVector& indices); | ||
|
||
/** | ||
* Adds certain gradients and Hessians in single row of a `CContiguousView`, whose positions are given as a | ||
* `PartialIndexVector`, to this vector. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and | ||
* Hessians to be added to this vector | ||
* @param row The index of the row to be added to this vector | ||
* @param indices A reference to a `PartialIndexVector' that provides access to the indices | ||
*/ | ||
void addToSubset(const BitDecomposableStatisticView& view, uint32 row, const PartialIndexVector& indices); | ||
|
||
/** | ||
* Adds certain gradients and Hessians in a single row of a `CContiguousView`, whose positions are given as | ||
* a `CompleteIndexVector`, to this vector. The gradients and Hessians to be added are multiplied by a | ||
* specific weight. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and | ||
* Hessians to be added to this vector | ||
* @param row The index of the row to be added to this vector | ||
* @param indices A reference to a `CompleteIndexVector' that provides access to the indices | ||
* @param weight The weight, the gradients and Hessians should be multiplied by | ||
*/ | ||
void addToSubset(const BitDecomposableStatisticView& view, uint32 row, const CompleteIndexVector& indices, | ||
float64 weight); | ||
|
||
/** | ||
* Adds certain gradients and Hessians in single row of a `CContiguousView`, whose positions are given as a | ||
* `PartialIndexVector`, to this vector. The gradients and Hessians to be added are multiplied by a specific | ||
* weight. | ||
* | ||
* @param view A reference to an object of type `CContiguousView` that stores the gradients and | ||
* Hessians to be added to this vector | ||
* @param row The index of the row to be added to this vector | ||
* @param indices A reference to a `PartialIndexVector' that provides access to the indices | ||
* @param weight The weight, the gradients and Hessians should be multiplied by | ||
*/ | ||
void addToSubset(const BitDecomposableStatisticView& view, uint32 row, const PartialIndexVector& indices, | ||
float64 weight); | ||
|
||
/** | ||
* Sets the gradients and Hessians in this vector to the difference `first - second` between the gradients | ||
* and Hessians in two other vectors, considering only the gradients and Hessians in the first vector that | ||
* correspond to the positions provided by a `CompleteIndexVector`. | ||
* | ||
* @param first A reference to an object of type `BitDecomposableStatisticVector` that stores the | ||
* gradients and Hessians in the first vector | ||
* @param firstIndices A reference to an object of type `CompleteIndexVector` that provides access to the | ||
* indices | ||
* @param second A reference to an object of type `BitDecomposableStatisticVector` that stores the | ||
* gradients and Hessians in the second vector | ||
*/ | ||
void difference(const BitDecomposableStatisticVector& first, const CompleteIndexVector& firstIndices, | ||
const BitDecomposableStatisticVector& second); | ||
|
||
/** | ||
* Sets the gradients and Hessians in this vector to the difference `first - second` between the gradients | ||
* and Hessians in two other vectors, considering only the gradients and Hessians in the first vector that | ||
* correspond to the positions provided by a `PartialIndexVector`. | ||
* | ||
* @param first A reference to an object of type `BitDecomposableStatisticVector` that stores the | ||
* gradients and Hessians in the first vector | ||
* @param firstIndices A reference to an object of type `PartialIndexVector` that provides access to the | ||
* indices | ||
* @param second A reference to an object of type `BitDecomposableStatisticVector` that stores the | ||
* gradients and Hessians in the second vector | ||
*/ | ||
void difference(const BitDecomposableStatisticVector& first, const PartialIndexVector& firstIndices, | ||
const BitDecomposableStatisticVector& second); | ||
|
||
/** | ||
* Sets all gradients and Hessians stored in the vector to zero. | ||
*/ | ||
void clear(); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
cpp/subprojects/boosting/src/mlrl/boosting/data/vector_statistic_decomposable_bit.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#include "mlrl/boosting/data/vector_statistic_decomposable_bit.hpp" | ||
|
||
namespace boosting { | ||
|
||
static inline void copyInternally(const BitVector<uint32>& firstView, BitVector<uint32>& secondView) { | ||
typename BitVector<uint32>::const_iterator begin = firstView.cbegin(); | ||
uint32 arraySize = firstView.cend() - begin; | ||
util::copyView(begin, secondView.begin(), arraySize); | ||
} | ||
|
||
static inline void addInternally(BitVector<uint32>& firstView, const BitVector<uint32>& secondView) { | ||
typename BitVector<uint32>::iterator begin = firstView.begin(); | ||
uint32 arraySize = firstView.end() - begin; | ||
util::addToView(begin, secondView.cbegin(), arraySize); | ||
} | ||
|
||
static inline void addInternally(BitVector<uint32>& firstView, const BitVector<uint32>& secondView, | ||
const PartialIndexVector& indices) { | ||
uint32 numIndices = indices.getNumElements(); | ||
|
||
for (uint32 i = 0; i < numIndices; i++) { | ||
uint32 index = indices[i]; | ||
uint32 value = secondView[index]; | ||
firstView.set(i, value); | ||
} | ||
} | ||
|
||
static inline void removeInternally(BitVector<uint32>& firstView, const BitVector<uint32>& secondView) { | ||
typename BitVector<uint32>::iterator begin = firstView.begin(); | ||
uint32 arraySize = firstView.end() - begin; | ||
util::removeFromView(begin, secondView.cbegin(), arraySize); | ||
} | ||
|
||
static inline void differenceInternally(BitVector<uint32>& firstView, const BitVector<uint32>& secondView, | ||
const BitVector<uint32>& thirdView) { | ||
typename BitVector<uint32>::iterator begin = firstView.begin(); | ||
uint32 arraySize = firstView.end() - begin; | ||
util::setViewToDifference(begin, secondView.cbegin(), thirdView.cbegin(), arraySize); | ||
} | ||
|
||
static inline void differenceInternally(BitVector<uint32>& firstView, const BitVector<uint32>& secondView, | ||
const PartialIndexVector& indices, const BitVector<uint32>& thirdView) { | ||
uint32 numIndices = indices.getNumElements(); | ||
|
||
for (uint32 i = 0; i < numIndices; i++) { | ||
uint32 index = indices[i]; | ||
uint32 firstValue = secondView[index]; | ||
uint32 secondValue = thirdView[i]; | ||
firstView.set(i, firstValue - secondValue); | ||
} | ||
} | ||
|
||
BitDecomposableStatisticVector::BitDecomposableStatisticVector(uint32 numElements, uint32 numBitsPerElement, | ||
bool init) | ||
: CompositeView<AllocatedBitVector<uint32>, AllocatedBitVector<uint32>>( | ||
AllocatedBitVector<uint32>(numElements, numBitsPerElement, init), | ||
AllocatedBitVector<uint32>(numElements, numBitsPerElement, init)) {} | ||
|
||
BitDecomposableStatisticVector::BitDecomposableStatisticVector(const BitDecomposableStatisticVector& other) | ||
: BitDecomposableStatisticVector(other.getNumElements(), other.getNumBitsPerElement()) { | ||
copyInternally(other.firstView, this->firstView); | ||
copyInternally(other.secondView, this->secondView); | ||
} | ||
|
||
uint32 BitDecomposableStatisticVector::getNumElements() const { | ||
return this->firstView.numElements; | ||
} | ||
|
||
uint32 BitDecomposableStatisticVector::getNumBitsPerElement() const { | ||
return this->firstView.numBitsPerElement; | ||
} | ||
|
||
void BitDecomposableStatisticVector::add(const BitDecomposableStatisticVector& vector) { | ||
addInternally(this->firstView, vector.firstView); | ||
addInternally(this->secondView, vector.secondView); | ||
} | ||
|
||
void BitDecomposableStatisticVector::add(const BitDecomposableStatisticView& view, uint32 row) { | ||
addInternally(this->firstView, view.firstView[row]); | ||
addInternally(this->secondView, view.secondView[row]); | ||
} | ||
|
||
void BitDecomposableStatisticVector::add(const BitDecomposableStatisticView& view, uint32 row, float64 weight) { | ||
// TODO Implement | ||
throw std::runtime_error("not implemented"); | ||
} | ||
|
||
void BitDecomposableStatisticVector::remove(const BitDecomposableStatisticView& view, uint32 row) { | ||
removeInternally(this->firstView, view.firstView[row]); | ||
removeInternally(this->secondView, view.secondView[row]); | ||
} | ||
|
||
void BitDecomposableStatisticVector::remove(const BitDecomposableStatisticView& view, uint32 row, float64 weight) { | ||
// TODO Implement | ||
throw std::runtime_error("not implemented"); | ||
} | ||
|
||
void BitDecomposableStatisticVector::addToSubset(const BitDecomposableStatisticView& view, uint32 row, | ||
const CompleteIndexVector& indices) { | ||
addInternally(this->firstView, view.firstView[row]); | ||
addInternally(this->secondView, view.secondView[row]); | ||
} | ||
|
||
void BitDecomposableStatisticVector::addToSubset(const BitDecomposableStatisticView& view, uint32 row, | ||
const PartialIndexVector& indices) { | ||
addInternally(this->firstView, view.firstView[row], indices); | ||
addInternally(this->secondView, view.secondView[row], indices); | ||
} | ||
|
||
void BitDecomposableStatisticVector::addToSubset(const BitDecomposableStatisticView& view, uint32 row, | ||
const CompleteIndexVector& indices, float64 weight) { | ||
// TODO Implement | ||
throw std::runtime_error("not implemented"); | ||
} | ||
|
||
void BitDecomposableStatisticVector::addToSubset(const BitDecomposableStatisticView& view, uint32 row, | ||
const PartialIndexVector& indices, float64 weight) { | ||
// TODO Implement | ||
throw std::runtime_error("not implemented"); | ||
} | ||
|
||
void BitDecomposableStatisticVector::difference(const BitDecomposableStatisticVector& first, | ||
const CompleteIndexVector& firstIndices, | ||
const BitDecomposableStatisticVector& second) { | ||
differenceInternally(this->firstView, first.firstView, second.firstView); | ||
differenceInternally(this->secondView, first.secondView, second.secondView); | ||
} | ||
|
||
void BitDecomposableStatisticVector::difference(const BitDecomposableStatisticVector& first, | ||
const PartialIndexVector& firstIndices, | ||
const BitDecomposableStatisticVector& second) { | ||
differenceInternally(this->firstView, first.firstView, firstIndices, second.firstView); | ||
differenceInternally(this->secondView, first.secondView, firstIndices, second.secondView); | ||
} | ||
|
||
void BitDecomposableStatisticVector::clear() { | ||
this->firstView.clear(); | ||
this->secondView.clear(); | ||
} | ||
|
||
} |
Oops, something went wrong.