源自 PyTorch 1.4教程
Outline
numpy.array(object, dtype = None, copy = True, order = None, subok = False, ndmin = 0)
import torch
x=torch.rand(5,3)
print(x)
输出
tensor([[0.3277, 0.8547, 0.0091],
[0.2796, 0.6905, 0.9170],
[0.8286, 0.8237, 0.4714],
[0.6104, 0.1064, 0.5559],
[0.2203, 0.8231, 0.2608]])
import torch
x = torch.tensor([5.5, 3])
print(x)
#获取张量的形状
print(x.size())
输出
tensor([5.5000, 3.0000])
torch.Size([2])
torch.Size本质上是tuple元组,支持tuple的一切操作,元组一旦创建后不能被修改,起到保护数据的作用,可用于固定搭配的场景。
关于元组的操作可参见python元组操作详解
对张量进行操作,转化的numpy数组也随之改变
import torch
a=torch.zeros(5)
print(a)
#torch转化成numpy
b=a.numpy()
print(b)
print(a.add_(1))
print(b)
输出
tensor([0., 0., 0., 0., 0.])
[0. 0. 0. 0. 0.]
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
import numpy as np
import torch
a=np.zeros(5)
print(a)
b=torch.from_numpy(a)
print(b)
np.add(a,1,out=a)
print(a)
print(b)
输出
[0. 0. 0. 0. 0.]
tensor([0., 0., 0., 0., 0.], dtype=torch.float64)
[1. 1. 1. 1. 1.]
tensor([1., 1., 1., 1., 1.], dtype=torch.float64)
numpy中原位操作加法add即可,torch中add_
print(torch.arange(1,13,2))
#输出
tensor([ 1, 3, 5, 7, 9, 11])
print(torch.range(1,13,2))
#输出
tensor([ 1., 3., 5., 7., 9., 11., 13.])
print(torch.range(1,16,5))
#输出
tensor([ 1., 6., 11., 16.])
import torch
x=torch.rand(3,2)
y=torch.rand(3,2)
#直接输出
print(x+y)
print(torch.add(x,y))
#在原变量y上操作,y改变
print(y.add_(x))
print(y)
#支持索引操作,取第二列
print(y[:,1])
print(y.size())
#改变形状
z=y.view(6)
print(z.size())
```
输出
```
tensor([[1.4144, 0.6279],
[1.3059, 1.0928],
[0.2199, 1.0268]])
tensor([[1.4144, 0.6279],
[1.3059, 1.0928],
[0.2199, 1.0268]])
tensor([[1.4144, 0.6279],
[1.3059, 1.0928],
[0.2199, 1.0268]])
tensor([[1.4144, 0.6279],
[1.3059, 1.0928],
[0.2199, 1.0268]])
tensor([0.6279, 1.0928, 1.0268])
torch.Size([3, 2])
torch.Size([6])
x = torch.Tensor([[1], [2], [3]])
print(x.size())
y=x.reshape(1,3) #原地操作 x.reshape(1,3) 输出x.size 还是torch.Size([3, 1])
print(y.size())
print(y)
#输出
torch.Size([3, 1])
torch.Size([1, 3]) #二维
tensor([[1., 2., 3.]])
x = torch.Tensor([[1], [2], [3]])
print(x.size())
y=x.squeeze()
print(y.size())
print(y)
#输出
torch.Size([3, 1])
torch.Size([3])
tensor([1., 2., 3.]) #一维
#x为二维,unsqueeze里面的范围取值只能是0-2,包含2
#即x为n维,取值范围为0-n
x = torch.Tensor([[1], [2], [3]])
print(x.size())
y=x.unsqueeze(0)
print(y.size())
print(y)
z=x.unsqueeze(2)
print(z.size())
print(z)
#输出
torch.Size([3, 1])
torch.Size([1, 3, 1])
tensor([[[1.],
[2.],
[3.]]])
torch.Size([3, 1, 1])
tensor([[[1.]],
[[2.]],
[[3.]]])
#x为三维,permute里面的三个数只能是0,1,2,且不能重复
#即x为n维,取值范围为0-n,不包括n
x = torch.Tensor([[[1,4], [2,5], [3,6]]])
print(x.size())
y=x.permute(2,0,1)
print(y.size())
print(y)
#输出
torch.Size([1, 3, 2])
torch.Size([2, 1, 3])
tensor([[[1., 2., 3.]],
[[4., 5., 6.]]])
x = torch.Tensor([[[1,4], [2,5], [3,6]]])
print(x.size())
y=x.transpose(1,2)
print(y.size())
print(y)
#输出
torch.Size([1, 3, 2])
torch.Size([1, 2, 3])
tensor([[[1., 2., 3.],
[4., 5., 6.]]])
x = torch.Tensor([[[1,4], [2,5], [3,6]]])
print(x.size())
y=x.index_select(1,torch.tensor([1,2]))
print(y.size())
print(y)
a = torch.linspace(1, 12, steps=12).view(3, 4)
print(a)
b = torch.index_select(a, 0, torch.tensor([0, 2]))
print(b)
#输出
torch.Size([1, 3, 2])
torch.Size([1, 2, 2])
tensor([[[2., 5.],
[3., 6.]]])
tensor([[ 1., 2., 3., 4.],
[ 5., 6., 7., 8.],
[ 9., 10., 11., 12.]])
tensor([[ 1., 2., 3., 4.],
[ 9., 10., 11., 12.]])
x=torch.randn(3,4)
print(x)
print(torch.masked_select(x,x>0))
#输出
x = torch.Tensor([[[1,4], [2,5], [3,6]]])
print(x.repeat(1,2,3))
print(x.size())
#输出
tensor([[[1., 4., 1., 4., 1., 4.],
[2., 5., 2., 5., 2., 5.],
[3., 6., 3., 6., 3., 6.],
[1., 4., 1., 4., 1., 4.],
[2., 5., 2., 5., 2., 5.],
[3., 6., 3., 6., 3., 6.]]])
torch.Size([1, 3, 2])
x = torch.Tensor([[1,4], [2,5], [3,6]])
y=torch.Tensor([[1,1,0],[0,0,1],[1,1,1]])
z=torch.cat([x,y],1)
print(z)
#输出
tensor([[1., 4., 1., 1., 0.],
[2., 5., 0., 0., 1.],
[3., 6., 1., 1., 1.]])
注意在1维度拼接要保证两个张量在0维度相同
x = torch.Tensor([[1,4], [2,5], [3,6]])
y=torch.Tensor([[1,1],[0,0],[1,1]])
z=torch.stack([x,y],2)
print(z)
print(z.size())
#输出
tensor([[[1., 1.],
[4., 1.]],
[[2., 0.],
[5., 0.]],
[[3., 1.],
[6., 1.]]])
torch.Size([3, 2, 2])
x = torch.Tensor([[1], [2], [3]])
print(x.expand(3,5))
#输出
tensor([[1., 1., 1., 1., 1.],
[2., 2., 2., 2., 2.],
[3., 3., 3., 3., 3.]])
input = torch.randn(2, 4)
print(input)
output = torch.zeros(2, 4)
index = torch.tensor([[3, 1, 2, 0], [1, 2, 0, 3]])
print(index)
#input数组中的数据只是在第1维上进行重新分配,第0维不变
output = output.scatter(1, index, input)
print(output)
#input[1][0]->output[1][index[1][0]]->output[1][1]
#input[1][1]->output[1][index[1][1]]->outpou[1][2]
#输出
tensor([[ 0.6964, 0.0709, 2.2572, -0.4595],
[-0.8429, -1.5339, 2.0352, 1.2176]])
tensor([[3, 1, 2, 0],
[1, 2, 0, 3]])
tensor([[-0.4595, 0.0709, 2.2572, 0.6964],
[ 2.0352, -0.8429, -1.5339, 1.2176]])
参见pytorch中torch.Tensor.scatter用法
下节写Autograd 自动求导
欢迎批评指正,一起学习进步!!!