Skip to content

Commit

Permalink
less verbose code in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Apr 29, 2023
1 parent aecb711 commit 2b7042b
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions docs/src/gpu.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,20 @@ In order to train the model using the GPU both model and the training data have

1. Iterating over the batches in a [DataLoader](@ref) object transferring each one of the training batches at a time to the GPU. This is recommended for large datasets. Done hand, it might look like this:
```julia
train_loader = Flux.DataLoader((xtrain, ytrain), batchsize = 64, shuffle = true)
train_loader = Flux.DataLoader((X, Y), batchsize=64, shuffle=true)
# ... model definition, optimiser setup
for epoch in 1:epochs
for (xtrain_batch, ytrain_batch) in train_loader
x = gpu(xtrain_batch)
y = gpu(ytrain_batch)
for (x_cpu, y_cpu) in train_loader
x = gpu(x_cpu)
y = gpu(y_cpu)
grads = gradient(m -> loss(m, x, y), model)
Flux.update!(opt_state, model, grads[1])
end
end
```
Rather than write this every time, there is a method of `gpu(::DataLoader)` which does it for you:
```julia
gpu_train_loader = Flux.DataLoader((xtrain, ytrain), batchsize = 64, shuffle = true) |> gpu
gpu_train_loader = Flux.DataLoader((X, Y), batchsize=64, shuffle=true) |> gpu
# ... model definition, optimiser setup
for epoch in 1:epochs
for (x, y) in gpu_train_loader
Expand All @@ -150,18 +150,18 @@ In order to train the model using the GPU both model and the training data have
end
end
```
This is equivalent to `DataLoader(MLUtils.mapobs(gpu, (xtrain, ytrain)); keywords...)`.
This is equivalent to `DataLoader(MLUtils.mapobs(gpu, (X, Y)); keywords...)`.
Something similar can also be done with [`CUDA.CuIterator`](https://cuda.juliagpu.org/stable/usage/memory/#Batching-iterator), `gpu_train_loader = CUDA.CuIterator(train_loader)`. However, this only works with a limited number of data types: `first(train_loader)` should be a tuple (or `NamedTuple`) of arrays.

2. Transferring all training data to the GPU at once before creating the [DataLoader](@ref) object. This is usually performed for smaller datasets which are sure to fit in the available GPU memory.
```julia
gpu_train_loader = Flux.DataLoader((xtrain, ytrain) |> gpu, batchsize = 32)
gpu_train_loader = Flux.DataLoader((X, Y) |> gpu, batchsize = 32)
# ...
for epoch in 1:epochs
for (x, y) in gpu_train_loader
# ...
```
Here `(xtrain, ytrain) |> gpu` applies [`gpu`](@ref) to both arrays -- it recurses into not just tuples, as here, but also whole Flux models.
Here `(X, Y) |> gpu` applies [`gpu`](@ref) to both arrays -- it recurses into not just tuples, as here, but also whole Flux models.

## Saving GPU-Trained Models

Expand Down

0 comments on commit 2b7042b

Please sign in to comment.