torch.clamp
主要用于对张量中的元素进行截断(clamping),将其限制在一个指定的区间范围内。
函数定义
torch.clamp(input, min=None, max=None) → Tensor
参数说明
input
类型:Tensor
需要进行截断操作的输入张量。
min
类型:float 或 None(默认值)
指定张量中元素的最小值。小于 min 的元素会被截断为 min 值。
如果设置为 None,则表示不限制最小值。
max
类型:float 或 None(默认值)
指定张量中元素的最大值。大于 max 的元素会被截断为 max 值。
如果设置为 None,则表示不限制最大值。
返回值
返回一个新的张量,其中元素已经被限制在 ([min, max]) 的范围内。
原张量不会被修改(函数是非原地操作),除非使用 torch.clamp_ 以进行原地操作。
使用场景
torch.clamp
常见的应用场景包括:
- 避免数值溢出:限制张量中的数值在合理范围内,防止出现过大或过小的值导致数值不稳定。
- 归一化操作:将张量值限制在 [0, 1] 或 [-1, 1] 的范围。
- 梯度截断:在训练神经网络时避免梯度爆炸或梯度消失问题。
demo
仅设置最大值或最小值
x = torch.tensor([0.5, 2.0, -1.0, 3.0, -2.0])# 仅限制最大值为 1.0
result_max = torch.clamp(x, max=1.0)# 仅限制最小值为 0.0
result_min = torch.clamp(x, min=0.0)print(result_max) # 输出: tensor([0.5000, 1.0000, -1.0000, 1.0000, -2.0000])
print(result_min) # 输出: tensor([0.5000, 2.0000, 0.0000, 3.0000, 0.0000])
torch.nonzero
返回 input中非零元素的索引下标
无论输入是几维,输出张量都是两维,每行代表输入张量中非零元素的索引位置(在所有维度上面的位置),行数表示非零元素的个数。
import torch
import torch.nn.functional as F
import mathX=torch.tensor([[0, 2, 0, 4],[5, 0, 7, 8]])
index = torch.nonzero(X)
# index = X.nonzero()
print(index)id_t = index.t_()
print(id_t)
tensor([[0, 1],
[0, 3],
[1, 0],
[1, 2],
[1, 3]])
tensor([[0, 0, 1, 1, 1],
[1, 3, 0, 2, 3]])
torch.sparse_coo_tensor
torch.sparse_coo_tensor(indices, values, size=None, *, dtype=None, device=None, requires_grad=False, check_invariants=None, is_coalesced=None)
创建一个Coordinate(COO) 格式的稀疏矩阵.
稀疏矩阵指矩阵中的大多数元素的值都为0,由于其中非常多的元素都是0,使用常规方法进行存储非常的浪费空间,所以采用另外的方法存储稀疏矩阵。
使用一个三元组来表示矩阵中的一个非0数,三元组分别表示元素(所在行,所在列,元素值),也就是上图中每一个竖的三元组就是表示了一个非零数,其余值都为0,这样就存储了一个稀疏矩阵.构造这样个矩阵需要知道所有非零数所在的行、所在的列、非零元素的值和矩阵的大小这四个值。
indices:此参数是指定非零元素所在的位置,也就是行和列,所以此参数应该是一个二维的数组,当然它可以是很多格式(ist, tuple, NumPy ndarray, scalar, and other types. )第一维指定了所有非零数所在的行数,第二维指定了所有非零元素所在的列数。例如indices=[[1, 4, 6], [3, 6, 7]]表示我们稀疏矩阵中(1, 3),(4, 6), (6, 7)几个位置是非零的数所在的位置。
values:此参数指定了非零元素的值,所以此矩阵长度应该和上面的indices一样长也可以是很多格式(list, tuple, NumPy ndarray, scalar, and other types.)。例如``values=[1, 4, 5]表示上面的三个位置非零数分别为1, 4, 5。
size:指定了稀疏矩阵的大小,例如size=[10, 10]表示矩阵大小为 10 × 10 10\times 10 10×10,此大小最小应该足以覆盖上面非零元素所在的位置,如果不给定此值,那么默认是生成足以覆盖所有非零值的最小矩阵大小。
dtype:指定返回tensor中数据的类型,如果不指定,那么采取values中数据的类型。
device:指定创建的tensor在cpu还是cuda上。
requires_grad:指定创建的tensor需不需要梯度信息,默认为False
import torchindices = torch.tensor([[4, 2, 1], [2, 0, 2]])
values = torch.tensor([3, 4, 5], dtype=torch.float32)
x = torch.sparse_coo_tensor(indices=indices, values=values, size=[5, 5])
print(x)
print('-----------------------------')
print(x.to_dense())
tensor(indices=tensor([[4, 2, 1],
[2, 0, 2]]),
values=tensor([3., 4., 5.]),
size=(5, 5), nnz=3, layout=torch.sparse_coo)
-----------------------------
tensor([[0., 0., 0., 0., 0.],
[0., 0., 5., 0., 0.],
[4., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 3., 0., 0.]])
torch.max
import torch
import torch.nn.functional as F
import math# 创建一个二维张量
x = torch.tensor([[1, 3, 2], [4, 5, 6], [7, 8, 9]])# 返回最大值
value = torch.max(x)
print("value:", value)# 返回最大值及其索引
max_value, max_index = torch.max(x, 0)
print("Max value:", max_value)
print("Max index:", max_index)# 在第一个维度(行)上查找最大值,并保留形状
max_value_row, max_index_row = torch.max(x, 0, keepdim=True)
print("dim=0 keeping shape: ", max_value_row)
print("dim=0 keeping shape: ", max_index_row)
value: tensor(9)
Max value: tensor([7, 8, 9])
Max index: tensor([2, 2, 2])
dim=0 keeping shape: tensor([[7, 8, 9]])
dim=0 keeping shape: tensor([[2, 2, 2]])
fastbev:
train
loss function
F.binary_cross_entropy
F.binary_cross_entropy(
input: Tensor, # 预测输入
target: Tensor, # 标签
weight: Optional[Tensor] = None, # 权重可选项
size_average: Optional[bool] = None, # 可选项,快被弃用了
reduce: Optional[bool] = None,
reduction: str = "mean", # 默认均值或求和等形式
) -> Tensor:
input
: 形状为(N, *)
的张量,表示模型的预测结果,取值范围可以是任意实数。target
: 形状为(N, *)
的张量,表示真实标签,取值为 0 或 1。
import torch
import torch.nn.functional as F
import mathrow, col = 2, 3
x = torch.randn(row, col)predict = torch.sigmoid(x)index = torch.randint(col, size = (row,), dtype=torch.long)
y = torch.zeros(row, col)
y[range(y.shape[0]), index] = 1
print(range(y.shape[0]))loss1 = F.binary_cross_entropy(predict, y)
print("loss1 ",loss1)def binary_cross_entropy(predict, y):loss = -torch.sum(y * torch.log(predict) + (1 - y) * torch.log(1 - predict)) / torch.numel(y)return lossloss2 = binary_cross_entropy(predict, y)
print("loss2 ", loss2)
range(0, 2)
loss1 tensor(1.1794)
loss2 tensor(1.1794)
F.binary_cross_entropy_with_logits
相比F.binary_cross_entropy函数,F.binary_cross_entropy_with_logits函数在内部使用了sigmoid函数.
F.binary_cross_entropy_with_logits = sigmoid + F.binary_cross_entropy。
F.cross_entropy
多分类交叉熵损失函数
F.cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction='mean')
参数说明:
input:(N, C)形状的张量,其中N为Batch_size,C为类别数。该参数对应于神经网络最后一个全连接层输出的未经Softmax处理的结果。
target:一个大小为(N,)张量,其值是0 <= targets[i] <= C-1的元素,其中C是类别数。该参数包含一组给定的真实标签(ground truth)。
weight:采用类别平衡的加权方式计算损失值。可以传入一个大小为(C,)张量,其中weight[j]是类别j的权重。默认为None。
size_average:弃用,可以忽略。该参数已经被reduce参数取代了。
ignore_index:指定被忽略的目标值的索引。如果目标值等于该索引,则不计算该样本的损失。默认值为-100,即不忽略任何目标值。
reduce:指定返回的损失值的方式。可以是“None”(不返回损失值)、“mean”(返回样本损失值的平均值)和“sum”(返回样本损失值的总和)。默认值为“mean”。
reduction:与reduce参数等价。表示返回的损失值的方式。默认值为“mean”。
F.cross_entropy
函数与nn.CrossEntropyLoss
类是相似的,但前者更适合于控制更多的细节。
由于内部已经使用SoftMax
函数处理,故两者的输入都不需要使用SoftMax
处理。
import torch
import torch.nn.functional as F
import math# redictions:一个2维tensor,表示模型的预测结果。它的形状是(2, 3),其中2是样本数量,3是类别数量。每一行对应一个样本的预测结果,每个元素表示该类别的概率。
# labels:一个1维tensor,表示样本的真实标签。它的形状是(2,),其中2是样本数量。
predictions = torch.tensor([[0.2, 0.3, 0.5], [0.8, 0.1, 0.1]])
labels = torch.tensor([2, 0])# 定义每个类别的权重
weights = torch.tensor([1.0, 2.0, 3.0])# 使用F.cross_entropy计算带权重的交叉熵损失
loss = F.cross_entropy(predictions, labels, weight=weights)
print(loss) # tensor(0.8773)# 测试计算过程
pred = F.softmax(predictions, dim=1)
loss2 = -(weights[2] * math.log(pred[0, 2]) + weights[0]*math.log(pred[1, 0]))/4 # 4 = 1+3 对应权重之和
print(loss2) # 0.8773049571540321# 对于第一个样本,它的预测结果为pred[0],真实标签为2。根据交叉熵损失的定义,我们可以计算出它的损失为:
# -weights[2] * math.log(pred[0, 2])
# 对于第二个样本,它的预测结果为pred[1],真实标签为0。根据交叉熵损失的定义,我们可以计算出它的损失为:
# -weights[0] * math.log(pred[1, 0])
torch.nn.BCELoss
torch.nn.BCELoss(
weight=None,
size_average=None,
reduce=None,
reduction='mean' # 默认计算的是批量样本损失的平均值,还可以为'sum'或者'none'
)
torch.nn.BCEWithLogitsLoss
torch.nn.BCEWithLogitsLoss(
weight=None,
size_average=None,
reduce=None,
reduction='mean', # 默认计算的是批量样本损失的平均值,还可以为'sum'或者'none'
pos_weight=None
)
import numpy as np
import torch
from torch import nn
import torch.nn.functional as Fy = torch.tensor([1, 0, 1], dtype=torch.float)
y_hat = torch.tensor([0.8, 0.2, 0.4], dtype=torch.float)bce_loss = nn.BCELoss()# nn.BCELoss()需要先对输入数据进行sigmod
print("官方BCELoss = ", bce_loss(torch.sigmoid(y_hat), y))# nn.BCEWithLogitsLoss()不需要自己sigmod
bcelogits_loss = nn.BCEWithLogitsLoss()
print("官方BCEWithLogitsLoss = ", bcelogits_loss(y_hat, y))# 我们根据二分类交叉熵损失函数实现:
def loss(y_hat, y):y_hat = torch.sigmoid(y_hat)l = -(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))l = sum(l) / len(l)return lprint('自己实现Loss = ', loss(y_hat, y))
# 可以看到结果值相同
官方BCELoss = tensor(0.5608)
官方BCEWithLogitsLoss = tensor(0.5608)
自己实现Loss = tensor(0.5608)
BCELoss与BCEWithLogitsLoss的关联:BCEWithLogitsLoss = Sigmoid + BCELoss
import numpy as np
import torch
from torch import nn
import torch.nn.functional as Fy = torch.tensor([1, 0, 1], dtype=torch.float)
y_hat = torch.tensor([0.8, 0.2, 0.4], dtype=torch.float)bce_loss = nn.BCELoss()# nn.BCELoss()需要先对输入数据进行sigmod
print("官方BCELoss = ", bce_loss(torch.sigmoid(y_hat), y))# nn.BCEWithLogitsLoss()不需要自己sigmod
bcelogits_loss = nn.BCEWithLogitsLoss()
print("官方BCEWithLogitsLoss = ", bcelogits_loss(y_hat, y))# 我们根据二分类交叉熵损失函数实现:
def loss(y_hat, y):y_hat = torch.sigmoid(y_hat)l = -(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))l = sum(l) / len(l)return lprint('自己实现Loss = ', loss(y_hat, y))
# 可以看到结果值相同
官方BCELoss = tensor(0.5608)
官方BCEWithLogitsLoss = tensor(0.5608)
自己实现Loss = tensor(0.5608)
torch.nn.CrossEntropyLoss()
torch.nn.CrossEntropyLoss(
weight=None,
size_average=None,
ignore_index=-100,
reduce=None,
reduction='mean',
label_smoothing=0.0 # 同样,默认计算的是批量样本损失的平均值,还可以为'sum'或者'none'
)
- 在关于二分类的问题中,输入交叉熵公式的网络预测值必须经过
Sigmoid
进行映射 - 而在多分类问题中,需要将
Sigmoid
替换成Softmax
,这是两者的一个重要区别 - CrossEntropyLoss =
softmax
+log
+nll_loss
的集成
多分类交叉熵损失函数
cross_loss = nn.CrossEntropyLoss(reduction="none") # 设置为none,这里输入每个样本的loss值,不计算平均值
target = torch.tensor([0, 1, 2])predict = torch.tensor([[0.9, 0.2, 0.8],[0.5, 0.2, 0.4],[0.4, 0.2, 0.9]]) # 未经过softmax
print('官方实现CrossEntropyLoss: ', cross_loss(predict, target))# 自己实现方便理解版本的CrossEntropyLoss
def cross_loss(predict, target, reduction=None):all_loss = []for index, value in enumerate(target):# 利用多分类简化后的公式,对每一个样本求loss值loss = -predict[index][value] + torch.log(sum(torch.exp(predict[index])))all_loss.append(loss)all_loss = torch.stack(all_loss)if reduction == 'none':return all_losselse:return torch.mean(all_loss)print('实现方便理解的CrossEntropyLoss: ', cross_loss(predict, target, reduction='none'))# 利用F.nll_loss实现的CrossEntropyLoss
def cross_loss2(predict, target, reduction=None):# Softmax的缺点:# 1、如果有得分值特别大的情况,会出现上溢情况;# 2、如果很小的负值很多,会出现下溢情况(超出精度范围会向下取0),分母为0,导致计算错误。# 引入log_softmax可以解决上溢和下溢问题logsoftmax = F.log_softmax(predict)print('target = ', target)print('logsoftmax:\n', logsoftmax)# nll_loss不是将标签值转换为one-hot编码,而是根据target的值,索引到对应元素,然后取相反数。loss = F.nll_loss(logsoftmax, target, reduction=reduction)return lossprint('F.nll_loss实现的CrossEntropyLoss: ', cross_loss2(predict, target, reduction='none'))
官方实现CrossEntropyLoss: tensor([0.8761, 1.2729, 0.7434])
实现方便理解的CrossEntropyLoss: tensor([0.8761, 1.2729, 0.7434])target = tensor([0, 1, 2])
logsoftmax:
tensor([[-0.8761, -1.5761, -0.9761],
[-0.9729, -1.2729, -1.0729],
[-1.2434, -1.4434, -0.7434]])
F.nll_loss实现的CrossEntropyLoss: tensor([0.8761, 1.2729, 0.7434])