SmartEngine  1.6.0
Graph.h
1 // Copyright (C) Entropy Software LLC - All Rights Reserved
2 
3 #pragma once
4 
5 #include "Context.h"
6 #include "GraphNode.h"
7 #include "Object.h"
8 #include "Resource.h"
9 
10 namespace SmartEngine
11 {
12 
13 #pragma pack(push, 4)
14 struct GraphCInfo : public ResourceCInfo
18 {
22  IContext* context = nullptr;
23 };
24 
35 {
39  const char* componentInputName = nullptr;
40 
44  IGraphNode* inputNode = nullptr;
45 };
46 #pragma pack(pop)
47 
60 class SMARTENGINE_EXPORT IGraph : public IResource
61 {
62 public:
63  SMARTENGINE_DECLARE_CLASS(IGraph)
64 
65 
66  virtual const char* GetName() const = 0;
69 
74  virtual ObjectPtr<IContext> GetContext() const = 0;
75 
80  virtual ObjectPtr<IGraph> DeepCopy() const = 0;
81 
86  virtual ObjectPtr<IGraph> Instance() = 0;
87 
105  virtual void BindComponentInputs(const ComponentInputBinding* bindings, int bindingCount) = 0;
106 
112  virtual void CopyWeightsFrom(IGraph* graph, float percent = 1.0f) = 0;
113 
117  virtual void SetRandomWeights() = 0;
118 
124  virtual void AddNode(IGraphNode* node) = 0;
125 
133  template <typename T> ObjectPtr<T> GetNode(const char* name) const;
134 
141  virtual ObjectPtr<IGraphNode> GetOutputNode(int index) const = 0;
142 
147  virtual float GetWeightStandardDeviation() const = 0;
148 
154  virtual int GetSequenceLength() const = 0;
155 
161  virtual void SetSequenceLength(int stepCount) = 0;
162 
166  virtual void ResetNeuronLayers() = 0;
167 
174  virtual void StepNeuronLayers(bool autoReset = true) = 0;
175 
176 protected:
177  virtual ObjectPtr<IGraphNode> GetNodeInternal(const char* name) const = 0;
178 };
179 
183 SMARTENGINE_EXPORT ObjectPtr<IGraph> CreateEmptyGraph(const GraphCInfo& cinfo);
184 
188 SMARTENGINE_EXPORT ObjectPtr<IGraph> CreateGraphFromDefinition(const GraphCInfo& cinfo, const char* graphDefinition);
189 
193 SMARTENGINE_EXPORT ObjectPtr<IGraph> CreateGraphFromResource(const GraphCInfo& cinfo,
194  const char* resourceName = nullptr);
195 
197 extern "C"
198 {
199  SMARTENGINE_EXPORT ObjPtr Graph_CreateEmpty(const GraphCInfo& cinfo);
200  SMARTENGINE_EXPORT ObjPtr Graph_CreateFromDefinition(const GraphCInfo& cinfo, const char* graphDefinition);
201  SMARTENGINE_EXPORT ObjPtr Graph_CreateFromResource(const GraphCInfo& cinfo, const char* resourceName);
202  SMARTENGINE_EXPORT const char* Graph_GetName(ObjPtr object);
203  SMARTENGINE_EXPORT ObjPtr Graph_GetContext(ObjPtr object);
204  SMARTENGINE_EXPORT ObjPtr Graph_DeepCopy(ObjPtr object);
205  SMARTENGINE_EXPORT ObjPtr Graph_Instance(ObjPtr object);
206  SMARTENGINE_EXPORT void Graph_BindComponentInputs(ObjPtr object, const ComponentInputBinding* bindings, int bindingCount);
207  SMARTENGINE_EXPORT void Graph_CopyWeightsFrom(ObjPtr object, ObjPtr graph, float percent);
208  SMARTENGINE_EXPORT void Graph_SetRandomWeights(ObjPtr object);
209  SMARTENGINE_EXPORT ObjPtr Graph_GetNode(ObjPtr object, const char* name, const char* type);
210  SMARTENGINE_EXPORT ObjPtr Graph_GetOutputNode(ObjPtr object, int index);
211  SMARTENGINE_EXPORT float Graph_GetWeightStandardDeviation(ObjPtr object);
212  SMARTENGINE_EXPORT void Graph_AddNode(ObjPtr object, ObjPtr node);
213  SMARTENGINE_EXPORT int Graph_GetSequenceLength(ObjPtr object);
214  SMARTENGINE_EXPORT void Graph_SetSequenceLength(ObjPtr object, int stepCount);
215  SMARTENGINE_EXPORT void Graph_ResetNeuronLayers(ObjPtr object);
216  SMARTENGINE_EXPORT void Graph_StepNeuronLayers(ObjPtr object, int autoReset);
217 }
219 
220 template <typename T> ObjectPtr<T> IGraph::GetNode(const char* name) const
221 {
222  return DynamicCast<T>(GetNodeInternal(name));
223 }
224 
225 } // 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::IGraph::GetSequenceLength
virtual int GetSequenceLength() const =0
Returns the desired sequence length for this graph. This is after how many steps the graph should be ...
SmartEngine::IGraph::SetSequenceLength
virtual void SetSequenceLength(int stepCount)=0
Sets the desired sequence length for this graph. This is after how many steps the graph should be res...
SmartEngine::IGraph::BindComponentInputs
virtual void BindComponentInputs(const ComponentInputBinding *bindings, int bindingCount)=0
Binds the component inputs in the graph to the specified set of nodes. Any component inputs not inclu...
SmartEngine::CreateGraphFromResource
SMARTENGINE_EXPORT ObjectPtr< IGraph > CreateGraphFromResource(const GraphCInfo &cinfo, const char *resourceName=nullptr)
Creates and loads a new IGraph from a resource.
SmartEngine::ComponentInputBinding::componentInputName
const char * componentInputName
The name component input node in the graph
Definition: Graph.h:39
SmartEngine::IGraph::SetRandomWeights
virtual void SetRandomWeights()=0
Initialize the weights trainable layers to random values.
SmartEngine::IGraph::ResetNeuronLayers
virtual void ResetNeuronLayers()=0
Calls Reset() on all trainable layers in the graph. This is only necessary for LSTM neuron layers.
SmartEngine::GraphCInfo
Data used to construct an IGraph instance
Definition: Graph.h:18
SmartEngine::IGraph::StepNeuronLayers
virtual void StepNeuronLayers(bool autoReset=true)=0
Calls Step() on all trainable layers in the graph. This is only necessary for LSTM neuron layers.
SmartEngine::IGraph::DeepCopy
virtual ObjectPtr< IGraph > DeepCopy() const =0
Copies the internal structure of the graph into a new graph
SmartEngine::GraphCInfo::context
IContext * context
The context this graph belongs to.
Definition: Graph.h:22
SmartEngine::IGraph::GetNode
ObjectPtr< T > GetNode(const char *name) const
Returns the specified node as the specified type. Null is returned if the node is not found or is of ...
Definition: Graph.h:220
SmartEngine::IGraph::GetWeightStandardDeviation
virtual float GetWeightStandardDeviation() const =0
Returns the average standard deviation of all trainable weights in the graph.
SmartEngine::IGraph::AddNode
virtual void AddNode(IGraphNode *node)=0
Adds a node to the graph. This will be considered an output node until a node connecting to this one ...
SmartEngine::ObjectPtr
Smart pointer to an IObject. Automatic ref counting.
Definition: ObjectPtr.h:16
SmartEngine
Definition: A2CTrainer.h:10
SmartEngine::CreateGraphFromDefinition
SMARTENGINE_EXPORT ObjectPtr< IGraph > CreateGraphFromDefinition(const GraphCInfo &cinfo, const char *graphDefinition)
Creates a new IGraph from a graph definition json string.
SmartEngine::IGraph::GetOutputNode
virtual ObjectPtr< IGraphNode > GetOutputNode(int index) const =0
Returns an output node of the graph as a GraphNode. The nodes are ordered in the appearance in the gr...
SmartEngine::IGraph
A graph is a collection of buffers and nodes that together form a neural network. The graph is create...
Definition: Graph.h:61
SmartEngine::IGraph::CopyWeightsFrom
virtual void CopyWeightsFrom(IGraph *graph, float percent=1.0f)=0
Copies the neuron layer weights from the specified graph into this graph. The specified graph must ha...
SmartEngine::IResource
Base class for objects that can be loaded from and saved to disk.
Definition: Resource.h:77
SmartEngine::CreateEmptyGraph
SMARTENGINE_EXPORT ObjectPtr< IGraph > CreateEmptyGraph(const GraphCInfo &cinfo)
Creates a new, empty IGraph instance.
SmartEngine::ComponentInputBinding
Dynamically binds a node to a component input.
Definition: Graph.h:35
SmartEngine::ComponentInputBinding::inputNode
IGraphNode * inputNode
The node to become the input into the component
Definition: Graph.h:44
SmartEngine::IContext
Every node in the AI graph must belong to the same context.
Definition: Context.h:40
SmartEngine::IGraph::GetContext
virtual ObjectPtr< IContext > GetContext() const =0
Returns the context associated with the graph
SmartEngine::IGraph::Instance
virtual ObjectPtr< IGraph > Instance()=0
Creates a copy of this graph with all internal weights referenced to this graph.