Medial Code Documentation
Loading...
Searching...
No Matches
ApplyKeras.h
1#pragma once
2
3//
4// A simple class to enable:
5// (1) reading a (simple) keras model from a text file (prepared in the training scripts in the print_layers function)
6// (2) Applying it to a given sparse mat, or a line to get the embedding layer (currently always one before the last)
7// (3) serialize/deserialize it via MedSerialize (to enable upper classes put it inside a model)
8//
9// Currently only supports :
10// (1) dense (fully connected, with bias) , with linear or relu or sigmoid activation
11// (2) LeakyReLU activation (typically right AFTER a dense layer)
12// (3) Dropout (nothing to do, may need to multiply by a factor)
13// (4) batch normalization
14// (5) Softmax or Sigmoid
15//
16
18#include <MedSparseMat/MedSparseMat/MedSparseMat.h>
19#include <MedMat/MedMat/MedMat.h>
20#include <External/Eigen/Core>
21
22//===================================================================================================
23
24enum KerasLayerTypes { K_UNKNOWN = 0, K_DENSE, K_LEAKY, K_DROPOUT, K_BN , K_ACTIVATION};
25enum KerasActivations { A_UNKNOWN = 0, A_LINEAR, A_SIGMOID , A_RELU, A_LEAKY, A_SOFTMAX};
26
27//===================================================================================================
29
30public:
31
32 int type = K_UNKNOWN;
33 string name = "";
34
35 // for dense
36 int in_dim = 0;
37 int out_dim = 0;
38 int n_bias = 0;
39 int activation = A_UNKNOWN;
40
41 // bn
42 int dim;
43
44 // dropout
45 float drop_rate;
46
47 // leaky
48 float leaky_alpha;
49
50 // weights (holding also transposed version for ease of use with Eigen)
51 MedMat<float> wgts, twgts;
52
53 // biases
54 vector<float> bias;
55
56 // helpers
57 unordered_map<string, int> name_to_type = { { "dense" , K_DENSE } ,{ "leaky", K_LEAKY } ,{ "dropout" , K_DROPOUT },{ "batch_normalization" , K_BN } , {"activation", K_ACTIVATION} };
58 unordered_map<string, int> name_to_activation = { { "linear" , A_LINEAR } ,{ "relu", A_RELU } ,{ "leaky" , A_LEAKY },{ "sigmoid" , A_SIGMOID }, {"softmax", A_SOFTMAX} };
59
60
61 // appliers for a single sample
62 int apply_sparse(vector<pair<int, float>> &sline, vector<float> &output) const;
63 int apply_sparse(map<int, float> &sline, vector<float> &output) const;
64 int apply(vector<float> &in, vector<float> &out) const;
65 int apply_bn(vector<float> &in, vector<float> &out) const;
66 int apply_activation(vector<float> &in, vector<float> &out) const; // for in place send same vector for in/out
67
68 // appliers for a batch of samples
69 int apply(MedMat<float> &in, MedMat<float>& out) const;
70 int apply_bn(MedMat<float> &in, MedMat<float> &out) const;
71 int apply_activation(MedMat<float> &in, MedMat<float> &out) const; // for in place send same vector for in/out
72
73 // initialization from string
74 int init(map<string, string>& _map);
75
76 ADD_CLASS_NAME(KerasLayer)
77 ADD_SERIALIZATION_FUNCS(type, name, in_dim, out_dim, n_bias, activation, dim, drop_rate, leaky_alpha, wgts, twgts, bias)
78};
79
80
81//===================================================================================================
83
84public:
85 vector<KerasLayer> layers;
86
87 int init_from_text_file(string layers_file);
88
89 int apply_sparse(vector<pair<int, float>> &sline, vector<float> &output, int to_layer) const;
90 int apply_sparse(map<int, float> &sline, vector<float> &output, int to_layer) const;
91 int apply(vector<float>& line, vector<float> &output, int to_layer) const;
92 int apply(vector<float>& line, vector<float> &output) const { return apply(line, output, (int)(layers.size() - 1)); }
93 int apply(MedMat<float> &line, MedMat<float>& output, int to_layer) const;
94 int apply(MedMat<float>& line, MedMat<float> &output) const { return apply(line, output, (int)(layers.size() - 1)); }
95
96 int get_all_embeddings(MedSparseMat &smat, int to_layer, MedMat<float> &emat);
97
98 int get_output_dimension() {
99 if (layers.size() > 0)
100 return layers.back().out_dim;
101 return 0;
102 }
103
104 ADD_CLASS_NAME(ApplyKeras)
106};
107
An Abstract class that can be serialized and written/read from file.
#define ADD_SERIALIZATION_FUNCS(...)
Definition SerializableObject.h:122
#define MEDSERIALIZE_SUPPORT(Type)
Definition SerializableObject.h:108
Definition ApplyKeras.h:82
Definition ApplyKeras.h:28
int init(map< string, string > &_map)
Virtual to init object from parsed fields.
Definition ApplyKeras.cpp:289
Definition MedMat.h:63
Definition MedSparseMat.h:19
Definition SerializableObject.h:32