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}")
print(f"index={index}")
print(f"output={output}")
2. torch.tile
torch.tile(input, dims) → Tensor
通过重复输入的元素构造一个张量。dims参数指定每个维度的重复次数
import torch
x = torch.tensor([1,2,3])
x_tile = x.tile((2,3))
print(f"x={x}")
print(f"x_tile={x_tile}")
3. torch.transpose
torch.transpose(input, dim0, dim1) → Tensor
返回一个张量,它是输入的转置版本。给定尺寸的dim0和dim1交换。
import torch
x = torch.ones(2,3,4)
x_transpose_0_1 = torch.transpose(x,0,1)
x_transpose_0_2 = torch.transpose(x,0,2)
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 torch
x = 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 torch
input = 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}")
print(f"input_unsqueeze_0.shape={input_unsqueeze_0.shape}")
print(f"input_unsqueeze_1.shape={input_unsqueeze_1.shape}")
print(f"input_unsqueeze_2.shape={input_unsqueeze_2.shape}")
6. torch.where
torch.where(condition, x, y) → Tensor
根据条件condition 来选择x,y ;condition 成立选择x,condition不成立,选择y
import torch
x = torch.randn(3, 4)
y = torch.ones(3, 4)
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的正态分布(也称为标准正态分布)中的随机数。
x = torch.rand(3, 4)
y = torch.randn(3, 4)
print(f"x={x}")
print(f"y={y}")
8. torch.manual_seed
设置生成随机数的种子。返回一个tensor,生成器对象。为了论文复现,经常要设置固定随机种子;
torch.manual_seed(seed)
9. torch.bernoulli
从伯努利分布中绘制二进制随机数(0或1)。基于输入的张量的概率生成0或1;
input_probablity = torch.empty(3, 3).uniform_(0, 1)
print(f"input_probablity={input_probablity}")
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方差
返回一个由不同正态分布的随机数组成的张量,其均值和标准差已给出。
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
返回一个张量,张量由低(包含)和高(不包含)之间均匀生成的随机整数填充。
output_randint = torch.randint(3,8,size=(3,4))
print(f"output_randint={output_randint}")
12. torch.randperm
返回从0到n - 1的整数的随机排列。
output_randperm = torch.randperm(12)
print(f"output_randperm={output_randperm}")
|