torch
- torch.randn(1,3)
返回一个1×3的tensor - torch.Tensor([[2],[1],[3]])
返回一个3×1的tensor - torch.sum(inputs,dim)
dim意味保留该维度
#返回所有值之和
torch.sum(a)
#按列求和
torch.sum(a,0)
#按行求和
torch.sum(a,1)
x = torch.randn(4, 5)
print(x)
print(x.sum(0)) #按列求和
print(x.sum(1)) #按行求和
print(torch.sum(x)) #求总和
print(torch.sum(x, 0))#按列求和
print(torch.sum(x, 1))#按行求和
tensor([[ 0.2210, 1.8035, 0.7671, -0.1836, -0.2794],
[-0.7922, -1.0881, -2.0180, 1.0981, 0.2320],
[-0.4681, 0.1820, 0.0502, 0.0067, 1.3218],
[ 0.4785, 1.0799, 1.6197, 0.6642, 0.6915]])
tensor([-0.5608, 1.9773, 0.4190, 1.5854, 1.9660])
tensor([ 2.3287, -2.5682, 1.0926, 4.5338])
tensor(5.3868)
tensor([-0.5608, 1.9773, 0.4190, 1.5854, 1.9660])
tensor([ 2.3287, -2.5682, 1.0926, 4.5338])
- torch.squeeze()
降维,去掉维数为1的维度,注意,只能去掉维数为1的维度
比如a的shape[A1B]
只能torch.squeeze(a,1)/torch.squeeze(a)==>shape[A*B]
因为1在第2维也就是1,不带参数的话就是把shape中所有为1维数的维度都去掉 - torch.unsqueeze()
对数据维度进行扩充,给指定位置加上维数为一的维度
比如a的shape[AB]
torch.squeeze(a,1)===>shape[A1B]
torch.squeeze(a,0)===>shape[1AB]
torch.squeeze(a,-1)===>shape[AB*1] - torch.index_select(input, dim, index, out=None)
在指定维度dim方向上从input中抽取由位置序列index所指定的值。output的其他维度的长度和原来矩阵相同,在第dim维度上的长度和index的长度相同
注意:index里的值可以重复
>>> x = torch.randn(3, 4)
>>> x
1.2045 2.4084 0.4001 1.1372
0.5596 1.5677 0.6219 -0.7954
1.3635 -1.2313 -0.5414 -1.8478
[torch.FloatTensor of size 3x4]
>>> indices = torch.LongTensor([0, 2])
>>> torch.index_select(x, 0, indices)
1.2045 2.4084 0.4001 1.1372
1.3635 -1.2313 -0.5414 -1.8478
[torch.FloatTensor of size 2x4]
>>> torch.index_select(x, 1, indices)
1.2045 0.4001 0.5596
0.6219 1.3635 -0.5414
[torch.FloatTensor of size 3x2]
- torch.narrow(dim, index, num)
narrow是取出某一维并进行裁剪从index到num个值
x = torch.Tensor([1,2,3],[4,5,6],[7,8,9])
>>>x.narrow(0,0,3)
1 2 3
4 5 6
7 8 9
>>>x.narrow(1,1,2)
2 3
5 6
8 9
- torch.mm(mat1, mat2, out=None)
Performs a matrix multiplication of the matrices mat1 and mat2.
If mat1 is a (n×m)
tensor, mat2 is a (m×p) tensor, out will be a (n×p) tensor.
>>> mat1 = torch.randn(2, 3)
>>> mat2 = torch.randn(3, 3)
>>> torch.mm(mat1, mat2)
tensor([[ 0.4851, 0.5037, -0.3633],
[-0.0760, -3.6705, 2.4784]])
- Variable 和 Tensor相互转换
Variable->Tensor
Variable.data
Tensor->Variable
Variable(Tensor)
- torch.Tensor.view()
会返回具有相同数据但大小不同的新张量。 返回的张量必须有与原张量相同的数据和相同数量的元素,但可以有不同的大小。一个张量必须是连续contiguous()的才能被查看。类似于Numpy的np.reshape()
torch.Tensor.view会将原有数据重新分配为一个新的张量,比如我们使用:
x = torch.randn(2, 4)
会输出一个随机张量:
1.5600 -1.6180 -2.0366 2.7115
0.8415 -1.0103 -0.4793 1.5734
[torch.FloatTensor of size 2x4]
然后我们看一下使用view重新构造一个Tensor
y = x.view(4,2)
print y
输出如下
1.5600 -1.6180
-2.0366 2.7115
0.8415 -1.0103
-0.4793 1.5734
[torch.FloatTensor of size 4x2]
.resize(): 将tensor的大小调整为指定的大小。如果元素个数比当前的内存大小大,就将底层存储大小调整为与新元素数目一致的大小。如果元素个数比当前内存小,则底层存储不会被改变。原来tensor中被保存下来的元素将保持不变,但新内存将不会被初始化。
.permute(dims):将tensor的维度换位。具体可以自己测试
torch.unsqueeze:返回一个新的张量,对输入的制定位置插入维度
相比之下,如果你想返回相同数量的元素,只是改变数组的形状推荐使用torch.view()
- torch.bmm(a,b)
a,b必须是3Dtensor. 计算batch tensor之间的matrix multiple
Example:
batch1 = torch.randn(10, 3, 4)
batch2 = torch.randn(10, 4, 5)
res = torch.bmm(batch1, batch2)
res.size()
torch.Size([10, 3, 5])
-
torch.ones_like(a)
torch.ones_like(input, dtype=None, layout=None, device=None, requires_grad=False) → Tensor
返回一个填充了标量值1的张量,其大小与之相同 input。 -
nn.MaxPool2d
-
torch.permute(0,1,3,2)置换
上述是把2 3维调换 -
torch.contiguous()
调用view之前最好先contiguous
x.contiguous().view()
因为view需要tensor的内存是整块的
contiguous:view只能用在contiguous的variable上。如果在view之前用了transpose, permute等,需要用contiguous()来返回一个contiguous copy。
一种可能的解释是:
有些tensor并不是占用一整块内存,而是由不同的数据块组成,而tensor的view()操作依赖于内存是整块的,这时只需要执行contiguous()这个函数,把tensor变成在内存中连续分布的形式。
判断是否contiguous用torch.Tensor.is_contiguous()函数。 -
显示维度
-
tensor.shape/tensor.size()/tensor.size(0)
-
初始化
torch.rand(2,3,3)/torch.ones(2,3,3)/torch.zeros(2,3,3) -
list[]
a=[]
a.append(tensor)
len(a)
for i in range(10):
a.append([]) -
list 转 torch.Tensor
tensor=torch.Tensor(list) -
vi 操作
dd 剪切, yy复制, p粘贴 -
list随机采样
list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
slice = random.sample(list, 5) #从list中随机获取5个元素,作为一个片断返回