Function Repository Resource:

# NetContract

Contract a subset of vertices in a NetGraph or NetChain into a single vertex

Contributed by: Maria Sargsyan
 ResourceFunction["NetContract"][net, {start, end}] contracts all the vertices from the start to the end in a NetGraph or NetChain into a single vertex. ResourceFunction["NetContract"][net, {start, end}→ name] contracts all the vertices from the start to the end in a NetGraph or NetChain into a single named vertex. ResourceFunction["NetContract"][net, string] contracts all the vertices which starts with a name string in a NetGraph or NetChain into a single vertex. ResourceFunction["NetContract"][net, string → name] contracts all the vertices which starts with a name string in a NetGraph or NetChain into a single named vertex. ResourceFunction["NetContract"][net, pattern] contracts all the vertices matched by a pattern in a NetGraph or NetChain into a single vertex. ResourceFunction["NetContract"][net, pattern→name] contracts all the vertices matched by a pattern in a NetGraph or NetChain into a single named vertex. ResourceFunction["NetContract"][net, rule1, rule2 , …] performs all the contractions specified by the rulei….

## Details

The patterns can be only string patterns such as RegularExpression[regex], s1~~ s2 ~~ or pure functions which operate on strings.
In ResourceFunction["NetContract"][net,{start,end}, ], the {start, end} can be any start and end specified in NetTake[net, {start, end}].

## Examples

### Basic Examples (9)

Define a net:

 In[1]:=
 Out[1]=

Obtain a new net by contracting everything from the starting to the ending node:

 In[2]:=
 Out[2]=

Give a new name to the contracted group of vertices:

 In[3]:=
 Out[3]=

Contract all the vertices after "1/a" and before "1/c":

 In[4]:=
 Out[4]=

Contract all the vertices which start with the name "1/":

 In[5]:=
 Out[5]=

Contract all the vertices which match a RegularExpression:

 In[6]:=
 Out[6]=

Contract all the vertices which match a StringExpression:

 In[7]:=
 Out[7]=

Contract all the vertices which match a pure function:

 In[8]:=
 Out[8]=

Contract several sets of vertices:

 In[9]:=
 Out[9]=

### Scope (3)

Simplify the net by combining ThreadingLayer and LogisticSigmoid into a single layer:

 In[10]:=
 Out[10]=

Create a new net:

 In[11]:=
 Out[11]=

Note that the new net is a bit faster:

 In[12]:=
 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=

## Version History

• 1.0.0 – 01 November 2022