简体   繁体   English

我可以不带“ with”语句调用tf.variable_scope吗?

[英]Can I call tf.variable_scope without a “with” statement?

I want to call tensorflow's python APIs in matlab (see https://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html ). 我想在matlab中调用tensorflow的python API(请参阅https://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html )。 The matlab does not support the "with" statement. Matlab不支持“ with”语句。 I can not create a tf.variable_scope without a "with" statement. 没有“ with”语句,我无法创建tf.variable_scope。 I have tried the two codes below, but both do not work. 我已经尝试了下面的两个代码,但两个都不起作用。 Is there any solution? 有什么解决办法吗?

Python: 蟒蛇:

import tensorflow as tf
with tf.variable_scope('123') as vs:
    print(vs.name)  # OK
vs2 = tf.variable_scope('456')
print(vs2.name)  # AttributeError: 'variable_scope' object has no attribute 'name'

Matlab: Matlab的:

vs = py.tensorflow.variable_scope('GRAPH', pyargs('reuse', py.tensorflow.AUTO_REUSE));
vs.name  % No appropriate method, property, or field 'name' for class 'py.tensorflow.python.ops.variable_scope.variable_scope'.

You can rewrite the python context like 您可以像这样重写python上下文

import tensorflow as tf

with tf.variable_scope('123') as vs:
    print(vs.name)  # OK


vs2_obj = tf.variable_scope('456')
vs2 = vs2_obj.__enter__()
try:
    print(vs2.name)  # OK as well
finally:
    vs2_obj.__exit__(None, None, None)

But I guess there are some site-effects. 但是我想这有一些网站效果。

Explanation: There is a difference between a context-object vs2_obj and the current context vs2 itself. 说明:上下文对象vs2_obj与当前上下文vs2本身之间存在差异。

This gives the output 这给出了输出

123
456

Also, I write a tool class to mimic "with" statements in matlab. 另外,我编写了一个工具类来模仿matlab中的“ with”语句。

helper.py: helper.py:

class With(object):
    def __init__(self, obj):
        self._obj = obj
        self._p = self._obj.__enter__()

    def __del__(self):
        self._obj.__exit__(None, None, None)

    def get(self):
        return self._p

Matlab: Matlab的:

with_vs = py.helper.With(py.tensorflow.variable_scope('X'));
with_vs.get().name
...
clear with_vs;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 tf.layers 变量放入 tf.name_scope/tf.variable_scope? - How to put tf.layers variables in tf.name_scope/tf.variable_scope? Tensorflow 中的 tf.variable_scope 和 variable_scope.variable_scope 有什么区别? - What is difference between tf.variable_scope and variable_scope.variable_scope in Tensorflow? tf.variable_scope()和tf.train.Saver()有什么区别? - What is the difference between tf.variable_scope() and tf.train.Saver()? TensorFlow 2.0:如何使用tf.keras对图表进行分组? tf.name_scope / tf.variable_scope不再使用了? - TensorFlow 2.0: how to group graph using tf.keras? tf.name_scope/tf.variable_scope not used anymore? TensorFlow为什么我们已经拥有函数tf.variable_scope时仍然使用tf.name_scope - TensorFlow why we still use tf.name_scope when we already have the function tf.variable_scope 为什么tf.variable_scope具有default_name参数? - Why does tf.variable_scope has a default_name argument? 哪个初始值设定项受tf.variable_scope影响(“Model”,reuse = None,initializer = initializer)? - Which initializers are affected by tf.variable_scope(“Model”, reuse=None, initializer=initializer)? “with”语句中变量的 Scope? - Scope of variable within "with" statement? Tensorflow:tf.name_scope 没有“with” - Tensorflow: tf.name_scope without 'with' Tensorflow:如何在不包装的情况下使用 tf.roll? - Tensorflow: How can I use tf.roll without wrapping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM