class nbla::utils::nnp::Network

class Network

Network object associated with Nnp object.

The following code will get Network instance from Nnp object nnp, and set batch size.

shared_ptr<Network> network = nnp.get_network("net1");
network.set_batch_size(64);

The next block will get the references to the variable object in the computation graph by name. The computation graph is built when get_variable(name) is called first time, or first time since the batch size or network topology is changed.

nbla::CgVariablePtr x = network.get_variable("input");
nbla::CgVariablePtr y = network.get_variable("output");

You can set data to a variable by accessing array data by using NNabla C++ interface.

float *data = x->variable()->cast_data_and_get_pointer<float>(
    dl.Context().set_array_class("CpuCachedArray"));
for (int i = 0; i < x->variable()->size(); i++) {
    data[i] = ...;  // Set data
}

The forward propagation of the network can be executed at any variable by calling forward method. The function execution will be propagated from root (input) variables to to the variable.

y->forward(true);

Getting and displaying output are as follows.

const float *out = y->variable()->get_data_pointer<float>(
    dl.Context().set_array_class("CpuCachedArray"));
for (int i = 0; i < y->variable()->size(); i++) {
    std::cout << out[i] << ",";
}
std::cout << std::endl;

Note

The Network instance is created by a class member function Nnp::get_network(). The constructor is hidden, and not called directly by users.

Public Functions

NBLA_API string name () const

Network name.

NBLA_API void set_batch_size (int batch_size)

Set batch size.

Parameters:

batch_size[in] Overwrite the default batch size in nnp file.

NBLA_API int batch_size () const

Get batch size.

Return values:

Batch – size. The if set_batch_size is not previously called, batch size written in nnp file will be returned.

NBLA_API void replace_variable (const string &name, CgVariablePtr variable)

Replace an arbitrary variable in the network with a given variable.

The predecessors of the variable in the networks are discarded, and replaced with the predecessors of the given variable.

Parameters:
  • name[in] Name of variable in the network you are replacing.

  • variable[in] Replaced with this.

NBLA_API CgVariablePtr get_variable (const string &name)

Get a variable by name.

This is usually used to set or get data inside the variable. The construction of a computation graph is invoked by calling this if the graph is not latest or not created.

Parameters:

name[in] Name of variable in the network.

Return values:

Variable – in a computation graph.