python|3-张量API-下


文章目录

  • 1. torch.take
  • 2. torch.tile
  • 3. torch.transpose
  • 4. torch.unblind
  • 5. torch.unsqueeze
  • 6. torch.where
  • 7. torch.rand&torch.randn
  • 8. torch.manual_seed
  • 9. torch.bernoulli
  • 10. torch.normal
  • 11. torch.randint
  • 12. torch.randperm
【python|3-张量API-下】
1. torch.take
torch.take(input, index) → Tensor

返回一个新的张量,其输入元素为给定指标。输入张量被看成是一维张量。结果与指标的形状相同。
分两步:
  • 将输入input展开成一个一维张量
  • 根据index序号进行索引input里面的值
import torch input = torch.tensor([[4, 3, 5], [6, 7, 8]]) index = torch.tensor([0, 2, 5]) output = torch.take(input,index) print(f"input={input}") # input=tensor([[4, 3, 5], #[6, 7, 8]]) print(f"index={index}") # index=tensor([0, 2, 5]) print(f"output={output}") # output=tensor([4, 5, 8])

2. torch.tile
torch.tile(input, dims) → Tensor

通过重复输入的元素构造一个张量。dims参数指定每个维度的重复次数
import torchx = torch.tensor([1,2,3])# 将x的行复制2倍,列复制3倍 x_tile = x.tile((2,3)) print(f"x={x}") # x=tensor([1, 2, 3]) print(f"x_tile={x_tile}") # x_tile=tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3], #[1, 2, 3, 1, 2, 3, 1, 2, 3]])

3. torch.transpose
torch.transpose(input, dim0, dim1) → Tensor

返回一个张量,它是输入的转置版本。给定尺寸的dim0和dim1交换。
import torch x = torch.ones(2,3,4) # 将第0维和第1维互换; (2,3,4) -> (3,2,4) x_transpose_0_1 = torch.transpose(x,0,1) # 将第0维和第2维互换; (2,3,4) -> (4,3,2) x_transpose_0_2 = torch.transpose(x,0,2) # 将第1维和第2维互换; (2,3,4) -> (2,4,3) x_transpose_1_2 = torch.transpose(x,1,2) print(f"x.shape={x.shape}") print(f"x_transpose_0_1.shape={x_transpose_0_1.shape}") print(f"x_transpose_0_2.shape={x_transpose_0_2.shape}") print(f"x_transpose_1_2.shape={x_transpose_1_2.shape}")

x.shape=torch.Size([2, 3, 4]) x_transpose_0_1.shape=torch.Size([3, 2, 4]) x_transpose_0_2.shape=torch.Size([4, 3, 2]) x_transpose_1_2.shape=torch.Size([2, 4, 3])

4. torch.unblind
torch.unbind(input, dim=0) → seq

将输入的张量删除指定的维度;比如输入大小为(2,3,4)
  • unblind; dim=0 得到(3,4); (3,4)
  • unblind; dim=1 得到(2,4); (2,4); (2,4)
  • unblind; dim=2 得到(2,3); (2,3); (2,3); (2,3)
import torchx = torch.arange(24).reshape(2,3,4) x_unbind_0 = torch.unbind(x,dim=0) x_unbind_1 = torch.unbind(x,dim=1) x_unbind_2 = torch.unbind(x,dim=2) print(f"x={x}") print(f"x_unbind_0={x_unbind_0}") print(f"x_unbind_0[0].shape={x_unbind_0[0].shape}") print(f"x_unbind_0[1].shape={x_unbind_0[1].shape}") print(f"x_unbind_1={x_unbind_1}") print(f"x_unbind_1[0].shape={x_unbind_1[0].shape}") print(f"x_unbind_1[1].shape={x_unbind_1[1].shape}") print(f"x_unbind_1[2].shape={x_unbind_1[2].shape}") print(f"x_unbind_2={x_unbind_2}") print(f"x_unbind_2[0].shape={x_unbind_2[0].shape}") print(f"x_unbind_2[1].shape={x_unbind_2[1].shape}") print(f"x_unbind_2[2].shape={x_unbind_2[2].shape}") print(f"x_unbind_2[3].shape={x_unbind_2[3].shape}")

x=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]]]) x_unbind_0=(tensor([[ 0,1,2,3], [ 4,5,6,7], [ 8,9, 10, 11]]), tensor([[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]])) x_unbind_0[0].shape=torch.Size([3, 4]) x_unbind_0[1].shape=torch.Size([3, 4]) x_unbind_1=(tensor([[ 0,1,2,3], [12, 13, 14, 15]]), tensor([[ 4,5,6,7], [16, 17, 18, 19]]), tensor([[ 8,9, 10, 11], [20, 21, 22, 23]])) x_unbind_1[0].shape=torch.Size([2, 4]) x_unbind_1[1].shape=torch.Size([2, 4]) x_unbind_1[2].shape=torch.Size([2, 4]) x_unbind_2=(tensor([[ 0,4,8], [12, 16, 20]]), tensor([[ 1,5,9], [13, 17, 21]]), tensor([[ 2,6, 10], [14, 18, 22]]), tensor([[ 3,7, 11], [15, 19, 23]])) x_unbind_2[0].shape=torch.Size([2, 3]) x_unbind_2[1].shape=torch.Size([2, 3]) x_unbind_2[2].shape=torch.Size([2, 3]) x_unbind_2[3].shape=torch.Size([2, 3])

5. torch.unsqueeze
torch.unsqueeze(input, dim) → Tensor

将大小为1的维度插入到指定的输入input张量中
import torchinput = torch.arange(24).reshape(2, 3, 4) input_unsqueeze_0 = torch.unsqueeze(input, dim=0) input_unsqueeze_1 = torch.unsqueeze(input, dim=1) input_unsqueeze_2 = torch.unsqueeze(input, dim=2) print(f"input.shape={input.shape}") # input.shape=torch.Size([2, 3, 4]) print(f"input_unsqueeze_0.shape={input_unsqueeze_0.shape}") # input_unsqueeze_0.shape=torch.Size([1, 2, 3, 4]) print(f"input_unsqueeze_1.shape={input_unsqueeze_1.shape}") # input_unsqueeze_1.shape=torch.Size([2, 1, 3, 4]) print(f"input_unsqueeze_2.shape={input_unsqueeze_2.shape}") # input_unsqueeze_2.shape=torch.Size([2, 3, 1, 4])

6. torch.where
torch.where(condition, x, y) → Tensor

根据条件condition 来选择x,y ;condition 成立选择x,condition不成立,选择y
import torch # 从正太分布中抽取数据组成3行4列矩阵 x = torch.randn(3, 4) # 创建一个全为1的3行4列矩阵 y = torch.ones(3, 4) # 如果x中的元素大于0,那么保留,如果小于等于0则用1替换 # 起到一个mask掩码的作用 # 作用:将x中所有的负数用1来填充 z = torch.where(x > 0, x, y) print(f"x={x}") print(f"y={y}") print(f"z={z}")

x=tensor([[ 1.8641,1.5247,1.2949,0.1723], [ 0.3793, -0.4579,0.0565, -0.8108], [-0.5820,0.1716,0.5962, -0.3010]]) y=tensor([[1., 1., 1., 1.], [1., 1., 1., 1.], [1., 1., 1., 1.]]) z=tensor([[1.8641, 1.5247, 1.2949, 0.1723], [0.3793, 1.0000, 0.0565, 1.0000], [1.0000, 0.1716, 0.5962, 1.0000]])

7. torch.rand&torch.randn
  • torch.rand:返回一个张量,里面填满了均匀分布在区间[0,1)[0,1)上的随机数。
  • torch.randn : 返回一个张量,里面填满了均值为0、方差为1的正态分布(也称为标准正态分布)中的随机数。
# 创建一个张量,张量的元素从均匀分布[0,1)中采样 x = torch.rand(3, 4) # 创建一个张量,张量的元素从正太分布N(0,1)中采样 y = torch.randn(3, 4) print(f"x={x}") print(f"y={y}") # x=tensor([[0.0086, 0.5198, 0.0839, 0.4737], #[0.2102, 0.9172, 0.5795, 0.3595], #[0.0384, 0.4539, 0.5219, 0.1834]]) # y=tensor([[ 1.2129, -0.2365,1.3958, -1.3845], #[-0.9289, -0.3948, -0.6431,0.4673], #[ 0.4783, -0.0453, -1.8524,1.1195]])

8. torch.manual_seed 设置生成随机数的种子。返回一个tensor,生成器对象。为了论文复现,经常要设置固定随机种子;
torch.manual_seed(seed)

9. torch.bernoulli 从伯努利分布中绘制二进制随机数(0或1)。基于输入的张量的概率生成0或1;
# 创建一个3X3的张量,用均匀[0,1]分布填充元素值,其值表示为概率大小 input_probablity = torch.empty(3, 3).uniform_(0, 1) print(f"input_probablity={input_probablity}") # 以输入的概率值生成0或1值 output_bernoulli = torch.bernoulli(input_probablity) print(f"output_bernoulli={output_bernoulli}")

input_probablity=tensor([[0.9762, 0.5216, 0.8038], [0.8500, 0.3650, 0.5082], [0.6399, 0.1677, 0.4346]]) output_bernoulli=tensor([[1., 1., 1.], [1., 0., 0.], [1., 1., 1.]])

10. torch.normal
torch.normal(mean, std, *, generator=None, out=None) → Tensor

  • 注:可以指定mean均值,std方差
    返回一个由不同正态分布的随机数组成的张量,其均值和标准差已给出。
# 从一个正太分布中采样,均值为2,方差为3,采样的元素组成3行4列矩阵 output_normal = torch.normal(2,3,size=(3,4)) print(f"output_normal={output_normal}")

output_normal=tensor([[-2.2242,4.8522,0.9539, -0.9935], [ 3.3374, -1.2745, -0.0622,0.5054], [ 7.7920,3.4281, -1.5371,1.3780]])

11. torch.randint 返回一个张量,张量由低(包含)和高(不包含)之间均匀生成的随机整数填充。
# 从[3,8)中随机抽取整数填充为3行4列矩阵 output_randint = torch.randint(3,8,size=(3,4)) print(f"output_randint={output_randint}") #output_randint=tensor([[4, 7, 7, 4], #[7, 3, 6, 5], #[6, 3, 7, 7]])

12. torch.randperm 返回从0到n - 1的整数的随机排列。
# 创建一个一维张量[0,1,..,11]并随机打乱里面元素 output_randperm = torch.randperm(12) print(f"output_randperm={output_randperm}") # output_randperm=tensor([ 6,1, 10, 11,5,9,8,2,7,3,0,4])

    推荐阅读