我想问一下,当在pytorch中使用
nn.MSELoss()
和reduction="mean"
计算类似(小批量,特征,序列长度)的时间序列数据的MSE损失时,平均值仅针对小批量?或者也隐含着时间顺序?
为了确认计算结果以检查我上面提出的疑问,我打印了下面的代码
nn.MSELoss() #reduction='mean' is default
x_t = torch.ones((32, 4, 100)) # [minibatch size, feature size, time sequence length]
x_est = torch.ones((32, 4, 100)) * 2
loss_result = loss(x_t, x_est)
print(loss_result)
>>> tensor(1.)
nn.MSELoss
损失计算两个输入之间逐点的 L2 距离。此外,缩减参数决定是否对结果张量进行平均或求和,在这种情况下,它将在所有维度上应用缩减。这是一个比较:
>>> x_t = torch.rand((32, 4, 100))
>>> x_est = torch.rand((32, 4, 100))
与
nn.MSELoss
:
>>> loss(x_t, x_est)
使用内置张量方法:
>>> (x_t-x_est).pow(2).mean()