Могу ли я вызвать tf.variable_scope без оператора «with»? - PullRequest
0 голосов
/ 02 мая 2018

Я хочу вызвать Python API-интерфейсы Тензорфлоу в Matlab (см. https://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html). Matlab не поддерживает оператор with. Я не могу создать tf.variable_scope без оператора «with». Я попробовал два кода ниже, но оба не работают. Есть ли решение?

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:

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'.

Ответы [ 2 ]

0 голосов
/ 03 мая 2018

Кроме того, я пишу класс инструментов для имитации операторов with в matlab.

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:

with_vs = py.helper.With(py.tensorflow.variable_scope('X'));
with_vs.get().name
...
clear with_vs;
0 голосов
/ 02 мая 2018

Вы можете переписать контекст 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)

Но я думаю, что есть некоторые эффекты сайта.

Объяснение: Существует разница между объектом контекста vs2_obj и самим текущим контекстом vs2.

Это дает вывод

123
456
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...