BinderScriptScript

Quickstart

Here we cover some key tasks involved in a typical machine learning pipeline and how these can be implemented with Furnace. Note that a significant part of Furnace's design has been influenced by PyTorch and you would feel mostly at home if you have familiarity with PyTorch.

Datasets and Data Loaders

Furnace provides the Dataset type that represents a data source and the DataLoader type that handles the loading of data from datasets and iterating over minibatches of data.

See the Furnace.Data namespace for the full API reference.

Datasets

Furnace has ready-to-use types that cover main datasets typically used in machine learning, such as MNIST, CIFAR10, CIFAR100, and also more generic dataset types such as TensorDataset or ImageDataset.

The following loads the MNIST dataset and shows one image entry and the corresponding label.

open Furnace
open Furnace.Data

// First ten images in MNIST training set
let dataset = MNIST("../data", train=true, transform=id, n=10)

// Inspect a single image and label
let data, label = dataset[7]

// Save image to file
data.saveImage("test.png")
No value returned by any evaluator
// Inspect data as ASCII and show label
printfn "Data: %A\nLabel: %A" (data.toImageString()) label

Data Loaders

A data loader handles tasks such as constructing minibatches from an underlying dataset on-the-fly, shuffling the data, and moving the data tensors between devices. In the example below we show a single batch of six MNIST images and their corresponding classification labels.

let loader = DataLoader(dataset, shuffle=true, batchSize=6)
let batch, labels = loader.batch()

printfn "%A\nLabels: %A" (batch.toImageString()) labels

In practice a data loader is typically used to iterate over all minibatches in a given dataset in order to feed each minibatch through a machine learning model. One full iteration over the dataset would be called an "epoch". Typically you would perform multiple such epochs of iterations during the training of a model.

for epoch = 1 to 10 do
    for i, data, labels in loader.epoch() do
        printfn "Epoch %A, minibatch %A" epoch (i+1)
        // Process the minibatch
        // ...

Models

Many machine learning models are differentiable functions whose parameters can be tuned via gradient-based optimization, finding an optimum for an objective function that quantifies the fit of the model to a given set of data. These models are typically built as compositions non-linear functions and ready-to-use building blocks such as linear, recurrent, and convolutional layers.

Furnace provides the most commonly used model building blocks including convolutions, transposed convolutions, batch normalization, dropout, recurrent and other architectures.

See the Furnace.Model namespace for the full API reference.

Constructing models, PyTorch style

If you have experience with PyTorch, you would find the following way of model definition familiar. Let's look at an example of a generative adversarial network (GAN) architecture.

open Furnace.Model
open Furnace.Compose

// PyTorch style

// Define a model class inheriting the base
type Generator(nz: int) =
    inherit Model()
    let fc1 = Linear(nz, 256)
    let fc2 = Linear(256, 512)
    let fc3 = Linear(512, 1024)
    let fc4 = Linear(1024, 28*28)
    do base.addModel(fc1, fc2, fc3, fc4)
    override self.forward(x) =
        x
        |> FurnaceImage.view([-1;nz])
        |> fc1.forward
        |> FurnaceImage.leakyRelu(0.2)
        |> fc2.forward
        |> FurnaceImage.leakyRelu(0.2)
        |> fc3.forward
        |> FurnaceImage.leakyRelu(0.2)
        |> fc4.forward
        |> FurnaceImage.tanh

// Define a model class inheriting the base
type Discriminator(nz:int) =
    inherit Model()
    let fc1 = Linear(28*28, 1024)
    let fc2 = Linear(1024, 512)
    let fc3 = Linear(512, 256)
    let fc4 = Linear(256, 1)
    do base.addModel(fc1, fc2, fc3, fc4)
    override self.forward(x) =
        x
        |> FurnaceImage.view([-1;28*28])
        |> fc1.forward
        |> FurnaceImage.leakyRelu(0.2)
        |> FurnaceImage.dropout(0.3)
        |> fc2.forward
        |> FurnaceImage.leakyRelu(0.2)
        |> FurnaceImage.dropout(0.3)
        |> fc3.forward
        |> FurnaceImage.leakyRelu(0.2)
        |> FurnaceImage.dropout(0.3)
        |> fc4.forward
        |> FurnaceImage.sigmoid

// Instantiate the defined classes
let nz = 128
let gen = Generator(nz)
let dis = Discriminator(nz)

print gen
print dis

Constructing models, Furnace style

A key advantage of Furnace lies in the functional programming paradigm enabled by the F# language, where functions are first-class citizens, many algorithms can be constructed by applying and composing functions, and differentiation operations can be expressed as composable higher-order functions. This allows very succinct (and beautiful) machine learning code to be expressed as a powerful combination of lambda calculus and differential calculus.

For example, the following constructs the same GAN architecture (that we constructed in PyTorch style in the previous section) using Furnace's --> composition operator, which allows you to seamlessly compose Model instances and differentiable Tensor->Tensor functions.

// Furnace style

// Model as a composition of models and Tensor->Tensor functions
let generator =
    FurnaceImage.view([-1;nz])
    --> Linear(nz, 256)
    --> FurnaceImage.leakyRelu(0.2)
    --> Linear(256, 512)
    --> FurnaceImage.leakyRelu(0.2)
    --> Linear(512, 1024)
    --> FurnaceImage.leakyRelu(0.2)
    --> Linear(1024, 28*28)
    --> FurnaceImage.tanh

// Model as a composition of models and Tensor->Tensor functions
let discriminator =
    FurnaceImage.view([-1; 28*28])
    --> Linear(28*28, 1024)
    --> FurnaceImage.leakyRelu(0.2)
    --> FurnaceImage.dropout(0.3)
    --> Linear(1024, 512)
    --> FurnaceImage.leakyRelu(0.2)
    --> FurnaceImage.dropout(0.3)
    --> Linear(512, 256)
    --> FurnaceImage.leakyRelu(0.2)
    --> FurnaceImage.dropout(0.3)
    --> Linear(256, 1)
    --> FurnaceImage.sigmoid

print generator
print discriminator
namespace Furnace
type FurnaceImage = static member abs: input: Tensor -> Tensor static member acos: input: Tensor -> Tensor static member add: a: Tensor * b: Tensor -> Tensor static member arange: endVal: float * ?startVal: float * ?step: float * ?device: Device * ?dtype: Dtype * ?backend: Backend -> Tensor + 1 overload static member arangeLike: input: Tensor * endVal: float * ?startVal: float * ?step: float * ?device: Device * ?dtype: Dtype * ?backend: Backend -> Tensor + 1 overload static member argmax: input: Tensor -> int array + 1 overload static member argmin: input: Tensor -> int array + 1 overload static member asin: input: Tensor -> Tensor static member atan: input: Tensor -> Tensor static member backends: unit -> Backend list ...
<summary> Tensor operations </summary>
static member FurnaceImage.config: unit -> Device * Dtype * Backend * Printer
static member FurnaceImage.config: configuration: (Device * Dtype * Backend * Printer) -> unit
static member FurnaceImage.config: ?device: Device * ?dtype: Dtype * ?backend: Backend * ?printer: Printer -> unit
Multiple items
module Backend from Furnace
<summary> Contains functions and settings related to backend specifications. </summary>

--------------------
type Backend = | Reference | Torch | Other of name: string * code: int override ToString: unit -> string member Name: string
<summary> Represents a backend for Furnace tensors </summary>
union case Backend.Reference: Backend
<summary> The reference backend </summary>
static member FurnaceImage.seed: ?seed: int -> unit
namespace Furnace.Util
namespace Furnace.Data
val dataset: MNIST
Multiple items
type MNIST = inherit Dataset new: path: string * ?urls: string seq * ?train: bool * ?transform: (Tensor -> Tensor) * ?targetTransform: (Tensor -> Tensor) * ?n: int -> MNIST override item: i: int -> Tensor * Tensor member classNames: string array member classes: int override length: int

--------------------
new: path: string * ?urls: string seq * ?train: bool * ?transform: (Tensor -> Tensor) * ?targetTransform: (Tensor -> Tensor) * ?n: int -> MNIST
val id: x: 'T -> 'T
argument n: int option
val data: Tensor
val label: Tensor
val pngToHtml: fileName: string -> widthPixels: int -> string
<summary> Given a PNG image file name, returns an HTML image element with the image content included as a Base64 encoded string </summary>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
val loader: DataLoader
Multiple items
type DataLoader = new: dataset: Dataset * batchSize: int * ?shuffle: bool * ?dropLast: bool * ?device: Device * ?dtype: Dtype * ?backend: Backend * ?targetDevice: Device * ?targetDtype: Dtype * ?targetBackend: Backend -> DataLoader member batch: ?batchSize: int -> Tensor * Tensor member epoch: ?numBatches: int -> (int * Tensor * Tensor) seq member length: int

--------------------
new: dataset: Dataset * batchSize: int * ?shuffle: bool * ?dropLast: bool * ?device: Device * ?dtype: Dtype * ?backend: Backend * ?targetDevice: Device * ?targetDtype: Dtype * ?targetBackend: Backend -> DataLoader
val batch: Tensor
val labels: Tensor
member DataLoader.batch: ?batchSize: int -> Tensor * Tensor
val epoch: int
val i: int
member DataLoader.epoch: ?numBatches: int -> (int * Tensor * Tensor) seq
namespace Furnace.Model
module Compose from Furnace
Multiple items
type Generator = inherit Model new: nz: int -> Generator override forward: x: Tensor -> Tensor

--------------------
new: nz: int -> Generator
val nz: int
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Multiple items
namespace Furnace.Model

--------------------
type Model = Model<Tensor,Tensor>

--------------------
new: ?f: ('In -> 'Out) * ?parameters: Parameter seq * ?buffers: Parameter seq * ?models: ModelBase seq -> Model<'In,'Out>
val fc1: Linear
Multiple items
type Linear = inherit Model new: inFeatures: int * outFeatures: int * ?bias: bool -> Linear override ToString: unit -> string override forward: value: Tensor -> Tensor member bias: Tensor with get, set member weight: Tensor with get, set
<summary>A model that applies a linear transformation to the incoming data: \(y = xA^T + b\)</summary>

--------------------
new: inFeatures: int * outFeatures: int * ?bias: bool -> Linear
val fc2: Linear
val fc3: Linear
val fc4: Linear
val self: Generator
val x: Tensor
static member FurnaceImage.view: shape: int seq -> (Tensor -> Tensor)
static member FurnaceImage.view: shape: int -> (Tensor -> Tensor)
static member FurnaceImage.view: input: Tensor * shape: int -> Tensor
static member FurnaceImage.view: input: Tensor * shape: int seq -> Tensor
override Linear.forward: value: Tensor -> Tensor
static member FurnaceImage.leakyRelu: ?negativeSlope: float -> (Tensor -> Tensor)
static member FurnaceImage.leakyRelu: input: Tensor * ?negativeSlope: float -> Tensor
static member FurnaceImage.tanh: input: Tensor -> Tensor
Multiple items
type Discriminator = inherit Model new: nz: int -> Discriminator override forward: x: Tensor -> Tensor

--------------------
new: nz: int -> Discriminator
val self: Discriminator
static member FurnaceImage.dropout: ?p: double -> (Tensor -> Tensor)
static member FurnaceImage.dropout: input: Tensor * ?p: double -> Tensor
static member FurnaceImage.sigmoid: input: Tensor -> Tensor
val gen: Generator
val dis: Discriminator
val print: x: 'a -> unit
<summary> Print the given value to the console using the '%A' printf format specifier </summary>
val generator: Model<Tensor,Tensor>
val discriminator: Model<Tensor,Tensor>

© Copyright 2025, Furnace Contributors.