# Getting strange "UndefVarError: xs not defined" in Zygote / Flux

**URL:** <https://discourse.julialang.org/t/getting-strange-undefvarerror-xs-not-defined-in-zygote-flux/67707>\
**Category:** Machine Learning\
**Created:** [September 5, 2021, 4:57am UTC](https://discourse.julialang.org/t/getting-strange-undefvarerror-xs-not-defined-in-zygote-flux/67707 "2021-09-05T04:57:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![outlace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outlace/32/2216_2.png) [@outlace](https://discourse.julialang.org/u/outlace)\
**Post date:** [September 5, 2021, 4:57am UTC](https://discourse.julialang.org/t/getting-strange-undefvarerror-xs-not-defined-in-zygote-flux/67707/1 "2021-09-05T04:57:20Z")

</div>

I’ve got a fairly simple custom 2-layer MLP implementation but I get this strange error when trying to get the gradient:

```julia
LoadError: UndefVarError: xs not defined
UndefVarError: xs not defined

```

Here’s my full code

```julia
using Flux
using Zygote
using LinearAlgebra;

mutable struct Layer
    theta::Matrix{Float64}
    bias::Matrix{Float64}
end

layer1 = Layer(randn(784,512),randn(1,512))
layer2 = Layer(randn(512,10),randn(1,10))
layers = [layer1,layer2];

function run_layer(X::Matrix,layer::Layer, cols::Vector{Int64}, rows::Vector{Int64})
    e1 = isempty(cols)
    e2 = isempty(rows)
    bias = e1 ? layer.bias : layer.bias[:,cols]
    theta = e1 ? layer.theta : layer.theta[:,cols]
    theta = e2 ? theta : theta[rows,:]
    y_ = X * theta .+ bias 
    return y_, cols
end

function model(X::Array,layers::Vector{Layer}, S::Vector)
    A1, c1 = run_layer(X,layers[1],S,Vector{Int64}([]))
    A1 = Flux.normalise(A1;dims=ndims(A1), ϵ=1e-5)
    A1 = NNlib.relu.(A1)
    A2, c2 = run_layer(A1,layers[2],Vector{Int64}([]),c1)
    A2 = Flux.normalise(A2;dims=ndims(A2), ϵ=1e-5)
    A2 = NNlib.softmax(A2,dims=2)
    return A2
end

lossfn(ŷ::Vector{Float64},y::Vector{Float64}) = -1.0 * LinearAlgebra.dot(log.(ŷ),y)

S = [1,50,90,112,145,240,300,301,500,505]
g = Zygote.gradient(w -> lossfn(vec(model(randn(1,784),w,S)),[1.0,0,0,0,0,0,0,0,0,0]),layers)
# error

```

I appreciate any guidance on this

---

<div class="post-metadata">

**Author:** ![outlace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outlace/32/2216_2.png) [@outlace](https://discourse.julialang.org/u/outlace)\
**Post date:** [September 5, 2021, 8:42pm UTC](https://discourse.julialang.org/t/getting-strange-undefvarerror-xs-not-defined-in-zygote-flux/67707/2 "2021-09-05T20:42:23Z")

</div>

I was able to re-write my code to do the same but not get this error, not sure why. I might have been in essence mutating an array and just getting a poor error message for that.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [September 5, 2021, 9:36pm UTC](https://discourse.julialang.org/t/getting-strange-undefvarerror-xs-not-defined-in-zygote-flux/67707/3 "2021-09-05T21:36:07Z")

</div>

Looks like it was supposed to be a mutation error, but a typo released in v0.6.15 made the error message formatting itself fail. Should be fixed in v0.6.18 and up after [https://github.com/FluxML/Zygote.jl/pull/1038](https://github.com/FluxML/Zygote.jl/pull/1038).

P.S. if you wouldn’t mind some unsolicited suggestions for the code itself, `Flux.normalise` is the only function that needs to be qualified to use. The rest are all exported by default, e.g. Flux reexports `gradient` and `relu` from Zygote and NNlib respectively. With respect to type annotations, the general convention is to [use them judiciously](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-writing-overly-specific-types) (exceptions documented [here](https://docs.julialang.org/en/v1/manual/performance-tips/#Type-declarations)) so that the model can work with Float32 arrays, GPU arrays, array views etc. without any implementation changes.

---

<div class="post-metadata">

**Author:** ![outlace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/outlace/32/2216_2.png) [@outlace](https://discourse.julialang.org/u/outlace)\
**Post date:** [September 5, 2021, 10:18pm UTC](https://discourse.julialang.org/t/getting-strange-undefvarerror-xs-not-defined-in-zygote-flux/67707/4 "2021-09-05T22:18:41Z")

</div>

Thanks for the explanation and thanks for the code suggestions! I was under the mistaken impression that Julia wanted me to write the most specific type annotations for performance reasons but I see from the docs that is wrong
