diff --git a/dev/gpu/index.html b/dev/gpu/index.html index 57c82beb..5178af98 100644 --- a/dev/gpu/index.html +++ b/dev/gpu/index.html @@ -15,13 +15,13 @@ predict(x) = W*x .+ b loss(x, y) = sum((predict(x) .- y).^2) x, y = cu(rand(5)), cu(rand(2)) # Dummy data -loss(x, y) # ~ 3
Note that we convert both the parameters (W
, b
) and the data set (x
, y
) to cuda arrays. Taking derivatives and training works exactly as before.
If you define a structured model, like a Dense
layer or Chain
, you just need to convert the internal parameters. Flux provides mapleaves
, which allows you to alter all parameters of a model at once.
d = Dense(10, 5, σ)
-d = mapleaves(cu, d)
+loss(x, y) # ~ 3
Note that we convert both the parameters (W
, b
) and the data set (x
, y
) to cuda arrays. Taking derivatives and training works exactly as before.
If you define a structured model, like a Dense
layer or Chain
, you just need to convert the internal parameters. Flux provides fmap
, which allows you to alter all parameters of a model at once.
d = Dense(10, 5, σ)
+d = fmap(cu, d)
d.W # Tracked CuArray
d(cu(rand(10))) # CuArray output
m = Chain(Dense(10, 5, σ), Dense(5, 2), softmax)
-m = mapleaves(cu, m)
+m = fmap(cu, m)
d(cu(rand(10)))
As a convenience, Flux provides the gpu
function to convert models and data to the GPU if one is available. By default, it'll do nothing, but loading CuArrays
will cause it to move data to the GPU instead.
julia> using Flux, CuArrays
julia> m = Dense(10,5) |> gpu
diff --git a/dev/models/basics/index.html b/dev/models/basics/index.html
index e33cdc9b..c82c5c67 100644
--- a/dev/models/basics/index.html
+++ b/dev/models/basics/index.html
@@ -110,4 +110,4 @@ model2(rand(10)) # => 2-element vector
This quickly starts to m(rand(10))
Likewise, Chain
will happily work with any Julia function.
m = Chain(x -> x^2, x -> x+1)
-m(5) # => 26
Flux provides a set of helpers for custom layers, which you can enable by calling
Flux.@treelike Affine
This enables a useful extra set of functionality for our Affine
layer, such as collecting its parameters or moving it to the GPU.