LayerCloning

Layers · AiDotNet.NeuralNetworks.Layers

Cloning for layers, built on the construction state layers already record for serialization.

For Beginners

layer.Clone() gives you a separate copy of a layer, including what it has learned. You do not write anything to make this work on a layer of your own: mark the constructor arguments the layer needs with [LayerState] — which it already needs for saving and loading — and cloning follows.

How It Works

This deliberately reuses the generated construction manifest and GeneratedLayerFactories.TryCreate rather than introducing a second reconstruction mechanism. Durable persistence and the in-memory object channel are emitted from the same [LayerState] model; the live channel avoids formatting constructor-owned child-layer weights during a clone. Both call the layer’s real constructor with the values it was originally given, and the build already fails for a layer whose required state cannot be sourced. Sharing the generated model means saving, loading, and cloning cannot silently drift into independent constructor recipes.

The learned parameters travel separately, through GetParameters and UpdateParameters. That is the contract training exercises on every step, so a clone cannot disagree with training about what the parameters are.

Methods

Method Summary
ILayer<T> Clone(LayerBase<T> source, CloneOptions options) Creates an independent copy of a layer.