-
Notifications
You must be signed in to change notification settings - Fork 1
Lua API
There two main components:
- Graph
- Nodes
Tensors are nodes, operations are nodes. A Graph can contain a single Tensor or an operation.
When using the Lua API, the library should be compiled as a shared object. Must be placed somewhere Lua can find it. Do not import the library yourself unless you know what you are doing. Use the CGraph.lua helper which provides advanced features for ease of graph construction via metatables.
local CGraph = require 'CGraph'CGraph.graph(name, rootNode)-
nameis the desired graph name (used as a filename as well when exporting the Graph to DOT) -
rootNodeis the root node, the first node to be evaluated. - Output: Graph
The result is a Graph object, that overrides the __index metatable.
It contains the following attributes:
-
nameGraph's name -
rootThe root node -
cdataPointer to C API Graph structure -
varsList of defined variables.
And the following methods
-
setVar(name, value)Define the variablenamewith the valuevalue. Value must be a Node. -
getNativeVar(name)Returns the C API Node value of the variablename. Better not to use it. -
getVar(name)Returns the value of the variablename, ornilif it does not exist. -
eval()Evaluates the graph and returns a result object. The result object can either contain a Tensor result or an error result (see below) -
plot()Generates a DOT graph description that can be plotted withgraphviz. -
diff(var, newName)Calculates and returns the derivative of the graph with respect to given variable name. The new graph's name isnewName. -
free()Deallocates all resources allocated by the graph, including nodes! So you should not use same node references between multiple graphs.
When evaluating the graph, you use a non-implemented operator, or run into dimensional issues. In that case, the output of the graph will be an error object that contains:
-
errorAn error enum value -
messageDescription of the issue -
nodeThe node responsible of the issue.
If you plot the graph after running the issue, you can see in the graph's visualization which node is responsible for the error.
Additionally, when plotting the graph, if a variable is bound to a Node (you have called graph:setVar) its value will be plotted as well (similar to the picture above).
There are 3 types of tensors
- Double
- Vector
- Matrix
In CGraph, matrixes are row major.
The create a Tensor use the following functions:
-
CGraph.double(value)Creates a scalar tensor with value =value. -
CGraph.vector(len, value)Creates a Vector with len =lenand value which is an array of doubles =value. -
CGraph.matrix(rows, cols, value)Creates a matrix with rows =rowsand cols =cols. Value is also an array of doubles and#value == rows*colsmust be true.
To create a variable:
CGraph.variable(name)
You can then set its value through its parent graph like graph:setVar('x', CGraph.double(3.14))
CGraph automatically overrides the following metamethods:
-
__unm:-a -
__add:a + b -
__sub:a - b -
__mul:a * b -
__div:a / b -
__pow:a ^ b -
__tostring:print(a)
Additionally, you can use the following functions:
CGraph.powCGraph.dotCGraph.invCGraph.trCGraph.logCGraph.expCGraph.sinCGraph.cosCGraph.tanCGraph.tanh
