-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
DiffCache/GeneralLazyBufferCache for an array with mixed types #71
Comments
ok. Fair. No stack trace. This is the one for the intended pre-allocated array. ar_alloc = [(; a=1.0, b=2.0, c=SVector(0.0)), (; a=1.0, b=2.0, c=SVector(0.0))]
ar_alloc = DiffCache(ar_alloc)
and for GLBC ar_allocG = GeneralLazyBufferCache(function (p)
ar_alloc
end
)
# no error, but then how is this suppose to work in the next steps?
loss(x) = fxy(x, ar_allocG, y)
ForwardDiff.gradient(loss, [0.2, 0.5, 0.6])
|
GLBC the code is just incorrect. You'd have to do something like: function fxy(x, _ar_alloc, y)
ar_alloc = _ar_alloc[x]
for i in 1:2
a = x + 1
b = x * x + 2
c = SVector(x * x * x + 3 + a)
ar_alloc[i] = (; a, b, c)
end
ŷ = [ar_alloc[1].a + ar_alloc[1].c[1], ar_alloc[2].a + +ar_alloc[2].c[1]]
return sum(abs.(ŷ .- y))
end and then you need an appropriate allocation: ar_allocG = GeneralLazyBufferCache(function (p)
[(; a=p, b=p, c=SVector(p)), (; a=p, b=p, c=SVector(p))]
end
) and you should be good. |
Thanks, unfortunately, it seems like is not that easy. After following some error suggestions from your code sample, this is the updated example: # after some type-size-fixes and using `dot` everywhere[for some reason] (suggested by the errors),
# this is a more representative example
using ForwardDiff
using PreallocationTools: DiffCache, GeneralLazyBufferCache
using StaticArrays
function fxy(x, _ar_alloc, y)
ar_alloc = _ar_alloc[x]
for i in 1:2
a = x .+ 1 # things will start to fail from here without the `dot`, why?, there is no need.
b = x .* x .+ 2
c = SVector{2}(x .* x .* x .+ 3 .+ a, x .* x)
ar_alloc[i] = (; a, b, c)
end
ŷ = [ar_alloc[1].a .+ ar_alloc[1].c[1], ar_alloc[2].a .+ ar_alloc[2].c[2]]
return sum(abs2.(ŷ .- y)) # ForwardDiff.gradient will fail here.
end
ar_allocG = GeneralLazyBufferCache(function (p)
[(; a=p, b=p, c=SVector{2}(p, p)), (; a=p, b=p, c=SVector{2}(p, p))]
end
)
# do we have a number?
y = [2.3, 5.0]
loss(x) = fxy(x, ar_allocG, y)
loss(0.5) # yes, it outputs a number
# the actual test
# but here, this one still fails.
ForwardDiff.gradient(loss, [0.2, 0.5, 0.6])
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there a way to make this work? The important part is the preallocation of the mixed tuples in the array.
The text was updated successfully, but these errors were encountered: