# ForwardDiff with function using broadcasting

**URL:** <https://discourse.julialang.org/t/forwarddiff-with-function-using-broadcasting/48959>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 24, 2020, 10:36pm UTC](https://discourse.julialang.org/t/forwarddiff-with-function-using-broadcasting/48959 "2020-10-24T22:36:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjhoshea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mjhoshea/32/18938_2.png) [@mjhoshea](https://discourse.julialang.org/u/mjhoshea)\
**Post date:** [October 24, 2020, 10:36pm UTC](https://discourse.julialang.org/t/forwarddiff-with-function-using-broadcasting/48959/1 "2020-10-24T22:36:22Z")

</div>

I am trying to use ForwardDiff to check some analytic gradients for a mse loss and logistic hypothesis function. So just looking at the loss of a single data point we have

```julia
mse(x) = (1/2)*((y - logistic.(transpose(x)*w))^2)

```

Where x and w are dimensionally consistent arrays. When using

```julia
 ForwardDiff.gradient(mse,X)

```

Where w and y were globally defined and I pass in x I am getting very different values from what I had calculated for the gradients. Is this the correct use of ForwardDiff?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 25, 2020, 8:41am UTC](https://discourse.julialang.org/t/forwarddiff-with-function-using-broadcasting/48959/2 "2020-10-25T08:41:46Z")

</div>

Please provide an MWE.

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757/):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …
