1、torch.is_tensor(obj) 如果obj 是一个pytorch张量,则返回True
1、torch.eye
torch.eye(n, m=None, out=None)
返回一个2维张量,对角线位置全1,其它位置全0
参数:
n (int ) – 行数
m (int, optional) – 列数
------------------------------
>>> torch.eye(3)
1 0 0
0 1 0
0 0 1
[torch.FloatTensor of size 3x3]
2、torch.ones
torch.ones(*sizes) → Tensor
返回一个全为1 的张量,形状由可变参数sizes定义
参数:
sizes (int...) – 整数序列,定义了输出形状
>>> torch.ones(2, 3)
1 1 1
1 1 1
3、torch.zeros
torch.zeros(*sizes) → Tensor
返回一个全为标量 0 的张量,形状由可变参数sizes 定义
>>> torch.zeros(2, 3)
0 0 0
0 0 0
4、from_numpy 将numpy转为tensor
torch.from_numpy(ndarray) → Tensor
返回的张量tensor和numpy的ndarray共享同一内存空间。修改一个会导致另外一个也被修改。返回的张量不能改变大小
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
torch.LongTensor([1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
5、torch.rand
torch.rand(*sizes) → Tensor
返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。
参数:
sizes (int...) – 整数序列,定义了输出形状
>>> torch.rand(2, 3)
0.5010 0.5140 0.0719
0.1435 0.5636 0.0538
[torch.FloatTensor of size 2x3]
6、torch.randn
torch.randn(*sizes) → Tensor
返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数,形状由可变参数sizes定义。 参数:
sizes (int...) – 整数序列,定义了输出形状
>>> torch.randn(2, 3)
1.4339 0.3351 -1.0999
1.5458 -0.9643 -0.3558
[torch.FloatTensor of size 2x3]
7、torch.linspace
torch.linspace(start, end, steps=100) → Tensor
返回一个1维张量,包含在区间start 和 end 上均匀间隔的steps个点
steps代表点数
参数:
start (float) – 序列的起始点
end (float) – 序列的最终值
steps (int) – 在start 和 end间生成的样本数
>>> torch.linspace(3, 10, steps=5)
3.0000
4.7500
6.5000
8.2500
10.0000
>>> torch.linspace(-10, 10, steps=5)
-10
-5
0
5
10
8、torch.arange
torch.arange(start, end, step=1) → Tensor
最大的数不能到达end
参数:
start (float) – 序列的起始点
end (float) – 序列的终止点
step (float) – 相邻点的间隔大小
>>>torch.arange(1, 2.5, 0.5)
1.0000
1.5000
2.0000
9、torch.range
torch.range(start, end, step=1) → Tensor
最大数可以到达end
参数:
start (float) – 序列的起始点
end (float) – 序列的最终值
step (int) – 相邻点的间隔大小
>>>torch.arange(1, 2.5, 0.5)
1.0000
1.5000
2.0000
2.5000
Pytorch:Tensor的合并与分割
torch.cat
torch.cat(inputs, dimension=0) → Tensor
dimension=0代表纵向连接 1代表横向连接
在给定维度上的两个张量进行合并
import torch
a=torch.randn(3,4) #随机生成一个shape(3,4)的tensort
[-0.0374, 0.1124, 0.1885, -0.1590],
[-0.4492, -0.3023, -0.8956, -0.8201],
[-0.2309, -0.3394, -0.0331, -0.1117
b=torch.randn(2,4) #随机生成一个shape(2,4)的tensor
[-0.6457, -1.5489, 1.5090, -1.3025],
[-0.6313, 0.3443, -0.4425, 1.5784]
torch.cat([a,b],dim=0)
[-0.0374, 0.1124, 0.1885, -0.1590],
[-0.4492, -0.3023, -0.8956, -0.8201],
[-0.2309, -0.3394, -0.0331, -0.1117],
[-0.6457, -1.5489, 1.5090, -1.3025],
[-0.6313, 0.3443, -0.4425, 1.5784]
#返回一个shape(5,4)的tensor
#把a和b拼接成一个shape(5,4)的tensor,
#可理解为沿着行增加的方向(即纵向)拼接
torch.stack
torch.stack(sequence, dim=0)
stack会增加一个新的维度,来表示拼接后的2个tensor,直观些理解的话,咱们不妨把一个2维的tensor理解成一张长方形的纸张,cat相当于是把两张纸缝合在一起,形成一张更大的纸,而stack相当于是把两张纸上下堆叠在一起
a=torch.randn(3,4)
-0.8448, 1.5477, -1.7584, -0.4371],
[ 0.1829, 0.4582, 1.3154, -1.0610],
[-0.1592, 0.5949, -0.0938, 0.3003]
b=torch.randn(3,4)
0.5147, 0.4190, -1.2803, 1.7544],
[-0.9126, -1.4551, 0.3540, -1.1717],
[ 0.3529, -0.5235, 0.1321, -0.7094
c=torch.stack([a,b],dim=0)
#返回一个shape(2,3,4)的tensor,新增的维度2分别指向a和b
c[0]= -0.8448, 1.5477, -1.7584, -0.4371],
[ 0.1829, 0.4582, 1.3154, -1.0610],
[-0.1592, 0.5949, -0.0938, 0.3003]
c[1]= 0.5147, 0.4190, -1.2803, 1.7544],
[-0.9126, -1.4551, 0.3540, -1.1717],
[ 0.3529, -0.5235, 0.1321, -0.7094
--------------------------------------------
--------------------------------------------
分割线
a=torch.randn(3,4)
0.2800, -0.8204, 0.0585, -1.2520],
[ 0.4567, 1.0563, 0.4361, -1.8747],
[-1.1983, -1.5724, 0.1776, 0.8497]
b=torch.randn(3,4)
1.5018, 1.9686, -0.0022, -0.0087],
[-0.4536, 0.2231, 0.3957, 0.0952],
[-0.6697, 1.2973, 0.2123, -0.6697]
d=torch.stack([a,b],dim=1)
#返回一个shape(3,2,4)的tensor,新增的维度2分别指向相应的a的第i行和b的第i行
#c[0]很明显是取了a的第0行(a[0])和b的第0行(b[0])
#c[0]=[a[0]
b[0]]
c[0]= 0.2800, -0.8204, 0.0585, -1.2520],
[ 1.5018, 1.9686, -0.0022, -0.0087]
#c[1]=[a[1]
b[1]]
c[1]= 0.2800, -0.8204, 0.0585, -1.2520],
[ 1.5018, 1.9686, -0.0022, -0.0087]
c[2]= -1.1983, -1.5724, 0.1776, 0.8497],
[-0.6697, 1.2973, 0.2123, -0.6697]
torch.chunk
torch.chunk(tensor, chunks, dim=0)
在给定维度(轴)上将输入张量进行分块
参数:
tensor (Tensor) – 待分块的输入张量
chunks (int) – 分块的个数
dim (int) – 沿着此维度进行分块
a=torch.randn(4,6)
#返回一个shape(2,6)的tensor
b=a.chunk(2,dim=0)
#返回一个shape(4,3)的tensor
c=a.chunk(2,dim=1)
torch.split
torch.split(tensor, split_size, dim=0)
根据长度去拆分tensor
a=torch.randn(3,4)
[ 0.7237, 0.0750, -2.3344, 0.1829],
[ 0.0650, 0.1460, 0.1325, -1.9555],
[ 0.8884, 1.8103, 0.1683, -0.1529]
b=a.split([1,2],dim=0)
1+2=3
#把维度0按照长度[1,2]拆分,形成2个tensor,
#shape(1,4)和shape(2,4)
[ 0.7237, 0.0750, -2.3344, 0.1829]
--------------------------------------
[[ 0.0650, 0.1460, 0.1325, -1.9555],
[ 0.8884, 1.8103, 0.1683, -0.1529]]
c=a.split([2,2],dim=1)
2+2=4
#把维度1按照长度[2,2]拆分,形成2个tensor,
#shape(3,2)和shape(3,2)
[[0.7237, 0.0750],
[0.0650, 0.1460],
[0.8884, 1.8103]]
-------------------------------------
[[-2.3344, 0.1829],
[ 0.1325, -1.9555],
[ 0.1683, -0.1529]]
torch.transpose
torch.transpose(input, dim0, dim1) → Tensor
返回输入矩阵input的转置。交换维度dim0和dim1。 输出张量与输入张量共享内存,所以改变其中一个会导致另外一个也被修改。
参数:
input (Tensor) – 输入张量
dim0 (int) – 转置的第一维
dim1 (int) – 转置的第二维
>>> x = torch.randn(2, 3)
>>> x
0.5983 -0.0341 2.4918
1.5981 -0.5265 -0.8735
>>> torch.transpose(x, 0, 1)
0.5983 1.5981
-0.0341 -0.5265
2.4918 -0.8735
torch.permute(a,b,c,d, …):可以对任意高维矩阵进行转置
torch.squeeze
torch.squeeze(input, dim=None, out=None)
将输入张量形状中的1 去除并返回。 如果输入是形如(A×1×B×1×C×1×D)
,那么输出形状就为: (A×B×C×D)
当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: (A×1×B)
, squeeze(input, 0) 将会保持张量不变,只有用 squeeze(input, 1),形状会变成 (A×B)
注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。
torch.unsqueeze
torch.unsqueeze(input, dim, out=None)
返回一个新的张量,对输入的制定位置插入维度 1
注意: 返回张量与输入张量共享内存,所以改变其中一个的内容会改变另一个。
torch.Tensor.expand expand函数用于将张量中单数维的数据扩展到指定的size。 首先解释下什么叫单数维(singleton dimensions),张量在某个维度上的size为1,则称为单数维。比如zeros(2,3,4)不存在单数维,而zeros(2,1,4)在第二个维度(即维度1)上为单数维。expand函数仅仅能作用于这些单数维的维度上。
输入参数是想要扩张的张量size
a=torch.tensor([[2],[3],[4]])
2
3
4
想要扩展成(3,2)
b=a.expand(3,2)
2, 2],
[3, 3],
[4, 4]
torch.Tensor.expand_as
torch.expand_as()的输入参数为一个张量
作用是将输入tensor的维度扩展为与指定tensor相同的size
a = torch.tensor([[2], [3], [4]])
b = torch.tensor([[2, 2], [3, 3], [5, 5]])
c = a.expand_as(b)
c的维度大小为(3,2)
[2, 2],
[3, 3],
[4, 4]]
torch.Tensor.repeat 参考【Pytorch】对比expand和repeat函数 前文提及expand仅能作用于单数维,那对于非单数维的拓展,那就需要借助于repeat函数了
torch.Tensor.repeat(*sizes)
参数*sizes指定了原始张量在各维度上复制的次数
import torch
a = torch.tensor([1, 0, 2])
b = a.repeat(4,2)
[1, 0, 2, 1, 0, 2],
[1, 0, 2, 1, 0, 2],
[1, 0, 2, 1, 0, 2],
[1, 0, 2, 1, 0, 2]
在行上有4个1, 0, 2
在列上有2个1, 0, 2
区别:expand只能在行上或者列上进行复制
repeat能在行上和列上同时进行复制
torch.contiguous 参考Pytorch中的permute函数和transpose,contiguous,view函数的关联
contiguous函数起什么作用呢?
当我们在使用transpose或者permute函数之后,tensor数据将会变的不在连续,而此时,如果我们采用view函数等需要tensor数据联系的函数时,将会抛出以下错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 2: view size is not compatible
with input tensor's size and stride (at least one dimension spans
across two contiguous subspaces). Call .contiguous() before
.view(). at ..\aten\src\TH/generic/THTensor.cpp:203
如果这是使用了contiguous函数,将会解决此错误
torch.Tensor.view
用于改变tensor的形状
x = torch.randn(2, 6)
-0.4840, 0.2860, -1.2218, -0.0474, -1.2809, -0.0823],
[-0.2656, -0.5796, -1.1803, 0.2565, -0.2999, -0.6816
y = x.view(3,4)
-0.4840, 0.2860, -1.2218, -0.0474],
[-1.2809, -0.0823, -0.2656, -0.5796],
[-1.1803, 0.2565, -0.2999, -0.6816
z = x.view(-1, 12) # the size -1 is inferred from other dimensions
[ -0.4840, 0.2860, -1.2218, -0.0474, -1.2809, -0.0823,-0.2656, -0.5796, -1.1803, 0.2565, -0.2999, -0.6816 ]
torch.reshape() 用于改变tensor的形状 功能上和view一样 不同点:view()方法只能改变连续的(contiguous)张量,否则需要先调用.contiguous()方法,而.reshape()方法不受此限制
torch.mean() 参考torch.mean()