utils.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:LiTeFlow 作者: petrux 项目源码 文件源码
def as_scope(scope):
    """Get the proper variable scope.

    Given an object that can represent a `tf.VariableScope`,
    namely a `str` or a `tf.VariableScope`, performs type checking
    and return a proper `tf.VariableScope` object. Such function is
    hancy when a function accepts an argument serving as a variable
    scope but doesn's know its proper type.

    Arguments:
      scope: a `str` or a `tf.VariableScope` representing a variable scope.

    Returns:
      a `tf.VariableScope` instance.

    Raises:
      ValueError: if `scope` is `None`.
      TypeError: if `scope` is neither `str` or `tf.VariableScope`.

    Example:
    ```python
    from dket import utils

    def do_something(scope):
        scope = utils.as_scope(scope or 'DefaultScope')
        with tf.variable_scope(scope) as scope:
            # do something
            pass
"""
if scope is None:
    raise ValueError('Cannot create a scope from a None.')
if isinstance(scope, str):
    return next(tf.variable_scope(scope).gen)  # pylint: disable=I0011,E1101
if isinstance(scope, tf.VariableScope):
    return scope
raise TypeError("""`scope` argument can be of type str, """
                """tf.VariableScope, while %s found.""",
                (str(type(scope))))

```

评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号