Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove train! from quickstart example #2110

Merged
merged 15 commits into from
Nov 27, 2022
Merged
Prev Previous commit
Next Next commit
dump train! and gpu from the readme too
  • Loading branch information
mcabbott committed Nov 26, 2022
commit 0faaa25b08e6bf24d63f34480efdd778906f92ce
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,27 @@ Flux is an elegant approach to machine learning. It's a 100% pure-Julia stack, a

Works best with [Julia 1.8](https://julialang.org/downloads/) or later. Here's a very short example to try it out:
```julia
using Flux # should install everything for you, including CUDA
using Flux, Zygote # should install everything for you, including CUDA

x = hcat(digits.(0:3, base=2, pad=2)...) |> gpu # let's solve the XOR problem!
y = Flux.onehotbatch(xor.(eachrow(cpu(x))...), 0:1) |> gpu
data = ((Float32.(x), y) for _ in 1:100) # an iterator making Tuples
x = hcat(digits.(0:3, base=2, pad=2)...) # data for the XOR problem
y = Flux.onehotbatch(xor.(eachrow(x)...), 0:1)

model = Chain(Dense(2 => 3, sigmoid), BatchNorm(3), Dense(3 => 2)) |> gpu
model = Chain(Dense(2 => 3, sigmoid), BatchNorm(3), Dense(3 => 2))
pars = Flux.params(model) # a dictionary of arrays in model
optim = Adam(0.1, (0.7, 0.95))
mloss(x, y) = Flux.logitcrossentropy(model(x), y) # closes over model

Flux.train!(mloss, Flux.params(model), data, optim) # updates model & optim
for _ in 1:100
grad = gradient(() -> Flux.logitcrossentropy(model(x), y), pars)
Flux.update!(optim, pars, grad) # this changes model & optim
end

all((softmax(model(x)) .> 0.5) .== y) # usually 100% accuracy.
```

The [quickstart page](https://fluxml.ai/Flux.jl/stable/models/quickstart/) has a longer version. See the [documentation](https://fluxml.github.io/Flux.jl/) for details, or the [model zoo](https://github.com/FluxML/model-zoo/) for examples. Ask questions on the [Julia discourse](https://discourse.julialang.org/) or [slack](https://discourse.julialang.org/t/announcing-a-julia-slack/4866).

If you use Flux in your research, please [cite](CITATION.bib) our work.