我目前正在使用 torch.sub 和 torch.div 來獲取我的神經網路的預測標簽和真實標簽之間的 MAPE,盡管我沒有得到我期望的答案。根據檔案中的示例,我應該得到一個 4x1 張量,而不是 4x4。誰能幫我解決這個問題?
print('y_true ', y_true)
y_true tensor([[ 46],
[262],
[ 33],
[ 35]], device=‘cuda:0’, dtype=torch.int16)
print('y_pred ', y_pred)
y_pred tensor([[[308.5075]],
[[375.8983]],
[[389.4587]],
[[406.4957]]], device=‘cuda:0’, grad_fn=)
print('torch.sub ', torch.sub(y_true, y_pred))
torch.sub tensor([[[-262.5075],
[ -46.5075],
[-275.5075],
[-273.5075]],
[[-329.8983],
[-113.8983],
[-342.8983],
[-340.8983]],
[[-343.4587],
[-127.4587],
[-356.4587],
[-354.4587]],
[[-360.4957],
[-144.4957],
[-373.4957],
[-371.4957]]], device='cuda:0', grad_fn=<SubBackward0>)
uj5u.com熱心網友回復:
那是因為y_pred有一個額外的維度,這意味著y_true張量可能被廣播到正確的維度。
如果您洗掉額外的最后一個維度,您將獲得所需的結果:
>>> torch.sub(y_true, y_pred[...,0]).shape
torch.Size([4, 1])
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405956.html
標籤:
上一篇:用變數創建陣列
下一篇:如何索引9x9數獨板的3x3子塊
