pytorch包学习心得--torch

1.torch.tensor(datadtype=Nonedevice=Nonerequires_grad=False) → Tensor

torch.tensor() always copies data. If you have a Tensor data and want to avoid a copy, use torch.Tensor.requires_grad_() or torch.Tensor.detach(). If you have a NumPy ndarrayand want to avoid a copy, use torch.from_numpy().

torch.tensor函数总会拷贝数据,除非使用其他函数可以避免拷贝。

2.torch.sparse_coo_tensor(indicesvaluessize=Nonedtype=Nonedevice=Nonerequires_grad=False) → Tensor

使用coordinate format matrix来进行稀疏矩阵的数据的存储。具体稀疏矩阵的表示方法可参考https://en.wikipedia.org/wiki/Sparse_matrix,其中,

一个稀疏矩阵:

能够被表示为如下一个三行的矩阵:

   A  = [ 5 8 3 6 ]
   IA = [ 0 0 2 3 4 ]
   JA = [ 0 1 2 1 ]

A用来表示矩阵中的元素(从左到右,从上到下),IA[i]表示从第一行到第i行中的所有元素,IA[0]始终为0,而JA则表示相应的

元素在哪一列。但在这里indices用一个二维矩阵表示每个稀疏项的位置,第一维是行,第二维是列。

3.torch.as_tensor(datadtype=Nonedevice=None) → Tensor

转化一个ndarray成为tensor,如果同为cpu存储,则无需拷贝,否则需要拷贝,例如从cpu类型变为GPU类型,也就是赋值device相关属性。

4.torch.zeros_like(inputdtype=Nonelayout=Nonedevice=Nonerequires_grad=False) → Tensor

其实在调用torch.zeros(input.size(),dtype=Nonelayout=Nonedevice=Nonerequires_grad=False)

torch.ones_like()类似用法。

5.torch.arange(start=0endstep=1out=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor 类似numpy.arange 函数,支持int和float,而python range函数不支持float类型。

6.torch.range(start=0endstep=1out=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor 同上arange函数,与python自带的不一样,其支持float类型。

7.torch.logspace(startendsteps=100out=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor 

>>> torch.logspace(start=-10, end=10, steps=5)
tensor([ 1.0000e-10,  1.0000e-05,  1.0000e+00,  1.0000e+05,  1.0000e+10])
>>> torch.logspace(start=0.1, end=1.0, steps=5)
tensor([  1.2589,   2.1135,   3.5481,   5.9566,  10.0000])

这里start和end都是指数。

8.torch.eye(nm=Noneout=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor

返回一个2维单位矩阵,

>>> torch.eye(3)
tensor([[ 1.,  0.,  0.],
        [ 0.,  1.,  0.],
        [ 0.,  0.,  1.]])

9.torch.empty(*sizesout=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor

Returns a tensor filled with uninitialized data. The shape of the tensor is defined by the variable argument sizes.

10.torch.empty_like(inputdtype=Nonelayout=Nonedevice=Nonerequires_grad=False) → Tensor

11.torch.full(sizefill_valueout=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor

Returns a tensor of size size filled with fill_value.

感觉这个函数可以替代torch.zeros()和torch.ones().

12.torch.full_like(inputfill_valueout=Nonedtype=Nonelayout=torch.strideddevice=Nonerequires_grad=False) → Tensor 类似其它_like

13.torch.cat(tensorsdim=0out=None) → Tensor

Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be empty.

>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497],
        [ 0.6580, -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497]])
>>> torch.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614,  0.6580, -1.0969, -0.4614,  0.6580,
         -1.0969, -0.4614],
        [-0.1034, -0.5790,  0.1497, -0.1034, -0.5790,  0.1497, -0.1034,
         -0.5790,  0.1497]])

14.torch.chunk(tensorchunksdim=0) → List of Tensors

Splits a tensor into a specific number of chunks.

Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by chunks.

Parameters:
  • tensor (Tensor) – the tensor to split
  • chunks (int) – number of chunks to return
  • dim (int) – dimension along which to split the tensor

 chunk函数用于分割torch数据。

15.torch.gather(inputdimindexout=None) → Tensor

Gathers values along an axis specified by dim.

就是使用input中的数进行替换,规则如下:

out[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
out[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
out[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2

eg:

>>> t = torch.tensor([[1,2],[3,4]])
>>> g=torch.tensor([[0,0],[1,0]])
>>> torch.gather(t, 1, g)
tensor([[ 1,  1],
        [ 4,  3]])

这里是:[[t[0][g[0][0]],t[0][g[0][1]]],[t[1][g[1][0]],t[1][g[1][1]]]]

16.torch.index_select(inputdimindexout=None) → Tensor

选择若干行和列

如下:

>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-0.4664,  0.2647, -0.1228, -1.1068],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> indices = torch.tensor([0, 2])
>>> torch.index_select(x, 0, indices)
tensor([[ 0.1427,  0.0231, -0.5414, -1.0009],
        [-1.1734, -0.6571,  0.7230, -0.6004]])
>>> torch.index_select(x, 1, indices)
tensor([[ 0.1427, -0.5414],
        [-0.4664, -0.1228],
        [-1.1734,  0.7230]])

17.torch.masked_select(inputmaskout=None) → Tensor

Returns a new 1-D tensor which indexes the input tensor according to the binary mask mask which is a ByteTensor.

The shapes of the mask tensor and the input tensor don’t need to match, but they must be broadcastable.

注意:返回的tensor是个拷贝而不是引用.

如下所示:

>>> x = torch.randn(3, 4)
>>> x
tensor([[ 0.3552, -2.3825, -0.8297,  0.3477],
        [-1.2035,  1.2252,  0.5002,  0.6248],
        [ 0.1307, -2.0608,  0.1244,  2.0139]])
>>> mask = x.ge(0.5)
>>> mask
tensor([[ 0,  0,  0,  0],
        [ 0,  1,  1,  1],
        [ 0,  0,  0,  1]], dtype=torch.uint8)
>>> torch.masked_select(x, mask)
tensor([ 1.2252,  0.5002,  0.6248,  2.0139])

18.torch.narrow(inputdimensionstartlength) → Tensor

Returns a new tensor that is a narrowed version of input tensor. The dimension dim is input from start to start + length. The returned tensor and input tensor share the same underlying storage.

选择一维tensor进行切片操作.

19.torch.nonzero(inputout=None) → LongTensor

Returns a tensor containing the indices of all non-zero elements of input. Each row in the result contains the indices of a non-zero element in input.

返回非0点的点的坐标:

>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
tensor([[ 0],
        [ 1],
        [ 2],
        [ 4]])
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
                                [0.0, 0.4, 0.0, 0.0],
                                [0.0, 0.0, 1.2, 0.0],
                                [0.0, 0.0, 0.0,-0.4]]))
tensor([[ 0,  0],
        [ 1,  1],
        [ 2,  2],
        [ 3,  3]])

20.torch.reshape(inputshape) → Tensor

Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.

 torch.Tensor.view() 也有类似功能.

A single dimension may be -1, in which case it’s inferred from the remaining dimensions and the number of elements in input.

21.torch.split(tensorsplit_size_or_sectionsdim=0)

Splits the tensor into chunks.

和torch.chunk一样.

22.torch.squeeze(inputdim=Noneout=None) → Tensor

Returns a tensor with all the dimensions of input of size 1 removed.

把维度为1的删除掉.

>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])

23.orch.stack(seqdim=0out=None) → Tensor

Concatenates sequence of tensors along a new dimension.

All tensors need to be of the same size.

类似torch.cat()

24.torch.t(input) → Tensor

Expects input to be a matrix (2-D tensor) and transposes dimensions 0 and 1.

和transpose(input, 0, 1)效果一样

转置

25.torch.transpose(inputdim0dim1) → Tensor

 

Parameters:
  • input (Tensor) – the input tensor
  • dim0 (int) – the first dimension to be transposed
  • dim1 (int) – the second dimension to be transposed

 26.torch.take(inputindices) → Tensor

提取数值用,返回的是个一维的tensor,就是把input展开为一维,然后取数.

>>> src = torch.tensor([[4, 3, 5],
                        [6, 7, 8]])
>>> torch.take(src, torch.tensor([0, 2, 5]))
tensor([ 4,  5,  8])

27.torch.unbind(tensordim=0) → seq

Removes a tensor dimension.类似split.

>>> torch.unbind(torch.tensor([[1, 2, 3],
>>>                            [4, 5, 6],
>>>                            [7, 8, 9]]))
(tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]))

28.

torch.where(conditionxy) → Tensor

Return a tensor of elements selected from either x or y, depending on condition.

>>> x = torch.randn(3, 2)
>>> y = torch.ones(3, 2)
>>> x
tensor([[-0.4620,  0.3139],
        [ 0.3898, -0.7197],
        [ 0.0478, -0.1657]])
>>> torch.where(x > 0, x, y)
tensor([[ 1.0000,  0.3139],
        [ 0.3898,  1.0000],
        [ 0.0478,  1.0000]])

29.torch.bernoulli(input*generator=Noneout=None) → Tensor

生成伯努利分布,input里面都是大于等于0小于等于1的数,这些都是概率,根据概率生成模型.

>>> a = torch.empty(3, 3).uniform_(0, 1)  # generate a uniform random matrix with range [0, 1]
>>> a
tensor([[ 0.1737,  0.0950,  0.3609],
        [ 0.7148,  0.0289,  0.2676],
        [ 0.9456,  0.8937,  0.7202]])
>>> torch.bernoulli(a)
tensor([[ 1.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 1.,  1.,  1.]])

>>> a = torch.ones(3, 3) # probability of drawing "1" is 1
>>> torch.bernoulli(a)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
>>> a = torch.zeros(3, 3) # probability of drawing "1" is 0
>>> torch.bernoulli(a)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

30.torch.save(objfpickle_module=pickle_protocol=2)

Saves an object to a disk file.

保存数据

>>> # Save to file
>>> x = torch.tensor([0, 1, 2, 3, 4])
>>> torch.save(x, 'tensor.pt')
>>> # Save to io.BytesIO buffer
>>> buffer = io.BytesIO()
>>> torch.save(x, buffer)

31.torch.load(fmap_location=Nonepickle_module=)

Loads an object saved with torch.save() from a file.

>>> torch.load('tensors.pt')
# Load all tensors onto the CPU
>>> torch.load('tensors.pt', map_location=torch.device('cpu'))
# Load all tensors onto the CPU, using a function
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage)
# Load all tensors onto GPU 1
>>> torch.load('tensors.pt', map_location=lambda storage, loc: storage.cuda(1))
# Map tensors from GPU 1 to GPU 0
>>> torch.load('tensors.pt', map_location={'cuda:1':'cuda:0'})
# Load tensor from io.BytesIO object
>>> with open('tensor.pt') as f:
        buffer = io.BytesIO(f.read())
>>> torch.load(buffer)

 

你可能感兴趣的:(pytorch包学习心得--torch)