python可变对象自定义对象_Tensorflow自定义操作过程参数作为可变对象(如按引用传递)...

我尝试在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]

一。在

一。在

有人能帮我解决这个问题吗?-提前还款:)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值