[英]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张量与
.size
的torch.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个通道,使我的错误张量有
.size
的torch.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.