Implement hessian! for scalar x
还没有人认领这个 Issue。
评估
调研方向
首先检查现有的 ForwardDiff.hessian! 入口点,以及它对数组输入使用 DiffResults.HessianResult 的方式。跟踪当前如何通过单元素 SVector 包装标量 x,然后确定预期的标量 API 和分配行为。当 f 本身不进行分配时,标量输入能够在无需包装器分配的情况下支持相同的值、梯度和 Hessian 结果,即表示完成。
由索引模型根据 Issue 内容生成。
描述
If I have a function f(x::Real)::Real I have two options for computing f(x), f'(x), f''(x):
using ForwardDiff, DiffResults, StaticArrays, BenchmarkTools
function computehessian(f, x::AbstractArray)
result = DiffResults.HessianResult(x)
result = ForwardDiff.hessian!(result, f, x)
return DiffResults.value(result), DiffResults.gradient(result), DiffResults.hessian(result)
end
computehessian(f, x::T) where T <: Number = map(first, computehessian(f ∘ first, SVector{1, T}(x)))
function computehessian2(f, x)
df(x) = ForwardDiff.derivative(f, x)
d2f(x) = ForwardDiff.derivative(df, x)
return (f(x), df(x), d2f(x))
end
x = rand()
w = rand()
s1 = rand()
s2 = rand()
f(w, s1, s2, x) = log(w * s1 * exp(-0.5 * x * s1 ^ 2) + (1-w) * s2 * exp(-0.5 * x * s2 ^ 2))
expandfunc(args, v) = args[1](args[2:end]..., v)
fixallbutlast(func, args...) = Base.Fix1(expandfunc, (func, args...))
g = fixallbutlast(f, w, s1, s2)
@btime computehessian($g, $x)
@btime computehessian2($g, $x)
69.075 ns (1 allocation: 64 bytes)
83.289 ns (0 allocations: 0 bytes)
The first approach, using the hessian! function, is faster because the value, 1st and 2nd derivatives can all be computed with a single call to f (using a DiffResult structure), whereas the second approach requires 4 calls of f to perform the same calculation. If f is slow, then the second approach is much worse. However, the first approach doesn't support scalar x::T (where T <: Number), requiring a wrapper to get around this. Furthermore, it causes an allocation, making the calculation slower than necessary.
Is it possible to implement the functionality of hessian! with a DiffResult input, but for scalar x? If so, is it possible to avoid the allocation (assuming f doesn't allocate)?
- 主要语言
- Julia
- 星标
- 1k
- 派生
- 162
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
JuliaDiff/ForwardDiff.jl 的其他 Issue
-
missing floatmin, floatmax, and issubnormal for Dual可能已有人在做 @devmotion 于 16 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
JuliaDiff/ForwardDiff.jl#852 · 6 条评论 ·
-
`fma` and `muladd` ignore NaN-safe mode可能已有人在做 @devmotion 于 2 天前认领。 未关闭
难度 3/5 1-2 天 新手友好度 55/100
JuliaDiff/ForwardDiff.jl#860 ·
-
Enable NaN-safe mode by default可能已有人在做 @devmotion 于 4 天前认领。 未关闭
难度 5/5 一周以上 新手友好度 25/100
JuliaDiff/ForwardDiff.jl#857 · 1 个 reaction ·
-
难度 4/5 3-5 天 新手友好度 52/100
JuliaDiff/ForwardDiff.jl#854 · 1 条评论 ·
-
难度 3/5 1-2 天 新手友好度 75/100
JuliaDiff/ForwardDiff.jl#849 ·
查看 JuliaDiff/ForwardDiff.jl 的全部 Issue
相似的 Issue
-
enhancement good first issue help wanted
难度 2/5 半天 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
JuliaGPU/KernelAbstractions.jl#897 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
oscar-system/GAP.jl#1455 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
JuliaAI/MLJTransforms.jl#85 ·