The NP on MXNet cheat sheet

To begin, import the np and npx module and update MXNet to run in NumPy-like mode.

[1]:
from mxnet import np, npx
npx.set_np()  # Change MXNet to the numpy-like mode.

NDArray figure (TODO)

Creating arrays

[2]:
np.array([1, 2, 3])  # default datatype is float32
[04:51:27] /work/mxnet/src/storage/storage.cc:202: Using Pooled (Naive) StorageManager for CPU
[2]:
array([1., 2., 3.])
[3]:
np.array([(1.5, 2, 3), (4, 5, 6)], dtype='float16')
[3]:
array([[1.5, 2. , 3. ],
       [4. , 5. , 6. ]], dtype=float16)
[4]:
np.array([[(15,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype='int32')
[4]:
array([[[15,  2,  3],
        [ 4,  5,  6]],

       [[ 3,  2,  1],
        [ 4,  5,  6]]], dtype=int32)

Initial placeholders

[5]:
np.zeros((3, 4))  # Create an array of zeros
[5]:
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
[6]:
np.ones((2, 3, 4), dtype='int8')  # Create an array of ones
[6]:
array([[[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]],

       [[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]], dtype=int8)
[7]:
np.arange(10, 25, 5)  # Create an array of evenly spaced values (step value)
[7]:
array([10., 15., 20.])
[8]:
# Create an array of evenly spaced values (number of samples)
# np.linspace(0, 2, 9)
[9]:
# np.full((2, 2), 7)  # Create a constant array
[10]:
# np.eye(2)  # Create a 2X2 identity matrix
[11]:
# np.random.random((2, 2))  # Create an array with random values
[12]:
np.empty((3,2))  # Create an empty array
[12]:
array([[3.3631163e-44,           nan],
       [1.3269378e+28, 4.5856091e-41],
       [1.4012985e-45, 0.0000000e+00]])

I/O

Saving and loading on disk

[13]:
# Save one array
a = np.array([1, 2, 3])
npx.save('my_array', a)
npx.load('my_array')
[13]:
array([1., 2., 3.])
[14]:
# Save a list of arrays
b = np.array([4, 6, 8])
npx.savez('my_arrays', *[a, b])
npx.load('my_arrays')
[14]:
{'arr_0': array([1., 2., 3.]), 'arr_1': array([4., 6., 8.])}

Saving and loading text files

[15]:
# np.loadtxt("myfile.txt")
# np.genfromtxt("my_file.csv", delimiter=',')
# np.savetxt("myarray.txt", a, delimiter=" ")

Data types

[16]:
# np.int64    # Signed 64-bit integer types
# np.float32  # Standard double-precision floating point
# np.complex  # Complex numbers represented by 128 floats
# np.bool     # Boolean type storing TRUE and FALSE values
# np.object   # Python object type
# np.string_  # Fixed-length string type
# np.unicode_ # Fixed-length unicode type

Inspecting your array

[17]:
a.shape # Array dimensions
[17]:
(3,)
[18]:
len(a) # Length of array
[18]:
3
[19]:
b.ndim # Number of array dimensions
[19]:
1
[20]:
b.size # Number of array elements
[20]:
3
[21]:
b.dtype # Data type of array elements
[21]:
dtype('float32')
[22]:
# b.dtype.name # Name of data type
[23]:
b.astype('int') # Convert an array to a different type
[23]:
array([4, 6, 8], dtype=int64)

Asking For Help

[24]:
# np.info(np.ndarray.dtype)

Array mathematics

Arithmetic operations

[25]:
a - b # Subtraction
[25]:
array([-3., -4., -5.])
[26]:
np.subtract(a, b) # Subtraction
[26]:
array([-3., -4., -5.])
[27]:
b + a # Addition
[27]:
array([ 5.,  8., 11.])
[28]:
np.add(b, a) # Addition
[28]:
array([ 5.,  8., 11.])
[29]:
a / b # Division
[29]:
array([0.25      , 0.33333334, 0.375     ])
[30]:
np.divide(a,b) # Division
[30]:
array([0.25      , 0.33333334, 0.375     ])
[31]:
a * b # Multiplication
[31]:
array([ 4., 12., 24.])
[32]:
np.multiply(a, b) # Multiplication
[32]:
array([ 4., 12., 24.])
[33]:
np.exp(b) # Exponentiation
[33]:
array([  54.59815,  403.4288 , 2980.958  ])
[34]:
np.sqrt(b) # Square root
[34]:
array([2.       , 2.4494898, 2.828427 ])
[35]:
np.sin(a) # Sines of an array
[35]:
array([0.84147096, 0.9092974 , 0.14112   ])
[36]:
np.cos(b) # Element-wise cosine
[36]:
array([-0.6536436 ,  0.96017027, -0.14550003])
[37]:
np.log(a) # Element-wise natural logarithm
[37]:
array([0.       , 0.6931472, 1.0986123])
[38]:
a.dot(b) # Dot product
[38]:
array(40.)

Comparison

Aggregate functions

[39]:
a.sum() # Array-wise sum
[39]:
array(6.)
[40]:
# a.min() # Array-wise minimum value
[41]:
c = np.array(([[1,2,3], [2,3,4]]))
# c.max(axis=0) # Maximum value of an array row
[42]:
# c.cumsum(axis=1) # Cumulative sum of the elements
[43]:
a.mean() # Mean
[43]:
array(2.)
[44]:
# b.median() # Median
[45]:
# a.corrcoef() # Correlation coefficient
[46]:
# np.std(b) # Standard deviation

Copying arrays

[47]:
# a.view() # Create a view of the array with the same data
[48]:
np.copy(a) # Create a copy of the array
[48]:
array([1., 2., 3.])
[49]:
a.copy() # Create a deep copy of the array
[49]:
array([1., 2., 3.])

Sorting Arrays

[50]:
# a.sort() # Sort an array
[51]:
# c.sort(axis=0) # Sort the elements of an array's axis

Subsetting, slicing, indexing

Subsetting

[52]:
a[2] # Select the element at the 2nd index 3
[52]:
array(3.)
[53]:
c[0,1] # Select the element at row 1 column 2
[53]:
array(2.)

Slicing

[54]:
a[0:2] # Select items at index 0 and 1
[54]:
array([1., 2.])
[55]:
c[0:2,1] # Select items at rows 0 and 1 in column 1
[55]:
array([2., 3.])
[56]:
c[:1] # Select all items at row 0
[56]:
array([[1., 2., 3.]])
[57]:
# c[1,...] # Same as [1,:,:]
[58]:
a[ : :-1] #Reversed array a array([3, 2, 1])
[58]:
array([3., 2., 1.])

Boolean Indexing

[59]:
# a[a<2] # Select elements from a less than 2

Fancy indexing

[60]:
c[[1,0,1,0], [0,1,2,0]] # Select elements (1,0),(0,1),(1,2) and (0,0)
[60]:
array([2., 2., 4., 1.])
[61]:
c[[1,0,1,0]][:,[0,1,2,0]] # Select a subset of the matrix’s rows
[61]:
array([[2., 3., 4., 2.],
       [1., 2., 3., 1.],
       [2., 3., 4., 2.],
       [1., 2., 3., 1.]])

Array manipulation

Transposing array

[62]:
np.transpose(c) # Permute array dimensions
[62]:
array([[1., 2.],
       [2., 3.],
       [3., 4.]])
[63]:
c.T # Permute array dimensions
[63]:
array([[1., 2.],
       [2., 3.],
       [3., 4.]])

Changing array shape

[64]:
# b.ravel() # Flatten the array
[65]:
# c.reshape(3,-2) # Reshape, but don’t change data

Adding and removing elements

[66]:
# c.resize((6,2)) # Return a new array with shape (6, 2)
[67]:
# np.append(h,g) # Append items to an array
[68]:
# np.insert(a, 1, 5) # Insert items in an array
[69]:
# np.delete(a, [1]) # Delete items from an array

Combining arrays

[70]:
np.concatenate((a,b),axis=0) # Concatenate arrays
[70]:
array([1., 2., 3., 4., 6., 8.])
[71]:
# np.vstack((a,b)) # Stack arrays vertically (row-wise)
[72]:
# np.r_[e,f] # Stack arrays vertically (row-wise)
[73]:
# np.hstack((e,f)) # Stack arrays horizontally (column-wise)
[74]:
# np.column_stack((a,d)) # Create stacked column-wise arrays
[75]:
# np.c_[a,d] # Create stacked column-wise arrays

Splitting arrays

[76]:
# np.hsplit(a,3) # Split the array horizontally at the 3rd index
[77]:
# np.vsplit(c,2) # Split the array vertically at the 2nd index

Use GPUs

Prerequisites: A GPU exists and GPU-enabled MXNet is installed.

[78]:
npx.num_gpus()  # Query number of GPUs
[78]:
1
[79]:
npx.gpu(0), npx.gpu(1)  # Context for the first and second GPUs
[79]:
(gpu(0), gpu(1))
[80]:
gpu_0 = npx.gpu(0) if npx.num_gpus() > 1 else npx.cpu()
g0 = np.zeros((2,3), device=gpu_0)  # Create array on GPU 0
g0
[80]:
array([[0., 0., 0.],
       [0., 0., 0.]])
[81]:
gpu_1 = npx.gpu(1) if npx.num_gpus() > 2 else npx.cpu()
g1 = np.random.uniform(size=(2,3), device=gpu_1)  # Create array on GPU 1
g1
[81]:
array([[0.6382473 , 0.795612  , 0.61245596],
       [0.07955429, 0.20390165, 0.67279226]])
[82]:
# Copy to another GPU
g1.copyto(gpu_0)
[82]:
array([[0.6382473 , 0.795612  , 0.61245596],
       [0.07955429, 0.20390165, 0.67279226]])
[83]:
# Return itself if matching the device, otherwise copy
g1.copyto(gpu_0), g1.copyto(gpu_0)
[83]:
(array([[0.6382473 , 0.795612  , 0.61245596],
        [0.07955429, 0.20390165, 0.67279226]]),
 array([[0.6382473 , 0.795612  , 0.61245596],
        [0.07955429, 0.20390165, 0.67279226]]))
[84]:
g1.device  # Query the device an array is on
[84]:
cpu(0)
[85]:
## The computation is performed by the devices on which the input arrays are
g0 + g1.copyto(gpu_0)
[85]:
array([[0.6382473 , 0.795612  , 0.61245596],
       [0.07955429, 0.20390165, 0.67279226]])

Auto differentiation

[86]:
a.attach_grad() # Allocate gradient for a variable
a.grad # access the gradient
[86]:
array([0., 0., 0.])

Compute the \(\nabla_a b=\exp(2a)^T a\)

[87]:
from mxnet import autograd

with autograd.record():
    b = np.exp(2*a).dot(a)
b.backward()
a.grad
[87]:
array([  22.167168,  272.99075 , 2824.0015  ])

Acknowledgement

Adapted from www.datacamp.com.