The NP on MXNet cheat sheet

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

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

NDArray figure (TODO)

Creating arrays

np.array([1, 2, 3])  # default datatype is float32
array([1., 2., 3.])
np.array([(1.5, 2, 3), (4, 5, 6)], dtype='float16')
array([[1.5, 2. , 3. ],
       [4. , 5. , 6. ]], dtype=float16)
np.array([[(15,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype='int32')
array([[[15,  2,  3],
        [ 4,  5,  6]],

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

Initial placeholders

np.zeros((3, 4))  # Create an array of zeros
array([[0., 0., 0., 0.],
       [0., 0., 0., 0.],
       [0., 0., 0., 0.]])
np.ones((2, 3, 4), dtype='int8')  # Create an array of ones
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)
np.arange(10, 25, 5)  # Create an array of evenly spaced values (step value)
array([10., 15., 20.])
# Create an array of evenly spaced values (number of samples)
# np.linspace(0, 2, 9)
# np.full((2, 2), 7)  # Create a constant array
# np.eye(2)  # Create a 2X2 identity matrix
# np.random.random((2, 2))  # Create an array with random values
np.empty((3,2))  # Create an empty array
array([[1.05535857e-22, 4.56627118e-41],
       [1.70921326e+01, 3.07220675e-41],
       [4.48415509e-44, 0.00000000e+00]])

I/O

Saving and loading on disk

# Save one array
a = np.array([1, 2, 3])
npx.save('my_array', a)
npx.load('my_array')
[array([1., 2., 3.])]
# Save a list of arrays
b = np.array([4, 6, 8])
npx.save('my_arrays', [a, b])  # FIXME, cannot be a tuple
npx.load('my_arrays')
[array([1., 2., 3.]), array([4., 6., 8.])]

Saving and loading text files

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

Data types

# 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

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

Asking For Help

# np.info(np.ndarray.dtype)

Array mathematics

Arithmetic operations

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

Comparison

Aggregate functions

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

Copying arrays

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

Sorting Arrays

# a.sort() # Sort an array
# c.sort(axis=0) # Sort the elements of an array's axis

Subsetting, slicing, indexing

Subsetting

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

Slicing

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

Boolean Indexing

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

Fancy indexing

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

Array manipulation

Transposing array

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

Changing array shape

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

Adding and removing elements

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

Combining arrays

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

Splitting arrays

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

Use GPUs

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

npx.num_gpus()  # Query number of GPUs
4
npx.gpu(0), npx.gpu(1)  # Context for the first and second GPUs
(gpu(0), gpu(1))
gpu_0 = npx.gpu(0) if npx.num_gpus() > 1 else npx.cpu()
g0 = np.zeros((2,3), ctx=gpu_0)  # Create array on GPU 0
g0
array([[0., 0., 0.],
       [0., 0., 0.]], ctx=gpu(0))
gpu_1 = npx.gpu(1) if npx.num_gpus() > 2 else npx.cpu()
g1 = np.random.uniform(size=(2,3), ctx=gpu_1)  # Create array on GPU 1
g1
array([[0.67478997, 0.07540122, 0.9956977 ],
       [0.09488854, 0.415456  , 0.11231736]], ctx=gpu(1))
# Copy to another GPU
g1.copyto(gpu_0)
array([[0.67478997, 0.07540122, 0.9956977 ],
       [0.09488854, 0.415456  , 0.11231736]], ctx=gpu(0))
# Return itself if matching the context, otherwise copy
g1.copyto(gpu_0), g1.copyto(gpu_0)
(array([[0.67478997, 0.07540122, 0.9956977 ],
        [0.09488854, 0.415456  , 0.11231736]], ctx=gpu(0)),
 array([[0.67478997, 0.07540122, 0.9956977 ],
        [0.09488854, 0.415456  , 0.11231736]], ctx=gpu(0)))
g1.context  # Query the device an array is on
gpu(1)
## The computation is performed by the devices on which the input arrays are
g0 + g1.copyto(gpu_0)
array([[0.67478997, 0.07540122, 0.9956977 ],
       [0.09488854, 0.415456  , 0.11231736]], ctx=gpu(0))

Auto differentiation

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

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

from mxnet import autograd

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

Acknowledgement

Adapted from www.datacamp.com.