# Zygote gradient Error

**URL:** <https://discourse.julialang.org/t/zygote-gradient-error/38206>\
**Category:** General Usage\
**Tags:** question, flux, zygote\
**Created:** [April 25, 2020, 8:57pm UTC](https://discourse.julialang.org/t/zygote-gradient-error/38206 "2020-04-25T20:57:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amirmasud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amirmasud/32/14326_2.png) [@amirmasud](https://discourse.julialang.org/u/amirmasud)\
**Post date:** [April 25, 2020, 8:57pm UTC](https://discourse.julialang.org/t/zygote-gradient-error/38206/1 "2020-04-25T20:57:43Z")

</div>

Hi all,  
I’m trying to write an Automatic Differentiation example,  
I get this error and I don’t know how to solve it.

```julia
ERROR: Need an adjoint for constructor Transpose{Float64,Array{Float64,2}}. Gradient is of type Array{Float64,2}

```

my original code is

```julia
using LinearAlgebra
using Zygote: gradient
function gaussian_log_likelihood(μ, σ, x)
  """
  compute log-likelihood of x under N(μ,σ)
  """
  ll = -1.0/2 * ((x-μ).*(x-μ)/(σ^2) .+ log(2*pi*σ^2))
  return ll # log-likelihood function
end

function lr_model_nll(β,x,y;σ=1.)
  return sum(-gaussian_log_likelihood(Transpose(x)*β, σ, y)) # Negative Log Likelihood
end
β_test = 0.6
σ_test = 0.3
x,y = ([3.45208 9.25217 4.81545], [7.03737, 17.9838, 10.1961])
ad_grad = gradient(β_test -> lr_model_nll(β_test,x,y;σ=σ_test), β_test)
# ...

```

The output of `lr_model_nll` function is a scalar, so I don’t understand what’s the problem with this `gradient(β_test -> lr_model_nll(β_test,x,y;σ=σ_test), β_test)` line of code.

Julia version is 1.0.4 (got it from official snap)  
Flux v0.10.3, Zygote v0.4.9

Thank you so much in advance.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 25, 2020, 9:09pm UTC](https://discourse.julialang.org/t/zygote-gradient-error/38206/2 "2020-04-25T21:09:30Z")

</div>

For your case, if you use `transpose` instead of `Transpose`, it should work. `Transpose` shouldn’t usually be called directly anyways. It would probably still make sense to have an adjoint for `Transpose` and `Adjoint` in Zygote, I will see if I can make a small PR.

FYI, it’s usually a lot easier to help you track down issues, if you provide a standalone minimal working example. See also [here](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757).

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [April 25, 2020, 9:36pm UTC](https://discourse.julialang.org/t/zygote-gradient-error/38206/3 "2020-04-25T21:36:59Z")

</div>

I made a PR to add these to Zygote: [adjoints for Transpose and Adjoint constructors by simeonschaub · Pull Request #618 · FluxML/Zygote.jl · GitHub](https://github.com/FluxML/Zygote.jl/pull/618)

---

<div class="post-metadata">

**Author:** ![KapilKhanal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapilkhanal/32/43976_2.png) [@KapilKhanal](https://discourse.julialang.org/u/KapilKhanal)\
**Post date:** [December 7, 2023, 6:56am UTC](https://discourse.julialang.org/t/zygote-gradient-error/38206/4 "2023-12-07T06:56:34Z")

</div>

I still get the error with Zygote 0.6.67
