Getting Started
This guide shows how to install Toro and train a model on the XOR problem.
Prerequisites
- .NET SDK 10.0 or later
Step 1: Create a Project
Run these commands:
dotnet new console -lang F# -o MyModel
cd MyModel
Step 2: Add Packages
Add Toro, Toro.NN, and a TorchSharp runtime:
dotnet add package Toro
dotnet add package Toro.NN
dotnet add package TorchSharp-cpu
Step 3: Define Training Data
Open Program.fs and add the training data for XOR:
open Toro
open Toro.NN
let x = Tensor.ofList ([ [ 0f; 0f ]; [ 0f; 1f ]; [ 1f; 0f ]; [ 1f; 1f ] ], Cpu)
let y = Tensor.ofList ([ [ 0f ]; [ 1f ]; [ 1f ]; [ 0f ] ], Cpu)
Tensor.ofList creates a tensor from an F# list. For arrays, use Tensor.ofArray.
Step 4: Build the Model
Create a two-layer network with the sequential { } computation expression:
let l1 = Linear.init 2 16 F32 Cpu
let l2 = Linear.init 16 1 F32 Cpu
let model = sequential {
l1; Relu; l2
}
Linear.init inDim outDim dtype devicecreates a fully connected layer.Reluis an activation function from theActivationtype.sequential { }combines layers into aSequentialmodel.
Step 5: Create an Optimizer
Create an AdamW optimizer from the model parameters:
let opt = AdamW.createWithLr 0.01 (Model.trainableVars model)
Model.trainableVars collects all tensors with RequiresGrad = true from the model record.
Step 6: Train
Run the training loop:
for epoch in 1..500 do
scoped {
opt.zeroGrad ()
let pred = model.forward x
let loss = Loss.mse pred y
loss.backward ()
opt.step ()
if epoch % 100 = 0 then
printfn "epoch %d loss=%.6f" epoch (loss.item ())
}
opt.zeroGrad ()clears accumulated gradients.model.forward xruns the forward pass.Loss.msecomputes mean squared error.loss.backward ()computes gradients via backpropagation.opt.step ()updates parameters using the gradients.scoped { }disposes intermediate tensors at the end of each iteration.
Step 7: Run
dotnet run
Expected output:
epoch 100 loss=0.001148
epoch 200 loss=0.000002
epoch 300 loss=0.000002
epoch 400 loss=0.000001
epoch 500 loss=0.000001
Next Steps
- Core Concepts -- Ownership management,
scoped { }CE, DType, Device - Tensor -- Tensor operations
- Neural Networks -- Layers and model composition
- Training -- Loss functions and optimizers