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