48 #ifndef __NEURAL_NETWORK_PREDICTION_MODEL_H__
49 #define __NEURAL_NETWORK_PREDICTION_MODEL_H__
51 #include "algorithms/algorithm.h"
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"
61 #include "algorithms/neural_networks/layers/split/split_layer_forward.h"
74 namespace neural_networks
88 class Parameter :
public daal::algorithms::Parameter
96 Parameter(
size_t batchSize_ = 1,
bool allocateWeightsAndBiases_ =
false) :
97 batchSize(batchSize_), allocateWeightsAndBiases(allocateWeightsAndBiases_)
101 bool allocateWeightsAndBiases;
108 class DAAL_EXPORT Model :
public neural_networks::ModelImpl
111 DECLARE_SERIALIZABLE_CAST(Model);
121 static services::SharedPtr<Model> create(services::Status *stat = NULL);
130 Model(
const neural_networks::ForwardLayersPtr &forwardLayersForModel,
131 const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel);
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);
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);
179 Model(
const Model &model);
186 Model(
const prediction::Topology &topology);
194 static services::SharedPtr<Model> create(
const prediction::Topology &topology, services::Status *stat = NULL);
206 template<
typename modelFPType>
207 services::Status allocate(
const services::Collection<size_t> &sampleSize,
const daal::algorithms::Parameter *parameter = NULL)
209 using namespace services;
210 using namespace data_management;
211 using namespace layers;
215 Parameter defaultParameter;
216 const Parameter *par = (parameter ?
static_cast<const Parameter *
>(parameter) : &defaultParameter);
218 if (_allocatedBatchSize == par->batchSize) {
return services::Status(); }
220 size_t nLayers = _forwardLayers->size();
222 _forwardLayers->get(0)->getLayerInput()->set(forward::data, HomogenTensor<modelFPType>::create(sampleSize, Tensor::doAllocate, &s));
225 for (
size_t i = 1; i < nLayers; i++)
227 _forwardLayers->get(i)->getLayerInput()->eraseInputData();
230 for (
size_t i = 0; i < nLayers; i++)
232 s |= connectForwardLayers(i);
236 bool checkWeightsAndBiasesAlloc =
true;
237 s |= createWeightsAndBiases<modelFPType>(checkWeightsAndBiasesAlloc);
239 _allocatedBatchSize = par->batchSize;
241 for(
size_t i = 0; i < nLayers; i++)
243 getLayer(i)->enableResetOnCompute(
false);
246 for(
size_t i = 0; i < nLayers; i++)
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())
253 const NextLayers &next = _nextLayers->get(i);
254 for (
size_t j = 0; j < next.size(); j++)
256 layers::forward::LayerIfacePtr nextLayer = _forwardLayers->get(next[j]);
257 nextLayer->getLayerParameter()->allowInplaceComputation =
false;
271 services::Status setLayers(
const neural_networks::ForwardLayersPtr &forwardLayers,
272 const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers)
274 _forwardLayers = forwardLayers;
275 _nextLayers = nextLayers;
276 return services::Status();
283 const neural_networks::ForwardLayersPtr getLayers()
const
285 return _forwardLayers;
293 const layers::forward::LayerIfacePtr getLayer(
size_t index)
const
295 return _forwardLayers->get(index);
299 size_t _allocatedBatchSize;
301 Model(services::Status &st);
303 Model(
const neural_networks::ForwardLayersPtr &forwardLayersForModel,
304 const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
305 services::Status &st);
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);
312 Model(
const prediction::Topology &topology, services::Status &st);
315 template<
typename Archive,
bool onDeserialize>
316 services::Status serialImpl(Archive *arch)
318 return services::Status();
321 services::Status insertLayer(
const layers::forward::LayerDescriptor &layerDescriptor)
323 layers::forward::LayerIfacePtr forwardLayer = layerDescriptor.layer()->clone();
324 _forwardLayers->insert(layerDescriptor.index(), forwardLayer);
325 _nextLayers->insert(layerDescriptor.index(), layerDescriptor.nextLayers());
328 forwardLayer->getLayerParameter()->predictionStage =
true;
329 return services::Status();
333 typedef services::SharedPtr<Model> ModelPtr;
337 using interface1::Model;
338 using interface1::ModelPtr;
339 using interface1::Parameter;
daal::algorithms::neural_networks::prediction::interface1::Parameter::batchSize
size_t batchSize
Definition: neural_networks_prediction_model.h:100
daal::algorithms::neural_networks::prediction::interface1::Model::getLayers
const neural_networks::ForwardLayersPtr getLayers() const
Definition: neural_networks_prediction_model.h:283
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::Model::~Model
virtual ~Model()
Destructor.
Definition: neural_networks_prediction_model.h:197
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::prediction::interface1::Parameter
Class representing the parameters of neural network prediction.
Definition: neural_networks_prediction_model.h:88
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::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::classifier::prediction::prediction
Definition: classifier_predict_types.h:102
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