tfp.substrates.numpy.math.batch_interp_regular_nd_grid
Stay organized with collections
Save and categorize content based on your preferences.
Multi-linear interpolation on a regular (constant spacing) grid.
tfp.substrates.numpy.math.batch_interp_regular_nd_grid(
x,
x_ref_min,
x_ref_max,
y_ref,
axis,
fill_value='constant_extension',
name=None
)
Given [a batch of] reference values, this function computes a multi-linear
interpolant and evaluates it on [a batch of] of new x
values. This is a
multi-dimensional generalization of Bilinear Interpolation.
The interpolant is built from reference values indexed by nd
dimensions
of y_ref
, starting at axis
.
The x grid span is defined by x_ref_min
, x_ref_max
. The number of grid
points is inferred from the shape of y_ref
.
For example, take the case of a 2-D
scalar valued function and no leading
batch dimensions. In this case, y_ref.shape = [C1, C2]
and y_ref[i, j]
is the reference value corresponding to grid point
[x_ref_min[0] + i * (x_ref_max[0] - x_ref_min[0]) / (C1 - 1),
x_ref_min[1] + j * (x_ref_max[1] - x_ref_min[1]) / (C2 - 1)]
In the general case, dimensions to the left of axis
in y_ref
are broadcast
with leading dimensions in x
, x_ref_min
, x_ref_max
.
Args |
x
|
Numeric Tensor The x-coordinates of the interpolated output values for
each batch. Shape [..., D, nd] , designating [a batch of] D
coordinates in nd space. D must be >= 1 and is not a batch dim.
|
x_ref_min
|
Tensor of same dtype as x . The minimum values of the
(implicitly defined) reference x_ref . Shape [..., nd] .
|
x_ref_max
|
Tensor of same dtype as x . The maximum values of the
(implicitly defined) reference x_ref . Shape [..., nd] .
|
y_ref
|
Tensor of same dtype as x . The reference output values. Shape
[..., C1, ..., Cnd, B1,...,BM] , designating [a batch of] reference
values indexed by nd dimensions, of a shape [B1,...,BM] valued
function (for M >= 0 ).
|
axis
|
Scalar integer Tensor . Dimensions [axis, axis + nd) of y_ref
index the interpolation table. E.g. 3-D interpolation of a scalar
valued function requires axis=-3 and a 3-D matrix valued function
requires axis=-5 .
|
fill_value
|
Determines what values output should take for x values that
are below x_ref_min or above x_ref_max . Scalar Tensor or
'constant_extension' ==> Extend as constant function.
Default value: 'constant_extension'
|
name
|
A name to prepend to created ops.
Default value: 'batch_interp_regular_nd_grid' .
|
Returns |
y_interp
|
Interpolation between members of y_ref , at points x .
Tensor of same dtype as x , and shape [..., D, B1, ..., BM].
|
Exceptions will be raised if shapes are statically determined to be wrong.
Raises |
ValueError
|
If rank(x) < 2 .
|
ValueError
|
If axis is not a scalar.
|
ValueError
|
If axis + nd > rank(y_ref) .
|
Examples
Interpolate a function of one variable.
y_ref = tf.exp(tf.linspace(start=0., stop=10., num=20))
tfp.math.batch_interp_regular_nd_grid(
# x.shape = [3, 1], x_ref_min/max.shape = [1]. Trailing `1` for `1-D`.
x=[[6.0], [0.5], [3.3]], x_ref_min=[0.], x_ref_max=[10.], y_ref=y_ref,
axis=0)
==> approx [exp(6.0), exp(0.5), exp(3.3)]
Interpolate a scalar function of two variables.
x_ref_min = [0., 0.]
x_ref_max = [2 * np.pi, 2 * np.pi]
# Build y_ref.
x0s, x1s = tf.meshgrid(
tf.linspace(x_ref_min[0], x_ref_max[0], num=100),
tf.linspace(x_ref_min[1], x_ref_max[1], num=100),
indexing='ij')
def func(x0, x1):
return tf.sin(x0) * tf.cos(x1)
y_ref = func(x0s, x1s)
x = 2 * np.pi * tf.random.stateless_uniform(shape=(10, 2))
tfp.math.batch_interp_regular_nd_grid(x, x_ref_min, x_ref_max, y_ref, axis=-2)
==> tf.sin(x[:, 0]) * tf.cos(x[:, 1])
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-11-21 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-11-21 UTC."],[],[],null,["# tfp.substrates.numpy.math.batch_interp_regular_nd_grid\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/probability/blob/v0.23.0/tensorflow_probability/substrates/numpy/math/interpolation.py#L487-L686) |\n\nMulti-linear interpolation on a regular (constant spacing) grid.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tfp.experimental.substrates.numpy.math.batch_interp_regular_nd_grid`](https://www.tensorflow.org/probability/api_docs/python/tfp/substrates/numpy/math/batch_interp_regular_nd_grid)\n\n\u003cbr /\u003e\n\n tfp.substrates.numpy.math.batch_interp_regular_nd_grid(\n x,\n x_ref_min,\n x_ref_max,\n y_ref,\n axis,\n fill_value='constant_extension',\n name=None\n )\n\nGiven \\[a batch of\\] reference values, this function computes a multi-linear\ninterpolant and evaluates it on \\[a batch of\\] of new `x` values. This is a\nmulti-dimensional generalization of [Bilinear Interpolation](https://en.wikipedia.org/wiki/Bilinear_interpolation).\n\nThe interpolant is built from reference values indexed by `nd` dimensions\nof `y_ref`, starting at `axis`.\n\nThe x grid span is defined by `x_ref_min`, `x_ref_max`. The number of grid\npoints is inferred from the shape of `y_ref`.\n\nFor example, take the case of a `2-D` scalar valued function and no leading\nbatch dimensions. In this case, `y_ref.shape = [C1, C2]` and `y_ref[i, j]`\nis the reference value corresponding to grid point \n\n [x_ref_min[0] + i * (x_ref_max[0] - x_ref_min[0]) / (C1 - 1),\n x_ref_min[1] + j * (x_ref_max[1] - x_ref_min[1]) / (C2 - 1)]\n\nIn the general case, dimensions to the left of `axis` in `y_ref` are broadcast\nwith leading dimensions in `x`, `x_ref_min`, `x_ref_max`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `x` | Numeric `Tensor` The x-coordinates of the interpolated output values for each batch. Shape `[..., D, nd]`, designating \\[a batch of\\] `D` coordinates in `nd` space. `D` must be `\u003e= 1` and is not a batch dim. |\n| `x_ref_min` | `Tensor` of same `dtype` as `x`. The minimum values of the (implicitly defined) reference `x_ref`. Shape `[..., nd]`. |\n| `x_ref_max` | `Tensor` of same `dtype` as `x`. The maximum values of the (implicitly defined) reference `x_ref`. Shape `[..., nd]`. |\n| `y_ref` | `Tensor` of same `dtype` as `x`. The reference output values. Shape `[..., C1, ..., Cnd, B1,...,BM]`, designating \\[a batch of\\] reference values indexed by `nd` dimensions, of a shape `[B1,...,BM]` valued function (for `M \u003e= 0`). |\n| `axis` | Scalar integer `Tensor`. Dimensions `[axis, axis + nd)` of `y_ref` index the interpolation table. E.g. `3-D` interpolation of a scalar valued function requires `axis=-3` and a `3-D` matrix valued function requires `axis=-5`. |\n| `fill_value` | Determines what values output should take for `x` values that are below `x_ref_min` or above `x_ref_max`. Scalar `Tensor` or 'constant_extension' ==\\\u003e Extend as constant function. Default value: `'constant_extension'` |\n| `name` | A name to prepend to created ops. Default value: `'batch_interp_regular_nd_grid'`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|------------|------------------------------------------------------------------------------------------------------------------------------|\n| `y_interp` | Interpolation between members of `y_ref`, at points `x`. `Tensor` of same `dtype` as `x`, and shape `[..., D, B1, ..., BM].` |\n\n\u003cbr /\u003e\n\nExceptions will be raised if shapes are statically determined to be wrong.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-------------------------------|\n| `ValueError` | If `rank(x) \u003c 2`. |\n| `ValueError` | If `axis` is not a scalar. |\n| `ValueError` | If `axis + nd \u003e rank(y_ref)`. |\n\n\u003cbr /\u003e\n\n#### Examples\n\nInterpolate a function of one variable. \n\n y_ref = tf.exp(tf.linspace(start=0., stop=10., num=20))\n\n tfp.math.batch_interp_regular_nd_grid(\n # x.shape = [3, 1], x_ref_min/max.shape = [1]. Trailing `1` for `1-D`.\n x=[[6.0], [0.5], [3.3]], x_ref_min=[0.], x_ref_max=[10.], y_ref=y_ref,\n axis=0)\n ==\u003e approx [exp(6.0), exp(0.5), exp(3.3)]\n\nInterpolate a scalar function of two variables. \n\n x_ref_min = [0., 0.]\n x_ref_max = [2 * np.pi, 2 * np.pi]\n\n # Build y_ref.\n x0s, x1s = tf.meshgrid(\n tf.linspace(x_ref_min[0], x_ref_max[0], num=100),\n tf.linspace(x_ref_min[1], x_ref_max[1], num=100),\n indexing='ij')\n\n def func(x0, x1):\n return tf.sin(x0) * tf.cos(x1)\n\n y_ref = func(x0s, x1s)\n\n x = 2 * np.pi * tf.random.stateless_uniform(shape=(10, 2))\n\n tfp.math.batch_interp_regular_nd_grid(x, x_ref_min, x_ref_max, y_ref, axis=-2)\n ==\u003e tf.sin(x[:, 0]) * tf.cos(x[:, 1])"]]