S1-11 · 进阶 · 3DS1-11 · Advanced · 3D

张量 · 数值数组的统一容器Tensor · One Container for Numeric Arrays

shape [batch, sequence, feature] = [2, 3, 4]

tensor 首先不是框架名词,而是有形状的数值数组。转动这堆 [2,3,4] 积木,再沿任一轴切一片。A tensor is first a shaped numeric array, not a framework buzzword. Orbit this [2,3,4] block and slice it along any axis.

01 · 从抽屉柜开始01 · Think of a drawer cabinet

24 个数排成一长串,和分成 2 批、每批 3 步、每步 4 个特征,有什么不同?What changes when 24 values become 2 batches × 3 steps × 4 features instead of one long row?

023D 逐轴切片台3D Axis Slicing Bench

24 values · 3 axes
shape
[2,3,4]
三条轴three axes
总元素Elements
24
2 × 3 × 4
切片元素Slice size
12
3 × 4
正在装配 [2,3,4] 张量…Assembling the [2,3,4] tensor…
axis 0 · batch[0]
batch · 2
sequence · 3
feature · 4
拖动旋转 · 滚轮缩放 · 闲置自动缓转drag to orbit · scroll to zoom · idle auto-rotates

03形状里的 4 个零件Four Parts Inside the Shape

rank = 3
VALUE

元素Element

每块积木代表一个数值,不代表一个轴。Each block is one numeric value, not one axis.

AXIS

Axis

批次、序列、特征是三种独立索引方向。Batch, sequence, and feature are three independent index directions.

SHAPE

形状Shape

[2,3,4] 依次说明三条轴各有多少格。[2,3,4] states the size of each axis in order.

SLICE

切片Slice

固定一条轴的索引,就得到少一维的截面。Fix one axis index to obtain a cross-section with one fewer dimension.

04最小机制:形状相乘得到元素总数Minimal Mechanism: Multiply Shape Sizes for Element Count

shape → count → slice
count = 2 × 3 × 4 = 24
固定 batch 索引后剩 [3,4] 共 12 个元素;固定 feature 索引后剩 [2,3] 共 6 个元素。Fixing a batch index leaves [3,4] with 12 elements; fixing a feature index leaves [2,3] with 6.
选择一条轴Choose an axis决定固定哪个索引decide which index to fix
高亮一层Highlight one layer其余值仍存在other values still exist
维数减少 1Rank drops by 1[2,3,4] → [3,4]

05边界实验:同样 24 个数,错轴会错义Boundary Test: Same 24 Values, Wrong Axis Means Wrong Semantics

主动制造失败Create a failure

交换“序列”和“特征”标签Swap “sequence” and “feature” labels

元素总数仍是 24,但程序会把 4 个时间步误当成 4 个特征,后续矩阵维度也会错。The count stays 24, but the program treats four time steps as four features and later matrix dimensions no longer match.

尚未执行。先沿三条正确轴各切一片。Not run yet. Slice once along each correct axis first.
tensor 只是某个软件框架。A tensor is just a software framework object. → ✓ 它首先是有形状的数值数组。It is first a shaped numeric array.
元素数相同,含义就相同。Equal element counts mean equal semantics. → ✓ 轴顺序与标签决定各维含义。Axis order and labels determine dimensional meaning.
切片会删掉整个原张量。Slicing deletes the original tensor. → ✓ 切片是选取视图,未选元素仍存在。A slice selects a view; unselected values remain.
06 · 一句话带走06 · One line to keep

张量是零到多维的数值数组;shape 记录每条轴的长度,沿轴切片能直接检验每个维度在组织什么。A tensor is a numeric array with zero or more dimensions. Its shape records each axis size, and slicing reveals what each dimension organizes.