From 2d18f0e297c693b4e2d9ccce70a4b8af295f6498 Mon Sep 17 00:00:00 2001 From: zeptodoctor <44736852+zeptodoctor@users.noreply.github.com> Date: Wed, 24 Apr 2019 16:21:13 +0000 Subject: [PATCH] build based on 01ffa21 --- dev/models/basics/index.html | 4 ++-- dev/models/layers/index.html | 16 ++++++++-------- dev/search_index.js | 2 +- dev/training/optimisers/index.html | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/dev/models/basics/index.html b/dev/models/basics/index.html index 99089dff..ef977b5f 100644 --- a/dev/models/basics/index.html +++ b/dev/models/basics/index.html @@ -34,10 +34,10 @@ julia> f(x) = W * x + b; julia> grads = Tracker.gradient(() -> f(4), params(W, b)); julia> grads[W] -4.0 +4.0 (tracked) julia> grads[b] -1.0

There are a few things to notice here. Firstly, W and b now show up as tracked. Tracked things behave like normal numbers or arrays, but keep records of everything you do with them, allowing Flux to calculate their gradients. gradient takes a zero-argument function; no arguments are necessary because the params tell it what to differentiate.

This will come in really handy when dealing with big, complicated models. For now, though, let's start with something simple.

Simple Models

Consider a simple linear regression, which tries to predict an output array y from an input x.

W = rand(2, 5)
+1.0 (tracked)

There are a few things to notice here. Firstly, W and b now show up as tracked. Tracked things behave like normal numbers or arrays, but keep records of everything you do with them, allowing Flux to calculate their gradients. gradient takes a zero-argument function; no arguments are necessary because the params tell it what to differentiate.

This will come in really handy when dealing with big, complicated models. For now, though, let's start with something simple.

Simple Models

Consider a simple linear regression, which tries to predict an output array y from an input x.

W = rand(2, 5)
 b = rand(2)
 
 predict(x) = W*x .+ b
diff --git a/dev/models/layers/index.html b/dev/models/layers/index.html
index 59300a8f..a15900eb 100644
--- a/dev/models/layers/index.html
+++ b/dev/models/layers/index.html
@@ -11,34 +11,34 @@ 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.

source
Flux.DenseType.
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.

source
Flux.DenseType.
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
source

Convolution and Pooling Layers

These layers are used to build convolutional neural networks (CNNs).

Flux.ConvType.
Conv(size, in=>out)
+  -0.00449443
source

Convolution and Pooling Layers

These layers are used to build convolutional neural networks (CNNs).

Flux.ConvType.
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.

Example: Applying Conv layer to a 1-channel input using a 2x2 window size, giving us a 16-channel output. Output is activated with ReLU.

size = (2,2)
 in = 1
 out = 16 
-Conv((2, 2), 1=>16, relu)

Data should be stored in WHCN order (width, height, # channels, # batches). In other words, a 100×100 RGB image would be a 100×100×3×1 array, and a batch of 50 would be a 100×100×3×50 array.

Takes the keyword arguments pad, stride and dilation.

source
Flux.MaxPoolType.
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.

source
Flux.MeanPoolType.
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.

source
Flux.DepthwiseConvType.
DepthwiseConv(size, in)
+Conv((2, 2), 1=>16, relu)

Data should be stored in WHCN order (width, height, # channels, # batches). In other words, a 100×100 RGB image would be a 100×100×3×1 array, and a batch of 50 would be a 100×100×3×50 array.

Takes the keyword arguments pad, stride and dilation.

source
Flux.MaxPoolType.
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.

source
Flux.MeanPoolType.
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.

source
Flux.DepthwiseConvType.
DepthwiseConv(size, in)
 DepthwiseConv(size, in=>mul)
-DepthwiseConv(size, in=>mul, relu)

Depthwise convolutional layer. size should be a tuple like (2, 2). in and mul specify the number of input channels and channel multiplier respectively. In case the mul is not specified it is taken as 1.

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 and stride.

source
Flux.ConvTransposeType.
ConvTranspose(size, in=>out)
-ConvTranspose(size, in=>out, relu)

Standard convolutional transpose 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.

source

Recurrent Layers

Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data).

Flux.RNNFunction.
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.

source
Flux.LSTMFunction.
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.

source
Flux.GRUFunction.
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.

source
Flux.RecurType.
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)
+DepthwiseConv(size, in=>mul, relu)

Depthwise convolutional layer. size should be a tuple like (2, 2). in and mul specify the number of input channels and channel multiplier respectively. In case the mul is not specified it is taken as 1.

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 and stride.

source
Flux.ConvTransposeType.
ConvTranspose(size, in=>out)
+ConvTranspose(size, in=>out, relu)

Standard convolutional transpose 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.

source

Recurrent Layers

Much like the core layers above, but can be used to process sequence data (as well as other kinds of structured data).

Flux.RNNFunction.
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.

source
Flux.LSTMFunction.
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.

source
Flux.GRUFunction.
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.

source
Flux.RecurType.
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
source

Other General Purpose Layers

These are marginally more obscure than the Basic Layers. But in contrast to the layers described in the other sections are not readily grouped around a particular purpose (e.g. CNNs or RNNs).

Flux.MaxoutType.
Maxout(over)

Maxout is a neural network layer, which has a number of internal layers, which all have the same input, and the maxout returns the elementwise maximium of the internal layers' outputs.

Maxout over linear dense layers satisfies the univeral approximation theorem.

Reference: Ian J. Goodfellow, David Warde-Farley, Mehdi Mirza, Aaron Courville, and Yoshua Bengio.

  1. Maxout networks.

In Proceedings of the 30th International Conference on International Conference on Machine Learning - Volume 28 (ICML'13), Sanjoy Dasgupta and David McAllester (Eds.), Vol. 28. JMLR.org III-1319-III-1327. https://arxiv.org/pdf/1302.4389.pdf

source

Normalisation & Regularisation

These layers don't affect the structure of the network but may improve training times or reduce overfitting.

Flux.testmode!Function.
testmode!(m)
-testmode!(m, false)

Put layers like Dropout and BatchNorm into testing mode (or back to training mode with false).

source
Flux.BatchNormType.
BatchNorm(channels::Integer, σ = identity;
+rnn.state # 60
source

Other General Purpose Layers

These are marginally more obscure than the Basic Layers. But in contrast to the layers described in the other sections are not readily grouped around a particular purpose (e.g. CNNs or RNNs).

Flux.MaxoutType.
Maxout(over)

Maxout is a neural network layer, which has a number of internal layers, which all have the same input, and the maxout returns the elementwise maximium of the internal layers' outputs.

Maxout over linear dense layers satisfies the univeral approximation theorem.

Reference: Ian J. Goodfellow, David Warde-Farley, Mehdi Mirza, Aaron Courville, and Yoshua Bengio.

  1. Maxout networks.

In Proceedings of the 30th International Conference on International Conference on Machine Learning - Volume 28 (ICML'13), Sanjoy Dasgupta and David McAllester (Eds.), Vol. 28. JMLR.org III-1319-III-1327. https://arxiv.org/pdf/1302.4389.pdf

source

Normalisation & Regularisation

These layers don't affect the structure of the network but may improve training times or reduce overfitting.

Flux.testmode!Function.
testmode!(m)
+testmode!(m, false)

Put layers like Dropout and BatchNorm into testing mode (or back to training mode with false).

source
Flux.BatchNormType.
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-1th 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)
source
Flux.DropoutType.
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!.

source
Flux.LayerNormType.
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.

source

Activation Functions

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.reluFunction.
relu(x) = max(0, x)

Rectified Linear Unit activation function.

NNlib.leakyreluFunction.
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.eluFunction.
elu(x, α = 1) =
+  softmax)
source
Flux.DropoutType.
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!.

source
Flux.LayerNormType.
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.

source

Activation Functions

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.reluFunction.
relu(x) = max(0, x)

Rectified Linear Unit activation function.

NNlib.leakyreluFunction.
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.eluFunction.
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.swishFunction.
swish(x) = x * σ(x)

Self-gated actvation function. See Swish: a Self-Gated Activation Function.

Normalisation & Regularisation

These layers don't affect the structure of the network but may improve training times or reduce overfitting.

Flux.testmode!
 BatchNorm
 Dropout
diff --git a/dev/search_index.js b/dev/search_index.js
index 19177ed9..e47215ec 100644
--- a/dev/search_index.js
+++ b/dev/search_index.js
@@ -53,7 +53,7 @@ var documenterSearchIndex = {"docs": [
     "page": "Basics",
     "title": "Taking Gradients",
     "category": "section",
-    "text": "Flux\'s core feature is taking gradients of Julia code. The gradient function takes another Julia function f and a set of arguments, and returns the gradient with respect to each argument. (It\'s a good idea to try pasting these examples in the Julia terminal.)julia> using Flux.Tracker\n\njulia> f(x) = 3x^2 + 2x + 1;\n\njulia> df(x) = Tracker.gradient(f, x; nest = true)[1]; # df/dx = 6x + 2\n\njulia> df(2)\n14.0 (tracked)\n\njulia> d2f(x) = Tracker.gradient(df, x; nest = true)[1]; # d²f/dx² = 6\n\njulia> d2f(2)\n6.0 (tracked)(We\'ll learn more about why these numbers show up as (tracked) below.)When a function has many parameters, we can pass them all in explicitly:julia> f(W, b, x) = W * x + b;\n\njulia> Tracker.gradient(f, 2, 3, 4)\n(4.0 (tracked), 1.0 (tracked), 2.0 (tracked))But machine learning models can have hundreds of parameters! Flux offers a nice way to handle this. We can tell Flux to treat something as a parameter via param. Then we can collect these together and tell gradient to collect the gradients of all params at once.julia> using Flux\n\njulia> W = param(2) \n2.0 (tracked)\n\njulia> b = param(3)\n3.0 (tracked)\n\njulia> f(x) = W * x + b;\n\njulia> grads = Tracker.gradient(() -> f(4), params(W, b));\n\njulia> grads[W]\n4.0\n\njulia> grads[b]\n1.0There are a few things to notice here. Firstly, W and b now show up as tracked. Tracked things behave like normal numbers or arrays, but keep records of everything you do with them, allowing Flux to calculate their gradients. gradient takes a zero-argument function; no arguments are necessary because the params tell it what to differentiate.This will come in really handy when dealing with big, complicated models. For now, though, let\'s start with something simple."
+    "text": "Flux\'s core feature is taking gradients of Julia code. The gradient function takes another Julia function f and a set of arguments, and returns the gradient with respect to each argument. (It\'s a good idea to try pasting these examples in the Julia terminal.)julia> using Flux.Tracker\n\njulia> f(x) = 3x^2 + 2x + 1;\n\njulia> df(x) = Tracker.gradient(f, x; nest = true)[1]; # df/dx = 6x + 2\n\njulia> df(2)\n14.0 (tracked)\n\njulia> d2f(x) = Tracker.gradient(df, x; nest = true)[1]; # d²f/dx² = 6\n\njulia> d2f(2)\n6.0 (tracked)(We\'ll learn more about why these numbers show up as (tracked) below.)When a function has many parameters, we can pass them all in explicitly:julia> f(W, b, x) = W * x + b;\n\njulia> Tracker.gradient(f, 2, 3, 4)\n(4.0 (tracked), 1.0 (tracked), 2.0 (tracked))But machine learning models can have hundreds of parameters! Flux offers a nice way to handle this. We can tell Flux to treat something as a parameter via param. Then we can collect these together and tell gradient to collect the gradients of all params at once.julia> using Flux\n\njulia> W = param(2) \n2.0 (tracked)\n\njulia> b = param(3)\n3.0 (tracked)\n\njulia> f(x) = W * x + b;\n\njulia> grads = Tracker.gradient(() -> f(4), params(W, b));\n\njulia> grads[W]\n4.0 (tracked)\n\njulia> grads[b]\n1.0 (tracked)There are a few things to notice here. Firstly, W and b now show up as tracked. Tracked things behave like normal numbers or arrays, but keep records of everything you do with them, allowing Flux to calculate their gradients. gradient takes a zero-argument function; no arguments are necessary because the params tell it what to differentiate.This will come in really handy when dealing with big, complicated models. For now, though, let\'s start with something simple."
 },
 
 {
diff --git a/dev/training/optimisers/index.html b/dev/training/optimisers/index.html
index bceba62f..4d53052c 100644
--- a/dev/training/optimisers/index.html
+++ b/dev/training/optimisers/index.html
@@ -27,4 +27,4 @@ end

Running this will alter the parameters W and

An optimiser update! accepts a parameter and a gradient, and updates the parameter according to the chosen rule. We can also pass opt to our training loop, which will update all parameters of the model in a loop. However, we can now easily replace Descent with a more advanced optimiser such as ADAM.

Optimiser Reference

All optimisers return an object that, when passed to train!, will update the parameters passed to it.

Flux.Optimise.DescentType.
Descent(η)

Classic gradient descent optimiser with learning rate η. For each parameter p and its gradient δp, this runs p -= η*δp.

source
Flux.Optimise.MomentumType.
Momentum(params, η = 0.01; ρ = 0.9)

Gradient descent with learning rate η and momentum ρ.

source
Flux.Optimise.NesterovType.
Nesterov(eta, ρ = 0.9)

Gradient descent with learning rate η and Nesterov momentum ρ.

source
Flux.Optimise.RMSPropType.
RMSProp(η = 0.001, ρ = 0.9)

RMSProp optimiser. Parameters other than learning rate don't need tuning. Often a good choice for recurrent networks.

source
Flux.Optimise.ADAMType.
ADAM(η = 0.001, β = (0.9, 0.999))

ADAM optimiser.

source
Flux.Optimise.AdaMaxType.
AdaMax(params, η = 0.001; β1 = 0.9, β2 = 0.999, ϵ = 1e-08)

AdaMax optimiser. Variant of ADAM based on the ∞-norm.

source
Flux.Optimise.ADAGradType.
ADAGrad(η = 0.1; ϵ = 1e-8)

ADAGrad optimiser. Parameters don't need tuning.

source
Flux.Optimise.ADADeltaType.
ADADelta(ρ = 0.9, ϵ = 1e-8)

ADADelta optimiser. Parameters don't need tuning.

source
Flux.Optimise.AMSGradType.
AMSGrad(η = 0.001, β = (0.9, 0.999))

AMSGrad optimiser. Parameters don't need tuning.

source
Flux.Optimise.NADAMType.
NADAM(η = 0.001, β = (0.9, 0.999))

NADAM optimiser. Parameters don't need tuning.

source
Flux.Optimise.ADAMWFunction.
ADAMW((η = 0.001, β = (0.9, 0.999), decay = 0)

ADAMW fixing weight decay regularization in Adam.

source
+end

An optimiser update! accepts a parameter and a gradient, and updates the parameter according to the chosen rule. We can also pass opt to our training loop, which will update all parameters of the model in a loop. However, we can now easily replace Descent with a more advanced optimiser such as ADAM.

Optimiser Reference

All optimisers return an object that, when passed to train!, will update the parameters passed to it.

Flux.Optimise.DescentType.
Descent(η)

Classic gradient descent optimiser with learning rate η. For each parameter p and its gradient δp, this runs p -= η*δp.

source
Flux.Optimise.MomentumType.
Momentum(params, η = 0.01; ρ = 0.9)

Gradient descent with learning rate η and momentum ρ.

source
Flux.Optimise.NesterovType.
Nesterov(eta, ρ = 0.9)

Gradient descent with learning rate η and Nesterov momentum ρ.

source
Flux.Optimise.RMSPropType.
RMSProp(η = 0.001, ρ = 0.9)

RMSProp optimiser. Parameters other than learning rate don't need tuning. Often a good choice for recurrent networks.

source
Flux.Optimise.ADAMType.
ADAM(η = 0.001, β = (0.9, 0.999))

ADAM optimiser.

source
Flux.Optimise.AdaMaxType.
AdaMax(params, η = 0.001; β1 = 0.9, β2 = 0.999, ϵ = 1e-08)

AdaMax optimiser. Variant of ADAM based on the ∞-norm.

source
Flux.Optimise.ADAGradType.
ADAGrad(η = 0.1; ϵ = 1e-8)

ADAGrad optimiser. Parameters don't need tuning.

source
Flux.Optimise.ADADeltaType.
ADADelta(ρ = 0.9, ϵ = 1e-8)

ADADelta optimiser. Parameters don't need tuning.

source
Flux.Optimise.AMSGradType.
AMSGrad(η = 0.001, β = (0.9, 0.999))

AMSGrad optimiser. Parameters don't need tuning.

source
Flux.Optimise.NADAMType.
NADAM(η = 0.001, β = (0.9, 0.999))

NADAM optimiser. Parameters don't need tuning.

source
Flux.Optimise.ADAMWFunction.
ADAMW((η = 0.001, β = (0.9, 0.999), decay = 0)

ADAMW fixing weight decay regularization in Adam.

source