torch函数

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

torchvision.ops.roi_align

https://zhuanlan.zhihu.com/p/104701190

torch.select

Docstring:
select(input, dim, index) -> Tensor
Slices the :attr:`input` tensor along the 
selected dimension at the given index.

沿着dim维度,去除index的数,index只能为int

nn.utils.clip_grad_norm_

函数的作用为:将函数的tot_norm限制为max_norm

import numpy as np
import torch
from torch import nn

def clip_grad_norm_(parameters, max_norm, norm_type=2):
    if isinstance(parameters, torch.Tensor):
        parameters = [parameters]
    parameters = list(filter(lambda p: p is not None, parameters))
    max_norm = float(max_norm)
    norm_type = float(norm_type)
    if norm_type == np.inf:
        total_norm = max(p.data.abs().max() for p in parameters)
    else:
        total_norm = 0
        for p in parameters:
            param_norm = p.data.norm(norm_type)
            total_norm += param_norm.item() ** norm_type
        total_norm = total_norm ** (1. / norm_type)
    clip_coef = max_norm / (total_norm + 1e-6)
    if clip_coef < 1:#小于1 才会clip
        for p in parameters:
            p.data.mul_(clip_coef)
            
    print("max_norm=%s, norm_type=%s, total_norm=%s, clip_coef=%s" % (max_norm, norm_type, total_norm, clip_coef))

torch.index_select

Docstring:
index_select(input, dim, index, *, out=None) -> Tensor
Returns a new tensor which indexes the 
:attr:`input` tensor along dimension

与上一个的区别就是以前index为int,只能取出一个,这里的index为vector,
现在可以按照index来取多个



torch.gather()

Docstring:
gather(input, dim, index, *, sparse_grad=False, out=None) -> Tensor
Gathers values along an axis specified by `dim`.

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

和上一个区别是,所有dim共用一个index,向量,但是使用gather可以让dim中的每个
拥有独自的index

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

torch.expand()

该函数的作用是扩充tensor的维度,

1import torch
a = torch.tensor([1, 2, 3])
c = a.expand(2, 3)
print(a)
print(c)
 
# 输出信息:
tensor([1, 2, 3])
tensor([[1, 2, 3],
        [1, 2, 3]]
 
 
 
2import torch
a = torch.tensor([1, 2, 3])
c = a.expand(3, 3)
print(a)
print(c)
 
# 输出信息:
tensor([1, 2, 3])
tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
 
 
3)
import torch
a = torch.tensor([[1], [2], [3]])
print(a.size())
c = a.expand(3, 3)
print(a)
print(c)
 
# 输出信息:
torch.Size([3, 1])
tensor([[1],
        [2],
        [3]])
tensor([[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]])
 
 
4)
import torch
a = torch.tensor([[1], [2], [3]])
print(a.size())
c = a.expand(3, 4)
print(a)
print(c)
 
# 输出信息:
torch.Size([3, 1])
tensor([[1],
        [2],
        [3]])
tensor([[1, 1, 1, 1],
        [2, 2, 2, 2],
        [3, 3, 3, 3]])

torch.repeat()

torch.repeat()里面参数代表是重复多少次,就是复制多少次,比如下面2, 3, 1, 6代表复制2, 3, 1, 6次,原来为2, 1, 3, 1。相乘就是后面维度:4, 3, 3, 6. 它不允许使用参数 -1。

print(x.shape)
x_rep = x.repeat(2, 3, 1, 6)
print(x_rep.shape)

++++++++++++++++++++++++++++++++++++++++++++++
torch.Size([2, 1, 3, 1])
torch.Size([4, 3, 3, 6])

functools.partial()

partial 可以预先的传递一些已知的参数,后续在补充剩余的参数

class Stu:
    def __init__(self,name,old):
        super(Stu, self).__init__()
        self.name = name
        self.old = old

    def __call__(self, *args, **kwargs):
        print('#$$$$$$#')

t = partial(Stu,name='zba')
print(t)

t(old = 123)()

>>functools.partial(<class '__main__.Stu'>, name='zba')
>>#$$$$$$#

torch.nn.functional.pad

参考 pad

F.binary_cross_entropy_with_logits

The difference between this function and BCEloss is that this function is equal to Sigmoid plus BCEloss

在这里插入代码片
import torch
import torch.nn.functional as F
from torch import nn
def binary_cross_entropyloss(prob, target, weight=None):
    prob = torch.sigmoid(prob)
    # manually
    # loss = -weight * (target * torch.log(prob) + (1 - target) * (torch.log(1 - prob)))
    # loss = torch.sum(loss) / torch.numel(target)
    bce = torch.nn.BCELoss()
    loss = bce(prob,target)
    return loss

label = torch.tensor([
    [1., 0],
    [1., 0],

])
predict = torch.tensor([
    [0.1, 0.3],
    [0.2, 0.8]
])

weight1 = torch.tensor([
    [1., 1 ],
    [1., 1.],
])

loss1 =F.binary_cross_entropy_with_logits
l1 = loss1(predict, label)
loss = binary_cross_entropyloss(predict, label, weight=weight1)
print(l1, loss)


“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值