File vector_statistic_decomposable_sparse.hpp

namespace boosting
template<typename StatisticType, typename WeightType>
struct SparseStatistic
#include <vector_statistic_decomposable_sparse.hpp>

An individual label space statistic that consists of a gradient, a Hessian and a weight.

Template Parameters:
  • StatisticType – The type of the gradient and Hessian

  • WeightType – The type of the weight

Public Functions

inline SparseStatistic()
inline SparseStatistic(StatisticType gradient, StatisticType hessian, WeightType weight)
Parameters:
  • gradient – The gradient

  • hessian – The Hessian

  • weight – The weight

inline SparseStatistic<StatisticType, WeightType> &operator+=(const SparseStatistic<StatisticType, WeightType> &rhs)

Adds the gradient, Hessian and weight of a given statistic to the gradient, Hessian and weight of this statistic.

Parameters:

rhs – A reference to the statistic, whose gradient, Hessian and weight should be added

Returns:

A reference to the modified statistic

inline SparseStatistic<StatisticType, WeightType> &operator-=(const SparseStatistic<StatisticType, WeightType> &rhs)

Subtracts the gradient, Hessian and weight of a given statistic from the gradient, Hessian and weight of this statistic.

Parameters:

rhs – A reference to the statistic, whose gradient, Hessian and weight should be subtracted

Returns:

A reference to the modified statistic

Public Members

StatisticType gradient

The gradient.

StatisticType hessian

The Hessian.

WeightType weight

The weight.

Friends

inline friend SparseStatistic<StatisticType, WeightType> operator+(SparseStatistic<StatisticType, WeightType> lhs, const SparseStatistic<StatisticType, WeightType> &rhs)

Creates and returns a new statistic that results from adding the gradient, Hessian and weight of a specific statistic to the gradient, Hessian and weight of an existing statistic.

Parameters:
  • lhs – The original statistic

  • rhs – A reference to the statistic, whose gradient, Hessian and weight should be added

Returns:

The statistic that has been created

inline friend SparseStatistic<StatisticType, WeightType> operator-(SparseStatistic<StatisticType, WeightType> lhs, const SparseStatistic<StatisticType, WeightType> &rhs)

Creates and returns a new statistic that results from subtracting the gradient, Hessian and weight of a specific statistic from the gradient, Hessian and weight of an existing statistic.

Parameters:
  • lhs – The original statistic

  • rhs – A reference to the value to be subtracted

Returns:

The statistic that has been created

template<typename StatisticType, typename WeightType>
class SparseDecomposableStatisticVectorView : public AllocatedVector<SparseStatistic<StatisticType, WeightType>>
#include <vector_statistic_decomposable_sparse.hpp>

A one-dimensional view that provides access to aggregated gradients and Hessians, as well as the sums of the weights of the aggregated gradients and Hessians, that have been calculated using a decomposable loss function and are stored in pre-allocated arrays.

Template Parameters:
  • StatisticType – The type of the gradients and Hessians

  • WeightType – The type of the weights

Public Types

using statistic_type = StatisticType

The type of the gradients and Hessians.

using gradient_const_iterator = GradientConstIterator

An iterator that provides read-only access to the gradients in the view.

using hessian_const_iterator = HessianConstIterator

An iterator that provides read-only access to the Hessians in the view.

Public Functions

SparseDecomposableStatisticVectorView(uint32 numElements, bool init = false)
Parameters:
  • numElements – The number of gradients and Hessians in the view

  • init – True, if all elements in the view should be value-initialized, false otherwise

gradient_const_iterator gradients_cbegin() const

Returns a gradient_const_iterator to the beginning of the gradients.

Returns:

a gradient_const_iterator to the beginning

gradient_const_iterator gradients_cend() const

Returns a gradient_const_iterator to the end of the gradients.

Returns:

a gradient_const_iterator to the end

hessian_const_iterator hessians_cbegin() const

Returns a hessian_const_iterator to the beginning of the Hessians.

Returns:

a hessian_const_iterator to the beginning

hessian_const_iterator hessians_cend() const

Returns a hessian_const_iterator to the end of the Hessians.

Returns:

a hessian_const_iterator to the end

const uint32 getNumElements() const

Returns the number of elements in the view.

Returns:

The number of elements in the view

Public Members

WeightType sumOfWeights

The sum of the weights of all statistics that have been added to the view.

class GradientConstIterator

An iterator that provides random read-only access to the gradients in a SparseDecomposableStatisticVectorView.

Public Types

using difference_type = int

The type that is used to represent the difference between two iterators.

using value_type = StatisticType

The type of the elements, the iterator provides access to.

using pointer = const StatisticType*

The type of a pointer to an element, the iterator provides access to.

using reference = const StatisticType&

The type of a reference to an element, the iterator provides access to.

using iterator_category = std::random_access_iterator_tag

The tag that specifies the capabilities of the iterator.

Public Functions

GradientConstIterator(typename View<SparseStatistic<StatisticType, WeightType>>::const_iterator iterator, WeightType sumOfWeights)
Parameters:
  • iterator – An iterator that provides access to the elements in a SparseDecomposableStatisticVectorView

  • sumOfWeights – The sum of the weights of all statistics that have been added to the view

value_type operator[](uint32 index) const

Returns the element at a specific index.

Parameters:

index – The index of the element to be returned

Returns:

The element at the given index

value_type operator*() const

Returns the element, the iterator currently refers to.

Returns:

The element, the iterator currently refers to

GradientConstIterator &operator++()

Returns an iterator to the next element.

Returns:

A reference to an iterator that refers to the next element

GradientConstIterator &operator++(int n)

Returns an iterator to the next element.

Returns:

A reference to an iterator that refers to the next element

GradientConstIterator &operator--()

Returns an iterator to the previous element.

Returns:

A reference to an iterator that refers to the previous element

GradientConstIterator &operator--(int n)

Returns an iterator to the previous element.

Returns:

A reference to an iterator that refers to the previous element

bool operator!=(const GradientConstIterator &rhs) const

Returns whether this iterator and another one refer to the same element.

Parameters:

rhs – A reference to another iterator

Returns:

True, if the iterators do not refer to the same element, false otherwise

bool operator==(const GradientConstIterator &rhs) const

Returns whether this iterator and another one refer to the same element.

Parameters:

rhs – A reference to another iterator

Returns:

True, if the iterators refer to the same element, false otherwise

difference_type operator-(const GradientConstIterator &rhs) const

Returns the difference between this iterator and another one.

Parameters:

rhs – A reference to another iterator

Returns:

The difference between the iterators

Private Members

View<SparseStatistic<StatisticType, WeightType>>::const_iterator iterator_
const WeightType sumOfWeights_
class HessianConstIterator

An iterator that provides random read-only access to the Hessians in a SparseDecomposableStatisticVectorView.

Public Types

using difference_type = int

The type that is used to represent the difference between two iterators.

using value_type = StatisticType

The type of the elements, the iterator provides access to.

using pointer = const StatisticType*

The type of a pointer to an element, the iterator provides access to.

using reference = const StatisticType&

The type of a reference to an element, the iterator provides access to.

using iterator_category = std::random_access_iterator_tag

The tag that specifies the capabilities of the iterator.

Public Functions

HessianConstIterator(typename View<SparseStatistic<StatisticType, WeightType>>::const_iterator iterator, WeightType sumOfWeights)
Parameters:
  • iterator – An iterator that provides access to the elements in a SparseDecomposableStatisticVectorView

  • sumOfWeights – The sum of the weights of all statistics that have been added to the view

value_type operator[](uint32 index) const

Returns the element at a specific index.

Parameters:

index – The index of the element to be returned

Returns:

The element at the given index

value_type operator*() const

Returns the element, the iterator currently refers to.

Returns:

The element, the iterator currently refers to

HessianConstIterator &operator++()

Returns an iterator to the next element.

Returns:

A reference to an iterator that refers to the next element

HessianConstIterator &operator++(int n)

Returns an iterator to the next element.

Returns:

A reference to an iterator that refers to the next element

HessianConstIterator &operator--()

Returns an iterator to the previous element.

Returns:

A reference to an iterator that refers to the previous element

HessianConstIterator &operator--(int n)

Returns an iterator to the previous element.

Returns:

A reference to an iterator that refers to the previous element

bool operator!=(const HessianConstIterator &rhs) const

Returns whether this iterator and another one refer to the same element.

Parameters:

rhs – A reference to another iterator

Returns:

True, if the iterators do not refer to the same element, false otherwise

bool operator==(const HessianConstIterator &rhs) const

Returns whether this iterator and another one refer to the same element.

Parameters:

rhs – A reference to another iterator

Returns:

True, if the iterators refer to the same element, false otherwise

difference_type operator-(const HessianConstIterator &rhs) const

Returns the difference between this iterator and another one.

Parameters:

rhs – A reference to another iterator

Returns:

The difference between the iterators

Private Members

View<SparseStatistic<StatisticType, WeightType>>::const_iterator iterator_
const WeightType sumOfWeights_
template<typename StatisticType, typename WeightType, typename VectorMath>
class SparseDecomposableStatisticVector : public VectorDecorator<SparseDecomposableStatisticVectorView<StatisticType, WeightType>>
#include <vector_statistic_decomposable_sparse.hpp>

An one-dimensional vector that stores aggregated gradients and Hessians that have been calculated using a decomposable loss function in a C-contiguous array. For each element in the vector, a single gradient and Hessian, as well as the sums of the weights of the aggregated gradients and Hessians, is stored.

Template Parameters:
  • StatisticType – The type of the gradients and Hessians

  • WeightType – The type of the weights

  • VectorMath – The type that implements basic operations for calculating with numerical arrays

Public Functions

SparseDecomposableStatisticVector(uint32 numElements, bool init = false)
Parameters:
  • numElements – The number of gradients and Hessians in the vector

  • init – True, if all gradients and Hessians in the vector should be initialized with zero, false otherwise

SparseDecomposableStatisticVector(const SparseDecomposableStatisticVector<StatisticType, WeightType, VectorMath> &other)
Parameters:

other – A reference to an object of type SparseDecomposableStatisticVector to be copied

void add(const SparseDecomposableStatisticVectorView<StatisticType, WeightType> &vector)

Adds all gradients and Hessians in another vector to this vector.

Parameters:

vector – A reference to an object of type SparseDecomposableStatisticVectorView that stores the gradients and Hessians to be added to this vector

void add(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row)

Adds all gradients and Hessians in a single row of a SparseSetView to this vector.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be added to this vector

  • row – The index of the row to be added to this vector

void add(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row, WeightType weight)

Adds all gradients and Hessians in a single row of a SparseSetView to this vector. The gradients and Hessians to be added are multiplied by a specific weight.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be added to this vector

  • row – The index of the row to be added to this vector

  • weight – The weight, the gradients and Hessians should be multiplied by

void remove(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row)

Removes all gradients and Hessians in a single row of a SparseSetView from this vector.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be removed from this vector

  • row – The index of the row to be removed from this vector

void remove(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row, WeightType weight)

Removes all gradients and Hessians in a single row of a SparseSetView from this vector. The gradients and Hessians to be added are multiplied by a specific weight.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be removed from this vector

  • row – The index of the row to be removed from this vector

  • weight – The weight, the gradients and Hessians should be multiplied by

void addToSubset(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row, const CompleteIndexVector &indices)

Adds certain gradients and Hessians in a single row of a SparseSetView, whose positions are given as a CompleteIndexVector, to this vector.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be added to this vector

  • row – The index of the row to be added to this vector

  • indices – A reference to a CompleteIndexVector that provides access to the indices

void addToSubset(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row, const PartialIndexVector &indices)

Adds certain gradients and Hessians in a single row of a SparseSetView, whose positions are given as a PartialIndexVector, to this vector.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be added to this vector

  • row – The index of the row to be added to this vector

  • indices – A reference to a PartialIndexVector that provides access to the indices

void addToSubset(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row, const CompleteIndexVector &indices, WeightType weight)

Adds certain gradients and Hessians in a single row of a SparseSetView, whose positions are given as a CompleteIndexVector, to this vector. The gradients and Hessians to be added are multiplied by a specific weight.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be added to this vector

  • row – The index of the row to be added to this vector

  • indices – A reference to a CompleteIndexVector that provides access to the indices

  • weight – The weight, the gradients and Hessians should be multiplied by

void addToSubset(const SparseDecomposableStatisticView<StatisticType> &view, uint32 row, const PartialIndexVector &indices, WeightType weight)

Adds certain gradients and Hessians in a single row of a SparseSetView, whose positions are given as a PartialIndexVector, to this vector. The gradients and Hessians to be added are multiplied by a specific weight.

Parameters:
  • view – A reference to an object of type SparseSetView that stores the gradients and Hessians to be added to this vector

  • row – The index of the row to be added to this vector

  • indices – A reference to a PartialIndexVector that provides access to the indices

  • weight – The weight, the gradients and Hessians should be multiplied by

void difference(const SparseDecomposableStatisticVectorView<StatisticType, WeightType> &first, const CompleteIndexVector &firstIndices, const SparseDecomposableStatisticVectorView<StatisticType, WeightType> &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 CompleteIndexVector.

Parameters:
  • first – A reference to an object of type SparseDecomposableStatisticVectorView that stores the gradients and Hessians in the first vector

  • firstIndices – A reference to an object of type CompleteIndexVector that provides access to the indices

  • second – A reference to an object of type SparseDecomposableStatisticVectorView that stores the gradients and Hessians in the second vector

void difference(const SparseDecomposableStatisticVectorView<StatisticType, WeightType> &first, const PartialIndexVector &firstIndices, const SparseDecomposableStatisticVectorView<StatisticType, WeightType> &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.

Parameters:
  • first – A reference to an object of type SparseDecomposableStatisticVectorView that stores the gradients and Hessians in the first vector

  • firstIndices – A reference to an object of type PartialIndexVector that provides access to the indices

  • second – A reference to an object of type SparseDecomposableStatisticVectorView that stores the gradients and Hessians in the second vector

void clear()

Sets all gradients and Hessians stored in this vector to zero.