Skip to content
Advertisement

How to make a custom gradient for a multiple output function?

I would like to know how to write a custom gradient for a function which have multiple outputs( or an array). For a simple example, I wrote the following code for y=tan( x @ w + b) with x shape is (2,3) and y shape is (2,2). To compare results, I calculated the operation by usual way and by the custom gradient.

Here is the code.

JavaScript

The result of the code gives different gradients for y and y2. Obviously, I did something wrong but, could not figure out how to fix it. (When there was no tan function, y = x @ w +b, the code seems to work. But, it does not work with tan function.)

Advertisement

Answer

There is some confusion about y and the _inner_function – the inner function is x @ w + b while tan is the outer function. And since y was set to tan(...), then later yp was calculated like cos(tan(...)) which didn’t make sense.

This will give the correct results:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement