You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use Zygote with Unitful. If I use standard units like m, s and m/s, everything works:
using Zygote
using Unitful
# Works:struct Foo
x::Number
t::Number
c::Numberendfunctionbar(f::Foo)
return f.x - f.c*f.t
end
foo =Foo(2u"m", 3u"s", 3e8u"m/s")
g = Zygote.gradient(f ->bar(f), foo)
println(g)
However, if I use a custom unit (in this case Euros) I get an error about requiring an adjoint for the constructor:
ERROR: LoadError: Need an adjoint for constructor Quantity{Float64, 𝐓, Unitful.FreeUnits{(yr,), 𝐓, nothing}}. Gradient is of type Quantity{Float64, 𝐓⁻¹, Unitful.FreeUnits{(€, yr⁻¹), 𝐓⁻¹, nothing}}
Apologies if my google-fu has failed me, but I have searched the Unitful docs, but did not manage to find anything about defining an adjoint for a constructor. How can I define one?
Code to reproduce:
using Zygote
using Unitful
@unit € "€" Euro 1.0falsestruct Loan
loan_amount::Number
interest_rate::Number
loan_term::Numberendfunctionmonthly_payment(l::Loan)
r = l.interest_rate*1.0u"yr"/12.0# use monthly interest rate for monthly payment# Number of payments:
n = l.loan_term*12.0/1.0u"yr"return l.loan_amount*r*(1+r)^n/((1+r)^n -1)
end
loan =Loan(500000.0*€, 0.025/1.0u"yr", 30.0u"yr")
g = Zygote.gradient(l ->monthly_payment(l), loan)
println(g)
The text was updated successfully, but these errors were encountered:
NAThompson
changed the title
Need an adjoint for constructor
Need an adjoint for constructor for Zygote compatibility
Aug 18, 2024
I'm trying to use Zygote with Unitful. If I use standard units like m, s and m/s, everything works:
However, if I use a custom unit (in this case Euros) I get an error about requiring an adjoint for the constructor:
Apologies if my google-fu has failed me, but I have searched the Unitful docs, but did not manage to find anything about defining an adjoint for a constructor. How can I define one?
Code to reproduce:
The text was updated successfully, but these errors were encountered: