[Introduction to Deep Learning] PyTorch Deep Learning [Numpy Basics] (Part 1)


Target audience for this article: Students or practitioners who are interested
in machine learning and deep learning , and those who are interested in Python , PyTorch , TensorFlow , etc. and hope to improve their skills.

I. Introduction

  In the era of artificial intelligence, the core of artificial intelligence is deep learning . But there are currently many deep learning frameworks, such as TensorFlow , PyTorch , FastAI, etc., each of which has its own advantages and disadvantages.So what framework should I choose for a quick introduction to deep learning??
  If you are a novice , or like me, because of time constraints, you have no time to learn everything before you start , then I suggest choosing PyTorch . After having a certain foundation, we can learn some other architectures, such as TensorFlow , CNTK , etc.
   Why is PyTorch the first to be promoted?The reasons are as follows:
  (1) PyTorch needs to manually define key steps such as network layers and parameter updates , which is very helpful to help us quickly understand the core of deep learning . Although the Keras framework is also very simple and easy to use , butEncapsulation granularity is very coarseMany key steps are hidden, Often we have only a half-knowledge of the whole process after building the neural network.
  (2) PyTorch is a dynamic calculation graph , and its usage is closer to Python . Moreover, PyTorch shares many Numpy commands with Python , which can lower the learning threshold and is easier to use than TensorFlow .
  (3) PyTorch's dynamic graph mechanism is very convenient in debugging . If the calculation graph runs wrong, the problem can be tracked immediately . The debugging of PyTorch is the same as that of Python, and problems can be solved efficiently through breakpoint checking .
  There are other advantages such as many open source projects and plug-in adaptation , so I won't list them one by one.
  Review of the previous article : [Data Analysis Encyclopedia] Python-based Data Analysis Encyclopedia - Numpy Foundation
  Let's continue to explain the Numpy part of this article .


2. Overview of Numpy

  In machine learning and deep learning, input data such as images, sounds, and texts must eventually be converted intoarray or matrix. How to efficiently perform operations on arrays and matrices? This requires full use of Numpy .
   Why Numpy ? In fact, Python itself contains lists (list) and arrays (array), but for large data, these structures arethere are many deficienciesof . Since the elements of a list can be any object, what is stored in the list is a pointer to the object. For example, to save a simple [1,2,3], you need to have 3 pointers and 3 integer objects. For numerical operations, this structure is obviously a waste of precious resources such as memory and CPU .
  As for the array object , it can directly save the value, which is similar to the one-dimensional array of C language. But because it does not support multi-dimensional , there are not many functions on it, so it is not suitable for numerical operations .
  Numpy is the lingua franca of data science and is very closely related to PyTorch , which is the cornerstone of scientific computing and deep learning.


3. Generate Numpy array

  Numpy is an external library to Python , not in the standard library . Therefore, to use it, you need to import Numpy first .

import numpy as np

  After importing Numpy, you can use the np.+Tab key to view the available functions . If you are not very clear about the use of some of the functions, you can alsoIn the corresponding function +?, and then run, you can easily see the help information on how to use the function .
  Enter np. and press the Tab key , the following interface will appear:
Please add a picture description
  Run the following command to view the detailed help information of the function abs .

np.abs?

  Numpy is not only powerful, but also very friendly. The following will introduce some common methods of Numpy , especially some related to machine learning and deep learning .
  Numpy encapsulates a new data type ndarray (N-dimensional Array), which is a multidimensional array object . This object encapsulates many commonly used mathematical operation functions , which is convenient for us to do data processing, data analysis, etc.
  So,How to generate ndarray?
  Here are several ways to generate ndarray , such as creating from existing data , using random to create , creating a multidimensional array of a specific shape , using arange and linspace functions to generate, etc.


3.1 Create an array from existing data

  Directly convert Python's basic data types (such as lists, tuples, etc.) to generate ndarray:
  (1) Convert the list to ndarray :

import numpy as np
lstl =[3.14,2.17,0,1,2]
ndl = np.array(lst1)
print(ndl)
# [3.142.17 0. 1. 2. ]
print(type(ndl))
# <class 'nurmpy.ndarray'>

  (2) Nested lists can be converted into multidimensional ndarray :

import numpy as np
lst2 = [[3.14,2.1701,2],[1,23,4,5]]
nd2 = np.array(lst2)
print(nd2)
# [[3.14 2.17 0. 1. 2. ]
# [1. 2. 3. 4. 5. ]]
print(type(nd2))
# <class " numpy.ndarray' >

The same applies   if you replace the list in the example above with a tuple .


3.2 Using the random module to generate an array

  In deep learning, we often need to initialize some parameters , so in order to train the model more effectively, improve the performance of the model. Some initializations also need to meet certain conditions , such as satisfying normal distribution or uniform distribution .
  Several commonly used methods are introduced here, as shown in the following table, which lists the commonly used functions of the np.random module :

function describe
np.random.random Generate a random number between 0 and 1
np.random.uniform Generate uniformly distributed random numbers
np.random.randn Generate standard normal random numbers
np.random.randint generate random integers
np.random.normal Generate a normal distribution
np.random.shuffle Shuffle the order randomly
np.random.seed Set random number seed
random_sample Generate random floating point numbers

  Let's look at the specific use of some functions :

import numpy as np

nd3 =np.random.random([3,3])
print (nd3)
#[[0.43007219 0.87135582 0.45327073]
# [0.7929617 0.06584697 0.82896613]
# [0.62518386 0.70709239 0.75959122]]
print("nd3的形状为:",nd3.shape)
# nd3的形状为:(3,3)

  In order to generate the same data each time , you can specify a random seed and use the shuffle function to scramble the generated random numbers .

import numpy as np
np.random.seed(123)
nd4 = np.random.randn(2,3)
print(nd4)
np.random.shuffle(nd4)
print("随机打乱后数据:")print (nd4)
print(type(nd4))

  Output result:

[[-1.0856306 0.99734545  0.2829785]
 [-1.50629471 -0.57860025 1.65143654]]

  Data after random scrambling:

[[-1.50629471 -0.57860025 1.65143654]
 [-1.0856306 0.99734545 0.2829785]]

3.3 Create a multidimensional array of a specific shape

  When parameters are initialized, sometimes it is necessary to generate some special matrices , such as an array or matrix full of 0 or 1. At this time, we can use np.zeros , np.ones , and np.diag to achieve it, as shown in the following table:

function describe
np.zeros((3,4)) Create a 3×4 array with all 0 elements
np.ones((3,4)) Create a 3×4 array with all 1s
np.empty( (2,3)) Create a 2×3 empty array, the value in the empty data is not 0, but an uninitialized garbage value
np.zeros_like(ndarr) Create an array with all 0 elements in the same dimension as ndarr
np.ones_like(ndarr) Create an array with all 1 elements in the same dimension as ndarr
np.empty_like(ndarr) Create an empty array with the same dimensions as ndarr
np.eye(5) This function is used to create a 5×5 matrix with 1s on the diagonal and 0s on the rest
np.full((3,5),666) Create an array with 3×5 elements all 666, 666 is the specified value

  Here are a few examples to illustrate:

import numpy as np
# 生成全是0的3x3矩阵
nd5 = np.zeros([3,3])
#生成与nd5形状一样的全0矩阵
#np .zeros_like(nd5)
#生成全是1的3x3矩阵
nd6 = np. ones([3,3])
#生成3阶的单位矩阵
nd7 = np.eye(3)
#生成3阶对角矩阵
nd8 = np.diag([1,2,3])
print(nd5)
#[[O. 0. 0.]
# [0. 0. 0.]
# [0. 0. 0.]]
print(nd6)
#[[1. 1. 1.]
# [1. 1. 1.]
# [1. 1. 1.]]
print(nd7)
#[[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
print(nd8)
# [[1 0 0]
# [0 2 0]
# [0 0 3]]

  Sometimes it may also be necessary to temporarily save the generated data for subsequent use.

import numpy as np

nd9 =np.random.random([5,5])
np.savetxt(×=nd9, fname='./testl.txt')
nd10 = np.loadtxt('./test1.txt ')
print(ndl0)

  Output result:

[[0.41092437 0.5796943 0.13995076 0.40101756 0.62731701]
[0.32415089 0.24475928 0.69475518 0.5939024 0.63179202]
[0.44025718 0.08372648 0.71233018 0.42786349 0.2977805]
[0.49208478 0.74029639.0.35772892 0.41720995 0.65472131]
[0.37380143 0.23451288 0.98799529 0.76599595 0.77700444]]

3.4 Use arange and linspace functions to generate arrays

  arange is a function in the numpy module , and its format is:

arange([start,]stop[,step,],dtype=None)

  Among them, start and stop are used to specify the range, and step is used to set the step size . When generating an ndarray, start defaults to 0 , and the step size can be a decimal . Python has a built-in function range that does something similar.

import numpy as np

print(np.arange (10))
#[0 1 2 3 4 5 6 7 8 9]
print(np.arange(0,10))
#[0 1 2 3 4 5 6 7 8 9]
print(np.arange(1,4,0.5))
#[ 1. 1.5 2. 2.5 3. 3.5]
print(np.arange(9,-1,-1))
#[9 8 7 6 5 4 3 2 1 0]

  linspace is also a commonly used function in the numpy module , and its format is:

np.linspace(start,stop,num=50,endpoint=True,retstep=False,dtype=None)

  linspace can automatically generate a linear equalization vector according to the specified input data range and the number of equalizations .
  Among them, endpoint (including the endpoint) defaults to True, and the number of equal parts num defaults to 50 . If retstep is set to True, an ndarray with step size is returned .

import numpy as np
print (np .linspace(0,1,10))
#[0. 0.11111111 0.222222220.33333333 0.44444444 0.55555556 0.666666670.77777778 0.88888889 1. ]

  It is worth mentioning that,This does not generate ndarray with a step size of 0.1 such as 0.1, 0.2, ... 1.0 as we expected, this is because linspace must contain the start and end points of the data , so its step size is (1-0) /9 =0.11111111. If you need to generate data such as 0.1, 0.2..., 1.0, you only need to modify the data starting point 0 to 0.1. In addition to the arange and linspace
  introduced above , Numpy also provides the logspace function , which can be used in the same way as linspace . Readers may wish to try it out by themselves.


4. Get elements

  In the third section we learned several ways to generate ndarray . After the data is generated,How to read the data we need? Next, we will introduce several commonly used methods for obtaining data .

import numpy as np
np.random.seed(2019)
nd11 = np.random.random([10])
#获取指定位置的数据,获取第4个元素
ndl1[3]
#截取一段数据
ndl1[3:6]
#截取固定间隔数据
ndl1[1:6:2]
#倒序取数nd11[::-2]
#截取一个多维数组的一个区域内数据
ndl2=np.arange(25).reshape([5,5])
nd12[1:3,1:3]
#截取一个多维数组中,数值在一个值域之内的数据ndl2[(ndl2>3)&(ndl2<10)]
#截取多维数组中,指定的行,如读取第2,3行
nd12[[1,2]]        #或nd12[1:3,:]
#截取多维数组中,指定的列,如读取第2,3列
nd12[:,1:3]

  If you are not very clear about the acquisition methods above, it doesn’t matter, the following will further explain in the form of graphics, as shown in the figure below: the left side is the expression, and the right side
  is the element obtained by the expression . Notice,Different bounds mean different expressions. In addition to specifying the index label to obtain
insert image description here
  some elements in the array , it can also be achieved by using some functions , such as randomly extracting data from the specified sample through the random.choice function .

import numpy as np
from numpy import random as nr
a=np.arange(1,25,dtype=float)
cl=nr.choice(a,size=(3,4)) 
#size指定输出数组形状
c2=nr.choice(a,size=(3,4), replace=False) 
#replace缺省为True,即可重复抽取
#下式中参数p指定每个元素对应的抽取概率,缺省为每个元素被抽取的概率相同。
3=nr.choice(a,size=(3,4),p=a/np.sum(a))
print("随机可重复抽取")
print(c1)
print("随机但不重复抽取")
print(c2)
print("随机但按制度概率抽取")
print(c3)

  Print result:

# 随机可重复抽取
[[7. 22. 19. 21.]
[ 7. 5. 5. 5. ]
[7. 9.22. 12.]]
# 随机但不重复抽取
[[21. 9. 15. 4.]
[23. 2. 3. 7.]
[13. 5. 6. 1.]]
# 随机但按制度概率抽取
[[15. 19. 24. 8.]
[5. 22. 5. 14.]
22. 13. 17. 1]

Five, Numpy arithmetic operations

  In machine learning and deep learning, a large number of array or matrix operations are involved . In this section we will focus onTwo commonly used operations. One is to multiply corresponding elements , also known as element-wise multiplication (Element-Wise Product), the operator isnp.multiply(), or *. The other is dot product or inner product element -wise, the operator isnp.dot( )


5.1 Multiplication of corresponding elements

  Element-Wise Product is the product of corresponding elements in two matrices . The np.multiply function is used to multiply the corresponding elements of an array or matrix, and the output is consistent with the size of the multiplied array or matrix . Its format is as follows:

numpy.multiply(x1,,x2,/,out=None,*,where=True,casting='same_kind',order='K',dtype-None,subok=True[,signature,extobj])

  Among them, the multiplication of the corresponding elements between x1 and x2 obeys the broadcasting rules. Numpy’s broadcasting rules will be introduced in the next article - Numpy Basics (Part 2). Below we further illustrate with some examples.

A=np.array([[l,2][-l,4]])
B=np.array([[2,0],[3,4]])
A*B
# 结果如下:
array([[2,0],[-316]])
# 或另一种表示方法np.multiply(A,B)#运算结果也是
array([[2,0],[-3,16]])

  The corresponding elements of matrices A and B are multiplied, which is visually represented by the following figure.
insert image description here
  Numpy arrays can not only multiply corresponding elements with arrays, but also operate with single values ​​(or called scalars) . During the operation, each element in the Numpy array is operated with a scalar , during which thebroadcast mechanism(will be explained in detail in the next article).

print (A*2.0)print(A/2.0)

  The output is:

[ [2. 4.]
[-2. 8.]]
[[0.5 1]
[-0.5 2. ]]

  Therefore, after generalization, after the array passes through some activation functions, the output is consistent with the input shape .

X=np.random.rand(2,3)
def softmoid(x):
	return 1/(1+np.exp(-x))
def relu(x):
	return np.maximum(0,x)
def softmax(x) :
	return np.exp(x)/np.sum(np.exp(X))
	
print("输入参数x的形状:",X.shape)
print("激活函数softmoid输出形状:",softmoid(x) .shape)
print("激活函数relu输出形状:", relu(X).shape)
print("激活函数softmax输出形状:",softmax(X).shape)

  Output result:

输入参数x的形状:(23)
激活函数softmoid输出形状:(2,3)
激活函数relu输出形状:(2,3)
激活函数softmax输出形状:(23)

5.2 Dot product operation

  Dot Product (Dot Product), also known as inner product , is used in Numpynp.dotIndicates that its general format is:

numpy.dot(a,b,out=None)

  The following uses an example to illustrate the specific usage and precautions of dot.

X1=np.array([[1,2],[3,4]])
x2=np.array([[56,7],[8,9,10]])
x3=np.dot(X1,X2)
print(X3)

  Output result:

[[21 2427]
 [47 5461]]

  The above operation can be expressed in the figure below.
insert image description here
  In the figure above, matrix X1 and matrix X2 perform dot product operations , where the number of elements in the corresponding dimensions of X1 and X2 (that is, the second dimension of X1 and the first dimension of X2) must be consistent . In addition, the shape of the matrix X3 is constituted by the number of rows of the matrix X1 and the number of columns of the matrix X2 .


6. Postscript

  This article mainly explains the overview of Numpy, generating Numpy arrays, getting elements and Numpy's arithmetic operations . Because there is too much content to write, it will be divided into two parts. In the past few days, the next part of the Numpy foundation will also be released.
  Thank you readers and friends for your long-term support! Thank you so much! ! !

Guess you like

Origin blog.csdn.net/m0_65748531/article/details/132135598