_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q266800 | _tensor_product | test | def _tensor_product(t1, t2):
"""Computes the outer product of two possibly batched vectors.
Args:
t1: A `tf.Tensor` of shape `[..., n]`.
t2: A `tf.Tensor` of shape `[..., m]`.
| python | {
"resource": ""
} |
q266801 | _batch_transpose | test | def _batch_transpose(mat):
"""Transpose a possibly batched matrix.
Args:
mat: A `tf.Tensor` of shape `[..., n, m]`.
Returns:
A tensor of shape `[..., m, n]` with matching batch dimensions.
"""
n = distribution_util.prefer_static_rank(mat)
| python | {
"resource": ""
} |
q266802 | pad_shape_right_with_ones | test | def pad_shape_right_with_ones(x, ndims):
"""Maybe add `ndims` ones to `x.shape` on the right.
If `ndims` is zero, this is a no-op; otherwise, we will create and return a
new `Tensor` whose shape is that of `x` with `ndims` ones concatenated on the
right side. If the shape of `x` is known statically, the shape of the return
value will be as well.
Args:
x: The `Tensor` we'll return a reshaping of.
ndims: Python `integer` number of ones to pad onto `x.shape`.
Returns:
If `ndims` is zero, `x`; otherwise, a `Tensor` whose shape is that of `x`
with `ndims` ones concatenated on the right side. If possible, returns a
`Tensor` whose shape is known statically.
Raises:
ValueError: if `ndims` is not a Python `integer` greater than or equal to
zero.
"""
if not (isinstance(ndims, int) and | python | {
"resource": ""
} |
q266803 | sum_rightmost_ndims_preserving_shape | test | def sum_rightmost_ndims_preserving_shape(x, ndims):
"""Return `Tensor` with right-most ndims summed.
Args:
x: the `Tensor` whose right-most `ndims` dimensions to sum
ndims: number of right-most dimensions to sum.
Returns:
A `Tensor` resulting from calling `reduce_sum` on the `ndims` right-most
dimensions. If the shape of `x` is statically known, the result will also
have statically known shape. Otherwise, the resulting shape will only be
| python | {
"resource": ""
} |
q266804 | sqrt_with_finite_grads | test | def sqrt_with_finite_grads(x, name=None):
"""A sqrt function whose gradient at zero is very large but finite.
Args:
x: a `Tensor` whose sqrt is to be computed.
name: a Python `str` prefixed to all ops created by this function.
Default `None` (i.e., "sqrt_with_finite_grads").
Returns:
sqrt: the square root of `x`, with an overridden gradient at zero
grad: a gradient function, which is the same as sqrt's gradient everywhere
except at zero, where it is given a large finite value, instead of `inf`.
Raises:
TypeError: if `tf.convert_to_tensor(x)` is not a `float` type.
Often in kernel functions, we need to compute the L2 norm of the difference
between two vectors, `x` and `y`: `sqrt(sum_i((x_i - y_i) ** 2))`. In the
case where `x` and `y` are identical, e.g., on the diagonal of a kernel
matrix, we get `NaN`s when we take gradients with respect to the inputs. To
see, this consider the forward pass:
```
[x_1 ... x_N] --> [x_1 ** 2 ... x_N ** 2] -->
(x_1 ** 2 + ... + x_N ** 2) --> sqrt((x_1 ** 2 + ... + x_N ** 2))
```
When we backprop through this forward pass, the `sqrt` yields an `inf` because
`grad_z(sqrt(z)) = 1 / (2 * sqrt(z))`. Continuing the backprop to the left, at
the `x ** 2` term, we pick up a `2 * x`, and when `x` is zero, we get
`0 * inf`, which is `NaN`.
We'd like to avoid these `NaN`s, since they infect the rest of the connected
computation graph. Practically, when two inputs to a kernel function are
equal, we are in one of two scenarios:
1. We are actually computing k(x, x), in which case norm(x - x) is
identically zero, independent of x. In this case, we'd like the
gradient to reflect this independence: it should be zero.
2. We are computing k(x, y), and x just *happens* to have the same value
as y. The gradient at such inputs is in fact ill-defined (there is a
| python | {
"resource": ""
} |
q266805 | maybe_get_common_dtype | test | def maybe_get_common_dtype(arg_list):
"""Return common dtype of arg_list, or None.
Args:
arg_list: an iterable of items which are either `None` or have a `dtype`
property.
Returns:
dtype: The common dtype of items in `arg_list`, or `None` if the list is
empty or all items are `None`.
"""
# | python | {
"resource": ""
} |
q266806 | minimize | test | def minimize(value_and_gradients_function,
initial_position,
num_correction_pairs=10,
tolerance=1e-8,
x_tolerance=0,
f_relative_tolerance=0,
initial_inverse_hessian_estimate=None,
max_iterations=50,
parallel_iterations=1,
stopping_condition=None,
name=None):
"""Applies the L-BFGS algorithm to minimize a differentiable function.
Performs unconstrained minimization of a differentiable function using the
L-BFGS scheme. See [Nocedal and Wright(2006)][1] for details of the algorithm.
### Usage:
The following example demonstrates the L-BFGS optimizer attempting to find the
minimum for a simple high-dimensional quadratic objective function.
```python
# A high-dimensional quadratic bowl.
ndims = 60
minimum = np.ones([ndims], dtype='float64')
scales = np.arange(ndims, dtype='float64') + 1.0
# The objective function and the gradient.
def quadratic(x):
value = tf.reduce_sum(scales * (x - minimum) ** 2)
return value, tf.gradients(value, x)[0]
start = np.arange(ndims, 0, -1, dtype='float64')
optim_results = tfp.optimizer.lbfgs_minimize(
quadratic, initial_position=start, num_correction_pairs=10,
tolerance=1e-8)
with tf.Session() as session:
results = session.run(optim_results)
# Check that the search converged
assert(results.converged)
# Check that the argmin is close to the actual value.
np.testing.assert_allclose(results.position, minimum)
```
### References:
[1] Jorge Nocedal, Stephen Wright. Numerical Optimization. Springer Series
in Operations Research. pp 176-180. 2006
http://pages.mtu.edu/~struther/Courses/OLD/Sp2013/5630/Jorge_Nocedal_Numerical_optimization_267490.pdf
Args:
value_and_gradients_function: A Python callable that accepts a point as a
real `Tensor` and returns a tuple of `Tensor`s of real dtype containing
the value of the function and its gradient at that point. The function
to be minimized. The input is of shape `[..., n]`, where `n` is the size
of the domain of input points, and all others are batching dimensions.
The first component of the return value is a real `Tensor` of matching
shape `[...]`. The second component (the gradient) is also of shape
`[..., n]` like the input value to the function.
initial_position: Real `Tensor` of shape `[..., n]`. The starting point, or
points when using batching dimensions, of the search procedure. At these
points the function value and the gradient norm should be finite.
num_correction_pairs: Positive integer. Specifies the maximum number of
(position_delta, gradient_delta) correction pairs to keep as implicit
approximation of the Hessian matrix.
tolerance: Scalar `Tensor` of real dtype. Specifies the gradient tolerance
for the procedure. If the supremum norm of the gradient vector is below
this number, the algorithm is stopped.
x_tolerance: Scalar `Tensor` of real dtype. If the absolute change in the
position between one iteration and the next is smaller than this number,
the algorithm is stopped.
f_relative_tolerance: Scalar `Tensor` of real dtype. If the relative change
in the objective value between one iteration and the next is smaller
than this value, the algorithm is stopped.
initial_inverse_hessian_estimate: None. Option currently not supported.
max_iterations: Scalar positive int32 `Tensor`. The maximum number of
iterations for L-BFGS updates.
parallel_iterations: Positive integer. The number of iterations allowed to
run in parallel.
stopping_condition: (Optional) A Python function that takes as input two
Boolean tensors of shape `[...]`, and returns a Boolean scalar tensor.
The input tensors are `converged` and `failed`, indicating the current
status of each respective batch member; the return value states whether
the algorithm should stop. The default is tfp.optimizer.converged_all
which only stops when all batch members have either converged or failed.
An alternative is tfp.optimizer.converged_any which stops as soon as one
batch member has converged, or when all have failed.
name: (Optional) Python str. The name prefixed to the ops created by this
function. If not supplied, the default name 'minimize' is used.
Returns:
optimizer_results: A namedtuple containing the following items:
converged: Scalar boolean tensor indicating whether the minimum was
found within tolerance.
failed: Scalar boolean tensor indicating whether a line search
step failed to find a suitable step size satisfying Wolfe
conditions. In the absence of any constraints on the
number of objective evaluations permitted, this value will
be the complement of `converged`. However, if there is
a constraint and the search stopped due to available
evaluations being exhausted, both `failed` and `converged`
will be simultaneously False.
num_objective_evaluations: The total number of objective
evaluations performed.
position: A tensor containing the last argument value found
during the search. If the search converged, then
this value is the argmin of the objective function.
objective_value: A tensor containing the value of the objective
function at the `position`. If the search converged, then this is
the (local) minimum of the objective function.
objective_gradient: A tensor containing the gradient of the objective
function at the `position`. If the search converged the
max-norm of this tensor should be below the tolerance.
position_deltas: A tensor encoding information about the latest
changes in `position` during the algorithm execution.
gradient_deltas: A tensor encoding information about the latest
| python | {
"resource": ""
} |
q266807 | _get_initial_state | test | def _get_initial_state(value_and_gradients_function,
initial_position,
num_correction_pairs,
tolerance):
"""Create LBfgsOptimizerResults with initial state of search procedure."""
| python | {
"resource": ""
} |
q266808 | _get_search_direction | test | def _get_search_direction(state):
"""Computes the search direction to follow at the current state.
On the `k`-th iteration of the main L-BFGS algorithm, the state has collected
the most recent `m` correction pairs in position_deltas and gradient_deltas,
where `k = state.num_iterations` and `m = min(k, num_correction_pairs)`.
Assuming these, the code below is an implementation of the L-BFGS two-loop
recursion algorithm given by [Nocedal and Wright(2006)][1]:
```None
q_direction = objective_gradient
for i in reversed(range(m)): # First loop.
inv_rho[i] = gradient_deltas[i]^T * position_deltas[i]
alpha[i] = position_deltas[i]^T * q_direction / inv_rho[i]
q_direction = q_direction - alpha[i] * gradient_deltas[i]
kth_inv_hessian_factor = (gradient_deltas[-1]^T * position_deltas[-1] /
gradient_deltas[-1]^T * gradient_deltas[-1])
r_direction = kth_inv_hessian_factor * I * q_direction
for i in range(m): # Second loop.
beta = gradient_deltas[i]^T * r_direction / inv_rho[i]
r_direction = r_direction + position_deltas[i] * (alpha[i] - beta)
return -r_direction # Approximates - H_k * objective_gradient.
```
Args:
state: A `LBfgsOptimizerResults` tuple with the current state of the
search procedure.
Returns:
A real `Tensor` of the same shape as the `state.position`. The direction
along which to perform line search.
"""
# The number of correction pairs that have been collected so far.
num_elements = tf.minimum(
state.num_iterations,
distribution_util.prefer_static_shape(state.position_deltas)[0])
def _two_loop_algorithm():
"""L-BFGS two-loop algorithm."""
# Correction pairs are always appended to the end, so only the latest
# `num_elements` vectors have valid position/gradient deltas.
position_deltas = state.position_deltas[-num_elements:]
gradient_deltas = state.gradient_deltas[-num_elements:]
# Pre-compute all `inv_rho[i]`s.
inv_rhos = tf.reduce_sum(
input_tensor=gradient_deltas * position_deltas, axis=-1)
def first_loop(acc, args):
_, q_direction = acc
position_delta, gradient_delta, inv_rho = args
alpha = tf.reduce_sum(
input_tensor=position_delta * | python | {
"resource": ""
} |
q266809 | _make_empty_queue_for | test | def _make_empty_queue_for(k, element):
"""Creates a `tf.Tensor` suitable to hold `k` element-shaped tensors.
For example:
```python
element = tf.constant([[0., 1., 2., 3., 4.],
[5., 6., 7., 8., 9.]])
# A queue capable of holding 3 elements.
_make_empty_queue_for(3, element)
# => [[[ 0., 0., 0., 0., 0.],
# [ 0., 0., 0., 0., 0.]],
#
# [[ 0., 0., 0., 0., 0.],
# [ 0., 0., 0., 0., 0.]],
#
# [[ 0., 0., 0., 0., 0.],
# | python | {
"resource": ""
} |
q266810 | _queue_push | test | def _queue_push(queue, should_update, new_vecs):
"""Conditionally push new vectors into a batch of first-in-first-out queues.
The `queue` of shape `[k, ..., n]` can be thought of as a batch of queues,
each holding `k` n-D vectors; while `new_vecs` of shape `[..., n]` is a
fresh new batch of n-D vectors. The `should_update` batch of Boolean scalars,
i.e. shape `[...]`, indicates batch members whose corresponding n-D vector in
`new_vecs` should be added at the back of its queue, pushing out the
corresponding n-D vector from the front. Batch members in `new_vecs` for
which `should_update` is False are ignored.
Note: the choice of placing `k` at the dimension 0 of the queue is
constrained by the L-BFGS two-loop algorithm above. The algorithm uses
tf.scan to iterate over the `k` correction pairs simulatneously across all
batches, and tf.scan itself can only iterate over dimension 0.
For example:
```python
k, b, n = (3, 2, 5)
queue = tf.reshape(tf.range(30), (k, b, n))
| python | {
"resource": ""
} |
q266811 | _psd_mask | test | def _psd_mask(x):
"""Computes whether each square matrix in the input is positive semi-definite.
Args:
x: A floating-point `Tensor` of shape `[B1, ..., Bn, M, M]`.
Returns:
mask: A floating-point `Tensor` of shape `[B1, ... Bn]`. Each
scalar is 1 if the corresponding matrix was PSD, otherwise 0.
"""
# Allegedly
# https://scicomp.stackexchange.com/questions/12979/testing-if-a-matrix-is-positive-semi-definite
# it is more efficient to test for positive semi-definiteness by
# trying to compute the Cholesky decomposition -- the matrix is PSD
# if you succeed and not PSD if you fail. However, TensorFlow's
# Cholesky raises an exception if _any_ of the input matrices are
# not PSD, from which I don't know how to extract _which ones_, so I
# proceed by explicitly computing all the eigenvalues and checking
# whether they are all positive or | python | {
"resource": ""
} |
q266812 | _det_large_enough_mask | test | def _det_large_enough_mask(x, det_bounds):
"""Returns whether the input matches the given determinant limit.
Args:
x: A floating-point `Tensor` of shape `[B1, ..., Bn, M, M]`.
det_bounds: A floating-point `Tensor` that must broadcast to shape
`[B1, ..., Bn]`, giving the desired lower bound on the
determinants in `x`.
Returns:
mask: A floating-point `Tensor` of shape [B1, ..., Bn]. Each
scalar is 1 if the corresponding | python | {
"resource": ""
} |
q266813 | _uniform_correlation_like_matrix | test | def _uniform_correlation_like_matrix(num_rows, batch_shape, dtype, seed):
"""Returns a uniformly random `Tensor` of "correlation-like" matrices.
A "correlation-like" matrix is a symmetric square matrix with all entries
between -1 and 1 (inclusive) and 1s on the main diagonal. Of these,
the ones that are positive semi-definite are exactly the correlation
matrices.
Args:
num_rows: Python `int` dimension of the correlation-like matrices.
batch_shape: `Tensor` or Python `tuple` of `int` shape of the
batch to return.
dtype: `dtype` of the `Tensor` to return.
seed: Random seed.
Returns:
matrices: A `Tensor` of shape `batch_shape + [num_rows, num_rows]`
and dtype `dtype`. Each entry is in [-1, 1], and each matrix
along the bottom two dimensions is symmetric and has 1s on the
main diagonal.
"""
num_entries = num_rows * (num_rows + 1) / 2
ones = tf.ones(shape=[num_entries], dtype=dtype)
# It seems wasteful to | python | {
"resource": ""
} |
q266814 | correlation_matrix_volume_rejection_samples | test | def correlation_matrix_volume_rejection_samples(
det_bounds, dim, sample_shape, dtype, seed):
"""Returns rejection samples from trying to get good correlation matrices.
The proposal being rejected from is the uniform distribution on
"correlation-like" matrices. We say a matrix is "correlation-like"
if it is a symmetric square matrix with all entries between -1 and 1
(inclusive) and 1s on the main diagonal. Of these, the ones that
are positive semi-definite are exactly the correlation matrices.
The rejection algorithm, then, is to sample a `Tensor` of
`sample_shape` correlation-like matrices of dimensions `dim` by
`dim`, and check each one for (i) being a correlation matrix (i.e.,
PSD), and (ii) having determinant at least the corresponding entry
of `det_bounds`.
Args:
det_bounds: A `Tensor` of lower bounds on the determinants of
acceptable matrices. The shape must broadcast with `sample_shape`.
dim: A Python `int` dimension of correlation matrices to sample.
sample_shape: Python `tuple` of `int` shape of the samples to
compute, excluding the two matrix dimensions.
dtype: The `dtype` in which to do the computation.
seed: Random seed.
Returns:
weights: A `Tensor` of shape `sample_shape`. Each entry is 0 if the
corresponding matrix was not a correlation matrix, or had too
small of a determinant. Otherwise, the entry is the
multiplicative inverse of the density of proposing that matrix
uniformly, i.e., the volume | python | {
"resource": ""
} |
q266815 | _clopper_pearson_confidence_interval | test | def _clopper_pearson_confidence_interval(samples, error_rate):
"""Computes a confidence interval for the mean of the given 1-D distribution.
Assumes (and checks) that the given distribution is Bernoulli, i.e.,
takes only two values. This licenses using the CDF of the binomial
distribution for the confidence, which is tighter (for extreme
probabilities) than the DKWM inequality. The method is known as the
[Clopper-Pearson method]
(https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval).
Assumes:
- The given samples were drawn iid from the distribution of interest.
- The given distribution is a Bernoulli, i.e., supported only on
low and high.
Guarantees:
- The probability (over the randomness of drawing the given sample)
that the true mean is outside the returned interval is no more
than the given error_rate.
Args:
samples: `np.ndarray` of samples drawn iid from the distribution
of interest.
error_rate: Python `float` admissible rate of mistakes.
Returns:
low: Lower bound of confidence interval.
high: Upper bound of confidence interval.
Raises:
ValueError: If `samples` has rank other than 1 (batch semantics
are not implemented), or if `samples` contains values other than
`low` or `high` (as that makes the distribution not Bernoulli).
"""
# TODO(b/78025336) Migrate this confidence interval function
# to statistical_testing.py. In order to do that
# - Get the binomial CDF from the Binomial distribution
# - Implement scalar root finding in TF. Batch bisection search
# shouldn't be too hard, and is definitely good enough for this
# problem. Batching the Brent algorithm (from scipy) that is used
# here may be more involved, but may also not be necessary---it's
# only used here because scipy made it convenient. In particular,
# robustness is more important than speed here, which may make
| python | {
"resource": ""
} |
q266816 | compute_true_volumes | test | def compute_true_volumes(
det_bounds, dim, num_samples, error_rate=1e-6, seed=42):
"""Returns confidence intervals for the desired correlation matrix volumes.
The confidence intervals are computed by the [Clopper-Pearson method]
(https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval).
Args:
det_bounds: A rank-1 numpy array of lower bounds on the
determinants of acceptable matrices. Entries must be unique.
dim: A Python `int` dimension of correlation matrices to sample.
num_samples: The number of samples to draw.
error_rate: The statistical significance of the returned
confidence intervals. The significance is broadcast: Each
returned interval separately may be incorrect with probability
(under the sample of correlation-like matrices drawn internally)
at most `error_rate`.
seed: Random seed.
Returns:
bounds: A Python `dict` mapping each determinant bound to the low, high
| python | {
"resource": ""
} |
q266817 | _von_mises_cdf_series | test | def _von_mises_cdf_series(x, concentration, num_terms, dtype):
"""Computes the von Mises CDF and its derivative via series expansion."""
# Keep the number of terms as a float. It should be a small integer, so
# exactly representable as a float.
num_terms = tf.cast(num_terms, dtype=dtype)
def loop_body(n, rn, drn_dconcentration, vn, dvn_dconcentration):
"""One iteration of the series loop."""
denominator = 2. * n / concentration + rn
ddenominator_dk = -2. * n / concentration ** 2 + drn_dconcentration
rn = 1. / denominator
drn_dconcentration = -ddenominator_dk / denominator ** 2
multiplier = tf.sin(n * x) / n + vn
vn = rn * multiplier
dvn_dconcentration = (drn_dconcentration * multiplier +
rn * dvn_dconcentration)
n -= 1.
return n, rn, drn_dconcentration, vn, dvn_dconcentration
(_, _, _, vn, dvn_dconcentration) = tf.while_loop(
cond=lambda n, *_: n > 0.,
body=loop_body,
loop_vars=(
num_terms, # n
tf.zeros_like(x, name="rn"),
tf.zeros_like(x, name="drn_dconcentration"), | python | {
"resource": ""
} |
q266818 | _von_mises_cdf_normal | test | def _von_mises_cdf_normal(x, concentration, dtype):
"""Computes the von Mises CDF and its derivative via Normal approximation."""
def cdf_func(concentration):
"""A helper function that is passed to value_and_gradient."""
# z is an "almost Normally distributed" random variable.
z = ((np.sqrt(2. / np.pi) / tf.math.bessel_i0e(concentration)) *
tf.sin(.5 * x))
# This is the correction described in [1] which reduces the error
# of the Normal approximation.
z2 = z ** 2
z3 = z2 * z
z4 = z2 ** 2
c = 24. | python | {
"resource": ""
} |
q266819 | one_step | test | def one_step(
objective_function,
population,
population_values=None,
differential_weight=0.5,
crossover_prob=0.9,
seed=None,
name=None):
"""Performs one step of the differential evolution algorithm.
Args:
objective_function: A Python callable that accepts a batch of possible
solutions and returns the values of the objective function at those
arguments as a rank 1 real `Tensor`. This specifies the function to be
minimized. The input to this callable may be either a single `Tensor`
or a Python `list` of `Tensor`s. The signature must match the format of
the argument `population`. (i.e. objective_function(*population) must
return the value of the function to be minimized).
population: `Tensor` or Python `list` of `Tensor`s representing the
current population vectors. Each `Tensor` must be of the same real dtype.
The first dimension indexes individual population members while the
rest of the dimensions are consumed by the value function. For example,
if the population is a single `Tensor` of shape [n, m1, m2], then `n` is
the population size and the output of `objective_function` applied to the
population is a `Tensor` of shape [n]. If the population is a python
list of `Tensor`s then each `Tensor` in the list should have the first
axis of a common size, say `n` and `objective_function(*population)`
should return a `Tensor of shape [n]. The population must have at least
4 members for the algorithm to work correctly.
population_values: A `Tensor` of rank 1 and real dtype. The result of
applying `objective_function` to the `population`. If not supplied it is
computed using the `objective_function`.
Default value: None.
differential_weight: Real scalar `Tensor`. Must be positive and less than
2.0. The parameter controlling the strength of mutation.
Default value: 0.5
crossover_prob: Real scalar `Tensor`. Must be between 0 and 1. The
probability of recombination per site.
Default value: 0.9
seed: `int` or None. The random seed for this `Op`. If `None`, no seed is
applied.
Default value: None.
name: (Optional) Python str. The name prefixed to the ops created by this
function. If not supplied, the default name 'one_step' is
used.
Default value: None
Returns:
A sequence containing the following elements (in order):
next_population: A `Tensor` or Python `list` of `Tensor`s of the same
structure as the input population. The population at the next generation.
next_population_values: A `Tensor` of same shape and dtype as input
`population_values`. The function values for the `next_population`.
"""
with tf.compat.v1.name_scope(
name, 'one_step',
[population, population_values, differential_weight, crossover_prob]):
population, _ = _ensure_list(population)
if population_values is None:
population_values = objective_function(*population)
population_size = tf.shape(input=population[0])[0]
seed_stream = | python | {
"resource": ""
} |
q266820 | minimize | test | def minimize(objective_function,
initial_population=None,
initial_position=None,
population_size=50,
population_stddev=1.,
max_iterations=100,
func_tolerance=0,
position_tolerance=1e-8,
differential_weight=0.5,
crossover_prob=0.9,
seed=None,
name=None):
"""Applies the Differential evolution algorithm to minimize a function.
Differential Evolution is an evolutionary optimization algorithm which works
on a set of candidate solutions called the population. It iteratively
improves the population by applying genetic operators of mutation and
recombination. The objective function `f` supplies the fitness of each
candidate. A candidate `s_1` is considered better than `s_2` if
`f(s_1) < f(s_2)`.
This method allows the user to either specify an initial population or a
single candidate solution. If a single solution is specified, a population
of the specified size is initialized by adding independent normal noise
to the candidate solution.
The implementation also supports a multi-part specification of the state. For
example, consider the objective function:
```python
# x is a tensor of shape [n, m] while y is of shape [n].
def objective(x, y):
return tf.math.reduce_sum(x ** 2, axis=-1) + y ** 2
```
The state in this case is specified by two input tensors `x` and `y`. To
apply the algorithm to this objective function, one would need to specify
either an initial population as a list of two tensors of shapes
`[population_size, k]` and `[population_size]`. The following code shows the
complete example:
```python
population_size = 40
# With an initial population and a multi-part state.
initial_population = (tf.random.normal([population_size]),
tf.random.normal([population_size]))
def easom_fn(x, y):
return -(tf.math.cos(x) * tf.math.cos(y) *
tf.math.exp(-(x-np.pi)**2 - (y-np.pi)**2))
optim_results = tfp.optimizers.differential_evolution_minimize(
easom_fn,
initial_population=initial_population,
seed=43210)
print (optim_results.converged)
print (optim_results.position) # Should be (close to) [pi, pi].
print (optim_results.objective_value) # Should be -1.
# With a single starting point
initial_position = (tf.constant(1.0), tf.constant(1.0))
optim_results = tfp.optimizers.differential_evolution_minimize(
easom_fn,
initial_position=initial_position,
population_size=40,
population_stddev=2.0,
seed=43210)
```
Args:
objective_function: A Python callable that accepts a batch of possible
solutions and returns the values of the objective function at those
arguments as a rank 1 real `Tensor`. This specifies the function to be
minimized. The input to this callable may be either a single `Tensor`
or a Python `list` of `Tensor`s. The signature must match the format of
the argument `population`. (i.e. objective_function(*population) must
return the value of the function to be minimized).
initial_population: A real `Tensor` or Python list of `Tensor`s.
If a list, each `Tensor` must be of rank at least 1 and with a common
first dimension. The first dimension indexes into the candidate solutions
while the rest of the dimensions (if any) index into an individual
solution. The size of the population must be at least 4. This is a
requirement of the DE algorithm.
initial_position: A real `Tensor` of any shape. The seed solution used
to initialize the population of solutions. If this parameter is specified
then `initial_population` must not be specified.
population_size: A positive scalar int32 `Tensor` greater than 4. The
size of the population to evolve. This parameter is ignored if
`initial_population` is specified.
Default value: 50.
population_stddev: A positive scalar real `Tensor` of the same dtype
as `initial_position`. This parameter is ignored if `initial_population`
is specified. Used to generate the population from the `initial_position`
by adding random normal noise with zero mean and the specified standard
deviation.
Default value: 1.0
max_iterations: Positive scalar int32 `Tensor`. The maximum number of
generations to evolve the population for.
Default value: 100
func_tolerance: Scalar `Tensor` of the same dtype as the output of the
`objective_function`. The algorithm stops if the absolute difference
between the largest and the smallest objective function value in the
population is below this number.
Default value: 0
position_tolerance: Scalar `Tensor` of the same real dtype as
`initial_position` or `initial_population`. The algorithm terminates if
the largest absolute difference between the coordinates of the population
members is below this threshold.
Default value: 1e-8
differential_weight: Real scalar `Tensor`. Must be positive and less than
2.0. The parameter controlling the strength of mutation in the algorithm.
Default value: 0.5
crossover_prob: Real scalar `Tensor`. Must be between 0 and 1. The
probability of recombination per site.
Default value: 0.9
seed: `int` or None. The random seed for this `Op`. If `None`, no seed is
applied.
Default value: None.
name: (Optional) Python str. The name prefixed to the ops created by this
function. If not supplied, the default name
'differential_evolution_minimize' is used.
Default value: None
Returns:
optimizer_results: An object containing the following attributes:
converged: Scalar boolean `Tensor` indicating whether the minimum was
found within the specified tolerances.
num_objective_evaluations: The total number of objective
evaluations performed.
position: A `Tensor` containing the best point found during the search.
If the search converged, then this value is the argmin of the
objective function within the specified tolerances.
objective_value: A `Tensor` containing the value of the objective
function at the `position`. If the search
converged, then this is the (local) minimum of
the objective function.
final_population: The final state of the population.
final_objective_values: The objective function evaluated at the
final population.
initial_population: The starting population.
initial_objective_values: The objective function evaluated | python | {
"resource": ""
} |
q266821 | _get_initial_args | test | def _get_initial_args(objective_function,
initial_population,
initial_position,
population_size,
population_stddev,
max_iterations,
func_tolerance,
position_tolerance,
differential_weight,
crossover_prob,
seed):
"""Processes initial args."""
was_iterable = False
if initial_position is not None:
initial_position, was_iterable = _ensure_list(initial_position)
if initial_population is not None:
initial_population, was_iterable = _ensure_list(initial_population)
population = _get_starting_population(initial_population,
initial_position,
population_size,
population_stddev,
| python | {
"resource": ""
} |
q266822 | _find_best_in_population | test | def _find_best_in_population(population, values):
"""Finds the population member with the lowest value."""
best_value = tf.math.reduce_min(input_tensor=values) | python | {
"resource": ""
} |
q266823 | _check_convergence | test | def _check_convergence(population,
population_values,
func_tolerance,
position_tolerance):
"""Checks whether the convergence criteria have been met."""
# Check func tolerance
value_range = tf.math.abs(
tf.math.reduce_max(input_tensor=population_values) -
tf.math.reduce_min(input_tensor=population_values))
value_converged = value_range <= func_tolerance
# Ideally, we would compute the position convergence by computing the
# pairwise distance between every member of the population and checking if
# the maximum of those is less than the supplied tolerance. However, this is
# completely infeasible in terms of performance. We adopt a more conservative
# approach which checks the distance between the first population member
# with the rest of the population. If the largest such distance is less than
# half the supplied tolerance, we stop. The reason why this is sufficient is
# as follows. For any pair of distinct points (a, b) in the population, we
# have the relation: |a - b| <= |x0 | python | {
"resource": ""
} |
q266824 | _get_starting_population | test | def _get_starting_population(initial_population,
initial_position,
population_size,
population_stddev,
seed):
"""Constructs the initial population.
If an initial population is not already provided, this function constructs
a population by adding random normal noise to the initial position.
Args:
initial_population: None or a list of `Tensor`s. The initial population.
initial_position: None or a list of `Tensor`s. The initial position.
If initial_population is None, this argument must not be None.
population_size: Scalar integer `Tensor`. The number of members in the
population. If the initial population is not None, this parameter is
ignored.
population_stddev: A positive scalar real `Tensor` of the same dtype
as `initial_position` or `initial_population` (whichever is not None).
This parameter is ignored if `initial_population`
| python | {
"resource": ""
} |
q266825 | _binary_crossover | test | def _binary_crossover(population,
population_size,
mutants,
crossover_prob,
seed):
"""Performs recombination by binary crossover for the current population.
Let v_i denote the i'th component of the member v and m_i the corresponding
component of the mutant vector corresponding to v. Then the crossed over
vector w_i is determined by setting w_i =
(m_i with probability=crossover_prob else v_i). In addition, DE requires that
at least one of the components is crossed over (otherwise we end
up with no change). This is done by choosing on index say k randomly where
a force crossover is performed (i.e. w_k = m_k). This is the scheme
implemented in this function.
Args:
population: A Python list of `Tensor`s where each `Tensor` in the list
must be of rank at least 1 and all the elements must have a common
first dimension. The base population to cross over.
population_size: A scalar integer `Tensor`. The number of elements in the
population (i.e. size of the first dimension of any member of
`population`).
mutants: A Python list of `Tensor`s with the same structure as `population`.
The mutated population.
crossover_prob: A postive real scalar `Tensor` bounded above by 1.0. The
probability of a crossover being performed for each axis.
seed: `int` or None. The random seed for this `Op`. If `None`, no seed is
applied.
Returns:
A list of `Tensor`s of the same structure, dtype and shape as `population`.
The recombined population.
"""
sizes = [tf.cast(tf.size(input=x), dtype=tf.float64) for x in population]
seed_stream = distributions.SeedStream(seed, salt='binary_crossover')
force_crossover_group = distributions.Categorical(sizes).sample(
[population_size, 1], seed=seed_stream())
recombinants = []
for i, population_part in enumerate(population):
pop_part_flat = tf.reshape(population_part, [population_size, -1])
mutant_part_flat = tf.reshape(mutants[i], | python | {
"resource": ""
} |
q266826 | _get_mutants | test | def _get_mutants(population,
population_size,
mixing_indices,
differential_weight):
"""Computes the mutatated vectors for each population member.
Args:
population: Python `list` of `Tensor`s representing the
current population vectors. Each `Tensor` must be of the same real dtype.
The first dimension of each `Tensor` indexes individual
population members. For example, if the population is a list with a
single `Tensor` of shape [n, m1, m2], then `n` is the population size and
the shape of an individual solution is [m1, m2].
If there is more than one element in the population, then each `Tensor`
in the list should have the first axis of the same size.
population_size: Scalar integer `Tensor`. The size of the population.
mixing_indices: `Tensor` of integral dtype and shape [n, 3] where `n` is the
number of members in the population. Each element of the `Tensor` must be
a valid index into the first dimension of the population (i.e range
| python | {
"resource": ""
} |
q266827 | _get_mixing_indices | test | def _get_mixing_indices(size, seed=None, name=None):
"""Generates an array of indices suitable for mutation operation.
The mutation operation in differential evolution requires that for every
element of the population, three distinct other elements be chosen to produce
a trial candidate. This function generates an array of shape [size, 3]
satisfying the properties that:
(a). array[i, :] does not contain the index 'i'.
(b). array[i, :] does not contain any overlapping indices.
(c). All elements in the array are between 0 and size - 1 inclusive.
Args:
size: Scalar integer `Tensor`. The number of samples as well as a the range
of the indices to sample from.
seed: `int` or None. The random seed for this `Op`. If `None`, no seed is
applied.
Default value: `None`.
name: Python `str` name prefixed to Ops created by this function.
Default value: 'get_mixing_indices'.
Returns:
sample: A `Tensor` of shape [size, 3] and same dtype as `size` containing
samples without replacement between 0 and size - 1 (inclusive) with the
`i`th row not including the number `i`.
"""
with tf.compat.v1.name_scope(
name, default_name='get_mixing_indices', values=[size]):
size = tf.convert_to_tensor(value=size)
dtype = size.dtype
seed_stream = distributions.SeedStream(seed, salt='get_mixing_indices')
first = tf.random.uniform([size],
maxval=size-1,
dtype=dtype,
seed=seed_stream())
second = tf.random.uniform([size],
maxval=size-2,
dtype=dtype,
seed=seed_stream())
third = tf.random.uniform([size],
| python | {
"resource": ""
} |
q266828 | _ensure_list | test | def _ensure_list(tensor_or_list):
"""Converts the input arg to a list if it is not a list already.
Args:
tensor_or_list: A `Tensor` or a Python list of `Tensor`s. The argument to
convert to a list of `Tensor`s.
Returns:
A tuple of two elements. The first is a Python list of `Tensor`s containing
the original arguments. The second is a boolean indicating whether
| python | {
"resource": ""
} |
q266829 | _get_tol | test | def _get_tol(tol, dtype, validate_args):
"""Gets a Tensor of type `dtype`, 0 if `tol` is None, validation optional."""
if tol is None:
return tf.convert_to_tensor(value=0, dtype=dtype)
tol = tf.convert_to_tensor(value=tol, dtype=dtype)
if validate_args:
tol = | python | {
"resource": ""
} |
q266830 | soft_threshold | test | def soft_threshold(x, threshold, name=None):
"""Soft Thresholding operator.
This operator is defined by the equations
```none
{ x[i] - gamma, x[i] > gamma
SoftThreshold(x, gamma)[i] = { 0, x[i] == gamma
{ x[i] + gamma, x[i] < -gamma
```
In the context of proximal gradient methods, we have
```none
SoftThreshold(x, gamma) = prox_{gamma L1}(x)
```
where `prox` is the proximity operator. Thus the soft thresholding operator
is used in proximal gradient descent for optimizing a smooth function with
(non-smooth) L1 regularization, as outlined below.
The proximity operator is defined as:
```none
prox_r(x) = argmin{ r(z) + 0.5 ||x - z||_2**2 : z },
```
where `r` is a (weakly) convex function, not necessarily differentiable.
Because the L2 norm is strictly convex, the above argmin is unique.
One important application of the proximity operator is as follows. Let `L` be
a convex and differentiable function with Lipschitz-continuous gradient. Let
`R` be a convex lower semicontinuous function which is possibly
nondifferentiable. Let `gamma` be an arbitrary positive real. Then
```none
x_star = argmin{ L(x) + R(x) : x }
```
if and only if the fixed-point equation is satisfied:
```none
x_star = prox_{gamma R}(x_star - gamma grad L(x_star))
```
Proximal gradient descent thus typically consists of choosing an initial value
`x^{(0)}` and repeatedly applying the update
```none
x^{(k+1)} = prox_{gamma^{(k)} R}(x^{(k)} - gamma^{(k)} grad L(x^{(k)}))
```
where `gamma` is allowed to vary from iteration to iteration. Specializing to
the case where `R(x) = ||x||_1`, we minimize `L(x) + ||x||_1` by repeatedly
applying the update
```
x^{(k+1)} = SoftThreshold(x - gamma grad L(x^{(k)}), gamma)
```
(This idea can also be extended to second-order approximations, although the
multivariate case does not have a known closed form like above.)
Args:
| python | {
"resource": ""
} |
q266831 | clip_by_value_preserve_gradient | test | def clip_by_value_preserve_gradient(t, clip_value_min, clip_value_max,
name=None):
"""Clips values to a specified min and max while leaving gradient unaltered.
Like `tf.clip_by_value`, this function returns a tensor of the same type and
shape as input `t` but with values clamped to be no smaller than to
`clip_value_min` and no larger than `clip_value_max`. Unlike
`tf.clip_by_value`, the gradient is unaffected by this op, i.e.,
```python
tf.gradients(tfp.math.clip_by_value_preserve_gradient(x), x)[0]
# ==> ones_like(x)
```
Note: `clip_value_min` needs to be smaller or equal to `clip_value_max` for
correct results.
Args:
t: A `Tensor`.
clip_value_min: A scalar `Tensor`, or a `Tensor` with the same shape
as `t`. The minimum value to clip by.
clip_value_max: A scalar `Tensor`, or a | python | {
"resource": ""
} |
q266832 | build_input_pipeline | test | def build_input_pipeline(train_images, batch_size):
"""Build an iterator over training batches."""
training_dataset = tf.data.Dataset.from_tensor_slices(train_images)
| python | {
"resource": ""
} |
q266833 | plot_generated_images | test | def plot_generated_images(images, fname):
"""Save a synthetic image as a PNG file.
Args:
images: samples of synthetic images generated by the generative network.
fname: Python `str`, filename to save the plot to.
"""
fig = plt.figure(figsize=(4, 4))
canvas = backend_agg.FigureCanvasAgg(fig)
for i, image in enumerate(images):
ax = fig.add_subplot(4, 4, i + 1)
plt.axis('off')
ax.set_xticklabels([])
| python | {
"resource": ""
} |
q266834 | SmilesGrammar.convert_to_string | test | def convert_to_string(self, productions):
"""Converts a sequence of productions into a string of terminal symbols.
Args:
productions: Tensor of shape [1, num_productions, num_production_rules].
Slices along the `num_productions` dimension represent one-hot vectors.
Returns:
str that concatenates all terminal symbols from `productions`.
Raises:
ValueError: If the first production rule does not begin with
`self.start_symbol`.
"""
symbols = []
for production in tf.unstack(productions, axis=1):
lhs, rhs = self.production_rules[tf.argmax(input=production, axis=-1)]
if not symbols: # first iteration
if lhs != self.start_symbol:
| python | {
"resource": ""
} |
q266835 | ProbabilisticGrammar.call | test | def call(self, inputs):
"""Runs the model forward to generate a sequence of productions.
Args:
inputs: Unused.
Returns:
productions: Tensor of shape [1, num_productions, num_production_rules].
Slices along the `num_productions` dimension represent one-hot vectors.
"""
del inputs # unused
latent_code = ed.MultivariateNormalDiag(loc=tf.zeros(self.latent_size),
sample_shape=1,
name="latent_code")
state = self.lstm.zero_state(1, dtype=tf.float32)
t = 0
productions = []
stack = [self.grammar.start_symbol]
while stack:
symbol = stack.pop()
net, state = self.lstm(latent_code, state)
logits = (self.output_layer(net) +
| python | {
"resource": ""
} |
q266836 | ProbabilisticGrammarVariational.call | test | def call(self, inputs):
"""Runs the model forward to return a stochastic encoding.
Args:
inputs: Tensor of shape [1, num_productions, num_production_rules]. It is
a sequence of productions of length `num_productions`. Each production
is a one-hot vector of length `num_production_rules`: it determines
which production rule the production corresponds to.
Returns:
latent_code_posterior: A random variable capturing a sample | python | {
"resource": ""
} |
q266837 | Zipf._hat_integral | test | def _hat_integral(self, x):
"""Integral of the `hat` function, used for sampling.
We choose a `hat` function, h(x) = x^(-power), which is a continuous
(unnormalized) density touching each positive integer at the (unnormalized)
pmf. This function implements `hat` integral: H(x) = int_x^inf h(t) dt;
which is needed for sampling purposes.
Arguments:
x: A Tensor of | python | {
"resource": ""
} |
q266838 | Zipf._hat_integral_inverse | test | def _hat_integral_inverse(self, x):
"""Inverse function of _hat_integral."""
x = tf.cast(x, self.power.dtype)
t = self.power - 1.
| python | {
"resource": ""
} |
q266839 | matrix_rank | test | def matrix_rank(a, tol=None, validate_args=False, name=None):
"""Compute the matrix rank; the number of non-zero SVD singular values.
Arguments:
a: (Batch of) `float`-like matrix-shaped `Tensor`(s) which are to be
pseudo-inverted.
tol: Threshold below which the singular value is counted as "zero".
Default value: `None` (i.e., `eps * max(rows, cols) * max(singular_val)`).
validate_args: When `True`, additional assertions might be embedded in the
graph.
Default value: `False` (i.e., no graph assertions are added).
name: Python `str` prefixed to ops created by this function.
Default value: "matrix_rank".
Returns:
matrix_rank: (Batch of) `int32` scalars representing the number of non-zero
singular values.
"""
with tf.compat.v1.name_scope(name, 'matrix_rank', [a, tol]):
a = tf.convert_to_tensor(value=a, dtype_hint=tf.float32, name='a')
assertions = _maybe_validate_matrix(a, validate_args)
if assertions:
| python | {
"resource": ""
} |
q266840 | pinv | test | def pinv(a, rcond=None, validate_args=False, name=None):
"""Compute the Moore-Penrose pseudo-inverse of a matrix.
Calculate the [generalized inverse of a matrix](
https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) using its
singular-value decomposition (SVD) and including all large singular values.
The pseudo-inverse of a matrix `A`, is defined as: "the matrix that 'solves'
[the least-squares problem] `A @ x = b`," i.e., if `x_hat` is a solution, then
`A_pinv` is the matrix such that `x_hat = A_pinv @ b`. It can be shown that if
`U @ Sigma @ V.T = A` is the singular value decomposition of `A`, then
`A_pinv = V @ inv(Sigma) U^T`. [(Strang, 1980)][1]
This function is analogous to [`numpy.linalg.pinv`](
https://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.pinv.html).
It differs only in default value of `rcond`. In `numpy.linalg.pinv`, the
default `rcond` is `1e-15`. Here the default is
`10. * max(num_rows, num_cols) * np.finfo(dtype).eps`.
Args:
a: (Batch of) `float`-like matrix-shaped `Tensor`(s) which are to be
pseudo-inverted.
rcond: `Tensor` of small singular value cutoffs. Singular values smaller
(in modulus) than `rcond` * largest_singular_value (again, in modulus) are
set to zero. Must broadcast against `tf.shape(a)[:-2]`.
Default value: `10. * max(num_rows, num_cols) * np.finfo(a.dtype).eps`.
validate_args: When `True`, additional assertions might be embedded in the
graph.
Default value: `False` (i.e., no graph assertions are added).
name: Python `str` prefixed to ops created by this function.
Default value: "pinv".
Returns:
a_pinv: The pseudo-inverse of input `a`. Has same shape as `a` except
rightmost two dimensions are transposed.
Raises:
TypeError: if input `a` does not have `float`-like `dtype`.
ValueError: if input `a` has fewer than 2 dimensions.
#### Examples
```python
import tensorflow as tf
import tensorflow_probability as tfp
a = tf.constant([[1., 0.4, 0.5],
[0.4, 0.2, 0.25],
[0.5, 0.25, 0.35]])
tf.matmul(tfp.math.pinv(a), a)
# ==> array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]], dtype=float32)
a = tf.constant([[1., 0.4, 0.5, 1.],
[0.4, 0.2, 0.25, 2.],
[0.5, 0.25, 0.35, 3.]])
tf.matmul(tfp.math.pinv(a), a)
# ==> array([[ 0.76, 0.37, 0.21, -0.02],
[ 0.37, 0.43, -0.33, 0.02],
[ 0.21, -0.33, 0.81, 0.01],
[-0.02, 0.02, 0.01, 1. ]], dtype=float32)
```
#### References
[1]: G. Strang. "Linear Algebra and Its Applications, 2nd Ed." Academic Press,
Inc., 1980, pp. 139-142.
| python | {
"resource": ""
} |
q266841 | lu_solve | test | def lu_solve(lower_upper, perm, rhs,
validate_args=False,
name=None):
"""Solves systems of linear eqns `A X = RHS`, given LU factorizations.
Note: this function does not verify the implied matrix is actually invertible
nor is this condition checked even when `validate_args=True`.
Args:
lower_upper: `lu` as returned by `tf.linalg.lu`, i.e., if
`matmul(P, matmul(L, U)) = X` then `lower_upper = L + U - eye`.
perm: `p` as returned by `tf.linag.lu`, i.e., if
`matmul(P, matmul(L, U)) = X` then `perm = argmax(P)`.
rhs: Matrix-shaped float `Tensor` representing targets for which to solve;
`A X = RHS`. To handle vector cases, use:
`lu_solve(..., rhs[..., tf.newaxis])[..., 0]`.
validate_args: Python `bool` indicating whether arguments should be checked
for correctness. Note: this function does not verify the implied matrix is
actually invertible, even when `validate_args=True`.
Default value: `False` (i.e., don't validate arguments).
name: Python `str` name given to ops managed by this object.
Default value: `None` (i.e., "lu_solve").
Returns:
x: The `X` in `A @ X = RHS`.
#### Examples
```python
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
x = [[[1., 2],
[3, 4]],
[[7, 8],
[3, 4]]]
inv_x = tfp.math.lu_solve(*tf.linalg.lu(x), rhs=tf.eye(2))
tf.assert_near(tf.matrix_inverse(x), inv_x)
# ==> True
```
"""
with tf.compat.v1.name_scope(name, 'lu_solve', [lower_upper, perm, rhs]):
lower_upper = tf.convert_to_tensor(
| python | {
"resource": ""
} |
q266842 | lu_matrix_inverse | test | def lu_matrix_inverse(lower_upper, perm, validate_args=False, name=None):
"""Computes a matrix inverse given the matrix's LU decomposition.
This op is conceptually identical to,
````python
inv_X = tf.lu_matrix_inverse(*tf.linalg.lu(X))
tf.assert_near(tf.matrix_inverse(X), inv_X)
# ==> True
```
Note: this function does not verify the implied matrix is actually invertible
nor is this condition checked even when `validate_args=True`.
Args:
lower_upper: `lu` as returned by `tf.linalg.lu`, i.e., if
`matmul(P, matmul(L, U)) = X` then `lower_upper = L + U - eye`.
perm: `p` as returned by `tf.linag.lu`, i.e., if
`matmul(P, matmul(L, U)) = X` then `perm = argmax(P)`.
validate_args: Python `bool` indicating whether arguments should be checked
for correctness. Note: this function does not verify the implied matrix is
actually invertible, even when `validate_args=True`.
| python | {
"resource": ""
} |
q266843 | _lu_reconstruct_assertions | test | def _lu_reconstruct_assertions(lower_upper, perm, validate_args):
"""Returns list of assertions related to `lu_reconstruct` assumptions."""
assertions = []
message = 'Input `lower_upper` must have at least 2 dimensions.'
if lower_upper.shape.ndims is not None:
if lower_upper.shape.ndims < 2:
raise ValueError(message)
elif validate_args:
assertions.append(
tf.compat.v1.assert_rank_at_least(lower_upper, rank=2, message=message))
message = '`rank(lower_upper)` must equal `rank(perm) + 1`'
if lower_upper.shape.ndims is not None and perm.shape.ndims is not None:
if lower_upper.shape.ndims != perm.shape.ndims + 1:
raise ValueError(message)
elif validate_args:
assertions.append(
tf.compat.v1.assert_rank(
lower_upper, rank=tf.rank(perm) + 1, message=message))
| python | {
"resource": ""
} |
q266844 | _lu_solve_assertions | test | def _lu_solve_assertions(lower_upper, perm, rhs, validate_args):
"""Returns list of assertions related to `lu_solve` assumptions."""
assertions = _lu_reconstruct_assertions(lower_upper, perm, validate_args)
message = 'Input `rhs` must have at least 2 dimensions.'
if rhs.shape.ndims is not None:
if rhs.shape.ndims < 2:
raise ValueError(message)
elif validate_args:
assertions.append(
tf.compat.v1.assert_rank_at_least(rhs, rank=2, message=message))
message = '`lower_upper.shape[-1]` must equal `rhs.shape[-1]`.'
if (tf.compat.dimension_value(lower_upper.shape[-1]) is not None and
| python | {
"resource": ""
} |
q266845 | _sparse_block_diag | test | def _sparse_block_diag(sp_a):
"""Returns a block diagonal rank 2 SparseTensor from a batch of SparseTensors.
Args:
sp_a: A rank 3 `SparseTensor` representing a batch of matrices.
Returns:
sp_block_diag_a: matrix-shaped, `float` `SparseTensor` with the same dtype
as `sparse_or_matrix`, of shape [B * M, B * N] where `sp_a` has shape
[B, M, N]. Each [M, N] batch of `sp_a` is lined up along the diagonal.
"""
# Construct the matrix [[M, N], [1, 0], [0, 1]] which would map the index
# (b, i, j) to (Mb + i, Nb + j). This effectively creates a block-diagonal
# matrix of dense shape [B * M, B * N].
# Note that this transformation doesn't increase the number of non-zero
# entries in the SparseTensor.
| python | {
"resource": ""
} |
q266846 | _maybe_validate_matrix | test | def _maybe_validate_matrix(a, validate_args):
"""Checks that input is a `float` matrix."""
assertions = []
if not a.dtype.is_floating:
raise TypeError('Input `a` must have `float`-like `dtype` '
'(saw {}).'.format(a.dtype.name))
if a.shape.ndims is not None:
if a.shape.ndims < 2:
raise ValueError('Input `a` must have at least 2 dimensions '
| python | {
"resource": ""
} |
q266847 | _grad_neg_log_likelihood_and_fim | test | def _grad_neg_log_likelihood_and_fim(model_matrix, linear_response, response,
model):
"""Computes the neg-log-likelihood gradient and Fisher information for a GLM.
Note that Fisher information is related to the Hessian of the log-likelihood
by the equation
```none
FisherInfo = E[Hessian with respect to model_coefficients of -LogLikelihood(
Y | model_matrix, model_coefficients)]
```
where `LogLikelihood` is the log-likelihood of a generalized linear model
parameterized by `model_matrix` and `model_coefficients`, and the expectation
is taken over Y, distributed according to the same GLM with the same parameter
values.
Args:
model_matrix: (Batch of) matrix-shaped, `float` `Tensor` or `SparseTensor`
where each row represents a sample's features. Has shape `[N, n]` where
`N` is the number of data samples and `n` is the number of features per
sample.
linear_response: (Batch of) vector-shaped `Tensor` with the same dtype as
`model_matrix`, equal to `model_matix @ model_coefficients` where
`model_coefficients` are the coefficients of the linear component of the
GLM.
response: (Batch of) vector-shaped `Tensor` with the same dtype as
`model_matrix` where each element represents a sample's observed response
(to the corresponding row of features).
model: `tfp.glm.ExponentialFamily`-like instance, which specifies the link
function and distribution of the GLM, and thus characterizes the negative
log-likelihood. Must have sufficient statistic equal to the response, that
is, `T(y) = y`.
Returns:
grad_neg_log_likelihood: (Batch of) vector-shaped `Tensor` with the same
shape and dtype as a single row of `model_matrix`, representing the
gradient of the negative log likelihood of `response` given linear
response `linear_response`.
| python | {
"resource": ""
} |
q266848 | fit_sparse | test | def fit_sparse(model_matrix,
response,
model,
model_coefficients_start,
tolerance,
l1_regularizer,
l2_regularizer=None,
maximum_iterations=None,
maximum_full_sweeps_per_iteration=1,
learning_rate=None,
name=None):
r"""Fits a GLM using coordinate-wise FIM-informed proximal gradient descent.
This function uses a L1- and L2-regularized, second-order quasi-Newton method
to find maximum-likelihood parameters for the given model and observed data.
The second-order approximations use negative Fisher information in place of
the Hessian, that is,
```none
FisherInfo = E_Y[Hessian with respect to model_coefficients of -LogLikelihood(
Y | model_matrix, current value of model_coefficients)]
```
For large, sparse data sets, `model_matrix` should be supplied as a
`SparseTensor`.
Args:
model_matrix: (Batch of) matrix-shaped, `float` `Tensor` or `SparseTensor`
where each row represents a sample's features. Has shape `[N, n]` where
`N` is the number of data samples and `n` is the number of features per
sample.
response: (Batch of) vector-shaped `Tensor` with the same dtype as
`model_matrix` where each element represents a sample's observed response
(to the corresponding row of features).
model: `tfp.glm.ExponentialFamily`-like instance, which specifies the link
function and distribution of the GLM, and thus characterizes the negative
log-likelihood which will be minimized. Must have sufficient statistic
equal to the response, that is, `T(y) = y`.
model_coefficients_start: (Batch of) vector-shaped, `float` `Tensor` with
the same dtype as `model_matrix`, representing the initial values of the
coefficients for the GLM regression. Has shape `[n]` where `model_matrix`
has shape `[N, n]`.
tolerance: scalar, `float` `Tensor` representing the tolerance for each
optiization step; see the `tolerance` argument of `fit_sparse_one_step`.
l1_regularizer: scalar, `float` `Tensor` representing the weight of the L1
regularization term.
l2_regularizer: scalar, `float` `Tensor` representing the weight of the L2
regularization term.
Default value: `None` (i.e., no L2 regularization).
maximum_iterations: Python integer specifying maximum number of iterations
of the outer loop of the optimizer (i.e., maximum number of calls to
`fit_sparse_one_step`). After this many iterations of the outer loop, the
algorithm will terminate even if the return value `model_coefficients` has
not converged.
Default value: `1`.
maximum_full_sweeps_per_iteration: Python integer specifying the maximum
number of coordinate descent sweeps allowed in each iteration.
Default value: `1`.
learning_rate: scalar, `float` `Tensor` representing a multiplicative factor
used to dampen the proximal gradient descent steps.
Default value: `None` (i.e., factor is conceptually `1`).
name: Python string representing the name of the TensorFlow operation.
The default name is `"fit_sparse"`.
Returns:
model_coefficients: (Batch of) `Tensor` of the same shape and dtype as
`model_coefficients_start`, representing the computed model coefficients
which minimize the regularized negative log-likelihood.
is_converged: scalar, `bool` `Tensor` indicating whether the minimization
procedure converged across all batches within the specified number of
iterations. Here convergence means that an iteration of the inner loop
(`fit_sparse_one_step`) returns `True` for its `is_converged` output
value.
iter: scalar, `int` `Tensor` indicating the actual number of iterations of
the outer loop of the optimizer completed (i.e., number of calls to
`fit_sparse_one_step` before achieving convergence).
#### Example
```python
from __future__ import print_function
import numpy as np
import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions
def make_dataset(n, d, link, scale=1., dtype=np.float32):
model_coefficients = tfd.Uniform(
low=np.array(-1, dtype), high=np.array(1, dtype)).sample(
d, seed=42)
radius = np.sqrt(2.)
model_coefficients *= radius / tf.linalg.norm(model_coefficients)
mask = tf.random_shuffle(tf.range(d)) < tf.to_int32(0.5 * tf.to_float(d))
model_coefficients = tf.where(mask, model_coefficients,
tf.zeros_like(model_coefficients))
model_matrix = tfd.Normal(
loc=np.array(0, dtype), scale=np.array(1, dtype)).sample(
[n, d], seed=43)
scale = tf.convert_to_tensor(scale, dtype)
linear_response = tf.matmul(model_matrix,
model_coefficients[..., tf.newaxis])[..., 0]
if link == 'linear':
response = tfd.Normal(loc=linear_response, scale=scale).sample(seed=44)
elif link == 'probit':
response = tf.cast(
tfd.Normal(loc=linear_response, scale=scale).sample(seed=44) > 0,
dtype)
elif link == 'logit':
response = tfd.Bernoulli(logits=linear_response).sample(seed=44)
else:
raise ValueError('unrecognized true link: {}'.format(link))
return model_matrix, response, model_coefficients, mask
with tf.Session() as sess:
x_, y_, model_coefficients_true_, _ = sess.run(make_dataset(
n=int(1e5), d=100, link='probit'))
model = tfp.glm.Bernoulli()
model_coefficients_start = tf.zeros(x_.shape[-1], np.float32)
model_coefficients, is_converged, num_iter = tfp.glm.fit_sparse(
model_matrix=tf.convert_to_tensor(x_),
response=tf.convert_to_tensor(y_),
model=model,
model_coefficients_start=model_coefficients_start,
l1_regularizer=800.,
l2_regularizer=None,
maximum_iterations=10,
maximum_full_sweeps_per_iteration=10,
tolerance=1e-6,
learning_rate=None)
model_coefficients_, is_converged_, num_iter_ = sess.run([
model_coefficients, is_converged, num_iter])
print("is_converged:", is_converged_)
print(" num_iter:", num_iter_)
print("\nLearned / True")
print(np.concatenate(
[[model_coefficients_], [model_coefficients_true_]], axis=0).T)
# ==>
# is_converged: True
# num_iter: 1
#
# Learned / True
# [[ 0. 0. ]
| python | {
"resource": ""
} |
q266849 | _gen_slices | test | def _gen_slices(num_blocks, n_in, n_out, mask_type=MASK_EXCLUSIVE):
"""Generate the slices for building an autoregressive mask."""
# TODO(b/67594795): Better support of dynamic shape.
slices = []
col = 0
d_in = n_in // num_blocks
d_out = n_out // num_blocks
row = d_out if mask_type == MASK_EXCLUSIVE else 0
for _ in range(num_blocks):
| python | {
"resource": ""
} |
q266850 | _gen_mask | test | def _gen_mask(num_blocks,
n_in,
n_out,
mask_type=MASK_EXCLUSIVE,
dtype=tf.float32):
"""Generate the mask for building an autoregressive dense layer."""
# TODO(b/67594795): Better support of dynamic shape.
mask = np.zeros([n_out, n_in], dtype=dtype.as_numpy_dtype())
| python | {
"resource": ""
} |
q266851 | masked_dense | test | def masked_dense(inputs,
units,
num_blocks=None,
exclusive=False,
kernel_initializer=None,
reuse=None,
name=None,
*args, # pylint: disable=keyword-arg-before-vararg
**kwargs):
"""A autoregressively masked dense layer. Analogous to `tf.layers.dense`.
See [Germain et al. (2015)][1] for detailed explanation.
Arguments:
inputs: Tensor input.
units: Python `int` scalar representing the dimensionality of the output
space.
num_blocks: Python `int` scalar representing the number of blocks for the
MADE masks.
exclusive: Python `bool` scalar representing whether to zero the diagonal of
the mask, used for the first layer of a MADE.
kernel_initializer: Initializer function for the weight matrix.
If `None` (default), weights are initialized using the
`tf.glorot_random_initializer`.
reuse: Python `bool` scalar representing whether to reuse the weights of a
previous layer by the same name.
name: Python `str` used to describe ops managed by this function.
*args: `tf.layers.dense` arguments.
**kwargs: `tf.layers.dense` keyword arguments.
Returns:
Output tensor.
Raises:
NotImplementedError: if rightmost dimension of `inputs` is unknown prior to
graph execution.
#### References
[1]: Mathieu Germain, Karol Gregor, Iain Murray, and Hugo Larochelle. MADE:
Masked Autoencoder for Distribution Estimation. In _International
Conference on Machine Learning_, 2015. https://arxiv.org/abs/1502.03509
"""
# TODO(b/67594795): Better support of dynamic shape.
input_depth = tf.compat.dimension_value(
tensorshape_util.with_rank_at_least(inputs.shape, 1)[-1])
if input_depth is None:
| python | {
"resource": ""
} |
q266852 | _create_input_order | test | def _create_input_order(input_size, input_order="left-to-right"):
"""Returns a degree vectors for the input."""
if isinstance(input_order, six.string_types):
if input_order == "left-to-right":
return np.arange(start=1, stop=input_size + 1)
elif input_order == "right-to-left":
return np.arange(start=input_size, stop=0, step=-1)
elif input_order == "random":
ret = np.arange(start=1, stop=input_size + 1)
| python | {
"resource": ""
} |
q266853 | _create_degrees | test | def _create_degrees(input_size,
hidden_units=None,
input_order="left-to-right",
hidden_degrees="equal"):
"""Returns a list of degree vectors, one for each input and hidden layer.
A unit with degree d can only receive input from units with degree < d. Output
units always have the same degree as their associated input unit.
Args:
input_size: Number of inputs.
hidden_units: list with the number of hidden units per layer. It does not
include the output layer. Each hidden unit size must be at least the size
of length (otherwise autoregressivity is not possible).
input_order: Order of degrees to the input units: 'random', 'left-to-right',
'right-to-left', or an array of an explicit order. For example,
'left-to-right' builds an autoregressive model
p(x) = p(x1) p(x2 | x1) ... p(xD | x<D).
hidden_degrees: Method for assigning degrees to the hidden units:
'equal', 'random'. If 'equal', hidden units in each layer are allocated
equally (up to a remainder term) to each degree. Default: 'equal'.
Raises:
ValueError: invalid input order.
ValueError: invalid hidden degrees.
"""
input_order = _create_input_order(input_size, input_order)
degrees = [input_order]
if hidden_units is None:
hidden_units = []
for units in hidden_units:
if isinstance(hidden_degrees, six.string_types):
if hidden_degrees == "random":
# samples from: [low, high)
degrees.append(
| python | {
"resource": ""
} |
q266854 | _create_masks | test | def _create_masks(degrees):
"""Returns a list of binary mask matrices enforcing autoregressivity."""
return [
# Create input->hidden and hidden->hidden masks.
inp[:, np.newaxis] <= out
| python | {
"resource": ""
} |
q266855 | _make_masked_initializer | test | def _make_masked_initializer(mask, initializer):
"""Returns a masked version of the given initializer."""
initializer = tf.keras.initializers.get(initializer)
def masked_initializer(shape, dtype=None, partition_info=None):
# If no `partition_info` is given, | python | {
"resource": ""
} |
q266856 | AutoregressiveLayer.build | test | def build(self, input_shape):
"""See tfkl.Layer.build."""
if self._event_shape is None:
# `event_shape` wasn't specied at __init__, so infer from `input_shape`.
self._event_shape = [tf.compat.dimension_value(input_shape[-1])]
self._event_size = self._event_shape[-1]
self._event_ndims = len(self._event_shape)
# Should we throw if input_shape has rank > 2?
if input_shape[-1] != self._event_shape[-1]:
raise ValueError("Invalid final dimension of `input_shape`. "
"Expected `{!r}`, but got `{!r}`".format(
self._event_shape[-1], input_shape[-1]))
# Construct the masks.
self._input_order = _create_input_order(
self._event_size, self._input_order_param)
self._masks = _create_masks(_create_degrees(
input_size=self._event_size,
hidden_units=self._hidden_units,
input_order=self._input_order,
hidden_degrees=self._hidden_degrees))
# In the final layer, we will produce `self._params` outputs for each of the
# `self._event_size` inputs to `AutoregressiveLayer`. But `masks[-1]` has
# shape `[self._hidden_units[-1], self._event_size]`. Thus, we need to
# expand the mask to `[hidden_units[-1], event_size * self._params]` such
# that all units for the same input are masked identically. In particular,
# we tile the mask so the j-th element of `tf.unstack(output, axis=-1)` is a
# tensor of the j-th parameter/unit for each input.
#
# NOTE: Other orderings of the output could be faster -- should benchmark.
self._masks[-1] = np.reshape(
np.tile(self._masks[-1][..., tf.newaxis], [1, 1, self._params]),
[self._masks[-1].shape[0], self._event_size * self._params])
self._network = tf.keras.Sequential([
# Starting this model with an `InputLayer` | python | {
"resource": ""
} |
q266857 | AutoregressiveLayer.call | test | def call(self, x):
"""See tfkl.Layer.call."""
with tf.compat.v2.name_scope(self.name or "AutoregressiveLayer_call"):
x = tf.convert_to_tensor(value=x, dtype=self.dtype, name="x")
| python | {
"resource": ""
} |
q266858 | draw_sample | test | def draw_sample(num_samples, num_classes, logits, num_trials, dtype, seed):
"""Sample a multinomial.
The batch shape is given by broadcasting num_trials with
remove_last_dimension(logits).
Args:
num_samples: Python int or singleton integer Tensor: number of multinomial
samples to draw.
num_classes: Python int or singleton integer Tensor: number of classes.
logits: Floating Tensor with last dimension k, of (unnormalized) logit
probabilities per class.
num_trials: Tensor of number of categorical trials each multinomial consists
of. num_trials[..., tf.newaxis] must broadcast with logits.
dtype: dtype at which to emit samples.
seed: Random seed.
Returns:
samples: Tensor of given dtype and shape [n] + batch_shape + [k].
"""
with tf.name_scope("multinomial.draw_sample"):
# broadcast the num_trials and logits to same shape
num_trials = tf.ones_like(
logits[..., 0], dtype=num_trials.dtype) * num_trials
logits = tf.ones_like(
num_trials[..., tf.newaxis], dtype=logits.dtype) * logits
# flatten the total_count and logits
# flat_logits has shape [B1B2...Bm, num_classes]
flat_logits = tf.reshape(logits, [-1, num_classes])
flat_num_trials = num_samples * tf.reshape(num_trials, [-1]) # [B1B2...Bm]
# Computes each logits and num_trials situation by map_fn.
# Using just one batch tf.random.categorical call doesn't work because that
# requires num_trials to be the same across all members of the batch of
# logits. This restriction makes sense for tf.random.categorical because
# for it, num_trials is part of the returned shape. However, the
# multinomial sampler does not need that restriction, because it sums out
# exactly that dimension.
# One possibility would be to draw a batch categorical whose sample count is
# max(num_trials) and mask out the excess ones. However, if the elements of
| python | {
"resource": ""
} |
q266859 | _zero_dimensional_mvndiag | test | def _zero_dimensional_mvndiag(dtype):
"""Build a zero-dimensional MVNDiag object."""
dummy_mvndiag = tfd.MultivariateNormalDiag(
scale_diag=tf.ones([0], dtype=dtype))
| python | {
"resource": ""
} |
q266860 | _observe_timeseries_fn | test | def _observe_timeseries_fn(timeseries):
"""Build an observation_noise_fn that observes a Tensor timeseries."""
def observation_noise_fn(t):
current_slice = timeseries[..., t, :]
return tfd.MultivariateNormalDiag(
| python | {
"resource": ""
} |
q266861 | SparseLinearRegression.params_to_weights | test | def params_to_weights(self,
global_scale_variance,
global_scale_noncentered,
local_scale_variances,
local_scales_noncentered,
weights_noncentered):
"""Build regression weights from model parameters."""
global_scale = (global_scale_noncentered *
| python | {
"resource": ""
} |
q266862 | _depth | test | def _depth(g):
"""Computes the number of edges on longest path from node to root."""
def _explore(v):
if v.depth < 0:
v.depth = ((1 + max([-1] + [_explore(annotated_graph[u])
| python | {
"resource": ""
} |
q266863 | _best_order | test | def _best_order(g):
"""Creates tuple of str tuple-str pairs representing resolved & sorted DAG."""
def _explore(u):
"""Recursive function to ascend up through unvisited dependencies."""
if u.depth < 0:
return # Already visited.
if not u.parents:
result.append((u.name, u.parents))
u.depth = -1 # Mark visited.
return
b = (u.name, [])
result.append(b)
u.depth = -1 # Mark visited.
d = 0
for v in sorted((g.get(p) for p in u.parents), key=lambda v: v.depth):
n0 = len(result)
| python | {
"resource": ""
} |
q266864 | _prob_chain_rule_flatten | test | def _prob_chain_rule_flatten(named_makers):
"""Creates lists of callables suitable for JDSeq."""
def _make(dist_fn, args):
if args is None:
return lambda *_: dist_fn
if not args:
return lambda *_: dist_fn()
def _fn(*xs):
kwargs = dict(zip(args, reversed(xs[-len(args):])))
kwargs.pop('_', None)
return dist_fn(**kwargs)
return _fn
named_makers = _convert_to_dict(named_makers)
g = {k: (None if distribution_util.is_distribution_instance(v)
else joint_distribution_sequential._get_required_args(v)) # pylint: disable=protected-access
for k, v in | python | {
"resource": ""
} |
q266865 | JointDistributionNamed._build | test | def _build(self, model):
"""Creates `dist_fn`, `dist_fn_wrapped`, `dist_fn_args`, `dist_fn_name`."""
if not _is_dict_like(model):
raise TypeError('`model` must be convertible to `dict` (saw: {}).'.format(
type(model).__name__))
[
self._dist_fn,
| python | {
"resource": ""
} |
q266866 | VariationalGaussianProcess.variational_loss | test | def variational_loss(self,
observations,
observation_index_points=None,
kl_weight=1.,
name='variational_loss'):
"""Variational loss for the VGP.
Given `observations` and `observation_index_points`, compute the
negative variational lower bound as specified in [Hensman, 2013][1].
Args:
observations: `float` `Tensor` representing collection, or batch of
collections, of observations corresponding to
`observation_index_points`. Shape has the form `[b1, ..., bB, e]`, which
must be brodcastable with the batch and example shapes of
`observation_index_points`. The batch shape `[b1, ..., bB]` must be
broadcastable with the shapes of all other batched parameters
(`kernel.batch_shape`, `observation_index_points`, etc.).
observation_index_points: `float` `Tensor` representing finite (batch of)
vector(s) of points where observations are defined. Shape has the
form `[b1, ..., bB, e1, f1, ..., fF]` where `F` is the number of feature
dimensions and must equal `kernel.feature_ndims` and `e1` is the number
(size) of index points in each batch (we denote it `e1` to distinguish
it from the numer of inducing index points, denoted `e2` below). If
set to `None` uses `index_points` as the origin for observations.
Default value: None.
kl_weight: Amount by which to scale the KL divergence loss between prior
and posterior.
Default value: 1.
name: Python `str` name prefixed to Ops created by this class.
Default value: "GaussianProcess".
Returns:
loss: Scalar tensor representing the negative variational lower bound.
Can be directly used in a `tf.Optimizer`.
Raises:
ValueError: if `mean_fn` is not `None` and is not callable.
#### References
[1]: Hensman, J., Lawrence, N. "Gaussian Processes for Big Data", 2013
https://arxiv.org/abs/1309.6835
"""
with tf.name_scope(name or 'variational_gp_loss'):
if observation_index_points is None:
observation_index_points = self._index_points
observation_index_points = tf.convert_to_tensor(
value=observation_index_points, dtype=self._dtype,
name='observation_index_points')
observations = tf.convert_to_tensor(
value=observations, dtype=self._dtype, name='observations')
kl_weight = tf.convert_to_tensor(
value=kl_weight, dtype=self._dtype,
name='kl_weight')
# The variational loss is a negative ELBO. The ELBO can be broken down
# into three terms:
# 1. a likelihood term
# 2. a trace term arising from the covariance of the posterior predictive
kzx = self.kernel.matrix(self._inducing_index_points,
observation_index_points)
kzx_linop = tf.linalg.LinearOperatorFullMatrix(kzx)
loc = (self._mean_fn(observation_index_points) +
kzx_linop.matvec(self._kzz_inv_varloc, adjoint=True))
likelihood = independent.Independent(
normal.Normal(
| python | {
"resource": ""
} |
q266867 | VariationalGaussianProcess.optimal_variational_posterior | test | def optimal_variational_posterior(
kernel,
inducing_index_points,
observation_index_points,
observations,
observation_noise_variance,
mean_fn=None,
jitter=1e-6,
name=None):
"""Model selection for optimal variational hyperparameters.
Given the full training set (parameterized by `observations` and
`observation_index_points`), compute the optimal variational
location and scale for the VGP. This is based of the method suggested
in [Titsias, 2009][1].
Args:
kernel: `PositiveSemidefiniteKernel`-like instance representing the
GP's covariance function.
inducing_index_points: `float` `Tensor` of locations of inducing points in
the index set. Shape has the form `[b1, ..., bB, e2, f1, ..., fF]`, just
like `observation_index_points`. The batch shape components needn't be
identical to those of `observation_index_points`, but must be broadcast
compatible with them.
observation_index_points: `float` `Tensor` representing finite (batch of)
vector(s) of points where observations are defined. Shape has the
form `[b1, ..., bB, e1, f1, ..., fF]` where `F` is the number of feature
dimensions and must equal `kernel.feature_ndims` and `e1` is the number
(size) of index points in each batch (we denote it `e1` to distinguish
it from the numer of inducing index points, denoted `e2` below).
observations: `float` `Tensor` representing collection, or batch of
collections, of observations corresponding to
`observation_index_points`. Shape has the form `[b1, ..., bB, e]`, which
must be brodcastable with the batch and example shapes of
`observation_index_points`. The batch shape `[b1, ..., bB]` must be
broadcastable with the shapes of all other batched parameters
(`kernel.batch_shape`, `observation_index_points`, etc.).
observation_noise_variance: `float` `Tensor` representing the variance
of the noise in the Normal likelihood distribution of the model. May be
batched, in which case the batch shape must be broadcastable with the
shapes of all other batched parameters (`kernel.batch_shape`,
`index_points`, etc.).
Default value: `0.`
mean_fn: Python `callable` that acts on index points to produce a (batch
of) vector(s) of mean values at those index points. Takes a `Tensor` of
shape `[b1, ..., bB, f1, ..., fF]` and returns a `Tensor` whose shape is
(broadcastable with) `[b1, ..., bB]`. Default value: `None` implies
constant zero function.
jitter: `float` scalar `Tensor` added to the diagonal of the covariance
matrix to ensure positive definiteness of the covariance matrix.
Default value: `1e-6`.
name: Python `str` name prefixed to Ops created by this class.
Default value: "optimal_variational_posterior".
Returns:
loc, scale: Tuple representing the variational location and scale.
Raises:
ValueError: if `mean_fn` is not `None` and is not callable.
#### References
[1]: Titsias, M. "Variational Model Selection for Sparse Gaussian Process
Regression", 2009.
http://proceedings.mlr.press/v5/titsias09a/titsias09a.pdf
"""
with tf.name_scope(name or 'optimal_variational_posterior'):
dtype = dtype_util.common_dtype( | python | {
"resource": ""
} |
q266868 | build_is_last_day_of_season | test | def build_is_last_day_of_season(num_steps_per_season):
"""Build utility method to compute whether the season is changing."""
num_steps_per_cycle = np.sum(num_steps_per_season)
changepoints = np.cumsum(np.ravel(num_steps_per_season)) - 1
def is_last_day_of_season(t):
t_ = dist_util.maybe_get_static_value(t)
if t_ is not None: # static case
step_in_cycle = t_ % num_steps_per_cycle
| python | {
"resource": ""
} |
q266869 | build_effects_to_residuals_matrix | test | def build_effects_to_residuals_matrix(num_seasons, dtype):
"""Build change-of-basis matrices for constrained seasonal effects.
This method builds the matrix that transforms seasonal effects into
effect residuals (differences from the mean effect), and additionally
projects these residuals onto the subspace where the mean effect is zero.
See `ConstrainedSeasonalStateSpaceModel` for mathematical details.
Args:
num_seasons: scalar `int` number of seasons.
dtype: TensorFlow `dtype` for the returned values.
Returns:
effects_to_residuals: `Tensor` of shape
`[num_seasons-1, num_seasons]`, such that `differences_from_mean_effect =
matmul(effects_to_residuals, seasonal_effects)`. In the
notation of `ConstrainedSeasonalStateSpaceModel`, this is
`effects_to_residuals = P * R`.
residuals_to_effects: the (pseudo)-inverse of the above; a
`Tensor` of shape `[num_seasons, num_seasons-1]`. In the
notation of `ConstrainedSeasonalStateSpaceModel`, this is
`residuals_to_effects = R^{-1} * P'`.
"""
# Build the matrix that converts effects `e_i` into differences from the mean
# effect | python | {
"resource": ""
} |
q266870 | build_seasonal_transition_matrix | test | def build_seasonal_transition_matrix(
num_seasons, is_last_day_of_season, dtype,
basis_change_matrix=None, basis_change_matrix_inv=None):
"""Build a function computing transitions for a seasonal effect model."""
with tf.compat.v1.name_scope('build_seasonal_transition_matrix'):
# If the season is changing, the transition matrix permutes the latent
# state to shift all seasons up by a dimension, and sends the current
# season's effect to the bottom.
seasonal_permutation = np.concatenate(
[np.arange(1, num_seasons), [0]], axis=0)
seasonal_permutation_matrix = tf.constant(
np.eye(num_seasons)[seasonal_permutation], dtype=dtype)
# Optionally transform the transition matrix into a reparameterized space,
# enforcing the zero-sum constraint for ConstrainedSeasonalStateSpaceModel.
if basis_change_matrix is not None:
seasonal_permutation_matrix = tf.matmul(
basis_change_matrix,
| python | {
"resource": ""
} |
q266871 | build_seasonal_transition_noise | test | def build_seasonal_transition_noise(
drift_scale, num_seasons, is_last_day_of_season):
"""Build the transition noise model for a SeasonalStateSpaceModel."""
# If the current season has just ended, increase the variance of its effect
# following drift_scale. (the just-ended seasonal effect will always be the
# bottom element of the vector). Otherwise, do nothing.
drift_scale_diag = tf.stack(
[tf.zeros_like(drift_scale)] * (num_seasons - 1) + [drift_scale],
axis=-1)
def seasonal_transition_noise(t):
| python | {
"resource": ""
} |
q266872 | build_constrained_seasonal_transition_noise | test | def build_constrained_seasonal_transition_noise(
drift_scale, num_seasons, is_last_day_of_season):
"""Build transition noise distribution for a ConstrainedSeasonalSSM."""
# Conceptually, this method takes the noise covariance on effects L @ L'
# computed by `build_seasonal_transition_noise`, with scale factor
# L = [ 0, 0, ..., 0
# ...
# 0, 0, ..., drift_scale],
# and transforms it to act on the constrained-residual representation.
#
# The resulting noise covariance M @ M' is equivalent to
# M @ M' = effects_to_residuals @ LL' @ residuals_to_effects
# where `@` is matrix multiplication. However because this matrix is
# rank-deficient, we can't take its Cholesky decomposition directly, so we'll
# construct its lower-triangular scale factor `M` by hand instead.
#
# Concretely, let `M = P @ R @ L` be the scale factor in the
# transformed space, with matrices `R`, `P` applying the reparameterization
# and zero-mean constraint respectively as defined in the
# "Mathematical Details" section of `ConstrainedSeasonalStateSpaceModel`. It's
# easy to see (*) that the implied covariance
# `M @ M' = P @ R @ L @ L' @ R' @ P'` is just the constant matrix
# `M @ M' = [ 1, 1, ..., 1, 0
# 1, 1, ..., 1, 0
# | python | {
"resource": ""
} |
q266873 | _is_empty_observation_data | test | def _is_empty_observation_data(
feature_ndims, observation_index_points, observations):
"""Returns `True` if given observation data is empty.
Emptiness means either
1. Both `observation_index_points` and `observations` are `None`, or
2. the "number of observations" shape is 0. The shape of
`observation_index_points` is `[..., N, f1, ..., fF]`, where `N` is the
number of observations and the `f`s are feature dims. Thus, we look at the
shape element just to the left of the leftmost feature dim. If that shape is
zero, we consider the data empty.
We don't check the shape of observations; validations are checked elsewhere in
the calling code, to ensure these shapes are consistent.
Args:
feature_ndims: the number of feature dims, as reported by the GP kernel.
observation_index_points: the observation data locations in the index set.
| python | {
"resource": ""
} |
q266874 | _validate_observation_data | test | def _validate_observation_data(
kernel, observation_index_points, observations):
"""Ensure that observation data and locations have consistent shapes.
This basically means that the batch shapes are broadcastable. We can only
ensure this when those shapes are fully statically defined.
Args:
kernel: The GP kernel.
observation_index_points: the observation data locations in the index set.
observations: the observation data.
Raises:
ValueError: if the observations' batch shapes are not broadcastable.
"""
# Check that observation index points and observation counts broadcast.
ndims = kernel.feature_ndims
if (tensorshape_util.is_fully_defined(
observation_index_points.shape[:-ndims]) and
| python | {
"resource": ""
} |
q266875 | SequentialSchedule.add | test | def add(self, scheduler, max_iteration, bigdl_type="float"):
"""
Add a learning rate scheduler to the contained `schedules`
:param scheduler: learning rate scheduler to be add
:param max_iteration: iteration | python | {
"resource": ""
} |
q266876 | BaseOptimizer.set_checkpoint | test | def set_checkpoint(self, checkpoint_trigger,
checkpoint_path, isOverWrite=True):
"""
Configure checkpoint settings.
:param checkpoint_trigger: the interval to write snapshots
:param checkpoint_path: the path to write snapshots into
:param isOverWrite: whether to overwrite existing snapshots in path.default is True
"""
| python | {
"resource": ""
} |
q266877 | BaseOptimizer.set_gradclip_const | test | def set_gradclip_const(self, min_value, max_value):
"""
Configure constant clipping settings.
:param min_value: the minimum value to clip by
:param max_value: the maxmimum | python | {
"resource": ""
} |
q266878 | BaseOptimizer.optimize | test | def optimize(self):
"""
Do an optimization.
"""
jmodel = callJavaFunc(self.value.optimize)
| python | {
"resource": ""
} |
q266879 | BaseOptimizer.set_train_summary | test | def set_train_summary(self, summary):
"""
Set train summary. A TrainSummary object contains information
necessary for the optimizer to know how often the logs are recorded,
where to store the logs and how to retrieve them, etc. For details,
refer to the docs of TrainSummary.
| python | {
"resource": ""
} |
q266880 | BaseOptimizer.set_val_summary | test | def set_val_summary(self, summary):
"""
Set validation summary. A ValidationSummary object contains information
necessary for the optimizer to know how often the logs are recorded,
where to store the logs and how to retrieve them, etc. For details,
refer to the docs of ValidationSummary.
| python | {
"resource": ""
} |
q266881 | Optimizer.create | test | def create(model,
training_set,
criterion,
end_trigger=None,
batch_size=32,
optim_method=None,
cores=None,
bigdl_type="float"):
"""
Create an optimizer.
Depend on the input type, the returning optimizer can be a local optimizer \
or a distributed optimizer.
:param model: the neural net model
:param training_set: (features, label) for local mode. RDD[Sample] for distributed mode.
:param criterion: the loss function
:param optim_method: the algorithm to use for optimization,
e.g. SGD, Adagrad, etc. If optim_method is None, the default algorithm is SGD.
:param end_trigger: when to end the optimization. default value is MapEpoch(1)
:param batch_size: training batch size
:param cores: This is for local optimizer only and use total physical cores as the default value
"""
if not end_trigger:
end_trigger = MaxEpoch(1)
if not optim_method:
optim_method = SGD()
if isinstance(training_set, RDD) or isinstance(training_set, DataSet):
return DistriOptimizer(model=model,
training_rdd=training_set,
criterion=criterion,
end_trigger=end_trigger,
batch_size=batch_size,
optim_method=optim_method,
| python | {
"resource": ""
} |
q266882 | Optimizer.set_traindata | test | def set_traindata(self, training_rdd, batch_size):
"""
Set new training dataset, for optimizer reuse
:param training_rdd: the training dataset
:param batch_size: training batch size
:return:
"""
| python | {
"resource": ""
} |
q266883 | TrainSummary.set_summary_trigger | test | def set_summary_trigger(self, name, trigger):
"""
Set the interval of recording for each indicator.
:param tag: tag name. Supported tag names are "LearningRate", "Loss","Throughput", "Parameters". "Parameters" is an umbrella tag thatincludes weight, bias, gradWeight, gradBias, and some running status(eg. runningMean and runningVar | python | {
"resource": ""
} |
q266884 | read_data_sets | test | def read_data_sets(train_dir, data_type="train"):
"""
Parse or download mnist data if train_dir is empty.
:param: train_dir: The directory storing the mnist data
:param: data_type: Reading training set or testing set.It can be either "train" or "test"
:return:
```
(ndarray, ndarray) representing (features, labels)
features is a 4D unit8 numpy array [index, y, x, depth] representing each pixel valued from 0 to 255.
labels is 1D unit8 nunpy array representing the label valued from 0 to 9.
```
"""
TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
if data_type == "train":
local_file = base.maybe_download(TRAIN_IMAGES, train_dir,
SOURCE_URL + TRAIN_IMAGES)
with open(local_file, 'rb') as f:
train_images = extract_images(f)
local_file = base.maybe_download(TRAIN_LABELS, train_dir,
| python | {
"resource": ""
} |
q266885 | get_news20 | test | def get_news20(source_dir="./data/news20/"):
"""
Parse or download news20 if source_dir is empty.
:param source_dir: The directory storing news data.
:return: A list of (tokens, label)
"""
news_dir = download_news20(source_dir)
texts = [] # list of text samples
label_id = 0
for name in sorted(os.listdir(news_dir)):
path = os.path.join(news_dir, name)
| python | {
"resource": ""
} |
q266886 | get_glove_w2v | test | def get_glove_w2v(source_dir="./data/news20/", dim=100):
"""
Parse or download the pre-trained glove word2vec if source_dir is empty.
:param source_dir: The directory storing the pre-trained word2vec
:param dim: The dimension of a vector
:return: A dict mapping from word to vector
"""
w2v_dir = download_glove_w2v(source_dir)
w2v_path = os.path.join(w2v_dir, "glove.6B.%sd.txt" % dim)
if sys.version_info < (3,):
| python | {
"resource": ""
} |
q266887 | KerasModel.compile | test | def compile(self, optimizer, loss, metrics=None):
"""
Configures the learning process. Must be called before fit or evaluate.
# Arguments
optimizer: Optimization method to be used. One can alternatively pass in the corresponding
string representation, such as 'sgd'.
loss: Criterion to be used. One can alternatively pass in the corresponding string
representation, such as 'mse'.
metrics: List of validation methods to be used. Default is None. One can alternatively use ['accuracy'].
"""
if isinstance(optimizer, six.string_types):
optimizer = self.__convert_optim_method(optimizer)
if isinstance(loss, six.string_types): | python | {
"resource": ""
} |
q266888 | KerasModel.fit | test | def fit(self, x, y=None, batch_size=32, nb_epoch=10, validation_data=None, distributed=True):
"""
Train a model for a fixed number of epochs on a dataset.
# Arguments
x: Input data. A Numpy array or RDD of Sample or Image DataSet.
y: Labels. A Numpy array. Default is None if x is already RDD of Sample or Image DataSet.
batch_size: Number of samples per gradient update.
nb_epoch: Number of iterations to train.
validation_data: Tuple (x_val, y_val) where x_val and y_val are both Numpy arrays.
Or RDD of Sample. Default is None if no validation is involved.
distributed: Boolean. Whether to train the model in distributed mode or local mode.
Default is True. In local mode, x and y must both be Numpy arrays.
"""
if distributed:
if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
training_data = to_sample_rdd(x, y)
if validation_data:
validation_data = to_sample_rdd(*validation_data)
elif (isinstance(x, RDD) or isinstance(x, DataSet)) and not y:
training_data = x
else:
raise TypeError("Unsupported training data type: %s" % type(x))
callBigDlFunc(self.bigdl_type, "fit",
self.value,
training_data,
batch_size,
nb_epoch,
validation_data)
| python | {
"resource": ""
} |
q266889 | KerasModel.evaluate | test | def evaluate(self, x, y=None, batch_size=32):
"""
Evaluate a model on a given dataset in distributed mode.
# Arguments
x: Input data. A Numpy array or RDD of Sample.
y: Labels. A Numpy array. Default is None if x is already RDD of Sample.
batch_size: Number of samples per gradient update.
"""
if isinstance(x, np.ndarray) and isinstance(y, np.ndarray):
evaluation_data = to_sample_rdd(x, y)
elif isinstance(x, RDD) and not y:
evaluation_data = x
| python | {
"resource": ""
} |
q266890 | KerasModel.predict | test | def predict(self, x, distributed=True):
"""
Use a model to do prediction.
# Arguments
x: Input data. A Numpy array or RDD of Sample.
distributed: Boolean. Whether to do prediction in distributed mode or local mode.
Default is True. In local mode, x must be a Numpy array.
"""
if is_distributed:
if isinstance(x, np.ndarray):
features = to_sample_rdd(x, np.zeros([x.shape[0]]))
elif isinstance(x, RDD):
features = x
| python | {
"resource": ""
} |
q266891 | get_mnist | test | def get_mnist(sc, data_type="train", location="/tmp/mnist"):
"""
Get mnist dataset and parallelize into RDDs.
Data would be downloaded automatically if it doesn't present at the specific location.
:param sc: SparkContext.
:param data_type: "train" for training data and "test" for testing data.
:param location: Location to store mnist dataset.
:return: RDD of (features: ndarray, label: ndarray).
"""
| python | {
"resource": ""
} |
q266892 | preprocess_mnist | test | def preprocess_mnist(sc, options):
"""
Preprocess mnist dataset.
Normalize and transform into Sample of RDDs.
"""
train_data = get_mnist(sc, "train", options.dataPath)\
.map(lambda rec_tuple: (normalizer(rec_tuple[0], mnist.TRAIN_MEAN, mnist.TRAIN_STD),
| python | {
"resource": ""
} |
q266893 | get_end_trigger | test | def get_end_trigger(options):
"""
When to end the optimization based on input option.
"""
| python | {
"resource": ""
} |
q266894 | validate_optimizer | test | def validate_optimizer(optimizer, test_data, options):
"""
Set validation and checkpoint for distributed optimizer.
"""
optimizer.set_validation(
batch_size=options.batchSize,
val_rdd=test_data, | python | {
"resource": ""
} |
q266895 | ModelBroadcast.value | test | def value(self):
""" Return the broadcasted value
"""
if not hasattr(self, "_value") and self._path is not None:
| python | {
"resource": ""
} |
q266896 | callBigDlFunc | test | def callBigDlFunc(bigdl_type, name, *args):
""" Call API in PythonBigDL """
gateway = _get_gateway()
error = Exception("Cannot find function: %s" % name)
for jinvoker in JavaCreator.instance(bigdl_type, gateway).value:
# hasattr(jinvoker, name) always return true | python | {
"resource": ""
} |
q266897 | callJavaFunc | test | def callJavaFunc(func, *args):
""" Call Java Function """
gateway = _get_gateway()
args = [_py2java(gateway, a) for | python | {
"resource": ""
} |
q266898 | _to_java_object_rdd | test | def _to_java_object_rdd(rdd):
""" Return a JavaRDD of Object by unpickling
It will convert each Python object into Java object by Pyrolite, whenever
the RDD is serialized in batch or not.
"""
| python | {
"resource": ""
} |
q266899 | _py2java | test | def _py2java(gateway, obj):
""" Convert Python object into Java """
if isinstance(obj, RDD):
obj = _to_java_object_rdd(obj)
elif isinstance(obj, DataFrame):
obj = obj._jdf
elif isinstance(obj, SparkContext):
obj = obj._jsc
elif isinstance(obj, (list, tuple)):
obj = ListConverter().convert([_py2java(gateway, x) for x in obj],
gateway._gateway_client)
elif isinstance(obj, dict):
result = {}
for (key, value) in obj.items():
result[key] = _py2java(gateway, value)
obj = MapConverter().convert(result, gateway._gateway_client)
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.