OpenNN
Open-source neural networks library
Loading...
Searching...
No Matches
model_expression.h
Go to the documentation of this file.
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// M O D E L E X P R E S S I O N H E A D E R
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#pragma once
10
11#include "variable.h"
12
13namespace opennn
14{
15
16class NeuralNetwork;
17class Layer;
18
21{
22public:
23
26
29
31 string build_expression() const;
32
36 void save(const filesystem::path&, ProgrammingLanguage) const;
37
38private:
39
40 const NeuralNetwork* neural_network = nullptr;
41
42 string get_expression_c() const;
43 string get_expression_python() const;
44 string get_expression_php() const;
45 string get_expression_javascript() const;
46
47 static string get_layer_expression(const Layer&, const vector<string>&, const vector<string>&);
48
49 void emit_c_prelude(ostringstream&) const;
50 void emit_c_activations(ostringstream&, const string& expression) const;
51 void emit_c_calculate_outputs(ostringstream&, const string& expression, const vector<string>& lines, bool has_softmax) const;
52 void emit_c_main(ostringstream&) const;
53
54 void emit_php_prelude(ostringstream&) const;
55 void emit_php_activations(ostringstream&, const string& expression) const;
56 void emit_php_inputs_setup(ostringstream&) const;
57 void emit_php_body(ostringstream&, const vector<string>& lines, bool has_softmax) const;
58 void emit_php_response(ostringstream&) const;
59
60 void emit_python_prelude(ostringstream&) const;
61 void emit_python_class_header(ostringstream&) const;
62 void emit_python_activations(ostringstream&, const string& expression) const;
63 void emit_python_calculate_outputs(ostringstream&, const vector<string>& lines, bool has_softmax) const;
64 void emit_python_batch_and_main(ostringstream&) const;
65
66 void emit_js_prelude(ostringstream&) const;
67 void emit_js_inputs_html(ostringstream&) const;
68 void emit_js_outputs_html(ostringstream&, bool use_category_select) const;
69 void emit_js_runtime(ostringstream&, const string& expression, const vector<string>& lines, bool has_softmax, bool use_category_select) const;
70
71 static vector<string> split_expression_lines(const string&);
72 static void rename_spaced_var_definitions(vector<string>&);
73 static vector<string> prepare_body_lines(const string& expression);
74 static vector<string> fix_names(const vector<string>&, const string& default_prefix);
75 static vector<string> fix_get_expression_outputs(const string&, const vector<string>&, const ProgrammingLanguage&);
76 static void apply_name_mapping(string&, const vector<string>& original, const vector<string>& mapped);
77 static string process_body_line(const string&, const vector<string>& input_names, const vector<string>& fixed_input_names);
78 static string replace_reserved_keywords(const string&);
79
80 struct ActivationBodies
81 {
82 const char* c;
83 const char* javascript;
84 const char* python;
85 const char* php;
86 };
87
88 static const vector<pair<string, ActivationBodies>>& activation_table();
89};
90
91}
92
93// OpenNN: Open Neural Networks Library.
94// Copyright(C) 2005-2026 Artificial Intelligence Techniques, SL.
95// Licensed under the GNU Lesser General Public License v2.1 or later.
Abstract base class for all OpenNN layers; orchestrates operators and shape propagation.
Definition layer.h:106
ProgrammingLanguage
Output language for the generated expression source file.
Definition model_expression.h:25
@ C
Definition model_expression.h:25
@ PHP
Definition model_expression.h:25
@ JavaScript
Definition model_expression.h:25
@ Python
Definition model_expression.h:25
ModelExpression(const NeuralNetwork *)
Builds an expression generator bound to the given trained neural network.
void save(const filesystem::path &, ProgrammingLanguage) const
Writes a self-contained source file implementing the network in the chosen language.
string build_expression() const
Returns a symbolic mathematical expression representing the network's forward pass.
Container of layers forming a feed-forward neural network, with parameter storage and I/O.
Definition neural_network.h:20
Definition adaptive_moment_estimation.h:14