Zygote gradient Error

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

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

my original code is

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.

1 Like

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.

1 Like

I made a PR to add these to Zygote: adjoints for Transpose and Adjoint constructors by simeonschaub · Pull Request #618 · FluxML/Zygote.jl · GitHub

1 Like

I still get the error with Zygote 0.6.67