feat: initial release
Release / gates (push) Successful in 4m38s
Test / test (push) Successful in 5m16s
Release / release (push) Successful in 35s

Assisted-by: GLM 5.3 Flash
This commit is contained in:
2026-09-03 10:00:00 +02:00
commit af4ee19703
617 changed files with 191195 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
// Copyright (c) 2026 Petr Balvín <opensource@petrbalvin.org> (https://petrbalvin.org)
// SPDX-License-Identifier: MIT
package grad
import (
"math"
"sourcedock.dev/petrbalvin/tensor/internal/core"
)
// numericGrad estimates the gradient of a scalar function at a by
// central differences, the reference the analytic backward is checked
// against.
func numericGrad(f func(a *core.Array) float64, a *core.Array) []float64 {
n := a.Len()
out := make([]float64, n)
for i := range n {
hi := 1e-6
up := cloneFlat(a)
down := cloneFlat(a)
up[i] += hi
down[i] -= hi
au, _ := core.FromFloats(up, a.Shape()...)
ad, _ := core.FromFloats(down, a.Shape()...)
out[i] = (f(au) - f(ad)) / (2 * hi)
}
return out
}
func cloneFlat(a *core.Array) []float64 {
out := make([]float64, a.Len())
for i := range a.Len() {
out[i] = a.FloatAt(i)
}
return out
}
func maxAbsDiff(a, b []float64) float64 {
m := 0.0
for i := range a {
if d := math.Abs(a[i] - b[i]); d > m {
m = d
}
}
return m
}