批量广播张量矩阵乘法

问题描述 投票:0回答:1

我如何找到每个批次响应和 X 数据的点积。

y_yhat_allBatches_matmulX_allBatches = torch.matmul(yTrue_yHat_allBatches_tensorSub, interceptXY_data_allBatches[:, :, :-1])

y_yhat_allBatches_matmulX_allBatches
的预期形状应为 2 x 5。其中每行代表特定批次

yTrue_yHat_allBatches_tensorSub.shape
=
[2, 15]
,其中行批次 (1&2) 和列 = 响应大小 (15)

interceptXY_data_allBatches[:, :, :-1].shape = torch.Size([2, 15, 5])
2 个批次的 5 个特征的 15 个观察值

请查看完整的可重现代码

#define dataset
nFeatures_withIntercept = 5
NObservations = 15
miniBatches = 2
interceptXY_data_allBatches = torch.randn(miniBatches, NObservations, nFeatures_withIntercept+1) #+1 Y(response variable)

#random assign beta to work with
beta_holder = torch.rand(nFeatures_withIntercept)

#y_predicted for each mini-batch
y_predBatchAllBatches = torch.matmul(interceptXY_data_allBatches[:, :, :-1], beta_holder)

#y_true - y_predicted for each mini-batch
yTrue_yHat_allBatches_tensorSub = torch.sub(interceptXY_data_allBatches[..., -1], y_predBatchAllBatches)
y_yhat_allBatches_matmulX_allBatches = torch.matmul(yTrue_yHat_allBatches_tensorSub, interceptXY_data_allBatches[:, :, :-1])
python tensorflow pytorch array-broadcasting
1个回答
0
投票

看起来你有:

  • yTrue_yHat_allBatches_tensorSub
    形状 (2, 15)
  • interceptXY_data_allBatches[:, :, :-1]
    形状 (2, 15, 5)

如果你想将它们相乘得到结果形状 (2, 5),那么你需要使用

.unsqueeze(dim=1)
将第一个形状变成 (2, 1, 15)。然后,您可以使用
torch.bmm()
@
运算符将 (2, 1, 15) 乘以 (2, 15, 5),得到形状为 (2, 1, 5) 的结果。最后,
.squeeze
将结果去掉单维,得到(2, 5)。

y_yhat_allBatches_matmulX_allBatches =\
    torch.bmm(yTrue_yHat_allBatches_tensorSub.unsqueeze(dim=1),
              interceptXY_data_allBatches[:, :, :-1]
             ).squeeze()

使用

@
运算符更紧凑的表示法:

y_yhat_allBatches_matmulX_allBatches =\
    (yTrue_yHat_allBatches_tensorSub.unsqueeze(dim=1) @ interceptXY_data_allBatches[:, :, :-1]).squeeze()
© www.soinside.com 2019 - 2024. All rights reserved.