2 #include <dlprim/operator.hpp> 23 Net const &operator=(
Net const &) =
delete;
26 Net &operator=(
Net &&) =
default;
88 return keep_intermediate_tensors_;
96 keep_intermediate_tensors_ = keep;
107 std::string
const &name,
108 std::vector<std::string>
const &inputs,
109 std::vector<std::string>
const &outputs,
110 std::vector<std::string>
const ¶meters = std::vector<std::string>(),
131 return shared_resource_;
148 void copy_parameters_to_device();
149 void copy_parameters_to_host();
166 return tensors_diff_;
181 return parameters_diff_;
187 auto p=tensors_.find(name);
188 if(p == tensors_.end())
195 auto p=tensors_diff_.find(name);
196 if(p == tensors_diff_.end())
204 auto p=parameters_.find(name);
205 if(p == parameters_.end())
212 auto p=parameters_diff_.find(name);
213 if(p == parameters_diff_.end())
236 if(
id >= inputs_.size())
238 return tensor(inputs_[
id]);
244 if(
id >= outputs_.size())
246 return tensor(outputs_[
id]);
252 std::unique_ptr<Operator> op;
254 std::vector<std::string> parameter_names;
256 std::vector<Tensor> input_tensors;
257 std::vector<TensorSpecs> input_specs;
260 std::vector<Tensor> output_tensors;
261 std::vector<TensorSpecs> output_specs;
263 std::vector<TensorSpecs> parameter_specs;
264 std::vector<Tensor> parameters;
266 std::vector<TensorAndGradient> in_grad;
267 std::vector<TensorAndGradient> out_grad;
268 std::vector<TensorAndGradient> param_grad;
275 void mark_backpropagating_edges();
276 void allocate_tensors();
277 void allocate_aliases();
278 bool is_loss(std::string
const &name);
279 void allocate_optimized_chunks(
bool forward_only);
280 void tensor_use_list(std::vector<std::list<std::string> > &start,
281 std::vector<std::list<std::string> > &stop);
282 void allocate_chunks();
287 std::map<std::string,TensorSpecs> tensor_specs_;
288 std::map<std::string,TensorSpecs> parameter_specs_;
290 std::vector<Connection> connections_;
291 std::map<std::string,unsigned> connections_index_;
294 std::map<std::string,std::string> alias_sources_;
295 std::map<std::string,Tensor> tensors_;
296 std::map<std::string,Tensor> tensors_diff_;
298 std::map<std::string,Tensor> parameters_;
299 std::map<std::string,Tensor> parameters_diff_;
300 std::map<std::string,float> loss_weights_;
301 std::vector<std::string> inputs_;
302 std::vector<std::string> outputs_;
303 std::vector<Tensor> memory_;
305 std::shared_ptr<SharedResource> shared_resource_;
308 bool keep_intermediate_tensors_;
void save_parameters_to_hdf5(std::string const &fname)
Save all network parameters to a file.
std::map< std::string, Tensor > & tensors()
Get all intermediate tensors (operators inputs/outputs). Note if keep_intermediate_tensors() == false...
Definition: net.hpp:156
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 mark_output_tensor(std::string const &name)
Make a tensor as an output tensor (will be preserverd) by name.
Definition of Tensor without actual memory/object.
Definition: tensor.hpp:11
Tensor & input(unsigned id)
Get input tensors by their numeric id - according to the order in input_names()
Definition: net.hpp:234
void keep_intermediate_tensors(bool keep)
Set if to keed intermediate results for debugging. Default is false - optimise memory use and reuse i...
Definition: net.hpp:94
std::vector< std::string > const & input_names()
Get of all network inputs.
Definition: net.hpp:222
void add_input_tensor(std::string const &name, TensorSpecs const &ts, bool requires_gradient=false)
Define network input.
CalculationsMode mode() const
get currenct calcuations mode
Definition: net.hpp:120
void setup()
Allocate all tensors and prepare network data.
void initialize_parameters(ExecutionContext const &e)
Initialize all parameters for training.
Tensor & output(unsigned id)
Get output tensors by their numeric id - according to the order in output_names() ...
Definition: net.hpp:242
void save_parameters(std::string const &fname)
Save network parameters to DLP format.
CalculationsMode
Operation mode of layers - inference of training.
Definition: definitions.hpp:283
void load_from_json_file(std::string const &name)
Configure network graph from json.
void load_parameters(std::istream &s, bool allow_missing=false)
Load parameters from binary stream DLP format (file) s must be seekable.
void reshape()
reshape all tensors in network after reshaping the input data
Tensor & tensor(std::string const &name)
Get tensor by name, throws ValidationError if does not exist.
Definition: net.hpp:185
This is main object that represent the pair of OpenCL platform and device all other objects use it...
Definition: context.hpp:302
void load_model(ModelBase &model)
Load from external model, shortcut to load_from_json(mode.network()) + setup() + load_parameters(mode...
void load_from_json(json::value const &v)
Configure network graph from json.
std::shared_ptr< SharedResource > shared_resource()
Get SharedResource object that can be shared between all operators in same network allowing operator ...
Definition: net.hpp:129
This class is central representation of json objects.
Definition: json.hpp:652
Tensor & tensor_diff(std::string const &name)
Get tensor gradient by name, throws ValidationError if does not exist.
Definition: net.hpp:193
bool keep_intermediate_tensors() const
True if all intermediate results are kept.
Definition: net.hpp:86
Tensor & param(std::string const &name)
Get parameter by name, throws ValidationError if does not exist.
Definition: net.hpp:202
std::map< std::string, Tensor > & tensor_diffs()
Get all intermediate tensor (operators inputs/outputs) gradient. Note if keep_intermediate_tensors() ...
Definition: net.hpp:164
Thrown in case of invalid parameters.
Definition: definitions.hpp:46
Base class used for loading non-native model formats to dlprimitives.
Definition: model.hpp:11
Net(Context &ctx)
Create an empty network object for a context.
Major object used for inference.
Definition: net.hpp:14
Mane namespace.
Definition: context.hpp:9
Tensor & param_diff(std::string const &name)
Get parameter gradient by name, throws ValidationError if does not exist.
Definition: net.hpp:210
Central Data Contrainer - Tensor.
Definition: tensor.hpp:99
std::vector< std::string > const & output_names()
Get of all network outputs.
Definition: net.hpp:228
std::map< std::string, Tensor > & param_diffs()
All operator parameters gradients trainable and not trainable.
Definition: net.hpp:179
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...
std::map< std::string, Tensor > & params()
All operator parameters trainable and not trainable.
Definition: net.hpp:172
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 ¶meters=std::vector< std::string >(), bool frozen=false)
Add an operator op to the network. name should be unique.
This class is used to pass cl::Events that the kernel should wait for and/or signal event completion...
Definition: context.hpp:121