我正在尝试在损失函数中使用分位数进行训练!(对于某些稳健性,例如最小修剪平方),但它会改变数组并且 Zygote 会抛出错误Mutating arrays is not supported
,来自sort!
. 下面是一个简单的例子(内容当然没有意义):
using Flux, StatsBase
xdata = randn(2, 100)
ydata = randn(100)
model = Chain(Dense(2,10), Dense(10, 1))
function trimmedLoss(x,y; trimFrac=0.f05)
yhat = model(x)
absRes = abs.(yhat .- y) |> vec
trimVal = quantile(absRes, 1.f0-trimFrac)
s = sum(ifelse.(absRes .> trimVal, 0.f0 , absRes ))/(length(absRes)*(1.f0-trimFrac))
#s = sum(absRes)/length(absRes) # using this and commenting out the two above works (no surprise)
end
println(trimmedLoss(xdata, ydata)) #works ok
Flux.train!(trimmedLoss, params(model), zip([xdata], [ydata]), ADAM())
println(trimmedLoss(xdata, ydata)) #changed loss?
这一切都在 Flux 0.10 和 Julia 1.2 中
提前感谢任何提示或解决方法!