pulsatrix 1.1.0
Loading...
Searching...
No Matches
soft_decision_tree.hpp
Go to the documentation of this file.
1
32#pragma once
33
34#include <cstdint>
35#include <utility>
36#include <vector>
37
39#include "pulsatrix/module.hpp"
40
41namespace pulsatrix {
42
46 int depth = 4;
48 float beta = 1.0f;
50 float penalty = 0.0f;
53 uint64_t seed = 0;
54};
55
63class SoftDecisionTree : public Module {
64public:
65 SoftDecisionTree(int64_t inputs, int64_t classes, DeviceBackend* backend, SoftDecisionTreeOptions options = {});
66
67 [[nodiscard]] Tensor backward(const Tensor& grad_output) override;
68 [[nodiscard]] Tensor propagate_relevance(const Tensor& relevance_out, const LRPRuleConfig& config) override;
69 [[nodiscard]] bool supports_lrp_rule(LRPRule) const override { return false; }
70 [[nodiscard]] OpType op_type() const override { return OpType::Composite; }
72 [[nodiscard]] std::vector<NamedParamRef> named_parameters() override;
73 [[nodiscard]] std::vector<NamedModuleRef> named_children() override;
74
75 [[nodiscard]] int depth() const { return options_.depth; }
76 [[nodiscard]] int64_t num_inner() const { return (int64_t{1} << options_.depth) - 1; }
77 [[nodiscard]] int64_t num_leaves() const { return int64_t{1} << options_.depth; }
78 [[nodiscard]] int64_t num_inputs() const { return inputs_; }
79 [[nodiscard]] int64_t num_classes() const { return classes_; }
80 [[nodiscard]] const SoftDecisionTreeOptions& options() const { return options_; }
81
84 [[nodiscard]] LinearModule& gates() { return gates_; }
86 [[nodiscard]] Tensor& leaf_logits() { return leaves_; }
88 [[nodiscard]] std::vector<double> LeafDistributions() const;
89
92 [[nodiscard]] const std::vector<double>& right_probabilities() const { return p_; }
93 [[nodiscard]] const std::vector<double>& leaf_probabilities() const { return reach_leaf_; }
96 [[nodiscard]] double penalty() const { return penalty_; }
97
100 [[nodiscard]] std::vector<int64_t> MostProbableLeaf() const;
103 [[nodiscard]] std::vector<std::pair<int64_t, bool>> PathTo(int64_t leaf) const;
104
105protected:
106 [[nodiscard]] Tensor forward_impl(const Tensor& input) override;
107
108private:
109 DeviceBackend* backend_;
110 int64_t inputs_;
111 int64_t classes_;
113 LinearModule gates_;
114 Tensor leaves_;
115 Tensor leaves_grad_;
116 // The last forward()'s values, row-major.
117 std::vector<double> p_; // (N, inner)
118 std::vector<double> reach_; // (N, inner): probability of reaching each inner node
119 std::vector<double> reach_leaf_; // (N, leaves)
120 std::vector<double> q_; // (leaves, classes)
121 std::vector<double> mixture_; // (N, classes)
122 double penalty_ = 0.0;
123 int64_t batch_ = 0;
124 DeviceType input_device_ = DeviceType::Cpu;
125 bool has_forwarded_ = false;
126};
127
128} // namespace pulsatrix
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
y = x @ W + b, batched (x is (N, in_features), y is (N, out_features)) – migrated from the original u...
Definition linear_module.hpp:43
Base class for every layer type (LinearModule, Conv2DModule, activations, ...).
Definition module.hpp:78
Frosst and Hinton's soft decision tree, (N, inputs) to (N, classes) log-probabilities.
Definition soft_decision_tree.hpp:63
Tensor & leaf_logits()
The leaves' logits φ, (leaves, classes).
Definition soft_decision_tree.hpp:86
const std::vector< double > & leaf_probabilities() const
Definition soft_decision_tree.hpp:93
Tensor backward(const Tensor &grad_output) override
Computes the gradient w.r.t. this module's input, given the gradient w.r.t. its output....
bool supports_lrp_rule(LRPRule) const override
Whether propagate_relevance() implements rule (no silent fallback: callers such as ExplainerContext::...
Definition soft_decision_tree.hpp:69
double penalty() const
The last forward()'s balance penalty C, λ included; 0 when λ is 0. Add it to the loss you report: bac...
Definition soft_decision_tree.hpp:96
int64_t num_leaves() const
Definition soft_decision_tree.hpp:77
std::vector< double > LeafDistributions() const
The leaves' distributions softmax(φ), row-major (leaves, classes).
std::vector< int64_t > MostProbableLeaf() const
For each input of the last forward(), the leaf its most probable path ends at: the greedy path,...
OpType op_type() const override
This module's operation-category tag, for ComputationGraph node tagging.
Definition soft_decision_tree.hpp:70
int64_t num_inputs() const
Definition soft_decision_tree.hpp:78
std::vector< std::pair< int64_t, bool > > PathTo(int64_t leaf) const
The inner nodes on the path from the root to leaf, root first, and whether the path goes right at eac...
int64_t num_inner() const
Definition soft_decision_tree.hpp:76
Tensor forward_impl(const Tensor &input) override
The actual forward computation. Called by forward() after precondition checks.
std::vector< NamedParamRef > named_parameters() override
gates.weight, gates.bias and leaves.
int64_t num_classes() const
Definition soft_decision_tree.hpp:79
Tensor propagate_relevance(const Tensor &relevance_out, const LRPRuleConfig &config) override
Computes this module's contribution to LRP relevance propagation.
std::vector< NamedModuleRef > named_children() override
This module's direct sub-modules, each under the prefix its parameters carry in named_parameters(),...
int depth() const
Definition soft_decision_tree.hpp:75
LinearModule & gates()
The gates: column i of its (inputs, inner) weight and entry i of its bias are node i's w_i and b_i.
Definition soft_decision_tree.hpp:84
const SoftDecisionTreeOptions & options() const
Definition soft_decision_tree.hpp:80
SoftDecisionTree(int64_t inputs, int64_t classes, DeviceBackend *backend, SoftDecisionTreeOptions options={})
const std::vector< double > & right_probabilities() const
From the last forward(): each input's probability of going right at each inner node,...
Definition soft_decision_tree.hpp:92
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Dense/fully-connected layer – the reference Module implementation.
Abstract base every layer subclasses – NVI forward(), pure-virtual LRP contract.
Definition acquisition_functions.hpp:16
DeviceType
Which physical device a Tensor's buffer resides on.
Definition device_backend.hpp:17
OpType
The op-type tag a Node carries. Charter Part 2 §3: nodes are tagged by a small closed set of op types...
Definition op_type.hpp:19
LRPRule
The LRP rule family a module applies. Semantics follow Zennit 1.0.0 exactly (Anders et al....
Definition lrp_rule_config.hpp:21
Configuration for LRP relevance propagation: which rule a module applies and its hyperparameters....
Definition lrp_rule_config.hpp:72
SoftDecisionTree's settings.
Definition soft_decision_tree.hpp:44
float beta
The gates' inverse temperature β: larger is closer to a hard tree.
Definition soft_decision_tree.hpp:48
int depth
Levels of inner nodes: 2^depth leaves. Between 1 and 16.
Definition soft_decision_tree.hpp:46
uint64_t seed
The gates' weights and biases start uniform in ±1/√inputs, as nn.Linear's do; the leaves start at zer...
Definition soft_decision_tree.hpp:53
float penalty
The balance penalty's λ; 0 turns it off. Frosst and Hinton use 0.1 or so.
Definition soft_decision_tree.hpp:50