lucid.multiply¶
The multiply function performs element-wise multiplication between two Tensor objects. It returns a new Tensor representing the product, with gradient support for backpropagation.
Function Signature¶
def mul(a: Tensor, b: Tensor) -> Tensor
Parameters¶
a (Tensor): The first tensor in the multiplication operation.
b (Tensor): The second tensor in the multiplication operation.
Returns¶
- Tensor:
A new Tensor representing the element-wise product of a and b. If either a or b requires gradients, the resulting tensor will also require gradients.
Forward Calculation¶
The forward calculation for the multiplication operation is:
where \(a\) and \(b\) are the data contained in the tensors a and b, respectively.
Backward Gradient Calculation¶
For each tensor a and b involved in the multiplication, the gradient with respect to the output (out) is computed as follows:
Examples¶
Using multiply to multiply two tensors:
>>> import lucid
>>> a = Tensor([1.0, 2.0, 3.0], requires_grad=True)
>>> b = Tensor([4.0, 5.0, 6.0], requires_grad=True)
>>> out = lucid.multiply(a, b)
>>> print(out)
Tensor([4.0, 10.0, 18.0], grad=None)
After calling backward() on out, gradients for a and b will be accumulated based on the backpropagation rules:
>>> out.backward()
>>> print(a.grad)
[4.0, 5.0, 6.0]
>>> print(b.grad)
[1.0, 2.0, 3.0]