百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
返回工具页/返回 Issues 列表
#482·autograd

TypeError: 无法根据类型 <class 'int'> 进行区分

作者: bgatessucks创建于 2019年2月27日更新于 2025年1月8日
标签bug

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

内容来源: HIPS/autograd

查看 GitHub 原文在 GitHub 查看讨论