2020-07-14 14:28:17 -07:00
<!--- Licensed to the Apache Software Foundation (ASF) under one -->
<!--- or more contributor license agreements. See the NOTICE file -->
<!--- distributed with this work for additional information -->
<!--- regarding copyright ownership. The ASF licenses this file -->
<!--- to you under the Apache License, Version 2.0 (the -->
<!--- "License"); you may not use this file except in compliance -->
<!--- with the License. You may obtain a copy of the License at -->
<!--- http://www.apache.org/licenses/LICENSE-2.0 -->
<!--- Unless required by applicable law or agreed to in writing, -->
<!--- software distributed under the License is distributed on an -->
<!--- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -->
<!--- KIND, either express or implied. See the License for the -->
<!--- specific language governing permissions and limitations -->
<!--- under the License. -->
# The NP on MXNet cheat sheet
To begin, import the `np` and `npx` module and update MXNet to run in
NumPy-like mode.
2020-08-14 17:35:34 -07:00
```{.python .input}
2020-07-14 14:28:17 -07:00
from mxnet import np, npx
npx.set_np() # Change MXNet to the numpy-like mode.
` ``
NDArray figure (TODO)
## Creating arrays
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.array([1, 2, 3]) # default datatype is float32
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.array([(1.5, 2, 3), (4, 5, 6)], dtype='float16')
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.array([[(15,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype='int32')
` ``
### Initial placeholders
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.zeros((3, 4)) # Create an array of zeros
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.ones((2, 3, 4), dtype='int8') # Create an array of ones
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.arange(10, 25, 5) # Create an array of evenly spaced values (step value)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# Create an array of evenly spaced values (number of samples)
# np.linspace(0, 2, 9)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.full((2, 2), 7) # Create a constant array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.eye(2) # Create a 2X2 identity matrix
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.random.random((2, 2)) # Create an array with random values
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.empty((3,2)) # Create an empty array
` ``
## I/O
### Saving and loading on disk
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# Save one array
a = np.array([1, 2, 3])
npx.save('my_array', a)
npx.load('my_array')
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# 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')
` ``
### Saving and loading text files
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.loadtxt("myfile.txt")
# np.genfromtxt("my_file.csv", delimiter=',')
# np.savetxt("myarray.txt", a, delimiter=" ")
` ``
## Data types
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# 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
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a.shape # Array dimensions
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
len(a) # Length of array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
b.ndim # Number of array dimensions
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
b.size # Number of array elements
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
b.dtype # Data type of array elements
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# b.dtype.name # Name of data type
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
b.astype('int') # Convert an array to a different type
` ``
## Asking For Help
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.info(np.ndarray.dtype)
` ``
## Array mathematics
### Arithmetic operations
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a - b # Subtraction
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.subtract(a, b) # Subtraction
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
b + a # Addition
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.add(b, a) # Addition
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a / b # Division
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.divide(a,b) # Division
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a * b # Multiplication
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.multiply(a, b) # Multiplication
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.exp(b) # Exponentiation
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.sqrt(b) # Square root
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.sin(a) # Sines of an array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.cos(b) # Element-wise cosine
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.log(a) # Element-wise natural logarithm
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a.dot(b) # Dot product
` ``
### Comparison
### Aggregate functions
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a.sum() # Array-wise sum
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# a.min() # Array-wise minimum value
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c = np.array(([[1,2,3], [2,3,4]]))
# c.max(axis=0) # Maximum value of an array row
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# c.cumsum(axis=1) # Cumulative sum of the elements
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a.mean() # Mean
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# b.median() # Median
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# a.corrcoef() # Correlation coefficient
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.std(b) # Standard deviation
` ``
## Copying arrays
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# a.view() # Create a view of the array with the same data
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.copy(a) # Create a copy of the array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a.copy() # Create a deep copy of the array
` ``
## Sorting Arrays
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# a.sort() # Sort an array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# c.sort(axis=0) # Sort the elements of an array's axis
` ``
## Subsetting, slicing, indexing
### Subsetting
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a[2] # Select the element at the 2nd index 3
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c[0,1] # Select the element at row 1 column 2
` ``
### Slicing
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a[0:2] # Select items at index 0 and 1
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c[0:2,1] # Select items at rows 0 and 1 in column 1
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c[:1] # Select all items at row 0
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# c[1,...] # Same as [1,:,:]
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
a[ : :-1] #Reversed array a array([3, 2, 1])
` ``
### Boolean Indexing
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# a[a<2] # Select elements from a less than 2
` ``
### Fancy indexing
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c[[1,0,1,0], [0,1,2,0]] # Select elements (1,0),(0,1),(1,2) and (0,0)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c[[1,0,1,0]][:,[0,1,2,0]] # Select a subset of the matrix’ s rows
` ``
## Array manipulation
### Transposing array
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.transpose(c) # Permute array dimensions
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
c.T # Permute array dimensions
` ``
### Changing array shape
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# b.ravel() # Flatten the array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# c.reshape(3,-2) # Reshape, but don’ t change data
` ``
### Adding and removing elements
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# c.resize((6,2)) # Return a new array with shape (6, 2)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.append(h,g) # Append items to an array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.insert(a, 1, 5) # Insert items in an array
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.delete(a, [1]) # Delete items from an array
` ``
### Combining arrays
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
np.concatenate((a,b),axis=0) # Concatenate arrays
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.vstack((a,b)) # Stack arrays vertically (row-wise)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.r_[e,f] # Stack arrays vertically (row-wise)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.hstack((e,f)) # Stack arrays horizontally (column-wise)
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.column_stack((a,d)) # Create stacked column-wise arrays
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.c_[a,d] # Create stacked column-wise arrays
` ``
### Splitting arrays
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.hsplit(a,3) # Split the array horizontally at the 3rd index
` ``
2020-08-14 17:35:34 -07:00
` ``{.python .input}
2020-07-14 14:28:17 -07:00
# np.vsplit(c,2) # Split the array vertically at the 2nd index
` ``
## Use GPUs
Prerequisites: A GPU exists and GPU-enabled MXNet is installed.
` ``{.python .input}
npx.num_gpus() # Query number of GPUs
` ``
` ``{.python .input}
npx.gpu(0), npx.gpu(1) # Context for the first and second GPUs
` ``
` ``{.python .input}
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
` ``
` ``{.python .input}
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
` ``
` ``{.python .input}
# Copy to another GPU
g1.copyto(gpu_0)
` ``
` ``{.python .input}
# Return itself if matching the context, otherwise copy
g1.copyto(gpu_0), g1.copyto(gpu_0)
` ``
` ``{.python .input}
g1.context # Query the device an array is on
` ``
` ``{.python .input}
## The computation is performed by the devices on which the input arrays are
g0 + g1.copyto(gpu_0)
` ``
## Auto differentiation
` ``{.python .input}
a.attach_grad() # Allocate gradient for a variable
a.grad # access the gradient
` ``
Compute the $\nabla_a b=\exp(2a)^T a$
` ``{.python .input}
from mxnet import autograd
with autograd.record():
b = np.exp(2*a).dot(a)
b.backward()
a.grad
` ``
**Acknowledgement **
Adapted from www.datacamp.com.