tf.contrib.distributions.matrix_diag_transform
Stay organized with collections
Save and categorize content based on your preferences.
Transform diagonal of [batch-]matrix, leave rest of matrix unchanged.
tf.contrib.distributions.matrix_diag_transform(
matrix, transform=None, name=None
)
Create a trainable covariance defined by a Cholesky factor:
# Transform network layer into 2 x 2 array.
matrix_values = tf.contrib.layers.fully_connected(activations, 4)
matrix = tf.reshape(matrix_values, (batch_size, 2, 2))
# Make the diagonal positive. If the upper triangle was zero, this would be a
# valid Cholesky factor.
chol = matrix_diag_transform(matrix, transform=tf.nn.softplus)
# LinearOperatorLowerTriangular ignores the upper triangle.
operator = LinearOperatorLowerTriangular(chol)
Example of heteroskedastic 2-D linear regression.
tfd = tfp.distributions
# Get a trainable Cholesky factor.
matrix_values = tf.contrib.layers.fully_connected(activations, 4)
matrix = tf.reshape(matrix_values, (batch_size, 2, 2))
chol = matrix_diag_transform(matrix, transform=tf.nn.softplus)
# Get a trainable mean.
mu = tf.contrib.layers.fully_connected(activations, 2)
# This is a fully trainable multivariate normal!
dist = tfd.MultivariateNormalTriL(mu, chol)
# Standard log loss. Minimizing this will "train" mu and chol, and then dist
# will be a distribution predicting labels as multivariate Gaussians.
loss = -1 * tf.reduce_mean(dist.log_prob(labels))
Args |
matrix
|
Rank R Tensor , R >= 2 , where the last two dimensions are
equal.
|
transform
|
Element-wise function mapping Tensors to Tensors . To be
applied to the diagonal of matrix . If None , matrix is returned
unchanged. Defaults to None .
|
name
|
A name to give created ops. Defaults to "matrix_diag_transform".
|
Returns |
A Tensor with same shape and dtype as matrix .
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2020-10-01 UTC.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.contrib.distributions.matrix_diag_transform\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v1.15.0/tensorflow/python/ops/distributions/util.py#L521-L580) |\n\nTransform diagonal of \\[batch-\\]matrix, leave rest of matrix unchanged. \n\n tf.contrib.distributions.matrix_diag_transform(\n matrix, transform=None, name=None\n )\n\nCreate a trainable covariance defined by a Cholesky factor: \n\n # Transform network layer into 2 x 2 array.\n matrix_values = tf.contrib.layers.fully_connected(activations, 4)\n matrix = tf.reshape(matrix_values, (batch_size, 2, 2))\n\n # Make the diagonal positive. If the upper triangle was zero, this would be a\n # valid Cholesky factor.\n chol = matrix_diag_transform(matrix, transform=tf.nn.softplus)\n\n # LinearOperatorLowerTriangular ignores the upper triangle.\n operator = LinearOperatorLowerTriangular(chol)\n\nExample of heteroskedastic 2-D linear regression. \n\n tfd = tfp.distributions\n\n # Get a trainable Cholesky factor.\n matrix_values = tf.contrib.layers.fully_connected(activations, 4)\n matrix = tf.reshape(matrix_values, (batch_size, 2, 2))\n chol = matrix_diag_transform(matrix, transform=tf.nn.softplus)\n\n # Get a trainable mean.\n mu = tf.contrib.layers.fully_connected(activations, 2)\n\n # This is a fully trainable multivariate normal!\n dist = tfd.MultivariateNormalTriL(mu, chol)\n\n # Standard log loss. Minimizing this will \"train\" mu and chol, and then dist\n # will be a distribution predicting labels as multivariate Gaussians.\n loss = -1 * tf.reduce_mean(dist.log_prob(labels))\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `matrix` | Rank `R` `Tensor`, `R \u003e= 2`, where the last two dimensions are equal. |\n| `transform` | Element-wise function mapping `Tensors` to `Tensors`. To be applied to the diagonal of `matrix`. If `None`, `matrix` is returned unchanged. Defaults to `None`. |\n| `name` | A name to give created ops. Defaults to \"matrix_diag_transform\". |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` with same shape and `dtype` as `matrix`. ||\n\n\u003cbr /\u003e"]]