# Some variable not defined, even though it is

**URL:** <https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386>\
**Category:** General Usage\
**Tags:** question, scope, function\
**Created:** [May 13, 2020, 10:06am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386 "2020-05-13T10:06:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [May 13, 2020, 10:06am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/1 "2020-05-13T10:06:33Z")

</div>

Hi,

I have two similar functions, for training a Boltzmann machine. Here’s the first:

```julia
function train(ptrns, n_h, ϵ, n_G_s, steps)
    w = rand(Normal(0,0.01),size(ptrns,2)+1, n_h+1)
    h = hcat(zeros(1,n_h),1);
    hidden_activation = zeros(size(ptrns,1),steps, n_h+1)
    visible_reconstructions = zeros(size(ptrns,1), steps, size(ptrns,2)+1)
    proportions = dropdims(mean(ptrns, dims=1), dims=1);
    visible_biases = log.(proportions./(1 .- proportions));
    w[end,1:end-1] = zeros(n_h)
    w[1:end-1,end] = visible_biases
    w[end,end] = 0
    fantasy_visible = vcat(zeros(size(ptrns,2)),1)
    fantasy_hidden = vcat(zeros(n_h),1)
    for i in 1:steps 
        v = hcat(copy(ptrns),ones(size(ptrns,1)));
        for pt in 1:size(v,1)
            for unts in 1:n_h
                h[unts] = 1/(1+exp(-(v[pt,:]'*w[:,unts])))
            end
            w[end,end] = 0
            for s in 1:n_G_s
                for unts in 1:n_h
                    p = 1/(1+exp(-(fantasy_visible'*w[:,unts])))
                    fantasy_hidden[unts] = rand(Bernoulli(p))
                    end
                end
                for unts in 1:(size(v,2)-1)
                    p = 1/(1+exp(-(fantasy_hidden'*w[unts,:])))
                    fantasy_visible[unts] = rand(Bernoulli(p))
                end
            end
            w = w + ϵ*(v[pt,:]*h) - ϵ*(fantasy_visible*fantasy_hidden')
            w[end,end] = 0
            hidden_activation[pt,i,:] = h
            visible_reconstructions[pt,i,:] = v[pt,:]
        end
    end
    return w , hidden_activation, visible_reconstructions
end;

```

and I get the following error:

```julia
UndefVarError: w not defined

Stacktrace:
 [1] top-level scope at In[1]:37

```

You can see that I defined `w` at the beginning of the function, and still Julia complains about `w` not being defined. What’s bothering me is that I don’t get any error in the following similar function:

```julia
function train(ptrns, n_h, ϵ, n_G_s, steps)
    w = rand(Normal(0,0.01),size(ptrns,2)+1, n_h+1)
    h = hcat(zeros(1,n_h),1);
    hidden_activation = zeros(size(ptrns,1),steps, n_h+1)
    visible_reconstructions = zeros(size(ptrns,1), steps, size(ptrns,2)+1)
    proportions = dropdims(mean(ptrns, dims=1), dims=1);
    visible_biases = log.(proportions./(1 .- proportions));
    w[end,1:end-1] = zeros(n_h)
    w[1:end-1,end] = visible_biases
    w[end,end] = 0
    for i in 1:steps 
        v = hcat(copy(ptrns),ones(size(ptrns,1)));
        for pt in 1:size(v,1)
            for unts in 1:n_h
                p = 1/(1+exp(-(v[pt,:]'*w[:,unts])))
                h[unts] = rand(Bernoulli(p))
            end
            w = w + ϵ*(v[pt,:]*h)
            w[end,end] = 0
            for s in 1:n_G_s
                for unts in 1:(size(v,2)-1)
                    p = 1/(1+exp(-((h*w'[:,unts])[1])))
                    v[pt,unts] = p
                end
                for unts in 1:n_h
                    p = 1/(1+exp(-(v[pt,:]'*w[:,unts])))
                    if s == n_G_s
                        h[unts] = p
                    else
                        h[unts] = rand(Bernoulli(p))
                    end
                end
            end
            w = w - ϵ*(v[pt,:]*h)
            w[end,end] = 0
            hidden_activation[pt,i,:] = h
            visible_reconstructions[pt,i,:] = v[pt,:]
        end
    end
    return w , hidden_activation, visible_reconstructions
end;

```

This function has the same structure as the previous one, yet it runs without any error. What’s the problem? Does it have something to do with scope of variables? If it does, why does the second function runs normally, and how to fix the first function?  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 13, 2020, 10:33am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/2 "2020-05-13T10:33:22Z")

</div>

Could you suggest some dummy input, so we have a complete, reproducible example?

---

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [May 13, 2020, 10:44am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/3 "2020-05-13T10:44:52Z")

</div>

Sure. I should’ve given some dummy input.  
So for instance, the first input, `ptrns` could be `zeros(3,10)`, `n_h=20`, `ϵ=0.15`, `n_G_s=4`, `steps=100`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 13, 2020, 10:59am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/4 "2020-05-13T10:59:51Z")

</div>

This error appears to occur when _defining_ the function, not while running it, correct? In that case the input isn’t actually needed.

You seem to have an extra `end` loitering about, probably the one here:

```julia
for unts in 1:n_h
    p = 1/(1+exp(-(fantasy_visible'*w[:,unts])))
    fantasy_hidden[unts] = rand(Bernoulli(p))
    end # <- oops
end

```

---

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [May 13, 2020, 11:10am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/5 "2020-05-13T11:10:07Z")

</div>

Thanks! The problem’s solved. I’m so sorry! I should’ve double-checked my code, that’s really embarrassing!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 13, 2020, 11:13am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/6 "2020-05-13T11:13:19Z")

</div>

Wasn’t that easy to catch. It’s the sort of thing that a linter should help with, I think.

---

<div class="post-metadata">

**Author:** ![HoldenCaulfield](https://avatars.discourse-cdn.com/v4/letter/h/22d042/32.png) [@HoldenCaulfield](https://discourse.julialang.org/u/HoldenCaulfield)\
**Post date:** [May 13, 2020, 11:30am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/7 "2020-05-13T11:30:16Z")

</div>

Never heard of the word “linter” before. Could you please introduce one?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 13, 2020, 11:35am UTC](https://discourse.julialang.org/t/some-variable-not-defined-even-though-it-is/39386/8 "2020-05-13T11:35:44Z")

</div>

A ‘linter’ checks your code for things that may be errors or bad style, missing variable definitions, misaligned block delimiters, syntax errors, etc. A brief intro on wikipedia: [Lint (software) - Wikipedia](https://en.wikipedia.org/wiki/Lint_(software))

Both Juno and Julia for VS Code should have linters, but I don’t know how much stuff they will find.
