Build nGraph Function

This section illustrates how to construct an nGraph function composed of operations from an available opset. Once created, it can wrap into a CNNNetwork, creating utility for data scientists or app developers to define a deep-learning model in a neutral way that does not depend on existing Deep Learning (DL) frameworks.

Operation Set opsetX integrates a list of nGraph pre-compiled operations that work for this purpose. In other words, opsetX defines a set of operations for building a graph.

For a complete list of operation sets supported by Inference Engine, see Available Operations Sets.

To add custom nGraph operations to an existing CNNNetwork, see the Add Custom nGraph Operations document.

Below you can find examples on to how build ngraph::Function from the opset3 operations:

#include <ngraph/ngraph.hpp>
#include <ngraph/opsets/opset3.hpp>
std::shared_ptr<ngraph::Function> create_simple_function() {
// This example shows how to create ngraph::Function
//
// Parameter--->Multiply--->Add--->Result
// Constant---' /
// Constant---'
// Create opset3::Parameter operation with static shape
auto data = std::make_shared<ngraph::opset3::Parameter>(ngraph::element::f32, ngraph::Shape{3, 1, 2});
auto mul_constant = ngraph::opset3::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {1.5});
auto mul = std::make_shared<ngraph::opset3::Multiply>(data, mul_constant);
auto add_constant = ngraph::opset3::Constant::create(ngraph::element::f32, ngraph::Shape{1}, {0.5});
auto add = std::make_shared<ngraph::opset3::Add>(mul, add_constant);
// Create opset3::Result operation
auto res = std::make_shared<ngraph::opset3::Result>(mul);
// Create nGraph function
return std::make_shared<ngraph::Function>(ngraph::ResultVector{res}, ngraph::ParameterVector{data});
}
Node add(NodeInput left_node, NodeInput right_node, str auto_broadcast="NUMPY", Optional[str] name=None)
std::shared_ptr<ngraph::Function> create_advanced_function() {
// Advanced example with multi output operation
//
// Parameter->Split---0-->Result
// | `--1-->Relu-->Result
// `----2-->Result
auto data = std::make_shared<ngraph::opset3::Parameter>(ngraph::element::f32, ngraph::Shape{1, 3, 64, 64});
// Create Constant for axis value
auto axis_const = ngraph::opset3::Constant::create(ngraph::element::i64, ngraph::Shape{}/*scalar shape*/, {1});
// Create opset3::Split operation that splits input to three slices across 1st dimension
auto split = std::make_shared<ngraph::opset3::Split>(data, axis_const, 3);
// Create opset3::Relu operation that takes 1st Split output as input
auto relu = std::make_shared<ngraph::opset3::Relu>(split->output(1)/*specify explicit output*/);
// Results operations will be created automatically based on provided OutputVector
return std::make_shared<ngraph::Function>(ngraph::OutputVector{split->output(0), relu, split->output(2)}, ngraph::ParameterVector{data});
}
Node relu(NodeInput node, Optional[str] name=None)
Node split(NodeInput data, NodeInput axis, int num_splits, Optional[str] name=None)

To wrap it into a CNNNetwork, use:

CNNNetwork net (ng_function);

See Also