#482·autograd

TypeError: Can't differentiate w.r.t. type <class 'int'>

Author: bgatessucksCreated Feb 27, 2019Updated Jan 8, 2025
Labelsbug

I have the same problem described at the bottom with autograd 1.2:

python
from autograd import grad

print(grad(lambda x: x*x)(2.0))  # This works, output: 4.0
print(grad(lambda x: x*x)(2))  # This doesn't work
 $ python problem.py
4.0
Traceback (most recent call last):
  File "C:\dev\bin\Anaconda3\envs\py3\lib\site-packages\autograd\tracer.py", line 139, in new_box
    return box_type_mappings[type(value)](value, trace, node)
KeyError: <class 'int'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "problem.py", line 4, in <module>
    print(grad(lambda x: x*x)(2))  # This doesn't work
  File "C:\dev\bin\Anaconda3\envs\py3\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "C:\dev\bin\Anaconda3\envs\py3\lib\site-packages\autograd\differential_operators.py", line 24, in grad
    vjp, ans = _make_vjp(fun, x)
  File "C:\dev\bin\Anaconda3\envs\py3\lib\site-packages\autograd\core.py", line 10, in make_vjp
    end_value, end_node =  trace(start_node, fun, x)
  File "C:\dev\bin\Anaconda3\envs\py3\lib\site-packages\autograd\tracer.py", line 9, in trace
    start_box = new_box(x, t, start_node)
  File "C:\dev\bin\Anaconda3\envs\py3\lib\site-packages\autograd\tracer.py", line 141, in new_box
    raise TypeError("Can't differentiate w.r.t. type {}".format(type(value)))
TypeError: Can't differentiate w.r.t. type <class 'int'>
$ conda list autograd
# packages in environment at C:\dev\bin\Anaconda3\envs\py3:
#
# Name                    Version                   Build  Channel
autograd                  1.2                        py_1    conda-forge

I think @dougalm fixed this in 1d5375d. The above example seems to work!

python
In [2]: grad(lambda x:x*x)(2)
autograd/core.py:209: UserWarning: Casting int to float to handle differentiation.
  warnings.warn("Casting int to float to handle differentiation.")
Out[2]: 4.0

Originally posted by @mattjj in https://github.com/HIPS/autograd/issues/3#issuecomment-96295679