NetSharedArray

NetSharedArray["name"]

represents an array in a net that is shared by more than one layer.

Details

  • All instances of NetSharedArray["name"] in a net are treated as the same underlying array by NetInitialize, NetTrain, etc.
  • The value of a shared array can be extracted with NetExtract[net,NetSharedArray["name"]].
  • The value of a shared array can be updated with NetReplacePart[net,NetSharedArray["name"]->value].
  • Any ordinary array of a layer such as LinearLayer, 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 NetExtract[net,NetSharedArray[All]].
  • NetInsertSharedArrays[layer] will return a new layer in which all its arrays have been replaced with shared arrays.

Examples

Basic Examples  (4)

Create a layer containing a shared array:

In[1]:=
Click for copyable input
Out[1]=

Create a layer, both of whose arrays are shared:

In[1]:=
Click for copyable input
Out[1]=

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

In[1]:=
Click for copyable input
Out[1]=

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

In[2]:=
Click for copyable input
Out[2]=

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

In[3]:=
Click for copyable input
Out[3]=
In[4]:=
Click for copyable input
Out[4]=

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

In[1]:=
Click for copyable input
Out[1]=

Use the LSTM layer on two separate inputs in a graph and combine the resulting states with a CatenateLayer:

In[2]:=
Click for copyable input
Out[2]=

See Also

NetInsertSharedArrays  NetExtract  NetReplacePart  NetInitialize  NetTrain  NetInformation

Tutorials

Introduced in 2018
(11.3)