-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add function to reduce tensors (similar to reduction in torch.nn)
- Loading branch information
1 parent
79f0731
commit b193059
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import torch | ||
|
||
|
||
def reduce(to_reduce: torch.Tensor, reduction: str) -> torch.Tensor: | ||
""" | ||
reduces a given tensor by a given reduction method | ||
Parameters | ||
---------- | ||
to_reduce : torch.Tensor | ||
the tensor, which shall be reduced | ||
reduction : str | ||
a string specifying the reduction method. | ||
should be one of 'elementwise_mean' | 'none' | 'sum' | ||
Returns | ||
------- | ||
torch.Tensor | ||
reduced Tensor | ||
Raises | ||
------ | ||
ValueError | ||
if an invalid reduction parameter was given | ||
""" | ||
if reduction == 'elementwise_mean': | ||
return torch.mean(to_reduce) | ||
if reduction == 'none': | ||
return to_reduce | ||
if reduction == 'sum': | ||
return torch.sum(to_reduce) | ||
raise ValueError('Reduction parameter unknown.') |