lstsq_rrule

Function lstsq_rrule 

Source
pub fn lstsq_rrule<T, C>(
    ctx: &mut C,
    a: &Tensor<T>,
    b: &Tensor<T>,
    cotangent_solution: Option<&Tensor<T>>,
    cotangent_residuals: Option<&Tensor<T::Real>>,
) -> AdResult<LstsqGrad<T>>
Expand description

Reverse-mode AD rule for least squares (VJP / pullback).

Returns cotangents for both A and b.

ยงExamples

use tenferro_linalg::lstsq_rrule;
use tenferro_prims::CpuContext;
use tenferro_tensor::{Tensor, MemoryOrder};
use tenferro_device::LogicalMemorySpace;

let col = MemoryOrder::ColumnMajor;
let mem = LogicalMemorySpace::MainMemory;
let mut ctx = CpuContext::new(1);
let a = Tensor::from_slice(&[1.0, 0.0, 1.0, 0.0, 1.0, 1.0], &[3, 2], col).unwrap();
let b = Tensor::from_slice(&[1.0, 2.0, 3.0], &[3], col).unwrap();
let dx = Tensor::<f64>::ones(&[2], mem, col).unwrap();
let grad = lstsq_rrule(&mut ctx, &a, &b, Some(&dx), None).unwrap();
// grad.a: cotangent for A, grad.b: cotangent for b