Zygote.jl: DimensionMismatch: matrix is not square error

The bug is in gradient(c -> sum(Matrix(c*I, 5, 3)), 1), because that tries to compute tr(Matrix(I, 5, 3)).

But you don’t need this here, you can construct const m53 = Matrix(1.0I, 5, 3) once outside the gradient call.

1 Like