My aggregated monorepo of OCaml code, automaintained
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix TF.js SVD: access return array by index, not property name

tf.linalg.svd returns [s, u, v] as a JS array, not {s, u, v} object.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+7 -5
+7 -5
tessera-tfjs/lib/tfjs.ml
··· 63 63 let cov = call xtx "div" [Js.Unsafe.inject scale] in 64 64 (* 5. SVD of symmetric covariance matrix. 65 65 For symmetric PSD matrices, SVD gives eigendecomposition: 66 - V columns are eigenvectors, S values are eigenvalues. *) 67 - let svd_result = Js.Unsafe.meth_call (get tf "linalg") "svd" 66 + V columns are eigenvectors, S values are eigenvalues. 67 + tf.linalg.svd returns [s, u, v] as a JS array (not an object). *) 68 + let svd_arr = Js.Unsafe.meth_call (get tf "linalg") "svd" 68 69 [| Js.Unsafe.inject cov; Js.Unsafe.inject Js._true |] in 69 - let v_full = get svd_result "v" in 70 + let svd_s = Js.array_get svd_arr 0 |> Js.Optdef.to_option |> Option.get in 71 + let svd_u = Js.array_get svd_arr 1 |> Js.Optdef.to_option |> Option.get in 72 + let v_full = Js.array_get svd_arr 2 |> Js.Optdef.to_option |> Option.get in 70 73 (* 6. Take top n_components columns of V *) 71 74 let v_top = Js.Unsafe.meth_call v_full "slice" 72 75 [| Js.Unsafe.inject (Js.array [| 0; 0 |]); ··· 78 81 let result = tensor_to_mat projected in 79 82 (* 9. Clean up all tensors *) 80 83 List.iter dispose [x; mean; x_centered; xt; xtx; scale; cov; 81 - get svd_result "s"; get svd_result "u"; v_full; 82 - v_top; projected]; 84 + svd_s; svd_u; v_full; v_top; projected]; 83 85 result