DLPrimitives
Classes | Public Member Functions | List of all members
dlprim::Net Class Reference

Major object used for inference. More...

#include <include/dlprim/net.hpp>

Public Member Functions

 Net (Context &ctx)
 Create an empty network object for a context.
 
Net const & operator= (Net const &)=delete
 
 Net (Net const &)=delete
 
Netoperator= (Net &&)=default
 
 Net (Net &&)=default
 
void load_model (ModelBase &model)
 Load from external model, shortcut to load_from_json(mode.network()) + setup() + load_parameters(model)
 
void load_from_json (json::value const &v)
 Configure network graph from json.
 
void load_from_json_file (std::string const &name)
 Configure network graph from json.
 
void add_input_tensor (std::string const &name, TensorSpecs const &ts, bool requires_gradient=false)
 Define network input.
 
void mark_output_tensor (std::string const &name)
 Make a tensor as an output tensor (will be preserverd) by name.
 
void set_loss_weight (std::string const &name, float loss_weight=1.0)
 Make a tensor as an output loss tensor, regardless of its name (by default a name starting with "loss") considered loss.
 
void load_parameters (std::istream &s, bool allow_missing=false)
 Load parameters from binary stream DLP format (file) s must be seekable.
 
void load_parameters (std::string const &name, bool allow_missing=false)
 Load parameters from file name can be either DLP or HDF5 format.
 
void load_parameters (ModelBase &model, bool allow_missing=false)
 Load parameters from external model.
 
void save_parameters (std::string const &fname)
 Save network parameters to DLP format.
 
void load_parameters_from_hdf5 (std::string const &fname, bool allow_missing=false)
 Copy all parameters for hdf5 file format. allow_missing - ignore if parameter is not defined.
 
void save_parameters_to_hdf5 (std::string const &fname)
 Save all network parameters to a file.
 
bool keep_intermediate_tensors () const
 True if all intermediate results are kept.
 
void keep_intermediate_tensors (bool keep)
 Set if to keed intermediate results for debugging. Default is false - optimise memory use and reuse intermediate memory chunks.
 
void add_operator (std::unique_ptr< Operator > op, std::string const &name, std::vector< std::string > const &inputs, std::vector< std::string > const &outputs, std::vector< std::string > const &parameters=std::vector< std::string >(), bool frozen=false)
 Add an operator op to the network. name should be unique. More...
 
void mode (CalculationsMode mode)
 Set the calculations mode. More...
 
CalculationsMode mode () const
 get currenct calcuations mode
 
std::shared_ptr< SharedResourceshared_resource ()
 Get SharedResource object that can be shared between all operators in same network allowing operator access to network level features.
 
void setup ()
 Allocate all tensors and prepare network data.
 
void reshape ()
 reshape all tensors in network after reshaping the input data
 
void initialize_parameters (ExecutionContext const &e)
 Initialize all parameters for training.
 
void copy_parameters_to_device ()
 
void copy_parameters_to_host ()
 
void clear_memory ()
 
std::map< std::string, Tensor > & tensors ()
 Get all intermediate tensors (operators inputs/outputs). Note if keep_intermediate_tensors() == false that same memory may be shared between different tensors.
 
std::map< std::string, Tensor > & tensor_diffs ()
 Get all intermediate tensor (operators inputs/outputs) gradient. Note if keep_intermediate_tensors() == false that same memory may be shared between different tensors.
 
std::map< std::string, Tensor > & params ()
 All operator parameters trainable and not trainable.
 
std::map< std::string, Tensor > & param_diffs ()
 All operator parameters gradients trainable and not trainable.
 
Tensortensor (std::string const &name)
 Get tensor by name, throws ValidationError if does not exist.
 
Tensortensor_diff (std::string const &name)
 Get tensor gradient by name, throws ValidationError if does not exist.
 
Tensorparam (std::string const &name)
 Get parameter by name, throws ValidationError if does not exist.
 
Tensorparam_diff (std::string const &name)
 Get parameter gradient by name, throws ValidationError if does not exist.
 
void forward (ExecutionContext const &ectx, bool sync=false)
 
void backward (ExecutionContext const &ectx, bool sync=false)
 
std::vector< std::string > const & input_names ()
 Get of all network inputs.
 
std::vector< std::string > const & output_names ()
 Get of all network outputs.
 
Tensorinput (unsigned id)
 Get input tensors by their numeric id - according to the order in input_names()
 
Tensoroutput (unsigned id)
 Get output tensors by their numeric id - according to the order in output_names()
 

Detailed Description

Major object used for inference.

Member Function Documentation

void dlprim::Net::add_operator ( std::unique_ptr< Operator op,
std::string const &  name,
std::vector< std::string > const &  inputs,
std::vector< std::string > const &  outputs,
std::vector< std::string > const &  parameters = std::vector< std::string >(),
bool  frozen = false 
)

Add an operator op to the network. name should be unique.

inputs - tensor input names - must be defined (as input or output of another operatos) outputs - tensor output names parameters - give cutom names to parameters

Referenced by keep_intermediate_tensors().

void dlprim::Net::mode ( CalculationsMode  mode)

Set the calculations mode.

default is testing. If setup was called when the network in testing mode it can't be switched to training if you plan to use the network for training call it before setup()


The documentation for this class was generated from the following file: