Giter Site home page Giter Site logo

Comments (2)

gAldeia avatar gAldeia commented on August 11, 2024

After talking with @lacava, we found that boolean values were missing in signature.h file:

struct Signatures<N, enable_if_t<is_in_v<N, NodeType::Constant, NodeType::Terminal>>>{
using type = std::tuple<
Signature<ArrayXf()>,
Signature<ArrayXi()>
>;
};

After adding it, I had to make some changes so:

  1. Terminal nodes with boolean features cannot be weighted;
  2. get_weights knows about 1 and behave accordingly.

For 1, I created a new function to check if a node's ret_type is one of the unweightable types:

brush/src/program/node.h

Lines 43 to 62 in 25c8674

template <DataType... T>
inline auto Isnt(DataType dt) -> bool { return !((dt == T) || ...); }
template<DataType DT>
inline auto IsWeighable() noexcept -> bool {
return Isnt<DataType::ArrayB,
DataType::MatrixB,
DataType::TimeSeriesB,
DataType::ArrayBJet,
DataType::MatrixBJet
>(DT);
}
inline auto IsWeighable(DataType dt) noexcept -> bool {
return Isnt<DataType::ArrayB,
DataType::MatrixB,
DataType::TimeSeriesB,
DataType::ArrayBJet,
DataType::MatrixBJet
>(dt);
}

and started using it in the Node's constructor.

I also changed how we manipulate the is_weighted: now we should use a getter and setter (as we already have for other attributes). The setter performs a check before enabling a node to be weightable:

brush/src/program/node.h

Lines 248 to 260 in 25c8674

inline bool get_is_weighted() const {return this->is_weighted;};
inline void set_is_weighted(bool is_weighted){
// cant change the weight of a boolean terminal
if (IsWeighable(this->ret_type))
this->is_weighted = is_weighted;
};
private:
/// @brief feature name for terminals or splitting nodes
string feature;
};

For 2, I've implemented a different function to get the weights of a boolean terminal (and changed the original one to require that the value is not boolean). The comment inside this function explains why it exists:

namespace util{
////////////////////////////////////////////////////////////////////////////////
/// @brief get weight
/// @tparam T return type
/// @tparam Scalar scalar type of return type
/// @param tn tree node
/// @param weights option pointer to a weight array, used in place of node weight
/// @return
template<typename T, typename Scalar, typename W>
requires (!is_one_of_v<Scalar,bool, bJet>)
Scalar get_weight(const TreeNode& tn, const W** weights=nullptr)
{
Scalar w;
// Prediction case: weight is stored in the node data.
if (weights == nullptr)
{
w = Scalar(tn.data.W);
}
else
{
// NLS case 1: floating point weight is stored in weights
if constexpr (is_same_v<Scalar, W>)
w = **weights;
// NLS case 2: a Jet/Dual weight is stored in weights, but this constant is a
// integer type. We need to do some casting
else if constexpr (is_same_v<Scalar, iJet> && is_same_v<W, fJet>) {
using WScalar = typename Scalar::Scalar;
WScalar tmp = WScalar((**weights).a);
w = Scalar(tmp);
}
// NLS case 3: a Jet/Dual weight is stored in weights, matching Scalar type
else
w = Scalar(**weights);
*weights = *weights+1;
}
return w;
};
template<typename T, typename Scalar, typename W>
requires (is_one_of_v<Scalar,bool, bJet>)
Scalar get_weight(const TreeNode& tn, const W** weights=nullptr)
{
// we cannot weight a boolean feature. Nevertheless, we need to provide
// an implementation for get_weight behavior, so the metaprogramming
// doesn't fail to get a matching signature.
return Scalar(true);
};
}

These changes resulted in a new brush version that successfully passes on all tests, including the most recent test_data.cpp that I wrote specifically to fail due to the bug reported in this issue.

I think we can close this issue.

from brush.

gAldeia avatar gAldeia commented on August 11, 2024

This was solved in the PR #43.

from brush.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.