blob: 28e5d6ba641dc178d9ea5cca08ea28a6c49f6430 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"id": "ef1377ae",
"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,
"id": "3b5c3476",
"metadata": {},
"outputs": [],
"source": [
"from mxnet import np, npx\n",
"npx.set_np() # Change MXNet to the numpy-like mode."
]
},
{
"cell_type": "markdown",
"id": "852a4313",
"metadata": {},
"source": [
"NDArray figure (TODO)\n",
"\n",
"## Creating arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6ef26309",
"metadata": {},
"outputs": [],
"source": [
"np.array([1, 2, 3]) # default datatype is float32"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8b28cfe7",
"metadata": {},
"outputs": [],
"source": [
"np.array([(1.5, 2, 3), (4, 5, 6)], dtype='float16')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ffffd8a5",
"metadata": {},
"outputs": [],
"source": [
"np.array([[(15,2,3), (4,5,6)], [(3,2,1), (4,5,6)]], dtype='int32')"
]
},
{
"cell_type": "markdown",
"id": "e422b3d5",
"metadata": {},
"source": [
"### Initial placeholders"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d60a1bca",
"metadata": {},
"outputs": [],
"source": [
"np.zeros((3, 4)) # Create an array of zeros"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b52c50ef",
"metadata": {},
"outputs": [],
"source": [
"np.ones((2, 3, 4), dtype='int8') # Create an array of ones"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e36ff5c6",
"metadata": {},
"outputs": [],
"source": [
"np.arange(10, 25, 5) # Create an array of evenly spaced values (step value)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c668d355",
"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,
"id": "d090ed0e",
"metadata": {},
"outputs": [],
"source": [
"# np.full((2, 2), 7) # Create a constant array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "72ee52c1",
"metadata": {},
"outputs": [],
"source": [
"# np.eye(2) # Create a 2X2 identity matrix"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e09b7b68",
"metadata": {},
"outputs": [],
"source": [
"# np.random.random((2, 2)) # Create an array with random values"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2b053c63",
"metadata": {},
"outputs": [],
"source": [
"np.empty((3,2)) # Create an empty array"
]
},
{
"cell_type": "markdown",
"id": "2018e72f",
"metadata": {},
"source": [
"## I/O\n",
"\n",
"### Saving and loading on disk"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "63e27a05",
"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,
"id": "10ae3929",
"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",
"id": "94e9eb31",
"metadata": {},
"source": [
"### Saving and loading text files"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1bc34fcb",
"metadata": {},
"outputs": [],
"source": [
"# np.loadtxt(\"myfile.txt\")\n",
"# np.genfromtxt(\"my_file.csv\", delimiter=',')\n",
"# np.savetxt(\"myarray.txt\", a, delimiter=\" \")"
]
},
{
"cell_type": "markdown",
"id": "5f050880",
"metadata": {},
"source": [
"## Data types"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03a88549",
"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",
"id": "069c23d6",
"metadata": {},
"source": [
"## Inspecting your array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0cee3960",
"metadata": {},
"outputs": [],
"source": [
"a.shape # Array dimensions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af14c4a3",
"metadata": {},
"outputs": [],
"source": [
"len(a) # Length of array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3a328287",
"metadata": {},
"outputs": [],
"source": [
"b.ndim # Number of array dimensions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "704da9df",
"metadata": {},
"outputs": [],
"source": [
"b.size # Number of array elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ff9efd0f",
"metadata": {},
"outputs": [],
"source": [
"b.dtype # Data type of array elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09fd5e17",
"metadata": {},
"outputs": [],
"source": [
"# b.dtype.name # Name of data type"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a217951",
"metadata": {},
"outputs": [],
"source": [
"b.astype('int') # Convert an array to a different type"
]
},
{
"cell_type": "markdown",
"id": "8a782398",
"metadata": {},
"source": [
"## Asking For Help"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "442c8bb7",
"metadata": {},
"outputs": [],
"source": [
"# np.info(np.ndarray.dtype)"
]
},
{
"cell_type": "markdown",
"id": "c5e20fa5",
"metadata": {},
"source": [
"## Array mathematics\n",
"\n",
"### Arithmetic operations"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "82a231bd",
"metadata": {},
"outputs": [],
"source": [
"a - b # Subtraction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "809e0b80",
"metadata": {},
"outputs": [],
"source": [
"np.subtract(a, b) # Subtraction"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5640a9e6",
"metadata": {},
"outputs": [],
"source": [
"b + a # Addition"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ff22cf3",
"metadata": {},
"outputs": [],
"source": [
"np.add(b, a) # Addition"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "47f97772",
"metadata": {},
"outputs": [],
"source": [
"a / b # Division"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea6e415a",
"metadata": {},
"outputs": [],
"source": [
"np.divide(a,b) # Division"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7f4cc087",
"metadata": {},
"outputs": [],
"source": [
"a * b # Multiplication"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "27de3ef8",
"metadata": {},
"outputs": [],
"source": [
"np.multiply(a, b) # Multiplication"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e1b35c27",
"metadata": {},
"outputs": [],
"source": [
"np.exp(b) # Exponentiation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "270cbb28",
"metadata": {},
"outputs": [],
"source": [
"np.sqrt(b) # Square root"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b50cfdb",
"metadata": {},
"outputs": [],
"source": [
"np.sin(a) # Sines of an array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "df964edd",
"metadata": {},
"outputs": [],
"source": [
"np.cos(b) # Element-wise cosine"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44874f0b",
"metadata": {},
"outputs": [],
"source": [
"np.log(a) # Element-wise natural logarithm"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68fd1520",
"metadata": {},
"outputs": [],
"source": [
"a.dot(b) # Dot product"
]
},
{
"cell_type": "markdown",
"id": "44a11e77",
"metadata": {},
"source": [
"### Comparison\n",
"\n",
"### Aggregate functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "253b2cbb",
"metadata": {},
"outputs": [],
"source": [
"a.sum() # Array-wise sum"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e951bc0",
"metadata": {},
"outputs": [],
"source": [
"# a.min() # Array-wise minimum value"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c0926fd",
"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,
"id": "097eb88f",
"metadata": {},
"outputs": [],
"source": [
"# c.cumsum(axis=1) # Cumulative sum of the elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e234eb8d",
"metadata": {},
"outputs": [],
"source": [
"a.mean() # Mean"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ad8ea23",
"metadata": {},
"outputs": [],
"source": [
"# b.median() # Median"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "09406271",
"metadata": {},
"outputs": [],
"source": [
"# a.corrcoef() # Correlation coefficient"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9193b759",
"metadata": {},
"outputs": [],
"source": [
"# np.std(b) # Standard deviation"
]
},
{
"cell_type": "markdown",
"id": "b3dedf5e",
"metadata": {},
"source": [
"## Copying arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea24e2fc",
"metadata": {},
"outputs": [],
"source": [
"# a.view() # Create a view of the array with the same data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e083456b",
"metadata": {},
"outputs": [],
"source": [
"np.copy(a) # Create a copy of the array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c9cd060c",
"metadata": {},
"outputs": [],
"source": [
"a.copy() # Create a deep copy of the array"
]
},
{
"cell_type": "markdown",
"id": "54d7559b",
"metadata": {},
"source": [
"## Sorting Arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52232331",
"metadata": {},
"outputs": [],
"source": [
"# a.sort() # Sort an array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5fb3c51",
"metadata": {},
"outputs": [],
"source": [
"# c.sort(axis=0) # Sort the elements of an array's axis"
]
},
{
"cell_type": "markdown",
"id": "12220b2a",
"metadata": {},
"source": [
"## Subsetting, slicing, indexing\n",
"\n",
"### Subsetting"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ea50a95",
"metadata": {},
"outputs": [],
"source": [
"a[2] # Select the element at the 2nd index 3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6298ee58",
"metadata": {},
"outputs": [],
"source": [
"c[0,1] # Select the element at row 1 column 2"
]
},
{
"cell_type": "markdown",
"id": "49341fab",
"metadata": {},
"source": [
"### Slicing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "41cdac95",
"metadata": {},
"outputs": [],
"source": [
"a[0:2] # Select items at index 0 and 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9638001b",
"metadata": {},
"outputs": [],
"source": [
"c[0:2,1] # Select items at rows 0 and 1 in column 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e74627a4",
"metadata": {},
"outputs": [],
"source": [
"c[:1] # Select all items at row 0"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "22860979",
"metadata": {},
"outputs": [],
"source": [
"# c[1,...] # Same as [1,:,:]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5619810",
"metadata": {},
"outputs": [],
"source": [
"a[ : :-1] #Reversed array a array([3, 2, 1])"
]
},
{
"cell_type": "markdown",
"id": "6ca88094",
"metadata": {},
"source": [
"### Boolean Indexing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd268d0e",
"metadata": {},
"outputs": [],
"source": [
"# a[a<2] # Select elements from a less than 2"
]
},
{
"cell_type": "markdown",
"id": "3e7e7d57",
"metadata": {},
"source": [
"### Fancy indexing"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "07b919b5",
"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,
"id": "5ce3bbe8",
"metadata": {},
"outputs": [],
"source": [
"c[[1,0,1,0]][:,[0,1,2,0]] # Select a subset of the matrix’s rows"
]
},
{
"cell_type": "markdown",
"id": "7fe7b0ec",
"metadata": {},
"source": [
"## Array manipulation\n",
"\n",
"### Transposing array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9a4338b",
"metadata": {},
"outputs": [],
"source": [
"np.transpose(c) # Permute array dimensions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1d0496fb",
"metadata": {},
"outputs": [],
"source": [
"c.T # Permute array dimensions"
]
},
{
"cell_type": "markdown",
"id": "596ded21",
"metadata": {},
"source": [
"### Changing array shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2faf69cb",
"metadata": {},
"outputs": [],
"source": [
"# b.ravel() # Flatten the array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83e7b9ae",
"metadata": {},
"outputs": [],
"source": [
"# c.reshape(3,-2) # Reshape, but don’t change data"
]
},
{
"cell_type": "markdown",
"id": "d86860a2",
"metadata": {},
"source": [
"### Adding and removing elements"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "25c56b82",
"metadata": {},
"outputs": [],
"source": [
"# c.resize((6,2)) # Return a new array with shape (6, 2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "964f8bf1",
"metadata": {},
"outputs": [],
"source": [
"# np.append(h,g) # Append items to an array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "082d5108",
"metadata": {},
"outputs": [],
"source": [
"# np.insert(a, 1, 5) # Insert items in an array"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a24f8ebe",
"metadata": {},
"outputs": [],
"source": [
"# np.delete(a, [1]) # Delete items from an array"
]
},
{
"cell_type": "markdown",
"id": "1058aeb5",
"metadata": {},
"source": [
"### Combining arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31071a26",
"metadata": {},
"outputs": [],
"source": [
"np.concatenate((a,b),axis=0) # Concatenate arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9025cda",
"metadata": {},
"outputs": [],
"source": [
"# np.vstack((a,b)) # Stack arrays vertically (row-wise)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ac0081d",
"metadata": {},
"outputs": [],
"source": [
"# np.r_[e,f] # Stack arrays vertically (row-wise)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4dcdaeaa",
"metadata": {},
"outputs": [],
"source": [
"# np.hstack((e,f)) # Stack arrays horizontally (column-wise)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "679e46ae",
"metadata": {},
"outputs": [],
"source": [
"# np.column_stack((a,d)) # Create stacked column-wise arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "10a3f699",
"metadata": {},
"outputs": [],
"source": [
"# np.c_[a,d] # Create stacked column-wise arrays"
]
},
{
"cell_type": "markdown",
"id": "a04482e8",
"metadata": {},
"source": [
"### Splitting arrays"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d8137553",
"metadata": {},
"outputs": [],
"source": [
"# np.hsplit(a,3) # Split the array horizontally at the 3rd index"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "af518fb9",
"metadata": {},
"outputs": [],
"source": [
"# np.vsplit(c,2) # Split the array vertically at the 2nd index"
]
},
{
"cell_type": "markdown",
"id": "181b24d4",
"metadata": {},
"source": [
"## Use GPUs\n",
"\n",
"Prerequisites: A GPU exists and GPU-enabled MXNet is installed."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "291ebf87",
"metadata": {},
"outputs": [],
"source": [
"npx.num_gpus() # Query number of GPUs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05024af5",
"metadata": {},
"outputs": [],
"source": [
"npx.gpu(0), npx.gpu(1) # Context for the first and second GPUs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83eb8c89",
"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,
"id": "2ebd05b2",
"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,
"id": "80c12f03",
"metadata": {},
"outputs": [],
"source": [
"# Copy to another GPU\n",
"g1.copyto(gpu_0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "742c8166",
"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,
"id": "fbd1cbe7",
"metadata": {},
"outputs": [],
"source": [
"g1.context # Query the device an array is on"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "906e2668",
"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",
"id": "d4f45000",
"metadata": {},
"source": [
"## Auto differentiation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c2866673",
"metadata": {},
"outputs": [],
"source": [
"a.attach_grad() # Allocate gradient for a variable\n",
"a.grad # access the gradient"
]
},
{
"cell_type": "markdown",
"id": "01eefcdf",
"metadata": {},
"source": [
"Compute the $\\nabla_a b=\\exp(2a)^T a$"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7caf6bf",
"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",
"id": "b56d0c70",
"metadata": {},
"source": [
"**Acknowledgement**\n",
"\n",
"Adapted from www.datacamp.com."
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 5
}