-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestANNlinRegression.jl
66 lines (43 loc) · 1.15 KB
/
testANNlinRegression.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# test simple ANN to make linear regression on random data
# DL rev 29-April-2019
using Knet
using PyPlot
using AutoGrad
using Compat.Statistics
predict(w,x)=(w[1]*x.+w[2])
loss(w,x,y)=(sum(abs2,y-predict(w,x))/size(x,2) )
lossgradient =grad(loss)
function train(w, x, y; lr=.1, epochs=20)
for epoch=1:epochs
g = lossgradient(w, x, y)
update!(w, g; lr=lr)
end
return w
end
n = 1000;
xtrn = zeros(Float64,1,n);
ytrn = zeros(Float64,1,n);
xtst = zeros(Float64,1,n);
ytst = zeros(Float64,1,n);
for i = 1:n
xtrn[i] = rand();
ytrn[i] = rand();
xtst[i] = xtrn[i];
ytst[i] = ytrn[i];
end
w = map(Array{Float64}, [ 0.0*randn(1,1), 0.0*randn(1,1) ])
report(epoch)=println((:epoch,epoch,:trn,loss(w,xtrn,ytrn),:tst,loss(w,xtst,ytst)))
Nepochs = 10;
report(0)
@time for epoch=1:Nepochs
train(w, xtrn, ytrn; lr=0.1, epochs=1)
report(epoch)
end
f = w[1]*xtrn .+ w[2];
figure(1);
clf();
plot(xtrn[1:end], ytrn[1:end],"or",label = "raw data");
plot(xtrn[1:end], f[1:end],"sk", label = "predicted regression")
xlabel("x");
ylabel("y");
legend();