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>)如何解決?
添加回答
舉報
0/150
提交
取消