TensorFlow 基础数学运算教程


发布日期 : 2023-02-06 21:27:31 UTC

访问量: 10 次浏览

Tensorflow的数学基础

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

Scalar

标量是一个没有方向的物理量,完全由其大小来表征。标量是只有一维的向量。

# importing packages
import tensorflow as tf

# creating a scalar
scalar = tf.constant(7)
scalar

输出:

<tf.Tensor: shape=(), dtype=int32, numpy=7>

检查尺寸:

scalar.ndim

输出:

0

Vector

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

# importing packages
import tensorflow as tf

# create a vector
vector = tf.constant([10, 10])

# checking the dimensions of vector
vector.ndim

输出:

1

Matrix

矩阵是一个术语,指的是以行和列组织的多维数组。行和列的长度决定了矩阵的大小。
当一个矩阵有 “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), dtype=int32)
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(matrix+matrix1)

输出:

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

减法

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

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

除法

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

# 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 mn的转置是MT(转置)nm,它是通过将列向量转置为行向量而得到的。
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), dtype=int32)

点积

匹配成分的乘积之和是两个向量的点积。在同一轴上的成分,可以表示为:。

Tensorflow的数学基础

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

输出:

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