设为首页 加入收藏

TOP

Pytorch 常用函数
2019-09-17 14:35:02 】 浏览:119
Tags:Pytorch 常用 函数

1. torch.renorm(inputpdimmaxnormout=None) → Tensor


Returns a tensor where each sub-tensor of input along dimension dim is normalized such that the p-norm of the sub-tensor is lower than the value maxnorm。


解释:返回一个张量,包含规范化后的各个子张量,使得沿着dim维划分的各子张量的p范数小于maxnorm


2. torch. scatter_(dimindexsrc) → Tensor


src中的所有值按照index确定的索引写入本tensor中。其中索引是根据给定的dimension,dim按照gather()描述的规则来确定。


注意,index的值必须是在0(self.size(dim)-1)之间,


参数:


>>> x = torch.rand(2, 5)
>>> x
 0.4319  0.6500  0.4080  0.8760  0.2355
 0.2609  0.4711  0.8486  0.8573  0.1029
[torch.FloatTensor of size 2x5]
>>> torch.zeros(3, 5).scatter_(0, torch.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)    #将 x 按照格式写入新的Tensor里
 0.4319  0.4711  0.8486  0.8760  0.2355
 0.0000  0.6500  0.0000  0.8573  0.0000
 0.2609  0.0000  0.4080  0.0000  0.1029
[torch.FloatTensor of size 3x5]
>>> z = torch.zeros(2, 4).scatter_(1, torch.LongTensor([[2], [3]]), 1.23)
>>> z
 0.0000  0.0000  1.2300  0.0000
 0.0000  0.0000  0.0000  1.2300
[torch.FloatTensor of size 2x4]


 


3.  torch.gather(input, dim, index, out=None) Tensor


沿给定轴dim,将输入索引张量index指定位置的值进行聚合。


参数:


 or:


>>> s=torch.randn(3,6)
>>> s
tensor([[-0.4857, -0.0982, -0.6532, -1.0273, -0.9205, -0.7440],
        [-0.6890, -0.3474, -1.4337, -0.3511, -0.2443, -0.6398],
        [ 1.2902,  1.1210,  1.7374,  0.0902, -0.4524, -0.6898]])
>>> s.gather(1,torch.LongTensor([[1,2,1],[1,2,3],[1,2,3]]))
tensor([[-0.0982, -0.6532, -0.0982],
        [-0.3474, -1.4337, -0.3511],
        [ 1.1210,  1.7374,  0.0902]])


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python3 queue队列类 下一篇Java反射机制点滴知识

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目