Tensor
A Tensor is a multi-dimensional array. Toro wraps TorchSharp tensors with an F# API.
All operations throw on failure for concise method chaining.
See the Tensor API reference for the complete method list.
Factory Methods
open Toro
let z = Tensor.zeros ([ 2; 3 ], F32, Cpu)
let o = Tensor.ones ([ 2; 3 ], F32, Cpu)
let r = Tensor.rand ([ 2; 3 ], F32, Cpu)
let n = Tensor.randn ([ 2; 3 ], F32, Cpu)
let f = Tensor.full ([ 2; 3 ], 3.14, F32, Cpu)
let a = Tensor.arange (1.0, 10.0, F32, Cpu)
Create tensors from F# collections with ofArray and ofList:
let t = Tensor.ofArray ([| 1f; 2f; 3f |], Cpu)
let t2 = Tensor.ofArray (array2D [| [| 1f; 2f |]; [| 3f; 4f |] |], Cpu)
let t3 = Tensor.ofList ([ [ 0L; 1L ]; [ 1L; 0L ] ], Cpu)
Combine tensors with cat, stack, and where:
let a = Tensor.ones ([ 2; 3 ], F32, Cpu)
let b = Tensor.zeros ([ 2; 3 ], F32, Cpu)
let catted = Tensor.cat ([ a; b ], 0) // shape: [4; 3]
let stacked = Tensor.stack ([ a; b ], 0) // shape: [2; 2; 3]
Arithmetic
Arithmetic operators return Tensor directly and throw on error:
let c = a + b
let d = a * 2.0
let e = -a
Shape Operations
Methods like reshape, view, flatten, squeeze, unsqueeze, transpose, permute, expand, and pad return Tensor:
let t = Tensor.randn ([ 2; 3; 4 ], F32, Cpu)
let reshaped = t.reshape [ 6; 4 ]
let transposed = t.transpose (0, 2)
let permuted = t.permute [ 2; 0; 1 ]
let flat = t.flatten (1, -1)
See the Tensor API reference for the full list of shape operations.
Indexing
Basic indexing uses the Item property and GetSlice. These throw on error and return Tensor:
let row = t[0]
let elem = t[1, 2]
let slice = t[0..2]
For complex patterns, use the at method with TIdx cases (I, S, Sf, St, A, T, E, N):
let result = t.at [ I 1; S(0, 3) ]
Methods like indexSelect, gather, narrow, chunk, maskedFill, and oneHot also return Tensor.
See the Tensor API reference for details.
Comparison
Element-wise comparison operators return a boolean tensor and throw on error:
let eq = a .=. b
let ne = a .<>. b
let gt = a .>. b
let lt = a .<. b
let ge = a .>=. b
let le = a .<=. b
Math and Reduction
Math methods (matmul, exp, log, sqrt, pow, clamp, ...) and reduction methods (sumAll, meanAll, sum, argmax, ...) return Tensor.
Activation functions are also available as tensor methods (relu, gelu, softmax, ...):
let x = Tensor.randn ([ 4; 3 ], F32, Cpu)
let y = x.matmul w
let activated = y.relu ()
let probs = activated.softmax 1
let mean = probs.meanAll ()
let idx = probs.argmax 1
See the Tensor API reference for the complete method list.
Conversion
Extract scalar values with item(), move tensors between devices with toDevice, and cast types with toDType:
let t = Tensor.randn ([ 2; 3 ], F32, Cpu)
let value = t[0, 0].item ()
let f16 = t.toDType F16
let gpu = t.toDevice (Cuda 0)
let detached = t.detach ()
Autograd
Enable gradient tracking with requiresGrad, compute gradients with backward, and read them with grad:
let w = Tensor.randn ([ 3; 1 ], F32, Cpu)
let w = w.requiresGrad ()
// ... forward pass and loss computation ...
loss.backward ()
let g = w.grad ()
Persistence
Save and load individual tensors:
let t = Tensor.randn ([ 3; 4 ], F32, Cpu)
t.save "weights.pt"
let loaded = Tensor.load "weights.pt"
For saving entire models, see Model Persistence and Hub.