pulsatrix 1.1.0
Loading...
Searching...
No Matches
representation_similarity.hpp
Go to the documentation of this file.
1
34#pragma once
35
36#include <cstddef>
37#include <cstdint>
38#include <vector>
39
40#include "pulsatrix/tensor.hpp"
41
42namespace pulsatrix {
43
45enum class CkaEstimator {
51 Biased,
52};
53
68[[nodiscard]] double LinearCka(const Tensor& x, const Tensor& y, CkaEstimator estimator = CkaEstimator::Debiased);
69
72struct CkaMatrix {
73 int64_t rows = 0;
74 int64_t cols = 0;
75 std::vector<double> values;
76
77 [[nodiscard]] double at(int64_t row, int64_t col) const;
78};
79
88[[nodiscard]] CkaMatrix LinearCkaMatrix(const std::vector<Tensor>& a, const std::vector<Tensor>& b,
90 std::size_t max_gram_bytes = std::size_t{2} << 30);
91
92// ---- RSA ----
93
95enum class RdmMetric {
100 Euclidean,
102 Cosine,
103};
104
108struct Rdm {
109 int64_t n = 0;
110 std::vector<double> values;
111
113 [[nodiscard]] double at(int64_t i, int64_t j) const;
114};
115
126
128enum class RdmComparison {
131 Spearman,
133 Pearson,
134};
135
142[[nodiscard]] double CompareRdms(const Rdm& a, const Rdm& b, RdmComparison comparison = RdmComparison::Spearman);
143
147[[nodiscard]] double Rsa(const Tensor& x, const Tensor& y, RdmMetric metric = RdmMetric::Correlation,
149
153 double similarity = 0;
156 double p_value = 1;
157 int64_t permutations = 0;
158};
159
171[[nodiscard]] RsaTestResult RsaPermutationTest(const Rdm& a, const Rdm& b, RdmComparison comparison = RdmComparison::Spearman,
172 int64_t permutations = 999, uint64_t seed = 0);
173
174// ---- SVCCA ----
175
190[[nodiscard]] std::vector<double> CanonicalCorrelations(const Tensor& x, const Tensor& y);
191
196 double variance_kept = 0.99;
199 int64_t directions = 0;
200};
201
205 double similarity = 0;
207 std::vector<double> correlations;
209 int64_t x_directions = 0;
210 int64_t y_directions = 0;
211};
212
218[[nodiscard]] SvccaResult Svcca(const Tensor& x, const Tensor& y, const SvccaOptions& options = {});
219
220} // namespace pulsatrix
N-dimensional tensor. Owns its data buffer exclusively; a DeviceBackend* is injected (not owned) – th...
Definition tensor.hpp:29
Definition acquisition_functions.hpp:16
double LinearCka(const Tensor &x, const Tensor &y, CkaEstimator estimator=CkaEstimator::Debiased)
Linear CKA between two representations of the same inputs.
SvccaResult Svcca(const Tensor &x, const Tensor &y, const SvccaOptions &options={})
SVCCA: CanonicalCorrelations() between each representation's top singular directions.
RsaTestResult RsaPermutationTest(const Rdm &a, const Rdm &b, RdmComparison comparison=RdmComparison::Spearman, int64_t permutations=999, uint64_t seed=0)
Whether two RDMs agree more than chance: the Mantel test RSA uses.
double CompareRdms(const Rdm &a, const Rdm &b, RdmComparison comparison=RdmComparison::Spearman)
The correlation between two RDMs of the same inputs, in the same order.
Rdm RepresentationalDissimilarity(const Tensor &x, RdmMetric metric=RdmMetric::Correlation)
The RDM of a representation, shape (n, ...): a row per input, later dimensions flattened.
double Rsa(const Tensor &x, const Tensor &y, RdmMetric metric=RdmMetric::Correlation, RdmComparison comparison=RdmComparison::Spearman)
RSA between two representations of the same inputs: CompareRdms(RepresentationalDissimilarity(x),...
CkaMatrix LinearCkaMatrix(const std::vector< Tensor > &a, const std::vector< Tensor > &b, CkaEstimator estimator=CkaEstimator::Debiased, std::size_t max_gram_bytes=std::size_t{2}<< 30)
LinearCka() between every representation in a and every one in b.
@ Euclidean
Euclidean distance. Lower is nearer.
@ Cosine
Cosine similarity: the angle between vectors, ignoring their lengths. Higher is nearer....
std::vector< double > CanonicalCorrelations(const Tensor &x, const Tensor &y)
The canonical correlations between two representations of the same inputs, largest first: min(rank x,...
CkaEstimator
Which estimator of CKA to use.
Definition representation_similarity.hpp:45
@ Biased
Kornblith et al.'s original estimator. Between 0 and 1, but biased upwards when the inputs are few co...
@ Debiased
Unbiased HSIC in place of HSIC: near 0 for unrelated representations at any number of inputs....
RdmComparison
How CompareRdms() correlates two RDMs.
Definition representation_similarity.hpp:128
@ Pearson
Pearson's correlation of the distances themselves.
@ Spearman
Spearman's rank correlation, ties given their average rank (scipy's spearmanr). Only the order of the...
RdmMetric
How RepresentationalDissimilarity() measures the distance between two inputs.
Definition representation_similarity.hpp:95
@ Correlation
1 minus the Pearson correlation of the two rows, between 0 and 2: the classic RSA choice,...
CKA between every pair from two lists of representations, such as the layers of two models: values[i ...
Definition representation_similarity.hpp:72
std::vector< double > values
Definition representation_similarity.hpp:75
int64_t cols
Definition representation_similarity.hpp:74
double at(int64_t row, int64_t col) const
int64_t rows
Definition representation_similarity.hpp:73
A representational dissimilarity matrix: the distance between every pair of n inputs.
Definition representation_similarity.hpp:108
std::vector< double > values
Definition representation_similarity.hpp:110
int64_t n
Definition representation_similarity.hpp:109
double at(int64_t i, int64_t j) const
The distance between inputs i and j; 0 when they are the same.
RsaPermutationTest()'s result.
Definition representation_similarity.hpp:151
double p_value
One-sided: the share of relabelings, counting the real one, that score at least similarity....
Definition representation_similarity.hpp:156
int64_t permutations
Definition representation_similarity.hpp:157
double similarity
CompareRdms() of the two RDMs as given.
Definition representation_similarity.hpp:153
Svcca()'s settings.
Definition representation_similarity.hpp:193
int64_t directions
If above 0, keep exactly this many directions of each instead (capped at its rank),...
Definition representation_similarity.hpp:199
double variance_kept
Keep each representation's fewest top singular directions whose share of the variance exceeds this,...
Definition representation_similarity.hpp:196
Svcca()'s result.
Definition representation_similarity.hpp:203
std::vector< double > correlations
The canonical correlations between the kept directions, largest first.
Definition representation_similarity.hpp:207
int64_t y_directions
Definition representation_similarity.hpp:210
int64_t x_directions
How many singular directions of x and of y were kept.
Definition representation_similarity.hpp:209
double similarity
The mean canonical correlation, Raghu et al.'s similarity.
Definition representation_similarity.hpp:205
N-dimensional tensor – owns a buffer via DeviceBackend*, RAII (Rule of Five).