| { |
| "cells": [ |
| { |
| "cell_type": "markdown", |
| "id": "4316ddf9", |
| "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", |
| "# An Intro: Manipulate Data the MXNet Way with NDArray\n", |
| "\n", |
| "## Overview\n", |
| "This guide\n", |
| "will introduce you to how data is handled with MXNet. You will learn the basics\n", |
| "about MXNet's multi-dimensional array format, `ndarray`.\n", |
| "\n", |
| "This content was extracted and simplified from the gluon tutorials in\n", |
| "[Dive Into Deep Learning](https://d2l.ai/).\n", |
| "\n", |
| "## Prerequisites\n", |
| "* [MXNet installed in a Python environment](https://mxnet.apache.org/get_started?version=master&platform=linux&language=python&environ=pip&processor=cpu).\n", |
| "* Python 2.7.x or Python 3.x\n", |
| "\n", |
| "\n", |
| "## Getting started\n", |
| "\n", |
| "In this chapter, we'll get\n", |
| "you going with the basic functionality. Don't worry if you don't understand any\n", |
| "of the basic math, like element-wise operations or normal distributions. In the\n", |
| "next two chapters we'll take another pass at `NDArray`, teaching you both the math\n", |
| "you'll need and how to realize it in code.\n", |
| "\n", |
| "To get started, let's import\n", |
| "`mxnet`. We'll also import `ndarray` from `mxnet` for convenience. We’ll make a\n", |
| "habit of setting a random seed so that you always get the same results that we\n", |
| "do." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "97778e55", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "import mxnet as mx\n", |
| "from mxnet import nd" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "9c085f1d", |
| "metadata": {}, |
| "source": [ |
| "Let's start with a very simple 1-dimensional array with a python list." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "6ffae285", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = nd.array([1,2,3])\n", |
| "print(x)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "c6ad26a2", |
| "metadata": {}, |
| "source": [ |
| "Now a 2-dimensional array." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "3c851d71", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y = nd.array([[1,2,3,4], [1,2,3,4], [1,2,3,4]])\n", |
| "print(y)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "db7f94b5", |
| "metadata": {}, |
| "source": [ |
| "Next, let's see how to create an `NDArray`, without any values initialized.\n", |
| "Specifically, we'll create a 2D array (also called a *matrix*) with 3 rows and 4\n", |
| "columns using the `.empty` function. We'll also try out `.full` which takes an\n", |
| "additional parameter for what value you want to fill in the array." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "bccf1839", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = nd.empty((3, 3))\n", |
| "print(x)\n", |
| "x = nd.full((3,3), 7)\n", |
| "print(x)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "55f8b56a", |
| "metadata": {}, |
| "source": [ |
| "`empty` just grabs some memory and hands us back a matrix without setting the\n", |
| "values of any of its entries. This means that the entries can have any form of\n", |
| "values, including very big ones! Typically, we'll want our matrices initialized\n", |
| "and very often we want a matrix of all zeros, so we can use the `.zeros`\n", |
| "function.\n", |
| "\n", |
| "<!-- showing something\n", |
| "different here (3,10) since the zeros may not produce anything different from\n", |
| "empty... or use the two demonstrations to show something interesting or\n", |
| "unique... when would I use one over the other?-->" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "746548dc", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = nd.zeros((3, 10))\n", |
| "print(x)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "e1c98f1b", |
| "metadata": {}, |
| "source": [ |
| "Similarly, `ndarray` has a function to create a matrix of all ones aptly named\n", |
| "[ones](../../../../api/legacy/ndarray/ndarray.rst#mxnet.ndarray.ones)." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "e86e7bda", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "x = nd.ones((3, 4))\n", |
| "print(x)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "ed6d58ee", |
| "metadata": {}, |
| "source": [ |
| "Often, we'll want to create arrays whose values are sampled randomly. This is\n", |
| "especially common when we intend to use the array as a parameter in a neural\n", |
| "network. In this snippet, we initialize with values drawn from a standard normal\n", |
| "distribution with zero mean and unit variance using\n", |
| "[random_normal](../../../../api/legacy/ndarray/ndarray.rst#mxnet.ndarray.random_normal).\n", |
| "\n", |
| "<!--\n", |
| "Is it that important to introduce zero mean and unit variance right now?\n", |
| "Describe more? Or how about explain which is which for the 0 and the 1 and what\n", |
| "they're going to do... if it actually matters at this point. -->" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "7a899c42", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y = nd.random_normal(0, 1, shape=(3, 4))\n", |
| "print(y)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "c7dc6469", |
| "metadata": {}, |
| "source": [ |
| "Sometimes you will want to copy an array by its shape but not its contents. You\n", |
| "can do this with `.zeros_like`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "4a8bff5e", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "z = nd.zeros_like(y)\n", |
| "print(z)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "509eab0a", |
| "metadata": {}, |
| "source": [ |
| "As in NumPy, the dimensions of each `NDArray` are accessible via the `.shape`\n", |
| "attribute." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "ca82bfc1", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y.shape" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "a52f924a", |
| "metadata": {}, |
| "source": [ |
| "We can also query its `.size`, which is equal to the product of the components\n", |
| "of the shape. Together with the precision of the stored values, this tells us\n", |
| "how much memory the array occupies.\n", |
| "<!-- is there a function for that or do you\n", |
| "just do it manually? Should we show that? -->" |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "4ef9e7c6", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y.size" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "bf69bc99", |
| "metadata": {}, |
| "source": [ |
| "We can query the data type using `.dtype`." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "4da4fa1f", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y.dtype" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "18bb7b02", |
| "metadata": {}, |
| "source": [ |
| "`float32` is the default data type. Performance can be improved with less\n", |
| "precision, or you might want to use a different data type. You can force the\n", |
| "data type when you create the array using a numpy type. This requires you to\n", |
| "import numpy first." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "4788d3a1", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "import numpy as np\n", |
| "a = nd.array([1,2,3])\n", |
| "b = nd.array([1,2,3], dtype=np.int32)\n", |
| "c = nd.array([1.2, 2.3], dtype=np.float16)\n", |
| "(a.dtype, b.dtype, c.dtype)" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "81b8f7d4", |
| "metadata": {}, |
| "source": [ |
| "As you will come to learn in detail later, operations and memory storage will\n", |
| "happen on specific devices that you can set. You can compute on CPU(s), GPU(s), a\n", |
| "specific GPU, or all of the above depending on your situation and preference.\n", |
| "Using `.context` reveals the location of the variable." |
| ] |
| }, |
| { |
| "cell_type": "code", |
| "execution_count": null, |
| "id": "63d66aa3", |
| "metadata": {}, |
| "outputs": [], |
| "source": [ |
| "y.context" |
| ] |
| }, |
| { |
| "cell_type": "markdown", |
| "id": "b5c206fa", |
| "metadata": {}, |
| "source": [ |
| "## Next Up\n", |
| "\n", |
| "[NDArray Operations](02-ndarray-operations.md)" |
| ] |
| } |
| ], |
| "metadata": { |
| "language_info": { |
| "name": "python" |
| } |
| }, |
| "nbformat": 4, |
| "nbformat_minor": 5 |
| } |