Pytorch官方文档
https://pytorch.org/docs/stable/generated/torch.nn.functional.log_softmax.html?highlight=log_softmax#torch.nn.functional.log_softmax
1、torch.zeros() & torch.scatter_()
scatter_(input, dim, index, src):将src中数据根据index中的索引按照dim的方向填进input。可以理解成放置元素或者修改元素
dim:沿着哪个维度进行索引
index:用来 scatter 的元素索引
src:用来 scatter 的源元素,可以是一个标量或一个张量
import torch
import matplotlib.pyplot as plt
x = torch.rand(2, 5)
# tensor([[0.1940, 0.3340, 0.8184, 0.4269, 0.5945],
# [0.2078, 0.5978, 0.0074, 0.0943, 0.0266]])
b = torch.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]])
# Longtensor([[0, 1, 2, 0, 0],
# [2, 0, 0, 1, 2]])
print(b)
c = torch.zeros(3, 5).scatter_(0, b, x)
# b与x的形状相同,x为值,b为索引,b中的值为表示对应到c中的行数,将x填入
print(c)
# tensor([[0.7536, 0.1863, 0.9075, 0.8558, 0.7230],
# [0.0000, 0.0612, 0.0000, 0.4090, 0.0000],
# [0.8661, 0.0000, 0.4762, 0.0000, 0.9599]])
2、torch.LongTensor()
b = torch.rand(3,3)#得到的是floattensor值,一般为int32
b = b.long()#得到的是longtensor值,即int64
b = torch.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]])
# 或使用以上,也为int64