Torch函数笔记(持续更新)

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值