Help with two Julia lines of code whose syntax is unclear to me

While studying neural nets I came across 2 lines of code whose syntax baffle me.

I looked at the docs, but I still don’t get the 2 lines of code discussed below.

Each line is prefaced by code context.

-----------FIRST LINE-----------------------------

i = 5003 # simple
display(images[i]) # simple

Here is the first line. I already know that this line outputs
a label value, largest Float64 in images data, and the prediction value associated with the largest Float64.

labels[i] are labels for data fed to neural network(NN);

images[i] are the data fed to the NN;

preprocess turns image data into Float64s;

model is the NN;

findmax returns the largest Float64.

What I don’t get is how the logic of this line works for the “.- (0, 1)” part; what is it doing and how?

labels[i], findmax(model(preprocess(images[i]))) .- (0, 1)

-----------SECOND LINE-----------------------------
#= findmax, model, preprocess and images have already been defined above.

The following function definition returns largest Float64 and an index.

What I don’t get in this function definition is what the “[2]-1” part does. What is the logic behind it and how does it work?

prediction(i) = findmax(model(preprocess(images[i])))[2]-1

Thank you in advance for helping me understand Julia syntax better.

Findmax returns a tuple, as you can check:

julia> r = rand(10);

julia> findmax(r)
(0.7466769692672559, 7)

And .- is element-wise subtraction, and [2] takes the second element:

julia> findmax(r) .- (2,3)
(-1.253323030732744, 4)

julia> findmax(r)[2]

ps. It would be easier to read if you could quote your code.

To further explain the motivation for the above code, findmax returns a tuple of (maximum value, index), and the .- (0, 1) part is used to subtract 1 from the index but leave the value unchanged, i.e. change the index from 1-based to 0-based:

julia> r = rand(3)
3-element Array{Float64,1}:

julia> findmax(r)
(0.7670653549580913, 2)

julia> findmax(r) .- (0, 1)
(0.7670653549580913, 1)

I agree with you that it’s not exactly obvious at first sight. A clearer way of writing the same thing would be:

julia> maxval, maxidx = findmax(r)
(0.7670653549580913, 2)

julia> (maxval, maxidx - 1)
(0.7670653549580913, 1)

In your second line, [2] chooses the index only, and -1 changes it to be 0-based.

1 Like

Hi. It is easier to read and understand your questions if you take a look at and follow the advice here: Please read: make it easier to help you

In particular, it is helpful if you quote your code in triple backticks (as explained in that post), so it’s easier to read.

If you click on the edit button, you can update your post to fix the layout and font issues.

1 Like

@improbable22 Thank you Thank you Thank you!!
I will definitely follow your suggestion and quote my code in the future.

In fact, I will edit this post right now so others can read it more easily.

@bennedich Thank you so very much for such a wonderful and clear explanation! You are an immense help. Thank you again!

@ DNF Thank you for your suggestion.

I editing the code in this post so others may read it better per you suggestion.

Thank you!