简体   繁体   English

如何获得特定维度上张量的 MSE?

[英]How can I get the MSE of a tensor across a specific dimension?

I have 2 tensors with .size of torch.Size([2272, 161]) .我有2张量与.sizetorch.Size([2272, 161]) I want to get mean-squared-error between them.我想得到它们之间的均方误差。 However, I want it along each of the 161 channels, so that my error tensor has a .size of torch.Size([161]) .不过,我想它沿着每个161个通道,使我的错误张量有.sizetorch.Size([161]) How can I accomplish this?我怎样才能做到这一点?

It seems that torch.nn.MSELoss doesn't let me specify a dimension.似乎torch.nn.MSELoss不允许我指定维度。

For the nn.MSELoss you can specify the option reduction='none' .对于nn.MSELoss您可以指定选项reduction='none' This then gives you back the squared error for each entry position of both of your tensors.然后,这将为您返回两个张量的每个条目位置的平方误差。 Then you can apply torch.sum/torch.mean.然后你可以应用torch.sum/torch.mean。

a = torch.randn(2272,161)
b = torch.randn(2272,161)
loss = nn.MSELoss(reduction='none')
loss_result = torch.sum(loss(a,b),dim=0) 

I don't think there is a direct way to specify at the initialisation of the loss to which dimension to apply mean/sum.我认为没有一种直接的方法可以在损失的初始化时指定应用均值/总和的维度。 Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将输入视为复张量? 运行时错误:张量必须具有步长为 1 的最后一个维度 - How can I get a view of input as a complex tensor? RuntimeError: Tensor must have a last dimension with stride 1 如何在 TensorFlow 中获取张量的特定行? - How can I get specific rows of a tensor in TensorFlow? 在 Pytorch 中计算 4D 张量的一个特定维度的平均值 - Calculate mean across one specific dimension of a 4D tensor in Pytorch 如何以MSE最小化的方式收敛权重? - How can I get weights converged in a way that MSE minimizes? 如何获得相同维度的两个图像中每个像素的MSE - How to get the MSE of each pixel in two images of the same dimension 如何沿某个维度向 PyTorch 张量添加元素? - How can I add an element to a PyTorch tensor along a certain dimension? 使用 Softmax 后如何减少张量的维数? - How can I reduce dimension of a tensor after using Softmax? 如何通过保留该维度的特定索引元素来减少张量的维度? - How to reduce dimension of a tensor with keeping specific indexed elements of that dimension? 如何使用 PyTorch 在维度上删除全部为零的元素? - How can I remove elements across a dimension that are all zero with PyTorch? 如何获得1维到3维的整形? - how can i get 1dimension to 3dimension reshape?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM