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.
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.
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")
|
// Inspect data as ASCII and show label
printfn "Data: %A\nLabel: %A" (data.toImageString()) label
|
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
// ...
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.
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
|
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
|
© Copyright 2025, Furnace Contributors.