Function Repository Resource:

NetContractRepeated

Source Notebook

Repeatedly contract subnets in a neural net until no pattern is found

Contributed by: Maria Sargsyan

ResourceFunction["NetContractRepeated"][net, pattern]

creates nested NetChain and NetGraphobjects within a net based on the layer names separated by delimiters matching the string expression pattern.

Details and Options

ResourceFunction["NetContractRepeated"] repeatedly uses the resource function NetContract to gather adjacent layers separated by a common string expression pattern into nested NetChain and NetGraph objects.
If pattern is set to the empty string (""), ResourceFunction["NetContractRepeated"] finds contraction groups separated by any character.
The pattern can be any string expression accepted by StringSplit.
The options "MinLength" and "MaxLength" can be used to respectively control the minimum and maximum sizes of the contracted groups.
If there are no groups of layer names separated by a common string expression pattern, NetContractRepeated returns the original net.

Examples

Basic Examples (3) 

Get a net:

In[1]:=
dummyNet = NetGraph[{"1/a" -> Ramp, "1/b" -> Tanh, "1/c" -> BatchNormalizationLayer[], "d" -> ThreadingLayer[Plus], "e" -> Ramp, "2/f" -> Tanh, "2/k" -> BatchNormalizationLayer[], "l" -> ThreadingLayer[Plus]}, {NetPort["Input"] -> "1/a" -> "1/b" -> "1/c", {"1/b", "1/c"} -> "d" -> "e" -> "2/f", "e" -> "2/k", {"2/k", "2/f"} -> "l"}]
Out[1]=

Apply NetContractRepeated on this net:

In[2]:=
ResourceFunction["NetContractRepeated"][dummyNet, "/"]
Out[2]=

Since there are no layer names separated by "_", NetContractRepeated returns the original net:

In[3]:=
ResourceFunction["NetContractRepeated"][dummyNet, "_"]
Out[3]=

Options (2) 

Get a net:

In[4]:=
dummyNet = NetGraph[{"1/a" -> Ramp, "1/b" -> Tanh, "1/c" -> BatchNormalizationLayer[], "d" -> ThreadingLayer[Plus], "e" -> Ramp, "2/f" -> Tanh, "2/k" -> BatchNormalizationLayer[], "l" -> ThreadingLayer[Plus]}, {NetPort["Input"] -> "1/a" -> "1/b" -> "1/c", {"1/b", "1/c"} -> "d" -> "e" -> "2/f", "e" -> "2/k", {"2/k", "2/f"} -> "l"}]
Out[4]=

Use the options "MinLength" and "MaxLength" to bound the length of the groups:

In[5]:=
ResourceFunction["NetContractRepeated"][dummyNet, "/", "MinLength" -> 2, "MaxLength" -> 2]
Out[5]=

Scope (2) 

Get a net:

In[6]:=
net = NetModel["ShuffleNet-V2 Trained on ImageNet Competition Data", "UninitializedEvaluationNet"]
Out[6]=

Note that the resulting net automatically retrieves all the original nested NetGraph and NetChain objects within the net:

In[7]:=
ResourceFunction["NetContractRepeated"][NetFlatten[net], "/"]
Out[7]=

Version History

  • 1.0.0 – 09 December 2022

Related Resources

License Information