Training a network¶
Note
This chapter needs the learning feature (see Installation).
The neural searches repair with the predictions of a network. A network is trained on a set of problems, one problem per example, and the trained network lives in a checkpoint directory, so a network trained once can be used by any later program.
Each network has its own restrictions
This chapter shows the workflow. What a given network accepts (how its problems may differ, how
many variables or values it handles, what it needs from a problem) depends on the network, and is
documented with it in the Rust documentation of the aicad crate, for the ConsFormer network in
the neural::consformer module. The examples below use graph colourings of the same size because
that is what the ConsFormer network accepts. The functions check what they can and return an error
that names the problem.
Data¶
The training problems are a list of Arc<Problem>. Training needs a validation set to pick the best
network; give one, or training holds out 20% of the problems itself. (The Python guide splits with
Dataset.split; in Rust you split your own Vec.)
There is no test set in training on purpose. The problems you evaluate on are yours to keep apart:
generate them separately and never pass them to training, so that the network has certainly not seen
them. The coloring function is the one of the previous chapter.
// The problems have their own constraints but the same variables. The problems to evaluate on
// are generated apart and never given to training, so that the network cannot have seen them.
let mut rng = StdRng::seed_from_u64(0);
let mut training: Vec<_> = (0..108).map(|_| coloring(&mut rng, 20, 3, 3.5)).collect();
let test: Vec<_> = (0..12).map(|_| coloring(&mut rng, 20, 3, 3.5)).collect();
// The validation problems pick the best network during training. Without them, training
// holds out 20% of the problems itself.
let validation: Vec<Arc<_>> = training.split_off(training.len() - 12);
println!(
"{} training, {} validation, {} test",
training.len(),
validation.len(),
test.len()
);
Training¶
A network is described by its configuration (ConsFormerConfig), and the optimisation by a
TrainingConfig. train_consformer_mdd scores the network with the exact probability that its
predictions satisfy each constraint, computed on the constraint’s diagram, labelled beforehand by
bp_rounds rounds of belief propagation (0 scores the predictions as they are, the messages being
constants of the gradient); the predictions are the softmax of the network’s logits after Gumbel noise
multiplied by gumbel_scale (1 is the Gumbel-softmax relaxation of ConsFormer, 0 adds no noise).
train_consformer uses a hand-written penalty per constraint type. Both run on the best device found (CUDA if there is a GPU,
the CPU otherwise), and have an _on variant that takes the backend. They write the best network and
its configuration to the checkpoint directory.
// The architecture and the optimisation are settings of two structs. The network here is tiny
// and trained for a few epochs, so it will not beat belief propagation alone on problems this
// easy: the example shows the workflow.
let network = ConsFormerConfig::new(
3, // domain_size: the number of values the network can predict
16, // embedding_size
16, // hidden_size
2, // num_heads
32, // expand_size
0, // positional_encoding_dimensions
0.5, // mask_fraction
0.1, // tau
);
let training_config = TrainingConfig::new()
.with_num_epochs(4)
.with_validation_interval(2)
.with_batch_size(32);
// `consformer-mdd` scores the network with the exact probability that its predictions satisfy
// each constraint, computed on the constraint's diagram, labelled after a round of belief
// propagation (0 scores the probabilities as they are). The best network and its
// configuration are written to the checkpoint directory.
train_consformer_mdd(
training,
Some(validation),
network,
training_config,
MddCompilationConfig::default(),
1, // rounds of belief propagation that label the diagrams before the loss
1.0, // scale of the Gumbel noise added to the logits (0: no noise)
&checkpoint,
)
.unwrap();
A network from another crate is trained with train_model: see Contributing.
Searching with the network¶
The checkpoint is given to solve, with a neural architecture in the configuration. The settings
are the ones of the belief propagation search, plus what the network adds.
// The checkpoint is a parameter of the neural searches, which otherwise take the same options
// as the belief-propagation one. `NeuralPrior` repairs with the network directly;
// `NeuralPriorBp` refines its predictions with belief propagation before choosing.
let limits = SolveConfig::default()
.with_runtime_limit(10)
.with_iteration_limit(100)
.with_seed(0);
let searches = [
(
"neural prior",
SearchConfig::NeuralPrior(NeuralPriorSearchConfig {
destroy_fraction: 0.2,
..NeuralPriorSearchConfig::default()
}),
),
(
"neural prior + BP",
SearchConfig::NeuralPriorBp(NeuralPriorBpSearchConfig {
destroy_fraction: 0.2,
..NeuralPriorBpSearchConfig::default()
}),
),
];
for (name, search) in searches {
let config = limits.clone().with_search(search);
let solutions = solve(&test, &config, Some(&checkpoint)).unwrap();
let solved = solutions
.iter()
.filter(|s| s.status() == Status::Satisfiable)
.count();
println!(
"{name:>20}: solved {solved} of {} test problems",
test.len()
);
}
Using a checkpoint later¶
A checkpoint is a directory: the configuration is config.json and the weights are next to it. A
network is tied to the problems it was trained on, in a way that depends on the network (see the
note at the top of the chapter): problems that fit it are solved with it directly. with_batch_size(1)
solves them one after the other, each with its own limits, as a deployment would; by default all the
problems are stepped together on the device.
// A checkpoint is a directory: the configuration is `config.json`, the weights are next to it.
// New problems that fit the network are solved with it directly. `with_batch_size(1)` solves
// them one after the other, each with its own limits, as a deployment would.
let network = ConsFormerConfig::load(checkpoint.join("config.json")).unwrap();
println!("a network over {} values", network.domain_size);
let mut rng = StdRng::seed_from_u64(123);
let new_problems: Vec<_> = (0..3).map(|_| coloring(&mut rng, 20, 3, 3.5)).collect();
let config = limits
.with_batch_size(1)
.with_search(SearchConfig::NeuralPriorBp(NeuralPriorBpSearchConfig {
destroy_fraction: 0.2,
..NeuralPriorBpSearchConfig::default()
}));
for (problem, solution) in new_problems
.iter()
.zip(solve(&new_problems, &config, Some(&checkpoint)).unwrap())
{
println!(
"{:?} after {:?} iterations",
solution.status(),
solution.iterations()
);
if let Some(assignment) = solution.assignment() {
assert!(problem.is_solution(assignment));
}
}
Compilation and local search are reproducible given their seed. Training is seeded too, but bit-for-bit reproducibility of a training run is not guaranteed.