📜  Tensorflow 中的数学基础

📅  最后修改于: 2022-05-13 01:55:08.265000             🧑  作者: Mango

Tensorflow 中的数学基础

在构建基本的 TensorFlow 程序之前,掌握 TensorFlow 所需的数学思想至关重要。任何机器学习算法的核心都被认为是数学。借助关键数学原理,为某种机器学习算法建立策略或解决方案。让我们深入了解 TensorFlow 的数学基础。

标量

标量是一个没有方向的物理量,完全以其大小为特征。标量是只有一维的向量。

Python3
# importing packages
import tensorflow as tf
  
# creating a scalar
scalar = tf.constant(7)
scalar


Python3
scalar.ndim


Python3
# importing packages
import tensorflow as tf
  
# create a vector
vector = tf.constant([10, 10])
  
# checking the dimensions of vector
vector.ndim


Python3
# 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))


Python3
# 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(matrix+matrix1)


Python3
# 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)


Python3
# 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)


Python3
# 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)


Python3
# importing packages
import tensorflow as tf
  
# creating a matrix
matrix = tf.constant([[1, 2], [3, 4]])
# transpose of the matrix
print(tf.transpose(matrix))


Python3
# 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, axes=1)))


输出:

检查尺寸:

Python3

scalar.ndim

输出:

0

向量

向量是具有大小和方向的二维对象。我们可以在几何上将向量解释为有向线段,箭头表示方向,线的长度等于向量的大小。下面是在 TensorFlow 中创建向量的示例。

Python3

# importing packages
import tensorflow as tf
  
# create a vector
vector = tf.constant([10, 10])
  
# checking the dimensions of vector
vector.ndim

输出:

1

矩阵

矩阵是一个术语,指的是按行和列组织的多维数组。行和列的长度决定了矩阵的大小。当矩阵有“a”行和“b”列时,矩阵表示为“a*b”矩阵,它也指定了矩阵的长度。

Python3

# 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), dtype=int32)
the number of dimensions of a matrix is : 2

数学运算

添加

当两个或多个矩阵具有相同的维度时,可以将它们相加。术语“添加”是指将每个元素添加到给定位置或位置的过程。

Python3

# 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(matrix+matrix1)

输出:

tf.Tensor(
[[ 3  6]
 [ 9 12]], shape=(2, 2), dtype=int32)

减法

矩阵的减法与两个矩阵相加的工作方式相同。如果两个矩阵的维数相同,用户可以将它们相减。

Python3

# 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), dtype=int32)

乘法

维度 n 必须等于 a 两个矩阵 m*n 和 a*b 才能相乘。 m*b 是结果矩阵。

Python3

# 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), dtype=int32)

分配

要执行除法,两个矩阵必须具有相同的维度,就像加法一样。

Python3

# 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), dtype=float64)

转置

矩阵的转置是通过将其行转换为列或将列转换为行来确定的。提供的矩阵上标中的字母“T”表示矩阵的转置。

矩阵 M m*n 的转置为 MT(转置)n*m,它是通过将列向量转置为行向量得出的。 tf.transpose() 方法用于在 TensorFlow 中查找矩阵的转置。如果 M 是矩阵,则转置由 M T表示

Python3

# 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), dtype=int32)

点积

匹配分量的乘积之和是两个向量的点积。位于同一轴上的组件可以表示为:

tf.tensodot() 方法用于在 TensorFlow 中查找点积。当我们指定轴 = 1 时,会发生矩阵乘法。

Python3

# 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, axes=1)))

输出:

dot product of matrices is : tf.Tensor(
[[ 7 10]
 [15 22]], shape=(2, 2), dtype=int32)