Implement hessian! for scalar x
まだ誰も着手していません。
評価
調査の方向性
まず、既存の ForwardDiff.hessian! エントリポイントと、配列入力に対する DiffResults.HessianResult の使用方法を調べます。スカラー x が現在、1要素の SVector を介してどのようにラップされているかを追跡し、その後、想定されるスカラー 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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 1週間以上 初心者へのやさしさ 25/100
JuliaDiff/ForwardDiff.jl#857 · リアクション 1 件 ·
-
難易度 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
-
難易度 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 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
SciML/OrdinaryDiffEq.jl#4875 ·
メンテナーはふだん 4 日以内に返信