edward源代码中class Normal的位置在哪里?
where is the location of class Normal in edward's source code?
在使用edward时,我们总是使用from edward.models import Normal
,但是我在github
中没有找到Normal
的声明
谁能告诉我它在哪里
它们在 edward/models/random_variables.py
中定义。
你像这样导入 Normal
class:
from edward.models import Normal
这建议查看 edward/models/__init__.py
,其中有这一行:
from edward.models.random_variables import *
查看 edward/models/random_variables.py
我们发现 this code:
from edward.models.random_variable import RandomVariable as _RandomVariable
from tensorflow.contrib import distributions as _distributions
# Automatically generate random variable classes from classes in
# tf.contrib.distributions.
_globals = globals()
for _name in sorted(dir(_distributions)):
_candidate = getattr(_distributions, _name)
if (_inspect.isclass(_candidate) and
_candidate != _distributions.Distribution and
issubclass(_candidate, _distributions.Distribution)):
# to use _candidate's docstring, must write a new __init__ method
def __init__(self, *args, **kwargs):
_RandomVariable.__init__(self, *args, **kwargs)
__init__.__doc__ = _candidate.__init__.__doc__
_params = {'__doc__': _candidate.__doc__,
'__init__': __init__}
_globals[_name] = type(_name, (_RandomVariable, _candidate), _params)
del _candidate
这通过 tensorflow.contrib.distributions
模块寻找从 tensorflow.contrib.distributions.Distribution
派生的 classes(忽略其他属性,例如模块的 __file__
成员,或基Distribution
class 本身)。对于每一个,它都会进行一些修改(这只会影响生成的文档)然后执行这个关键行:
_globals[_name] = type(_name, (_RandomVariable, _candidate), _params)
type()
built-in function creates a new type i.e. declares a new class. The second parameter is the list of base classes, which here is edward's RandomVariable
class and the TensorFlow random variable class. Earlier it defined _globals
to be globals()
,这是一个返回模块变量字典的内置函数。因此,如果您感兴趣,上面的行等同于以下内容:
from edward.models.random_variable import RandomVariable as EdRandVar
from tensorflow.contrib.distributions import Normal as TfNormal
Normal = type("Normal", (EdRandVar, TfNormal), {...})
这又等同于此(如果您忽略文档字符串内容):
from edward.models.random_variable import RandomVariable as EdRandVar
from tensorflow.contrib.distributions import Normal as TfNormal
class Normal(EdRandVar, TfNormal):
pass
在使用edward时,我们总是使用from edward.models import Normal
,但是我在github
Normal
的声明
谁能告诉我它在哪里
它们在 edward/models/random_variables.py
中定义。
你像这样导入 Normal
class:
from edward.models import Normal
这建议查看 edward/models/__init__.py
,其中有这一行:
from edward.models.random_variables import *
查看 edward/models/random_variables.py
我们发现 this code:
from edward.models.random_variable import RandomVariable as _RandomVariable
from tensorflow.contrib import distributions as _distributions
# Automatically generate random variable classes from classes in
# tf.contrib.distributions.
_globals = globals()
for _name in sorted(dir(_distributions)):
_candidate = getattr(_distributions, _name)
if (_inspect.isclass(_candidate) and
_candidate != _distributions.Distribution and
issubclass(_candidate, _distributions.Distribution)):
# to use _candidate's docstring, must write a new __init__ method
def __init__(self, *args, **kwargs):
_RandomVariable.__init__(self, *args, **kwargs)
__init__.__doc__ = _candidate.__init__.__doc__
_params = {'__doc__': _candidate.__doc__,
'__init__': __init__}
_globals[_name] = type(_name, (_RandomVariable, _candidate), _params)
del _candidate
这通过 tensorflow.contrib.distributions
模块寻找从 tensorflow.contrib.distributions.Distribution
派生的 classes(忽略其他属性,例如模块的 __file__
成员,或基Distribution
class 本身)。对于每一个,它都会进行一些修改(这只会影响生成的文档)然后执行这个关键行:
_globals[_name] = type(_name, (_RandomVariable, _candidate), _params)
type()
built-in function creates a new type i.e. declares a new class. The second parameter is the list of base classes, which here is edward's RandomVariable
class and the TensorFlow random variable class. Earlier it defined _globals
to be globals()
,这是一个返回模块变量字典的内置函数。因此,如果您感兴趣,上面的行等同于以下内容:
from edward.models.random_variable import RandomVariable as EdRandVar
from tensorflow.contrib.distributions import Normal as TfNormal
Normal = type("Normal", (EdRandVar, TfNormal), {...})
这又等同于此(如果您忽略文档字符串内容):
from edward.models.random_variable import RandomVariable as EdRandVar
from tensorflow.contrib.distributions import Normal as TfNormal
class Normal(EdRandVar, TfNormal):
pass