Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create a NetGraph that returns the current NetEvalutionMode as 0 or 1
| ResourceFunction["TrainingStateLayer"][] provides a NetGraph that returns the current NetEvalutionMode. | 
Create the training state layer:
| In[1]:= | ![ts = ResourceFunction["TrainingStateLayer"][]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/3ae19393dddc1ecd.png) | 
| Out[1]= |  | 
Simple output:
| In[2]:= | ![ts[]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/02d00c56f9de75d3.png) | 
| Out[2]= |  | 
| In[3]:= | ![ts[NetEvaluationMode -> "Train"]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/7c65f3858fc83699.png) | 
| Out[3]= |  | 
| In[4]:= | ![ts[NetEvaluationMode -> "Test"]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/21b304bcf6165d30.png) | 
| 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)
      )] &]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/562e71333ad082f6.png) | 
| Out[5]= |  | 
Apply an input to the net, no noise is added:
| In[6]:= | ![augmentationNet[<|"Input" -> {1, 2, 3, 4}|>]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/2fd36295d002fc5e.png) | 
| 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]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/47fe17196666bc4c.png) | 
| Out[7]= |  | 
Test mode does not add noise:
| In[8]:= | ![Table[
 augmentationNet[<|"Input" -> {1, 2, 3, 4}|>, NetEvaluationMode -> "Test"]
 , 4]](https://www.wolframcloud.com/obj/resourcesystem/images/035/03501cc9-63b5-4e8c-b4a3-2a8578370c4e/1f8e231ddcfc8224.png) | 
| Out[8]= |  | 
Wolfram Language 13.0 (December 2021) or above
This work is licensed under a Creative Commons Attribution 4.0 International License