@@ -17,14 +17,14 @@ model = Chain(
17
17
Dense (2 => 3 , tanh), # activation function inside layer
18
18
BatchNorm (3 ),
19
19
Dense (3 => 2 ),
20
- softmax)
20
+ softmax) |> gpu # move model to GPU, if available
21
21
22
22
# The model encapsulates parameters, randomly initialised. Its initial output is:
23
- out1 = model (noisy) # 2×1000 Matrix{Float32}
23
+ out1 = model (noisy |> gpu) |> cpu # 2×1000 Matrix{Float32}
24
24
25
25
# To train the model, we use batches of 64 samples, and one-hot encoding:
26
26
target = Flux. onehotbatch (truth, [true , false ]) # 2×1000 OneHotMatrix
27
- loader = Flux. DataLoader ((noisy, target), batchsize= 64 , shuffle= true );
27
+ loader = Flux. DataLoader ((noisy, target) |> gpu , batchsize= 64 , shuffle= true );
28
28
# 16-element DataLoader with first element: (2×64 Matrix{Float32}, 2×64 OneHotMatrix)
29
29
30
30
pars = Flux. params (model) # contains references to arrays in model
@@ -34,7 +34,7 @@ opt = Flux.Adam(0.01) # will store optimiser momentum, etc.
34
34
losses = []
35
35
for epoch in 1 : 1_000
36
36
for (x, y) in loader
37
- loss, grad = withgradient (pars) do
37
+ loss, grad = Flux . withgradient (pars) do
38
38
# Evaluate model and loss inside gradient context:
39
39
y_hat = model (x)
40
40
Flux. crossentropy (y_hat, y)
46
46
47
47
pars # parameters, momenta and output have all changed
48
48
opt
49
- out2 = model (noisy) # first row is prob. of true, second row p(false)
49
+ out2 = model (noisy |> gpu) |> cpu # first row is prob. of true, second row p(false)
50
50
51
51
mean ((out2[1 ,:] .> 0.5 ) .== truth) # accuracy 94% so far!
52
52
```
0 commit comments