diff --git a/latest/models/basics.html b/latest/models/basics.html index d70ca15e..c603dfd6 100644 --- a/latest/models/basics.html +++ b/latest/models/basics.html @@ -13,17 +13,17 @@ predict(x) = W*x .+ b loss(x, y) = sum((predict(x) .- y).^2) x, y = rand(5), rand(2) # Dummy data -loss(x, y) # ~ 3
To improve the prediction we can take the gradients of W
and b
with respect to the loss function and perform gradient descent. We could calculate gradients by hand, but Flux will do it for us if we tell it that W
and b
are trainable parameters.
using Flux.Tracker: param, back!, data, grad
+loss(x, y) # ~ 3
To improve the prediction we can take the gradients of W
and b
with respect to the loss function and perform gradient descent. We could calculate gradients by hand, but Flux will do it for us if we tell it that W
and b
are trainable parameters.
using Flux.Tracker
W = param(W)
b = param(b)
l = loss(x, y)
-back!(l)
loss(x, y)
returns the same number, but it's now a tracked value that records gradients as it goes along. Calling back!
then calculates the gradient of W
and b
. We can see what this gradient is, and modify W
to train the model.
grad(W)
+back!(l)
loss(x, y)
returns the same number, but it's now a tracked value that records gradients as it goes along. Calling back!
then calculates the gradient of W
and b
. We can see what this gradient is, and modify W
to train the model.
W.grad
# Update the parameter
-W.data .-= 0.1grad(W)
+W.data .-= 0.1(W.grad)
loss(x, y) # ~ 2.5
The loss has decreased a little, meaning that our prediction x
is closer to the target y
. If we have some data we can already try training the model.
All deep learning in Flux, however complex, is a simple generalisation of this example. Of course, models can look very different – they might have millions of parameters or complex control flow, and there are ways to manage this complexity. Let's see what that looks like.
It's common to create more complex models than the linear regression above. For example, we might want to have two linear layers with a nonlinearity like sigmoid (σ
) in between them. In the above style we could write this as:
W1 = param(rand(3, 5))
b1 = param(rand(3))
diff --git a/latest/models/layers.html b/latest/models/layers.html
index 79669ee4..9bf6917b 100644
--- a/latest/models/layers.html
+++ b/latest/models/layers.html
@@ -11,16 +11,16 @@ m(5) == 26
m = Chain(Dense(10, 5), Dense(5, 2))
x = rand(10)
-m(x) == m[2](m[1](x))
Chain
also supports indexing and slicing, e.g. m[2]
or m[1:end-1]
. m[1:3](x)
will calculate the output of the first three layers.
Flux.Dense
— Type.Dense(in::Integer, out::Integer, σ = identity)
Creates a traditional Dense
layer with parameters W
and b
.
y = σ.(W * x .+ b)
The input x
must be a vector of length in
, or a batch of vectors represented as an in × N
matrix. The out y
will be a vector or batch of length out
.
julia> d = Dense(5, 2)
+m(x) == m[2](m[1](x))
Chain
also supports indexing and slicing, e.g. m[2]
or m[1:end-1]
. m[1:3](x)
will calculate the output of the first three layers.
Flux.Dense
— Type.Dense(in::Integer, out::Integer, σ = identity)
Creates a traditional Dense
layer with parameters W
and b
.
y = σ.(W * x .+ b)
The input x
must be a vector of length in
, or a batch of vectors represented as an in × N
matrix. The out y
will be a vector or batch of length out
.
julia> d = Dense(5, 2)
Dense(5, 2)
julia> d(rand(5))
Tracked 2-element Array{Float64,1}:
0.00257447
- -0.00449443
Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data).
Flux.RNN
— Function.RNN(in::Integer, out::Integer, σ = tanh)
The most basic recurrent layer; essentially acts as a Dense
layer, but with the output fed back into the input each time step.
Flux.LSTM
— Function.LSTM(in::Integer, out::Integer, σ = tanh)
Long Short Term Memory recurrent layer. Behaves like an RNN but generally exhibits a longer memory span over sequences.
See this article for a good overview of the internals.
Flux.Recur
— Type.Recur(cell)
Recur
takes a recurrent cell and makes it stateful, managing the hidden state in the background. cell
should be a model of the form:
h, y = cell(h, x...)
For example, here's a recurrent network that keeps a running total of its inputs.
accum(h, x) = (h+x, x)
+ -0.00449443
Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data).
Flux.RNN
— Function.RNN(in::Integer, out::Integer, σ = tanh)
The most basic recurrent layer; essentially acts as a Dense
layer, but with the output fed back into the input each time step.
Flux.LSTM
— Function.LSTM(in::Integer, out::Integer, σ = tanh)
Long Short Term Memory recurrent layer. Behaves like an RNN but generally exhibits a longer memory span over sequences.
See this article for a good overview of the internals.
Flux.Recur
— Type.Recur(cell)
Recur
takes a recurrent cell and makes it stateful, managing the hidden state in the background. cell
should be a model of the form:
h, y = cell(h, x...)
For example, here's a recurrent network that keeps a running total of its inputs.
accum(h, x) = (h+x, x)
rnn = Flux.Recur(accum, 0)
rnn(2) # 2
rnn(3) # 3
rnn.state # 5
rnn.(1:10) # apply to a sequence
-rnn.state # 60
Non-linearities that go between layers of your model. Most of these functions are defined in NNlib but are available by default in Flux.
Note that, unless otherwise stated, activation functions operate on scalars. To apply them to an array you can call σ.(xs)
, relu.(xs)
and so on.
NNlib.σ
— Function.σ(x) = 1 / (1 + exp(-x))
Classic sigmoid activation function.
NNlib.relu
— Function.relu(x) = max(0, x)
Rectified Linear Unit activation function.
NNlib.leakyrelu
— Function.leakyrelu(x) = max(0.01x, x)
Leaky Rectified Linear Unit activation function.
You can also specify the coefficient explicitly, e.g. leakyrelu(x, 0.01)
.
NNlib.elu
— Function.elu(x; α = 1) = x > 0 ? x : α * (exp(x) - one(x)
Exponential Linear Unit activation function. See Fast and Accurate Deep Network Learning by Exponential Linear Units
NNlib.swish
— Function.