[SOLVED] Nothing returned in the Zygote derivative

In my mind the following definitions are equivalent:

function test_working(x)
    return max(x - 1.0, 0.0)
end

function test_not_working(x)
    if x > 1.0
        return x - 1.0
    else
        return 0.0
    end
end

And indeed they give identical results as expected:

test_working(2.0)     # 1.0 as expected
test_not_working(2.0) # 1.0 as expected

test_working(0.0)     # 0.0 as expected
test_not_working(0.0) # 0.0 as expected

However the derivative of the second function gives unexpected result in the return 0.0 branch:

using Zygote

test_working'(2.0)     # 1.0 as expected
test_not_working'(2.0) # 1.0 as expected

test_working'(0.0)     # 0.0 as expected
test_not_working'(0.0) # nothing not as expected. I was expecting to see 0.0

Any idea why?

Thanks

Zygote treats nothing like a zero, so although this might be a bit confusing, it is expected. You can use something to convert nothing to a numerical zero.

3 Likes