forked from alesfav/convolutional_neural_kernels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
67 lines (47 loc) · 1.56 KB
/
Copy pathutils.py
File metadata and controls
67 lines (47 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import torch
def hypersphere_random_sampler(n_points, input_dim):
"""
Samples inputs uniformly on a d-dimensional hypersphere.
Args:
n_points: number of points to sample
input_dim: dimension of the inputs
"""
x = torch.randn(n_points, input_dim)
x /= torch.norm(x, dim=1, keepdim=True)
return x
def grf_generator(gram, device):
"""
Generates a centered Gaussian random field with given covariance.
Args:
gram: covariance matrix
device: device to use (cpu or cuda)
"""
N = torch.distributions.multivariate_normal.MultivariateNormal(
torch.zeros(len(gram)).to(device), gram
)
y = N.sample()
return y
def kernel_regression(K_trtr, K_tetr, y_tr, y_te, ridge, device):
"""
Computes the generalisation error of kernel regression.
Args:
K_trtr: kernel matrix evaluated on the training points
K_tetr: mixed kernel matrix evaluated on the test and training points
y_tr: training labels
y_te: test labels
ridge: L2 regularizer
device: device to use (cpu or gpu)
"""
alpha = torch.linalg.inv(K_trtr + ridge * torch.eye(y_tr.size(0)).to(device)) @ y_tr
f = K_tetr @ alpha
mse = (f - y_te).pow(2).mean()
return mse
def kernel_spectrum(gram):
"""
Computes the eigenvalues of a kernel matrix (returned in decreasing order).
Args:
gram: kernel matrix to diagonalise
"""
evals, _ = torch.linalg.eigh(gram)
evals = 1 / gram.size(-1) * torch.flip(evals, dims=(0,))
return evals