美文网首页
pytorch-torch2:张量计算和连接

pytorch-torch2:张量计算和连接

作者: 罗泽坤 | 来源:发表于2020-04-06 21:49 被阅读0次

索引、切片、连接、变异操作

torch.cat(seq,dim=0,out=None)->Tensor
在给定维度上对输入张量序列seq进行连接操作由这可以想到卷积神经网络
的全连接层
torch.cat()可以看做torch.split()torch.chunk()的逆运算

  • seq:任意(Tensors的序列)
  • dim:张量连接尺寸
  • out
import torch
x = torch.randn(2,3)
print(x)
tensor([[ 0.2403, -0.5606,  0.0291],
        [ 0.2671,  0.7454,  1.9962]])
y = torch.cat((x,x),dim=0)
print(y)
z = torch.cat((x,x),dim=1)
print(z)
tensor([[ 0.2403, -0.5606,  0.0291],
        [ 0.2671,  0.7454,  1.9962],
        [ 0.2403, -0.5606,  0.0291],
        [ 0.2671,  0.7454,  1.9962]])
tensor([[ 0.2403, -0.5606,  0.0291,  0.2403, -0.5606,  0.0291],
        [ 0.2671,  0.7454,  1.9962,  0.2671,  0.7454,  1.9962]])
x1 = torch.randn(3,3,3)
print(x1)
y1 = torch.cat((x1,x1),dim=2)
z1 = torch.cat((x1,x1),dim=1)
print(y1)
print(z1)
tensor([[[-0.3847,  0.9418, -0.8916],
         [ 0.5488,  1.0235,  0.2930],
         [-0.2274,  0.2622,  1.5223]],

        [[-1.7501, -0.2777, -1.1390],
         [ 1.6078, -0.9990, -1.3961],
         [ 1.4809,  1.1763, -0.3197]],

        [[-0.4526, -0.1629,  0.0279],
         [-0.9971, -0.3301,  0.1106],
         [ 1.4824,  0.2740, -0.2242]]])
tensor([[[-0.3847,  0.9418, -0.8916, -0.3847,  0.9418, -0.8916],
         [ 0.5488,  1.0235,  0.2930,  0.5488,  1.0235,  0.2930],
         [-0.2274,  0.2622,  1.5223, -0.2274,  0.2622,  1.5223]],

        [[-1.7501, -0.2777, -1.1390, -1.7501, -0.2777, -1.1390],
         [ 1.6078, -0.9990, -1.3961,  1.6078, -0.9990, -1.3961],
         [ 1.4809,  1.1763, -0.3197,  1.4809,  1.1763, -0.3197]],

        [[-0.4526, -0.1629,  0.0279, -0.4526, -0.1629,  0.0279],
         [-0.9971, -0.3301,  0.1106, -0.9971, -0.3301,  0.1106],
         [ 1.4824,  0.2740, -0.2242,  1.4824,  0.2740, -0.2242]]])
tensor([[[-0.3847,  0.9418, -0.8916],
         [ 0.5488,  1.0235,  0.2930],
         [-0.2274,  0.2622,  1.5223],
         [-0.3847,  0.9418, -0.8916],
         [ 0.5488,  1.0235,  0.2930],
         [-0.2274,  0.2622,  1.5223]],

        [[-1.7501, -0.2777, -1.1390],
         [ 1.6078, -0.9990, -1.3961],
         [ 1.4809,  1.1763, -0.3197],
         [-1.7501, -0.2777, -1.1390],
         [ 1.6078, -0.9990, -1.3961],
         [ 1.4809,  1.1763, -0.3197]],

        [[-0.4526, -0.1629,  0.0279],
         [-0.9971, -0.3301,  0.1106],
         [ 1.4824,  0.2740, -0.2242],
         [-0.4526, -0.1629,  0.0279],
         [-0.9971, -0.3301,  0.1106],
         [ 1.4824,  0.2740, -0.2242]]])

torch.chunk(tensor,chunks,dim)->>tensors
将张量沿给定维度分块

  • chunks:分块数
  • dim:指定分块维度
    我们可以利用chunk和cat,先chunk后cat就能将张量
    向量化
x = torch.randn(3,3)
a = torch.chunk(x,3,dim=0)
b = torch.chunk(x,3,dim=1)
print(a)
print(b)
print(torch.cat((a[0],a[1],a[2]),dim=1)) # 按列连接
print(torch.cat((b[0],b[1],b[2]),dim=0)) # 按行连接
(tensor([[ 0.0026, -0.4883,  0.7212]]), tensor([[-0.8090,  0.5345, -0.4566]]), tensor([[ 0.5391, -1.5702,  0.2347]]))
(tensor([[ 0.0026],
        [-0.8090],
        [ 0.5391]]), tensor([[-0.4883],
        [ 0.5345],
        [-1.5702]]), tensor([[ 0.7212],
        [-0.4566],
        [ 0.2347]]))
tensor([[ 0.0026, -0.4883,  0.7212, -0.8090,  0.5345, -0.4566,  0.5391, -1.5702,
          0.2347]])
tensor([[ 0.0026],
        [-0.8090],
        [ 0.5391],
        [-0.4883],
        [ 0.5345],
        [-1.5702],
        [ 0.7212],
        [-0.4566],
        [ 0.2347]])

torch.gather(input,dim,index,out=None,sparse_grad=Flase,out=None)->Tensor

通俗点解释就是把指定索引dim的下标进行替换

a = torch.LongTensor([[0,0],[1,2]])
t = torch.Tensor([[1,2],[3,4]])
# 将4个元素的第一个维度下标替换成0,0,1,0
print(torch.gather(t,0,torch.tensor([[0,0],[1,0]])))
# 将4个元素的第二个维度下标替换成0,0,0,1
print(torch.gather(t,1,torch.tensor([[0,0],[0,1]])))
tensor([[1., 2.],
        [3., 2.]])
tensor([[1., 1.],
        [3., 4.]])

torch.index_select(input,dim,index,out=None)->>Tensor

  • input:输入张量
  • dim(int):索引维度
  • index(LongTensor):索引下标
x = torch.randn(3,4)
print(x)
# 按行索引,2,3行
print(torch.index_select(x,0,torch.LongTensor([1,2])))
# 按列索引,2,3,4列
print(torch.index_select(x,1,torch.LongTensor([1,2,3])))
tensor([[ 0.9431,  0.3714,  2.0795,  0.3076],
        [-1.3413, -0.2518,  0.0665, -0.7127],
        [ 0.1430,  0.3192,  0.2244, -3.1105]])
tensor([[-1.3413, -0.2518,  0.0665, -0.7127],
        [ 0.1430,  0.3192,  0.2244, -3.1105]])
tensor([[ 0.3714,  2.0795,  0.3076],
        [-0.2518,  0.0665, -0.7127],
        [ 0.3192,  0.2244, -3.1105]])

torch.masked_select(input,mask,out=None)->>Tensor
根据mask输出一个一维张量

  • input:输入张量
  • mask(ByteTensor):模板只含有0,1二值张量必须与input张量维度一致
mask = torch.ByteTensor([[1,0],[0,1]])
y = torch.randn(2,2)
print(y)
print(torch.masked_select(y,mask))
tensor([[ 0.6960, -1.2918],
        [ 0.4685,  2.1284]])


..\aten\src\ATen\native\LegacyDefinitions.cpp:67: UserWarning: masked_select received a mask with dtype torch.uint8, this behavior is now deprecated,please use a mask with dtype torch.bool instead.


tensor([0.6960, 2.1284])

torch.split(tensor,split_size,dim=0)->>tensor
如果可分,张量沿着指定维度指定大小进行分割,直到大小不足则停止

  • tensor
    ch* split_size:分割尺寸
  • dim(int):分割维度
z = torch.randn(3,4)
print(z)
print(torch.split(z,1,dim=1))
print(torch.split(z,2,dim=0))
tensor([[ 1.1178,  0.8798, -0.2490,  1.3292],
        [-0.6898, -0.7655,  1.2808, -0.3745],
        [ 0.4639,  1.9754,  0.7621, -2.3465]])
(tensor([[ 1.1178],
        [-0.6898],
        [ 0.4639]]), tensor([[ 0.8798],
        [-0.7655],
        [ 1.9754]]), tensor([[-0.2490],
        [ 1.2808],
        [ 0.7621]]), tensor([[ 1.3292],
        [-0.3745],
        [-2.3465]]))
(tensor([[ 1.1178,  0.8798, -0.2490,  1.3292],
        [-0.6898, -0.7655,  1.2808, -0.3745]]), tensor([[ 0.4639,  1.9754,  0.7621, -2.3465]]))

torch.t(input,out=None)->Tensor
张量转置相当于
torch.transpose(input,o,1)

n = torch.randn(3,4)
print(n)
print(torch.t(n))
print(torch.transpose(n,0,1))
print(torch.transpose(n,1,0))
print(n)
tensor([[-0.3948,  0.6652,  0.1606,  0.6889],
        [ 0.3723,  0.5568,  1.2067,  0.3230],
        [-0.5423,  0.8979, -0.3635, -0.7440]])
tensor([[-0.3948,  0.3723, -0.5423],
        [ 0.6652,  0.5568,  0.8979],
        [ 0.1606,  1.2067, -0.3635],
        [ 0.6889,  0.3230, -0.7440]])
tensor([[-0.3948,  0.3723, -0.5423],
        [ 0.6652,  0.5568,  0.8979],
        [ 0.1606,  1.2067, -0.3635],
        [ 0.6889,  0.3230, -0.7440]])
tensor([[-0.3948,  0.3723, -0.5423],
        [ 0.6652,  0.5568,  0.8979],
        [ 0.1606,  1.2067, -0.3635],
        [ 0.6889,  0.3230, -0.7440]])
tensor([[-0.3948,  0.6652,  0.1606,  0.6889],
        [ 0.3723,  0.5568,  1.2067,  0.3230],
        [-0.5423,  0.8979, -0.3635, -0.7440]])

随机抽样

#返回原始种子
print(torch.initial_seed())
#设置种子返回一个生成器对象
print(torch.manual_seed(4))
173688027046900
<torch._C.Generator object at 0x0000022D14609090>

torch.bernoulli(input,out=None)

从伯努利分布中抽取二元随机数(0或者1)这里的bernoulli概率p是随机的
输入张量值需是一个概率

a = torch.Tensor(3,3).uniform_(0,1)
print(a)
print(torch.bernoulli(a))
tensor([[0.9744, 0.3189, 0.2148],
        [0.9263, 0.4735, 0.5949],
        [0.7956, 0.7635, 0.2137]])
tensor([[1., 1., 0.],
        [1., 0., 1.],
        [0., 1., 0.]])

torch.multinomial(input,num_samples,replacement=Flase,out=None)->>LongTensor
从输入张量中每行取num_samples个样本,可以设置replacement设置是否重复取值
返回取值的下标

c = torch.rand(3,3)
d = torch.multinomial(c,2)
print(c)
print(d)
tensor([[0.3050, 0.8421, 0.9032],
        [0.6319, 0.6535, 0.8703],
        [0.9382, 0.1838, 0.2943]])
tensor([[1, 0],
        [2, 1],
        [0, 1]])

torch.normal(means,std,out)->>tensor
按照指定均值和方差选取样本,均值个数决定样本个数
若均值和方差都为张量则两个张量元素个数必须相等

  • means(Tensor):均值
  • std(Tensor):方差
torch.normal(mean=0.5,std=torch.arange(1,0,-0.1))
#torch.normal(mean=torch.arange(1.0, 11.0), std=torch.arange(1, 0, -0.1))
tensor([ 0.9111,  0.4022, -0.4876,  0.9415,  0.4344, -0.0191,  0.6611,  0.5365,
         0.3165,  0.4264])

Math operations

torch.abs(input,out)->tensor 输出张量元素绝对值

torch.acos(input,out) 求反余弦

torch.add(input,value,out) 对每个张量元素逐个加上value

torch.addcdiv(tensor,value=1,tensor1,tensor2) 张量(tensor1/tensor2)*value+tensor

torch.addmul 相乘相加

torch.ceil(input,out) 向上取整

torch.clamp(input,min,max,out=None)
将元素调整至[min,max]区间

torch.div(input,value)

torch.exp(tensor,out) 指数

torch.floor(input,out) 向下去整

torch.fmod(input,divisor,out) 取余数

torch.frac 取分数部分

torch.lerp(start, end, weight, out=None)
线性插值:out = start+weight*(end-start)

torch.log 取自然对数

torch.mul(input, value, out=None)

torch.mul(input, other, out=None) 哈达玛积

torch.neg 取复数

torch.pow(input, exponent, out=None) 求幂

torch.reciprocal(input, out=None) → Tensor 去倒数

torch.remainder(input, divisor, out=None) → Tensor 取余数

torch.rsqrt(input, out=None) → Tensor 平方根倒数

torch.sigmoid(input, out=None) → Tensor sigmoid值

torch.sigmoid(input, out=None) → Tensor 符号函数

torch.cumprod(input, dim, out=None) → Tensor 按指定维度累积

torch.cumsum(input, dim, out=None) → Tensor 指定维度累加

torch.dist(input, other, p=2, out=None) → Tensor 求P范数

torch.mean(input) → float 均值

torch.mean(input, dim, out=None) → Tensor 指定维度均值

torch.median(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor) 指定维度中位数

torch.mode(input, dim=-1, values=None, indices=None) -> (Tensor, LongTensor) 众数

torch.norm(input, p, dim, out=None) → Tensor 指定维度p范数

torch.prod(input) → float 所有积

torch.prod(input, dim, out=None) → Tensor 指定维度积

torch.std(input, dim, out=None) → Tensor 标准差

torch.sum(input, dim, out=None) → Tensor 按维度求和

torch.sum(input) → float 所有元素和

var 按行方差,所有元素方差

torch.eq(input, other, out=None) → Tensor 相等比较操作 返回01

torch.equal(tensor1, tensor2) → bool 张量比较shape and value返回bool

torch.ge(input, other, out=None) → Tensor 大于

torch.gt(input, other, out=None) → Tensor 与equal类似返回不同

torch.kthvalue(input, k, dim=None, out=None) -> (Tensor, LongTensor) 取指定维度最小值

torch.le(input, other, out=None) → Tensor 小于等于

torch.lt(input, other, out=None) → Tensor 小于

torch.max(input, dim, max=None, max_indices=None) -> (Tensor, LongTensor) 返回指定维度最大值和索引

x = torch.linspace(1,10,10)
print(x)
print(torch.clamp(x,1,5,))
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
tensor([1., 2., 3., 4., 5., 5., 5., 5., 5., 5.])

相关文章

网友评论

      本文标题:pytorch-torch2:张量计算和连接

      本文链接:https://www.haomeiwen.com/subject/tsiiphtx.html