深度学习之Numpy整理

栏目: Python · 发布时间: 6年前

内容简介:因为Numpy对向量计算做了优化,用到了CPU或GPU的并行计算,所以速度要比单纯的for循环要快。具体细节感兴趣可以自行研究。

1、Numpy介绍

NumpyPython 的一个扩展包,语法和Matlab有很多相似之处。它支持高维数组和矩阵运算,也提供了许多数组和矩阵运算的函数。另外,它在数组和矩阵运算方面速度很快,效率很高。

2、为什么要用Numpy

Numpy向量化计算非向量化计算 性能比较

# coding: utf-8
import time
import numpy as np


# Numpy向量化测试
a = np.random.rand(1000000)
b = np.random.rand(1000000)

tic = time.time()
c = np.dot(a,b)
toc = time.time()
print("计算结果"+str(c))
print("向量化使用时间:"+str(1000*(toc-tic)) + "ms")


# 使用for循环测试
c = 0
tic = time.time()

for i in range(1000000):
    c += a[i] * b[i]

toc = time.time()
print("计算结果"+str(c))
print("循环使用时间:"+str(1000*(toc-tic)) + "ms")

#计算结果250011.92519533934
#向量化使用时间:0.9119510650634766ms
#计算结果250011.92519534056
#循环使用时间:466.2208557128906ms

为什么向量化比循环快?

因为Numpy对向量计算做了优化,用到了CPU或GPU的并行计算,所以速度要比单纯的for循环要快。具体细节感兴趣可以自行研究。

二、数组创建

1、生成数组

(1)一维数组

import numpy as np
x = np.array([1.0,2.0,3.0])
print(x)
# [1. 2. 3.]

x = np.np.arange(0,5)
print(x)
# [0 1 2 3 4]

各个元素与标量之间进行运算,称为广播。

(2)二维数组

A = np.array([[1,2],[3,4]])
A.shape 
# 返回n*m的数组

A.dtype 
# 返回数据类型

A.size 
# 返回数组元素个数4

np.ndim(A) 
#返回数组维数

(3)创建shape相同的zero数组

Return an array of zeros with the same shape and type as a given array.
>>>x = np.arange(6)
>>>x = x.reshape((2, 3))
>>>x
array([[0, 1, 2],
       [3, 4, 5]])
>>>np.zeros_like(x)
array([[0, 0, 0],
       [0, 0, 0]])

3、访问元素

for row in A:
  print(row)

A[0]
A[0][1]

A.flatten() #将X转换为一维数组
# [1 2 3 4]

A[np.array([0,1])]#获取索引为1、2的元素
#[[1 2]
#[3 4]]

A > 15 #获得布尔型数组
#[[False False]
# [False False]]

三、矩阵

1、创建矩阵

A = np.matrix('1,2;3,4')
print(A)
print(type((A)))
#[[1 2]
# [3 4]]
#<class 'numpy.matrixlib.defmatrix.matrix'>

2、数组转矩阵

A = np.array([[1,2],[3,4]])
#[[1 2]
# [3 4]]
B = np.mat(A)
print(B)
print(type(B))
#<class 'numpy.matrixlib.defmatrix.matrix'>

3、创建单位矩阵

E = np.eye(3)
#[[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]

4、改变数组形状

# 将2*2的数组变形为4*1的数组
A = np.array([[1,2],[3,4]])
A = A.reshape((A.size,1))
print(A)

#[[1]
 #[2]
 #[3]
 #[4]]

四、计算

1、广播

为何叫广播,因为单个标量被广播、复制成n维数组。

如二维数组 [[1,2],[3,4]] 与标量10相乘,等同于 [[1,2],[3,4]] 乘以 [[10,10],[10,10]]

2、矩阵转置

因为 np.random.randn(5) 写法不直观,建议使用写法 np.random.randn(5,1)

a = np.random.randn(5,1)

#转置
b = a.T
print(a)

#[[-0.30232915]
# [-0.13689176]
# [ 0.74737671]
# [ 0.58641912]
# [ 0.14419141]]

print(a.T)

#[[-0.30232915 -0.13689176 0.74737671 0.58641912 0.14419141]]

3、矩阵乘法(点积)

(1)

import numpy as np
A = np.array([[1,2],[3,4]])
B = np.array([[5,6],[7,8]])
#点积
print(np.dot(A,B))
#array([[19, 22],[43, 50]])

(2)

a = np.random.randn(5,1)
b = a.T
# (5,1)(1,5)的点积得到的是5*5的矩阵
print(np.dot(a,b))
# (1,5)(5,1)的点积得到的是1*1的矩阵
print(np.dot(b,a))

4、矩阵求逆

$AB=E$,矩阵A与B互为 逆矩阵 ,其中E为单位矩阵,E的 行列式计算 为1

A = np.matrix('1,2;3,4')
print(A.I)#求逆
#[[-2. 1. ]
# [ 1.5 -0.5]]

E = np.dot(A,A.I)
print(E)
#[[1.0000000e+00 0.0000000e+00]
#[8.8817842e-16 1.0000000e+00]]

value = np.linalg.det(E)#行列式计算
#0.9999999999999996

5、算术运算

x = np.array([1.0,2.0,3.0])
y = np.array([2.0,3.0,4.0])

#减法
print(x - y) 
# [-1. -1. -1.]

#加法
print(x + y)
#[3. 5. 7.]

#乘法
print(x * y)
#[ 2. 6. 12.]

#除法
print(x / y)
#[0.5 0.66666667 0.75 ]

#开方根
print(np.sqrt(x))
#[1. 1.41421356 1.73205081]

#对数
print(np.log(x))
#[0. 0.69314718 1.09861229]

# 指数
print(np.exp(a))
# [ 2.71828183 7.3890561 20.08553692]

6、平均值、方差、标准差

x = np.array([1,2,3,4,5,6])

# 平均值
np.mean([1,2,3,4])
# 3.5

# 方差
print(np.var(x))
#2.9166666666666665

# 标准差
print(np.std(x))
#1.707825127659933

7、转为二进制

numpy.unpackbits

Each element of myarray represents a bit-field that should be unpacked into a binary-valued output array. The shape of the output array is either 1-D (if axis is None) or the same shape as the input array with unpacking done along the axis specified.
>>>a = np.array([[2], [7], [23]], dtype=np.uint8)
>>>a
array([[ 2],
       [ 7],
       [23]], dtype=uint8)
>>>b = np.unpackbits(a, axis=1)
>>>b
array([[0, 0, 0, 0, 0, 0, 1, 0],
       [0, 0, 0, 0, 0, 1, 1, 1],
       [0, 0, 0, 1, 0, 1, 1, 1]], dtype=uint8)

五、应用

1、sigmoid函数

def sigmoid(x):

    s = 1.0 / (1.0 + np.exp(-x))
    return s

2、sigmoid求导

深度学习之Numpy整理
def sigmoid_derivative(x):
    s = sigmoid(x)
    ds = s*(1-s)
    return ds

3、softmax函数计算

深度学习之Numpy整理
def softmax(x):

    x_exp = np.exp(x)
    x_sum = np.sum(np.exp(x),axis=1,keepdims = True)
    s = x_exp / x_sum
    return s

4、计算损失函数

深度学习之Numpy整理

def L1(yhat, y):
    loss = np.sum(np.abs(yhat - y))
    return loss

深度学习之Numpy整理

def L1(yhat, y):
    loss = np.sum((y - yhat)**2)
    return loss

5、向量化练习

x1 = [9, 2, 5, 0, 0, 7, 5, 0, 0, 0, 9, 2, 5, 0, 0]
x2 = [9, 2, 2, 9, 0, 9, 2, 5, 0, 0, 9, 2, 5, 0, 0]

### VECTORIZED DOT PRODUCT OF VECTORS ###
tic = time.process_time()
dot = np.dot(x1,x2)
toc = time.process_time()
print ("dot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### VECTORIZED OUTER PRODUCT ###
tic = time.process_time()
outer = np.outer(x1,x2)
toc = time.process_time()
print ("outer = " + str(outer) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### VECTORIZED ELEMENTWISE MULTIPLICATION ###
tic = time.process_time()
mul = np.multiply(x1,x2)
toc = time.process_time()
print ("elementwise multiplication = " + str(mul) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

### VECTORIZED GENERAL DOT PRODUCT ###
tic = time.process_time()
dot = np.dot(W,x1)
toc = time.process_time()
print ("gdot = " + str(dot) + "\n ----- Computation time = " + str(1000*(toc - tic)) + "ms")

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Learning PHP, MySQL, JavaScript, and CSS

Learning PHP, MySQL, JavaScript, and CSS

Robin Nixon / O'Reilly Media / 2012-9-3 / USD 39.99

If you're familiar with HTML, you can quickly learn how to build interactive, data-driven websites with the powerful combination of PHP, MySQL, and JavaScript - the top technologies for creating moder......一起来看看 《Learning PHP, MySQL, JavaScript, and CSS》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具