Function Repository Resource:

TrainingStateLayer

Source Notebook

Create a NetGraph that returns the current NetEvalutionMode as 0 or 1

Contributed by: Pierre-André Brousseau

ResourceFunction["TrainingStateLayer"][]

provides a NetGraph that returns the current NetEvalutionMode.

Details and Options

During training, ResourceFunction["TrainingStateLayer"] returns 1. Otherwise, it returns 0.
Any network can have online data augmentation by simply multiplying by this layer.
If NetEvaluationMode"Train", this graph returns 1. If NetEvaluationMode"Test", this graph returns 0.

Examples

Basic Examples

Create the training state layer:

In[1]:=
ts = ResourceFunction["TrainingStateLayer"][]
Out[1]=

Simple output:

In[2]:=
ts[]
Out[2]=
In[3]:=
ts[NetEvaluationMode -> "Train"]
Out[3]=
In[4]:=
ts[NetEvaluationMode -> "Test"]
Out[4]=

Use it as part of a network for data augmentation. This net adds noise as data augmentation during training:

In[5]:=
augmentationNet = NetGraph@FunctionLayer[Block[{ra, ts}, (
      ra = RandomArrayLayer[UniformDistribution[{-1, 1}], "Output" -> {4}][];
      ts = ResourceFunction["TrainingStateLayer"][][];
      #Input + (ra*ts)
      )] &]
Out[5]=

Apply an input to the net, no noise is added:

In[6]:=
augmentationNet[<|"Input" -> {1, 2, 3, 4}|>]
Out[6]=

When in training mode, noise is added each time the net is used:

In[7]:=
Table[
 augmentationNet[<|"Input" -> {1, 2, 3, 4}|>, NetEvaluationMode -> "Train"]
 , 4]
Out[7]=

Test mode does not add noise:

In[8]:=
Table[
 augmentationNet[<|"Input" -> {1, 2, 3, 4}|>, NetEvaluationMode -> "Test"]
 , 4]
Out[8]=

Publisher

Pierre-Andre Brousseau

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 07 June 2024

Related Resources

Author Notes

-

License Information