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



##########
File path: python/mxnet/gluon/probability/distributions/distribution.py
##########
@@ -0,0 +1,196 @@
+# 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
+# pylint: disable=wildcard-import
+"""Base distribution class."""
+__all__ = ['Distribution']
+
+from .utils import cached_property
+from numbers import Number
+
+
+class Distribution(object):
+    r"""Base class for distribution.
+    
+    Parameters
+    ----------
+    F : mx.ndarray or mx.symbol.numpy._Symbol
+        Variable that stores the running mode.
+    event_dim : int, default None
+        Variable indicating the dimension of the distribution's support.
+    validate_args : bool, default None
+        Whether to validate the distribution parameters
+    """          
+
+    # Variable indicating whether the sampling method has
+    # pathwise gradient.
+    has_grad = False
+    support = None
+    has_enumerate_support = False
+    arg_constraints = {}
+    _validate_args = False
+
+    @staticmethod
+    def set_default_validate_args(value):
+        if value not in [True, False]:
+            raise ValueError
+        Distribution._validate_args = value
+
+    def __init__(self, F=None, event_dim=None, validate_args=None):
+        self.F = F

Review comment:
       @leezu 
   
   Maybe I did not make it clear in the previous comments, `Distribution` and 
`getF` both have nothing to do with `Block`, `HybridBlock` or 
`StochasticBlock`. A `Distribution` object can be used inside any types of 
blocks or even outside a block. The `getF` function is invoked in the 
initialization phase of a `Distribution` object to determine *whether to use 
`sym.np` or `nd.np` for the computation in the member function*, for example:
   
https://github.com/apache/incubator-mxnet/pull/18403/files#diff-e83cafb33610cdde004c72426d6d1492R82
   
   In short, if the parameters are Symbols, then all the methods in the object 
will use function under the `sym.np` namespace. If the parameters are NDArray, 
then we'll choose to use functions under `nd.np`. 
   
   I understand the possibility of `hybrid_forward` getting deleted in the 
future, but that deprecation hasn't reach community consensus, right? 
(https://github.com/apache/incubator-mxnet/issues/17676).
   
   Furthermore, as I demonstrate in this test case: 
https://github.com/apache/incubator-mxnet/pull/18403/files#diff-27fee32587466a570c77da139b7136f6R94-R96,
 current implementation works perfectly well with the new Gluon API (notice 
that `F` is not specified in the initialization function), that is to say, 
distribution object itself **does not rely on** the deprecated interface at all.
   
   What I can do at this moment is:
   - Create a new test file based on the new Gluon API. 
   - Raise warning to discourage users from explicitly passing `F` when 
creating a distribution object.
   
   Once `hybrid_forward` got officially deleted from the code base, I would 
then consider replacing all the `F` in this module with `mx.np` as well as 
removing `F` from the initialization function.
   
   ------------------------------
   Your suggestion on StochasticBlock is correct, current implementation is 
based on the old version of `HybridBlock`, it is necessary to make some 
modifications.




----------------------------------------------------------------
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