Problem with error reporting in Mocha

Hello. My name is Alejandro, nice to meet you all. On to business…

I am a total Machine Learning novice beyond the theoretical aspect though now it seems I need to get to work in some applications if I am to actually make use of it. After deciding to settle for Mocha as the package to use going forward I decided to get my toes wet with a tutorial, which is what led me to Diego Acuña’s multi-perceptron one, which he uses to approximate a function (link here). I’ve followed it to the letter but I ran into this problem when executing:

Basically that reads:

ERROR: LoadError: MethodError: no method matching Mocha.Net(::String, ::Mocha.CPUBackend, ::Array{Any,1})
Closest candidates are:
  Mocha.Net(::AbstractString, ::Mocha.Backend, ::Array{Mocha.Layer,1}) at /home/alejandro/.julia/v0.6/Mocha/src/net.jl:196
  Mocha.Net(::AbstractString, ::T<:Mocha.Backend, ::Array{Mocha.Layer,1}, ::Array{Mocha.LayerState,1}, ::Array{Array{Mocha.Blob,1},1}, ::Array{Array{Mocha.Blob,1},1}, ::Array{Int64,1}, ::Dict{Symbol,Mocha.Blob}, ::Dict{Symbol,Mocha.Blob}) where T<:Mocha.Backend at /home/alejandro/.julia/v0.6/Mocha/src/net.jl:7
Stacktrace:
 [1] include_from_node1(::String) at ./loading.jl:569
 [2] include(::String) at ./sysimg.jl:14
while loading /home/alejandro/Desktop/UNI_SYNC/OTHER/NEURAL NETWORKS/Mocha_tst.jl, in expression starting on line 41

Which I don’t understand since the line it refers to is

net = Net("MLP", backend, [data_layer, common_layers, layer_loss])

Which is used to create the actual network. Save for some reference to deprecated stuff, I have had no problem executing each and every line up until that one comes up, and after googling around I can’t find what, exactly, should be wrong because, for all I know, that line should be correct. Even the help document seems to be of no help (no pun intended).

I can provide the whole of the code I’m using though it should be easier to just check on the site in the link I provided. Honestly, I’m stumped :neutral_face:.

Thank you so much to anyone that can lend a hand. Cheers.

I should also comment how running Pkg.test(“Mocha”) returns no issues.

As the error message says, the Net constructor expects an Array{Mocha.Layer,1} (also known as a Vector{Mocha.Layer}), but you are passing it an Array{Any}. So let’s look closer at that input. Your third argument to Net is:

[data_layer, common_layers, layer_loss]

where common_layers is defined as:

common_layers = [ip_layer, aggregator]

And that’s exactly the problem. Your input, which should be a flat vector of layers, is actually nested. It looks like:

[data_layer, [ip_layer, aggregator], layer_loss]

which isn’t something Mocha.jl understands. The reason this worked in the blog post is that it used to be the case that the [ ] syntax would sort-of-magically flatten its arguments…sometimes? I honestly never fully understood when it did or didn’t, and I was extremely glad to see that semi-magical flattening removed sometime around Julia v0.4.

In your case, you should be able to get a vector of layers by doing:

vcat(data_layer, common_layers, layer_loss)

instead of using [ ].

1 Like

Thank you so much! That really does help. :+1:

1 Like