pytorch常用函数

torch.cat()

torch.squeeze()

torch.unsqueeze()

torch.stack()

torch.sum()

torch.sum(input, dim, out=None) → Tensor
#input (Tensor) – 输入张量
#dim (int) – 缩减的维度
#out (Tensor, optional) – 结果张量

pytorch常用函数_第1张图片

torch.multinomial()

torch.multinomial(input, num_samples,replacement=False, out=None) → LongTensor

这个函数的作用是对input的每一行做num_samples次抽取,输出的张量是每一次取值时input张量对应的列下标,每次抽取的依据是input中元素值的大小,值越大,越有几率被先抽到。如果有值为0的元素,那么在所有非0的元素集合被取空之前是不会取0元素的。replacement指的是取样时是否是有放回的取样,True是有放回,False无放回。这个函数可以用来实现word2vec算法中的负采样。

下面看官网的一个例子。

>>> weights = torch.Tensor([0, 10, 3, 0]) # create a Tensor of weights
>>> torch.multinomial(weights, 4)
 
 1
 2
 0
 0
[torch.LongTensor of size 4]
 
>>> torch.multinomial(weights, 4, replacement=True)
 
 1
 2
 1
 2
[torch.LongTensor of size 4]

参考:
torch.multinomial()理解

你可能感兴趣的:(pytorch)