Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/SimpleChains.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export SimpleChain,
Flatten,
AbsoluteLoss,
SquaredLoss,
WeightedSquaredLoss,
LogitCrossEntropyLoss,
relu,
static,
Expand Down
60 changes: 60 additions & 0 deletions src/loss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,66 @@ function (sl::SquaredLoss{<:AbstractArray{<:Number}})(
T(0.5) * s, p, pu
end

"""
WeightedSquaredLoss(target)

Calculates half of mean weighted squared loss of the target.
"""
struct WeightedSquaredLoss{Y, W} <: AbstractLoss{Y}
y::Y
weights::W
end
(::WeightedSquaredLoss)(y, w) = WeightedSquaredLoss(y, w)
WeightedSquaredLoss() = WeightedSquaredLoss(nothing)
WeightedSquaredLoss(x::Tuple) = WeightedSquaredLoss(x...)
target(wsl::WeightedSquaredLoss) = getfield(wsl, :y), getfield(wsl, :weights)
function view_slice_last(x::Tuple, r)
return Tuple(view_slice_last(f, r) for f in x::Tuple)
end

Base.getindex(wsl::WeightedSquaredLoss, r) = WeightedSquaredLoss(view_slice_last(target(wsl), r))

weighted_squared_loss(chn::SimpleChain, y, w) = add_loss(chn, WeightedSquaredLoss(y, w))

Base.show(io::IO, ::WeightedSquaredLoss) = print(io, "WeightedSquaredLoss")

@inline loss_multiplier(::WeightedSquaredLoss, N, ::Type{T}) where {T} = T(2) / T(N)

function chain_valgrad!(
_,
arg::AbstractArray{T,D},
layers::Tuple{WeightedSquaredLoss},
p::Ptr,
pu::Ptr{UInt8}
) where {T,D}
y = getfield(getfield(layers, 1), :y)
w = getfield(getfield(layers, 1), :weights)
# invN = T(inv(static_size(arg, D)))
s = zero(T)
@turbo for i ∈ eachindex(arg)
δ = arg[i] - y[i]
δw = δ*w[i]
arg[i] = δw
s += δ * δw
end
T(0.5) * s, arg, pu
end
function (sl::WeightedSquaredLoss{<:AbstractArray{<:Number}})(
arg::AbstractArray{T,N},
p,
pu
) where {T,N}
y = getfield(sl, :y)
w = getfield(sl, :weights)
s = zero(T)
@turbo for i ∈ eachindex(arg)
δ = arg[i] - y[i]
s += δ * δ * w[i]
end
# NOTE: we're not dividing by static_size(arg,N)
T(0.5) * s, p, pu
end

"""
AbsoluteLoss

Expand Down