-
-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a quick start example, and change some headings (#2069)
* add quickstart page * tidy welcome page * adjust folders, and some headings * move one page to first section * tweaks * say linear regression somewhere, just not in the title * tweaks * add emoji for API, re-order * also mention function names * activations intro * move destructure to a new file, along with modules * tweaks * tweaks * sciml link * less negative spacing * rm all negative spacing * better Layer Helpers section * move Custom Layers to Tutorials section * fixup * Apply 3 suggestions Co-authored-by: Saransh Chopra <[email protected]> * one more Co-authored-by: Saransh Chopra <[email protected]> Co-authored-by: Saransh Chopra <[email protected]>
- Loading branch information
1 parent
1ec32c2
commit b08cb67
Showing
17 changed files
with
261 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# [Flat vs. Nested Structures](@id man-destructure) | ||
|
||
|
||
A Flux model is a nested structure, with parameters stored within many layers. Sometimes you may want a flat representation of them, to interact with functions expecting just one vector. This is provided by `destructure`: | ||
|
||
```julia | ||
julia> model = Chain(Dense(2=>1, tanh), Dense(1=>1)) | ||
Chain( | ||
Dense(2 => 1, tanh), # 3 parameters | ||
Dense(1 => 1), # 2 parameters | ||
) # Total: 4 arrays, 5 parameters, 276 bytes. | ||
|
||
julia> flat, rebuild = Flux.destructure(model) | ||
(Float32[0.863101, 1.2454957, 0.0, -1.6345707, 0.0], Restructure(Chain, ..., 5)) | ||
|
||
julia> rebuild(zeros(5)) # same structure, new parameters | ||
Chain( | ||
Dense(2 => 1, tanh), # 3 parameters (all zero) | ||
Dense(1 => 1), # 2 parameters (all zero) | ||
) # Total: 4 arrays, 5 parameters, 276 bytes. | ||
``` | ||
|
||
Both `destructure` and the `Restructure` function can be used within gradient computations. For instance, this computes the Hessian `∂²L/∂θᵢ∂θⱼ` of some loss function, with respect to all parameters of the Flux model. The resulting matrix has off-diagonal entries, which cannot really be expressed in a nested structure: | ||
|
||
```julia | ||
julia> x = rand(Float32, 2, 16); | ||
|
||
julia> grad = gradient(m -> sum(abs2, m(x)), model) # nested gradient | ||
((layers = ((weight = Float32[10.339018 11.379145], bias = Float32[22.845667], σ = nothing), (weight = Float32[-29.565302;;], bias = Float32[-37.644184], σ = nothing)),),) | ||
|
||
julia> function loss(v::Vector) | ||
m = rebuild(v) | ||
y = m(x) | ||
sum(abs2, y) | ||
end; | ||
|
||
julia> gradient(loss, flat) # flat gradient, same numbers | ||
(Float32[10.339018, 11.379145, 22.845667, -29.565302, -37.644184],) | ||
|
||
julia> Zygote.hessian(loss, flat) # second derivative | ||
5×5 Matrix{Float32}: | ||
-7.13131 -5.54714 -11.1393 -12.6504 -8.13492 | ||
-5.54714 -7.11092 -11.0208 -13.9231 -9.36316 | ||
-11.1393 -11.0208 -13.7126 -27.9531 -22.741 | ||
-12.6504 -13.9231 -27.9531 18.0875 23.03 | ||
-8.13492 -9.36316 -22.741 23.03 32.0 | ||
|
||
julia> Flux.destructure(grad) # acts on non-models, too | ||
(Float32[10.339018, 11.379145, 22.845667, -29.565302, -37.644184], Restructure(Tuple, ..., 5)) | ||
``` | ||
|
||
### All Parameters | ||
|
||
The function `destructure` now lives in [`Optimisers.jl`](https://github.com/FluxML/Optimisers.jl). | ||
(Be warned this package is unrelated to the `Flux.Optimisers` sub-module! The confusion is temporary.) | ||
|
||
```@docs | ||
Optimisers.destructure | ||
Optimisers.trainable | ||
Optimisers.isnumeric | ||
``` | ||
|
||
### All Layers | ||
|
||
Another kind of flat view of a nested model is provided by the `modules` command. This extracts a list of all layers: | ||
|
||
```@docs | ||
Flux.modules | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,31 @@ | ||
# Flux: The Julia Machine Learning Library | ||
|
||
Flux is a library for machine learning geared towards high-performance production pipelines. It comes "batteries-included" with many useful tools built in, but also lets you use the full power of the Julia language where you need it. We follow a few key principles: | ||
Flux is a library for machine learning. It comes "batteries-included" with many useful tools built in, but also lets you use the full power of the Julia language where you need it. We follow a few key principles: | ||
|
||
* **Doing the obvious thing**. Flux has relatively few explicit APIs for features like regularisation or embeddings. Instead, writing down the mathematical form will work – and be fast. | ||
* **Extensible by default**. Flux is written to be highly extensible and flexible while being performant. Extending Flux is as simple as using your own code as part of the model you want - it is all [high-level Julia code](https://github.com/FluxML/Flux.jl/blob/ec16a2c77dbf6ab8b92b0eecd11661be7a62feef/src/layers/recurrent.jl#L131). When in doubt, it’s well worth looking at [the source](https://github.com/FluxML/Flux.jl/). If you need something different, you can easily roll your own. | ||
* **Performance is key**. Flux integrates with high-performance AD tools such as [Zygote.jl](https://github.com/FluxML/Zygote.jl) for generating fast code. Flux optimizes both CPU and GPU performance. Scaling workloads easily to multiple GPUs can be done with the help of Julia's [GPU tooling](https://github.com/JuliaGPU/CUDA.jl) and projects like [DaggerFlux.jl](https://github.com/DhairyaLGandhi/DaggerFlux.jl). | ||
* **Play nicely with others**. Flux works well with Julia libraries from [data frames](https://github.com/JuliaComputing/JuliaDB.jl) and [images](https://github.com/JuliaImages/Images.jl) to [differential equation solvers](https://github.com/JuliaDiffEq/DifferentialEquations.jl), so you can easily build complex data processing pipelines that integrate Flux models. | ||
* **Extensible by default**. Flux is written to be highly extensible and flexible while being performant. Extending Flux is as simple as using your own code as part of the model you want - it is all [high-level Julia code](https://github.com/FluxML/Flux.jl/blob/ec16a2c77dbf6ab8b92b0eecd11661be7a62feef/src/layers/recurrent.jl#L131). When in doubt, it’s well worth looking at [the source](https://github.com/FluxML/Flux.jl/tree/master/src). If you need something different, you can easily roll your own. | ||
* **Play nicely with others**. Flux works well with Julia libraries from [images](https://github.com/JuliaImages/Images.jl) to [differential equation solvers](https://github.com/SciML/DifferentialEquations.jl), so you can easily build complex data processing pipelines that integrate Flux models. | ||
|
||
## Installation | ||
|
||
Download [Julia 1.6](https://julialang.org/) or later, if you haven't already. You can add Flux using Julia's package manager, by typing `] add Flux` in the Julia prompt. | ||
Download [Julia 1.6](https://julialang.org/downloads/) or later, preferably the current stable release. You can add Flux using Julia's package manager, by typing `] add Flux` in the Julia prompt. | ||
|
||
If you have CUDA you can also run `] add CUDA` to get GPU support; see [here](gpu.md) for more details. | ||
This will automatically install several other packages, including [CUDA.jl](https://github.com/JuliaGPU/CUDA.jl) which supports Nvidia GPUs. To directly access some of its functionality, you may want to add `] add CUDA` too. The page on [GPU support](gpu.md) has more details. | ||
|
||
NOTE: Flux used to have a CuArrays.jl dependency until v0.10.4, replaced by CUDA.jl in v0.11.0. If you're upgrading Flux from v0.10.4 or a lower version, you may need to remove CuArrays (run `] rm CuArrays`) before you can upgrade. | ||
Other closely associated packages, also installed automatically, include [Zygote](https://github.com/FluxML/Zygote.jl), [Optimisers](https://github.com/FluxML/Optimisers.jl), [NNlib](https://github.com/FluxML/NNlib.jl), [Functors](https://github.com/FluxML/Functors.jl) and [MLUtils](https://github.com/JuliaML/MLUtils.jl). | ||
|
||
## Learning Flux | ||
|
||
There are several different ways to learn Flux. If you just want to get started writing models, the [model zoo](https://github.com/FluxML/model-zoo/) gives good starting points for many common ones. This documentation provides a reference to all of Flux's APIs, as well as a from-scratch introduction to Flux's take on models and how they work. Once you understand these docs, congratulations, you also understand [Flux's source code](https://github.com/FluxML/Flux.jl), which is intended to be concise, legible and a good reference for more advanced concepts. | ||
The [quick start](models/quickstart.md) page trains a simple neural network. | ||
|
||
This rest of this documentation provides a from-scratch introduction to Flux's take on models and how they work, starting with [fitting a line](models/overview.md). Once you understand these docs, congratulations, you also understand [Flux's source code](https://github.com/FluxML/Flux.jl), which is intended to be concise, legible and a good reference for more advanced concepts. | ||
|
||
Sections with 📚 contain API listings. The same text is avalable at the Julia prompt, by typing for example `?gpu`. | ||
|
||
If you just want to get started writing models, the [model zoo](https://github.com/FluxML/model-zoo/) gives good starting points for many common ones. | ||
|
||
## Community | ||
|
||
All Flux users are welcome to join our community on the [Julia forum](https://discourse.julialang.org/), or the [slack](https://discourse.julialang.org/t/announcing-a-julia-slack/4866) (channel #machine-learning). If you have questions or issues we'll try to help you out. | ||
Everyone is welcome to join our community on the [Julia discourse forum](https://discourse.julialang.org/), or the [slack chat](https://discourse.julialang.org/t/announcing-a-julia-slack/4866) (channel #machine-learning). If you have questions or issues we'll try to help you out. | ||
|
||
If you're interested in hacking on Flux, the [source code](https://github.com/FluxML/Flux.jl) is open and easy to understand -- it's all just the same Julia code you work with normally. You might be interested in our [intro issues](https://github.com/FluxML/Flux.jl/labels/good%20first%20issue) to get started or our [contributing guide](https://github.com/FluxML/Flux.jl/blob/master/CONTRIBUTING.md). | ||
If you're interested in hacking on Flux, the [source code](https://github.com/FluxML/Flux.jl) is open and easy to understand -- it's all just the same Julia code you work with normally. You might be interested in our [intro issues](https://github.com/FluxML/Flux.jl/labels/good%20first%20issue) to get started, or our [contributing guide](https://github.com/FluxML/Flux.jl/blob/master/CONTRIBUTING.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.