← 返回首页
in-place的概念和广播机制
发表时间:2024-01-22 08:07:14
in-place的概念和广播机制

in-place的概念和广播机制。

1.in-place概念

in-place就是“就地”操作,既不允许使用临时变量,也称为原位操作。在pytorch中以下划线结尾的函数都是in-place操作,比如:add_, sub_, sqrt_ 等等。

2.广播机制

PyTorch也支持广播机制(broadcasting),其条件和 NumPy 的广播机制条件基本相同。具体来说,PyTorch的广播机制条件如下:

  1. 每个tensor都最起码有1维;
  2. 从后往前数(右对齐),两个tensor的纬度要么相等,要么其中一个是1,要么其中一个不存在;

注意:其中一个维度不存在,其本质是填充1。

实例:

import torch

a = torch.rand(2, 3)
b = torch.rand(3)

c = a + b
print(a)
print(b)
print(c)
print(c.shape)

print("----------------")
a = torch.rand(2, 1, 1, 3)
b = torch.rand(4, 2, 3)

print(a)
print(b)
print(c)
print(c.shape)

运行结果:

tensor([[0.4833, 0.3292, 0.1739],
        [0.2008, 0.1520, 0.0281]])
tensor([0.5991, 0.2582, 0.4218])
tensor([[1.0824, 0.5874, 0.5957],
        [0.7999, 0.4103, 0.4500]])
torch.Size([2, 3])
----------------
tensor([[[[0.9055, 0.4813, 0.0120]]],


        [[[0.6923, 0.7887, 0.8543]]]])
tensor([[[0.2827, 0.9406, 0.7365],
         [0.1974, 0.9283, 0.7740]],

        [[0.1252, 0.2627, 0.2651],
         [0.5452, 0.6899, 0.1529]],

        [[0.6063, 0.9572, 0.9244],
         [0.5382, 0.9868, 0.2223]],

        [[0.3477, 0.0734, 0.8235],
         [0.6516, 0.4204, 0.1022]]])
tensor([[1.0824, 0.5874, 0.5957],
        [0.7999, 0.4103, 0.4500]])
torch.Size([2, 3])