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() {
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);
auto res = std::make_shared<ngraph::opset3::Result>(mul);
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() {
auto data = std::make_shared<ngraph::opset3::Parameter>(ngraph::element::f32,
ngraph::Shape{1, 3, 64, 64});
auto axis_const = ngraph::opset3::Constant::create(ngraph::element::i64,
ngraph::Shape{}, {1});
auto split = std::make_shared<ngraph::opset3::Split>(data, axis_const, 3);
auto relu = std::make_shared<ngraph::opset3::Relu>(
split->output(1));
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