# ForwardDiff.derivative pointwise

**URL:** https://discourse.julialang.org/t/forwarddiff-derivative-pointwise/92867
**Category:** Performance
**Tags:** forwarddiff
**Created:** [January 12, 2023, 11:12am UTC](https://discourse.julialang.org/t/forwarddiff-derivative-pointwise/92867 "2023-01-12T11:12:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)
#### Post date: [January 12, 2023, 11:12am UTC](https://discourse.julialang.org/t/forwarddiff-derivative-pointwise/92867/1 "2023-01-12T11:12:49Z")

</div>

Hello,  
is it possible to use ForwardDiff in the following way. I have a function (a forward diff through a neural network). I want to take the second derivative of the output with respect to the input.  
If I use n input points, the n outputs are point independent.

Is it possible to use ForwardDiff for this? (The Jacobian obviously takes too many derivatives).

Thank you very much for your time.

---

<div class="post-metadata">

### Author: ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)
#### Post date: [January 12, 2023, 12:27pm UTC](https://discourse.julialang.org/t/forwarddiff-derivative-pointwise/92867/2 "2023-01-12T12:27:49Z")

</div>

Hi I wrote an own small extension for the forwardDiff api.

Maby someone will need this too:

```julia
@inline function derivative(f::F, x::AbstractArray{R}) where {F,R<:Real}
    T = typeof(ForwardDiff.Tag(f, R))
    
    dual_vec = ForwardDiff.Dual{T}.(x, ones(size(x)))

    return ForwardDiff.extract_derivative(T,f(dual_vec))
end

```

---

<div class="post-metadata">

### Author: ![c\_sell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c_sell/32/38854_2.png) [@c\_sell](https://discourse.julialang.org/u/c_sell)
#### Post date: [January 12, 2023, 1:15pm UTC](https://discourse.julialang.org/t/forwarddiff-derivative-pointwise/92867/3 "2023-01-12T13:15:48Z")

</div>

Zygote.jl cant take the derivative of a loss function containing the above code.

```julia
MethodError: no method matching Zygote.OneElement(::NamedTuple{(:value, :partials), Tuple{Nothing, Zygote.OneElement{Float64, 1, Tuple{Int64}, Tuple{Base.OneTo{Int64}}}}}, ::Tuple{Int64}, 
::Tuple{Base.OneTo{Int64}})
Closest candidates are:
  Zygote.OneElement(::T, ::I, ::A) where {N, T<:Number, I<:Tuple{Vararg{Int64, N}}, A<:Tuple{Vararg{AbstractUnitRange, N}}} at C:\Users\Lenovo\.julia\packages\Zygote\arfUC\src\lib\array.jl:69 

```

There is a problem with the namedTuple of the Partials type I guess. Is there a work around?
