Rate this Page
★ ★ ★ ★ ★

TensorDictBase.addcmul#

TensorDictBase.addcmul(other1: TensorDictBase | Tensor, other2: TensorDictBase | Tensor, *, value: float | None = 1) → Self#

Returns a new tensordict with value * other1 * other2 added to every leaf.

\[\text{out}_i = \text{input}_i + \text{value} \times \text{other1}_i \times \text{other2}_i\]
Parameters:
  • other1 (TensorDictBase or Tensor) – the first factor. A tensordict is paired with the tensordict key by key and must have all its keys. A tensor is aligned with the batch dimensions of each leaf and expanded over its feature dimensions.

  • other2 (TensorDictBase or Tensor) – the second factor, paired like other1. One of other1 and other2 can be a tensordict and the other a tensor.

Keyword Arguments:

value (Number, optional) – the multiplier for \(\text{other1} \times \text{other2}\). Defaults to 1.

Examples

>>> import torch
>>> from tensordict import TensorDict
>>> td = TensorDict(x=torch.ones(2), batch_size=[2])
>>> td.addcmul(torch.tensor([1.0, 2.0]), torch.tensor([3.0, 3.0]), value=2)["x"]
tensor([ 7., 13.])

Every tensordict class and every tensorclass has this method. For the other methods, see Methods by task.