Arctan class in Theano

Hello everyone,

I am trying to build a couple of models in PyMC3 which use functions that are not readily available in pymc3.math or in theano.tensor. One simple example here is arctan . Following this thread I have built a ArcTan class which implements the function (from numpy) and the gradient, which in this case is very simple.

Here is my code:

import theano
import theano.tensor as tt
import theano.tests.unittest_tools

class ArcTan(tt.Op):
    itypes = [tt.dscalar]
    otypes = [tt.dscalar]
    
    def perform(self, node, inputs, outputs):
        x, = inputs
        y = np.arctan(x)
        outputs[0][0] = np.array(y)

    def grad(self, inputs, g):
        x, = inputs
        return [1 / (1 + x ** 2) ]

This looks simple enough, however when I check the gradient I get the following error:

theano.tests.unittest_tools.verify_grad(ArcTan(), [np.array(0.2)])

GradientError: GradientError: numeric gradient and analytic gradient exceed tolerance:
    At position 0 of argument 0 with shape (),
        val1 = 0.961538      ,  val2 = 1.154266
        abs. error = 0.192728,  abs. tolerance = 0.000100
        rel. error = 0.091090,  rel. tolerance = 0.000100
Exception args: 
The error happened with the following inputs:, [array(0.2)], 
The value of eps is:, None, 
The out_type is:, None

The correct value (both analytical and from numerical gradient in numpy) is the first one, 0.96. Can anyone tell me what is wrong here?

Hi,

for what it’s worth, even though it is not listed in the documentation, theano actually has an arctan function: theano.tensor.arctan.

1 Like