Partial of Modulo operator ? (with non-integers)

853 Views Asked by At

I am trying to derive gradient for a special neural network, but got stuck on the Modulo Arithmetic.

With usual funcitons such as $f(a,x) = a/x$ the partials would be $\frac{1}{x}$ and $-\frac{1}{x^2}$, but am struggling to find such a rule on the internet for Modulo operator. So I have:

$$f(A, x) = A\%x$$ or in other words: $$f(A, x) = mod(A, x)$$

This means the value A "wraps around" the value X several times, and spits out remainder.


The trick in my case is A or X are not integers - they can be any real number such as 0.123 etc

$$\frac{\partial f}{\partial A}f(A,x) = ?$$ $$\frac{\partial f}{\partial x}f(A,x) = ?$$

Edit: $A\%x$ Will be real number (such as 19.123 etc), can be positive, negative, or zero

2

There are 2 best solutions below

1
On BEST ANSWER

A reasonable definition of the modulo function is $$\operatorname{mod}(a,b) = a - b \left\lfloor\frac{a}{b}\right\rfloor$$ where $\lfloor\cdot\rfloor$ is the floor function.

The derivative of the floor function is $0$ at non-integer values and undefined at integer values. Thus using the usual rules of derivation, we get $$\frac{\partial}{\partial a}\operatorname{mod}(a,b) = 1\quad \text{for } \frac{a}{b}\notin\mathbb N$$ and $$\frac{\partial}{\partial b}\operatorname{mod}(a,b) = -\left\lfloor\frac{a}{b}\right\rfloor\quad \text{for } \frac{a}{b}\notin\mathbb N$$ Both partial derivatives are undefined whenever $a$ is an integer multiple of $b$.

3
On

A reasonable definition for your module-extended-to-reals is a piecewise-defined function:

$$\mod(A,x) = A -nx$$ for $A/x\le n<A/x +1$, $n\in\Bbb Z$.

Partial derivatives will exist in the interior of the chunks.