网站插件 wordpress,长春老火车站,钉钉付费版多少钱,WordPress zend 乱码在 PyTorch 中#xff0c;张量#xff08;tensor#xff09;运算是核心操作之一#xff0c;PyTorch 提供了丰富的函数来进行张量运算#xff0c;包括数学运算、线性代数、索引操作等。以下是常见的张量运算函数及其用途#xff1a;
1. 基本数学运算 加法运算#xff1a… 在 PyTorch 中张量tensor运算是核心操作之一PyTorch 提供了丰富的函数来进行张量运算包括数学运算、线性代数、索引操作等。以下是常见的张量运算函数及其用途
1. 基本数学运算 加法运算torch.add(a, b) 或者直接使用 a torch.tensor([1, 2])
b torch.tensor([3, 4])
c torch.add(a, b) # [4, 6]
# 或者
c a b 减法运算torch.sub(a, b) 或者直接使用 - c torch.sub(a, b) # [-2, -2]
# 或者
c a - b 乘法运算逐元素torch.mul(a, b) 或者直接使用 * c torch.mul(a, b) # [3, 8]
# 或者
c a * b 除法运算逐元素torch.div(a, b) 或者直接使用 / c torch.div(a, b) # [0.3333, 0.5]
# 或者
c a / b 指数运算torch.pow(a, b) 或者 a ** b c torch.pow(a, b) # a^b - [1^3, 2^4] [1, 16]
# 或者
c a ** b 求幂函数torch.sqrt(a)torch.exp(a)torch.log(a) c torch.sqrt(torch.tensor([4.0, 9.0])) # [2.0, 3.0]
d torch.exp(torch.tensor([1.0, 2.0])) # e^1, e^2
e torch.log(torch.tensor([1.0, 2.0])) # log(1), log(2)
2. 聚合操作 求和torch.sum(tensor, dimNone) a torch.tensor([[1, 2], [3, 4]])
total_sum torch.sum(a) # 全局求和: 10
row_sum torch.sum(a, dim1) # 对每一行求和: [3, 7] 求平均值torch.mean(tensor, dimNone) avg torch.mean(a.float()) # 平均值: 2.5 最大值torch.max(tensor) 或 torch.max(tensor, dim) max_val torch.max(a) # 最大值: 4
max_val_row, idx torch.max(a, dim1) # 每一行的最大值: [2, 4] 最小值torch.min(tensor) 或 torch.min(tensor, dim) min_val torch.min(a) # 最小值: 1 标准差torch.std(tensor) std torch.std(a.float()) # 标准差
3. 线性代数运算 矩阵乘法torch.mm(a, b) 或者使用 a torch.tensor([[1, 2], [3, 4]])
b torch.tensor([[5, 6], [7, 8]])
c torch.mm(a, b) # 矩阵乘法
# 或者
c a b 矩阵转置torch.t(tensor) 或者使用 .T a_t torch.t(a) # 转置
# 或者
a_t a.T 矩阵求逆torch.inverse(tensor) a torch.tensor([[1.0, 2.0], [3.0, 4.0]])
a_inv torch.inverse(a) # 求矩阵的逆 行列式torch.det(tensor) det torch.det(a) # 计算行列式 特征值和特征向量torch.eig(tensor, eigenvectorsTrue) e_vals, e_vecs torch.eig(a, eigenvectorsTrue) # 计算特征值和特征向量
4. 张量形状操作 张量重塑torch.reshape(tensor, new_shape) 或 tensor.view(new_shape) a torch.tensor([[1, 2], [3, 4], [5, 6]])
reshaped torch.reshape(a, (2, 3)) # 改变形状为 (2, 3) 张量扩展torch.unsqueeze(tensor, dim)torch.squeeze(tensor, dim) a torch.tensor([1, 2, 3])
unsqueezed torch.unsqueeze(a, 0) # 在第0维添加一个新维度 - [[1, 2, 3]]
squeezed torch.squeeze(unsqueezed) # 移除维度 - [1, 2, 3] 拼接张量torch.cat(tensors, dim) 或 torch.stack(tensors, dim) a torch.tensor([1, 2])
b torch.tensor([3, 4])
concatenated torch.cat((a, b), dim0) # 拼接 - [1, 2, 3, 4]stacked torch.stack((a, b), dim0) # 堆叠 - [[1, 2], [3, 4]]
5. 索引操作 通过索引选择元素tensor[index] a torch.tensor([[1, 2], [3, 4], [5, 6]])
selected a[0, 1] # 选择第0行第1列的元素 - 2 高级索引tensor[range]、布尔索引等 a torch.tensor([1, 2, 3, 4, 5])
selected a[a 3] # 选择大于3的元素 - [4, 5]
6. 随机数生成 均匀分布随机数torch.rand(size) random_tensor torch.rand(3, 3) # 生成一个 3x3 的均匀分布随机张量 正态分布随机数torch.randn(size) normal_random torch.randn(3, 3) # 生成一个 3x3 的正态分布随机张量 指定范围的整数随机数torch.randint(low, high, size) randint_tensor torch.randint(0, 10, (3, 3)) # 生成 0 到 10 之间的随机整数
7. 广播机制 广播运算当张量的形状不同但维度兼容时PyTorch 会自动应用广播机制扩展张量。 a torch.tensor([1, 2, 3])
b torch.tensor([[1], [2], [3]])
c a b # 广播操作
8. 自动微分 启用自动微分requires_gradTrue x torch.tensor(2.0, requires_gradTrue)
y x ** 2
y.backward() # 计算梯度
print(x.grad) # 输出: 4.0
总结
PyTorch 中的张量运算函数非常丰富从基本的数学运算到高级的线性代数操作、形状调整和随机数生成这些函数让张量的处理非常灵活和高效。通过这些运算你可以实现各种数值计算和深度学习模型的训练。