# Cannot use size(::Array{Int, 2}): ERROR: LoadError: UndefRefError: access to undefined reference

**URL:** https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138
**Category:** General Usage
**Tags:** question
**Created:** [June 7, 2017, 2:34pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138 "2017-06-07T14:34:21Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![fufjvnvnf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fufjvnvnf/32/2046_2.png) [@fufjvnvnf](https://discourse.julialang.org/u/fufjvnvnf)
#### Post date: [June 7, 2017, 2:34pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/1 "2017-06-07T14:34:21Z")

</div>

I keep getting undefreferror whenever I call function size. Here is my code. The error is at the first `if` in function `forward`. Full error message is:

```julia
ERROR: LoadError: UndefRefError: access to undefined reference
 in #forward#55(::Array{Any,1}, ::Function, ::NN.CrossEntropyLoss, ::Array{Float64,2}, ::Array{Int64,2}) at /Users/haonanchen/Documents/CS/NN.jl/src/layers/CrossEntropyLoss.jl:37
 in forward(::NN.CrossEntropyLoss, ::Array{Float64,2}, ::Array{Int64,2}) at /Users/haonanchen/Documents/CS/NN.jl/src/layers/CrossEntropyLoss.jl:36
...

```

```julia
type CrossEntropyLoss <: LossCriteria
  x :: Array{Float64}
  y :: Array{Float64}
  classes :: Int64

  function CrossEntropyLoss()
    return new(Float64[], Float64[], 0)
  end
end

function init(l::CrossEntropyLoss, p::Union{Layer,Void}, config::Dict{String,Any}; kwargs...)
  if p==nothing || typeof(p)<:InputLayer
    error("Loss functions cannot be the first layer; makes no sense")
  end
  out_size = getOutputSize(p)
  l.classes = out_size[2]
  l.x = Array{Float64}(out_size)
  l.y = Array{Float64}(out_size)
end

function convert_to_one_hot(old_label::Array{Int, 2})
  m = size(old_label)[1]
  new_label::Array{Int,2}
  new_label=zeros(Int64,size(old_label,2)[1],l.classes)
  for i=1:m
    new_label[i][old_label[i]+1] = 1;
  end
end

"""
Invaraint for this to work:
  Label needs to be either a vector specifying each item's class or a matrix made up with multiple one hot vectors.
  In the former case, labels need to be in the range of 0:classes-1.
"""
function forward(l::CrossEntropyLoss, Y::Array{Float64,2}, label::Array{Int, 2}; kwargs...)
  if size(label)[2] == 1
    # convert one-dim label to one hot vectors
    label = convert_to_one_hot(label)
  end
  # from now on label is guaranteed to be of one-hot
  @assert size(Y) = size(label)
  loss = zeros(m)
  m,n = size(Y)
  for i=1:m
    log_sum = 0;
    for j=1:n
      p = Y[i,j]
      q = label[i,j]
      log_sum+=q*log(q/p)
    end
    loss = log_sum/n
    loss[i]=loss
  end
  x = Y;
  y = loss;
  # generate prediction
  pred = zeros(m)
  for i=1:m
    pred[i] = findmax(Y[i,:])[2]-1
  end
  return loss, pred
end

"""
for each row x, let x_i be j^th element, loss(x)=log(q_i/x_i)/n+...(other elements)
thus d(loss_j)/dx_j=1/n*x_j/q = x_j/(q_j*n)
where j is the num of classes,
"""
function backward(l::CrossEntropyLoss, label::Array{Int, 2};kwargs...)
  dldx = zeros(classes)
  m,n=size(l.x)
  for i=1:m
    for j=:1:n
      dlidx=l.x[i,j]/(label[i,j]*classes)
      dldlx[j]+=dlidx
    end
  end
  return dldx
end

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 7, 2017, 4:16pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/2 "2017-06-07T16:16:38Z")

</div>

Please post an example that can actually be run and try remove as much unnecessary code as possible.

---

<div class="post-metadata">

### Author: ![fufjvnvnf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fufjvnvnf/32/2046_2.png) [@fufjvnvnf](https://discourse.julialang.org/u/fufjvnvnf)
#### Post date: [June 7, 2017, 4:27pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/3 "2017-06-07T16:27:18Z")

</div>

My code is rather complicated and this class lies in about 4-5 layers under the main class. I have no idea which code should be removed since the error is extremely bizarre. Within the function `forward`, same error is raised whenever `size()` is used. I even tried in `forward` create a new array `a=rand(3,4)` and call `size(a)`, which also results in the same error. I have no idea how to duplicate the error, and I am expecting it to be caused by some small syntax error in my code which could be discovered by looking solely at this class, if it’s not a bug with julia.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 7, 2017, 4:38pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/4 "2017-06-07T16:38:27Z")

</div>

`new_label::Array{Int,2}` That’s your problem line in `convert_to_one_hot`. `new_label` was not defined, hence the error `ERROR: LoadError: UndefRefError: access to undefined reference`.

Edited to add: `::T` is a type assertion not a declaration.

---

<div class="post-metadata">

### Author: ![fufjvnvnf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fufjvnvnf/32/2046_2.png) [@fufjvnvnf](https://discourse.julialang.org/u/fufjvnvnf)
#### Post date: [June 7, 2017, 4:40pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/5 "2017-06-07T16:40:10Z")

</div>

That only resulted in a `deprecated syntax` warning, which is removed once I remove that line and added a `local` to its next line, and the program still result in the old error. The line that triggers the error, as I mentioned in the original post, is the first line that calls `size` in the function `forward`.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 7, 2017, 4:46pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/6 "2017-06-07T16:46:37Z")

</div>

`@assert size(Y) = size(label)` should be `==`

Edited to add: also not sure what version of Julia you are using, but this `new_label[i][old_label[i]+1]` would give a bounds error in v0.5.

```julia
julia> zeros(Int,3,3)[1][2]
ERROR: BoundsError
 in getindex(::Int64, ::Int64) at .\number.jl:21

```

---

<div class="post-metadata">

### Author: ![fufjvnvnf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fufjvnvnf/32/2046_2.png) [@fufjvnvnf](https://discourse.julialang.org/u/fufjvnvnf)
#### Post date: [June 7, 2017, 4:50pm UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/7 "2017-06-07T16:50:56Z")

</div>

That actually solves the error! Julia’s faulty error message misguided for the entire evening. Not quite sure why the error in that line got reported way before. Error message is really not helping much here. Thank you!

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [June 8, 2017, 1:40am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/8 "2017-06-08T01:40:35Z")

</div>

I think this is yet another manifestation of a Julia feature that I have complained about several times in this forum. The statement `@assert size(Y) = size(label)` actually redefines the function `size`. The new definition is nonsense, but it means that any invocation of `size` elsewhere in the function (even before the erroneous redefinition!) will not call `Base.size`. I made a similar mistake in one of my codes last year, accidentally redefining `+` because of a one-character typo, and the error I received was separated by many lines from the typo.

Let me again request of the core Julia developers that you change the language to require more verbiage when redefining functions in Base so that it is not so easy to do it unintentionally.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 8, 2017, 1:55am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/9 "2017-06-08T01:55:39Z")

</div>

Note that this does **NOT** redefine a function. It shadows the global definition which is a must in order to make adding Base functions not being a breaking change.

---

<div class="post-metadata">

### Author: ![fufjvnvnf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fufjvnvnf/32/2046_2.png) [@fufjvnvnf](https://discourse.julialang.org/u/fufjvnvnf)
#### Post date: [June 8, 2017, 2:04am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/10 "2017-06-08T02:04:16Z")

</div>

Is it possible to make it so that the error message would be less misleading next time?

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [June 8, 2017, 2:17am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/11 "2017-06-08T02:17:18Z")

</div>

@yuyichao, I really think you should brainstorm about this with your fellow core developers. Surely there is a compromise solution.

For example, there could be a warning when a user-defined function shadows a base function, especially if the shadowing definition appears at the non-global scope. Furthermore, there could be a macro to suppress the warning. And furthermore, the warning could appear only for one-line redefinitions (i.e., no warning for the more explicit: `function size(..)`) And `@compat` could also suppress the warning. How much breakage would result in this case?

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 8, 2017, 2:21am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/12 "2017-06-08T02:21:00Z")

</div>

I get the following in v0.5 REPL. Seems pretty on point.

```julia
julia> @assert f(a) = rand()
ERROR: TypeError: non-boolean (#f) used in boolean context

```

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [June 8, 2017, 2:25am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/13 "2017-06-08T02:25:57Z")

</div>

@mohamed82008, the problem occurs if the redefinition is inside a function. For example, the code below gives an error on the `println` statement rather than on the erroneous redefinition. And the error given is the same mystery error that the original poster saw.

```julia
module test_assert

function f(x,y)
    println("size of x is ", size(x))
    @assert size(x) = size(y)
    nothing
end

end

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 8, 2017, 2:27am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/14 "2017-06-08T02:27:50Z")

</div>

Interesting. Good thing I never trust the error messages anyway!

---

<div class="post-metadata">

### Author: ![fufjvnvnf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fufjvnvnf/32/2046_2.png) [@fufjvnvnf](https://discourse.julialang.org/u/fufjvnvnf)
#### Post date: [June 8, 2017, 2:33am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/15 "2017-06-08T02:33:39Z")

</div>

LUL thats bad practice tho

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [June 8, 2017, 2:37am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/16 "2017-06-08T02:37:12Z")

</div>

Probably but occasionally useful.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 8, 2017, 2:49am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/17 "2017-06-08T02:49:49Z")

</div>

> [@Stephen\_Vavasis](#):
>
> For example, there could be a warning when a user-defined function shadows a base function, especially if the shadowing definition appears at the non-global scope.

Definitely not. This happens all the time.

> [@Stephen\_Vavasis](#):
>
> Furthermore, there could be a macro to suppress the warning. And furthermore, the warning could appear only for one-line redefinitions (i.e., no warning for the more explicit: function size(…)) And @compat could also suppress the warning. How much breakage would result in this case?

All of these are the job for a linter, and I definitely agree it should be added there if not already.

> [@mohamed82008](#):
>
> Interesting. Good thing I never trust the error messages anyway!

And that’s why we have many people posting questions where the answer is clearly in the error message.

---

<div class="post-metadata">

### Author: ![Stephen\_Vavasis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephen_vavasis/32/3389_2.png) [@Stephen\_Vavasis](https://discourse.julialang.org/u/Stephen_Vavasis)
#### Post date: [June 8, 2017, 3:01am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/18 "2017-06-08T03:01:02Z")

</div>

@yuyichao, if this statement in your previous message: “And that’s why we have many people posting questions where the answer is clearly in the error message” is referring to the current thread, then I disagree. The error message seen by the original poster is obscure. Furthermore, the first responder, @kristoffer.carlsson, is an experienced Julia programmer, and he also did not spot the mistake (nor did I).

Again, I request that you do not dismiss this issue so quickly. An important objective in language design is to make it difficult for the programmer to shoot himself/herself in the foot.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 8, 2017, 3:04am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/19 "2017-06-08T03:04:33Z")

</div>

> [@Stephen\_Vavasis](#):
>
> f this statement in your previous message: “And that’s why we have many people posting questions where the answer is clearly in the error message” is referring to the current thread,

No and that’s why I explicitly quote what I’m responding to.

> [@Stephen\_Vavasis](#):
>
> Again, I request that you do not dismiss this issue so quickly. An important objective in language design is to make it difficult for the programmer to shoot himself/herself in the foot.

And again, I definitely agree this is a good thing for a linter. Adding warning for things that can be useful and can be bad is always anony.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 8, 2017, 3:09am UTC](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138/20 "2017-06-08T03:09:50Z")

</div>

And since the OP didn’t cross link I’ll repeat my comment from [https://github.com/JuliaLang/julia/issues/22269#issuecomment-306871697](https://github.com/JuliaLang/julia/issues/22269#issuecomment-306871697) . The error message in this case is particularly bad due to [https://github.com/JuliaLang/julia/issues/20016](https://github.com/JuliaLang/julia/issues/20016) . The error message should be `ERROR: UndefVarError: size not defined`. Though still not pointing to the exact location, it is much more clear what’s the issue is and we can certainly print a better error message about it just like what we do for a method error when the name refer to a method in the enclosing scope/base. That will not require printing annoy warnings which we should not do.

[Next page](https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138.md?page=2)
