Medial Code Documentation
Loading...
Searching...
No Matches
ExternalNN.h
1//
2// ExternalNN predictor is used in cases we train the model in external code such as tensorflow or keras,
3// and then wish to use it as a predictor. We use the ApplyKeras mechanism for that.
4//
5
6//#pragma once
7#ifndef __EXTERNAL_NN_H__
8#define __EXTERNAL_NN_H__
9
12#include <MedEmbed/MedEmbed/ApplyKeras.h>
13
14
16public:
17 // Model
18 ApplyKeras ak;
19 int n_preds = 1;
20
22 string init_file = "";
23
24 // Function
26
27 ~MedExternalNN() {};
28
29 int set_params(map<string, string>& mapper) {
30 for (auto &e : mapper)
31 if (e.first == "init_file") {
32 init_file = e.second;
33 if (ak.init_from_text_file(init_file) < 0)
34 HMTHROW_AND_ERR("ERROR: Failed reading layers file %s\n", init_file.c_str());
35 n_preds = ak.get_output_dimension();
36 //fprintf(stderr, " ===> n_preds is %d\n", n_preds);
37 }
38 return 0;
39 }
40
41 void init_defaults() { init_file = ""; };
42
43 // the following simply initializes 'ak' and 'n_preds' from init_file
44 int external_nn_learn() {
45
46 return 0;
47 };
48
49 // learn simply calls init from file
50 int learn(const MedFeatures& features) { return external_nn_learn(); }
51 int learn(MedMat<float> &x, MedMat<float> &y, const vector<float> &wgts) { return external_nn_learn(); }
52 int Learn(float *x, float *y, int nsamples, int nftrs) { return external_nn_learn(); }
53 int Learn(float *x, float *y, const float *w, int nsamples, int nftrs) { return external_nn_learn(); };
54
55 // predict - we only have the medmat option
56 int Predict(MedMat<float> &x, vector<float> &preds) {
57
58 MedMat<float> res;
59 ak.apply(x, res);
60 res.copy_vec(preds);
61
62 return 0;
63 }
64
65 // following are not implemented and will simply HMTHROW_AND_ERR
66
67 int Predict(float *x, float *&preds, int nsamples, int nftrs) { HMTHROW_AND_ERR("ExternalNN: Predict(float *,...) not implemented, used the MedMat API instead\n"); };
68 int Predict(float *x, float *&preds, int nsamples, int nftrs, int transposed_flag) { HMTHROW_AND_ERR("ExternalNN: Predict(float *,...) not implemented, used the MedMat API instead\n"); };
69
70 int n_preds_per_sample() const { return n_preds; }
71
72 bool predict_single_not_implemented() { return true; }
73
74 ADD_CLASS_NAME(MedExternalNN)
76};
77
79
80
81#endif
Logger.h - allowing logs with more control.
MedAlgo - APIs to different algorithms: Linear Models, RF, GBM, KNN, and more.
@ MODEL_EXTERNAL_NN
to_use: "external_nn" , initialize a neural net using a layers file. creates MedExternalNN
Definition MedAlgo.h:64
#define ADD_SERIALIZATION_FUNCS(...)
Definition SerializableObject.h:122
#define MEDSERIALIZE_SUPPORT(Type)
Definition SerializableObject.h:108
Definition ApplyKeras.h:82
Definition ExternalNN.h:15
int learn(MedMat< float > &x, MedMat< float > &y, const vector< float > &wgts)
MedMat x,y : will transpose/normalize x,y if needed by algorithm The convention is that untransposed ...
Definition ExternalNN.h:51
int n_preds_per_sample() const
Number of predictions per sample. typically 1 - but some models return several per sample (for exampl...
Definition ExternalNN.h:70
bool predict_single_not_implemented()
Prepartion function for fast prediction on single item each time.
Definition ExternalNN.h:72
int Learn(float *x, float *y, const float *w, int nsamples, int nftrs)
Learn should be implemented for each model.
Definition ExternalNN.h:53
string init_file
Parameters.
Definition ExternalNN.h:22
A class for holding features data as a virtual matrix
Definition MedFeatures.h:47
Definition MedMat.h:63
Base Interface for predictor.
Definition MedAlgo.h:78
MedPredictorTypes classifier_type
The Predicotr enum type.
Definition MedAlgo.h:80