tf.function内部if、while条件使用反斜杠换行报错

今天遇到一个诡异的问题:class中用tf.function修饰的成员函数,在使用if、while等语句时条件用‘\’符号换行会报错,demo如下。
环境:python-3.7 tensorflow-2.1.0

import tensorflow as tf

class T(object):
	@tf.function
	def test(self, x):
		if \
				x == 0:
			pass

t = T()
t.test(tf.constant(0))

控制台信息如下:
WARNING:tensorflow:AutoGraph could not transform <bound method T.test of <tensorflow.python.eager.function.TfMethodTarget object at 0x114b90650>> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output.
Cause: expected an indented block (, line 5)
Traceback (most recent call last):
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py”, line 526, in converted_call
converted_f = conversion.convert(target_entity, program_ctx)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py”, line 326, in convert
free_nonglobal_var_names)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py”, line 240, in _convert_with_cache
entity, program_ctx)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py”, line 475, in convert_entity_to_ast
nodes, name, entity_info = convert_func_to_ast(o, program_ctx)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py”, line 634, in convert_func_to_ast
node, source = parser.parse_entity(f, future_features=future_features)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/parser.py”, line 207, in parse_entity
return _attempt_to_parse_normal_source(source, future_features)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/parser.py”, line 111, in _attempt_to_parse_normal_source
return parse_str(source, preamble_len=len(future_features)), source
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/parser.py”, line 224, in parse_str
module_node = gast.parse(src)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/gast/gast.py”, line 240, in parse
return ast_to_gast(_ast.parse(*args, **kwargs))
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/ast.py”, line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File “”, line 5
pass
^
IndentationError: expected an indented block
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “”, line 1, in
File “/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py”, line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File “/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+"\n", file, ‘exec’), glob, loc)
File “/Users/guliqi/Desktop/kws/aaa.py”, line 12, in
t.test(tf.constant(0))
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py”, line 568, in call
result = self._call(*args, **kwds)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py”, line 615, in _call
self._initialize(args, kwds, add_initializers_to=initializers)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py”, line 497, in _initialize
*args, **kwds))
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py”, line 2389, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py”, line 2703, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py”, line 2593, in _create_graph_function
capture_by_value=self._capture_by_value),
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py”, line 978, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/def_function.py”, line 439, in wrapped_fn
return weak_wrapped_fn().wrapped(*args, **kwds)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py”, line 3211, in bound_method_wrapper
return wrapped_fn(*args, **kwargs)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/framework/func_graph.py”, line 964, in wrapper
user_requested=True,
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py”, line 560, in converted_call
return call_unconverted(f, args, kwargs, options)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py”, line 326, in call_unconverted
return f.self.call(args, kwargs)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/eager/function.py”, line 3173, in call
return wrapped_fn(self.weakrefself_target
(), *args, **kwargs)
File “/Users/guliqi/Desktop/kws/aaa.py”, line 8, in test
x == 0:
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py”, line 757, in bool
self._disallow_bool_casting()
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py”, line 523, in _disallow_bool_casting
"using a tf.Tensor as a Python bool“)
File “/Users/guliqi/anaconda3/envs/ai/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py”, line 510, in _disallow_when_autograph_enabled
" decorating it directly with @tf.function.”.format(task))
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a tf.Tensor as a Python bool is not allowed: AutoGraph did not convert this function. Try decorating it directly with @tf.function.

报错信息为 IndentationError: expected an indented block,但实际上不存在空格和tab的问题。

之后我做了更进一步的尝试,代码和结果如下

  1. 取消换行。无报错。
import tensorflow as tf

class T(object):
	@tf.function
	def test(self, x):
		if x == 0:
			pass

t = T()
t.test(tf.constant(0))

  1. 换行但传入参数为int而不是tf.constant。无报错但提示WARNING。
    WARNING:tensorflow:AutoGraph could not transform <bound method T.test of <tensorflow.python.eager.function.TfMethodTarget object at 0x10e798fd0>> and will run it as-is.
    Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, export AUTOGRAPH_VERBOSITY=10) and attach the full output.
    Cause: expected an indented block (, line 5)
import tensorflow as tf

class T(object):
	@tf.function
	def test(self, x):
		if \
				x == 0:
			pass

t = T()
t.test(0)
  1. 去除tf.function修饰。无报错。
class T(object):
	def test(self, x):
		if \
				x == 0:
			pass

t = T()
t.test(tf.constant(0))
  1. 作为一个普通的函数而非类的成员函数。无报错。
@tf.function
def test(x):
	if \
			x == 0:
		pass

test(tf.constant(0))

现在出错的具体原因未知,如果有知道的朋友可以留言或者一起探讨,蟹蟹。

3.31更新:google了一下发现这的确是tensorflow的bug,github上已经有人提交这个issue了。https://github.com/tensorflow/tensorflow/issues/35765

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值