-
Notifications
You must be signed in to change notification settings - Fork 16
Loosen types for Reactant #162
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
Changes from 1 commit
1b1308b
ef2c0d8
9ffba74
593f623
eceb64e
b0c579d
9ec1c9a
7d1d756
40b7d33
9f8fd8c
0ddba3d
8a54315
9b6e831
5f368e8
4d74ff2
962db49
9ee12fd
5f7a0a4
7e6352b
bab0a40
070c448
a5f1e40
f5173e6
235062f
b401cea
480917c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,11 @@ abstract type ScalarTransform <: AbstractTransform end | |
|
|
||
| dimension(::ScalarTransform) = 1 | ||
|
|
||
| function transform_with(flag::NoLogJac, t::ScalarTransform, x::AbstractVector, index::Int) | ||
| function transform_with(flag::NoLogJac, t::ScalarTransform, x::AbstractVector, index) | ||
| transform(t, @inbounds x[index]), flag, index + 1 | ||
| end | ||
|
|
||
| function transform_with(::LogJac, t::ScalarTransform, x::AbstractVector, index::Int) | ||
| function transform_with(::LogJac, t::ScalarTransform, x::AbstractVector, index) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| transform_and_logjac(t, @inbounds x[index])..., index + 1 | ||
| end | ||
|
|
||
|
|
@@ -43,15 +43,15 @@ Identity ``x ↦ x``. | |
| """ | ||
| struct Identity <: ScalarTransform end | ||
|
|
||
| transform(::Identity, x::Real) = x | ||
| transform(::Identity, x::Number) = x | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In principle I am OK with widening
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya I agree it is very sad. |
||
|
|
||
| transform_and_logjac(::Identity, x::Real) = x, logjac_zero(LogJac(), typeof(x)) | ||
| transform_and_logjac(::Identity, x::Number) = x, logjac_zero(LogJac(), typeof(x)) | ||
|
|
||
| inverse_eltype(t::Identity, ::Type{T}) where T = T | ||
|
|
||
| inverse(::Identity, x::Number) = x | ||
|
|
||
| inverse_and_logjac(::Identity, x::Real) = x, logjac_zero(LogJac(), typeof(x)) | ||
| inverse_and_logjac(::Identity, x::Number) = x, logjac_zero(LogJac(), typeof(x)) | ||
|
|
||
| #### | ||
| #### elementary scalar transforms | ||
|
|
@@ -64,9 +64,9 @@ Exponential transformation `x ↦ eˣ`. Maps from all reals to the positive real | |
| """ | ||
| struct TVExp <: ScalarTransform end | ||
|
|
||
| transform(::TVExp, x::Real) = exp(x) | ||
| transform(::TVExp, x::Number) = exp(x) | ||
|
|
||
| transform_and_logjac(t::TVExp, x::Real) = transform(t, x), x | ||
| transform_and_logjac(t::TVExp, x::Number) = transform(t, x), x | ||
|
|
||
| inverse_eltype(t::TVExp, ::Type{T}) where T = _ensure_float(T) | ||
|
|
||
|
|
@@ -83,9 +83,9 @@ Logistic transformation `x ↦ logit(x)`. Maps from all reals to (0, 1). | |
| """ | ||
| struct TVLogistic <: ScalarTransform end | ||
|
|
||
| transform(::TVLogistic, x::Real) = logistic(x) | ||
| transform(::TVLogistic, x::Number) = logistic(x) | ||
|
|
||
| transform_and_logjac(t::TVLogistic, x::Real) = transform(t, x), logistic_logjac(x) | ||
| transform_and_logjac(t::TVLogistic, x::Number) = transform(t, x), logistic_logjac(x) | ||
|
|
||
| inverse_eltype(t::TVLogistic, ::Type{T}) where T = _ensure_float(T) | ||
|
|
||
|
|
@@ -100,13 +100,13 @@ $(TYPEDEF) | |
|
|
||
| Shift transformation `x ↦ x + shift`. | ||
| """ | ||
| struct TVShift{T <: Real} <: ScalarTransform | ||
| struct TVShift{T} <: ScalarTransform | ||
| shift::T | ||
| end | ||
|
|
||
| transform(t::TVShift, x::Real) = x + t.shift | ||
| transform(t::TVShift, x::Number) = x + t.shift | ||
|
|
||
| transform_and_logjac(t::TVShift, x::Real) = transform(t, x), logjac_zero(LogJac(), typeof(x)) | ||
| transform_and_logjac(t::TVShift, x::Number) = transform(t, x), logjac_zero(LogJac(), typeof(x)) | ||
|
|
||
| inverse_eltype(t::TVShift{S}, ::Type{T}) where {S,T} = typeof(zero(_ensure_float(T)) - zero(S)) | ||
|
|
||
|
|
@@ -129,15 +129,15 @@ end | |
|
|
||
| TVScale(scale::T) where {T} = TVScale{T}(scale) | ||
|
|
||
| transform(t::TVScale, x::Real) = t.scale * x | ||
| transform(t::TVScale, x::Number) = t.scale * x | ||
|
|
||
| transform_and_logjac(t::TVScale{<:Real}, x::Real) = transform(t, x), log(t.scale) | ||
| transform_and_logjac(t::TVScale{<:Real}, x::Number) = transform(t, x), log(t.scale) | ||
|
|
||
| inverse_eltype(t::TVScale{S}, ::Type{T}) where {S,T} = typeof(oneunit(T) / oneunit(S)) | ||
|
|
||
| inverse(t::TVScale, x::Number) = x / t.scale | ||
|
|
||
| inverse_and_logjac(t::TVScale{<:Real}, x::Number) = inverse(t, x), -log(t.scale) | ||
| inverse_and_logjac(t::TVScale, x::Number) = inverse(t, x), -log(t.scale) | ||
|
|
||
| """ | ||
| $(TYPEDEF) | ||
|
|
@@ -147,8 +147,8 @@ Negative transformation `x ↦ -x`. | |
| struct TVNeg <: ScalarTransform | ||
| end | ||
|
|
||
| transform(::TVNeg, x::Real) = -x | ||
| transform_and_logjac(t::TVNeg, x::Real) = transform(t, x), logjac_zero(LogJac(), typeof(x)) | ||
| transform(::TVNeg, x::Number) = -x | ||
| transform_and_logjac(t::TVNeg, x::Number) = transform(t, x), logjac_zero(LogJac(), typeof(x)) | ||
|
|
||
| inverse_eltype(::TVNeg, ::Type{T}) where T = typeof(-oneunit(T)) | ||
| inverse(::TVNeg, x::Number) = -x | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit surprised that you need this, since
transform_withis called internally andindexis an integer.Can you explain what the actual type is that you need here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sometimes in Reactant you can get a
TracedNumber{Integer}if I loop through this and the arrayxis a AbstractTraced array.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically for the
@tracemacro it will raise the index to aTracedRNumber{Int}so I needed to handle this.