我尝试在tensorflow中开发一个定制的op,在这个过程中,我更改属于输入张量的值,并且在op完成之后,它仍然应该反映这些更改。在
问题是,python通过赋值(=在我们的例子中是不可变类型)传递函数参数,从而阻止我访问在op中实际想要访问的数据
一。在
最小工作示例:
测试_脚本.pyimg = imread("some image path");
img = ((img_gt[:, : , 0:3]).flatten()).astype('float32')
img[0] = 50
print "First time (start value): ", img[0]
Custom_Loss_Module = tf.load_op_library('some shared library')
with tf.Session(''):
Custom_Loss_Module.custom_loss(img)
print "Fourth time (after custom op): ", img[0]
自定义操作_主.cpp
^{pr2}$
输出:First time (start value): 50
Second time (in op): 50
Third time (in op): 100
Fourth time (after custom op): 50
一。在
如上所述,我知道这种行为是从何而来的,我知道这是意料之中的。但是,我想找到一种方法,使从50到100的变化(在自定义操作中完成)是可变的,因此一旦找到自定义操作,就会被img张量反映出来。在C++中:我想通过引用传递函数参数,而不是赋值。在
一。在
我目前所做的努力:
我尝试使用标准的python技巧并将参数作为列表传递,因为列表是可变的。示例:
测试_脚本.pyimg = imread("some image path");
img = ((img_gt[:, : , 0:3]).flatten()).astype('float32')
img[0] = 50
print "First time (start value): ", img[0]
Custom_Loss_Module = tf.load_op_library('some shared library')
with tf.Session(''):
Custom_Loss_Module.custom_loss([img])
print "Fourth time (after custom op): ", img[0]
一。在
一。在
有人能帮我解决这个问题吗?-提前还款:)