Updated huber_loss with other minute changes

This commit is contained in:
Adarsh Kumar 2020-03-02 13:25:23 +05:30 committed by GitHub
parent 08dabce57e
commit f9e31a020c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 25 deletions

View File

@ -16,38 +16,31 @@ mse(ŷ, y) = sum((ŷ .- y).^2) * 1 // length(y)
"""
msle(, y;ϵ1=eps.(Float64.()),ϵ2=eps.(Float64.(y)))
msle(, y; ϵ1=eps.(Float64.()))
Mean Squared Logarithmic Error. Returns the mean of the squared logarithmic errors `sum((log.(ŷ+ϵ1).-log.(y+ϵ2)).^2) * 1 / length(y)`.<br>
The ϵ1 and ϵ2 terms provide numerical stability. This error penalizes an under-predicted estimate greater than an over-predicted estimate.
Mean Squared Logarithmic Error. Returns the mean of the squared logarithmic errors `sum((log.(ŷ+ϵ1) .- log.(y+ϵ2)).^2) * 1 / length(y)`.<br>
The `ϵ` term provides numerical stability. This error penalizes an under-predicted estimate greater than an over-predicted estimate.
"""
msle(, y;ϵ1=eps.(),ϵ2=eps.(eltype().(y))) = sum((log.(+ϵ1).-log.(y+ϵ2)).^2) * 1 // length(y)
msle(, y; ϵ=eps.()) = sum((log.(+ϵ).-log.(y+ϵ)).^2) * 1 // length(y)
"""
huber_loss(, y,delta=1.0)
huber_loss(, y; delta=1.0)
Computes the mean of the Huber loss given the prediction `` and true values `y`. By default, delta is set to 1.0.
Computes the mean of the Huber loss. By default, delta is set to 1.0.
| 0.5*|(-y)|, for |-y|<=delta
Hubber loss = |
| delta*(|-y| - 0.5*delta), otherwise
[`Huber Loss`](https://en.wikipedia.org/wiki/Huber_loss).
"""
function huber_loss(, y,delta=1.0)
abs_error = abs.(.-y)
dtype= eltype()
delta = dtype(delta)
hub_loss = dtype(0)
for i in 1:length(y)
if (abs_error[i]<=delta)
hub_loss+=abs_error[i]^2*dtype(0.5)
else
hub_loss+=delta*(abs_error[i]- dtype(0.5*delta))
end
end
hub_loss*1//length(y)
function huber_loss(, y; delta = eltype()(1))
abs_error = abs.(.-y)
temp = abs_error.<delta
x = eltype()(0.5)
hub_loss = sum(((abs_error.^2).*temp).*x .+ delta*(abs_error.- x*delta).*(1 .-temp)) * 1 // length(y)
end
function _crossentropy(::AbstractVecOrMat, y::AbstractVecOrMat, weight::Nothing)
@ -167,6 +160,7 @@ poisson(ŷ, y) = sum(ŷ .- y .* log.(ŷ)) *1 // size(y,2)
hinge(, y)
Measures the loss given the prediction `` and true labels `y` (containing 1 or -1).
Returns `sum((max.(0,1 .-ŷ .* y))) *1 // size(y, 2)`
[Hinge Loss](https://en.wikipedia.org/wiki/Hinge_loss)
See also [`squared_hinge`](@ref).
@ -176,35 +170,38 @@ hinge(ŷ, y) = sum(max.(0, 1 .- ŷ .* y)) *1 // size(y,2)
"""
squared_hinge(, y)
Computes squared hinge loss given the prediction `` and true labels `y` (conatining 1 or -1)
Computes squared hinge loss given the prediction `` and true labels `y` (conatining 1 or -1).
Returns `sum((max.(0,1 .-ŷ .* y)).^2) *1 // size(y, 2)`
See also [`hinge`](@ref).
"""
squared_hinge(, y) = sum((max.(0,1 .- .* y)).^2) *1//size(y,2)
"""
dice_coeff_loss(y_pred,y_true,smooth = 1)
dice_coeff_loss(y_pred, y_true, smooth = 1)
Loss function used in Image Segmentation. Calculates loss based on dice coefficient. Similar to F1_score
Loss function used in Image Segmentation. Calculates loss based on dice coefficient. Similar to F1_score.
Dice_Coefficient(A,B) = 2 * sum( |A*B| + smooth) / (sum( A^2 ) + sum( B^2 )+ smooth)
Dice_loss = 1 - Dice_Coefficient
Ref: [V-Net: Fully Convolutional Neural Networks forVolumetric Medical Image Segmentation](https://arxiv.org/pdf/1606.04797v1.pdf)
"""
function dice_coeff_loss(y_pred,y_true,smooth=eltype(y_pred)(1.0))
function dice_coeff_loss(y_pred, y_true; smooth=eltype(y_pred)(1.0))
intersection = sum(y_true.*y_pred)
return 1 - (2*intersection + smooth)/(sum(y_true.^2) + sum(y_pred.^2)+smooth)
end
"""
tversky_loss(y_pred,y_true,beta = 0.7)
tversky_loss(y_pred, y_true, beta = 0.7)
Used with imbalanced data to give more weightage to False negatives. Larger β weigh recall higher than precision (by placing more emphasis on false negatives)
tversky_loss(,y,beta) = 1 - sum(|y.*| + 1) / (sum(y.* + beta*(1 .- y).* + (1 .- beta)*y.*(1 .- ))+ 1)
Ref: [Tversky loss function for image segmentation using 3D fully convolutional deep networks](https://arxiv.org/pdf/1706.05721.pdf)
"""
function tversky_loss(y_pred,y_true,beta = eltype(y_pred)(0.7))
function tversky_loss(y_pred, y_true; beta = eltype(y_pred)(0.7))
intersection = sum(y_true.*y_pred)
return 1 - (intersection+1)/(sum(y_true.*y_pred + beta*(1 .- y_true).* y_pred + (1-beta).*y_true.*(1 .- y_pred))+1)
end