Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Making the code work with the newer Flux syntax #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions docs/src/ML/ml.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@
[From the model zoo](https://github.com/FluxML/model-zoo/blob/master/mnist/mnist.jl)

```Julia
using Flux, MNIST, CuArrays
using Flux: onehotbatch, argmax, mse, throttle
using Flux, MLDatasets, CuArrays
using Flux: onehotbatch, argmax, crossentropy, throttle, onecold
using Base.Iterators: repeated

x, y = traindata()
y = onehotbatch(y, 0:9)
x, y = x, y = MNIST.traindata()
x = reshape(x,28*28,:) |> gpu
y = onehotbatch(y, 0:9) |> gpu

m = Chain(
Dense(28^2, 32, σ),
Dense(32, 10),
softmax
)
) |> gpu

using CuArrays
# or CLArrays (you then need to use cl

x, y = cu(x), cu(y)
m = mapparams(cu, m)
loss(x, y) = mse(m(x), y)
loss(x, y) = crossentropy(m(x), y)

dataset = repeated((x, y), 500)
evalcb = () -> @show(loss(x, y))
opt = SGD(params(m), 1)
opt = Descent()

Flux.train!(loss, dataset, opt, cb = throttle(evalcb, 10))
Flux.train!(loss, params(m), dataset, opt, cb = throttle(evalcb, 10))

# Check the prediction for the first digit
argmax(m(x[:,1]), 0:9) == argmax(y[:,1], 0:9)
onecold(m(x[:,1]), 0:9) == onecold(y[:,1], 0:9)
```