torch.Tensor.Tensor.addcdiv
Tensor.addcdiv<T1 extends Shape, T2 extends Shape>(tensor1: Tensor<T1>, tensor2: Tensor<T2>, options?: ValueOptions): Tensor<DynamicShape, D, Dev>Tensor.addcdiv<T1 extends Shape, T2 extends Shape>(tensor1: Tensor<T1>, tensor2: Tensor<T2>, value: number, options?: ValueOptions): Tensor<DynamicShape, D, Dev>Performs out = self + (tensor1 / tensor2) * value element-wise.
Computes a scaled division of two tensors and adds it to this tensor. Useful for:
- Gradient update rules (e.g., parameter -= learning_rate * (loss_grad / param_grad))
- Normalized addition operations
- Adaptive learning rate computation
- Ratio-based updates in optimization algorithms
- Denominator must be non-zero (will produce inf/nan otherwise)
- All three tensors must have broadcastable shapes
- Inverse of addcmul: uses division instead of multiplication
Parameters
tensor1Tensor<T1>- Numerator tensor
tensor2Tensor<T2>- Denominator tensor
optionsValueOptionsoptional
Returns
Tensor<DynamicShape, D, Dev>– Tensor with out = self + (tensor1 / tensor2) * valueExamples
// Adaptive learning rate update
const params = torch.ones(3);
const gradients = torch.tensor([0.1, 0.2, 0.3]);
const adaptive_scale = torch.tensor([0.5, 1.0, 2.0]);
const lr = 0.01;
const updated = params.addcdiv(gradients, adaptive_scale, lr);
// params -= lr * (gradients / adaptive_scale)
// Normalized update
const values = torch.tensor([1, 2, 3]);
const numerator = torch.tensor([10, 20, 30]);
const denominator = torch.tensor([2, 4, 5]);
values.addcdiv(numerator, denominator, 2);
// [1 + (10/2)*2, 2 + (20/4)*2, 3 + (30/5)*2] = [11, 12, 15]See Also
- PyTorch torch.addcdiv()
- addcmul - Similar but uses multiplication instead of division
- div - Element-wise division
- add - Element-wise addition