Signature
Derivative table representation utilities
- ndmap.signature.apply(table: list, function: Callable) None
Apply function to all bottom elements
- Parameters:
table (Table) – input derivative table representation
function (Callable) – function to apply
- Return type:
None
Examples
>>> import torch >>> from ndmap.derivative import derivative >>> def fn(x, y): ... x1, x2 = x ... y1, y2 = y ... return (x1 + x2 + x1**2 + x1*x2 + x2**2)*(1 + y1 + y2) >>> x = torch.tensor([0.0, 0.0]) >>> y = torch.zeros_like(x) >>> t = derivative((2, 1), fn, x, y) >>> apply(t, torch.norm) >>> t [[tensor(0.), tensor(0.)], [tensor(1.4142), tensor(2.)], [tensor(3.1623), tensor(4.4721)]]
- ndmap.signature.chop(table: list, *, threshold: float = 1e-09, value: float = 0.0, replace: bool = False) None [source]
Chop tensor elements in a table below a given threshold
- Parameters:
table (Table) – input derivative table representation
threshold (float, default=1.0E-9) – threshold value
value (float, default=0.0) – set value
replace (bool, default=False) – flag to replace zero tensors
- Return type:
None
Examples
>>> import torch >>> from ndmap.derivative import derivative >>> def fn(x, y): ... x1, x2 = x ... y1, y2 = y ... return (x1 + x2 + x1**2 + x1*x2 + x2**2)*(1 + y1 + y2) >>> x = torch.tensor([0.0, 0.0]) >>> y = torch.zeros_like(x) >>> t = derivative((0, 1), fn, x, y) >>> apply(t, lambda x: x + 1.0E-10) >>> chop(t) >>> t [[tensor(0.), tensor([0., 0.])]]
- ndmap.signature.get(table: list, index: tuple[int, ...]) torch.Tensor | list [source]
Get derivative table element at a given (bottom) element signature
Note, index can correspond to a bottom element or a subtable
- Parameters:
table (Table) – input derivative table representation
index (tuple[int, ...]) – element signature
- Returns:
element value
- Return type:
Union[Tensor, Table]
Examples
>>> import torch >>> from ndmap.derivative import derivative >>> def fn(x, y): ... x1, x2 = x ... y1, y2 = y ... return (x1 + x2 + x1**2 + x1*x2 + x2**2)*(1 + y1 + y2) >>> x = torch.tensor([0.0, 0.0]) >>> y = torch.zeros_like(x) >>> t = derivative((2, 1), fn, x, y) >>> get(t, (1, 1)) tensor([[1., 1.], [1., 1.]])
- ndmap.signature.set(table: list, index: tuple[int, ...], value: torch.Tensor | list) None [source]
Set derivative table element at a given (bottom) element signature
Note, index can correspond to a bottom element or a subtable
- Parameters:
table (Table) – input derivative table representation
index (tuple[int, ...]) – element signature
value (Union[Tensor, Table]) – element value
- Return type:
None
Examples
>>> import torch >>> from ndmap.derivative import derivative >>> def fn(x, y): ... x1, x2 = x ... y1, y2 = y ... return (x1 + x2 + x1**2 + x1*x2 + x2**2)*(1 + y1 + y2) >>> x = torch.tensor([0.0, 0.0]) >>> y = torch.zeros_like(x) >>> t = derivative((2, 1), fn, x, y) >>> set(t, (1, 1), 1 + get(t, (1, 1))) >>> get(t, (1, 1)) tensor([[2., 2.], [2., 2.]])
- ndmap.signature.signature(index: list[int], table: torch.Tensor, *, factor: bool = False)
(auxiliary)
- ndmap.signature.to(table: list, *args: tuple, **kwargs: dict) None [source]
Perform dtype and/or device conversion for all bottom table element elements (tensors)
- Parameters:
*args (tuple) – passed to torch.to function
**kwargs (dict) – passed to torch.to function
- Return type:
None
Examples
>>> import torch >>> from ndmap.derivative import derivative >>> x = torch.tensor(0.0) >>> y = torch.tensor(0.0) >>> t = derivative((1, 1), lambda x, y: x + y, x, y) >>> to(t, torch.float64) >>> t [[tensor(0., dtype=torch.float64), tensor(1., dtype=torch.float64)], [tensor(1., dtype=torch.float64), tensor(0., dtype=torch.float64)]] [[tensor(0.), tensor([0., 0.])]]