Akshat Mehrotra
Akshat Mehrotra

Reputation: 141

Strange and uninformative error in my simple Julia-Flux dense model

Most of my code is a direct copy from the Flux model zoo repo ( this example in particular https://github.com/FluxML/model-zoo/blob/master/vision/cifar10/cifar10.jl) .

I'm new here so I have no Idea what can be causing this error. I'm using a custom dataset of images which are of random sizes. I want to categorize them into 30 classes. Just for testing purposes, I have set the test and train size to be 20.

using Flux
using Statistics
using Statistics: mean
using Base.Iterators: partition
using BSON
using CSV
using Images

# defining some variables not really important
train_path = "G:\\Book-Cover-Train"
test_path = "G:\\Book-Cover-Test"
train_set = CSV.File("train.csv")
test_set = CSV.File("test.csv")
train_size = 20
test_size = 20
acc = 0


function getarray(path, number)
    # code to get image based on the number and convert it to float # not really important to question
    file = load(path*"\\img$number.png")
    file = imresize(file, 100,100)
    X = convert(Array{Float64},channelview(file))
    X = permutedims(X, (2,3,1))

    individual_image_in_float = X
    return individual_image_in_float
end

imgs = [getarray(path_to_training_set, i) for i in 1:train_set_size] 
labels = onehotbatch([train_set[i][6] for i in 1:train_set_size],0:29) # every row in training set csv has the 6th column as the label
train = [(cat(imgs[i]..., dims = 4), labels[:,i]) for i in partition(1:train_size, 100)]

@info("Constructing model...")

model = Chain(
    Dense(100*100*3, 64, relu),
    Dense(64, 30),
  softmax) 

loss(x, y) = crossentropy(model(x), y)  

@info("Beginning training loop...")

for epoch_idx in 1:4
    println("epoch number $epoch_idx")
    Flux.train!(loss, params(model), train, ADAM())#, cb = evalcb)
end
BSON.@save pwd()*"\\model-final.bson" model)

The error message is

MethodError: no method matching *(::Array{Float32,2}, ::Array{Float32,4})
Stacktrace:
 [1] macro expansion at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0 [inlined]
 [2] _pullback(::Zygote.Context, ::typeof(*), ::Array{Float32,2}, ::Array{Float32,4}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:7
 [3] Dense at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\layers\basic.jl:102 [inlined]
 [4] _pullback(::Zygote.Context, ::typeof(invoke), ::Dense{typeof(relu),Array{Float32,2},Array{Float32,1}}, ::Type{Tuple{AbstractArray}}, ::Array{Float32,4}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [5] Dense at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\layers\basic.jl:113 [inlined]
 [6] _pullback(::Zygote.Context, ::Dense{typeof(relu),Array{Float32,2},Array{Float32,1}}, ::Array{Float32,4}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [7] Dense at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\layers\basic.jl:116 [inlined]
 [8] _pullback(::Zygote.Context, ::Dense{typeof(relu),Array{Float32,2},Array{Float32,1}}, ::Array{Float64,4}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [9] applychain at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\layers\basic.jl:30 [inlined]
 [10] _pullback(::Zygote.Context, ::typeof(Flux.applychain), ::Tuple{Dense{typeof(relu),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}},typeof(softmax)}, ::Array{Float64,4}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [11] Chain at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\layers\basic.jl:32 [inlined]
 [12] _pullback(::Zygote.Context, ::Chain{Tuple{Dense{typeof(relu),Array{Float32,2},Array{Float32,1}},Dense{typeof(identity),Array{Float32,2},Array{Float32,1}},typeof(softmax)}}, ::Array{Float64,4}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [13] loss at .\In[57]:45 [inlined]
 [14] _pullback(::Zygote.Context, ::typeof(loss), ::Array{Float64,4}, ::Flux.OneHotMatrix{Array{Flux.OneHotVector,1}}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [15] adjoint at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\lib\lib.jl:139 [inlined]
 [16] _pullback at C:\Users\Zubu\.julia\packages\ZygoteRules\6nssF\src\adjoint.jl:47 [inlined]
 [17] #15 at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\optimise\train.jl:69 [inlined]
 [18] _pullback(::Zygote.Context, ::Flux.Optimise.var"#15#21"{typeof(loss),Tuple{Array{Float64,4},Flux.OneHotMatrix{Array{Flux.OneHotVector,1}}}}) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface2.jl:0
 [19] pullback(::Function, ::Zygote.Params) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface.jl:96
 [20] gradient(::Function, ::Zygote.Params) at C:\Users\Zubu\.julia\packages\Zygote\8dVxG\src\compiler\interface.jl:46
 [21] macro expansion at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\optimise\train.jl:68 [inlined]
 [22] macro expansion at C:\Users\Zubu\.julia\packages\Juno\oLB1d\src\progress.jl:134 [inlined]
 [23] #train!#12(::Flux.Optimise.var"#16#22", ::typeof(Flux.Optimise.train!), ::Function, ::Zygote.Params, ::Array{Tuple{Array{Float64,4},Flux.OneHotMatrix{Array{Flux.OneHotVector,1}}},1}, ::ADAM) at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\optimise\train.jl:66
 [24] train!(::Function, ::Zygote.Params, ::Array{Tuple{Array{Float64,4},Flux.OneHotMatrix{Array{Flux.OneHotVector,1}}},1}, ::ADAM) at C:\Users\Zubu\.julia\packages\Flux\oX9Pi\src\optimise\train.jl:64
 [25] top-level scope at .\In[57]:68 # which is the line containing train!

Upvotes: 3

Views: 796

Answers (1)

logankilpatrick
logankilpatrick

Reputation: 14521

The actual error you are seeing in your stack trace comes from when you try to multiply two arrays together.

julia> a = [1,2,3]
3-element Array{Int64,1}:
 1
 2
 3

julia> b = [2,3,4]
3-element Array{Int64,1}:
 2
 3
 4

julia> a * b
ERROR: MethodError: no method matching *(::Array{Int64,1}, ::Array{Int64,1})
Closest candidates are:

The correct way to multiple the contents of two arrays is by doing:

julia> a .* b
3-element Array{Int64,1}:
  2
  6
 12

Upvotes: 1

Related Questions