Numpy from entry to proficiency - detailed broadcast mechanism

This column is called " Numpy from entry to proficiency ". As the name suggests, it is to record the learning process of learning numpy, and it is also convenient for you to review later! Lay the foundation for further study in deep learning! I hope it can help everyone, and I wish you a happy life if you love to sleep! This article introduces " Numpy from entry to mastery - broadcasting mechanism "

insert image description here

1. Comparison between np and math libraries

Students who have used python believe that they are not unfamiliar with math. I also mentioned the use of math library in my very early blog. For details, please refer to the link: Eight ways to find the approximate value of π ,

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :numpy学习 
@File    :task_11.py
@IDE     :PyCharm 
@Author  :咋
@Date    :2023/4/17 22:09 
"""
import time
import math
import numpy as np

x = [i * 0.001 for i in np.arange(1000000)]
start = time.time()
for i, t in enumerate(x):
    x[i] = math.sin(t)
print("math.sin:", time.time() - start)

x = [i * 0.001 for i in np.arange(1000000)]
x = np.array(x)
start = time.time()
np.sin(x)
print("numpy.sin:", time.time() - start)

Output:
insert image description here
You can see that np is 50 times faster than math.
Let's look at another example:

def basic_sigmoid(x):定义basich_sigmoid函数,需要一个参数x
    """
    Compute sigmoid of x.

    Arguments:
    x -- A scalar

    Return:
    s -- sigmoid(x)
    """

    ### START CODE HERE ### (≈ 1 line of code)
    s = 1.0 / (1.0 + 1.0 / math.exp(x))  math.exp(x) 相当于自然系数e的x次方
上一行写成公式就是s=1/(1+e^x )
    ### END CODE HERE ###

    return s返回s

basic_sigmoid(3) 调用上面定义的函数





x = np.linspace(-5,5,1000)-55生成1000个数
y = []   定义一个列表
for i in range(1000):  循环
    y.append(basic_sigmoid(x[i]))   调用上面的函数,传入一个数字,即可通过函数生成一个值,将生成值添加到列表上
    
plt.plot(x,y,c="r") 通过一系列(x,y),用plt将图画出来,

The drawn picture is shown in Figure 1:
insert image description here

### One reason why we use "numpy" instead of "math" in Deep Learning ###
x = [1, 2, 3]
basic_sigmoid(x) # you will see this give an error when you run it, because x is a vector.传入的是列表会报错,之前定义的函数需要出人的是具体的数值,所以传入一个列表会报错

import numpy as np 导入numpy模块

# example of np.exp
x = np.array([1, 2, 3])  创建一个numpy矩阵,值为1,2,3
print(np.exp(x)) # result is (exp(1), exp(2), exp(3))   将numpy矩阵传入进去,发现可以正常运行,结果为[ 2.71828183  7.3890561  20.08553692]



# example of vector operation
x = np.array([1, 2, 3])
print (x + 3)  numpy矩阵整体+1,结果为np.array([4 5 6])



# GRADED FUNCTION: sigmoid

import numpy as np # this means you can access numpy functions by writing np.function() instead of numpy.function()  给numpy取了个别名np,减少了代码量

def sigmoid(x):
    """
    Compute the sigmoid of x

    Arguments:
    x -- A scalar or numpy array of any size

    Return:
    s -- sigmoid(x)
    """
    
    ### START CODE HERE ### (≈ 1 line of code)
    s = 1.0 / (1.0 + np.exp(-1.0 * x))  和上面相同,np.exp也是e的x次方,写成公式为:
s=1/(1+e^(-x) )
    ### END CODE HERE ###
    
    return s返回s的值

x = np.array([1, 2, 3])  创建numpy矩阵
sigmoid(x) 直接调用定义好的函数,不再需要for循环
x = np.linspace(-15,15,1000) 同上,创建-15151000个数值
y = sigmoid(x) 将返回结果给y

plt.plot(x,y,c="r") plt绘图
plt.show() 展示图片

The picture is as follows:
insert image description here

Summary: Through this small case, we know that the numpy matrix can be directly calculated. This is why deep learning uses the numpy library instead of the math library. In deep learning, there are a large number of matrix-related operations. Using numpy can improve the speed of operation. And reduce the amount of code, numpy is a very useful scientific computing library!

2. Operate with a single element

Using numpy's broadcast mechanism, np matrices can be directly operated with a single element. Let's look directly at the code example:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :numpy学习 
@File    :task_12.py
@IDE     :PyCharm 
@Author  :咋
@Date    :2023/4/17 22:23 
"""
import numpy as np
A = np.array([[1, 2], [-1, 4]])
print("+运算:")
print(A+1)
print("-运算:")
print(A-1)
print("*运算:")
print(A*2)
print("除整数运算:")
print(A/2)
print("除小数运算:")
print(A/2.0)

output:

+运算:
[[2 3]
 [0 5]]
-运算:
[[ 0  1]
 [-2  3]]
*运算:
[[ 2  4]
 [-2  8]]
除整数运算:
[[ 0.5  1. ]
 [-0.5  2. ]]
除小数运算:
[[ 0.5  1. ]
 [-0.5  2. ]]

3. Comparison of loop and vector operations

Since numpy has the characteristics of broadcasting mechanism, ndarray can run directly without the help of for loop, and the speed is greatly improved, but it is worth noting that numpy does not support GPU operations:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project :numpy学习 
@File    :task_13.py
@IDE     :PyCharm 
@Author  :咋
@Date    :2023/4/17 22:32 
"""
import time
import numpy as np
x1 = np.random.rand(1000000)
x2 = np.random.rand(1000000)
##使用循环计算向量点积
tic = time.process_time()
dot = 0
for i in range(len(x1)):
    dot += x1[i] * x2[i]
toc = time.process_time()
print("dot = " + str(dot) + "\n for loop----- Computation time = " + str(1000 * (toc - tic)) + "ms")
##使用numpy函数求点积
tic = time.process_time()
dot = 0
dot = np.dot(x1, x2)
toc = time.process_time()
print("dot = " + str(dot) + "\n verctor version---- Computation time = " + str(1000 * (toc - tic)) + "ms")

output:

dot = 249929.02170318714
 for loop----- Computation time = 187.5ms
dot = 249929.0217031858
 verctor version---- Computation time = 15.625ms

The time of the for loop is 400 times that of vector operations, which proves once again that vectorized matrix operations are generally used in deep learning algorithms!
insert image description here

Guess you like

Origin blog.csdn.net/weixin_63866037/article/details/130205609