KeyedTensor

class KeyedTensor(iterable_or_mapping=None, **kwargs)

Bases: collectionish._attydict.AttyDict

abs()

Like torch.abs but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.abs()
KeyedTensor(a=tensor([0.0075, 0.5364, 0.8230]), b=tensor([0.7359, 0.3852]))
>>> torch.abs(kt)
KeyedTensor(a=tensor([0.0075, 0.5364, 0.8230]), b=tensor([0.7359, 0.3852]))
Return type

KeyedTensor

acos()

Like torch.acos but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.acos()
KeyedTensor(a=tensor([1.5783, 1.0046, 2.5375]), b=tensor([2.3978, 1.9662]))
>>> torch.acos(kt)
KeyedTensor(a=tensor([1.5783, 1.0046, 2.5375]), b=tensor([2.3978, 1.9662]))
Return type

KeyedTensor

add(other)
Return type

KeyedTensor

all(dim=None, **kwargs)

Like torch.all but for keyed tensor. dim may optionally be a keyed

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

any(dim=None, **kwargs)

Like torch.any but for keyed tensor, dim may optionally be a keyed

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

argmax(dim=None, **kwargs)

Like torch.argmax but for keyed tensor, dim may optionally be a keyed

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> from keyedtensor import KeyedTensor
>>>
>>> _ = torch.manual_seed(0)
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt.argmax(dim=-1)
KeyedTensor(a=tensor([1, 2, 1]), b=tensor(0))
>>> kt.argmax(dim='key')
KeyedTensor(a=tensor(7), b=tensor(0))
argmin(dim=None, **kwargs)

Like torch.argmin but for keyed tensor, dim may optionally be a keyed

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> from keyedtensor import KeyedTensor
>>>
>>> _ = torch.manual_seed(0)
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt.argmin(dim=-1)
KeyedTensor(a=tensor([2, 0, 2]), b=tensor(1))
>>> kt.argmin(dim='key')
KeyedTensor(a=tensor(2), b=tensor(1))
asin()

Like torch.asin but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.asin()
KeyedTensor(a=tensor([-0.0075,  0.5662, -0.9668]), b=tensor([-0.8271, -0.3954]))
>>> torch.asin(kt)
KeyedTensor(a=tensor([-0.0075,  0.5662, -0.9668]), b=tensor([-0.8271, -0.3954]))
Return type

KeyedTensor

atan()

Like torch.atan but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.atan()
KeyedTensor(a=tensor([-0.0075,  0.4924, -0.6886]), b=tensor([-0.6344, -0.3676]))
>>> torch.atan(kt)
KeyedTensor(a=tensor([-0.0075,  0.4924, -0.6886]), b=tensor([-0.6344, -0.3676]))
Return type

KeyedTensor

ceil()

Like torch.ceil but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.ceil()
KeyedTensor(a=tensor([-0., 1., -0.]), b=tensor([-0., -0.]))
>>> torch.ceil(kt)
KeyedTensor(a=tensor([-0., 1., -0.]), b=tensor([-0., -0.]))
Return type

KeyedTensor

chunk(chunks, dim=0)
Return type

List[KeyedTensor]

cos()

Like torch.cos but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.cos()
KeyedTensor(a=tensor([1.0000, 0.8595, 0.6800]), b=tensor([0.7412, 0.9267]))
>>> torch.cos(kt)
KeyedTensor(a=tensor([1.0000, 0.8595, 0.6800]), b=tensor([0.7412, 0.9267]))
Return type

KeyedTensor

cosh()

Like torch.cosh but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.cosh()
KeyedTensor(a=tensor([1.0000, 1.1474, 1.3583]), b=tensor([1.2832, 1.0751]))
>>> torch.cosh(kt)
KeyedTensor(a=tensor([1.0000, 1.1474, 1.3583]), b=tensor([1.2832, 1.0751]))
Return type

KeyedTensor

cuda(*args, **kwargs)
Return type

KeyedTensor

property data
Return type

KeyedTensor

detach()

Like torch.detach but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.detach()
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> torch.detach(kt)
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
Return type

KeyedTensor

digamma()

Like torch.digamma but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.digamma()
KeyedTensor(a=tensor([132.9785,  -1.7941,  -4.7548]), b=tensor([-2.6389,  1.1087]))
>>> torch.digamma(kt)
KeyedTensor(a=tensor([132.9785,  -1.7941,  -4.7548]), b=tensor([-2.6389,  1.1087]))
Return type

KeyedTensor

dim()
div(other)
Return type

KeyedTensor

eq(other)
Return type

KeyedTensor

erf()

Like torch.erf but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.erf()
KeyedTensor(a=tensor([-0.0084,  0.5519, -0.7556]), b=tensor([-0.7020, -0.4140]))
>>> torch.erf(kt)
KeyedTensor(a=tensor([-0.0084,  0.5519, -0.7556]), b=tensor([-0.7020, -0.4140]))
Return type

KeyedTensor

erfc()

Like torch.erfc but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.erfc()
KeyedTensor(a=tensor([1.0084, 0.4481, 1.7556]), b=tensor([1.7020, 1.4140]))
>>> torch.erfc(kt)
KeyedTensor(a=tensor([1.0084, 0.4481, 1.7556]), b=tensor([1.7020, 1.4140]))
Return type

KeyedTensor

erfinv()

Like torch.erfinv but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.erfinv()
KeyedTensor(a=tensor([-0.0066,  0.5183, -0.9547]), b=tensor([-0.7897, -0.3558]))
>>> torch.erfinv(kt)
KeyedTensor(a=tensor([-0.0066,  0.5183, -0.9547]), b=tensor([-0.7897, -0.3558]))
Return type

KeyedTensor

exp()

Like torch.exp but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.exp()
KeyedTensor(a=tensor([0.9925, 1.7099, 0.4391]), b=tensor([0.4791, 0.6803]))
>>> torch.exp(kt)
KeyedTensor(a=tensor([0.9925, 1.7099, 0.4391]), b=tensor([0.4791, 0.6803]))
Return type

KeyedTensor

expm1()

Like torch.expm1 but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.expm1()
KeyedTensor(a=tensor([-0.0075,  0.7099, -0.5609]), b=tensor([-0.5209, -0.3197]))
>>> torch.expm1(kt)
KeyedTensor(a=tensor([-0.0075,  0.7099, -0.5609]), b=tensor([-0.5209, -0.3197]))
Return type

KeyedTensor

floor()

Like torch.floor but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.floor()
KeyedTensor(a=tensor([-1.,  0., -1.]), b=tensor([-1., -1.]))
>>> torch.floor(kt)
KeyedTensor(a=tensor([-1.,  0., -1.]), b=tensor([-1., -1.]))
Return type

KeyedTensor

frac()

Like torch.frac but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.frac()
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> torch.frac(kt)
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
Return type

KeyedTensor

ge(other)
Return type

KeyedTensor

hardshrink(*args, **kwargs)
Return type

KeyedTensor

isfinite()

Like torch.isfinite but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=[float('inf'),  0.5364, -0.8230], b=[-0.7359, -0.3852])
>>> kt
KeyedTensor(a=tensor([    inf,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.isinf()
KeyedTensor(a=tensor([ True, False, False]), b=tensor([False, False]))
>>> torch.isinf(kt)
KeyedTensor(a=tensor([ True, False, False]), b=tensor([False, False]))
Return type

KeyedTensor

isinf()

Like torch.isinf but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=[float('inf'),  0.5364, -0.8230], b=[-0.7359, -0.3852])
>>> kt
KeyedTensor(a=tensor([    inf,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.isinf()
KeyedTensor(a=tensor([ True, False, False]), b=tensor([False, False]))
>>> torch.isinf(kt)
KeyedTensor(a=tensor([ True, False, False]), b=tensor([False, False]))
Return type

KeyedTensor

isnan()

Like torch.isnan but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = torch.sqrt(KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1)
>>> kt
KeyedTensor(a=tensor([   nan, 0.7324,    nan]), b=tensor([nan, nan]))
>>> kt.isnan()
KeyedTensor(a=tensor([ True, False,  True]), b=tensor([True, True]))
>>> torch.isnan(kt)
KeyedTensor(a=tensor([ True, False,  True]), b=tensor([True, True]))
Return type

KeyedTensor

le(other)
Return type

KeyedTensor

lgamma()

Like torch.lgamma but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.lgamma()
KeyedTensor(a=tensor([4.8990, 0.5040, 1.8482]), b=tensor([1.5368, 1.3299]))
>>> torch.lgamma(kt)
KeyedTensor(a=tensor([4.8990, 0.5040, 1.8482]), b=tensor([1.5368, 1.3299]))
Return type

KeyedTensor

log()

Like torch.log but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2))
>>> kt
KeyedTensor(a=tensor([0.4963, 0.7682, 0.0885]), b=tensor([0.1320, 0.3074]))
>>> kt.log()
KeyedTensor(a=tensor([-0.7007, -0.2637, -2.4250]), b=tensor([-2.0247, -1.1795]))
>>> torch.log(kt)
KeyedTensor(a=tensor([-0.7007, -0.2637, -2.4250]), b=tensor([-2.0247, -1.1795]))
Return type

KeyedTensor

log10()

Like torch.log10 but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2))
>>> kt
KeyedTensor(a=tensor([0.4963, 0.7682, 0.0885]), b=tensor([0.1320, 0.3074]))
>>> kt.log10()
KeyedTensor(a=tensor([-0.3043, -0.1145, -1.0532]), b=tensor([-0.8793, -0.5123]))
>>> torch.log10(kt)
KeyedTensor(a=tensor([-0.3043, -0.1145, -1.0532]), b=tensor([-0.8793, -0.5123]))
Return type

KeyedTensor

lt(other)
Return type

KeyedTensor

mean(dim=None, **kwargs)

Like torch.mean but for keyed tensor.

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt
KeyedTensor(a=tensor([[0.4963, 0.7682, 0.0885],
                      [0.1320, 0.3074, 0.6341],
                      [0.4901, 0.8964, 0.4556]]),
            b=tensor([0.6323, 0.3489, 0.4017]))

with dim unspecified we apply mean over all values in the KeyedTensor:

>>> kt.mean()
tensor(0.4710)

specify a numeric dim to apply mean along each keyed tensor:

>>> kt.mean(dim=-1)
KeyedTensor(a=tensor([0.4510, 0.3578, 0.6141]), b=tensor(0.4610))

or specify key to apply mean per keyed tensor:

>>> kt.mean(dim='key')
KeyedTensor(a=tensor(0.4743), b=tensor(0.4610))

calling torch.mean on a KeyedTensor works as expected:

>>> torch.mean(kt, dim='key')
KeyedTensor(a=tensor(0.4743), b=tensor(0.4610))
mul(other)
Return type

KeyedTensor

neg()

Like torch.neg but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.neg()
KeyedTensor(a=tensor([ 0.0075, -0.5364,  0.8230]), b=tensor([0.7359, 0.3852]))
>>> torch.neg(kt)
KeyedTensor(a=tensor([ 0.0075, -0.5364,  0.8230]), b=tensor([0.7359, 0.3852]))
Return type

KeyedTensor

norm(p='fro', dim=None, **kwargs)

Like torch.norm but for keyed tensor, dim may optionally be a keyed

Parameters
  • p – norm type (see torch.norm for full details. Defaults to ‘fro’.

  • dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> from keyedtensor import KeyedTensor
>>> _ = torch.manual_seed(0)
>>>
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt.norm()
tensor(1.8145)
>>> kt.norm(dim=-1)
KeyedTensor(a=tensor([0.9188, 0.7169, 1.1187]), b=tensor(0.8264))
>>> kt.norm(dim='key')
KeyedTensor(a=tensor(1.6154), b=tensor(0.8264))
>>> kt.norm(p=1, dim='key')
KeyedTensor(a=tensor(4.2687), b=tensor(1.3829))
numel()

Like torch.numel but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.numel()
KeyedTensor(a=tensor(3), b=tensor(2))
>>> torch.numel(kt)
KeyedTensor(a=tensor(3), b=tensor(2))
Return type

KeyedTensor

polygamma(*args, **kwargs)
pow(other)
Return type

KeyedTensor

prod(dim=None, **kwargs)

Like torch.prod but for keyed tensor.

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt
KeyedTensor(a=tensor([[0.4963, 0.7682, 0.0885],
                      [0.1320, 0.3074, 0.6341],
                      [0.4901, 0.8964, 0.4556]]),
            b=tensor([0.6323, 0.3489, 0.4017]))

with dim unspecified we apply prod over all values in the KeyedTensor:

>>> kt.prod()
tensor(1.5400e-05)

specify a numeric dim to apply prod along each keyed tensor:

>>> kt.prod(dim=-1)
KeyedTensor(a=tensor([0.0337, 0.0257, 0.2002]), b=tensor(0.0886))

or specify key to apply prod per keyed tensor:

>>> kt.prod(dim='key')
KeyedTensor(a=tensor(0.0002), b=tensor(0.0886))

calling torch.prod on a KeyedTensor works as expected:

>>> torch.prod(kt, dim='key')
KeyedTensor(a=tensor(0.0002), b=tensor(0.0886))
reciprocal()

Like torch.reciprocal but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.reciprocal()
KeyedTensor(a=tensor([-133.5681,    1.8641,   -1.2150]), b=tensor([-1.3588, -2.5964]))
>>> torch.reciprocal(kt)
KeyedTensor(a=tensor([-133.5681,    1.8641,   -1.2150]), b=tensor([-1.3588, -2.5964]))
Return type

KeyedTensor

relu()

Like torch.relu but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.relu()
KeyedTensor(a=tensor([0.0000, 0.5364, 0.0000]), b=tensor([0., 0.]))
>>> torch.relu(kt)
KeyedTensor(a=tensor([0.0000, 0.5364, 0.0000]), b=tensor([0., 0.]))
Return type

KeyedTensor

round()

Like torch.round but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.round()
KeyedTensor(a=tensor([-0.,  1., -1.]), b=tensor([-1., -0.]))
>>> torch.round(kt)
KeyedTensor(a=tensor([-0.,  1., -1.]), b=tensor([-1., -0.]))
Return type

KeyedTensor

rsqrt()

Like torch.rsqrt but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2))
>>> kt
KeyedTensor(a=tensor([0.4963, 0.7682, 0.0885]), b=tensor([0.1320, 0.3074]))
>>> kt.rsqrt()
KeyedTensor(a=tensor([1.4195, 1.1409, 3.3619]), b=tensor([2.7521, 1.8036]))
>>> torch.rsqrt(kt)
KeyedTensor(a=tensor([1.4195, 1.1409, 3.3619]), b=tensor([2.7521, 1.8036]))
Return type

KeyedTensor

property shape

return a KeyedSize for this KeyedTensor

see keyedtensor.KeyedSize docs for more details on working with KeyedSize.

Example

>>> from keyedtensor import KeyedTensor
>>> import torch
>>>
>>> x = KeyedTensor(a=torch.rand(3, 4), b=torch.rand(3, 1), c=torch.rand(3))
>>> x.size()
KeyedSize(a=torch.Size([3, 4]), b=torch.Size([3, 1]), c=torch.Size([3]))
Return type

KeyedSize

sigmoid()

Like torch.sigmoid but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.sigmoid()
KeyedTensor(a=tensor([0.4981, 0.6310, 0.3051]), b=tensor([0.3239, 0.4049]))
>>> torch.sigmoid(kt)
KeyedTensor(a=tensor([0.4981, 0.6310, 0.3051]), b=tensor([0.3239, 0.4049]))
Return type

KeyedTensor

sign()

Like torch.sign but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.sign()
KeyedTensor(a=tensor([-1.,  1., -1.]), b=tensor([-1., -1.]))
>>> torch.sign(kt)
KeyedTensor(a=tensor([-1.,  1., -1.]), b=tensor([-1., -1.]))
Return type

KeyedTensor

sin()

Like torch.sin but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.sin()
KeyedTensor(a=tensor([-0.0075,  0.5111, -0.7332]), b=tensor([-0.6713, -0.3757]))
>>> torch.sin(kt)
KeyedTensor(a=tensor([-0.0075,  0.5111, -0.7332]), b=tensor([-0.6713, -0.3757]))
Return type

KeyedTensor

sinh()

Like torch.sinh but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.sinh()
KeyedTensor(a=tensor([-0.0075,  0.5625, -0.9192]), b=tensor([-0.8042, -0.3947]))
>>> torch.sinh(kt)
KeyedTensor(a=tensor([-0.0075,  0.5625, -0.9192]), b=tensor([-0.8042, -0.3947]))
Return type

KeyedTensor

size()
Return type

KeyedSize

split(split_size_or_sections, dim=0)
Return type

List[KeyedTensor]

sqrt()

Like torch.sqrt but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2))
>>> kt
KeyedTensor(a=tensor([0.4963, 0.7682, 0.0885]), b=tensor([0.1320, 0.3074]))
>>> kt.sqrt()
KeyedTensor(a=tensor([0.7045, 0.8765, 0.2975]), b=tensor([0.3634, 0.5545]))
>>> torch.sqrt(kt)
KeyedTensor(a=tensor([0.7045, 0.8765, 0.2975]), b=tensor([0.3634, 0.5545]))
Return type

KeyedTensor

square()

Like torch.square but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.square()
KeyedTensor(a=tensor([5.6052e-05, 2.8777e-01, 6.7740e-01]), b=tensor([0.5416, 0.1483]))
>>> torch.square(kt)
KeyedTensor(a=tensor([5.6052e-05, 2.8777e-01, 6.7740e-01]), b=tensor([0.5416, 0.1483]))
Return type

KeyedTensor

squeeze(*args, **kwargs)
Return type

KeyedTensor

std(dim=None, **kwargs)

Like torch.std but for keyed tensor.

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt
KeyedTensor(a=tensor([[0.4963, 0.7682, 0.0885],
                      [0.1320, 0.3074, 0.6341],
                      [0.4901, 0.8964, 0.4556]]),
            b=tensor([0.6323, 0.3489, 0.4017]))

with dim unspecified we apply std over all values in the KeyedTensor:

>>> kt.std()
tensor(0.2395)

specify a numeric dim to apply std along each keyed tensor:

>>> kt.std(dim=-1)
KeyedTensor(a=tensor([0.3421, 0.2548, 0.2452]), b=tensor(0.1507))

or specify key to apply std per keyed tensor:

>>> kt.std(dim='key')
KeyedTensor(a=tensor(0.2704), b=tensor(0.1507))

calling torch.std on a KeyedTensor works as expected:

>>> torch.std(kt, dim='key')
KeyedTensor(a=tensor(0.2704), b=tensor(0.1507))
sub(other)
Return type

KeyedTensor

sum(dim=None, **kwargs)

Like torch.sum but for keyed tensor.

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt
KeyedTensor(a=tensor([[0.4963, 0.7682, 0.0885],
                      [0.1320, 0.3074, 0.6341],
                      [0.4901, 0.8964, 0.4556]]),
            b=tensor([0.6323, 0.3489, 0.4017]))

with dim unspecified we apply sum over all values in the KeyedTensor:

>>> kt.sum()
tensor(5.6516)

specify a numeric dim to apply sum along each keyed tensor:

>>> kt.sum(dim=-1)
KeyedTensor(a=tensor([1.3530, 1.0735, 1.8422]), b=tensor(1.3829))

or specify key to apply sum per keyed tensor:

>>> kt.sum(dim='key')
KeyedTensor(a=tensor(4.2687), b=tensor(1.3829))

calling torch.sum on a KeyedTensor works as expected:

>>> torch.sum(kt, dim='key')
KeyedTensor(a=tensor(4.2687), b=tensor(1.3829))
t(*args, **kwargs)
tan(*args, **kwargs)
tanh(*args, **kwargs)
to(*args, **kwargs)
torchfunc_registry: keyedtensor._registry.TorchFuncRegistry

a classlevel registry for registring __torch_function__ overides.

transpose(dim0, dim1)
Return type

KeyedTensor

true_divide(other)
Return type

KeyedTensor

trunc()

Like torch.trunc but for keyed tensor.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3), b=torch.rand(2)) * 2 - 1
>>> kt
KeyedTensor(a=tensor([-0.0075,  0.5364, -0.8230]), b=tensor([-0.7359, -0.3852]))
>>> kt.trunc()
KeyedTensor(a=tensor([-0., 0., -0.]), b=tensor([-0., -0.]))
>>> torch.trunc(kt)
KeyedTensor(a=tensor([-0., 0., -0.]), b=tensor([-0., -0.]))
Return type

KeyedTensor

unbind()
Return type

List[KeyedTensor]

unsqueeze(dim)
Return type

KeyedTensor

var(dim=None, **kwargs)

Like torch.var but for keyed tensor.

Parameters

dim (Union[Literal[‘keys’], int, None]) – the dimension to reduce -this may optionally be the string literal ‘key’ to reduce by key. Defaults to None.

Example

>>> import torch
>>> _ = torch.manual_seed(0)
>>> from keyedtensor import KeyedTensor
>>>
>>> kt = KeyedTensor(a=torch.rand(3, 3), b=torch.rand(3))
>>> kt
KeyedTensor(a=tensor([[0.4963, 0.7682, 0.0885],
                      [0.1320, 0.3074, 0.6341],
                      [0.4901, 0.8964, 0.4556]]),
            b=tensor([0.6323, 0.3489, 0.4017]))

with dim unspecified we apply var over all values in the KeyedTensor:

>>> kt.var()
tensor(0.0574)

specify a numeric dim to apply var along each keyed tensor:

>>> kt.var(dim=-1)
KeyedTensor(a=tensor([0.1171, 0.0649, 0.0601]), b=tensor(0.0227))

or specify key to apply var per keyed tensor:

>>> kt.var(dim='key')
KeyedTensor(a=tensor(0.0731), b=tensor(0.0227))

calling torch.var on a KeyedTensor works as expected:

>>> torch.var(kt, dim='key')
KeyedTensor(a=tensor(0.0731), b=tensor(0.0227))