numerical
optimus_dl.core.numerical
¶
get_item(number)
¶
Extract the scalar value from a tensor or array.
Source code in optimus_dl/core/numerical.py
safe_round(number, ndigits)
¶
Safely round a number, handling various numeric types.
This function handles rounding for Python numbers, PyTorch tensors, and NumPy arrays. It recursively handles nested types (e.g., single-element tensors) until it reaches a roundable Python number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number
|
ScalarLike
|
The number to round. Can be a Python number, PyTorch tensor, or NumPy array. |
required |
ndigits
|
int | None
|
Number of decimal places to round to. If None, returns the number unchanged. |
required |
Returns:
| Type | Description |
|---|---|
float | int
|
Rounded number as float or int (depending on whether rounding occurred). |