1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Identity bijector.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21from tensorflow.python.framework import constant_op 22from tensorflow.python.ops.distributions import bijector 23from tensorflow.python.util.tf_export import tf_export 24 25 26__all__ = [ 27 "Identity", 28] 29 30 31@tf_export("distributions.bijectors.Identity") 32class Identity(bijector.Bijector): 33 """Compute Y = g(X) = X. 34 35 Example Use: 36 37 ```python 38 # Create the Y=g(X)=X transform which is intended for Tensors with 1 batch 39 # ndim and 1 event ndim (i.e., vector of vectors). 40 identity = Identity(event_ndims=1) 41 x = [[1., 2], 42 [3, 4]] 43 x == identity.forward(x) == identity.inverse(x) 44 ``` 45 46 """ 47 48 def __init__(self, validate_args=False, event_ndims=0, name="identity"): 49 super(Identity, self).__init__( 50 is_constant_jacobian=True, 51 event_ndims=event_ndims, 52 validate_args=validate_args, 53 name=name) 54 55 def _forward(self, x): 56 return x 57 58 def _inverse(self, y): 59 return y 60 61 def _inverse_log_det_jacobian(self, y): 62 return constant_op.constant(0., dtype=y.dtype) 63 64 def _forward_log_det_jacobian(self, x): 65 return constant_op.constant(0., dtype=x.dtype) 66