| { |
| "cells": [ |
| { |
| "cell_type": "markdown", |
| "id": "0421a834", |
| "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", |
| "# Step 1: Manipulate data with NP on MXNet\n", |
| "\n", |
| "This getting started exercise introduces the MXNet `np` package for ndarrays.\n", |
| "These ndarrays extend the functionality of the common NumPy ndarrays, by adding\n", |
| "support for gpu's and by adding auto-differentiation with autograd. Now, many\n", |
| "NumPy methods are available within MXNet; therefore, we will only briefly cover\n", |
| "some of what is available.\n", |
| "\n", |
| "## Import packages and create an array\n", |
| "To get started, run the following commands to import the `np` package together\n", |
| "with the NumPy extensions package `npx`. Together, `np` with `npx` make up the\n", |
| "NP on MXNet front end." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "efe3f891", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "import mxnet as mx\n", |
| "from mxnet import np, npx\n", |
| "npx.set_np() # Activate NumPy-like mode." |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "beeca579", |
| "metadata": {}, |
| "source": [ |
| "In this step, create a 2D array (also called a matrix). The following code\n", |
| "example creates a matrix with values from two sets of numbers: 1, 2, 3 and 4, 5,\n", |
| "6. This might also be referred to as a tuple of a tuple of integers." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "2b626df0", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "np.array(((1, 2, 3), (5, 6, 7)))" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "fa39d96a", |
| "metadata": {}, |
| "source": [ |
| "You can also create a very simple matrix with the same shape (2 rows by 3\n", |
| "columns), but fill it with 1's." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "8a539d2e", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = np.full((2, 3), 1) \n", |
| "x" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "68018e6b", |
| "metadata": {}, |
| "source": [ |
| "Alternatively, you could use the following array creation routine." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "92f6657c", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = np.ones((2, 3)) \n", |
| "x" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "1a7913dd", |
| "metadata": {}, |
| "source": [ |
| "You can create arrays whose values are sampled randomly. For example, sampling\n", |
| "values uniformly between -1 and 1. The following code example creates the same\n", |
| "shape, but with random sampling." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "18647013", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y = np.random.uniform(-1, 1, (2, 3))\n", |
| "y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "ccc953e3", |
| "metadata": {}, |
| "source": [ |
| "As with NumPy, the dimensions of each ndarray are shown by accessing the\n", |
| "`.shape` attribute. As the following code example shows, you can also query for\n", |
| "`size`, which is equal to the product of the components of the shape. In\n", |
| "addition, `.dtype` tells the data type of the stored values. As you notice when\n", |
| "we generate random uniform values we generate `float32` not `float64` as normal\n", |
| "NumPy arrays." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "b4eb0237", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "(x.shape, x.size, x.dtype)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "8fe8d146", |
| "metadata": {}, |
| "source": [ |
| "You could also specifiy the datatype when you create your ndarray." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "b158b643", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = np.full((2, 3), 1, dtype=\"int8\") \n", |
| "x.dtype" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "4bf46ffe", |
| "metadata": {}, |
| "source": [ |
| "Versus the default of `float32`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "e4d6bd5c", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = np.full((2, 3), 1) \n", |
| "x.dtype" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "f320b83a", |
| "metadata": {}, |
| "source": [ |
| "When we multiply, by default we use the datatype with the most precision." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "ca1f21c9", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = x.astype(\"int8\") + x.astype(int) + x.astype(\"float32\")\n", |
| "x.dtype" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "b15fb879", |
| "metadata": {}, |
| "source": [ |
| "## Performing operations on an array\n", |
| "\n", |
| "A ndarray supports a large number of standard mathematical operations. Here are\n", |
| "some examples. You can perform element-wise multiplication by using the\n", |
| "following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "0526f122", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x * y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "c1f80e58", |
| "metadata": {}, |
| "source": [ |
| "You can perform exponentiation by using the following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "27f0611e", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "np.exp(y)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "eb7d576f", |
| "metadata": {}, |
| "source": [ |
| "You can also find a matrix’s transpose to compute a proper matrix-matrix product\n", |
| "by using the following code example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "c79d9cca", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "np.dot(x, y.T)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "a9330a6b", |
| "metadata": {}, |
| "source": [ |
| "Alternatively, you could use the matrix multiplication function." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "752c72df", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "np.matmul(x, y.T)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "3b1463c8", |
| "metadata": {}, |
| "source": [ |
| "You can leverage built in operators, like summation." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "c43bd713", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x.sum()" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "9e6319df", |
| "metadata": {}, |
| "source": [ |
| "You can also gather a mean value." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "a11a092b", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x.mean()" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "ba712a5a", |
| "metadata": {}, |
| "source": [ |
| "You can perform flatten and reshape just like you normally would in NumPy!" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "c46cecbf", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x.flatten()" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "551c9190", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x.reshape(6, 1)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "6fd5c1f8", |
| "metadata": {}, |
| "source": [ |
| "## Indexing an array\n", |
| "\n", |
| "The ndarrays support slicing in many ways you might want to access your data.\n", |
| "The following code example shows how to read a particular element, which returns\n", |
| "a 1D array with shape `(1,)`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "5723b720", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y[1, 2]" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "e425a51a", |
| "metadata": {}, |
| "source": [ |
| "This example shows how to read the second and third columns from `y`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "21116898", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y[:, 1:3]" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "3191d8bc", |
| "metadata": {}, |
| "source": [ |
| "This example shows how to write to a specific element." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "452c2961", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y[:, 1:3] = 2\n", |
| "y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "b7716ddd", |
| "metadata": {}, |
| "source": [ |
| "You can perform multi-dimensional slicing, which is shown in the following code\n", |
| "example." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "7289af41", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y[1:2, 0:2] = 4\n", |
| "y" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "0183eb02", |
| "metadata": {}, |
| "source": [ |
| "## Converting between MXNet ndarrays and NumPy arrays\n", |
| "\n", |
| "You can convert MXNet ndarrays to and from NumPy ndarrays, as shown in the\n", |
| "following example. The converted arrays do not share memory." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "577db810", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "a = x.asnumpy()\n", |
| "(type(a), a)" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "e83c10eb", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "a = np.array(a)\n", |
| "(type(a), a)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "66134911", |
| "metadata": {}, |
| "source": [ |
| "Additionally, you can move them to different GPU contexts. You will dive more\n", |
| "into this later, but here is an example for now." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "4b727483", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "a.copyto(mx.gpu(0))" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "62f69dd3", |
| "metadata": {}, |
| "source": [ |
| "## Next Steps\n", |
| "\n", |
| "Ndarrays also have some additional features which make Deep Learning possible\n", |
| "and efficient. Namely, differentiation, and being able to leverage GPU's.\n", |
| "Another important feature of ndarrays that we will discuss later is \n", |
| "autograd. But first, we will abstract an additional level and talk about building\n", |
| "Neural Network Layers [Step 2: Create a neural network](./2-create-nn.ipynb)" |
| ] |
| } |
| ], |
| "metadata": { |
| "language_info": { |
| "name": "python" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 5 |
| } |