SmartEngine  1.6.0
NeuronLayer.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 #include "ActivationTypes.h"
6 #include "GraphNode.h"
7 #include "ObjectPtr.h"
8 #include "Resource.h"
9 
10 namespace SmartEngine
11 {
12 
13 #pragma pack(push, 4)
14 enum class LayerType : byte
18 {
22  LinearNoBias,
23 
27  Linear,
28 
32  LSTM,
33 
34  Count
35 };
36 SMARTENGINE_ENUM_STRING(LayerType, "LinearNoBias", "Linear", "LSTM");
37 
41 struct LayerInfo
42 {
46  LayerType type = LayerType::Linear;
47 
51  int neuronCount = 1;
52 
56  ActivationType activationType = ActivationType::Sigmoid;
57 
62 
63  bool operator==(const LayerInfo& rhs)
64  {
65  bool equal = type == rhs.type && neuronCount == rhs.neuronCount && activationType == rhs.activationType;
66  if (!equal)
67  {
68  return false;
69  }
70 
71  return true;
72  }
73 
74  bool operator!=(const LayerInfo& rhs) { return !(*this == rhs); }
75 };
76 
81 {
86  IGraphNode* input = nullptr;
87 
93  bool staticInstance = false;
94 
99 };
100 #pragma pack(pop)
101 
106 class SMARTENGINE_EXPORT INeuronLayer : public IGraphNode, public ISerializable
107 {
108 public:
109  SMARTENGINE_DECLARE_CLASS(INeuronLayer)
110 
111 
112  virtual LayerType GetType() const = 0;
115 
123  virtual ObjectPtr<INeuronLayer> InstanceLayer(const char* name, IGraphNode* input) = 0;
124 
128  virtual void SetRandomWeights() = 0;
129 
135  virtual void Reset() = 0;
136 
142  virtual void Step() = 0;
143 };
144 
149 
151 extern "C"
152 {
153  SMARTENGINE_EXPORT ObjPtr NeuronLayer_CreateInstance(const NeuronLayerCInfo& cinfo);
154  SMARTENGINE_EXPORT ObjPtr NeuronLayer_InstanceLayer(ObjPtr object, const char* name, ObjPtr input);
155  SMARTENGINE_EXPORT void NeuronLayer_SetRandomWeights(ObjPtr object);
156  SMARTENGINE_EXPORT void NeuronLayer_Reset(ObjPtr object);
157  SMARTENGINE_EXPORT void NeuronLayer_Step(ObjPtr object);
158 }
160 
161 } // namespace SmartEngine
SmartEngine::IGraphNode
A logical node in the AI graph. Some nodes, like NeuralNetwork, are composed of other nodes (neuron l...
Definition: GraphNode.h:34
SmartEngine::INeuronLayer::Step
virtual void Step()=0
Steps the internal state. Should be after execution of the layer and before new data is put into the ...
SmartEngine::NeuronLayerCInfo::layerInfo
LayerInfo layerInfo
Layer specification and details
Definition: NeuronLayer.h:98
SmartEngine::ISerializable
Base class for objects that can be loaded from and saved to an in memory buffer.
Definition: Resource.h:54
SmartEngine::INeuronLayer::InstanceLayer
virtual ObjectPtr< INeuronLayer > InstanceLayer(const char *name, IGraphNode *input)=0
Creates a new INeuronLayer with a different input, but weights shared with this layer.
SmartEngine::NeuronLayerCInfo::input
IGraphNode * input
Input into this layer. When deserializing the layer, the dimension of this input must be equal to the...
Definition: NeuronLayer.h:86
SmartEngine::LayerInfo::neuronCount
int neuronCount
Number of neurons in the layer. This is the output channel count in Conv2D layers.
Definition: NeuronLayer.h:51
SmartEngine::CreateNeuronLayer
SMARTENGINE_EXPORT ObjectPtr< INeuronLayer > CreateNeuronLayer(const NeuronLayerCInfo &cinfo)
Creates an instance of INeuronLayer
SmartEngine::GraphNodeCInfo
Data used to construct an IGraphNode instance
Definition: GraphNode.h:17
SmartEngine::LayerInfo::type
LayerType type
Layer type
Definition: NeuronLayer.h:46
SmartEngine::INeuronLayer::SetRandomWeights
virtual void SetRandomWeights()=0
Initialize the weights of the layer to random values.
SmartEngine::NeuronLayerCInfo
Data used to construct an INeuronLayer instance
Definition: NeuronLayer.h:81
SmartEngine::NeuronLayerCInfo::staticInstance
bool staticInstance
If true, this node will not be instanced, but rather referenced directly when instancing the graph it...
Definition: NeuronLayer.h:93
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine::LayerType::LinearNoBias
@ LinearNoBias
Linear with no bias (X * W)
SmartEngine
Definition: A2CTrainer.h:10
SmartEngine::LayerType
LayerType
The type of layer to create
Definition: NeuronLayer.h:18
SmartEngine::ActivationType
ActivationType
The activation function to apply to the output of the NeuronLayer
Definition: ActivationTypes.h:14
SmartEngine::LayerInfo::activationType
ActivationType activationType
Activation type
Definition: NeuronLayer.h:56
SmartEngine::LayerInfo::operator==
bool operator==(const LayerInfo &rhs)
Definition: NeuronLayer.h:63
SmartEngine::INeuronLayer
A neuron layer is the trainable unit in the neural network graph. These can chained together to allow...
Definition: NeuronLayer.h:107
SmartEngine::LayerInfo::operator!=
bool operator!=(const LayerInfo &rhs)
Definition: NeuronLayer.h:74
SmartEngine::INeuronLayer::Reset
virtual void Reset()=0
Resets the internal state. Should be called periodically on layers that need stepping (LSTM)....
SmartEngine::LayerInfo
Layer information
Definition: NeuronLayer.h:42
SmartEngine::LayerInfo::weightStandardDeviation
float weightStandardDeviation
The standard deviation applied when randomizing the weights
Definition: NeuronLayer.h:61