Watch talks from the 2019 TensorFlow Dev Summit

## Class `RationalQuadratic`

Inherits From: `PositiveSemidefiniteKernel`

This kernel function has the form:

``````k(x, y) = amplitude**2 * (1. + ||x - y|| ** 2 / (
2 * scale_mixture_rate * length_scale**2)) ** -scale_mixture_rate
``````

where the double-bars represent vector length (i.e. Euclidean, or L2 Norm). This kernel acts over the space `S = R^(D1 x D2 .. Dd)`. When `scale_mixture_rate` tends towards infinity, this kernel acts like an ExponentiatedQuadratic kernel.

The name `scale_mixture_rate` comes from the interpretation that that this kernel is an Inverse-Gamma weighted mixture of ExponentiatedQuadratic Kernels with different length scales.

More formally, if `r = ||x - y|||`, then:

``````integral from 0 to infinity (k_EQ(r | sqrt(t)) p(t | a, a * l ** 2)) dt =
(1 + r**2 / (2 * a * l ** 2)) ** -a = k(r)
``````

where: * `a = scale_mixture_rate` * `l = length_scale` * `p(t | a, b)` is the Inverse-Gamma density. https://en.wikipedia.org/wiki/Inverse-gamma_distribution * `k_EQ(r | l) = exp(-r ** 2 / (2 * l ** 2)` is the ExponentiatedQuadratic positive semidefinite kernel.

: Filip Tronarp, Toni Karvonen, and Simo Saarka. Mixture representation of the Matern class with applications in state space approximations and Bayesian quadrature. In 28th IEEE International Workshop on Machine Learning for Signal Processing, 2018. https://users.aalto.fi/~karvont2/pdf/TronarpKarvonenSarkka2018-MLSP.pdf

## `__init__`

``````__init__(
amplitude=None,
length_scale=None,
scale_mixture_rate=None,
feature_ndims=1,
validate_args=False,
)
``````

Construct a RationalQuadratic kernel instance.

#### Args:

• `amplitude`: Positive floating point `Tensor` that controls the maximum value of the kernel. Must be broadcastable with `length_scale` and `scale_mixture_rate` and inputs to `apply` and `matrix` methods.
• `length_scale`: Positive floating point `Tensor` that controls how sharp or wide the kernel shape is. This provides a characteristic "unit" of length against which `||x - y||` can be compared for scale. Must be broadcastable with `amplitude`, `scale_mixture_rate` and inputs to `apply` and `matrix` methods.
• `scale_mixture_rate`: Positive floating point `Tensor` that controls how the ExponentiatedQuadratic kernels are mixed. Must be broadcastable with `amplitude`, `length_scale` and inputs to `apply` and `matrix` methods.
• `feature_ndims`: Python `int` number of rightmost dims to include in the squared difference norm in the exponential.
• `validate_args`: If `True`, parameters are checked for validity despite possibly degrading runtime performance
• `name`: Python `str` name prefixed to Ops created by this class.

## Properties

### `amplitude`

Amplitude parameter.

### `batch_shape`

The batch_shape property of a PositiveSemidefiniteKernel.

This property describes the fully broadcast shape of all kernel parameters. For example, consider an ExponentiatedQuadratic kernel, which is parameterized by an amplitude and length_scale:

``````exp_quad(x, x') := amplitude * exp(||x - x'||**2 / length_scale**2)
``````

The batch_shape of such a kernel is derived from broadcasting the shapes of `amplitude` and `length_scale`. E.g., if their shapes were

``````amplitude.shape = [2, 1, 1]
length_scale.shape = [1, 4, 3]
``````

then `exp_quad`'s batch_shape would be `[2, 4, 3]`.

Note that this property defers to the private _batch_shape method, which concrete implementation sub-classes are obliged to provide.

#### Returns:

`TensorShape` instance describing the fully broadcast shape of all kernel parameters.

### `dtype`

DType over which the kernel operates.

### `feature_ndims`

The number of feature dimensions.

Kernel functions generally act on pairs of inputs from some space like

``````R^(d1 x ... x  dD)
``````

or, in words: rank-`D` real-valued tensors of shape `[d1, ..., dD]`. Inputs can be vectors in some `R^N`, but are not restricted to be. Indeed, one might consider kernels over matrices, tensors, or even more general spaces, like strings or graphs.

#### Returns:

The number of feature dimensions (feature rank) of this kernel.

### `length_scale`

Length scale parameter.

### `name`

Name prepended to all ops created by this class.

### `scale_mixture_rate`

scale_mixture_rate parameter.

## Methods

### `__add__`

``````__add__(k)
``````

### `__iadd__`

``````__iadd__(k)
``````

### `__imul__`

``````__imul__(k)
``````

### `__mul__`

``````__mul__(k)
``````

### `apply`

``````apply(
x1,
x2
)
``````

Apply the kernel function to a pair of (batches of) inputs.

#### Args:

• `x1`: `Tensor` input to the first positional parameter of the kernel, of shape `[b1, ..., bB, f1, ..., fF]`, where `B` may be zero (ie, no batching) and `F` (number of feature dimensions) must equal the kernel's `feature_ndims` property. Batch shape must broadcast with the batch shape of `x2` and with the kernel's parameters.
• `x2`: `Tensor` input to the second positional parameter of the kernel, shape `[c1, ..., cC, f1, ..., fF]`, where `C` may be zero (ie, no batching) and `F` (number of feature dimensions) must equal the kernel's `feature_ndims` property. Batch shape must broadcast with the batch shape of `x1` and with the kernel's parameters.

#### Returns:

`Tensor` containing the (batch of) results of applying the kernel function to inputs `x1` and `x2`. If the kernel parameters' batch shape is `[k1, ..., kK]` then the shape of the `Tensor` resulting from this method call is `broadcast([b1, ..., bB], [c1, ..., cC], [k1, ..., kK])`.

Given an index set `S`, a kernel function is mathematically defined as a real- or complex-valued function on `S` satisfying the positive semi-definiteness constraint:

``````sum_i sum_j (c[i]*) c[j] k(x[i], x[j]) >= 0
``````

for any finite collections `{x, ..., x[N]}` in `S` and `{c, ..., c[N]}` in the reals (or the complex plane). '*' is the complex conjugate, in the complex case.

This method most closely resembles the function described in the mathematical definition of a kernel. Given a PositiveSemidefiniteKernel `k` with scalar parameters and inputs `x` and `y` in `S`, `apply(x, y)` yields a single scalar value. Given the same kernel and, say, batched inputs of shape `[b1, ..., bB, f1, ..., fF]`, it will yield a batch of scalars of shape `[b1, ..., bB]`.

#### Examples

``````import tensorflow_probability as tfp

# Suppose `SomeKernel` acts on vectors (rank-1 tensors)
scalar_kernel = tfp.positive_semidefinite_kernels.SomeKernel(param=.5)
scalar_kernel.batch_shape
# ==> []

# `x` and `y` are batches of five 3-D vectors:
x = np.ones([5, 3], np.float32)
y = np.ones([5, 3], np.float32)
scalar_kernel.apply(x, y).shape
# ==> 
``````

The above output is the result of vectorized computation of the five values

``````[k(x, y), k(x, y), ..., k(x, y)]
``````

Now we can consider a kernel with batched parameters:

``````batch_kernel = tfp.positive_semidefinite_kernels.SomeKernel(param=[.2, .5])
batch_kernel.batch_shape
# ==> 
batch_kernel.apply(x, y).shape
# ==> Error!  and  can't broadcast.
``````

The parameter batch shape of `` and the input batch shape of `` can't be broadcast together. We can fix this by giving the parameter a shape of `[2, 1]` which will correctly broadcast with `` to yield `[2, 5]`:

``````batch_kernel = tfp.positive_semidefinite_kernels.SomeKernel(
param=[[.2], [.5]])
batch_kernel.batch_shape
# ==> [2, 1]
batch_kernel.apply(x, y).shape
# ==> [2, 5]
``````

### `batch_shape_tensor`

``````batch_shape_tensor()
``````

The batch_shape property of a PositiveSemidefiniteKernel as a `Tensor`.

#### Returns:

`Tensor` which evaluates to a vector of integers which are the fully-broadcast shapes of the kernel parameters.

### `matrix`

``````matrix(
x1,
x2
)
``````

Construct (batched) matrices from (batches of) collections of inputs.

#### Args:

• `x1`: `Tensor` input to the first positional parameter of the kernel, of shape `[b1, ..., bB, e1, f1, ..., fF]`, where `B` may be zero (ie, no batching), e1 is an integer greater than zero, and `F` (number of feature dimensions) must equal the kernel's `feature_ndims` property. Batch shape must broadcast with the batch shape of `x2` and with the kernel's parameters after parameter expansion (see `param_expansion_ndims` argument).
• `x2`: `Tensor` input to the second positional parameter of the kernel, shape `[c1, ..., cC, e2, f1, ..., fF]`, where `C` may be zero (ie, no batching), e2 is an integer greater than zero, and `F` (number of feature dimensions) must equal the kernel's `feature_ndims` property. Batch shape must broadcast with the batch shape of `x1` and with the kernel's parameters after parameter expansion (see `param_expansion_ndims` argument).

#### Returns:

```Tensor containing (batch of) matrices of kernel applications to pairs from inputs```x1`and`x2`. If the kernel parameters' batch shape is`[k1, ..., kK]`, then the shape of the resulting`Tensor`is`broadcast([b1, ..., bB], [c1, ..., cC], [k1, ..., kK]) + [e1, e2]`.

Given inputs `x1` and `x2` of shapes

``````[b1, ..., bB, e1, f1, ..., fF]
``````

and

``````[c1, ..., cC, e2, f1, ..., fF]
``````

This method computes the batch of `e1 x e2` matrices resulting from applying the kernel function to all pairs of inputs from `x1` and `x2`. The shape of the batch of matrices is the result of broadcasting the batch shapes of `x1`, `x2`, and the kernel parameters (see examples below). As such, it's required that these shapes all be broadcast compatible. However, the kernel parameter batch shapes need not broadcast against the 'example shapes' (`e1` and `e2` above).

When the two inputs are the (batches of) identical collections, the resulting matrix is the so-called Gram (or Gramian) matrix (https://en.wikipedia.org/wiki/Gramian_matrix).

N.B., this method can only be used to compute the pairwise application of the kernel function on rank-1 collections. E.g., it does support inputs of shape `[e1, f]` and `[e2, f]`, yielding a matrix of shape `[e1, e2]`. It does not support inputs of shape `[e1, e2, f]` and `[e3, e4, f]`, yielding a `Tensor` of shape `[e1, e2, e3, e4]`. To do this, one should instead reshape the inputs and pass them to `apply`, e.g.:

``````k = psd_kernels.SomeKernel()
t1 = tf.placeholder([4, 4, 3], tf.float32)
t2 = tf.placeholder([5, 5, 3], tf.float32)
k.apply(
tf.reshape(t1, [4, 4, 1, 1, 3]),
tf.reshape(t2, [1, 1, 5, 5, 3])).shape
# ==> [4, 4, 5, 5, 3]
``````

`matrix` is a special case of the above, where there is only one example dimension; indeed, its implementation looks almost exactly like the above (reshaped inputs passed to the private version of `_apply`).

#### Examples

First, consider a kernel with a single scalar parameter.

``````import tensorflow_probability as tfp

scalar_kernel = tfp.positive_semidefinite_kernels.SomeKernel(param=.5)
scalar_kernel.batch_shape
# ==> []

# Our inputs are two lists of 3-D vectors
x = np.ones([5, 3], np.float32)
y = np.ones([4, 3], np.float32)
scalar_kernel.matrix(x, y).shape
# ==> [5, 4]
``````

The result comes from applying the kernel to the entries in `x` and `y` pairwise, across all pairs:

``````| k(x, y)    k(x, y)  ...  k(x, y) |
| k(x, y)    k(x, y)  ...  k(x, y) |
|      ...              ...                 ...      |
| k(x, y)    k(x, y)  ...  k(x, y) |
``````

Now consider a kernel with batched parameters with the same inputs

``````batch_kernel = tfp.positive_semidefinite_kernels.SomeKernel(param=[1., .5])
batch_kernel.batch_shape
# ==> 

batch_kernel.matrix(x, y).shape
# ==> [2, 5, 4]
``````

This results in a batch of 2 matrices, one computed from the kernel with `param = 1.` and the other with `param = .5`.

We also support batching of the inputs. First, let's look at that with the scalar kernel again.

``````# Batch of 10 lists of 5 vectors of dimension 3
x = np.ones([10, 5, 3], np.float32)

# Batch of 10 lists of 4 vectors of dimension 3
y = np.ones([10, 4, 3], np.float32)

scalar_kernel.matrix(x, y).shape
# ==> [10, 5, 4]
``````

The result is a batch of 10 matrices built from the batch of 10 lists of input vectors. These batch shapes have to be broadcastable. The following will not work:

``````x = np.ones([10, 5, 3], np.float32)
y = np.ones([20, 4, 3], np.float32)
scalar_kernel.matrix(x, y).shape
# ==> Error!  and  can't broadcast.
``````

Now let's consider batches of inputs in conjunction with batches of kernel parameters. We require that the input batch shapes be broadcastable with the kernel parameter batch shapes, otherwise we get an error:

``````x = np.ones([10, 5, 3], np.float32)
y = np.ones([10, 4, 3], np.float32)

batch_kernel = tfp.positive_semidefinite_kernels.SomeKernel(params=[1., .5])
batch_kernel.batch_shape
# ==> 
batch_kernel.matrix(x, y).shape
# ==> Error!  and  can't broadcast.
``````

The fix is to make the kernel parameter shape broadcastable with `` (or reshape the inputs to be broadcastable!):

``````x = np.ones([10, 5, 3], np.float32)
y = np.ones([10, 4, 3], np.float32)

batch_kernel = tfp.positive_semidefinite_kernels.SomeKernel(
params=[[1.], [.5]])
batch_kernel.batch_shape
# ==> [2, 1]
batch_kernel.matrix(x, y).shape
# ==> [2, 10, 5, 4]

# Or, make the inputs broadcastable:
x = np.ones([10, 1, 5, 3], np.float32)
y = np.ones([10, 1, 4, 3], np.float32)

batch_kernel = tfp.positive_semidefinite_kernels.SomeKernel(
params=[1., .5])
batch_kernel.batch_shape
# ==> 
batch_kernel.matrix(x, y).shape
# ==> [10, 2, 5, 4]

``````

Here, we have the result of applying the kernel, with 2 different parameters, to each of a batch of 10 pairs of input lists.