[英]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.