本文整理汇总了Python中sonnet.AbstractModule方法的典型用法代码示例。如果您正苦于以下问题:Python sonnet.AbstractModule方法的具体用法?Python sonnet.AbstractModule怎么用?Python sonnet.AbstractModule使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sonnet
的用法示例。
在下文中一共展示了sonnet.AbstractModule方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __str__
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import AbstractModule [as 别名]
def __str__(self):
if isinstance(self._module, tf.Tensor):
return str(self._module)
if isinstance(self._module, types.LambdaType):
return self._module.__name__
if isinstance(self._module, snt.AbstractModule):
return self._module.module_name
if hasattr(self._module, '__class__'):
return self._module.__class__.__name__
return str(self._module)
示例2: __init__
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import AbstractModule [as 别名]
def __init__(self, *args, **kwargs):
super(AbstractModule, self).__init__(*args, **kwargs)
self.__call__.__func__.__doc__ = self._build.__doc__
# In snt2 calls to `_enter_variable_scope` are ignored.
示例3: create_variables_in_class_scope
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import AbstractModule [as 别名]
def create_variables_in_class_scope(method):
"""Force the variables constructed in this class to live in the sonnet module.
Wraps a method on a sonnet module.
For example the following will create two different variables.
```
class Mod(snt.AbstractModule):
@create_variables_in_class_scope
def dynamic_thing(self, input, name):
return snt.Linear(name)(input)
mod.dynamic_thing(x, name="module_nameA")
mod.dynamic_thing(x, name="module_nameB")
# reuse
mod.dynamic_thing(y, name="module_nameA")
```
"""
@functools.wraps(method)
def wrapper(obj, *args, **kwargs):
def default_context_manager(reuse=None):
variable_scope = obj.variable_scope
return tf.variable_scope(variable_scope, reuse=reuse)
variable_scope_context_manager = getattr(obj, "_enter_variable_scope",
default_context_manager)
graph = tf.get_default_graph()
# Temporarily enter the variable scope to capture it
with variable_scope_context_manager() as tmp_variable_scope:
variable_scope = tmp_variable_scope
with variable_scope_ops._pure_variable_scope(
variable_scope, reuse=tf.AUTO_REUSE) as pure_variable_scope:
name_scope = variable_scope.original_name_scope
if name_scope[-1] != "/":
name_scope += "/"
with tf.name_scope(name_scope):
sub_scope = snt_util.to_snake_case(method.__name__)
with tf.name_scope(sub_scope) as scope:
out_ops = method(obj, *args, **kwargs)
return out_ops
return wrapper