为了账号安全,请及时绑定邮箱和手机立即绑定

tf.custom_gradient具有多个输入

tf.custom_gradient具有多个输入

HUWWW 2021-05-15 19:16:14
tf.custom_gradient仅接受一个Tensor x,如果此操作需要多个输入呢?例如,定义需要输入x和label?的Softmax的梯度。更新感谢@AllenLavoie的建议,我使用Python列表作为输入。def self_define_op_multiple_inputs():    @tf.custom_gradient    def loss_func(input_):        x = input_[0]        label = input_[2]        def grad(dy):            return [dy, dy]        return x - label, grad    x = tf.range(10, dtype=tf.float32)    y = tf.range(10, dtype=tf.int32)    loss = loss_func([x, y])if __name__ == '__main__':    self_define_op_multiple_inputs()看来它将把Python转换list为Tensor。上面的代码段将引发TypeError: TypeError: Cannot convert a list containing a tensor of dtype <dtype: 'int32'> to <dtype: 'float32'> (Tensor is: <tf.Tensor 'range_1:0' shape=(10,) dtype=int32>)如何解决?
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 140 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号