diff --git a/latest/internals/tracker.html b/latest/internals/tracker.html index c536e24d..1fed9856 100644 --- a/latest/internals/tracker.html +++ b/latest/internals/tracker.html @@ -45,11 +45,18 @@ julia> W.grad julia> x.grad 2-element Array{Float64,1}: -2.0 - -2.0
You may sometimes want to drop derivative information and just get the plain value back. You can do this by calling Tracker.data(W)
.
We can hook in to the processes above to implement custom gradients for a function or kernel. For a toy example, imagine a custom implementation of minus
:
minus(a, b) = a - b
Firstly, we must tell the tracker system to stop when it sees a call to minus
, and record it. We can do this using dispatch:
using Flux.Tracker: TrackedReal, track, @grad
+ -2.0
You may sometimes want to drop derivative information and just get the plain value back. You can do this by calling Tracker.data(W)
.
We can hook in to the processes above to implement custom gradients for a function or kernel. For a toy example, imagine a custom implementation of minus
:
minus(a, b) = a - b
Firstly, we must tell the tracker system to stop when it sees a call to minus
, and record it. We can do this using dispatch:
using Flux.Tracker: TrackedArray, track, @grad
-minus(a::TrackedArray, b::TrackedArray) = Tracker.track(minus, a, b)
track
takes care of building a new Tracked
object and recording the operation on the tape. We just need to provide a gradient definition.
@grad function minus(a, b)
- return minus(data(a),data(b)), Δ -> (Δ, -Δ)
-end
This is essentially just a way of overloading the forward
function we saw above. We strip tracking from a
and b
so that we are calling the original definition of minus
(otherwise, we'd just try to track the call again and hit an infinite regress).
Note that in the backpropagator we don't call data(a)
; we do in fact want to track this, since nest AD will take a derivative through the backpropagator itself. For example, the gradient of *
might look like this.
@grad a * b = data(a)*data(b), Δ -> (Δ*b, a*Δ)
For multi-argument functions with custom gradients, you likely want to catch not just minus(::TrackedArray, ::TrackedArray)
but also minus(::Array, TrackedArray)
and so on. To do so, just define those extra signatures as needed:
minus(a::AbstractArray, b::TrackedArray) = Tracker.track(minus, a, b)
+minus(a::TrackedArray, b::TrackedArray) = track(minus, a, b)
track
takes care of building a new Tracked
object and recording the operation on the tape. We just need to provide a gradient definition.
@grad function minus(a, b)
+ return minus(data(a), data(b)), Δ -> (Δ, -Δ)
+end
This is essentially just a way of overloading the forward
function we saw above. We strip tracking from a
and b
so that we are calling the original definition of minus
(otherwise, we'd just try to track the call again and hit an infinite regress).
Note that in the backpropagator we don't call data(a)
; we do in fact want to track this, since nest AD will take a derivative through the backpropagator itself. For example, the gradient of *
might look like this.
@grad a * b = data(a)*data(b), Δ -> (Δ*b, a*Δ)
We can then calculate the first derivative of minus
as follows:
a = param([1,2,3])
+b = param([3,2,1])
+
+c = minus(a, b) # [-2.0 (tracked), 0.0 (tracked), 2.0 (tracked)]
+
+Tracker.back!(c, 1)
+Tracker.grad(a) # [1.00, 1.00, 1.00]
+Tracker.grad(b) # [-1.00, -1.00, -1.00]
For multi-argument functions with custom gradients, you likely want to catch not just minus(::TrackedArray, ::TrackedArray)
but also minus(::Array, TrackedArray)
and so on. To do so, just define those extra signatures as needed:
minus(a::AbstractArray, b::TrackedArray) = Tracker.track(minus, a, b)
minus(a::TrackedArray, b::AbstractArray) = Tracker.track(minus, a, b)
All Tracked*
objects (TrackedArray
, TrackedReal
) are light wrappers around the Tracked
type, which you can access via the .tracker
field.
julia> x.tracker
Flux.Tracker.Tracked{Array{Float64,1}}(0x00000000, Flux.Tracker.Call{Nothing,Tuple{}}(nothing, ()), true, [5.0, 6.0], [-2.0, -2.0])
The Tracker
stores the gradient of a given object, which we've seen before.
julia> x.tracker.grad
2-element Array{Float64,1}:
diff --git a/latest/models/layers.html b/latest/models/layers.html
index f35b581c..d626edbf 100644
--- a/latest/models/layers.html
+++ b/latest/models/layers.html
@@ -11,26 +11,26 @@ 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
Flux.Conv
— Type.Conv(size, in=>out)
-Conv(size, in=>out, relu)
Standard convolutional layer. size
should be a tuple like (2, 2)
. in
and out
specify the number of input and output channels respectively.
Data should be stored in WHCN order. In other words, a 100×100 RGB image would be a 100×100×3
array, and a batch of 50 would be a 100×100×3×50
array.
Takes the keyword arguments pad
, stride
and dilation
.
Flux.MaxPool
— Type.MaxPool(k)
Max pooling layer. k
stands for the size of the window for each dimension of the input.
Takes the keyword arguments pad
and stride
.
Flux.MeanPool
— Type.MeanPool(k)
Mean pooling layer. k
stands for the size of the window for each dimension of the input.
Takes the keyword arguments pad
and stride
.
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)
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.GRU
— Function.GRU(in::Integer, out::Integer)
Gated Recurrent Unit 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
Flux.Conv
— Type.Conv(size, in=>out)
+Conv(size, in=>out, relu)
Standard convolutional layer. size
should be a tuple like (2, 2)
. in
and out
specify the number of input and output channels respectively.
Data should be stored in WHCN order. In other words, a 100×100 RGB image would be a 100×100×3
array, and a batch of 50 would be a 100×100×3×50
array.
Takes the keyword arguments pad
, stride
and dilation
.
Flux.MaxPool
— Type.MaxPool(k)
Max pooling layer. k
stands for the size of the window for each dimension of the input.
Takes the keyword arguments pad
and stride
.
Flux.MeanPool
— Type.MeanPool(k)
Mean pooling layer. k
stands for the size of the window for each dimension of the input.
Takes the keyword arguments pad
and stride
.
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)
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.GRU
— Function.GRU(in::Integer, out::Integer)
Gated Recurrent Unit 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) =
+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) - 1)
Exponential Linear Unit activation function. See Fast and Accurate Deep Network Learning by Exponential Linear Units. You can also specify the coefficient explicitly, e.g. elu(x, 1)
.
NNlib.swish
— Function.swish(x) = x * σ(x)
Self-gated actvation function. See Swish: a Self-Gated Activation Function.
These layers don't affect the structure of the network but may improve training times or reduce overfitting.
Flux.testmode!
— Function.Flux.BatchNorm
— Type.Flux.BatchNorm
— Type.BatchNorm(channels::Integer, σ = identity;
initβ = zeros, initγ = ones,
ϵ = 1e-8, momentum = .1)
Batch Normalization layer. The channels
input should be the size of the channel dimension in your data (see below).
Given an array with N
dimensions, call the N-1
th the channel dimension. (For a batch of feature vectors this is just the data dimension, for WHCN
images it's the usual channel dimension.)
BatchNorm
computes the mean and variance for each each W×H×1×N
slice and shifts them to have a new mean and variance (corresponding to the learnable, per-channel bias
and scale
parameters).
See Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift.
Example:
m = Chain(
Dense(28^2, 64),
BatchNorm(64, relu),
Dense(64, 10),
BatchNorm(10),
- softmax)
Flux.Dropout
— Type.Dropout(p)
A Dropout layer. For each input, either sets that input to 0
(with probability p
) or scales it by 1/(1-p)
. This is used as a regularisation, i.e. it reduces overfitting during training.
Does nothing to the input once in testmode!
.
Flux.LayerNorm
— Type.LayerNorm(h::Integer)
A normalisation layer designed to be used with recurrent hidden states of size h
. Normalises the mean/stddev of each input before applying a per-neuron gain/bias.