Tensor Operations
15 functions for creating and manipulating tensors
Quick Reference
| Function | Description |
|---|---|
| tensor() | Create tensor from array |
| tensor_zeros() | Create zero-initialized tensor |
| tensor_ones() | Create one-initialized tensor |
| tensor_randn() | Create random normal tensor |
| tensor_add() | Element-wise addition |
| tensor_sub() | Element-wise subtraction |
| tensor_mul() | Element-wise multiplication |
| tensor_div() | Element-wise division |
| tensor_matmul() | Matrix multiplication |
| tensor_reshape() | Reshape tensor dimensions |
| tensor_transpose() | Transpose 2D tensor |
| tensor_sum() | Sum all elements |
| tensor_mean() | Mean of all elements |
| tensor_shape() | Get tensor dimensions |
| tensor_print() | Print tensor with formatting |
tensor()
tensor(values: [float]) → Tensor
Creates a tensor from an array of values. The resulting tensor is 1-dimensional with length equal to the input array.
Parameters
| Parameter | Type | Description |
|---|---|---|
| values | [float] | Array of floating point numbers |
Returns
A 1D tensor containing the input values
Example
// Create a vector
let v = tensor([1.0, 2.0, 3.0, 4.0, 5.0])
// Shape: [5]
// Create a scalar (wrapped in 1D tensor)
let scalar = tensor([42.0])
// Shape: [1]
Note: For multi-dimensional tensors, use tensor_reshape() after creation.
tensor_zeros()
tensor_zeros(shape: [int]) → Tensor
Creates a tensor filled with zeros of the specified shape. Useful for initializing parameters or creating placeholder tensors.
Parameters
| Parameter | Type | Description |
|---|---|---|
| shape | [int] | Dimensions of the tensor (e.g., [3, 4] for 3x4 matrix) |
Returns
A tensor filled with zeros of the specified shape
Examples
// 1D vector of zeros
let v = tensor_zeros([5])
// [0.0, 0.0, 0.0, 0.0, 0.0]
// 2D matrix of zeros (3x4)
let matrix = tensor_zeros([3, 4])
// [[0.0, 0.0, 0.0, 0.0],
// [0.0, 0.0, 0.0, 0.0],
// [0.0, 0.0, 0.0, 0.0]]
// Initialize biases for a neural network layer
let biases = tensor_zeros([128])
Common Use Cases
- Initializing bias parameters (common practice)
- Creating placeholder tensors
- Accumulator tensors for gradient computation
tensor_ones()
tensor_ones(shape: [int]) → Tensor
Creates a tensor filled with ones of the specified shape. Useful for masking operations or constant initialization.
Parameters
| Parameter | Type | Description |
|---|---|---|
| shape | [int] | Dimensions of the tensor |
Examples
// Vector of ones
let v = tensor_ones([3])
// [1.0, 1.0, 1.0]
// Identity matrix (requires additional operations)
let ones = tensor_ones([3, 3])
// Attention mask (all positions visible)
let mask = tensor_ones([128, 128])
tensor_randn()
tensor_randn(shape: [int]) → Tensor
Creates a tensor filled with random values from a standard normal distribution (mean=0, std=1) using the Box-Muller transform. Essential for weight initialization in neural networks.
Parameters
| Parameter | Type | Description |
|---|---|---|
| shape | [int] | Dimensions of the tensor |
Returns
A tensor with random values sampled from N(0, 1)
Examples
// Initialize weights for a 10x5 layer
let weights = tensor_randn([10, 5])
// For better initialization, scale by input size
let fan_in = 784
let w = tensor_randn([784, 128])
w = tensor_mul(w, 1.0 / sqrt(fan_in)) // Xavier/Glorot scaling
// Initialize small random vector
let noise = tensor_randn([100])
Important: Values are drawn from N(0,1). For other distributions, manually scale after generation.
tensor_add()
tensor_add(a: Tensor, b: Tensor) → Tensor
Performs element-wise addition of two tensors. Tensors must have the same shape.
Parameters
| Parameter | Type | Description |
|---|---|---|
| a | Tensor | First tensor |
| b | Tensor | Second tensor (must have same shape as a) |
Returns
Element-wise sum: result[i] = a[i] + b[i]
Examples
let a = tensor([1.0, 2.0, 3.0])
let b = tensor([4.0, 5.0, 6.0])
let c = tensor_add(a, b)
// c = [5.0, 7.0, 9.0]
// Adding bias in a neural network layer
let output = tensor_matmul(input, weights)
output = tensor_add(output, biases)
Error Conditions
Throws error if tensor shapes don't match
tensor_matmul()
tensor_matmul(a: Tensor, b: Tensor) → Tensor
Performs matrix multiplication. For 2D tensors with shapes [M, K] and [K, N], produces a [M, N] result.
Parameters
| Parameter | Type | Description |
|---|---|---|
| a | Tensor | Left matrix [M, K] |
| b | Tensor | Right matrix [K, N] |
Returns
Matrix product with shape [M, N]
Examples
// Basic matrix multiplication
let a = tensor_reshape(tensor([1.0, 2.0, 3.0, 4.0]), [2, 2])
let b = tensor_reshape(tensor([5.0, 6.0, 7.0, 8.0]), [2, 2])
let c = tensor_matmul(a, b)
// c = [[19, 22], [43, 50]]
// Neural network forward pass
let input = tensor([1.0, 0.5]) // [2]
let weights = tensor_randn([2, 128]) // [2, 128]
let hidden = tensor_matmul(tensor_reshape(input, [1, 2]), weights)
// hidden shape: [1, 128]
Mathematical Definition
C[i,j] = Σ(k=0 to K-1) A[i,k] * B[k,j]
Constraint: Inner dimensions must match. If A is [M, K] and B is [K, N], result is [M, N].
tensor_shape()
tensor_shape(t: Tensor) → [int]
Returns the dimensions of a tensor as an array of integers. Useful for inspecting tensor structure and debugging.
Parameters
| Parameter | Type | Description |
|---|---|---|
| t | Tensor | Tensor to inspect |
Returns
Array of integers representing the tensor's dimensions
Examples
// 1D tensor
let v = tensor([1.0, 2.0, 3.0])
let shape = tensor_shape(v)
// shape = [3]
// 2D tensor
let m = tensor_reshape(tensor([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]), [2, 3])
let shape2 = tensor_shape(m)
// shape2 = [2, 3]
// Check dimensions before operations
let a = tensor_randn([10, 20])
let shape_a = tensor_shape(a) // [10, 20]
print("Tensor dimensions: " + str(shape_a))
Common Use Cases
- Debugging tensor dimensions
- Validating shape before matrix operations
- Dynamic reshaping based on input shape
tensor_print()
tensor_print(t: Tensor) → ()
Prints a tensor with formatted output showing both its shape and data. More informative than using print() directly.
Parameters
| Parameter | Type | Description |
|---|---|---|
| t | Tensor | Tensor to display |
Output Format
Tensor(shape: [dimensions], data: [values])
Examples
// Print a vector
let v = tensor([1.0, 2.0, 3.0])
tensor_print(v)
// Output: Tensor(shape: [3], data: [1.0, 2.0, 3.0])
// Print a matrix
let m = tensor_reshape(tensor([1.0, 2.0, 3.0, 4.0]), [2, 2])
tensor_print(m)
// Output: Tensor(shape: [2, 2], data: [1.0, 2.0, 3.0, 4.0])
// Debugging neural network layers
let weights = tensor_randn([784, 128])
print("Layer 1 weights:")
tensor_print(weights)
Tip: Use tensor_print() instead of print() for tensors to see both shape and values clearly.