Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/gluonts/torch/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ def weighted_average(
weights != 0, x * weights, torch.zeros_like(x)
)
sum_weights = torch.clamp(
weights.sum(dim=dim) if dim else weights.sum(), min=1.0
weights.sum(dim=dim) if dim is not None else weights.sum(),
min=1.0,
)
return (
weighted_tensor.sum(dim=dim) if dim else weighted_tensor.sum()
weighted_tensor.sum(dim=dim)
if dim is not None
else weighted_tensor.sum()
) / sum_weights
else:
return x.mean(dim=dim)
Expand Down
10 changes: 10 additions & 0 deletions test/torch/test_torch_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@
from gluonts.torch.util import (
lagged_sequence_values,
unsqueeze_expand,
weighted_average,
)


def test_weighted_average_dim_zero():
x = torch.tensor([[1.0, 10.0], [3.0, 30.0]])
weights = torch.tensor([[1.0, 1.0], [3.0, 1.0]])

result = weighted_average(x, weights=weights, dim=0)

torch.testing.assert_close(result, torch.tensor([2.5, 20.0]))


@pytest.mark.parametrize(
"lag_indices, prior_sequence, sequence, output_shape",
[
Expand Down