({{, (RefLink[NetSharedArray, paclet:ref/NetSharedArray])["name"] 
represents an array in a net that is shared by more than one layer.}})

OpenersNotesSection(Spacer1)Details

All instances of (RefLink[NetSharedArray, paclet:ref/NetSharedArray])["name"] in a net are treated as the same underlying array by (RefLink[NetInitialize, paclet:ref/NetInitialize]), (RefLink[NetTrain, paclet:ref/NetTrain]), etc.

The value of a shared array can be extracted with (RefLink[NetExtract, paclet:ref/NetExtract])[net,NetSharedArray["name"]].

The value of a shared array can be updated with (RefLink[NetReplacePart, paclet:ref/NetReplacePart])[net,NetSharedArray["name"]->value].

Any ordinary array of a layer such as (RefLink[LinearLayer, paclet:ref/LinearLayer]), (RefLink[ConvolutionLayer, paclet:ref/ConvolutionLayer]), etc. can be specified to be a shared array.

When one or more nets are combined into a larger net, the shared arrays of the larger net will be the union of the shared arrays of the original nets.

A list of the values of all shared arrays in a net can be retrieved with (RefLink[NetExtract, paclet:ref/NetExtract])[net,NetSharedArray[(RefLink[All, paclet:ref/All])]].

(RefLink[NetInsertSharedArrays, paclet:ref/NetInsertSharedArrays])[layer] will return a new layer in which all its arrays have been replaced with shared arrays.

OpenersPrimaryExamplesSection(Spacer1)Examples  (4)

OpenersExampleSection0(Spacer1)Basic Examples  (4)

Create a layer containing a shared array:

LinearLayer[2,"Weights"->NetSharedArray["W"]]

Create a layer, both of whose arrays are shared:

NetInsertSharedArrays[LinearLayer[2,"Input"->2]]

Create a chain that contains a shared array, used in two layers:

net=NetChain[{
LinearLayer[2,"Weights"->NetSharedArray["W"]],
ElementwiseLayer[Ramp],
LinearLayer[2,"Weights"->NetSharedArray["W"]]
}]

Initialize all arrays and extract the value of the shared array:

net=NetInitialize[net];
NetExtract[net,NetSharedArray[All]]

Extracting the corresponding weights from the two linear layers returns the same value:

NetExtract[net,{1,"Weights"}]

NetExtract[net,{3,"Weights"}]

Create an LSTM layer whose arrays will be shared every time it is used in a larger net:

lstm=NetInsertSharedArrays[LongShortTermMemoryLayer[5,"Input"->{"Varying",3}]]

Use the LSTM layer on two separate inputs in a graph and combine the resulting states with a (RefLink[CatenateLayer, paclet:ref/CatenateLayer]):

NetGraph[<|"lstm1"->lstm,"lstm2"->lstm,"last1"->SequenceLastLayer[],"last2"->SequenceLastLayer[],"cat"->CatenateLayer[]|>,{NetPort["A"]->"lstm1"->"last1"->"cat",
NetPort["B"]->"lstm2"->"last2"->"cat"}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[NetInsertSharedArrays, paclet:ref/NetInsertSharedArrays])  ▪  (RefLink[NetExtract, paclet:ref/NetExtract])  ▪  (RefLink[NetReplacePart, paclet:ref/NetReplacePart])  ▪  (RefLink[NetInitialize, paclet:ref/NetInitialize])  ▪  (RefLink[NetTrain, paclet:ref/NetTrain])  ▪  (RefLink[NetInformation, paclet:ref/NetInformation])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Neural Networks in the Wolfram Language, paclet:tutorial/NeuralNetworksOverview])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Neural Networks, paclet:guide/NeuralNetworks])}})}})