Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"size(x) == size(x̄) ? x̄ :" can not take the size, only length #792

Open
lisa-dot opened this issue Sep 15, 2020 · 7 comments
Open

"size(x) == size(x̄) ? x̄ :" can not take the size, only length #792

lisa-dot opened this issue Sep 15, 2020 · 7 comments

Comments

@lisa-dot
Copy link

Hello, I have written a loss function that Zygote cannot handle:
`function loss_ungelabelt2(X,Y)

a_list = Float32[]
b_list = Float32[]
mix_res = Float32[]

λ::Float32 = 0.3
loss::Float32 = 0

for i in 1:2  #eigentlich size(X,4)  
    a = X[:,:,:,rand(1:size(X,4))] 
    b = X[:,:,:,rand(1:size(X,4))]
    println(size(a))
    mix = λ .* a .+ (1 - λ) .* b
    println(length(mix))
    println(size(mix))
    println("NEU")
    mix_res=reshape([mix_res...,mix...],size(mix)...,:) # einzelne Elemente 
    a_list=reshape([a_list...,a...],size(a)...,:)
    b_list=reshape([b_list...,b...],size(b)...,:)
    println(size(mix_res))
    println(length(mix_res))
    println("neueRunde")
    
    
end
y_mix = model(mix_res)
y_a = model(a_list)
y_b = model(b_list)
println("a", size(y_a))
println("b", size(y_b))
y_mix_ab =  λ .* y_a .+ (1 - λ) .* y_b 
println(length(y_mix_ab))

quadrat_loss = sum(abs2,y_mix .- y_mix_ab)

return quadrat_loss

end

Error: MethodError: no method matching size(::NTuple{784,Float32})
Closest candidates are:
size(::Tuple, !Matched::Integer) at tuple.jl:22
size(!Matched::Flux.OneHotVector) at /Users/lisa/.julia/packages/Flux/IjMZL/src/onehot.jl:8
size(!Matched::ZMQ.Message) at /Users/lisa/.julia/packages/ZMQ/R3wSD/src/message.jl:95
...

Stacktrace:
[1] unbroadcast(::Array{Float32,3}, ::NTuple{784,Float32}) at /Users/lisa/.julia/packages/Zygote/seGHk/src/lib/broadcast.jl:53
[2] (::Zygote.var"#1107#1109"{NTuple{784,Float32}})(::Array{Float32,3}) at /Users/lisa/.julia/packages/Zygote/seGHk/src/lib/broadcast.jl:74
[3] map(::Zygote.var"#1107#1109"{NTuple{784,Float32}}, ::Tuple{Array{Float32,3},Array{Float32,3}}) at ./tuple.jl:158
[4] (::Zygote.var"#1106#1108"{Tuple{Array{Float32,3},Array{Float32,3}}})(::NTuple{784,Float32}) at /Users/lisa/.julia/packages/Zygote/seGHk/src/lib/broadcast.jl:74
[5] (::Zygote.var"#3852#back#1110"{Zygote.var"#1106#1108"{Tuple{Array{Float32,3},Array{Float32,3}}}})(::NTuple{784,Float32}) at /Users/lisa/.julia/packages/ZygoteRules/6nssF/src/adjoint.jl:49
[6] loss_ungelabelt2 at ./In[13]:14 [inlined]
[7] (::typeof(∂(loss_ungelabelt2)))(::Float32) at /Users/lisa/.julia/packages/Zygote/seGHk/src/compiler/interface2.jl:0
[8] (::Zygote.var"#41#42"{typeof(∂(loss_ungelabelt2))})(::Float32) at /Users/lisa/.julia/packages/Zygote/seGHk/src/compiler/interface.jl:45
[9] gradient(::Function, ::Array{Float32,4}, ::Vararg{Any,N} where N) at /Users/lisa/.julia/packages/Zygote/seGHk/src/compiler/interface.jl:54
[10] top-level scope at In[14]:1
[11] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091`

Someone told me that this is a Zygote bug:
"The error is this line

size(x) == size(x̄) ?:

where zygote tries to take size(x̄)
x̄ is turning out to be a Tuple which don’t have size defined on them.
Only length.
However, that is just a symptom, as that line should be fine,
because if x is an Array,
then x̄ should also be an Array.

So something is going wrong somewhere else that is causing it to be a Tuple."

If this was the case, I would be grateful if it could be fixed. Since I am currently writing my bachelor thesis and need the code for it.

@lisa-dot lisa-dot changed the title "size(x) == size(x̄) ? x̄ :" can not take the length, only length "size(x) == size(x̄) ? x̄ :" can not take the size, only length Sep 15, 2020
@mcabbott
Copy link
Member

Might be #599 again?

But avoiding splatting things of length 784 would be a good idea too, even without Zygote involved. Isn't [a_list...,a...] just vcat(a_list, a)?

@lisa-dot
Copy link
Author

I usually work less with Julia and am therefore rather inexperienced. I think instead of [a_list...,a...] I can also use vcat(a_list, a).

@mcabbott
Copy link
Member

Sure, it's hard to know the best way the first time. And doing strange things is a good to find bugs like this one, ideally this ought to work.

@DhairyaLGandhi
Copy link
Member

In some ways pushing people to not jump into performance sinks like this would be, is a nice thing, but that's just me.

Does vcat work?

@lisa-dot
Copy link
Author

vcat works for a_list and b_list, not for mix_res. But unfortunately this does not fix my error.

@DhairyaLGandhi
Copy link
Member

You might need vec for that

@lisa-dot
Copy link
Author

vec would also not work.
vec -> MethodError: no method matching vec(::Array{Float32,1}, ::Array{Float32,3})
vcat -> DimensionMismatch("cannot broadcast array to have fewer dimensions")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants