22 #ifndef __NEURAL_NETWORK_PREDICTION_MODEL_H__
23 #define __NEURAL_NETWORK_PREDICTION_MODEL_H__
25 #include "algorithms/algorithm.h"
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"
35 #include "algorithms/neural_networks/layers/split/split_layer_forward.h"
48 namespace neural_networks
62 class Parameter :
public daal::algorithms::Parameter
70 Parameter(
size_t batchSize_ = 1,
bool allocateWeightsAndBiases_ =
false) :
71 batchSize(batchSize_), allocateWeightsAndBiases(allocateWeightsAndBiases_)
75 bool allocateWeightsAndBiases;
82 class DAAL_EXPORT Model :
public neural_networks::ModelImpl
85 DECLARE_SERIALIZABLE_CAST(Model);
95 static services::SharedPtr<Model> create(services::Status *stat = NULL);
104 Model(
const neural_networks::ForwardLayersPtr &forwardLayersForModel,
105 const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel);
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);
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);
153 Model(
const Model &model);
160 Model(
const prediction::Topology &topology);
168 static services::SharedPtr<Model> create(
const prediction::Topology &topology, services::Status *stat = NULL);
180 template<
typename modelFPType>
181 services::Status allocate(
const services::Collection<size_t> &sampleSize,
const daal::algorithms::Parameter *parameter = NULL)
183 using namespace services;
184 using namespace data_management;
185 using namespace layers;
189 Parameter defaultParameter;
190 const Parameter *par = (parameter ?
static_cast<const Parameter *
>(parameter) : &defaultParameter);
192 if (_allocatedBatchSize == par->batchSize) {
return services::Status(); }
194 size_t nLayers = _forwardLayers->size();
196 _forwardLayers->get(0)->getLayerInput()->set(forward::data, HomogenTensor<modelFPType>::create(sampleSize, Tensor::doAllocate, &s));
199 for (
size_t i = 1; i < nLayers; i++)
201 _forwardLayers->get(i)->getLayerInput()->eraseInputData();
204 for (
size_t i = 0; i < nLayers; i++)
206 s |= connectForwardLayers(i);
210 bool checkWeightsAndBiasesAlloc =
true;
211 s |= createWeightsAndBiases<modelFPType>(checkWeightsAndBiasesAlloc);
213 _allocatedBatchSize = par->batchSize;
215 for(
size_t i = 0; i < nLayers; i++)
217 getLayer(i)->enableResetOnCompute(
false);
220 for(
size_t i = 0; i < nLayers; i++)
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())
227 const NextLayers &next = _nextLayers->get(i);
228 for (
size_t j = 0; j < next.size(); j++)
230 layers::forward::LayerIfacePtr nextLayer = _forwardLayers->get(next[j]);
231 nextLayer->getLayerParameter()->allowInplaceComputation =
false;
245 services::Status setLayers(
const neural_networks::ForwardLayersPtr &forwardLayers,
246 const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayers)
248 _forwardLayers = forwardLayers;
249 _nextLayers = nextLayers;
250 return services::Status();
257 const neural_networks::ForwardLayersPtr getLayers()
const
259 return _forwardLayers;
267 const layers::forward::LayerIfacePtr getLayer(
size_t index)
const
269 return _forwardLayers->get(index);
273 size_t _allocatedBatchSize;
275 Model(services::Status &st);
277 Model(
const neural_networks::ForwardLayersPtr &forwardLayersForModel,
278 const services::SharedPtr<services::Collection<layers::NextLayers> > &nextLayersForModel,
279 services::Status &st);
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);
286 Model(
const prediction::Topology &topology, services::Status &st);
289 template<
typename Archive,
bool onDeserialize>
290 services::Status serialImpl(Archive *arch)
292 return services::Status();
295 services::Status insertLayer(
const layers::forward::LayerDescriptor &layerDescriptor)
297 layers::forward::LayerIfacePtr forwardLayer = layerDescriptor.layer()->clone();
298 _forwardLayers->insert(layerDescriptor.index(), forwardLayer);
299 _nextLayers->insert(layerDescriptor.index(), layerDescriptor.nextLayers());
302 forwardLayer->getLayerParameter()->predictionStage =
true;
303 return services::Status();
307 typedef services::SharedPtr<Model> ModelPtr;
311 using interface1::Model;
312 using interface1::ModelPtr;
313 using interface1::Parameter;
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::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