Naming the network - xkqg/DeepSharp GitHub Wiki
Step 9 β Naming the network, or the trainer
In short
The last step of the declaration says which network these rows are prepared for β the layers, what moves them, what judges them, when the run stops β and running the pipeline then trains exactly that network. The model is declared with the pipeline rather than written beside it, so the whole course from a file to a validated model is one artefact. The one rule to remember: one learner a declaration, so comparing two learners is two declarations that differ in this line.
| You want to⦠| Write |
|---|---|
| name a network in the words Keras uses | .WithTensorflow(network => network.Dense(16).Relu().Dense(1).Adam(0.01).BinaryCrossEntropy().Run(seed: 20260929, epochs: 100)) |
| name it in the words PyTorch uses | .WithTorch(network => β¦) |
| stop once the validation loss has stopped falling | .StoppingAfter(patience: 10) |
| leave a layer without its bias | .Dense(16, bias: false) |
| give a layer a penalty, a bound or a hold | .L2(0.0001), .MaxNorm(3), .Held() |
| set the optimizer's options | .Sgd(0.05, momentum: 0.9).Nesterov().WeightDecay(0.0001) |
| set the run's options | .ClipValue(1), .StepEvery(4), .Average(0.99) |
| train what the pipeline names | pipeline.Train() |
| name a trainer from ML.NET instead, and train it | .WithML(trainer => trainer.FastTree()), then pipeline.TrainWithML() |
The rest of this page explains each one.
What the step looks like
using DeepSharp.Learners.Networks;
using DeepSharp.Pipelines;
var pipeline = Pdd.Create()
.ReadCsv("titanic.csv")
.Declare(schema => schema.Integer("survived", "sibsp", "parch").Category("pclass", "sex").Optional("age", ColumnKind.Number).Number("fare"))
.SplitStratified("survived", train: 0.70, validation: 0.15)
.Target("survived")
.FillMissing(fill => fill.Median("age"))
.EncodeCategories()
.Normalise("age", "fare", "sibsp", "parch")
.WithTensorflow(network => network // the words TensorFlow and Keras use
.Dense(16).Relu().Dense(1) // the widths come from the rows
.Adam(0.01)
.BinaryCrossEntropy()
.Run(seed: 20260929, epochs: 100)
.StoppingAfter(patience: 10))
.Build();
var trained = pipeline.Train(); // the course, from the file to the trained model
Two vocabularies, one model
.WithTensorflow(β¦) reads as Keras does β stack, compile, fit. .WithTorch(β¦) reads as PyTorch does.
They lower onto the same model, so nothing downstream knows which door you used: the same network, bit for bit, and the same file. Each writes down the number its own library would have left implicit, so the file says which run was asked for.
What a layer and a run can be told
Beyond the widths:
- a layer can be left without its bias, started another way, and given a penalty, a bound or a hold β
.Dense(16, bias: false).L2(0.0001),.MaxNorm(3),.Held(); - the optimizer and the run take the options PyTorch's have β
.Sgd(0.05, momentum: 0.9).Nesterov().WeightDecay(0.0001),.ClipValue(1),.StepEvery(4),.Average(0.99).
The options that are PyTorch's alone are said in .WithTorch(β¦) and refused by name in Keras's words; the rest are said
in both.
Said at their default they are not written, so a declaration that never mentions them is the file it was. Networks has each, with which to reach for when.
A network is not the only learner
.WithML(trainer => trainer.FastTree()) names a trainer from ML.NET instead, and pipeline.TrainWithML() trains it.
The run is then made for what a tree needs, which leaves the scalings out and writes down which. Everything else is the same steps over the same rows, so the report measures the two against each other. Comparing them is two declarations differing in this one line: ML.NET learner.
You do not have to use this step
A network written as code and fitted by hand is still fitted on the rows a pipeline prepared β
compiled.Fit(prepared, options) β and still measured by that pipeline's report.
Naming it here is what lets the file say what was trained behind which steps.
The engine is a name, not a dependency
The declaration may say which engine to run on; the application fills that name in. A model written against the seam cannot tell whether the light engine or libtorch is underneath, on the processor or on a graphics card.
Next: Saving and replaying Β· Every layer, loss, optimizer and schedule: Networks