Aiming for Jarvis, Creating D.A.N.I.

Tutorial 27: Tensors and maths

This tutorial introduces std/tensor for numerical computing and std/math for common mathematical functions. We use tensors because, eventually, everything in machine learning and data processing boils down to shuffling multidimensional arrays of numbers.

Creating tensors

Tensors are initialized by shape:

module main

import "std/tensor"

fn main() -> int {
    // Create a 2x3 matrix of zeros
    a := tensor.Zeros([2, 3])
    // Create a 2x3 matrix of ones
    b := tensor.Ones([2, 3])
    
    if a.Rank() == 2 && b.Rank() == 2 {
        print("Tensor rank: {a.Rank()}")
        print("Shape length: {len(a.Shape())}")
    }
    return 0
}

Element-wise operations

Operations like Add, Sub, and Mul are element-wise. Scale performs scalar multiplication.

sum := tensor.Add(a, b)
diff := tensor.Sub(a, b)
prod := tensor.Mul(a, b)
scaled := tensor.Scale(a, 2.0)

Indexing and setting values

Accessing data is done via coordinate arrays:

a.Set([0, 1], 5.0)
val := a.Get([0, 1])
print("Value at (0,1): {val}")

Matrix multiplication

For matrix multiplication, ensure your inner dimensions match—or prepare for a runtime error.

m := tensor.Zeros([2, 3])
n := tensor.Zeros([3, 4])

// Result will have shape [2, 4]
result := tensor.MatMul(m, n)
print("Result shape: {result.Shape()}")

Math helpers

std/math provides standard C-style math functions:

import "std/math"

fn main() -> int {
    angle := math.Pi / 2.0
    print("sin: {math.Sin(angle)}")
    print("sqrt: {math.Sqrt(16.0)}")
    return 0
}

Exercises

  1. Initialize two 2x2 matrices and compute their sum.
  2. Perform a matrix multiplication of a 3x2 matrix and a 2x3 matrix.
  3. Use tensor.Sigmoid on a tensor with large values and verify that all results are clamped between and .

Next

Building a custom std module