wip functional exposition
This commit is contained in:
parent
9b76b307b6
commit
1bba9631a2
|
@ -1,6 +1,8 @@
|
|||
# Flux
|
||||
|
||||
Flux is a machine learning library, implemented in Julia. In a nutshell, it simply lets you run normal Julia code on a backend like TensorFlow. It also provides many conveniences for doing deep learning in particular.
|
||||
*... Initialising Photon Beams ...*
|
||||
|
||||
Flux is a machine learning library, implemented in Julia. In a nutshell, it simply lets you run normal Julia code on a backend like TensorFlow. It also provides many conveniences for doing deep learning.
|
||||
|
||||
This gives you great flexibility. You can use a convenient Keras-like API if you want something simple, but you can also drop down to straight mathematics, or build your own abstractions. You can even use Flux's utilities (like optimisers) with a completely different backend (like [Knet](https://github.com/denizyuret/Knet.jl)) or mix and match approaches.
|
||||
|
||||
|
@ -10,13 +12,15 @@ Note that Flux is in alpha. Many things work but the API is still in a state of.
|
|||
|
||||
## Where do I start?
|
||||
|
||||
*... Charging Ion Capacitors ...*
|
||||
|
||||
The [examples](examples/logreg.html) give a feel for high-level usage. This a great way to start if you're a relative newbie to machine learning or neural networks; you can get up and running running easily.
|
||||
|
||||
If you have more experience with ML, or you just don't want to see *those digits* again, check out the [model building guide](models/basics.html) instead. The guide attempts to show how Flux's abstractions are built up and why it's powerful, but it's not all necessary to get started.
|
||||
|
||||
## Installation
|
||||
|
||||
*... Charging Ion Capacitors ...*
|
||||
*... Inflating Graviton Zeppelins ...*
|
||||
|
||||
```julia
|
||||
Pkg.update()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Model Building Basics
|
||||
|
||||
## Functions
|
||||
## Net Functions
|
||||
|
||||
Flux's core feature is the `@net` macro, which adds some superpowers to regular ol' Julia functions. Consider this simple function with the `@net` annotation applied:
|
||||
|
||||
|
@ -24,28 +24,53 @@ Using MXNet, we can get the gradient of the function, too:
|
|||
back!(f_mxnet, [1,1,1], [1,2,3]) == ([2.0, 4.0, 6.0])
|
||||
```
|
||||
|
||||
At first glance, this may seem broadly similar to building a graph in TensorFlow. The difference is that the Julia code still behaves like Julia code. Error messages continue to give you helpful stacktraces that pinpoint mistakes. You can step through the code in the debugger. The code only runs once when it's called, as usual, rather than once to build the graph and once to execute it.
|
||||
`f` is effectively `x^2`, so the gradient is `2x` as expected.
|
||||
|
||||
For TensorFlow users this may seem similar to building a graph as usual. The difference is that Julia code still behaves like Julia code. Error messages give you helpful stacktraces that pinpoint mistakes. You can step through the code in the debugger. The code runs when it's called, as usual, rather than running once to build the graph and then again to execute it.
|
||||
|
||||
## The Model
|
||||
|
||||
*... Initialising Photon Beams ...*
|
||||
The core concept in Flux is the *model*. This corresponds to what might be called a "layer" or "module" in other frameworks. A model is simply a differentiable function with parameters. Given a model `m` we can do things like:
|
||||
|
||||
The core concept in Flux is the *model*. A model (or "layer") is simply a function with parameters. For example, in plain Julia code, we could define the following function to represent a logistic regression (or simple neural network):
|
||||
```julia
|
||||
m(x) # See what the model does to an input vector `x`
|
||||
back!(m, Δ, x) # backpropogate the gradient `Δ` through `m`
|
||||
update!(m, η) # update the parameters of `m` using the gradient
|
||||
```
|
||||
|
||||
We can implement a model however we like as long as it fits this interface. But as hinted above, `@net` is a particularly easy way to do it, as `@net` functions are models already.
|
||||
|
||||
## Parameters
|
||||
|
||||
Consider how we'd write a logistic regression. We just take the Julia code and add `@net`.
|
||||
|
||||
```julia
|
||||
W = randn(3,5)
|
||||
b = randn(3)
|
||||
affine(x) = W * x + b
|
||||
@net logistic(x) = softmax(W * x + b)
|
||||
|
||||
x1 = rand(5) # [0.581466,0.606507,0.981732,0.488618,0.415414]
|
||||
y1 = softmax(affine(x1)) # [0.32676,0.0974173,0.575823]
|
||||
y1 = logistic(x1) # [0.32676,0.0974173,0.575823]
|
||||
```
|
||||
|
||||
`affine` is simply a function which takes some vector `x1` and outputs a new one `y1`. For example, `x1` could be data from an image and `y1` could be predictions about the content of that image. However, `affine` isn't static. It has *parameters* `W` and `b`, and if we tweak those parameters we'll tweak the result – hopefully to make the predictions more accurate.
|
||||
<!-- TODO -->
|
||||
|
||||
## Layers
|
||||
|
||||
This is all well and good, but we usually want to have more than one affine layer in our network; writing out the above definition to create new sets of parameters every time would quickly become tedious. For that reason, we want to use a *template* which creates these functions for us:
|
||||
Bigger networks contain many affine transformations like `W * x + b`. We don't want to write out the definition every time we use it. Instead, we can factor this out by making a function that produces models:
|
||||
|
||||
```julia
|
||||
function create_affine(in, out)
|
||||
W = randn(out,in)
|
||||
b = randn(out)
|
||||
@net x -> W * x + b
|
||||
end
|
||||
|
||||
affine1 = create_affine(3,2)
|
||||
affine1([1,2,3])
|
||||
```
|
||||
|
||||
Flux has a [more powerful syntax](templates.html) for this pattern, but also provides a bunch of layers out of the box. So we can instead write:
|
||||
|
||||
```julia
|
||||
affine1 = Affine(5, 5)
|
||||
|
@ -55,12 +80,8 @@ softmax(affine1(x1)) # [0.167952, 0.186325, 0.176683, 0.238571, 0.23047]
|
|||
softmax(affine2(x1)) # [0.125361, 0.246448, 0.21966, 0.124596, 0.283935]
|
||||
```
|
||||
|
||||
We just created two separate `Affine` layers, and each contains its own (randomly initialised) version of `W` and `b`, leading to a different result when called with our data. It's easy to define templates like `Affine` ourselves (see [templates](templates.html)), but Flux provides `Affine` out of the box, so we'll use that for now.
|
||||
|
||||
## Combining Layers
|
||||
|
||||
*... Inflating Graviton Zeppelins ...*
|
||||
|
||||
A more complex model usually involves many basic layers like `affine`, where we use the output of one layer as the input to the next:
|
||||
|
||||
```julia
|
||||
|
@ -85,11 +106,9 @@ mymodel3 = Chain(
|
|||
|
||||
You now know enough to take a look at the [logistic regression](../examples/logreg.md) example, if you haven't already.
|
||||
|
||||
## A Function in Model's Clothing
|
||||
## Dressed like a model
|
||||
|
||||
*... Booting Dark Matter Transmogrifiers ...*
|
||||
|
||||
We noted above that a "model" is a function with some number of trainable parameters. This goes both ways; a normal Julia function like `exp` is effectively a model with 0 parameters. Flux doesn't care, and anywhere that you use one, you can use the other. For example, `Chain` will happily work with regular functions:
|
||||
We noted above that a model is a function with trainable parameters. Normal functions like `exp` are actually models too, that happen to have 0 parameters. Flux doesn't care, and anywhere that you use one, you can use the other. For example, `Chain` will happily work with regular functions:
|
||||
|
||||
```julia
|
||||
foo = Chain(exp, sum, log)
|
||||
|
|
Loading…
Reference in New Issue