File indexing completed on 2026-07-16 08:34:51
0001
0002 #ifndef RIVET_RivetLWTNN_HH
0003 #define RIVET_RivetLWTNN_HH
0004
0005 #include "Rivet/Tools/RivetPaths.hh"
0006 #include "lwtnn/LightweightNeuralNetwork.hh"
0007 #include "lwtnn/LightweightGraph.hh"
0008 #include "lwtnn/Exceptions.hh"
0009 #include "lwtnn/parse_json.hh"
0010 #include <fstream>
0011
0012 namespace Rivet {
0013 using namespace std;
0014
0015
0016
0017
0018
0019
0020 lwt::JSONConfig readLWTNNConfig(const string& jsonpath) {
0021 ifstream input;
0022 try {
0023
0024
0025 input = std::ifstream(jsonpath);
0026 return lwt::parse_json(input);
0027 } catch (lwt::LightweightNNException &e) {
0028 input.close();
0029 throw IOError("Error loading LWTNN JSON config");
0030 }
0031 }
0032
0033
0034
0035
0036
0037
0038
0039
0040 lwt::GraphConfig readLWTNNGraphConfig(const string& jsonpath) {
0041 ifstream input;
0042 try {
0043
0044
0045 input = std::ifstream(jsonpath);
0046 return lwt::parse_json_graph(input);
0047 } catch (lwt::LightweightNNException &e) {
0048 input.close();
0049 throw IOError("Error loading LWTNN JSON config");
0050 }
0051 }
0052
0053
0054
0055
0056
0057 std::unique_ptr<lwt::LightweightNeuralNetwork> mkLWTNN(const lwt::JSONConfig& jsonconfig) {
0058 try {
0059 return std::make_unique<lwt::LightweightNeuralNetwork>(jsonconfig.inputs, jsonconfig.layers, jsonconfig.outputs);
0060 } catch (lwt::LightweightNNException &e) {
0061 throw IOError("Error initialising from LWTNN JSON config");
0062 }
0063 }
0064
0065
0066
0067
0068
0069
0070 std::unique_ptr<lwt::LightweightGraph> mkGraphLWTNN(const lwt::GraphConfig& graphconfig) {
0071 try {
0072 return std::make_unique<lwt::LightweightGraph>(graphconfig);
0073 } catch (lwt::LightweightNNException &e) {
0074 throw IOError("Error initialising from LWTNN JSON config");
0075 }
0076 }
0077
0078
0079
0080
0081
0082
0083 std::unique_ptr<lwt::LightweightNeuralNetwork> mkLWTNN(const string& jsonpath) {
0084 lwt::JSONConfig config = readLWTNNConfig(jsonpath);
0085 return mkLWTNN(config);
0086 }
0087
0088
0089
0090
0091
0092
0093
0094 std::unique_ptr<lwt::LightweightGraph> mkGraphLWTNN(const string& jsonpath) {
0095 lwt::GraphConfig config = readLWTNNGraphConfig(jsonpath);
0096 return mkGraphLWTNN(config);
0097 }
0098
0099 }
0100
0101 #endif