向量基础:从数学到人工智能 | Vector Fundamentals: From Mathematics to AI
向量基础:从数学到人工智能
Vector Fundamentals: From Mathematics to AI
当我们谈论人工智能时,常常会听到“向量”这个词。向量不仅是数学中的基本概念,更是现代人工智能技术的基石。从ChatGPT理解你的问题,到Stable Diffusion生成精美图像,再到推荐系统猜到你可能喜欢的商品——这一切的背后,都是向量在发挥作用。向量将这些看似不同的数据类型——文字、图像、声音——统一成机器能够理解和计算的数值形式。理解向量,就是打开了人工智能世界的第一扇门。
When we talk about artificial intelligence, the term "vector" frequently emerges. Vectors are not only fundamental concepts in mathematics but also the cornerstone of modern AI technology. From ChatGPT understanding your questions, to Stable Diffusion generating beautiful images, to recommendation systems predicting what you might like—behind all of these lies the power of vectors. Vectors unify different types of data—text, images, audio—into numerical forms that machines can understand and process. Understanding vectors is opening the first gate to the world of artificial intelligence.
1.1 向量的定义:从物理到数学
1.1 Definition of Vectors: From Physics to Mathematics
什么是向量?
What is a Vector?
在物理学中,我们用向量来描述那些既有大小又有方向的物理量。速度是一个向量——它不仅告诉我们移动有多快,还指明了我们向哪个方向移动。力也是一个向量——推拉物体时,力的大小决定了效果的程度,力的方向决定了效果的方向。当你在地图上标记从当前位置到目的地的路线时,你实际上也在使用向量的概念。
In physics, we use vectors to describe quantities that have both magnitude and direction. Velocity is a vector—it tells us not only how fast something is moving, but also in which direction. Force is also a vector—when pushing or pulling an object, the magnitude of the force determines the intensity of the effect, while the direction determines where that effect is directed. When you mark a route from your current location to your destination on a map, you are essentially using the concept of vectors.
在数学中,我们对向量给出更精确的定义:向量是由一组有序数组成的对象。这组有序数称为向量的分量(components)。例如,在二维空间中,一个向量可以表示为 $\vec{v} = (v_1, v_2)$,其中 $v_1$ 和 $v_2$ 是两个实数;在三维空间中,向量表示为 $\vec{v} = (v_1, v_2, v_3)$。这种表示方法看似简单,却蕴含着强大的抽象能力——我们可以将任意多个数组成一个向量,从而描述任意维度的数据。
In mathematics, we provide a more precise definition of vectors: a vector is an object composed of an ordered set of numbers. This ordered set of numbers is called the components of the vector. For example, in two-dimensional space, a vector can be represented as $\vec{v} = (v_1, v_2)$, where $v_1$ and $v_2$ are two real numbers; in three-dimensional space, the vector is represented as $\vec{v} = (v_1, v_2, v_3)$. This seemingly simple representation contains powerful abstract capability—we can form a vector from any number of components, thereby describing data of any dimension.
向量的表示方法
Methods of Representing Vectors
向量有多种等价的表示方式,理解这些不同的表示方法有助于我们从不同角度理解向量的本质。
Vectors have several equivalent representations, and understanding these different methods helps us grasp the essence of vectors from various perspectives.
几何表示法是最直观的。我们把向量画成一条带有方向的线段——通常用箭头表示。箭头的起点称为向量的尾部(tail),终点称为向量的头部(head)。箭头的长度表示向量的大小(magnitude),箭头指向的方向表示向量的方向。
Geometric representation is the most intuitive. We draw a vector as a line segment with direction—typically represented by an arrow. The starting point of the arrow is called the tail of the vector, and the endpoint is called the head. The length of the arrow represents the magnitude of the vector, and the direction the arrow points represents the direction of the vector.
代数表示法用坐标来描述向量。在二维笛卡尔坐标系中,向量 $\vec{v}$ 可以表示为有序对 $(v_x, v_y)$,其中 $v_x$ 是向量在 $x$ 轴方向上的分量,$v_y$ 是向量在 $y$ 轴方向上的分量。在三维空间中,向量表示为三元组 $(v_x, v_y, v_z)$。
Algebraic representation uses coordinates to describe vectors. In a two-dimensional Cartesian coordinate system, the vector $\vec{v}$ can be represented as an ordered pair $(v_x, v_y)$, where $v_x$ is the component of the vector in the $x$-axis direction, and $v_y$ is the component in the $y$-axis direction. In three-dimensional space, vectors are represented as triples $(v_x, v_y, v_z)$.
矩阵表示法将向量看作一个特殊的矩阵。当我们讨论列向量时,向量 $\vec{v} = (v_1, v_2, ..., v_n)^T$ 是一个 $n \times 1$ 的列矩阵;当讨论行向量时,$\vec{v} = [v_1, v_2, ..., v_n]$ 是一个 $1 \times n$ 的行矩阵。在深度学习框架中,列向量是最常用的表示方式。
Matrix representation views vectors as special matrices. When discussing column vectors, the vector $\vec{v} = (v_1, v_2, ..., v_n)^T$ is an $n \times 1$ column matrix; when discussing row vectors, $\vec{v} = [v_1, v_2, ..., v_n]$ is a $1 \times n$ row matrix. In deep learning frameworks, column vectors are the most commonly used representation.
```python
Python中向量的几种表示方式
Several ways to represent vectors in Python
import numpy as np
使用NumPy数组表示向量
Using NumPy arrays to represent vectors
v_2d = np.array([3, 4]) # 二维向量 / 2D vector v_3d = np.array([1, 2, 3]) # 三维向量 / 3D vector v_nd = np.array([1, 2, 3, 4, 5]) # 五维向量 / 5D vector
print("二维向量:", v_2d) # [3 4] print("三维向量:", v_3d) # [1 2 3] print("五维向量:", v_nd) # [1 2 3 4 5]
使用列表表示向量(不推荐用于计算)
Using lists to represent vectors (not recommended for computation)
v_list = [3, 4] print("列表表示:", v_list) # [3, 4] ```
1.2 向量的几何意义
1.2 Geometric Meaning of Vectors
二维向量:从平面到点
2D Vectors: From Plane to Points
让我们从最简单的二维向量开始深入理解。考虑向量 $\vec{v} = (3, 4)$,它表示在 $x$ 轴方向移动 3 个单位,在 $y$ 轴方向移动 4 个单位。
Let's start with the simplest 2D vectors to build a deep understanding. Consider the vector $\vec{v} = (3, 4)$, which represents moving 3 units in the $x$-axis direction and 4 units in the $y$-axis direction.
从几何角度,我们可以将这个向量理解为从原点 $(0, 0)$ 指向点 $(3, 4)$ 的箭头。但这还不是全部——向量的起点并不一定要在原点。如果我们将这个向量平移,使其从点 $(2, 1)$ 开始,那么向量的头部将到达点 $(2+3, 1+4) = (5, 5)$。向量的平移不变性是向量的一个重要特性:向量只描述大小和方向,而不关心具体位置。
From a geometric perspective, we can understand this vector as an arrow pointing from the origin $(0, 0)$ to the point $(3, 4)$. But that's not all—the starting point of a vector doesn't have to be at the origin. If we translate this vector so it starts from the point $(2, 1)$, then the head of the vector will reach $(2+3, 1+4) = (5, 5)$. Translation invariance of vectors is an important characteristic: vectors only describe magnitude and direction, not specific position.
```python import matplotlib.pyplot as plt import numpy as np
可视化二维向量
Visualizing 2D vectors
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
定义向量
Define vectors
v = np.array([3, 4]) origin = np.array([[0, 0], [2, 1]], [[0, 0], [0, 0]]) # 两个向量的起点 / Starting points for two vectors
绘制向量v从原点出发
Plot vector v from origin
ax.arrow(0, 0, v[0], v[1], head_width=0.2, head_length=0.15, fc='blue', ec='blue', linewidth=2, label=f'v = {v}')
绘制同一个向量v从点(2,1)出发
Plot the same vector v starting from point (2,1)
ax.arrow(2, 1, v[0], v[1], head_width=0.2, head_length=0.15, fc='red', ec='red', linewidth=2, label=f'v (translated)')
绘制点
Plot points
ax.scatter([3, 5], [4, 5], c=['blue', 'red'], s=100, zorder=5) ax.scatter([0, 2], [0, 1], c=['blue', 'red'], s=100, zorder=5)
设置坐标轴
Set up axes
ax.set_xlim(-1, 6) ax.set_ylim(-1, 6) ax.set_aspect('equal') ax.grid(True, alpha=0.3) ax.axhline(y=0, color='k', linewidth=0.5) ax.axvline(x=0, color='k', linewidth=0.5) ax.set_xlabel('x', fontsize=12) ax.set_ylabel('y', fontsize=12) ax.set_title('向量平移不变性 / Vector Translation Invariance', fontsize=14) ax.legend(loc='upper left')
plt.tight_layout() plt.savefig('vector_translation.png', dpi=150, bbox_inches='tight') plt.show() ```
向量的大小(也称为范数或长度)是向量的重要属性。对于二维向量 $\vec{v} = (v_x, v_y)$,其大小可以通过勾股定理计算:
The magnitude of a vector (also called norm or length) is an important attribute. For a 2D vector $\vec{v} = (v_x, v_y)$, its magnitude can be calculated using the Pythagorean theorem:
$$||\vec{v}|| = \sqrt{v_x^2 + v_y^2}$$
对于向量 $\vec{v} = (3, 4)$,其大小为:
For the vector $\vec{v} = (3, 4)$, its magnitude is:
$$||\vec{v}|| = \sqrt{3^2 + 4^2} = \sqrt{9 + 16} = \sqrt{25} = 5$$
这个结果验证了一个经典的勾股数:3-4-5直角三角形。
This result confirms a classic Pythagorean triple: the 3-4-5 right triangle.
三维向量:进入立体空间
3D Vectors: Entering Three-Dimensional Space
三维向量将我们从平面带入立体空间。考虑向量 $\vec{v} = (1, 2, 3)$,它表示在 $x$、$y$、$z$ 三个坐标轴方向上分别移动 1、2、3 个单位。
3D vectors take us from the plane into three-dimensional space. Consider the vector $\vec{v} = (1, 2, 3)$, which represents moving 1, 2, and 3 units alon