https://zhuanlan.zhihu.com/p/104701190
Docstring:
select(input, dim, index) -> Tensor
Slices the :attr:`input` tensor along the
selected dimension at the given index.
沿着dim维度,去除index的数,index只能为int
函数的作用为:将函数的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))
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来取多个
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]])
该函数的作用是扩充tensor的维度,
1)
import 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]]
2)
import 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()里面参数代表是重复多少次,就是复制多少次,比如下面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])
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')
>>#$$$$$$#
参考
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)
因篇幅问题不能全部显示,请点此查看更多更全内容