Hi,
I'm an R user trying to learn Julia. I got hold of some code from the Knet
package that I was playing around with. My goal is to set values to zero in
a loop based on a logical expression, but I cannot figure out how the
indexing works. Any help would be appreciated (the problem lies in
w[1,(w[1].<z)&(w[1].>-(z))] = 0):
using Knet
predict(w,x) = w[1]*x .+ w[2]
lambda = 2
z = Array{Float64}(1,13)
loss(w,x,y) = sumabs2(y - predict(w,x)) / size(y,2)
lossgradient = grad(loss)
function train(w, data; lr=.1)
for (x,y) in data
dw = lossgradient(w, x, y)
z[:] = lr * lambda
w[1] -= lr * dw[1]
w[2] -= lr * dw[2]
w[1,(w[1].<z)&(w[1].>-(z))] = 0
end
return w
end
url =
"https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data"
rawdata = readdlm(download(url))
x = rawdata[:,1:13]'
x = (x .- mean(x,2)) ./ std(x,2)
y = rawdata[:,14:14]'
w = Any[ 0.1*randn(1,13), 0 ]
niter = 25
lossest = zeros(niter)
for i=1:niter; train(w, [(x,y)]); lossest[i]=loss(w,x,y); end
Best regards,
Patrik