C++ API Reference for Intel® Data Analytics Acceleration Library 2019 Update 5

neural_networks_prediction_model.h
1 /* file: neural_networks_prediction_model.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 /*
17 //++
18 // Implementation of neural network prediction model.
19 //--
20 */
21 
22 #ifndef __NEURAL_NETWORK_PREDICTION_MODEL_H__
23 #define __NEURAL_NETWORK_PREDICTION_MODEL_H__
24 
25 #include "algorithms/algorithm.h"
26 
27 #include "data_management/data/tensor.h"
28 #include "services/daal_defines.h"
29 #include "algorithms/neural_networks/neural_networks_learnable_parameters.h"
30 #include "algorithms/neural_networks/neural_networks_prediction_topology.h"
31 #include "algorithms/neural_networks/layers/layer.h"
32 #include "algorithms/neural_networks/layers/layer_types.h"
33 #include "algorithms/neural_networks/layers/layer_forward.h"
34 
35 #include "algorithms/neural_networks/layers/split/split_layer_forward.h"
36 
37 namespace daal
38 {
39 namespace algorithms
40 {
48 namespace neural_networks
49 {
50 namespace prediction
51 {
52 namespace interface1
53 {
62 class Parameter : public daal::algorithms::Parameter
63 {
64 public:
70  Parameter(size_t batchSize_ = 1, bool allocateWeightsAndBiases_ = false) :
71  batchSize(batchSize_), allocateWeightsAndBiases(allocateWeightsAndBiases_)
72  {}
73 
74  size_t batchSize;
75  bool allocateWeightsAndBiases;
76 };
77 
82 class DAAL_EXPORT Model : public neural_networks::ModelImpl
83 {
84 public:
85  DECLARE_SERIALIZABLE_CAST(Model);
86 
88  Model();
89 
95  static services::SharedPtr<Model> create(services::Status *stat = NULL);
96 
104  Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
105  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel);
106 
115  static services::SharedPtr<Model> create(
116  const neural_networks::ForwardLayersPtr &forwardLayersForModel,
117  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
118  services::Status *stat = NULL);
119 
132  template<typename modelFPType>
133  DAAL_EXPORT Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
134  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
135  modelFPType dummy, bool storeWeightsInTable);
147  template<typename modelFPType>
148  DAAL_EXPORT static services::SharedPtr<Model> create(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
149  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
150  bool storeWeightsInTable, services::Status *stat = NULL);
151 
153  Model(const Model &model);
154 
160  Model(const prediction::Topology &topology);
161 
168  static services::SharedPtr<Model> create(const prediction::Topology &topology, services::Status *stat = NULL);
169 
171  virtual ~Model() {}
172 
180  template<typename modelFPType>
181  services::Status allocate(const services::Collection<size_t> &sampleSize, const daal::algorithms::Parameter *parameter = NULL)
182  {
183  using namespace services;
184  using namespace data_management;
185  using namespace layers;
186 
187  services::Status s;
188 
189  Parameter defaultParameter;
190  const Parameter *par = (parameter ? static_cast<const Parameter *>(parameter) : &defaultParameter);
191 
192  if (_allocatedBatchSize == par->batchSize) { return services::Status(); }
193 
194  size_t nLayers = _forwardLayers->size();
195 
196  _forwardLayers->get(0)->getLayerInput()->set(forward::data, HomogenTensor<modelFPType>::create(sampleSize, Tensor::doAllocate, &s));
197 
198  /* Clear layers' inputs if needed */
199  for (size_t i = 1; i < nLayers; i++)
200  {
201  _forwardLayers->get(i)->getLayerInput()->eraseInputData();
202  }
203 
204  for (size_t i = 0; i < nLayers; i++)
205  {
206  s |= connectForwardLayers(i);
207  }
208  if(!s) return s;
209 
210  bool checkWeightsAndBiasesAlloc = true;
211  s |= createWeightsAndBiases<modelFPType>(checkWeightsAndBiasesAlloc);
212 
213  _allocatedBatchSize = par->batchSize;
214 
215  for(size_t i = 0; i < nLayers; i++)
216  {
217  getLayer(i)->enableResetOnCompute(false);
218  }
219 
220  for(size_t i = 0; i < nLayers; i++)
221  {
222  layers::forward::LayerIfacePtr layer = _forwardLayers->get(i);
223  SharedPtr<split::forward::Batch<float> > splitLayerFloat = dynamicPointerCast<split::forward::Batch<float>, forward::LayerIface>(layer);
224  SharedPtr<split::forward::Batch<double> > splitLayerDouble = dynamicPointerCast<split::forward::Batch<double>, forward::LayerIface>(layer);
225  if(splitLayerFloat.get() || splitLayerDouble.get())
226  {
227  const NextLayers &next = _nextLayers->get(i);
228  for (size_t j = 0; j < next.size(); j++)
229  {
230  layers::forward::LayerIfacePtr nextLayer = _forwardLayers->get(next[j]);
231  nextLayer->getLayerParameter()->allowInplaceComputation = false;
232  }
233  }
234  }
235  return s;
236  }
237 
245  services::Status setLayers(const neural_networks::ForwardLayersPtr &forwardLayers,
246  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers)
247  {
248  _forwardLayers = forwardLayers;
249  _nextLayers = nextLayers;
250  return services::Status();
251  }
252 
257  const neural_networks::ForwardLayersPtr getLayers() const
258  {
259  return _forwardLayers;
260  }
261 
267  const layers::forward::LayerIfacePtr getLayer(size_t index) const
268  {
269  return _forwardLayers->get(index);
270  }
271 
272 protected:
273  size_t _allocatedBatchSize;
275  Model(services::Status &st);
276 
277  Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
278  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
279  services::Status &st);
280 
281  template<typename modelFPType>
282  DAAL_EXPORT Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
283  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
284  modelFPType dummy, bool storeWeightsInTable, services::Status &st);
285 
286  Model(const prediction::Topology &topology, services::Status &st);
287 
289  template<typename Archive, bool onDeserialize>
290  services::Status serialImpl(Archive *arch)
291  {
292  return services::Status();
293  }
294 
295  services::Status insertLayer(const layers::forward::LayerDescriptor &layerDescriptor)
296  {
297  layers::forward::LayerIfacePtr forwardLayer = layerDescriptor.layer()->clone();
298  _forwardLayers->insert(layerDescriptor.index(), forwardLayer);
299  _nextLayers->insert(layerDescriptor.index(), layerDescriptor.nextLayers());
300 
301  /* Set prediction stage parameter */
302  forwardLayer->getLayerParameter()->predictionStage = true;
303  return services::Status();
304  }
305 };
306 
307 typedef services::SharedPtr<Model> ModelPtr;
310 } // namespace interface1
311 using interface1::Model;
312 using interface1::ModelPtr;
313 using interface1::Parameter;
314 } // namespace prediction
315 } // namespace neural_networks
316 } // namespace algorithms
317 } //namespace daal
318 
319 #endif
daal::algorithms::neural_networks::prediction::interface1::Parameter::batchSize
size_t batchSize
Definition: neural_networks_prediction_model.h:74
daal::algorithms::neural_networks::prediction::interface1::Model::getLayers
const neural_networks::ForwardLayersPtr getLayers() const
Definition: neural_networks_prediction_model.h:257
daal
Definition: algorithm_base_common.h:31
daal::algorithms::neural_networks::prediction::interface1::Model::setLayers
services::Status setLayers(const neural_networks::ForwardLayersPtr &forwardLayers, const services::SharedPtr< services::Collection< layers::NextLayers > > &nextLayers)
Definition: neural_networks_prediction_model.h:245
daal::algorithms::neural_networks::prediction::interface1::Model::~Model
virtual ~Model()
Destructor.
Definition: neural_networks_prediction_model.h:171
daal::algorithms::neural_networks::prediction::interface1::Model::getLayer
const layers::forward::LayerIfacePtr getLayer(size_t index) const
Definition: neural_networks_prediction_model.h:267
daal::algorithms::neural_networks::prediction::interface1::Parameter
Class representing the parameters of neural network prediction.
Definition: neural_networks_prediction_model.h:62
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:181
daal_defines.h
daal::algorithms::neural_networks::prediction::interface1::Parameter::Parameter
Parameter(size_t batchSize_=1, bool allocateWeightsAndBiases_=false)
Definition: neural_networks_prediction_model.h:70
daal::algorithms::association_rules::data
Definition: apriori_types.h:81
daal::algorithms::neural_networks::prediction::model
Definition: neural_networks_prediction_input.h:64
daal::algorithms::classifier::prediction::prediction
Definition: classifier_predict_types.h:76
daal::algorithms::neural_networks::prediction::interface1::Model
Class Model object for the prediction stage of neural network algorithm.
Definition: neural_networks_prediction_model.h:82

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