Monte Carlo Sampling¶
Function to compute Monte Carlo NNGP and NTK estimates.
This module contains a function monte_carlo_kernel_fn
that allow to compute
Monte Carlo estimates of NNGP and NTK kernels of arbitrary functions. For more
details on how individual samples are computed, refer to utils/empirical.py
.
Note that the monte_carlo_kernel_fn
accepts arguments like batch_size
,
device_count
, and store_on_device
, and is appropriately batched /
parallelized. You don’t need to apply the nt.batch
or jax.jit
decorators to
it. Further, you do not need to apply jax.jit
to the input apply_fn
function, as the resulting empirical kernel function is JITted internally.
- neural_tangents.utils.monte_carlo.monte_carlo_kernel_fn(init_fn, apply_fn, key, n_samples, batch_size=0, device_count=- 1, store_on_device=True, trace_axes=(- 1,), diagonal_axes=(), vmap_axes=None, implementation=1)[source]¶
Return a Monte Carlo sampler of NTK and NNGP kernels of a given function.
Note that the returned function is appropriately batched / parallelized. You don’t need to apply the
nt.batch
orjax.jit
decorators to it. Further, you do not need to applyjax.jit
to the inputapply_fn
function, as the resulting empirical kernel function is JITted internally.- Parameters
init_fn (
Callable
[[ndarray
,Union
[List
[Tuple
[int
, …]],Tuple
[Tuple
[int
, …], …],Tuple
[int
, …]]],Tuple
[Union
[List
[Tuple
[int
, …]],Tuple
[Tuple
[int
, …], …],Tuple
[int
, …]],Any
]]) – a function initializing parameters of the neural network. Fromjax.experimental.stax
: “takes an rng key and an input shape and returns an(output_shape, params)
pair”.apply_fn (
Callable
[[Any
,Union
[List
[ndarray
],Tuple
[ndarray
, …],ndarray
]],Union
[List
[ndarray
],Tuple
[ndarray
, …],ndarray
]]) – a function computing the output of the neural network. Fromjax.experimental.stax
: “takes params, inputs, and an rng key and applies the layer”.key (
ndarray
) – RNG (jax.random.PRNGKey
) for sampling random networks. Must have shape(2,)
.n_samples (
Union
[int
,Iterable
[int
]]) – number of Monte Carlo samples. Can be either an integer or an iterable of integers at which the resulting generator will yield estimates. Example: usen_samples=[2**k for k in range(10)]
for the generator to yield estimates using 1, 2, 4, …, 512 Monte Carlo samples.batch_size (
int
) – an integer making the kernel computed in batches ofx1
andx2
of this size.0
means computing the whole kernel. Must dividex1.shape[0]
andx2.shape[0]
.device_count (
int
) – an integer making the kernel be computed in parallel across this number of devices (e.g. GPUs or TPU cores).-1
means use all available devices.0
means compute on a single device sequentially. If not0
, must dividex1.shape[0]
.store_on_device (
bool
) – a boolean, indicating whether to store the resulting kernel on the device (e.g. GPU or TPU), or in the CPU RAM, where larger kernels may fit.trace_axes (
Union
[int
,Sequence
[int
]]) – output axes to trace the output kernel over, i.e. compute only the trace of the covariance along the respective pair of axes (one pair for each axis intrace_axes
). This allows to save space and compute if you are only interested in the respective trace, but also improve approximation accuracy if you know that covariance along these pairs of axes converges to aconstant * identity matrix
in the limit of interest (e.g. infinite width or infiniten_samples
). A common use case is the channel / feature / logit axis, since activation slices along such axis are i.i.d. and the respective covariance along the respective pair of axes indeed converges to a constant-diagonal matrix in the infinite width or infiniten_samples
limit. Also related to “contracting dimensions” in XLA terms. (https://www.tensorflow.org/xla/operation_semantics#dotgeneral)diagonal_axes (
Union
[int
,Sequence
[int
]]) – output axes to diagonalize the output kernel over, i.e. compute only the diagonal of the covariance along the respective pair of axes (one pair for each axis indiagonal_axes
). This allows to save space and compute, if off-diagonal values along these axes are not needed, but also improve approximation accuracy if their limiting value is known theoretically, e.g. if they vanish in the limit of interest (e.g. infinite width or infiniten_samples
). If you further know that on-diagonal values converge to the same constant in your limit of interest, you should specify these axes intrace_axes
instead, to save even more compute and gain even more accuracy. A common use case is computing the variance (instead of covariance) along certain axes. Also related to “batch dimensions” in XLA terms. (https://www.tensorflow.org/xla/operation_semantics#dotgeneral)vmap_axes (
Optional
[Tuple
[Union
[List
[int
],Tuple
[int
, …],int
,None
],Union
[List
[int
],Tuple
[int
, …],int
,None
],Dict
[str
,Union
[List
[int
],Tuple
[int
, …],int
,None
]]]]) – applicable only to NTK. A triple of(in_axes, out_axes, kwargs_axes)
passed tovmap
to evaluate the empirical NTK in parallel ove these axes. Precisely, providing this argument implies thatf(params, x, **kwargs)
equals to a concatenation alongout_axes
off
applied to slices ofx
and**kwargs
alongin_axes
andkwargs_axes
, i.e.f
can be evaluated as avmap
. This allows to evaluate Jacobians much more efficiently. Ifvmap_axes
is not a triple, it is interpreted asin_axes = out_axes = vmap_axes, kwargs_axes = {}
. For example a very common usecase isvmap_axes=0
for a neural network with leading (0
) batch dimension, both for inputs and outputs, and no interactions between different elements of the batch (e.g. no BatchNorm, and, in the case ofnt.stax
, also no Dropout). However, if there is interaction between batch elements or no concept of a batch axis at all,vmap_axes
must be set toNone
, to avoid wrong (and potentially silent) results.implementation (
int
) –applicable only to NTK.
1
or2
.1
directly instantiates Jacobians and computes their outer product.2
uses implicit differentiation to avoid instantiating whole Jacobians at once. The implicit kernel is derived by observing that: \(\Theta = J(X_1) J(X_2)^T = [J(X_1) J(X_2)^T](I)\), i.e. a linear function \([J(X_1) J(X_2)^T]\) applied to an identity matrix \(I\). This allows the computation of the NTK to be phrased as: \(a(v) = J(X_2)^T v\), which is computed by a vector-Jacobian product; \(b(v) = J(X_1) a(v)\) which is computed by a Jacobian-vector product; and \(\Theta = [b(v)] / d[v^T](I)\) which is computed via avmap
of \(b(v)\) over columns of the identity matrix \(I\).It is best to benchmark each method on your specific task. We suggest using
1
unless you get OOMs due to large number of trainable parameters, otherwise -2
.
- Return type
Callable
[[Union
[List
[ndarray
],Tuple
[ndarray
, …],ndarray
],Union
[List
[ndarray
],Tuple
[ndarray
, …],ndarray
,None
],Union
[Tuple
[str
, …],str
,None
]],Union
[List
[ndarray
],Tuple
[ndarray
, …],ndarray
,Generator
[Union
[List
[ndarray
],Tuple
[ndarray
, …],ndarray
],None
,None
]]]- Returns
If
n_samples
is an integer, returns a function of signaturekernel_fn(x1, x2, get)
that returns an MC estimation of the kernel usingn_samples
. Ifn_samples
is a collection of integers,kernel_fn(x1, x2, get)
returns a generator that yields estimates usingn
samples forn in n_samples
.
Example
>>> from jax import random >>> import neural_tangents as nt >>> from neural_tangents import stax >>> >>> key1, key2 = random.split(random.PRNGKey(1), 2) >>> x_train = random.normal(key1, (20, 32, 32, 3)) >>> y_train = random.uniform(key1, (20, 10)) >>> x_test = random.normal(key2, (5, 32, 32, 3)) >>> >>> init_fn, apply_fn, _ = stax.serial( >>> stax.Conv(128, (3, 3)), >>> stax.Relu(), >>> stax.Conv(256, (3, 3)), >>> stax.Relu(), >>> stax.Conv(512, (3, 3)), >>> stax.Flatten(), >>> stax.Dense(10) >>> ) >>> >>> n_samples = 200 >>> kernel_fn = nt.monte_carlo_kernel_fn(init_fn, apply_fn, key1, n_samples) >>> kernel = kernel_fn(x_train, x_test, get=('nngp', 'ntk')) >>> # `kernel` is a tuple of NNGP and NTK MC estimate using `n_samples`. >>> >>> n_samples = [1, 10, 100, 1000] >>> kernel_fn_generator = nt.monte_carlo_kernel_fn(init_fn, apply_fn, key1, >>> n_samples) >>> kernel_samples = kernel_fn_generator(x_train, x_test, >>> get=('nngp', 'ntk')) >>> for n, kernel in zip(n_samples, kernel_samples): >>> print(n, kernel) >>> # `kernel` is a tuple of NNGP and NTK MC estimate using `n` samples.