xidulu commented on a change in pull request #18403:
URL: https://github.com/apache/incubator-mxnet/pull/18403#discussion_r432805069



##########
File path: python/mxnet/gluon/probability/transformation/transformation.py
##########
@@ -0,0 +1,289 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# coding: utf-8
+"""Transformation Classes"""
+__all__ = ["Transformation", "TransformBlock","ComposeTransform", 
"ExpTransform",
+           "AffineTransform", "PowerTransform", "AbsTransform", 
'SigmoidTransform',
+           'SoftmaxTransform']
+
+from ..distributions.utils import _clip_prob, cached_property, sum_right_most
+from ...block import HybridBlock
+import weakref
+
+
+class Transformation(object):
+    r"""Abstract class for implementing invertible transformation
+    with computable log  det jacobians
+    
+    Attributes
+    ----------
+    bijective : bool
+        
+    """
+    bijective = False
+    event_dim = 0
+
+    def __init__(self, F=None):
+        self._inv = None
+        self._F = F
+        super(Transformation, self).__init__()
+
+    @property
+    def F(self):
+        return self._F
+
+    @F.setter
+    def F(self, value):
+        self._F = value
+
+    @property
+    def sign(self):
+        """
+        Returns the sign of the determinant of the Jacobian.
+        """
+        raise NotImplementedError
+
+    @property
+    def inv(self):
+        inv = None
+        if self._inv is not None:
+            inv = self._inv()
+        if inv is None:
+            inv = _InverseTransformation(self)
+            self._inv = weakref.ref(inv)
+        return inv
+
+    def __call__(self, x):
+        return self._forward_compute(x)
+
+    def _inv_call(self, y):
+        return self._inverse_compute(y)
+
+    def _forward_compute(self, x):
+        raise NotImplementedError
+
+    def _inverse_compute(self, x):
+        raise NotImplementedError
+    
+    def log_det_jacobian(self, x, y):
+        """
+        Compute the value of log(|dy/dx|)
+        """
+        raise NotImplementedError
+
+
+class _InverseTransformation(Transformation):
+    """
+    A private class representing the invert of `Transformation`,
+    which should be accessed through `Transformation.inv` property.
+    """
+    def __init__(self, forward_transformation):
+        super(_InverseTransformation, self).__init__()
+        self._inv = forward_transformation
+
+    @property
+    def inv(self):
+        return self._inv
+
+    @property
+    def sign(self):
+        return self._inv.sign
+
+    @property
+    def event_dim(self):
+        return self._inv.event_dim
+
+    def __call__(self, x):
+        return self._inv._inverse_compute(x)
+
+    def log_det_jacobian(self, x, y):
+        return -self._inv.log_det_jacobian(y, x)
+
+
+class TransformBlock(Transformation, HybridBlock):

Review comment:
       I did not check whether setting the prefix would have any issues, but 
the TransformBlock works well, here is an example 
https://github.com/xidulu/mystery_demo/blob/master/real_nvp.ipynb




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to