网站建设的原则有哪些,wordpress主题修改不了,不懂代码用cms做网站,阿里云服务器怎么安装wordpressTensorflow的数学基础 在构建一个基本的TensorFlow程序之前#xff0c;关键是要掌握TensorFlow所需的数学思想。任何机器学习算法的核心都被认为是数学。某种机器学习算法的策略或解决方案是借助于关键的数学原理建立的。让我们深入了解一下TensorFlow的数学基础。
Scalar
标…Tensorflow的数学基础 在构建一个基本的TensorFlow程序之前关键是要掌握TensorFlow所需的数学思想。任何机器学习算法的核心都被认为是数学。某种机器学习算法的策略或解决方案是借助于关键的数学原理建立的。让我们深入了解一下TensorFlow的数学基础。
Scalar
标量是一个没有方向的物理量完全由其大小来表征。标量是只有一维的向量。
# importing packages
import tensorflow as tf# creating a scalar
scalar tf.constant(7)
scalar输出:
tf.Tensor: shape(), dtypeint32, numpy7检查尺寸
scalar.ndim输出:
0Vector
矢量是一个有大小和方向的二维对象。我们可以把矢量从几何上解释为一个有方向的线段箭头显示方向线的长度等于矢量的大小。下面是一个在TensorFlow中创建矢量的例子。
# importing packages
import tensorflow as tf# create a vector
vector tf.constant([10, 10])# checking the dimensions of vector
vector.ndim输出:
1Matrix
矩阵是一个术语指的是以行和列组织的多维数组。行和列的长度决定了矩阵的大小。当一个矩阵有 “a “行和 “b “列时该矩阵被表示为 “a*b “矩阵这也指定了该矩阵的长度。
# importing packages
import tensorflow as tf# creating a matrix
matrix tf.constant([[1, 2], [3, 4]])
print(matrix)
print(the number of dimensions of a matrix is :\
str(matrix.ndim))输出:
tf.Tensor(
[[1 2][3 4]], shape(2, 2), dtypeint32)
the number of dimensions of a matrix is : 2数学操作
加法
当两个或多个矩阵具有相同的维度时它们可以被加在一起。术语 “加法 “指的是将每个元素添加到给定的位置或地点的过程。
# importing packages
import tensorflow as tf# creating two tensors
matrix tf.constant([[1, 2], [3, 4]])
matrix1 tf.constant([[2, 4], [6, 8]])# addition of two matrices
print(matrixmatrix1)输出:
tf.Tensor(
[[ 3 6][ 9 12]], shape(2, 2), dtypeint32)减法
矩阵的减法与两个矩阵的加法的工作方式相同。如果两个矩阵的尺寸相同用户可以将它们相减。
# importing packages
import tensorflow as tf# creating two tensors
matrix tf.constant([[1, 2], [3, 4]])
matrix1 tf.constant([[2, 4], [6, 8]])# subtraction of two matrices
print(matrix1 - matrix)输出:
tf.Tensor(
[[1 2][3 4]], shape(2, 2), dtypeint32)乘法
维度n必须等于a两个矩阵m*n和a*b才可以相乘。m*b是结果矩阵。
# importing packages
import tensorflow as tf# creating two tensors
matrix tf.constant([[1, 2], [3, 4]])
matrix1 tf.constant([[2, 4], [6, 8]])# multiplication of two matrices
print(matrix1 * matrix)输出:
tf.Tensor(
[[ 2 8][18 32]], shape(2, 2), dtypeint32)除法
为了执行除法两个矩阵必须具有相同的维度就像加法一样。
# importing packages
import tensorflow as tf# creating two tensors
matrix tf.constant([[1, 2],[3, 4]])
matrix1 tf.constant([[2, 4],[6, 8]])# division of two matrices
print(matrix1 / matrix)输出:
tf.Tensor(
[[2. 2.][2. 2.]], shape(2, 2), dtypefloat64)矩阵的转置
矩阵的转置是通过将其行转换成列或将列转换成行来确定的。所提供的矩阵上标中的字母 “T “表示该矩阵的转置。
矩阵M m_n的转置是MT转置n_m它是通过将列向量转置为行向量而得到的。Tf.transpose()方法用于在TensorFlow中查找矩阵的转置。如果M是一个矩阵转置用M T表示
# importing packages
import tensorflow as tf# creating a matrix
matrix tf.constant([[1, 2], [3, 4]])
# transpose of the matrix
print(tf.transpose(matrix))输出:
tf.Tensor(
[[1 3][2 4]], shape(2, 2), dtypeint32)点积
匹配成分的乘积之和是两个向量的点积。在同一轴上的成分可以表示为 tf.tensodot()方法用于在TensorFlow中寻找点积。当我们指定轴1时矩阵乘法就会发生。
# importing packages
import tensorflow as tf# creating a matrix
matrix tf.constant([[1, 2], [3, 4]])# dot product of matrices
print(dot product of matrices is : str(tf.tensordot(matrix, matrix, axes1)))输出:
dot product of matrices is : tf.Tensor(
[[ 7 10][15 22]], shape(2, 2), dtypeint32)