pulsatrix 1.1.0
Loading...
Searching...
No Matches
uncertainty.hpp
Go to the documentation of this file.
1
26#pragma once
27
28#include <cstdint>
29#include <functional>
30#include <vector>
31
33#include "pulsatrix/json.hpp"
34#include "pulsatrix/module.hpp"
35#include "pulsatrix/tensor.hpp"
36
37namespace pulsatrix {
38
55[[nodiscard]] std::vector<Tensor> McDropoutSamples(Module& model, const std::function<Tensor(const Tensor&)>& predict,
56 const Tensor& input, int64_t samples);
57
64[[nodiscard]] std::vector<Tensor> EnsembleSamples(const std::vector<std::function<Tensor(const Tensor&)>>& members,
65 const Tensor& input);
66
72 std::vector<int64_t> predicted;
74 std::vector<double> total;
76 std::vector<double> aleatoric;
78 std::vector<double> epistemic;
80 std::vector<double> agreement;
81 int64_t samples = 0;
82};
83
92[[nodiscard]] UncertaintyDecomposition DecomposeUncertainty(const std::vector<Tensor>& samples);
93
97 std::vector<double> mean;
99 std::vector<double> variance;
101 std::vector<double> aleatoric;
103 std::vector<double> epistemic;
104};
105
116[[nodiscard]] GaussianMixturePrediction CombineGaussians(const std::vector<std::vector<double>>& means,
117 const std::vector<std::vector<double>>& variances = {});
118
131public:
137 explicit GaussianNLLLoss(DeviceBackend* backend, double min_variance = 1e-6);
138
145 [[nodiscard]] float forward(const Tensor& prediction, const Tensor& target);
146
151 [[nodiscard]] Tensor backward() const;
152
154 [[nodiscard]] double variance(double s) const;
155
156private:
157 DeviceBackend* backend_;
158 double min_variance_;
159 Tensor last_prediction_;
160 std::vector<float> last_target_;
161 bool has_forwarded_ = false;
162};
163
166[[nodiscard]] JsonValue ToJson(const UncertaintyDecomposition& decomposition);
167
168} // namespace pulsatrix
Vendor-agnostic compute/memory backend. CPUBackend, CUDABackend (Phase 1.5), and HIPBackend (Phase 1....
Definition device_backend.hpp:248
The Gaussian negative log likelihood, for regressors that predict their own noise (Nix and Weigend,...
Definition uncertainty.hpp:130
double variance(double s) const
The variance a prediction's s gives: softplus(s) + min_variance.
float forward(const Tensor &prediction, const Tensor &target)
GaussianNLLLoss(DeviceBackend *backend, double min_variance=1e-6)
Tensor backward() const
The gradient with respect to the prediction, (N, 2).
One JSON value: null, a boolean, a number, a string, an array or an object.
Definition json.hpp:22
Base class for every layer type (LinearModule, Conv2DModule, activations, ...).
Definition module.hpp:78
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Abstract interface isolating vendor-specific memory/compute operations from Tensor/ComputationGraph.
A small strict JSON value, parser and deterministic writer (RFC 8259).
Abstract base every layer subclasses – NVI forward(), pure-virtual LRP contract.
Definition acquisition_functions.hpp:16
std::vector< Tensor > McDropoutSamples(Module &model, const std::function< Tensor(const Tensor &)> &predict, const Tensor &input, int64_t samples)
Monte Carlo dropout: runs predict on input samples times with every dropout layer under model in trai...
JsonValue ToJson(const AttackResult &result, const AttackOptions &options)
The result as a pulsatrix.attack.v1 document, without the adversarial inputs.
GaussianMixturePrediction CombineGaussians(const std::vector< std::vector< double > > &means, const std::vector< std::vector< double > > &variances={})
Mixes M Gaussian predictions per input into one, as Lakshminarayanan et al. do: the mixture's mean is...
UncertaintyDecomposition DecomposeUncertainty(const std::vector< Tensor > &samples)
Splits the uncertainty of a set of probability samples into its aleatoric and epistemic parts,...
std::vector< Tensor > EnsembleSamples(const std::vector< std::function< Tensor(const Tensor &)> > &members, const Tensor &input)
A deep ensemble's samples: each member's output for input.
CombineGaussians()' result: one value per input.
Definition uncertainty.hpp:95
std::vector< double > epistemic
The variance of the members' means: how much they disagree.
Definition uncertainty.hpp:103
std::vector< double > variance
The mixture's variance: aleatoric + epistemic.
Definition uncertainty.hpp:99
std::vector< double > mean
The mixture's mean, the members' mean.
Definition uncertainty.hpp:97
std::vector< double > aleatoric
The members' mean variance: the noise each predicts.
Definition uncertainty.hpp:101
DecomposeUncertainty()'s result: one value per input, entropies in nats.
Definition uncertainty.hpp:68
std::vector< double > total
H[E p], the entropy of the mean.
Definition uncertainty.hpp:74
std::vector< int64_t > predicted
The most probable class of the mean, the lower index on a tie.
Definition uncertainty.hpp:72
std::vector< double > aleatoric
E H[p], the samples' mean entropy.
Definition uncertainty.hpp:76
std::vector< double > agreement
The fraction of samples whose most probable class is the mean's.
Definition uncertainty.hpp:80
int64_t samples
Definition uncertainty.hpp:81
Tensor mean
The mean of the samples' probabilities, (N, K): the prediction to use.
Definition uncertainty.hpp:70
std::vector< double > epistemic
total - aleatoric, the mutual information (never negative).
Definition uncertainty.hpp:78
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).