Shortcuts

Dask Arrays

Dask arrays are blocked numpy arrays

Dask arrays coordinate many Numpy arrays, arranged into chunks within a grid. They support a large subset of the Numpy API.

Start Dask Client for Dashboard

Starting the Dask Client is optional. It will provide a dashboard which is useful to gain insight on the computation.

The link to the dashboard will become visible when you create the client below. We recommend having it open on one side of your screen while using your notebook on the other side. This can take some effort to arrange your windows, but seeing them both at the same is very useful when learning.

[1]:
from dask.distributed import Client, progress
client = Client(processes=False, threads_per_worker=4,
                n_workers=1, memory_limit='2GB')
client
[1]:

Client

Cluster

  • Workers: 1
  • Cores: 4
  • Memory: 2.00 GB

Create Random array

This creates a 10000x10000 array of random numbers, represented as many numpy arrays of size 1000x1000 (or smaller if the array cannot be divided evenly). In this case there are 100 (10x10) numpy arrays of size 1000x1000.

[2]:
import dask.array as da
x = da.random.random((10000, 10000), chunks=(1000, 1000))
x
[2]:
Array Chunk
Bytes 800.00 MB 8.00 MB
Shape (10000, 10000) (1000, 1000)
Count 100 Tasks 100 Chunks
Type float64 numpy.ndarray
10000 10000

Use NumPy syntax as usual

[3]:
y = x + x.T
z = y[::2, 5000:].mean(axis=1)
z
[3]:
Array Chunk
Bytes 40.00 kB 4.00 kB
Shape (5000,) (500,)
Count 430 Tasks 10 Chunks
Type float64 numpy.ndarray
5000 1

Call .compute() when you want your result as a NumPy array.

If you started Client() above then you may want to watch the status page during computation.

[4]:
z.compute()
[4]:
array([1.00110284, 0.988118  , 0.99416733, ..., 1.00324168, 0.99759421,
       0.99583532])

Persist data in memory

If you have the available RAM for your dataset then you can persist data in memory.

This allows future computations to be much faster.

[5]:
y = y.persist()
[6]:
%time y[0, 0].compute()
CPU times: user 11.5 ms, sys: 2.61 ms, total: 14.1 ms
Wall time: 11.7 ms
[6]:
0.5986720892786701
[7]:
%time y.sum().compute()
CPU times: user 297 ms, sys: 31.2 ms, total: 329 ms
Wall time: 214 ms
[7]:
100000439.551797

Further Reading

A more in-depth guide to working with Dask arrays can be found in the dask tutorial, notebook 03.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources