76 throw std::invalid_argument(
"LinearProbe: activation_dim must be positive");
78 std::mt19937 rng(seed);
80 for (
float& w : weights) {
81 w = uniform_symmetric(rng, 0.01f);
84 classifier_.
set_bias(std::vector<float>{uniform_symmetric(rng, 0.01f)});
104 template <
typename OptimizerT>
106 validate_batch(activation_batch, label_batch,
"LinearProbe::train_step");
108 optimizer.zero_grad(classifier_);
111 const float loss_value = loss_.
forward(logits, label_batch);
114 (void)classifier_.
backward(grad_logits);
116 optimizer.step(classifier_);
141 validate_batch(activation_batch, label_batch,
"LinearProbe::accuracy");
144 const int64_t n = logits.
numel();
146 for (int64_t i = 0; i < n; ++i) {
147 const bool predicted_positive = logits.
data()[i] >= 0.0f;
148 const bool labeled_positive = label_batch.
data()[i] >= 0.5f;
149 if (predicted_positive == labeled_positive) {
153 return static_cast<float>(correct) /
static_cast<float>(n);
171 static float uniform_symmetric(std::mt19937& rng,
float scale) {
175 const float unit =
static_cast<float>(rng() - std::mt19937::min()) /
176 static_cast<float>(std::mt19937::max() - std::mt19937::min() + 1ull);
177 return (unit * 2.0f - 1.0f) * scale;
195 void validate_batch(
const Tensor& activation_batch,
const Tensor& label_batch,
const char* method)
const {
196 const std::string where(method);
197 if (activation_batch.rank() != 2) {
198 throw std::invalid_argument(where +
": activation_batch must be rank-2 (N, activation_dim)");
200 if (label_batch.rank() != 2 || label_batch.shape().dim(1) != 1) {
201 throw std::invalid_argument(where +
": label_batch must be rank-2 (N, 1)");
203 if (activation_batch.shape().dim(1) != activation_dim_) {
204 throw std::invalid_argument(where +
": activation_batch width must equal activation_dim");
206 if (activation_batch.shape().dim(0) != label_batch.shape().dim(0)) {
207 throw std::invalid_argument(where +
": activation_batch and label_batch must have the same batch size");
209 if (activation_batch.shape().dim(0) <= 0) {
210 throw std::invalid_argument(where +
": batch must not be empty");
214 int64_t activation_dim_;
215 mutable LinearModule classifier_;
216 BCEWithLogitsLoss loss_;
Binary cross-entropy on raw logits – combined sigmoid + BCE, numerically stable.
Tensor backward() const
Gradient w.r.t. the logits: grad[i] = (sigmoid(x[i]) - y[i]) / numel.
float forward(const Tensor &logits, const Tensor &target)
Computes the loss value and caches logits/target for backward().
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
Tensor backward(const Tensor &grad_output) override
Computes the gradient w.r.t. this module's input, and accumulates the weight/bias gradients internall...
void set_bias(std::initializer_list< float > values)
Overwrites the bias buffer – test/initialization use only.
void set_weight(std::initializer_list< float > values)
Overwrites the weight buffer – test/initialization use only.
A linear probe: LinearModule(activation_dim, 1) + BCEWithLogitsLoss, trained on (activation,...
Definition linear_probe.hpp:52
LinearProbe(int64_t activation_dim, DeviceBackend *backend)
Constructs a probe over activations of a given dimension.
Definition linear_probe.hpp:65
int64_t activation_dim() const
Width of the activation vectors this probe reads.
Definition linear_probe.hpp:157
float train_step(const Tensor &activation_batch, const Tensor &label_batch, OptimizerT &optimizer)
Runs one training step: forward, BCE-with-logits loss, backward, one optimizer update of the probe's ...
Definition linear_probe.hpp:105
const LinearModule & classifier() const
Const overload of classifier().
Definition linear_probe.hpp:167
LinearProbe(int64_t activation_dim, DeviceBackend *backend, unsigned seed)
Definition linear_probe.hpp:68
LinearModule & classifier()
The underlying linear classifier – inspection (learned weights are the whole point of a probe: their ...
Definition linear_probe.hpp:164
float accuracy(const Tensor &activation_batch, const Tensor &label_batch) const
Fraction of the batch the probe classifies correctly.
Definition linear_probe.hpp:140
Tensor forward(const Tensor &input)
Runs this module's forward computation.
Definition module.hpp:93
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
int64_t numel() const
Total element count – shape().numel().
Definition tensor.hpp:116
const float * data() const
Raw buffer access. nullptr iff numel() == 0.
Definition tensor.hpp:167
One global seed for everything that isn't given its own, and a deterministic mode that forbids nondet...
Dense/fully-connected layer – the reference Module implementation.
Definition acquisition_functions.hpp:16
uint64_t next_seed()
The next seed in the global stream: a distinct, well-mixed 64-bit value per call, reproducible for a ...