tf.custom_gradient accepts only one Tensor x, what if this op needs more than one inputs?
For example, to define the gradient of Softmax which needs input x and label?
Update
Thanks for the suggestion from @AllenLavoie, I use a Python list as input.
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()
It seems that it will convert the Python list to a Tensor. The snippet above will raise a 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>)
How to fix it?
xandycan both either be Tensors or sequences of Tensors. Did this not work for you?listof Tensor?list(ortuple, etc.). Solen(x)is the number of inputs to the operation,len(y)is the number of outputs. Then the gradient function takeslen(y)Tensorargument and returnslen(x)Tensors.listbut it seems like alistwill be converted as aTensor, which will cause an error if there are multiple inputs with different type and matched shape. The question has been updated.