一、記憶敲門
先說最簡單的結(jié)論:
1維,只有W(width)
2維,先H(height),后W,即HW
3維,先C(channel),后HW,即CHW
4維,先B(batch),后CHW,即BCHW
更多維,依然理解維先大后小
二、代碼表示
2維張量示例,形狀3x5,(h=3,w=5)
>>> torch.arange(15).reshape([3,5])
tensor([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
可以看到果然最里面橫著的部分的數(shù)量是5,豎著的部分數(shù)量是3??梢钥闯龆S張量,這種高和寬的特性,與書寫是有對應關(guān)系的。
然后再看3維張量
>>> torch.arange(30).reshape([2,3,5])
tensor([[[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]],
[[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
[25, 26, 27, 28, 29]]])
可以看到,雖然無法通過多維數(shù)組顯示立體的東西,但依然可以看出,“里面”依然保留了“先大后小”的規(guī)律。
以下是讓大模型幫做的一個總結(jié),更為詳細(雖然理解上依然有些繞)
核心記憶規(guī)律:從外到內(nèi),從大到小;
# 最通用的記憶順序:[Batch, Channel, Height, Width]
# 可以記成:BCHW 或者 "批通高寬"
tensor_4d = torch.randn(8, 3, 224, 224)
# 維度0: Batch Size (8) - 一次處理幾張圖
# 維度1: Channels (3) - 每張圖有幾個通道
# 維度2: Height (224) - 圖片高度
# 維度3: Width (224) - 圖片寬度
具體對應關(guān)系和記憶技巧:
import torch
# 1D張量 - 向量
vector = torch.randn(100)
print(f"1D: {vector.shape}")
# 只有長度,像一條線
# 2D張量 - 矩陣/灰度圖
matrix = torch.randn(28, 28)
print(f"2D: {matrix.shape}")
# [高度, 寬度] - 像一張灰度照片
# 記憶:行列,先說行(高)再說列(寬)
# 3D張量 - 彩色圖/視頻幀
image_3d = torch.randn(3, 224, 224)
print(f"3D: {image_3d.shape}")
# [通道數(shù), 高度, 寬度] - RGB三層疊起來
# 記憶:CHW (Channel-Height-Width)
# 4D張量 - 批量圖像 (最常見)
batch_images = torch.randn(32, 3, 224, 224)
print(f"4D: {batch_images.shape}")
# [批次, 通道, 高度, 寬度] - 一摞彩色照片
# 記憶:BCHW (Batch-Channel-Height-Width)
# 5D張量 - 視頻/3D數(shù)據(jù)
video = torch.randn(16, 3, 10, 224, 224)
print(f"5D: {video.shape}")
# [批次, 通道, 時間/深度, 高度, 寬度]
# 記憶:BCTHW 或 BCDHW
記憶口訣和可視化:
def visualize_dimensions():
"""
維度記憶法:像俄羅斯套娃,從外到內(nèi)
"""
print("=== 維度記憶口訣 ===")
print("1D: 一條線 [長度]")
print("2D: 一張紙 [高, 寬] - 想象一張紙的長寬")
print("3D: 一本書 [頁數(shù), 高, 寬] - 想象書的厚度、長、寬")
print("4D: 一摞書 [摞數(shù), 頁數(shù), 高, 寬] - 想象書架上一摞摞書")
print("5D: 一排書架 [排數(shù), 摞數(shù), 頁數(shù), 高, 寬]")
print("\n=== 圖像數(shù)據(jù)的具體對應 ===")
print("2D圖像: [H, W] - 灰度圖")
print("3D圖像: [C, H, W] - 彩色圖")
print("4D批次: [B, C, H, W] - 一批彩色圖")
print("5D視頻: [B, C, T, H, W] - 一批視頻")
visualize_dimensions()
# 實際例子對比
print("\n=== 實際數(shù)據(jù)例子 ===")
# 單張灰度圖 MNIST
mnist_single = torch.randn(28, 28)
print(f"單張MNIST: {mnist_single.shape} [H, W]")
# 單張彩色圖
cifar_single = torch.randn(3, 32, 32)
print(f"單張CIFAR: {cifar_single.shape} [C, H, W]")
# 一批訓練圖像
batch_cifar = torch.randn(128, 3, 32, 32)
print(f"一批CIFAR: {batch_cifar.shape} [B, C, H, W]")
# 一個視頻片段
video_clip = torch.randn(1, 3, 16, 224, 224)
print(f"視頻片段: {video_clip.shape} [B, C, T, H, W]")
不同框架的差異要注意:
# PyTorch: BCHW (Channel First)
pytorch_tensor = torch.randn(8, 3, 224, 224)
print(f"PyTorch格式: {pytorch_tensor.shape} [B, C, H, W]")
# TensorFlow: BHWC (Channel Last)
# tensorflow_tensor = tf.random.normal([8, 224, 224, 3])
# print(f"TensorFlow格式: [B, H, W, C]")
# 轉(zhuǎn)換方法
# BCHW -> BHWC
pytorch_to_tf = pytorch_tensor.permute(0, 2, 3, 1)
print(f"轉(zhuǎn)換后: {pytorch_to_tf.shape} [B, H, W, C]")
記憶技巧總結(jié):
-
層次思維:從整體到局部
- Batch → Channel → Height → Width
- 整批 → 通道 → 行 → 列
-
物理類比:
- 1D: 一條線
- 2D: 一張紙
- 3D: 一本書(多頁)
- 4D: 一摞書
- 5D: 一排書架
HW永遠在最后:高度和寬度總是最后兩個維度
-
Channel的位置:
- PyTorch: Channel在HW前面
- TensorFlow: Channel在HW后面
口訣:"批通高寬" (BCHW) - 最常用的4D格式