当前位置:网站首页>Pytorch calculates the loss for each sample in the batch
Pytorch calculates the loss for each sample in the batch
2022-07-25 10:23:00 【Haulyn5】
Preface
PyTorch Loss function of ( Here I only use and research MSELoss) By default, a Batch Calculate the loss of all samples , And find the mean value . If I need the loss of each sample for some subsequent calculations ( And optimize model parameters , Gradient descent is irrelevant ), For example, use the loss of samples to do some operations , You can't use the default loss function , I searched and didn't find the relevant information , stay PyTorch The forum found relevant problems .
Loss for each sample in batch - PyTorch Forums
https://discuss.pytorch.org/t/loss-for-each-sample-in-batch/36200 Besides , Also refer to official documents :
Solution
Suppose the original loss function instantiation code is as follows :
loss_fn = nn.MSELoss()Change it to the following code :
loss_fn_each = nn.MSELoss(reduction ='none') #MSESuppose the code of the original loss calculation stage is as follows :
loss = loss_fn(Ypred, Y)Is changed to :
loss_each = torch.mean(loss_fn_each(Ypred, Y), 1) # The loss calculated here is for each sample
loss = torch.mean(loss_each) # Here is the whole Batch The average loss of If you need to do something with the loss of each sample , Use `loss_each` that will do . If your prediction results include multiple dimensions , That may need to be used many times mean function . If necessary, switch to list preservation , You can use it directly tolist() Method . For relevant contents, please refer to documents and network .
Usually , If you want the loss of each sample , It should be Eval Stage , After all, it doesn't make much sense for each sample to lose instability in the training stage . If in training , Don't forget the following code .
total_loss.append(loss.item()) # Record the loss
optimizer.zero_grad() # Basic code of training stage , Clear the gradient
loss.backward() # Basic code of training stage , Back propagation
optimizer.step() # Basic code of training stage , Optimizing model parameters principle
When setting the loss function , It is usually written in the following way , There are no parameters .
loss_fn = nn.MSELoss() Actually, refer to official documents , This loss function can pass in parameters reduction Of .reduction There are three kinds of , The default is 'mean' , It can be 'none' | 'mean' | 'sum'.
from none Start with ,none Is the most complete default loss , For example, the model output I calculated is batch_size * output_dim Of shape Of Tensor( That is to say, each sample has output_dim Dimension data ), Then send MSELoss Two inputs of ( My prediction and goal ) The size is like this , and MSELoss The output size of is exactly the same as the input size , It's also (batch_size * output_dim ).
But what I want is (batch_size * 1) The loss of , That is, for each sample , The loss of dimensions within the sample is averaged , But the samples cannot be averaged . Use mean Words ( Default ), What you get is a ( 1*1 ) Of Tensor, hold Batch The dimensions and internal dimensions of the sample are averaged .
So the idea is to directly use `nn.MSELoss(reduction ='none')` Calculate the loss as a loss function , Got Tensor Along the dim=1 Calculating mean , In this way, each sample calculates an independent loss , There is no mean between samples .
边栏推荐
- Use of dictionary tree
- PyTorch 代码模板 (CNN)
- 构建 Dompteur 容器问题小记
- 测试计划、测试方案
- 数组静态初始化,遍历,最值
- 史上最全面的UE4 文件操作,打开,读、写,增、删、改、查
- 异常处理Exception
- Selenium 等待元素出现与等待操作可以执行的条件
- Number theory -- Research on divisor
- Duplicate SSL_ Anti spoofing, spoofing attacks and deep forgery detection using wav2vec 2.0 and data enhanced automatic speaker authentication
猜你喜欢
随机推荐
conda 配置深度学习环境 pytorch transformers
Angr (IX) -- angr_ ctf
Radio and multi selection buttons of swing components
VoxCeleb1 数据集下载
NPM details
Test plan and test plan
Snake games
多线程——五大状态
@Import, conditional and @importresource annotations
Fastdfs离线部署(图文)
The ultimate summary of jsonobject parsing JSON format
OSPF协议的配置(以华为eNSP为例)
Number theory -- negative Radix conversion
Angr(三)——angr_ctf
UE4 快速找到打包失败的原因
多线程——死锁和synchronized
Oh my Zsh and TMUX configuration (personal)
Angr(二)——angr_ctf
Angr(四)——angr_ctf
Angr (II) -- angr_ ctf








