MXNET deep learning framework-01-ndarray most basic use (data structure)

Keywords: Python network Pycharm

The most basic use of ndarray in mxnet

Recently, I was learning the deep learning framework of mxnet. As the saying goes, "a good memory is better than a bad pen". Now, I will record the learning process and content so that I can review it at any time.

PS: on my laptop, I installed the mxnet GPU version, which is convenient for building the deep neural network. The language is python 3.6.7, and IDE is PyCharm.

First, you need to import the package of ndarray (ndarray is very similar to numpy):

from mxnet import ndarray as nd

1. Create matrix

mat = nd.zeros(shape=(3, 4))  # Create a matrix of all 0 (3 rows and 4 columns)
ones = nd.ones(shape=(5, 6))  # Create a matrix of all 1 (5 rows and 6 columns)
array = nd.array([[1, 2], [3, 4]])
print(mat)
print("==========")
print(ones)
print("==========")
print(array)

Operation result:

2. Create random array

random_array = nd.random_normal(0, 1, shape=(3, 4))  # Obey the normal distribution of mean 0 variance 1
print("Random array:",random_array)
print("Array dimension:",random_array.shape)  # View array dimensions, just like numpy
print("Array size:",random_array.size)   # View the number of array elements, row and column multiplication

Operation result:

3. Operator
Normal addition, subtraction, multiplication and division don't need to use darray in MXNET. It's a bit overqualified. Here, only index operation and point multiplication are recorded.
(1) Exponential operation:
Code generated random array:

ex_array=nd.exp(random_array)  # Index each element
print("Exponential operation-Array:",ex_array)

Operation result:

(2) point multiplication

print("Point multiplication:",nd.dot(random_array,random_array.T)) # Multiplication of (3,4) matrix and (4,3) matrix

Operation result:

4, broadcast
When the left and right dimensions of the binary operators are different, the system will try to copy them to a common shape.

a=nd.arange(3).reshape(shape=(3,1))
b=nd.arange(2).reshape(shape=(1,2))
print("a:",a)
print("b:",b)
print("a+b:",a+b)

Operation result:

It will expand "a" and "b" into a matrix of 3X2 so that they can be added.

5. Conversion to numpy

import numpy as np
x=np.ones(shape=(1,2))
y=nd.array(x) # numpy-->ndarray(mxnet)
z=y.asnumpy() # ndarray(mxnet)-->numpy
print(type(y),type(z))

Operation result:

6. Replace operation
Using python operations such as p=l+p will point y from the current instance to a new instance, which will open up a new memory and waste resources.

l=nd.ones(shape=(3,4))
p=nd.ones(shape=(3,4))
z=nd.elemwise_add(l,p) # addition
print(z)

Operation result:

Bottom line: ndarray is a method package in mxnet that deals with arrays (multidimensional), very similar to numpy. Other relevant API can be found in Official website Query.
All codes in this section:

from mxnet import ndarray as nd
'''----Create matrix---'''
mat = nd.zeros(shape=(3, 4))  # Create a matrix of all 0 (3 rows and 4 columns)
ones = nd.ones(shape=(5, 6))  # Create a matrix of all 1 (5 rows and 6 columns)
array = nd.array([[1, 2], [3, 4]])
print(mat)
print("==========")
print(ones)
print("==========")
print(array)
'''---Create random array-----'''
random_array = nd.random_normal(0, 1, shape=(3, 4))  # Obey the normal distribution of mean 0 variance 1
print("Random array:",random_array)
print("Array dimension:",random_array.shape)  # View array dimensions, just like numpy
print("Array size:",random_array.size)   # View the number of array elements, row and column multiplication

'''---Operator---'''
ex_array=nd.exp(random_array)  # Index each element
print("Exponential operation-Array:",ex_array)
print("Point multiplication:",nd.dot(random_array,random_array.T)) # Multiplication of (3,4) matrix and (4,3) matrix

'''---Radio broadcast---'''
# When the left and right dimensions of the binary operators are different, the system will try to copy them to a common shape.
a=nd.arange(3).reshape(shape=(3,1))
b=nd.arange(2).reshape(shape=(1,2))
print("a:",a)
print("b:",b)
print("a+b:",a+b)

'''---Follow numpy Transformation---'''
# ndarray in mxnet can be transformed into numpy
import numpy as np
x=np.ones(shape=(1,2))
y=nd.array(x) # numpy-->ndarray(mxnet)
z=y.asnumpy() # ndarray(mxnet)-->numpy
print(type(y),type(z))

'''---Replacement operation---(Do not open memory, save memory)'''
# Using python operations such as p=l+p will point y from the current instance to a new instance, which will open up a new memory and waste resources
l=nd.ones(shape=(3,4))
p=nd.ones(shape=(3,4))
z=nd.elemwise_add(l,p) # Add, creating a temporary space
print(z)
71 original articles published, 33 praised, 20000 visitors+
Private letter follow

Posted by xionhack on Sun, 15 Mar 2020 03:34:02 -0700