C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

neural_networks_prediction_model.h
1 /* file: neural_networks_prediction_model.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Implementation of neural network prediction model.
45 //--
46 */
47 
48 #ifndef __NEURAL_NETWORK_PREDICTION_MODEL_H__
49 #define __NEURAL_NETWORK_PREDICTION_MODEL_H__
50 
51 #include "algorithms/algorithm.h"
52 
53 #include "data_management/data/tensor.h"
54 #include "services/daal_defines.h"
55 #include "algorithms/neural_networks/neural_networks_learnable_parameters.h"
56 #include "algorithms/neural_networks/neural_networks_prediction_topology.h"
57 #include "algorithms/neural_networks/layers/layer.h"
58 #include "algorithms/neural_networks/layers/layer_types.h"
59 #include "algorithms/neural_networks/layers/layer_forward.h"
60 
61 #include "algorithms/neural_networks/layers/split/split_layer_forward.h"
62 
63 namespace daal
64 {
65 namespace algorithms
66 {
74 namespace neural_networks
75 {
76 namespace prediction
77 {
78 namespace interface1
79 {
88 class Parameter : public daal::algorithms::Parameter
89 {
90 public:
96  Parameter(size_t batchSize_ = 1, bool allocateWeightsAndBiases_ = false) :
97  batchSize(batchSize_), allocateWeightsAndBiases(allocateWeightsAndBiases_)
98  {}
99 
100  size_t batchSize;
101  bool allocateWeightsAndBiases;
102 };
103 
108 class DAAL_EXPORT Model : public neural_networks::ModelImpl
109 {
110 public:
111  DECLARE_SERIALIZABLE_CAST(Model);
112 
114  Model();
115 
121  static services::SharedPtr<Model> create(services::Status *stat = NULL);
122 
130  Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
131  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel);
132 
141  static services::SharedPtr<Model> create(
142  const neural_networks::ForwardLayersPtr &forwardLayersForModel,
143  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
144  services::Status *stat = NULL);
145 
158  template<typename modelFPType>
159  DAAL_EXPORT Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
160  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
161  modelFPType dummy, bool storeWeightsInTable);
173  template<typename modelFPType>
174  DAAL_EXPORT static services::SharedPtr<Model> create(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
175  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
176  bool storeWeightsInTable, services::Status *stat = NULL);
177 
179  Model(const Model &model);
180 
186  Model(const prediction::Topology &topology);
187 
194  static services::SharedPtr<Model> create(const prediction::Topology &topology, services::Status *stat = NULL);
195 
197  virtual ~Model() {}
198 
206  template<typename modelFPType>
207  services::Status allocate(const services::Collection<size_t> &sampleSize, const daal::algorithms::Parameter *parameter = NULL)
208  {
209  using namespace services;
210  using namespace data_management;
211  using namespace layers;
212 
213  services::Status s;
214 
215  Parameter defaultParameter;
216  const Parameter *par = (parameter ? static_cast<const Parameter *>(parameter) : &defaultParameter);
217 
218  if (_allocatedBatchSize == par->batchSize) { return services::Status(); }
219 
220  size_t nLayers = _forwardLayers->size();
221 
222  _forwardLayers->get(0)->getLayerInput()->set(forward::data, HomogenTensor<modelFPType>::create(sampleSize, Tensor::doAllocate, &s));
223 
224  /* Clear layers' inputs if needed */
225  for (size_t i = 1; i < nLayers; i++)
226  {
227  _forwardLayers->get(i)->getLayerInput()->eraseInputData();
228  }
229 
230  for (size_t i = 0; i < nLayers; i++)
231  {
232  s |= connectForwardLayers(i);
233  }
234  if(!s) return s;
235 
236  bool checkWeightsAndBiasesAlloc = true;
237  s |= createWeightsAndBiases<modelFPType>(checkWeightsAndBiasesAlloc);
238 
239  _allocatedBatchSize = par->batchSize;
240 
241  for(size_t i = 0; i < nLayers; i++)
242  {
243  getLayer(i)->enableResetOnCompute(false);
244  }
245 
246  for(size_t i = 0; i < nLayers; i++)
247  {
248  layers::forward::LayerIfacePtr layer = _forwardLayers->get(i);
249  SharedPtr<split::forward::Batch<float> > splitLayerFloat = dynamicPointerCast<split::forward::Batch<float>, forward::LayerIface>(layer);
250  SharedPtr<split::forward::Batch<double> > splitLayerDouble = dynamicPointerCast<split::forward::Batch<double>, forward::LayerIface>(layer);
251  if(splitLayerFloat.get() || splitLayerDouble.get())
252  {
253  const NextLayers &next = _nextLayers->get(i);
254  for (size_t j = 0; j < next.size(); j++)
255  {
256  layers::forward::LayerIfacePtr nextLayer = _forwardLayers->get(next[j]);
257  nextLayer->getLayerParameter()->allowInplaceComputation = false;
258  }
259  }
260  }
261  return s;
262  }
263 
271  services::Status setLayers(const neural_networks::ForwardLayersPtr &forwardLayers,
272  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers)
273  {
274  _forwardLayers = forwardLayers;
275  _nextLayers = nextLayers;
276  return services::Status();
277  }
278 
283  const neural_networks::ForwardLayersPtr getLayers() const
284  {
285  return _forwardLayers;
286  }
287 
293  const layers::forward::LayerIfacePtr getLayer(size_t index) const
294  {
295  return _forwardLayers->get(index);
296  }
297 
298 protected:
299  size_t _allocatedBatchSize;
301  Model(services::Status &st);
302 
303  Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
304  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
305  services::Status &st);
306 
307  template<typename modelFPType>
308  DAAL_EXPORT Model(const neural_networks::ForwardLayersPtr &forwardLayersForModel,
309  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
310  modelFPType dummy, bool storeWeightsInTable, services::Status &st);
311 
312  Model(const prediction::Topology &topology, services::Status &st);
313 
315  template<typename Archive, bool onDeserialize>
316  services::Status serialImpl(Archive *arch)
317  {
318  return services::Status();
319  }
320 
321  services::Status insertLayer(const layers::forward::LayerDescriptor &layerDescriptor)
322  {
323  layers::forward::LayerIfacePtr forwardLayer = layerDescriptor.layer()->clone();
324  _forwardLayers->insert(layerDescriptor.index(), forwardLayer);
325  _nextLayers->insert(layerDescriptor.index(), layerDescriptor.nextLayers());
326 
327  /* Set prediction stage parameter */
328  forwardLayer->getLayerParameter()->predictionStage = true;
329  return services::Status();
330  }
331 };
332 
333 typedef services::SharedPtr<Model> ModelPtr;
336 } // namespace interface1
337 using interface1::Model;
338 using interface1::ModelPtr;
339 using interface1::Parameter;
340 } // namespace prediction
341 } // namespace neural_networks
342 } // namespace algorithms
343 } //namespace daal
344 
345 #endif
daal::algorithms::neural_networks::prediction::interface1::Parameter::batchSize
size_t batchSize
Definition: neural_networks_prediction_model.h:100
daal::services::interface1::Status
Class that holds the results of API calls. In case of API routine failure it contains the list of err...
Definition: error_handling.h:491
daal
Definition: algorithm_base_common.h:57
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:271
daal::algorithms::neural_networks::prediction::interface1::Topology
Class defining a neural network topology - a set of layers and connection between them - on the predi...
Definition: neural_networks_prediction_topology.h:68
daal::algorithms::neural_networks::prediction::interface1::Model::~Model
virtual ~Model()
Destructor.
Definition: neural_networks_prediction_model.h:197
daal::algorithms::neural_networks::prediction::prediction
Definition: neural_networks_prediction_result.h:79
daal::algorithms::neural_networks::layers::forward::interface1::LayerDescriptor::layer
const forward::LayerIfacePtr & layer() const
Definition: layer_forward_descriptor.h:133
daal::algorithms::neural_networks::prediction::interface1::Parameter
Class representing the parameters of neural network prediction.
Definition: neural_networks_prediction_model.h:88
daal::algorithms::neural_networks::layers::forward::interface1::LayerDescriptor::index
size_t index() const
Definition: layer_forward_descriptor.h:115
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:207
daal_defines.h
daal::algorithms::interface1::Parameter
Base class to represent computation parameters. Algorithm-specific parameters are represented as deri...
Definition: algorithm_types.h:86
daal::services::interface1::SharedPtr
Shared pointer that retains shared ownership of an object through a pointer. Several SharedPtr object...
Definition: daal_shared_ptr.h:187
daal::algorithms::neural_networks::prediction::interface1::Parameter::Parameter
Parameter(size_t batchSize_=1, bool allocateWeightsAndBiases_=false)
Definition: neural_networks_prediction_model.h:96
daal::algorithms::association_rules::data
Definition: apriori_types.h:107
daal::algorithms::neural_networks::prediction::model
Definition: neural_networks_prediction_input.h:90
daal::algorithms::neural_networks::prediction::interface1::Model::getLayer
const layers::forward::LayerIfacePtr getLayer(size_t index) const
Definition: neural_networks_prediction_model.h:293
daal::algorithms::neural_networks::layers::forward::interface1::LayerDescriptor::nextLayers
const NextLayers & nextLayers() const
Definition: layer_forward_descriptor.h:139
daal::algorithms::neural_networks::prediction::interface1::Model::getLayers
const neural_networks::ForwardLayersPtr getLayers() const
Definition: neural_networks_prediction_model.h:283
daal::algorithms::neural_networks::prediction::interface1::Model
Class Model object for the prediction stage of neural network algorithm.
Definition: neural_networks_prediction_model.h:108
daal::algorithms::neural_networks::layers::forward::interface1::LayerDescriptor
Class defining descriptor for layer on forward stage.
Definition: layer_forward_descriptor.h:83
daal::algorithms::neural_networks::interface1::ModelImpl
Class Model object for the prediction stage of neural network algorithm.
Definition: neural_networks_learnable_parameters.h:89
daal::services::interface1::Collection
Class that implements functionality of the Collection container.
Definition: collection.h:69

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