# Gradient of NN with respect to inputs

**URL:** <https://discourse.julialang.org/t/gradient-of-nn-with-respect-to-inputs/53037>\
**Category:** Machine Learning\
**Created:** [January 8, 2021, 11:26am UTC](https://discourse.julialang.org/t/gradient-of-nn-with-respect-to-inputs/53037 "2021-01-08T11:26:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sunny](https://discourse.julialang.org/u/Sunny)\
**Post date:** [January 8, 2021, 11:26am UTC](https://discourse.julialang.org/t/gradient-of-nn-with-respect-to-inputs/53037/1 "2021-01-08T11:26:19Z")

</div>

I want to take a gradient of NN but get the error

`MethodError: no method matching extract_gradient!(::Type{ForwardDiff.Tag{Chain{Tuple{Dense{typeof(relu),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}},Int64}}, ::Array{Array{ForwardDiff.Dual{ForwardDiff.Tag{Chain{Tuple{Dense{typeof(relu),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}},Int64},Float32,2},1},1}, ::Array{ForwardDiff.Dual{ForwardDiff.Tag{Chain{Tuple{Dense{typeof(relu),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}}}},Int64},Float32,2},1})`

Here is my code

```julia
function build_model(x)
    nn = Chain(Dense(2, 64, relu), Dense(64, 1))
    ∂f_∂x(input) = ForwardDiff.gradient(nn, input)
    println(∂f_∂x(x))
 end
x_vec = Vector([1,2])
build_model(x_vec)

```

---

<div class="post-metadata">

**Author:** ![Sunny](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sunny](https://discourse.julialang.org/u/Sunny)\
**Post date:** [January 8, 2021, 1:21pm UTC](https://discourse.julialang.org/t/gradient-of-nn-with-respect-to-inputs/53037/2 "2021-01-08T13:21:46Z")

</div>

Currently, I’m using jacobian instead of gradient because of a vector output, and it works
