0
我想在函数之间传递一个可变值用于调试目的。共享变量不起作用,因为他们的任务需要调用.eval()
或sess.run()
,而我位于我的程序和输出值的深处,使用tf.Print()
。Tensorflow:共享可变张量
假设我要输出其梯度的时刻计算层的数量,所以我试图用张量做到这一点:
# initialize shared tensor somewhere outside
LevelCounter = tf.constant(5, name ="sharedtensor", dtype=tf.int32)
#function that updates shared tensor value
def process_gradient_layer_counter(): # show current layer
#access shared tensor
current_layer = tf.get_default_graph().get_tensor_by_name("sharedtensor:0")
#modify it's value
current_layer -= 1
# My attempt: save it again using the same name. Didn't work
current_layer = tf.identity(current_layer, name ="sharedtensor")
return current_layer
# compute gradient
@tf.RegisterGradient('MyGrad')
def mygrad(op, grad):
x = op.inputs[0]
# ... compute grad_new, which I want to debug ... #
#Display layer counter
grad_new = tf.Print(grad_new, [process_gradient_layer_counter()], message = 'debug: ')
return grad_new
但不幸的是,这段代码的输出始终4.我应该怎么如何在不破坏工作流程的情况下在不同功能之间共享可变值?
谢谢!然而,py_func实际上歼灭了我的GPU,或者至少显着减慢了速度。梯度绝对是按顺序调用的,我也注意到张量名称是“sharedtensor:0”。那么也许我可以每次创建一个新节点并从这些节点访问最后一个节点?一些内存泄漏是可以接受的调试... – Slowpoke
如果没有其他的方式,那么我会去与CPU和pyfunc解决方案... – Slowpoke
,而不是添加到'sharedtensor'你可以添加到一些全球性的python变量包含最后另外 –