我正在尝试使用@ function.Defun装饰器在tensorflow中定义自己的操作,因为我需要为它配备自定义渐变。但是,在我的代码中,我还需要调用tf.map_fn函数,将特定函数应用于一批数据。为简单起见,我尝试做类似的事情:
def add_func(x):
return x+1
@function.Defun(tf.float32)
def test(a):
return tf.map_fn(add, a)
with tf.Session() as sess:
a = tf.ones(shape=(6,1))
res = sess.run(test(a))
这个代码在没有@ function.Defun装饰器的情况下运行正常,但是使用装饰器我得到以下错误:
InvalidArgumentError: 25 nodes in a cycle
[[Node: test_8028ca0d_2 = test_8028ca0d[_device="/job:localhost/replica:0/task:0/cpu:0"](ones_4)]]
Caused by op 'test_8028ca0d_2', defined at:
File "C:\Users\Nicki\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 223, in <module>
main()
File "C:\Users\Nicki\Anaconda3\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 219, in main
kernel.start()
File "C:\Users\Nicki\Anaconda3\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
ioloop.IOLoop.instance().start()
File "C:\Users\Nicki\Anaconda3\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
super(ZMQIOLoop, self).start()
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tornado\ioloop.py", line 887, in start
handler_func(fd_obj, events)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
self._handle_recv()
File "C:\Users\Nicki\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
self._run_callback(callback, msg)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
callback(*args, **kwargs)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
return fn(*args, **kwargs)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
return self.dispatch_shell(stream, msg)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
handler(stream, idents, msg)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
user_expressions, allow_stdin)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_ast_nodes
if self.run_code(code, result):
File "C:\Users\Nicki\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-19-c5e48d04d428>", line 1, in <module>
runfile('C:/Users/Nicki/.spyder-py3/temp.py', wdir='C:/Users/Nicki/.spyder-py3')
File "C:\Users\Nicki\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Nicki/.spyder-py3/temp.py", line 156, in <module>
res = sess.run(test(a))
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tensorflow\python\framework\function.py", line 618, in __call__
return _call(self._definition.signature, *args, **kwargs)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tensorflow\python\framework\function.py", line 271, in _call
compute_shapes=False)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2240, in create_op
original_op=self._default_original_op, op_def=op_def)
File "C:\Users\Nicki\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 1128, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): 25 nodes in a cycle
[[Node: test_8028ca0d_2 = test_8028ca0d[_device="/job:localhost/replica:0/task:0/cpu:0"](ones_4)]]
答案 0 :(得分:1)
为了关闭这个问题的循环,discussion on GitHub显示问题存在于旧版本的TensorFlow(0.12或更早版本)中。升级到TensorFlow 1.0可以解决问题。