深度学习的12个矩阵运算
深度学习的12个矩阵运算
在深度学习中,矩阵运算是核心基础。本文详细介绍了12种重要的矩阵运算方法,包括矩阵的基本概念、加减法、形状和大小、稀疏矩阵转换、转置、统计量计算、跟踪、元素操作、乘法、逆矩阵以及矩阵重塑等。通过具体的Python代码示例,帮助读者深入理解这些矩阵运算在深度学习中的应用。
并不是说向量和矩阵是执行这些操作的唯一方法,但如果你这样做,它们就会变得非常高效。深度学习背后的核心数据结构包括:
- 标量
- 向量
- 矩阵和
- 张量。
矩阵运算用于许多深度学习算法的描述。
因此,如果您真的想成为深度学习领域的专业人士,那么您就无法逃避掌握其中的一些概念。因此,在本文中,我们将讨论用于描述深度学习方法的重要线性代数矩阵运算。
什么是矩阵?
矩阵是由数字组成的矩形数组,可以看作是2个nd阶张量。如果m和n是正整数,即m、n∈N,则m×n矩阵包含m*n个元素,其中m个行数和n个列数。
m×n矩阵的图形表示如下所示:
有时,我们使用以下矩阵的缩写来代替完整的矩阵组件:
例如:
在这个例子中,在numpy库的帮助下,我们将创建一个矩阵。并检查形成的矩阵的维度。
import numpy as np
matrix = np.array([[45,34],[67,58]])
# Create a matrix
print("The original matrix is given by n", matrix)
# Check the dimension of the matrix
print("The dimension of the given matrix is", matrix.ndim)
矩阵加法和减法
在本节中,我们将使用add和subtract方法进行矩阵加法和减法。这些方法采用两个参数,并分别返回这些矩阵的和值和差值。如果矩阵的形状不同,则会引发一个错误,指出无法进行加法或减法。
matrix_1 = np.array([[45,34],[67,58]])
matrix_2 = np.array([[35,24],[57,48]])
# Add the two matrices
print("The result after adding matrix 1 and matrix 2 is given by n" , np.add(matrix_1, matrix_2))
# Subtract one matrix from the other matrices
print("The result after subtracting matrix 1 from matrix 2 is given by n" , np.subtract(matrix_1, matrix_2))
print("The result after subtracting matrix 2 from matrix 1 is given by n" , np.subtract(matrix_2, matrix_1))
输出:
The result after adding matrix 1 and matrix 2 is given by
[[ 80 58]
[124 106]]
The result after subtracting matrix 1 from matrix 2 is given by
[[10 10]
[10 10]]
The result after subtracting matrix 2 from matrix 1 is given by
[[-10 -10]
[-10 -10]]
矩阵的形状和大小
在本节中,我们将找到形状,即给定矩阵中的行数和列数,以及大小,即给定矩阵的矩阵中的元素数。
matrix = np.array([[45,34,75],[67,58,89]])
# Finding number of rows and columns in the matrix
print("The number of rows and columns in the given matrix are " + str(matrix.shape[0]) + " and " + str(matrix.shape[1]) + " respectively")
# Number of elements in the matrix
print("The size of the given matrix is" , matrix.size)
输出:
The number of rows and columns in the given matrix are 2 and 3 respectively
The size of the given matrix is 6
将给定的Dense矩阵转换为Sparse矩阵
让我们首先了解Sparse和Dense Matrix的确切含义。
稀疏矩阵是主要由零值组成的矩阵。稀疏矩阵不同于大部分非零值的矩阵,后者被称为密集矩阵。
from scipy import sparse
# Create a Dense Matrix
dense_matrix = np.array([[0,0],[0,17],[78,0]])
# Convert Dense matrix to Sparse matrix
sparse_matrix = sparse.csr_matrix(dense_matrix)
print("The sparse matrix corresponding to a given dense matrix is given by n" , sparse_matrix)
输出:
The sparse matrix corresponding to a given dense matrix is given by
(1, 1) 17
(2, 0) 78
矩阵转置
在Matrix Transpose中,我们可以将行向量转换为列向量,反之亦然,即row变成columns,columns变成rows。
如果我们有矩阵A=[aij]MXN,则此矩阵的转置为AT=[aij]n×m
import numpy as np
matrix = np.array([[45,34],[67,58]])
print("The original matrix is given by n" , matrix)
print("The transpose matrix of the given matrix is n" , matrix.T)
输出:
The original matrix is given by
[[45 34]
[67 58]]
The transpose matrix of the given matrix is
[[45 67]
[34 58]]
矩阵的均值、方差和标准差
在本节中,我们将尝试找到一些与矩阵相关的统计内容。在这里,我们使用numpy函数计算矩阵的平均值、方差和标准差。
import numpy as np
matrix = np.array([[45,34],[67,58], [23,89]])
# Finding the mean of a matrix elements
print("The mean of the elements of a matrix is equal to", np.mean(matrix))
# Finding the Variance of a matrix elements
print("The variance of the elements of a matrix is equal to", np.var(matrix))
# Finding the Standard Deviation of a matrix elements
print("The standard deviation of the elements of a matrix is equal to", np.std(matrix))
print("The standard deviation of the elements of a matrix is equal to", np.sqrt(np.var(matrix)))
输出:
The mean of the elements of a matrix is equal to 52.666666666666664
The variance of the elements of a matrix is equal to 473.5555555555555
The standard deviation of the elements of a matrix is equal to 21.761331658599286
The standard deviation of the elements of a matrix is equal to 21.761331658599286
矩阵的跟踪
在本节中,我们将尝试找到矩阵的轨迹,即给定矩阵中存在的所有对角线元素的总和。
import numpy as np
matrix = np.array([[1,2,3],[4,5,6], [7,8,9]])
# Get the diagonal elements of a matrix
print("The diagonal elements of a given matrix are n", matrix.diagonal())
# Finding the trace of the matrix
print("The trace of a given matrix is equal to", matrix.diagonal().sum())
输出:
The diagonal elements of a given matrix are
[1 5 9]
The trace of a given matrix is equal to 15
从Matrix中查找最小和最大元素
在本节中,我们将尝试找到矩阵的最小和最大元素,即所有元素中具有最高和最低值的元素。
import numpy as np
matrix = np.array([[1,2,3],[4,5,6], [7,8,9]])
# Find the minimum element of the matrix
print("The minimum element in a given matrix is", np.min(matrix))
# Find the maximum element of the matrix
print("The maximum element in a given matrix is", np.max(matrix))
输出:
The minimum element in a given matrix is 1
The maximum element in a given matrix is 9
矩阵的行列式
在本节中,我们将尝试找到矩阵的行列式。在这里,为了计算行列式,我们使用Numpy包中的线性代数模块。
import numpy as np
matrix = np.array([[1,2,4],[3,4,6], [7,8,5]])
# Find the determinant of the matrix
print("The determinant of the given matrix is equal to", np.linalg.det(matrix))
输出:
The determinant of the given matrix is equal to 9.999999999999993
矩阵乘法
形状为(m x n)的矩阵和形状为(n x p)的B矩阵相乘,得到形状为(m x p)的C。请记住,在对矩阵进行乘法时,第一个矩阵中的列数与第二个矩阵中的行数相同,以便无误地执行乘法。
在本节中,我们将尝试求两个矩阵的乘法。
import numpy as np
matrix_1 = np.array([[45,34],[67,58]])
matrix_2 = np.array([[35,24],[57,48]])
print("The matrix multiplication of given two matrices is given by n", np.matmul(matrix_1, matrix_2))
输出:
The matrix multiplication of given two matrices is given by
[[3513 2712]
[5651 4392]]
使用内联函数(Lambda)的元素操作
在此示例中,我们将尝试为矩阵的每个元素添加某个值。
import numpy as np
matrix = np.array([[1,2,4],[3,4,6], [7,8,5]])
addition = lambda i:i+5
add_5_vec = np.vectorize(addition)
print("The matrix after adding 5 to all its elements is n", add_5_vec(matrix))
输出:
The matrix after adding 5 to all its elements is
[[ 6 7 9]
[ 8 9 11]
[12 13 10]]
矩阵的逆
在本节中,我们将尝试找到矩阵的逆函数。
import numpy as np
matrix = np.array([[1,2,4],[3,4,6], [7,8,5]])
# Finding the inverse of a matrix
print("The inverse matrix of a given matrix is n", np.linalg.inv(matrix))
输出:
The inverse matrix of a given matrix is
[[-2.8 2.2 -0.4]
[ 2.7 -2.3 0.6]
[-0.4 0.6 -0.2]]
重塑给定的矩阵
在本节中,我们将尝试重塑给定的矩阵,即更改给定矩阵的形状。但在这里我们必须注意,重塑矩阵后大小保持不变,即元素的数量保持不变。
import numpy as np
matrix = np.array([[1,2,4],[3,4,6],[7,8,5],[9,2,1]])
print("The reshaped matrix is given by n", matrix.reshape(6,2))
输出:
The reshaped matrix is given by
[[1 2]
[4 3]
[4 6]
[7 8]
[5 9]
[2 1]]
本文原文来自CSDN