Python训练营打卡Day48(2025.6.8)
知识点回顾:
- 随机张量的生成:torch.randn函数
- 卷积和池化的计算公式(可以不掌握,会自动计算的)
- pytorch的广播机制:加法和乘法的广播机制
import torch
# 生成标量(0维张量)
scalar = torch.randn(())
print(f"标量: {scalar}, 形状: {scalar.shape}") # 生成向量(1维张量)
vector = torch.randn(5) # 长度为5的向量
print(f"向量: {vector}, 形状: {vector.shape}") # 生成矩阵(2维张量)
matrix = torch.randn(3, 4) # 3行4列的矩阵
print(f"矩阵:{matrix},矩阵形状: {matrix.shape}") # 生成3维张量(常用于图像数据的通道、高度、宽度)
tensor_3d = torch.randn(3, 224, 224) # 3通道,高224,宽224
print(f"3维张量形状: {tensor_3d.shape}") # 输出: torch.Size([3, 224, 224])# 生成4维张量(常用于批量图像数据:[batch, channel, height, width])
tensor_4d = torch.randn(2, 3, 224, 224) # 批量大小为2,3通道,高224,宽224
print(f"4维张量形状: {tensor_4d.shape}") # 输出: torch.Size([2, 3, 224, 224])x = torch.rand(3, 2) # 生成3x2的张量
print(f"均匀分布随机数: {x}, 形状: {x.shape}")x = torch.randint(low=0, high=10, size=(3,)) # 生成3个0到9之间的整数
print(f"随机整数: {x}, 形状: {x.shape}")mean = torch.tensor([0.0, 0.0])
std = torch.tensor([1.0, 2.0])
x = torch.normal(mean, std) # 生成两个正态分布随机数
print(f"正态分布随机数: {x}, 形状: {x.shape}")# 一维张量与二维张量相加
a = torch.tensor([[1, 2, 3], [4, 5, 6]]) # 形状: (2, 3)
b = torch.tensor([10, 20, 30]) # 形状: (3,)# 广播后:b被扩展为[[10, 20, 30], [10, 20, 30]]
result = a + b
resultimport torch
import torch.nn as nn# 生成输入张量 (批量大小, 通道数, 高度, 宽度)
input_tensor = torch.randn(1, 3, 32, 32) # 例如CIFAR-10图像
print(f"输入尺寸: {input_tensor.shape}") # 输出: [1, 3, 32, 32]# 1. 卷积层操作
conv1 = nn.Conv2d(in_channels=3, # 输入通道数out_channels=16, # 输出通道数(卷积核数量)kernel_size=3, # 卷积核大小stride=1, # 步长padding=1 # 填充
)
conv_output = conv1(input_tensor) # 由于 padding=1 且 stride=1,空间尺寸保持不变
print(f"卷积后尺寸: {conv_output.shape}") # 输出: [1, 16, 32, 32]# 2. 池化层操作 (减小空间尺寸)
pool = nn.MaxPool2d(kernel_size=2, stride=2) # 创建一个最大池化层
pool_output = pool(conv_output)
print(f"池化后尺寸: {pool_output.shape}") # 输出: [1, 16, 16, 16]# 3. 将多维张量展平为向量
flattened = pool_output.view(pool_output.size(0), -1)
print(f"展平后尺寸: {flattened.shape}") # 输出: [1, 4096] (16*16*16=4096)# 4. 线性层操作
fc1 = nn.Linear(in_features=4096, # 输入特征数out_features=128 # 输出特征数
)
fc_output = fc1(flattened)
print(f"线性层后尺寸: {fc_output.shape}") # 输出: [1, 128]# 5. 再经过一个线性层(例如分类器)
fc2 = nn.Linear(128, 10) # 假设是10分类问题
final_output = fc2(fc_output)
print(f"最终输出尺寸: {final_output.shape}") # 输出: [1, 10]
print(final_output)# 使用Softmax替代Sigmoid
softmax = nn.Softmax(dim=1) # 在类别维度上进行Softmax
class_probs = softmax(final_output)
print(f"Softmax输出: {class_probs}") # 总和为1的概率分布
print(f"Softmax输出总和: {class_probs.sum():.4f}")import torch# 创建原始张量
a = torch.tensor([[10], [20], [30]]) # 形状: (3, 1)
b = torch.tensor([1, 2, 3]) # 形状: (3,)result = a + b
# 广播过程
# 1. b补全维度: (3,) → (1, 3)
# 2. a扩展列: (3, 1) → (3, 3)
# 3. b扩展行: (1, 3) → (3, 3)
# 最终形状: (3, 3)print("原始张量a:")
print(a)print("\n原始张量b:")
print(b)print("\n广播后a的值扩展:")
print(torch.tensor([[10, 10, 10],[20, 20, 20],[30, 30, 30]])) # 实际内存中未复制,仅逻辑上扩展print("\n广播后b的值扩展:")
print(torch.tensor([[1, 2, 3],[1, 2, 3],[1, 2, 3]])) # 实际内存中未复制,仅逻辑上扩展print("\n加法结果:")
print(result)# 创建原始张量
a = torch.tensor([[[1], [2]], [[3], [4]]]) # 形状: (2, 2, 1)
b = torch.tensor([[10, 20]]) # 形状: (1, 2)# 广播过程
# 1. b补全维度: (1, 2) → (1, 1, 2)
# 2. a扩展第三维: (2, 2, 1) → (2, 2, 2)
# 3. b扩展第一维: (1, 1, 2) → (2, 1, 2)
# 4. b扩展第二维: (2, 1, 2) → (2, 2, 2)
# 最终形状: (2, 2, 2)result = a + b
print("原始张量a:")
print(a)print("\n原始张量b:")
print(b)print("\n广播后a的值扩展:")
print(torch.tensor([[[1, 1],[2, 2]],[[3, 3],[4, 4]]])) # 实际内存中未复制,仅逻辑上扩展print("\n广播后b的值扩展:")
print(torch.tensor([[[10, 20],[10, 20]],[[10, 20],[10, 20]]])) # 实际内存中未复制,仅逻辑上扩展print("\n加法结果:")
print(result)# 创建原始张量
a = torch.tensor([[1, 2], [3, 4]]) # 形状: (2, 2)
b = 10 # 标量,形状视为 ()# 广播过程
# 1. b补全维度: () → (1, 1)
# 2. b扩展第一维: (1, 1) → (2, 1)
# 3. b扩展第二维: (2, 1) → (2, 2)
# 最终形状: (2, 2)result = a + b
print("原始张量a:")
print(a)
# 输出:
# tensor([[1, 2],
# [3, 4]])print("\n标量b:")
print(b)
# 输出: 10print("\n广播后b的值扩展:")
print(torch.tensor([[10, 10],[10, 10]])) # 实际内存中未复制,仅逻辑上扩展print("\n加法结果:")
print(result)
# 输出:
# tensor([[11, 12],
# [13, 14]])# 创建原始张量
a = torch.tensor([[[1, 2], [3, 4]]]) # 形状: (1, 2, 2)
b = torch.tensor([[5, 6]]) # 形状: (1, 2)# 广播过程
# 1. b补全维度: (1, 2) → (1, 1, 2)
# 2. b扩展第二维: (1, 1, 2) → (1, 2, 2)
# 最终形状: (1, 2, 2)result = a + b
print("原始张量a:")
print(a)
# 输出:
# tensor([[[1, 2],
# [3, 4]]])print("\n原始张量b:")
print(b)
# 输出:
# tensor([[5, 6]])print("\n广播后b的值扩展:")
print(torch.tensor([[[5, 6],[5, 6]]])) # 实际内存中未复制,仅逻辑上扩展print("\n加法结果:")
print(result)
# 输出:
# tensor([[[6, 8],
# [8, 10]]])import torch# A: 批量大小为2,每个是3×4的矩阵
A = torch.randn(2, 3, 4) # 形状: (2, 3, 4)# B: 单个4×5的矩阵
B = torch.randn(4, 5) # 形状: (4, 5)# 广播过程:
# 1. B补全维度: (4, 5) → (1, 4, 5)
# 2. B扩展第一维: (1, 4, 5) → (2, 4, 5)
# 矩阵乘法: (2, 3, 4) @ (2, 4, 5) → (2, 3, 5)
result = A @ B # 结果形状: (2, 3, 5)print("A形状:", A.shape) # 输出: torch.Size([2, 3, 4])
print("B形状:", B.shape) # 输出: torch.Size([4, 5])
print("结果形状:", result.shape) # 输出: torch.Size([2, 3, 5])# A: 批量大小为3,每个是2×4的矩阵
A = torch.randn(3, 2, 4) # 形状: (3, 2, 4)# B: 批量大小为1,每个是4×5的矩阵
B = torch.randn(1, 4, 5) # 形状: (1, 4, 5)# 广播过程:
# B扩展第一维: (1, 4, 5) → (3, 4, 5)
# 矩阵乘法: (3, 2, 4) @ (3, 4, 5) → (3, 2, 5)
result = A @ B # 结果形状: (3, 2, 5)print("A形状:", A.shape) # 输出: torch.Size([3, 2, 4])
print("B形状:", B.shape) # 输出: torch.Size([1, 4, 5])
print("结果形状:", result.shape) # 输出: torch.Size([3, 2, 5])# A: 批量大小为2,通道数为3,每个是4×5的矩阵
A = torch.randn(2, 3, 4, 5) # 形状: (2, 3, 4, 5)# B: 单个5×6的矩阵
B = torch.randn(5, 6) # 形状: (5, 6)# 广播过程:
# 1. B补全维度: (5, 6) → (1, 1, 5, 6)
# 2. B扩展第一维: (1, 1, 5, 6) → (2, 1, 5, 6)
# 3. B扩展第二维: (2, 1, 5, 6) → (2, 3, 5, 6)
# 矩阵乘法: (2, 3, 4, 5) @ (2, 3, 5, 6) → (2, 3, 4, 6)
result = A @ B # 结果形状: (2, 3, 4, 6)print("A形状:", A.shape) # 输出: torch.Size([2, 3, 4, 5])
print("B形状:", B.shape) # 输出: torch.Size([5, 6])
print("结果形状:", result.shape) # 输出: torch.Size([2, 3, 4, 6])
@浙大疏锦行