Tensorflow 简明教程
TensorFlow - Mathematical Foundations
在 TensorFlow 中创建基本应用程序之前,了解 TensorFlow 所需的数学概念非常重要。数学被认为是任何机器学习算法的核心。借助数学的核心概念,可以定义特定机器学习算法的解决方案。
It is important to understand mathematical concepts needed for TensorFlow before creating the basic application in TensorFlow. Mathematics is considered as the heart of any machine learning algorithm. It is with the help of core concepts of Mathematics, a solution for specific machine learning algorithm is defined.
Vector
连续或离散的一组数字被定义为向量。机器学习算法处理固定长度的向量以生成更好的输出。
An array of numbers, which is either continuous or discrete, is defined as a vector. Machine learning algorithms deal with fixed length vectors for better output generation.
机器学习算法处理多维数据,因此向量起着至关重要的作用。
Machine learning algorithms deal with multidimensional data so vectors play a crucial role.
向量模型的图片表示如下所示 −
The pictorial representation of vector model is as shown below −
Scalar
标量可以定义为一维向量。标量是仅包含大小且没有方向的标量。对于标量,我们只关心大小。
Scalar can be defined as one-dimensional vector. Scalars are those, which include only magnitude and no direction. With scalars, we are only concerned with the magnitude.
标量的示例包括儿童的体重和身高参数。
Examples of scalar include weight and height parameters of children.
Matrix
矩阵可以定义为多维数组,这些数组按行和列的格式排列。矩阵的大小由行长度和列长度定义。下图显示了任何指定矩阵的表示。
Matrix can be defined as multi-dimensional arrays, which are arranged in the format of rows and columns. The size of matrix is defined by row length and column length. Following figure shows the representation of any specified matrix.
考虑上面提到的具有“m”行和“n”列的矩阵,矩阵表示将指定为“m*n 矩阵”,该矩阵也定义了矩阵的长度。
Consider the matrix with “m” rows and “n” columns as mentioned above, the matrix representation will be specified as “m*n matrix” which defined the length of matrix as well.
Mathematical Computations
在本部分,我们将了解 TensorFlow 中的不同数学计算。
In this section, we will learn about the different Mathematical Computations in TensorFlow.
Addition of matrices
如果矩阵具有相同的维数,则可以对两个或更多个矩阵进行加法。加法意味着按给定位置对每个元素进行加法。
Addition of two or more matrices is possible if the matrices are of the same dimension. The addition implies addition of each element as per the given position.
考虑以下示例以了解矩阵加法如何工作 −
Consider the following example to understand how addition of matrices works −
示例:A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B=\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}\:then\:A+B=\begin{bmatrix}1+5 & 2+6 \\3+7 & 4+8 \end{bmatrix}=\begin{bmatrix}6 & 8 \\10 & 12 \end{bmatrix}
Example:A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B=\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}\:then\:A+B=\begin{bmatrix}1+5 & 2+6 \\3+7 & 4+8 \end{bmatrix}=\begin{bmatrix}6 & 8 \\10 & 12 \end{bmatrix}
Subtraction of matrices
矩阵的减法操作方式与两个矩阵的加法类似。只要维数相等,用户就可以减去两个矩阵。
The subtraction of matrices operates in similar fashion like the addition of two matrices. The user can subtract two matrices provided the dimensions are equal.
示例:A-\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B-\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}\:then\:A-B-\begin{bmatrix}1-5 & 2-6 \\3-7 & 4-8 \end{bmatrix}-\begin{bmatrix}-4 & -4 \\-4 & -4 \end{bmatrix}
Example:A-\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B-\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}\:then\:A-B-\begin{bmatrix}1-5 & 2-6 \\3-7 & 4-8 \end{bmatrix}-\begin{bmatrix}-4 & -4 \\-4 & -4 \end{bmatrix}
Multiplication of matrices
对于两个矩阵 A m*n 和 B p*q 可以相乘, n 应等于 p 。结果矩阵为 −
For two matrices A m*n and B p*q to be multipliable, n should be equal to p. The resulting matrix is −
C m*q
A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}B=\begin{bmatrix}5 & 6 \\7 & 8 \end{bmatrix}
c_{11}=\begin{bmatrix}1 & 2 \end{bmatrix}\begin{bmatrix}5 \\7 \end{bmatrix}=1\times5+2\times7=19\:c_{12}=\begin{bmatrix}1 & 2 \end{bmatrix}\begin{bmatrix}6 \\8 \end{bmatrix}=1\times6+2\times8=22
c_{21}=\begin{bmatrix}3 & 4 \end{bmatrix}\begin{bmatrix}5 \\7 \end{bmatrix}=3\times5+4\times7=43\:c_{22}=\begin{bmatrix}3 & 4 \end{bmatrix}\begin{bmatrix}6 \\8 \end{bmatrix}=3\times6+4\times8=50
C=\begin{bmatrix}c_{11} & c_{12} \\c_{21} & c_{22} \end{bmatrix}=\begin{bmatrix}19 & 22 \\43 & 50 \end{bmatrix}
Transpose of matrix
矩阵 A 的转置,m*n 通常表示为 AT(转置)n*m;可通过将列向量转置为行向量获得。
The transpose of a matrix A, m*n is generally represented by AT (transpose) n*m and is obtained by transposing the column vectors as row vectors.
示例:A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}:然后 A^{T}\begin{bmatrix}1 & 3 \\2 & 4 \end{bmatrix}
Example:A=\begin{bmatrix}1 & 2 \\3 & 4 \end{bmatrix}\:then\:A^{T}\begin{bmatrix}1 & 3 \\2 & 4 \end{bmatrix}
Dot product of vectors
任何维度为 n 的向量可表示为矩阵 v = R^n*1。
Any vector of dimension n can be represented as a matrix v = R^n*1.
v_{1}=\begin{bmatrix}v_{11} \\v_{12} \\\cdot\\\cdot\\\cdot\\v_{1n}\end{bmatrix}v_{2}=\begin{bmatrix}v_{21} \\v_{22} \\\cdot\\\cdot\\\cdot\\v_{2n}\end{bmatrix}
两个向量的点积是对应分量的乘积之和 - 相同维度上的分量,其表示形式为
The dot product of two vectors is the sum of the product of corresponding components − Components along the same dimension and can be expressed as
v_{1}\cdot v_{2}=v_1 Tv_{2}=v_2 Tv_{1}=v_{11}v_{21}v_{12}v_{22}\cdot\cdot+v_{1n}v_{2n}=\displaystyle\sum\limits_{k=1}^n v_{1k}v_{2k}
v_{1}\cdot v_{2}=v_1Tv_{2}=v_2Tv_{1}=v_{11}v_{21}v_{12}v_{22}\cdot\cdot+v_{1n}v_{2n}=\displaystyle\sum\limits_{k=1}^n v_{1k}v_{2k}
下面提到了向量的点积示例 -
The example of dot product of vectors is mentioned below −
示例:v_{1}=\begin{bmatrix}1 \\2 \\3\end{bmatrix}v_{2}=\begin{bmatrix}3 \\5 \\-1\end{bmatrix}v_{1}\cdot v_{2}=v_1^Tv_{2}=1\times3+2\times5-3\times1=10
Example:v_{1}=\begin{bmatrix}1 \\2 \\3\end{bmatrix}v_{2}=\begin{bmatrix}3 \\5 \\-1\end{bmatrix}v_{1}\cdot v_{2}=v_1^Tv_{2}=1\times3+2\times5-3\times1=10