-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmlp.jl
72 lines (60 loc) · 1.7 KB
/
mlp.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
67
68
69
70
71
72
using PyCall
using ArgParse
include("dataUtils.jl")
torch = pyimport("torch")
optim = pyimport("torch.optim")
nn = pyimport("torch.nn")
F = pyimport("torch.nn.functional")
args = let s = ArgParseSettings()
@add_arg_table s begin
"--nocuda"
action=:store_true
"--batchsize"
arg_type=Int
default=256
end
parse_args(s)
end
for (arg, val) in args
println("$arg => $val")
end
device = torch.device(ifelse(!args["nocuda"] && torch.cuda.is_available(), "cuda", "cpu"))
println(device)
trainLoader, testLoader = getmnistDataLoaders(args["batchsize"])
model = nn.Sequential(nn.Linear(784, 400),
nn.ReLU(),
nn.Linear(400, 10),
nn.LogSoftmax(dim=1)).to(device)
optimizer = optim.Adam(model.parameters(), lr=0.001)
function train()
for epoch in 1:10
for (i, (x, y)) in enumerate(trainLoader)
(x, y) = x.to(device), y.to(device)
x = x.reshape(-1, 784)
o = model(x)
loss = F.nll_loss(o, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
i % 100 == 0 && println("Epoch: $epoch\tLoss: $(loss.item())")
end
GC.gc(false)
end
end
println("Training...")
@time train()
println("Testing...")
let (n, N) = (0, 0)
@pywith torch.no_grad() begin
for (x, y) in testLoader
(x, y) = x.to(device), y.to(device)
x = x.reshape(-1, 784)
o = model(x)
_, ŷ = torch.max(o, 1)
N += y.size(0)
n += torch.sum(ŷ == y).item()
end
GC.gc(false)
println("Accuracy: $(n/N)")
end
end