C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

neural_networks_prediction_model.h
1 /* file: neural_networks_prediction_model.h */
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *******************************************************************************/
17 
18 /*
19 //++
20 // Implementation of neural network prediction model.
21 //--
22 */
23 
24 #ifndef __NEURAL_NETWORK_PREDICTION_MODEL_H__
25 #define __NEURAL_NETWORK_PREDICTION_MODEL_H__
26 
27 #include "algorithms/algorithm.h"
28 
29 #include "data_management/data/tensor.h"
30 #include "services/daal_defines.h"
31 #include "algorithms/neural_networks/neural_networks_learnable_parameters.h"
32 #include "algorithms/neural_networks/neural_networks_prediction_topology.h"
33 #include "algorithms/neural_networks/layers/layer.h"
34 #include "algorithms/neural_networks/layers/layer_types.h"
35 #include "algorithms/neural_networks/layers/layer_forward.h"
36 
37 #include "algorithms/neural_networks/layers/split/split_layer_forward.h"
38 
39 namespace daal
40 {
41 namespace algorithms
42 {
50 namespace neural_networks
51 {
52 namespace prediction
53 {
54 namespace interface1
55 {
65 class Parameter : public daal::algorithms::Parameter
66 {
67 public:
74  Parameter(size_t batchSize_ = 1, bool allocateWeightsAndBiases_ = false) :
75  batchSize(batchSize_), allocateWeightsAndBiases(allocateWeightsAndBiases_)
76  {}
77 
78  size_t batchSize;
79  bool allocateWeightsAndBiases;
80 };
81 
87 class DAAL_EXPORT Model : public neural_networks::ModelImpl
88 {
89 public:
90  DECLARE_SERIALIZABLE_CAST(Model);
91 
92  /*
93  * Default constructor
94  * \DAAL_DEPRECATED
95  */
96  DAAL_DEPRECATED Model();
97 
104  DAAL_DEPRECATED static services::SharedPtr<Model> create(services::Status *stat = NULL);
105 
113  Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
114  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel);
115 
125  DAAL_DEPRECATED static services::SharedPtr<Model> create(
126  const neural_networks::ForwardLayersPtr &forwardLayersForModel,
127  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
128  services::Status *stat = NULL);
129 
142  template<typename modelFPType>
143  DAAL_EXPORT Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
144  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
145  modelFPType dummy, bool storeWeightsInTable);
158  template<typename modelFPType>
159  DAAL_EXPORT DAAL_DEPRECATED static services::SharedPtr<Model> create(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
160  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
161  bool storeWeightsInTable, services::Status *stat = NULL);
162 
163  /*
164  * Copy constructor
165  * \DAAL_DEPRECATED
166  */
167  DAAL_DEPRECATED Model(const Model &model);
168 
174  Model(const prediction::Topology &topology);
175 
183  DAAL_DEPRECATED static services::SharedPtr<Model> create(const prediction::Topology &topology, services::Status *stat = NULL);
184 
185  /*
186  * \brief Destructor
187  * \DAAL_DEPRECATED
188  */
189  virtual ~Model() {}
190 
199  template<typename modelFPType>
200  services::Status allocate(const services::Collection<size_t> &sampleSize, const daal::algorithms::Parameter *parameter = NULL)
201  {
202  using namespace services;
203  using namespace data_management;
204  using namespace layers;
205 
206  services::Status s;
207 
208  Parameter defaultParameter;
209  const Parameter *par = (parameter ? static_cast<const Parameter *>(parameter) : &defaultParameter);
210 
211  if (_allocatedBatchSize == par->batchSize) { return services::Status(); }
212 
213  size_t nLayers = _forwardLayers->size();
214 
215  _forwardLayers->get(0)->getLayerInput()->set(forward::data, HomogenTensor<modelFPType>::create(sampleSize, Tensor::doAllocate, &s));
216 
217  /* Clear layers' inputs if needed */
218  for (size_t i = 1; i < nLayers; i++)
219  {
220  _forwardLayers->get(i)->getLayerInput()->eraseInputData();
221  }
222 
223  for (size_t i = 0; i < nLayers; i++)
224  {
225  s |= connectForwardLayers(i);
226  }
227  if(!s) return s;
228 
229  bool checkWeightsAndBiasesAlloc = true;
230  s |= createWeightsAndBiases<modelFPType>(checkWeightsAndBiasesAlloc);
231 
232  _allocatedBatchSize = par->batchSize;
233 
234  for(size_t i = 0; i < nLayers; i++)
235  {
236  getLayer(i)->enableResetOnCompute(false);
237  }
238 
239  for(size_t i = 0; i < nLayers; i++)
240  {
241  layers::forward::LayerIfacePtr layer = _forwardLayers->get(i);
242  SharedPtr<split::forward::Batch<float> > splitLayerFloat = dynamicPointerCast<split::forward::Batch<float>, forward::LayerIface>(layer);
243  SharedPtr<split::forward::Batch<double> > splitLayerDouble = dynamicPointerCast<split::forward::Batch<double>, forward::LayerIface>(layer);
244  if(splitLayerFloat.get() || splitLayerDouble.get())
245  {
246  const NextLayers &next = _nextLayers->get(i);
247  for (size_t j = 0; j < next.size(); j++)
248  {
249  layers::forward::LayerIfacePtr nextLayer = _forwardLayers->get(next[j]);
250  nextLayer->getLayerParameter()->allowInplaceComputation = false;
251  }
252  }
253  }
254  return s;
255  }
256 
265  DAAL_DEPRECATED services::Status setLayers(const neural_networks::ForwardLayersPtr &forwardLayers,
266  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers)
267  {
268  _forwardLayers = forwardLayers;
269  _nextLayers = nextLayers;
270  return services::Status();
271  }
272 
278  DAAL_DEPRECATED const neural_networks::ForwardLayersPtr getLayers() const
279  {
280  return _forwardLayers;
281  }
282 
289  DAAL_DEPRECATED const layers::forward::LayerIfacePtr getLayer(size_t index) const
290  {
291  return _forwardLayers->get(index);
292  }
293 
294 protected:
295  size_t _allocatedBatchSize;
297  /*
298  * \DAAL_DEPRECATED
299  */
300  DAAL_DEPRECATED Model(services::Status &st);
301 
302  /*
303  * \DAAL_DEPRECATED
304  */
305  DAAL_DEPRECATED Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
306  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
307  services::Status &st);
308 
309  /*
310  * \DAAL_DEPRECATED
311  */
312  template<typename modelFPType>
313  DAAL_EXPORT DAAL_DEPRECATED Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
314  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
315  modelFPType dummy, bool storeWeightsInTable, services::Status &st);
316 
317  /*
318  * \DAAL_DEPRECATED
319  */
320  DAAL_DEPRECATED Model(const prediction::Topology &topology, services::Status &st);
321 
322  /*
323  * \private
324  * \DAAL_DEPRECATED
325  */
326  template<typename Archive, bool onDeserialize>
327  services::Status serialImpl(Archive *arch)
328  {
329  return services::Status();
330  }
331 
332  /*
333  * \DAAL_DEPRECATED
334  */
335  DAAL_DEPRECATED services::Status insertLayer(const layers::forward::LayerDescriptor &layerDescriptor)
336  {
337  layers::forward::LayerIfacePtr forwardLayer = layerDescriptor.layer()->clone();
338  _forwardLayers->insert(layerDescriptor.index(), forwardLayer);
339  _nextLayers->insert(layerDescriptor.index(), layerDescriptor.nextLayers());
340 
341  /* Set prediction stage parameter */
342  forwardLayer->getLayerParameter()->predictionStage = true;
343  return services::Status();
344  }
345 };
346 
347 typedef services::SharedPtr<Model> ModelPtr;
350 } // namespace interface1
351 using interface1::Model;
352 using interface1::ModelPtr;
353 using interface1::Parameter;
354 } // namespace prediction
355 } // namespace neural_networks
356 } // namespace algorithms
357 } //namespace daal
358 
359 #endif
daal::algorithms::neural_networks::prediction::interface1::Model::getLayers
DAAL_DEPRECATED const neural_networks::ForwardLayersPtr getLayers() const
Definition: neural_networks_prediction_model.h:278
daal::algorithms::neural_networks::prediction::interface1::Parameter
Class representing the parameters of neural network prediction.
Definition: neural_networks_prediction_model.h:65
daal::algorithms::association_rules::data
Definition: apriori_types.h:83
daal::algorithms::neural_networks::prediction::interface1::Model::allocate
services::Status allocate(const services::Collection< size_t > &sampleSize, const daal::algorithms::Parameter *parameter=NULL)
Definition: neural_networks_prediction_model.h:200
daal::algorithms::neural_networks::prediction::prediction
Definition: neural_networks_prediction_result.h:55
daal::algorithms::neural_networks::prediction::interface1::Model::setLayers
DAAL_DEPRECATED services::Status setLayers(const neural_networks::ForwardLayersPtr &forwardLayers, const services::SharedPtr< services::Collection< layers::NextLayers > > &nextLayers)
Definition: neural_networks_prediction_model.h:265
daal::algorithms::neural_networks::prediction::interface1::Model
Class Model object for the prediction stage of neural network algorithm.
Definition: neural_networks_prediction_model.h:87
daal_defines.h
daal::algorithms::neural_networks::prediction::interface1::Model::getLayer
DAAL_DEPRECATED const layers::forward::LayerIfacePtr getLayer(size_t index) const
Definition: neural_networks_prediction_model.h:289
daal::algorithms::neural_networks::prediction::interface1::Parameter::Parameter
Parameter(size_t batchSize_=1, bool allocateWeightsAndBiases_=false)
Definition: neural_networks_prediction_model.h:74
daal::algorithms::neural_networks::prediction::model
Definition: neural_networks_prediction_input.h:66
daal::algorithms::neural_networks::prediction::interface1::Parameter::batchSize
size_t batchSize
Definition: neural_networks_prediction_model.h:78

For more complete information about compiler optimizations, see our Optimization Notice.