本文整理匯總了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