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

neural_networks_learnable_parameters.h
1 /* file: neural_networks_learnable_parameters.h */
2 /*******************************************************************************
3 * Copyright 2014-2017 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 model.
45 //--
46 */
47 
48 #ifndef __NEURAL_NETWORKS_LEARNABLE_PARAMETERS_H__
49 #define __NEURAL_NETWORKS_LEARNABLE_PARAMETERS_H__
50 
51 #include "algorithms/algorithm.h"
52 #include "algorithms/neural_networks/neural_networks_types.h"
53 
54 #include "data_management/data/homogen_numeric_table.h"
55 #include "data_management/data/homogen_tensor.h"
56 
57 namespace daal
58 {
59 namespace algorithms
60 {
61 namespace neural_networks
62 {
63 namespace interface1
64 {
73 class LearnableParametersIface : public data_management::SerializationIface
74 {
75 public:
76  virtual ~LearnableParametersIface() {}
77 
78  virtual data_management::NumericTablePtr copyToTable() const = 0;
79  virtual data_management::NumericTablePtr copyToTable(size_t idx) const = 0;
80  virtual services::Status copyFromTable(const data_management::NumericTablePtr &table) = 0;
81  virtual services::Status copyFromTable(const data_management::NumericTablePtr &table, size_t idx) = 0;
82 };
83 typedef services::SharedPtr<LearnableParametersIface> LearnableParametersIfacePtr;
84 
89 class DAAL_EXPORT ModelImpl : public daal::algorithms::Model
90 {
91 public:
92  virtual ~ModelImpl() {}
93 
98  const services::SharedPtr<services::Collection<layers::NextLayers> > getNextLayers() const
99  {
100  return _nextLayers;
101  }
102 
109  services::Status setWeightsAndBiases(const data_management::NumericTablePtr &weightsAndBiases)
110  {
111  return _weightsAndBiases->copyFromTable(weightsAndBiases);
112  }
113 
118  const data_management::NumericTablePtr getWeightsAndBiases() const
119  {
120  return _weightsAndBiases->copyToTable();
121  }
122 
123 protected:
124  ModelImpl() :
125  _forwardLayers(new neural_networks::ForwardLayers),
126  _nextLayers(new services::Collection<layers::NextLayers>),
127  _weightsAndBiasesCreated(false), _storeWeightsInTable(false)
128  {}
129 
130  ModelImpl(services::Status &st) :
131  _weightsAndBiasesCreated(false), _storeWeightsInTable(false)
132  {
133  _forwardLayers.reset(new neural_networks::ForwardLayers);
134  if (!_forwardLayers)
135  st.add(services::ErrorMemoryAllocationFailed);
136  _nextLayers.reset(new services::Collection<layers::NextLayers>);
137  if (!_nextLayers)
138  st.add(services::ErrorMemoryAllocationFailed);
139  }
140 
148  ModelImpl(const neural_networks::ForwardLayersPtr &forwardLayers,
149  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers,
150  bool storeWeightsInTable = false) :
151  _forwardLayers(forwardLayers), _nextLayers(nextLayers),
152  _weightsAndBiasesCreated(false), _storeWeightsInTable(storeWeightsInTable) {}
153 
154  ModelImpl(const neural_networks::ForwardLayersPtr &forwardLayers,
155  const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers,
156  bool storeWeightsInTable, services::Status &st) :
157  _forwardLayers(forwardLayers), _nextLayers(nextLayers),
158  _weightsAndBiasesCreated(false), _storeWeightsInTable(storeWeightsInTable) {}
159 
160  ModelImpl(const ModelImpl &model) :
161  _forwardLayers(model._forwardLayers), _nextLayers(model._nextLayers),
162  _storeWeightsInTable(model._storeWeightsInTable),
163  _weightsAndBiasesCreated(model._weightsAndBiasesCreated)/*,
164  _weightsAndBiases(model._weightsAndBiases->clone()) */ {}
165 
167  ModelImpl(const ModelImpl &model, services::Status &st) :
168  _forwardLayers(model._forwardLayers), _nextLayers(model._nextLayers),
169  _storeWeightsInTable(model._storeWeightsInTable),
170  _weightsAndBiasesCreated(model._weightsAndBiasesCreated)/*,
171  _weightsAndBiases(model._weightsAndBiases->clone()) */ {}
172 
173  services::Status checkWeightsAndBiasesAllocation()
174  {
175  using namespace services;
176  using namespace layers;
177 
178  _storeWeightsInTable = true;
179  size_t nLayers = _forwardLayers->size();
180  for (size_t i = 0; i < nLayers; i++)
181  {
182  forward::Input *forwardInput = _forwardLayers->get(i)->getLayerInput();
183  /* Check if weights and biases are allocated by user */
184  if (forwardInput->get(forward::weights) || forwardInput->get(forward::biases))
185  {
186  _storeWeightsInTable = false;
187  break;
188  }
189  }
190  return services::Status();
191  }
192 
193  services::Status connectForwardLayers(size_t layerId)
194  {
195  using namespace services;
196  using namespace layers;
197 
198  forward::LayerIfacePtr forwardLayer = _forwardLayers->get(layerId);
199  forwardLayer->allocateResult();
200  forward::ResultPtr forwardResult = forwardLayer->getLayerResult();
201  const NextLayers &next = _nextLayers->get(layerId);
202  for(size_t j = 0; j < next.size(); j++)
203  {
204  _forwardLayers->get(next[j])->addInput(forwardResult, j, 0 /* index in input object of next[j] forward layer */);
205  }
206  return services::Status();
207  }
208 
209  template<typename modelFPType>
210  DAAL_EXPORT services::Status createWeightsAndBiases(bool checkAllocation);
211 
212  bool _weightsAndBiasesCreated;
213  bool _storeWeightsInTable;
215  neural_networks::ForwardLayersPtr _forwardLayers;
216  services::SharedPtr<services::Collection<layers::NextLayers> > _nextLayers;
217  LearnableParametersIfacePtr _weightsAndBiases;
218 };
220 }
221 using interface1::ModelImpl;
222 using interface1::LearnableParametersIface;
223 using interface1::LearnableParametersIfacePtr;
224 }
225 }
226 }
227 #endif
daal
Definition: algorithm_base_common.h:57
daal::algorithms::neural_networks::interface1::ModelImpl::setWeightsAndBiases
services::Status setWeightsAndBiases(const data_management::NumericTablePtr &weightsAndBiases)
Definition: neural_networks_learnable_parameters.h:109
daal::algorithms::neural_networks::interface1::ForwardLayers
services::Collection< layers::forward::LayerIfacePtr > ForwardLayers
Represents a collection of forward stages of neural network layers.
Definition: neural_networks_types.h:83
daal::algorithms::neural_networks::interface1::ModelImpl::getNextLayers
const services::SharedPtr< services::Collection< layers::NextLayers > > getNextLayers() const
Definition: neural_networks_learnable_parameters.h:98
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:170
daal::algorithms::classifier::prediction::model
Definition: classifier_predict_types.h:92
daal::algorithms::neural_networks::interface1::ModelImpl::_forwardLayers
neural_networks::ForwardLayersPtr _forwardLayers
Definition: neural_networks_learnable_parameters.h:215
daal::algorithms::classifier::training::weights
Definition: classifier_training_types.h:88
daal::algorithms::neural_networks::interface1::ModelImpl::ModelImpl
ModelImpl(const ModelImpl &model, services::Status &st)
Definition: neural_networks_learnable_parameters.h:167
daal::algorithms::neural_networks::interface1::LearnableParametersIface
Learnable parameters for the prediction stage of neural network algorithm.
Definition: neural_networks_learnable_parameters.h:73
daal::algorithms::neural_networks::interface1::ModelImpl::ModelImpl
ModelImpl(const neural_networks::ForwardLayersPtr &forwardLayers, const services::SharedPtr< services::Collection< layers::NextLayers > > &nextLayers, bool storeWeightsInTable=false)
Definition: neural_networks_learnable_parameters.h:148
daal::algorithms::neural_networks::interface1::ModelImpl::getWeightsAndBiases
const data_management::NumericTablePtr getWeightsAndBiases() const
Definition: neural_networks_learnable_parameters.h:118
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::algorithms::neural_networks::interface1::ModelImpl::_storeWeightsInTable
bool _storeWeightsInTable
Definition: neural_networks_learnable_parameters.h:213
daal::algorithms::neural_networks::interface1::ModelImpl::_nextLayers
services::SharedPtr< services::Collection< layers::NextLayers > > _nextLayers
Definition: neural_networks_learnable_parameters.h:216

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